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-sha1-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 testhost true" \
78 # kill the sshd process if it's still running
82 if [ "$RETURN" = "$CODE" ] ; then
83 echo "##### ssh connection test PASSED. returned: $RETURN"
86 echo "##### ssh connection test FAILED. returned: $RETURN, expected: $CODE"
94 trap failed_cleanup EXIT
97 ######################################################################
100 ## set up some variables to ensure that we're operating strictly in
101 ## the tests, not system-wide:
105 # NOTE: /tmp can not be used as the temp dir here, since the
106 # permissions on /tmp are usually such that they will not pass the
107 # monkeysphere/ssh path permission checking. If you need to use a
108 # different location than the current source, please set $TMPDIR
109 # somewhere with tighter permissions.
111 mkdir -p "$TESTDIR"/tmp
112 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
114 # Use the local copy of executables first, instead of system ones.
115 # This should help us test without installing.
116 export PATH="$TESTDIR"/../src:"$PATH"
118 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
119 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
120 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
121 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
123 export MONKEYSPHERE_CHECK_KEYSERVER=false
124 # example.org does not respond to the HKP port, so this should cause
125 # any keyserver connection attempts that do happen (they shouldn't!)
126 # to hang, so we'll notice them:
127 export MONKEYSPHERE_KEYSERVER=example.org
129 export MONKEYSPHERE_LOG_LEVEL=DEBUG
130 export MONKEYSPHERE_CORE_KEYLENGTH=1024
131 export MONKEYSPHERE_PROMPT=false
133 # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
134 # into trying to use the user's key, instead of the testuser's key
135 unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
137 export SSHD_CONFIG="$TEMPDIR"/sshd_config
138 export SOCKET="$TEMPDIR"/ssh-socket
140 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
141 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
142 # $DISPLAY (if one exists) because this test suite should not be doing
143 # *anything* with any running X11 session.
144 export DISPLAY=monkeys
147 ######################################################################
148 ### CONFIGURE ENVIRONMENTS
150 # copy in admin and testuser home to tmp
152 echo "##################################################"
153 echo "### configuring testuser home..."
154 (cd "$TESTDIR"/home && find testuser | cpio -pdu "$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
167 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
168 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
170 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
173 echo "##################################################"
174 echo "### configuring admin home..."
175 (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
179 echo "##################################################"
180 echo "### configuring sshd..."
181 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
182 # write the sshd_config
183 cat <<EOF >> "$SSHD_CONFIG"
184 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
185 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
189 ######################################################################
190 ### SERVER HOST SETUP
194 echo "##################################################"
195 echo "### import host key..."
196 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
197 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
200 echo "##################################################"
201 echo "### getting host key fingerprint..."
202 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
205 # change host key expiration
207 echo "##################################################"
208 echo "### setting host key expiration..."
209 monkeysphere-host set-expire 1
210 # FIXME: how do we check that the expiration has really been set?
212 # certify host key with the "Admin's Key".
213 # (this would normally be done via keyservers)
215 echo "##################################################"
216 echo "### certifying server host key..."
217 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
218 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
220 # FIXME: add revoker?
222 # FIXME: how can we test publish-key without flooding junk into the
225 # FIXME: should we run "diagnostics" here to test setup?
228 ######################################################################
229 ### SERVER AUTHENTICATION SETUP
231 # set up monkeysphere authentication
233 echo "##################################################"
234 echo "### setup monkeysphere authentication..."
235 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
236 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
237 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
239 monkeysphere-authentication setup
240 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
242 # add admin as identity certifier for testhost
244 echo "##################################################"
245 echo "### adding admin as certifier..."
246 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
249 echo "##################################################"
250 echo "### list certifiers..."
251 monkeysphere-authentication list-certifiers
253 # FIXME: should we run "diagnostics" here to test setup?
255 ######################################################################
258 # generate an auth subkey for the test user that expires in 2 days
260 echo "##################################################"
261 echo "### generating key for testuser..."
262 monkeysphere gen-subkey
264 # add server key to testuser keychain
266 echo "##################################################"
267 echo "### export server key to testuser..."
268 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
270 # teach the "server" about the testuser's key
272 echo "##################################################"
273 echo "### export testuser key to server..."
274 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
276 # update authorized_keys for user
278 echo "##################################################"
279 echo "### update server authorized_keys file for this testuser..."
280 monkeysphere-authentication update-users $(whoami)
281 # FIXME: this is maybe not failing properly for:
282 # ms: improper group or other writability on path '/tmp'.
284 ######################################################################
287 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
288 # the identity before connection. This should work in both directions!
290 echo "##################################################"
291 echo "### ssh connection test for success..."
294 # remove the testuser's authorized_user_ids file, update, and make
295 # sure that the ssh authentication FAILS
297 echo "##################################################"
298 echo "### removing testuser authorized_user_ids and updating..."
299 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
300 monkeysphere-authentication update-users $(whoami)
302 echo "##################################################"
303 echo "### ssh connection test for server authentication denial..."
305 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
307 # put improper permissions on authorized_user_ids file, update, and
308 # make sure ssh authentication FAILS
310 echo "##################################################"
311 echo "### setting group writability on authorized_user_ids and updating..."
312 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
313 monkeysphere-authentication update-users $(whoami)
315 echo "##################################################"
316 echo "### ssh connection test for server authentication denial..."
318 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
320 echo "##################################################"
321 echo "### setting other writability on authorized_user_ids and updating..."
322 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
323 monkeysphere-authentication update-users $(whoami)
325 echo "##################################################"
326 echo "### ssh connection test for server authentication denial..."
328 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
329 monkeysphere-authentication update-users $(whoami)
331 # FIXME: addtest: remove admin as id-certifier and check ssh failure
333 # FIXME: addtest: add hostname on host key
334 # FIXME: addtest: revoke hostname on host key and check ssh failure
336 # addtest: revoke the host key and check ssh failure
337 # test to make sure things are OK after the previous tests:
340 echo "##################################################"
341 echo "### ssh connection test for server with revoked key..."
342 # generate the revocation certificate and feed it directly to the test
343 # user's keyring (we're not publishing to the keyservers)
344 monkeysphere-host revoke-key | gpg --import
348 ######################################################################
353 echo "##################################################"
354 echo " Monkeysphere basic tests completed successfully!"
355 echo "##################################################"