3 # Tests to ensure that the monkeysphere is working
5 # unset MONKEYSPHERE_TEST_NO_EXAMINE to examine
8 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
9 # Jameson Rollins <jrollins@fifthhorseman.net>
10 # Micah Anderson <micah@riseup.net>
12 # Copyright: 2008-2009
13 # License: GPL v3 or later
15 # these tests should all be able to run as a non-privileged user.
17 # put all the test output to stdout
19 # all subcommands in this script should complete without failure:
21 # piped commands should return the code of the first non-zero return
24 # make sure the TESTDIR is an absolute path, not a relative one.
25 export TESTDIR=$(cd $(dirname "$0") && pwd)
27 source "$TESTDIR"/common
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; }
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; }
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; }
42 ## FIXME: other checks?
44 ######################################################################
47 # gpg command for test admin user
49 chmod 0700 "$TEMPDIR"/admin
50 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
54 # first argument is expected return code from ssh connection
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 &
67 # wait until the socket is created before continuing
68 while [ ! -S "$SOCKET" ] ; do
72 # make a client connection to the socket
73 echo "##### starting ssh client..."
75 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config ${target_hostname:-testhost} true" \
78 # kill the sshd process if it's still running
79 kill "$SSHD_PID" || true
83 if [ "$RETURN" = "$CODE" ] ; then
84 echo "##### ssh connection test PASSED. returned: $RETURN"
87 echo "##### ssh connection test FAILED. returned: $RETURN, expected: $CODE"
95 trap failed_cleanup EXIT
98 ######################################################################
101 ## set up some variables to ensure that we're operating strictly in
102 ## the tests, not system-wide:
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.
112 mkdir -p "$TESTDIR"/tmp
113 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
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"
119 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
120 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
121 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
122 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
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
130 export MONKEYSPHERE_LOG_LEVEL=DEBUG
131 export MONKEYSPHERE_CORE_KEYLENGTH=1024
132 export MONKEYSPHERE_PROMPT=false
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
138 export SSHD_CONFIG="$TEMPDIR"/sshd_config
139 export SOCKET="$TEMPDIR"/ssh-socket
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
148 ######################################################################
149 ### CONFIGURE ENVIRONMENTS
151 # copy in admin and testuser home to tmp
153 echo "##################################################"
154 echo "### configuring testuser home..."
155 (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
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
168 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
169 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
171 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
174 echo "##################################################"
175 echo "### configuring admin home..."
176 (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
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
190 ######################################################################
191 ### SERVER HOST SETUP
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
201 echo "##################################################"
202 echo "### getting host key fingerprint..."
203 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
206 # change host key expiration
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?
213 # certify host key with the "Admin's Key".
214 # (this would normally be done via keyservers)
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"
221 # FIXME: add revoker?
223 # FIXME: how can we test publish-key without flooding junk into the
226 # FIXME: should we run "diagnostics" here to test setup?
229 ######################################################################
230 ### SERVER AUTHENTICATION SETUP
232 # set up monkeysphere authentication
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"
240 monkeysphere-authentication setup
241 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
243 # add admin as identity certifier for testhost
245 echo "##################################################"
246 echo "### adding admin as certifier..."
247 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
250 echo "##################################################"
251 echo "### list certifiers..."
252 monkeysphere-authentication list-certifiers
254 # FIXME: should we run "diagnostics" here to test setup?
256 ######################################################################
259 # generate an auth subkey for the test user that expires in 2 days
261 echo "##################################################"
262 echo "### generating key for testuser..."
263 monkeysphere gen-subkey
265 # add server key to testuser keychain
267 echo "##################################################"
268 echo "### export server key to testuser..."
269 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
271 # teach the "server" about the testuser's key
273 echo "##################################################"
274 echo "### export testuser key to server..."
275 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
277 # update authorized_keys for user
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'.
285 ######################################################################
288 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
289 # the identity before connection. This should work in both directions!
291 echo "##################################################"
292 echo "### ssh connection test for success..."
295 # remove the testuser's authorized_user_ids file, update, and make
296 # sure that the ssh authentication FAILS
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)
303 echo "##################################################"
304 echo "### ssh connection test for failure..."
306 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
308 # put improper permissions on authorized_user_ids file, update, and
309 # make sure ssh authentication FAILS
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)
316 echo "##################################################"
317 echo "### ssh connection test for failure..."
319 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
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)
326 echo "##################################################"
327 echo "### ssh connection test for failure..."
329 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
330 monkeysphere-authentication update-users $(whoami)
334 echo "##################################################"
335 echo "### setup for symlink tests..."
336 cp -a "$TESTHOME"/.monkeysphere{,.linktest}
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)
345 echo "##################################################"
346 echo "### ssh connection test for success..."
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)
354 echo "##################################################"
355 echo "### ssh connection test for failure..."
357 chmod o-w "$TESTHOME"/.monkeysphere.linktest
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)
364 echo "##################################################"
365 echo "### ssh connection test for success..."
368 echo "##################################################"
369 echo "### create bad permissions on link dir updating..."
370 chmod o+w "$TESTHOME"/.monkeysphere.linktest
371 monkeysphere-authentication update-users $(whoami)
373 echo "##################################################"
374 echo "### ssh connection test for failure..."
376 chmod o-w "$TESTHOME"/.monkeysphere.linktest
377 # FIXME: implement check of link path, and uncomment this test
379 # echo "##################################################"
380 # echo "### create bad permissions on link dir and updating..."
381 # chmod o+w "$TESTHOME"/.monkeysphere
382 # monkeysphere-authentication update-users $(whoami)
384 # echo "##################################################"
385 # echo "### ssh connection test for failure..."
387 # chmod o-w "$TESTHOME"/.monkeysphere
388 rm "$TESTHOME"/.monkeysphere/authorized_user_ids
389 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
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)
398 echo "##################################################"
399 echo "### ssh connection test for success..."
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)
407 echo "##################################################"
408 echo "### ssh connection test for failure..."
410 chmod o-w "$TESTHOME"/.monkeysphere.linktest
412 echo "##################################################"
413 echo "### make .monkeysphere directory a relative symlink and updating..."
414 ln -sfn .monkeysphere{.linktest,}
415 monkeysphere-authentication update-users $(whoami)
417 echo "##################################################"
418 echo "### ssh connection test for success..."
421 echo "##################################################"
422 echo "### create bad permissions on link dir updating..."
423 chmod o+w "$TESTHOME"/.monkeysphere.linktest
424 monkeysphere-authentication update-users $(whoami)
426 echo "##################################################"
427 echo "### ssh connection test for failure..."
429 chmod o-w "$TESTHOME"/.monkeysphere.linktest
430 rm "$TESTHOME"/.monkeysphere
431 mv "$TESTHOME"/.monkeysphere{.bak,}
433 # ensure we're back to normal:
435 echo "##################################################"
436 echo "### making sure we are back to normal..."
437 monkeysphere-authentication update-users $(whoami)
442 echo "##################################################"
443 echo "### ssh connection test directly to 'testhost2' without new name..."
444 target_hostname=testhost2 ssh_test 255
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"
453 echo "##################################################"
454 echo "### ssh connection test with hostname 'testhost2' added..."
455 gpgadmin --export "$HOSTKEYID" | gpg --import
460 echo "##################################################"
461 echo "### ssh connection test directly to 'testhost2' ..."
462 gpg --import <"$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg
464 target_hostname=testhost2 ssh_test
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
472 target_hostname=testhost2 ssh_test 255
474 # FIXME: addtest: remove admin as id-certifier and check ssh failure
476 # FIXME: addtest: how do we test that set-expire makes sense after new
477 # hostnames have been added?
479 # FIXME: addtest: revoke the host key and check ssh failure
481 # test to make sure things are OK after the previous tests:
483 echo "##################################################"
484 echo "### settings reset, updating..."
485 monkeysphere-authentication update-users $(whoami)
487 echo "##################################################"
488 echo "### ssh connection test for success..."
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
498 echo "##################################################"
499 echo "### ssh connection test for failure..."
503 ######################################################################
508 echo "##################################################"
509 echo " Monkeysphere basic tests completed successfully!"
510 echo "##################################################"