add a 'wait' in the test ssh_test function to try to get around the race condition...
[monkeysphere.git] / tests / basic
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4 #
5 # unset MONKEYSPHERE_TEST_NO_EXAMINE to examine
6
7 # Authors: 
8 #   Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 #   Jameson Rollins <jrollins@fifthhorseman.net>
10 #   Micah Anderson <micah@riseup.net> 
11 #
12 # Copyright: 2008-2009
13 # License: GPL v3 or later
14
15 # these tests should all be able to run as a non-privileged user.
16
17 # put all the test output to stdout
18 exec 2>&1
19 # all subcommands in this script should complete without failure:
20 set -e
21 # piped commands should return the code of the first non-zero return
22 set -o pipefail
23
24 # make sure the TESTDIR is an absolute path, not a relative one.
25 export TESTDIR=$(cd $(dirname "$0") && pwd)
26
27 source "$TESTDIR"/common
28
29 ## make sure that the right tools are installed to run the test.  the
30 ## test has *more* requirements than plain ol' monkeysphere:
31 [ -f /usr/sbin/sshd ] || { echo "You must have sshd installed to run this test." ; exit 1; }
32 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
33
34 perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.  
35 On debian-derived systems, you can set this up with:
36   apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
37
38 perl -MDigest::SHA -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA installed to run this test.  
39 On debian-derived systems, you can set this up with:
40   apt-get install libdigest-sha-perl" ; exit 1; }
41
42 ## FIXME: other checks?
43
44 ######################################################################
45 ### FUNCTIONS
46
47 # gpg command for test admin user
48 gpgadmin() {
49     chmod 0700 "$TEMPDIR"/admin
50     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
51 }
52
53 # test ssh connection
54 # first argument is expected return code from ssh connection
55 ssh_test() {
56     local RETURN=0
57
58     umask 0077
59
60     CODE=${1:-0}
61
62     # start the ssh daemon on the socket
63     echo "##### starting ssh server..."
64     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
65     SSHD_PID="$!"
66
67     # wait until the socket is created before continuing
68     while [ ! -S "$SOCKET" ] ; do
69         sleep 1
70     done
71
72     # make a client connection to the socket
73     echo "##### starting ssh client..."
74     ssh-agent bash -c \
75         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config ${target_hostname:-testhost} true" \
76         || RETURN="$?"
77
78     # kill the sshd process if it's still running
79     kill "$SSHD_PID" || true
80     wait
81     SSHD_PID=
82
83     if [ "$RETURN" = "$CODE" ] ; then
84         echo "##### ssh connection test PASSED. returned: $RETURN"
85         return 0
86     else
87         echo "##### ssh connection test FAILED. returned: $RETURN, expected: $CODE"
88         return 1
89     fi
90 }
91
92 SSHD_PID=
93
94 ## setup trap
95 trap failed_cleanup EXIT
96
97
98 ######################################################################
99 ### SETUP VARIABLES
100
101 ## set up some variables to ensure that we're operating strictly in
102 ## the tests, not system-wide:
103
104 # set up temp dir
105
106 # NOTE: /tmp can not be used as the temp dir here, since the
107 # permissions on /tmp are usually such that they will not pass the
108 # monkeysphere/ssh path permission checking.  If you need to use a
109 # different location than the current source, please set $TMPDIR
110 # somewhere with tighter permissions.
111
112 mkdir -p "$TESTDIR"/tmp
113 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
114
115 # Use the local copy of executables first, instead of system ones.
116 # This should help us test without installing.
117 export PATH="$TESTDIR"/../src:"$PATH"
118
119 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
120 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
121 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
122 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
123
124 export MONKEYSPHERE_CHECK_KEYSERVER=false
125 # example.org does not respond to the HKP port, so this should cause
126 # any keyserver connection attempts that do happen (they shouldn't!)
127 # to hang, so we'll notice them:
128 export MONKEYSPHERE_KEYSERVER=example.org
129
130 export MONKEYSPHERE_LOG_LEVEL=DEBUG
131 export MONKEYSPHERE_CORE_KEYLENGTH=1024
132 export MONKEYSPHERE_PROMPT=false
133
134 # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
135 # into trying to use the user's key, instead of the testuser's key
136 unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
137
138 export SSHD_CONFIG="$TEMPDIR"/sshd_config
139 export SOCKET="$TEMPDIR"/ssh-socket
140
141 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
142 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
143 # $DISPLAY (if one exists) because this test suite should not be doing
144 # *anything* with any running X11 session.
145 export DISPLAY=monkeys
146
147
148 ######################################################################
149 ### CONFIGURE ENVIRONMENTS
150
151 # copy in admin and testuser home to tmp
152 echo
153 echo "##################################################"
154 echo "### configuring testuser home..."
155 (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
156
157 # set up environment for testuser
158 export TESTHOME="$TEMPDIR"/testuser
159 export GNUPGHOME="$TESTHOME"/.gnupg
160 chmod 0700 "$GNUPGHOME"
161 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
162 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
163 cat <<EOF >> "$TESTHOME"/.ssh/config
164 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
165 IdentityFile $TESTHOME/.ssh/no-such-identity
166 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
167 EOF
168 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
169 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
170 EOF
171 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
172
173 echo
174 echo "##################################################"
175 echo "### configuring admin home..."
176 (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
177
178 # set up sshd
179 echo
180 echo "##################################################"
181 echo "### configuring sshd..."
182 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
183 # write the sshd_config
184 cat <<EOF >> "$SSHD_CONFIG"
185 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
186 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
187 EOF
188
189
190 ######################################################################
191 ### SERVER HOST SETUP
192
193 # import host key
194 echo
195 echo "##################################################"
196 echo "### import host key..."
197 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
198 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
199
200 echo
201 echo "##################################################"
202 echo "### getting host key fingerprint..."
203 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
204 echo "$HOSTKEYID"
205
206 # change host key expiration
207 echo
208 echo "##################################################"
209 echo "### setting host key expiration..."
210 monkeysphere-host set-expire 1
211 # FIXME: how do we check that the expiration has really been set?
212
213 # certify host key with the "Admin's Key".
214 # (this would normally be done via keyservers)
215 echo
216 echo "##################################################"
217 echo "### certifying server host key..."
218 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
219 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
220
221 # FIXME: add revoker?
222
223 # FIXME: how can we test publish-key without flooding junk into the
224 # keyservers?
225
226 # FIXME: should we run "diagnostics" here to test setup?
227
228
229 ######################################################################
230 ### SERVER AUTHENTICATION SETUP
231
232 # set up monkeysphere authentication
233 echo
234 echo "##################################################"
235 echo "### setup monkeysphere authentication..."
236 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
237 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
238 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
239 EOF
240 monkeysphere-authentication setup
241 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
242
243 # add admin as identity certifier for testhost
244 echo
245 echo "##################################################"
246 echo "### adding admin as certifier..."
247 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
248
249 echo
250 echo "##################################################"
251 echo "### list certifiers..."
252 monkeysphere-authentication list-certifiers
253
254 # FIXME: should we run "diagnostics" here to test setup?
255
256 ######################################################################
257 ### TESTUSER SETUP
258
259 # generate an auth subkey for the test user that expires in 2 days
260 echo
261 echo "##################################################"
262 echo "### generating key for testuser..."
263 monkeysphere gen-subkey
264
265 # add server key to testuser keychain
266 echo
267 echo "##################################################"
268 echo "### export server key to testuser..."
269 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
270
271 # teach the "server" about the testuser's key
272 echo
273 echo "##################################################"
274 echo "### export testuser key to server..."
275 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
276
277 # update authorized_keys for user
278 echo
279 echo "##################################################"
280 echo "### update server authorized_keys file for this testuser..."
281 monkeysphere-authentication update-users $(whoami)
282 # FIXME: this is maybe not failing properly for:
283 # ms: improper group or other writability on path '/tmp'.
284
285 ######################################################################
286 ### TESTS
287
288 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
289 # the identity before connection.  This should work in both directions!
290 echo
291 echo "##################################################"
292 echo "### ssh connection test for success..."
293 ssh_test
294
295 # remove the testuser's authorized_user_ids file, update, and make
296 # sure that the ssh authentication FAILS
297 echo
298 echo "##################################################"
299 echo "### removing testuser authorized_user_ids and updating..."
300 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
301 monkeysphere-authentication update-users $(whoami)
302 echo
303 echo "##################################################"
304 echo "### ssh connection test for failure..."
305 ssh_test 255
306 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
307
308 # put improper permissions on authorized_user_ids file, update, and
309 # make sure ssh authentication FAILS
310 echo
311 echo "##################################################"
312 echo "### setting group writability on authorized_user_ids and updating..."
313 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
314 monkeysphere-authentication update-users $(whoami)
315 echo
316 echo "##################################################"
317 echo "### ssh connection test for failure..."
318 ssh_test 255
319 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
320 echo
321 echo "##################################################"
322 echo "### setting other writability on authorized_user_ids and updating..."
323 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
324 monkeysphere-authentication update-users $(whoami)
325 echo
326 echo "##################################################"
327 echo "### ssh connection test for failure..."
328 ssh_test 255
329 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
330 monkeysphere-authentication update-users $(whoami)
331
332 # test symlinks
333 echo
334 echo "##################################################"
335 echo "### setup for symlink tests..."
336 cp -a "$TESTHOME"/.monkeysphere{,.linktest}
337
338 echo
339 echo "##################################################"
340 echo "### make authorized_user_ids an absolute symlink and updating..."
341 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
342 ln -s "$TESTHOME"/.monkeysphere{.linktest,}/authorized_user_ids
343 monkeysphere-authentication update-users $(whoami)
344 echo
345 echo "##################################################"
346 echo "### ssh connection test for success..."
347 ssh_test
348 echo
349 echo "##################################################"
350 echo "### create bad permissions on link dir and updating..."
351 chmod o+w "$TESTHOME"/.monkeysphere.linktest
352 monkeysphere-authentication update-users $(whoami)
353 echo
354 echo "##################################################"
355 echo "### ssh connection test for failure..."
356 ssh_test 255
357 chmod o-w "$TESTHOME"/.monkeysphere.linktest
358 echo
359 echo "##################################################"
360 echo "### make authorized_user_ids a relative symlink and updating..."
361 ln -sf ../.monkeysphere.linktest/authorized_user_ids "$TESTHOME"/.monkeysphere/authorized_user_ids
362 monkeysphere-authentication update-users $(whoami)
363 echo
364 echo "##################################################"
365 echo "### ssh connection test for success..."
366 ssh_test
367 echo
368 echo "##################################################"
369 echo "### create bad permissions on link dir updating..."
370 chmod o+w "$TESTHOME"/.monkeysphere.linktest
371 monkeysphere-authentication update-users $(whoami)
372 echo
373 echo "##################################################"
374 echo "### ssh connection test for failure..."
375 ssh_test 255
376 chmod o-w "$TESTHOME"/.monkeysphere.linktest
377 # FIXME: implement check of link path, and uncomment this test
378 # echo
379 # echo "##################################################"
380 # echo "### create bad permissions on link dir and updating..."
381 # chmod o+w "$TESTHOME"/.monkeysphere
382 # monkeysphere-authentication update-users $(whoami)
383 # echo
384 # echo "##################################################"
385 # echo "### ssh connection test for failure..."
386 # ssh_test 255
387 # chmod o-w "$TESTHOME"/.monkeysphere
388 rm "$TESTHOME"/.monkeysphere/authorized_user_ids
389 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
390
391 echo
392 echo "##################################################"
393 echo "### make .monkeysphere directory an absolute symlink and updating..."
394 mv "$TESTHOME"/.monkeysphere{,.bak}
395 ln -s "$TESTHOME"/.monkeysphere{.linktest,}
396 monkeysphere-authentication update-users $(whoami)
397 echo
398 echo "##################################################"
399 echo "### ssh connection test for success..."
400 ssh_test
401 echo
402 echo "##################################################"
403 echo "### create bad permissions on link dir and updating..."
404 chmod o+w "$TESTHOME"/.monkeysphere.linktest
405 monkeysphere-authentication update-users $(whoami)
406 echo
407 echo "##################################################"
408 echo "### ssh connection test for failure..."
409 ssh_test 255
410 chmod o-w "$TESTHOME"/.monkeysphere.linktest
411 echo
412 echo "##################################################"
413 echo "### make .monkeysphere directory a relative symlink and updating..."
414 ln -sfn .monkeysphere{.linktest,}
415 monkeysphere-authentication update-users $(whoami)
416 echo
417 echo "##################################################"
418 echo "### ssh connection test for success..."
419 ssh_test
420 echo
421 echo "##################################################"
422 echo "### create bad permissions on link dir updating..."
423 chmod o+w "$TESTHOME"/.monkeysphere.linktest
424 monkeysphere-authentication update-users $(whoami)
425 echo
426 echo "##################################################"
427 echo "### ssh connection test for failure..."
428 ssh_test 255
429 chmod o-w "$TESTHOME"/.monkeysphere.linktest
430 rm "$TESTHOME"/.monkeysphere
431 mv "$TESTHOME"/.monkeysphere{.bak,}
432
433 # ensure we're back to normal:
434 echo
435 echo "##################################################"
436 echo "### making sure we are back to normal..."
437 monkeysphere-authentication update-users $(whoami)
438 ssh_test
439
440
441 echo
442 echo "##################################################"
443 echo "### ssh connection test directly to 'testhost2' without new name..."
444 target_hostname=testhost2 ssh_test 255
445 echo
446 echo "##################################################"
447 echo "### add hostname, certify by admin, import by user..."
448 monkeysphere-host add-hostname testhost2
449 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
450 printf "y\ny\n" | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
451
452 echo
453 echo "##################################################"
454 echo "### ssh connection test with hostname 'testhost2' added..."
455 gpgadmin --export "$HOSTKEYID" | gpg --import
456 gpg --check-trustdb
457 ssh_test
458
459 echo
460 echo "##################################################"
461 echo "### ssh connection test directly to 'testhost2' ..."
462 gpg --import <"$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg 
463 gpg --check-trustdb
464 target_hostname=testhost2 ssh_test
465
466 echo
467 echo "##################################################"
468 echo "### ssh connection test for failure with 'testhost2' revoked..."
469 monkeysphere-host revoke-hostname testhost2
470 gpg --import <"$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg
471 gpg --check-trustdb
472 target_hostname=testhost2 ssh_test 255
473
474 # FIXME: addtest: remove admin as id-certifier and check ssh failure
475
476 # FIXME: addtest: how do we test that set-expire makes sense after new
477 # hostnames have been added?
478
479 # FIXME: addtest: revoke the host key and check ssh failure
480
481 # test to make sure things are OK after the previous tests:
482 echo
483 echo "##################################################"
484 echo "### settings reset, updating..."
485 monkeysphere-authentication update-users $(whoami)
486 echo
487 echo "##################################################"
488 echo "### ssh connection test for success..."
489 ssh_test
490
491 echo
492 echo "##################################################"
493 echo "### revoking host key..."
494 # generate the revocation certificate and feed it directly to the test
495 # user's keyring (we're not publishing to the keyservers)
496 monkeysphere-host revoke-key | gpg --import
497 echo
498 echo "##################################################"
499 echo "### ssh connection test for failure..."
500 ssh_test 255
501
502
503 ######################################################################
504
505 trap - EXIT
506
507 echo
508 echo "##################################################"
509 echo " Monkeysphere basic tests completed successfully!"
510 echo "##################################################"
511
512 cleanup