3 # Tests to ensure that the monkeysphere is working
6 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # Micah Anderson <micah@riseup.net>
10 # Copyright: 2008-2009
11 # License: GPL v3 or later
13 # these tests should all be able to run as a non-privileged user.
15 # put all the test output to stdout
17 # all subcommands in this script should complete without failure:
19 # piped commands should return the code of the first non-zero return
22 # make sure the TESTDIR is an absolute path, not a relative one.
23 export TESTDIR=$(cd $(dirname "$0") && pwd)
25 source "$TESTDIR"/common
27 ## make sure that the right tools are installed to run the test. the
28 ## test has *more* requirements than plain ol' monkeysphere:
29 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
31 perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.
32 On debian-derived systems, you can set this up with:
33 apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
36 perl -MDigest::SHA1 -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA1 installed to run this test.
37 On debian-derived systems, you can set this up with:
38 apt-get install libdigest-sha1-perl" ; exit 1; }
40 ## FIXME: other checks?
42 ######################################################################
45 # gpg command for test admin user
47 chmod 0700 "$TEMPDIR"/admin
48 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
52 # first argument is expected return code from ssh connection
58 # start the ssh daemon on the socket
59 echo "##### starting ssh server..."
60 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
63 # wait until the socket is created before continuing
64 while [ ! -S "$SOCKET" ] ; do
68 # make a client connection to the socket
69 echo "##### starting ssh client..."
71 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true" \
74 # kill the sshd process if it's still running
78 echo "##### return $RETURN"
79 if [ "$RETURN" = "$CODE" ] ; then
80 echo "##### ssh connection test returned as desired"
83 echo "##### ssh connection test failed. expected return code $CODE"
91 trap failed_cleanup EXIT
94 ######################################################################
97 ## set up some variables to ensure that we're operating strictly in
98 ## the tests, not system-wide:
102 # NOTE: /tmp can not be used as the temp dir here, since the
103 # permissions on /tmp are usually such that they will not pass the
104 # monkeysphere/ssh path permission checking. If you need to use a
105 # different location than the current source, please set $TMPDIR
106 # somewhere with tighter permissions.
108 mkdir -p "$TESTDIR"/tmp
109 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
111 # Use the local copy of executables first, instead of system ones.
112 # This should help us test without installing.
113 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
115 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
116 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
117 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
118 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
120 export MONKEYSPHERE_CHECK_KEYSERVER=false
121 # example.org does not respond to the HKP port, so this should cause
122 # any keyserver connection attempts that do happen (they shouldn't!)
123 # to hang, so we'll notice them:
124 export MONKEYSPHERE_KEYSERVER=example.org
126 export MONKEYSPHERE_LOG_LEVEL=DEBUG
127 export MONKEYSPHERE_CORE_KEYLENGTH=1024
128 export MONKEYSPHERE_PROMPT=false
130 export SSHD_CONFIG="$TEMPDIR"/sshd_config
131 export SOCKET="$TEMPDIR"/ssh-socket
133 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
134 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
135 # $DISPLAY (if one exists) because this test suite should not be doing
136 # *anything* with any running X11 session.
137 export DISPLAY=monkeys
139 ## make sure that the version number matches the debian changelog
140 ## (don't bother if this is being run from the tests).
142 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
144 echo "##################################################"
145 echo "### checking version string match..."
146 repver=$(monkeysphere version)
147 debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
148 if [ "$repver" = "$debver" ] ; then
149 echo "Versions match!"
151 printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
156 ######################################################################
157 ### CONFIGURE ENVIRONMENTS
159 # copy in admin and testuser home to tmp
161 echo "##################################################"
162 echo "### configuring testuser home..."
163 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
164 # set up environment for testuser
165 export TESTHOME="$TEMPDIR"/testuser
166 export GNUPGHOME="$TESTHOME"/.gnupg
167 chmod 0700 "$GNUPGHOME"
168 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
169 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
170 cat <<EOF >> "$TESTHOME"/.ssh/config
171 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
172 IdentityFile $TESTHOME/.ssh/no-such-identity
173 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
175 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
176 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
178 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
181 echo "##################################################"
182 echo "### configuring admin home..."
183 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
187 echo "##################################################"
188 echo "### configuring sshd..."
189 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
190 # write the sshd_config
191 cat <<EOF >> "$SSHD_CONFIG"
192 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
193 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
197 ######################################################################
198 ### SERVER HOST SETUP
202 echo "##################################################"
203 echo "### import host key..."
204 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
205 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
208 echo "##################################################"
209 echo "### getting host key fingerprint..."
210 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
213 # change host key expiration
215 echo "##################################################"
216 echo "### setting host key expiration..."
217 monkeysphere-host set-expire 1
218 # FIXME: how do we check that the expiration has really been set?
220 # certify host key with the "Admin's Key".
221 # (this would normally be done via keyservers)
223 echo "##################################################"
224 echo "### certifying server host key..."
225 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
226 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
228 # FIXME: add revoker?
230 # FIXME: how can we test publish-key without flooding junk into the
233 # FIXME: should we run "diagnostics" here to test setup?
236 ######################################################################
237 ### SERVER AUTHENTICATION SETUP
239 # set up monkeysphere authentication
241 echo "##################################################"
242 echo "### setup monkeysphere authentication..."
243 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
244 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
245 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
247 monkeysphere-authentication setup
248 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
250 # add admin as identity certifier for testhost
252 echo "##################################################"
253 echo "### adding admin as certifier..."
254 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
257 echo "##################################################"
258 echo "### list certifiers..."
259 monkeysphere-authentication list-certifiers
261 # FIXME: should we run "diagnostics" here to test setup?
263 ######################################################################
266 # generate an auth subkey for the test user that expires in 2 days
268 echo "##################################################"
269 echo "### generating key for testuser..."
270 monkeysphere gen-subkey
272 # add server key to testuser keychain
274 echo "##################################################"
275 echo "### export server key to testuser..."
276 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
278 # teach the "server" about the testuser's key
280 echo "##################################################"
281 echo "### export testuser key to server..."
282 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
284 # update authorized_keys for user
286 echo "##################################################"
287 echo "### update server authorized_keys file for this testuser..."
288 monkeysphere-authentication update-users $(whoami)
289 # FIXME: this is maybe not failing properly for:
290 # ms: improper group or other writability on path '/tmp'.
293 ######################################################################
296 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
297 # the identity before connection. This should work in both directions!
299 echo "##################################################"
300 echo "### ssh connection test for success..."
303 # remove the testuser's authorized_user_ids file, update, and make
304 # sure that the ssh authentication FAILS
306 echo "##################################################"
307 echo "### removing testuser authorized_user_ids and updating..."
308 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
309 monkeysphere-authentication update-users $(whoami)
311 echo "##################################################"
312 echo "### ssh connection test for server authentication denial..."
314 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
316 # put improper permissions on authorized_user_ids file, update, and
317 # make sure ssh authentication FAILS
319 echo "##################################################"
320 echo "### setting group writability on authorized_user_ids and updating..."
321 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
322 monkeysphere-authentication update-users $(whoami)
324 echo "##################################################"
325 echo "### ssh connection test for server authentication denial..."
327 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
329 echo "##################################################"
330 echo "### setting other writability on authorized_user_ids and updating..."
331 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
332 monkeysphere-authentication update-users $(whoami)
334 echo "##################################################"
335 echo "### ssh connection test for server authentication denial..."
337 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
338 monkeysphere-authentication update-users $(whoami)
340 # FIXME: addtest: remove admin as id-certifier and check ssh failure
342 # FIXME: addtest: add hostname on host key
343 # FIXME: addtest: revoke hostname on host key and check ssh failure
345 # addtest: revoke the host key and check ssh failure
346 # test to make sure things are OK after the previous tests:
349 echo "##################################################"
350 echo "### ssh connection test for server with revoked key..."
351 # generate the revocation certificate and feed it directly to the test
352 # user's keyring (we're not publishing to the keyservers)
353 monkeysphere-host revoke-key | gpg --import
357 ######################################################################
362 echo "##################################################"
363 echo " Monkeysphere basic tests completed successfully!"
364 echo "##################################################"