fd4f6736059f684543ae61241d63accf4c7a6735
[monkeysphere.git] / tests / basic
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4
5 # Authors: 
6 #   Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 #   Jameson Rollins <jrollins@fifthhorseman.net>
8 #   Micah Anderson <micah@riseup.net> 
9 #
10 # Copyright: 2008-2009
11 # License: GPL v3 or later
12
13 # these tests should all be able to run as a non-privileged user.
14
15 # all subcommands in this script should complete without failure:
16 set -e
17 # piped commands should return the code of the first non-zero return
18 set -o pipefail
19
20 # make sure the TESTDIR is an absolute path, not a relative one.
21 export TESTDIR=$(cd $(dirname "$0") && pwd)
22
23 source "$TESTDIR"/common
24
25 ## make sure that the right tools are installed to run the test.  the
26 ## test has *more* requirements than plain ol' monkeysphere:
27 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
28
29
30 ## FIXME: other checks?
31
32 ######################################################################
33 ### FUNCTIONS
34
35 # gpg command for test admin user
36 gpgadmin() {
37     chmod 0700 "$TEMPDIR"/admin
38     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
39 }
40
41 # test ssh connection
42 # first argument is expected return code from ssh connection
43 ssh_test() {
44     umask 0077
45
46     CODE=${1:-0}
47
48     # start the ssh daemon on the socket
49     echo "##### starting ssh server..."
50     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
51     SSHD_PID="$!"
52
53     # wait until the socket is created before continuing
54     while [ ! -S "$SOCKET" ] ; do
55         sleep 1
56     done
57
58     set +e
59
60     # make a client connection to the socket
61     echo "##### starting ssh client..."
62     ssh-agent bash -c \
63         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
64     RETURN="$?"
65
66     # kill the sshd process if it's still running
67     kill "$SSHD_PID"
68     SSHD_PID=
69
70     set -e
71
72     echo "##### return $RETURN"
73     if [ "$RETURN" = "$CODE" ] ; then
74         echo "##### ssh connection test returned as desired"
75         return 0
76     else
77         echo "##### ssh connection test failed.  expected return code $CODE"
78         return 1
79     fi
80 }
81
82 SSHD_PID=
83
84 ## setup trap
85 trap failed_cleanup EXIT
86
87
88 ######################################################################
89 ### SETUP VARIABLES
90
91 ## set up some variables to ensure that we're operating strictly in
92 ## the tests, not system-wide:
93
94 # set up temp dir
95
96 # NOTE: /tmp can not be used as the temp dir here, since the
97 # permissions on /tmp are usually such that they will not pass the
98 # monkeysphere/ssh path permission checking.  If you need to use a
99 # different location than the current source, please set $TMPDIR
100 # somewhere with tighter permissions.
101
102 mkdir -p "$TESTDIR"/tmp
103 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
104
105 # Use the local copy of executables first, instead of system ones.
106 # This should help us test without installing.
107 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
108
109 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
110 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
111 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
112 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
113
114 export MONKEYSPHERE_CHECK_KEYSERVER=false
115 # example.org does not respond to the HKP port, so this should cause
116 # any keyserver connection attempts that do happen (they shouldn't!)
117 # to hang, so we'll notice them:
118 export MONKEYSPHERE_KEYSERVER=example.org
119
120 export MONKEYSPHERE_LOG_LEVEL=DEBUG
121 export MONKEYSPHERE_CORE_KEYLENGTH=1024
122 export MONKEYSPHERE_PROMPT=false
123
124 export SSHD_CONFIG="$TEMPDIR"/sshd_config
125 export SOCKET="$TEMPDIR"/ssh-socket
126
127 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
128 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
129 # $DISPLAY (if one exists) because this test suite should not be doing
130 # *anything* with any running X11 session.
131 export DISPLAY=monkeys
132
133 ## make sure that the version number matches the debian changelog
134 ## (don't bother if this is being run from the tests).
135
136 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
137     echo "##################################################"
138     echo "### checking version string match..."
139     repver=$(monkeysphere version)
140     debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
141     if [ "$repver" = "$debver" ] ; then
142         echo "Versions match!"
143     else
144         printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
145         exit 1
146     fi
147 fi
148
149 ######################################################################
150 ### CONFIGURE ENVIRONMENTS
151
152 # copy in admin and testuser home to tmp
153 echo "##################################################"
154 echo "### configuring testuser home..."
155 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
156 # set up environment for testuser
157 export TESTHOME="$TEMPDIR"/testuser
158 export GNUPGHOME="$TESTHOME"/.gnupg
159 chmod 0700 "$GNUPGHOME"
160 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
161 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
162 cat <<EOF >> "$TESTHOME"/.ssh/config
163 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
164 IdentityFile $TESTHOME/.ssh/no-such-identity
165 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
166 EOF
167 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
168 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
169 EOF
170 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
171
172 echo "##################################################"
173 echo "### configuring admin home..."
174 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
175
176 # set up sshd
177 echo "##################################################"
178 echo "### configuring sshd..."
179 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
180 # write the sshd_config
181 cat <<EOF >> "$SSHD_CONFIG"
182 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
183 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
184 EOF
185
186
187 ######################################################################
188 ### SERVER HOST SETUP
189
190 # import host key
191 echo "##################################################"
192 echo "### import host key..."
193 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
194 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
195
196 echo "##################################################"
197 echo "### getting host key fingerprint..."
198 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
199 echo "$HOSTKEYID"
200
201 # change host key expiration
202 echo "##################################################"
203 echo "### setting host key expiration..."
204 monkeysphere-host set-expire 1
205 # FIXME: how do we check that the expiration has really been set?
206
207 # certify host key with the "Admin's Key".
208 # (this would normally be done via keyservers)
209 echo "##################################################"
210 echo "### certifying server host key..."
211 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
212 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
213
214 # FIXME: add revoker?
215
216 # FIXME: how can we test publish-key without flooding junk into the
217 # keyservers?
218
219 # FIXME: should we run "diagnostics" here to test setup?
220
221
222 ######################################################################
223 ### SERVER AUTHENTICATION SETUP
224
225 # set up monkeysphere authentication
226 echo "##################################################"
227 echo "### setup monkeysphere authentication..."
228 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
229 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
230 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
231 EOF
232 monkeysphere-authentication setup
233 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
234
235 # add admin as identity certifier for testhost
236 echo "##################################################"
237 echo "### adding admin as certifier..."
238 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
239
240 echo "##################################################"
241 echo "### list certifiers..."
242 monkeysphere-authentication list-certifiers
243
244 # FIXME: should we run "diagnostics" here to test setup?
245
246 ######################################################################
247 ### TESTUSER SETUP
248
249 # generate an auth subkey for the test user that expires in 2 days
250 echo "##################################################"
251 echo "### generating key for testuser..."
252 monkeysphere gen-subkey
253
254 # add server key to testuser keychain
255 echo "##################################################"
256 echo "### export server key to testuser..."
257 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
258
259 # teach the "server" about the testuser's key
260 echo "##################################################"
261 echo "### export testuser key to server..."
262 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
263
264 # update authorized_keys for user
265 echo "##################################################"
266 echo "### update server authorized_keys file for this testuser..."
267 monkeysphere-authentication update-users $(whoami)
268 # FIXME: this is maybe not failing properly for:
269 # ms: improper group or other writability on path '/tmp'.
270
271
272 ######################################################################
273 ### TESTS
274
275 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
276 # the identity before connection.  This should work in both directions!
277 echo "##################################################"
278 echo "### ssh connection test for success..."
279 ssh_test
280
281 # remove the testuser's authorized_user_ids file, update, and make
282 # sure that the ssh authentication FAILS
283 echo "##################################################"
284 echo "### removing testuser authorized_user_ids and updating..."
285 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
286 monkeysphere-authentication update-users $(whoami)
287 echo "##################################################"
288 echo "### ssh connection test for server authentication denial..."
289 ssh_test 255
290 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
291
292 # put improper permissions on authorized_user_ids file, update, and
293 # make sure ssh authentication FAILS
294 echo "##################################################"
295 echo "### setting group writability on authorized_user_ids and updating..."
296 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
297 monkeysphere-authentication update-users $(whoami)
298 echo "##################################################"
299 echo "### ssh connection test for server authentication denial..."
300 ssh_test 255
301 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
302 echo "##################################################"
303 echo "### setting other writability on authorized_user_ids and updating..."
304 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
305 monkeysphere-authentication update-users $(whoami)
306 echo "##################################################"
307 echo "### ssh connection test for server authentication denial..."
308 ssh_test 255
309 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
310 monkeysphere-authentication update-users $(whoami)
311
312 # FIXME: addtest: remove admin as id-certifier and check ssh failure
313
314 # FIXME: addtest: add hostname on host key
315 # FIXME: addtest: revoke hostname on host key and check ssh failure
316
317 # addtest: revoke the host key and check ssh failure
318 # test to make sure things are OK after the previous tests:
319 ssh_test
320 echo "##################################################"
321 echo "### ssh connection test for server with revoked key..."
322 # generate the revocation certificate and feed it directly to the test
323 # user's keyring (we're not publishing to the keyservers)
324 monkeysphere-host revoke-key | gpg --import
325 ssh_test 255
326
327
328 ######################################################################
329
330 trap - EXIT
331
332 echo "##################################################"
333 echo " Monkeysphere basic tests completed successfully!"
334 echo "##################################################"
335
336 cleanup