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
60 # start the ssh daemon on the socket
61 echo "##### starting ssh server..."
62 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
65 # wait until the socket is created before continuing
66 while [ ! -S "$SOCKET" ] ; do
70 # make a client connection to the socket
71 echo "##### starting ssh client..."
73 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true" \
76 # kill the sshd process if it's still running
80 echo "##### return $RETURN"
81 if [ "$RETURN" = "$CODE" ] ; then
82 echo "##### ssh connection test returned as desired"
85 echo "##### ssh connection test failed. expected return code $CODE"
93 trap failed_cleanup EXIT
96 ######################################################################
99 ## set up some variables to ensure that we're operating strictly in
100 ## the tests, not system-wide:
104 # NOTE: /tmp can not be used as the temp dir here, since the
105 # permissions on /tmp are usually such that they will not pass the
106 # monkeysphere/ssh path permission checking. If you need to use a
107 # different location than the current source, please set $TMPDIR
108 # somewhere with tighter permissions.
110 mkdir -p "$TESTDIR"/tmp
111 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
113 # Use the local copy of executables first, instead of system ones.
114 # This should help us test without installing.
115 export PATH="$TESTDIR"/../src:"$PATH"
117 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
118 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
119 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
120 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
122 export MONKEYSPHERE_CHECK_KEYSERVER=false
123 # example.org does not respond to the HKP port, so this should cause
124 # any keyserver connection attempts that do happen (they shouldn't!)
125 # to hang, so we'll notice them:
126 export MONKEYSPHERE_KEYSERVER=example.org
128 export MONKEYSPHERE_LOG_LEVEL=DEBUG
129 export MONKEYSPHERE_CORE_KEYLENGTH=1024
130 export MONKEYSPHERE_PROMPT=false
132 # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
133 # into trying to use the user's key, instead of the testuser's key
134 unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
136 export SSHD_CONFIG="$TEMPDIR"/sshd_config
137 export SOCKET="$TEMPDIR"/ssh-socket
139 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
140 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
141 # $DISPLAY (if one exists) because this test suite should not be doing
142 # *anything* with any running X11 session.
143 export DISPLAY=monkeys
146 ######################################################################
147 ### CONFIGURE ENVIRONMENTS
149 # copy in admin and testuser home to tmp
151 echo "##################################################"
152 echo "### configuring testuser home..."
153 (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
155 # set up environment for testuser
156 export TESTHOME="$TEMPDIR"/testuser
157 export GNUPGHOME="$TESTHOME"/.gnupg
158 chmod 0700 "$GNUPGHOME"
159 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
160 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
161 cat <<EOF >> "$TESTHOME"/.ssh/config
162 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
163 IdentityFile $TESTHOME/.ssh/no-such-identity
164 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
166 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
167 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
169 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
172 echo "##################################################"
173 echo "### configuring admin home..."
174 (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
178 echo "##################################################"
179 echo "### configuring sshd..."
180 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
181 # write the sshd_config
182 cat <<EOF >> "$SSHD_CONFIG"
183 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
184 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
188 ######################################################################
189 ### SERVER HOST SETUP
193 echo "##################################################"
194 echo "### import host key..."
195 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
196 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
199 echo "##################################################"
200 echo "### getting host key fingerprint..."
201 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
204 # change host key expiration
206 echo "##################################################"
207 echo "### setting host key expiration..."
208 monkeysphere-host set-expire 1
209 # FIXME: how do we check that the expiration has really been set?
211 # certify host key with the "Admin's Key".
212 # (this would normally be done via keyservers)
214 echo "##################################################"
215 echo "### certifying server host key..."
216 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
217 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
219 # FIXME: add revoker?
221 # FIXME: how can we test publish-key without flooding junk into the
224 # FIXME: should we run "diagnostics" here to test setup?
227 ######################################################################
228 ### SERVER AUTHENTICATION SETUP
230 # set up monkeysphere authentication
232 echo "##################################################"
233 echo "### setup monkeysphere authentication..."
234 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
235 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
236 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
238 monkeysphere-authentication setup
239 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
241 # add admin as identity certifier for testhost
243 echo "##################################################"
244 echo "### adding admin as certifier..."
245 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
248 echo "##################################################"
249 echo "### list certifiers..."
250 monkeysphere-authentication list-certifiers
252 # FIXME: should we run "diagnostics" here to test setup?
254 ######################################################################
257 # generate an auth subkey for the test user that expires in 2 days
259 echo "##################################################"
260 echo "### generating key for testuser..."
261 monkeysphere gen-subkey
263 # add server key to testuser keychain
265 echo "##################################################"
266 echo "### export server key to testuser..."
267 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
269 # teach the "server" about the testuser's key
271 echo "##################################################"
272 echo "### export testuser key to server..."
273 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
275 # update authorized_keys for user
277 echo "##################################################"
278 echo "### update server authorized_keys file for this testuser..."
279 monkeysphere-authentication update-users $(whoami)
280 # FIXME: this is maybe not failing properly for:
281 # ms: improper group or other writability on path '/tmp'.
283 ######################################################################
286 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
287 # the identity before connection. This should work in both directions!
289 echo "##################################################"
290 echo "### ssh connection test for success..."
293 # remove the testuser's authorized_user_ids file, update, and make
294 # sure that the ssh authentication FAILS
296 echo "##################################################"
297 echo "### removing testuser authorized_user_ids and updating..."
298 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
299 monkeysphere-authentication update-users $(whoami)
301 echo "##################################################"
302 echo "### ssh connection test for server authentication denial..."
304 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
306 # put improper permissions on authorized_user_ids file, update, and
307 # make sure ssh authentication FAILS
309 echo "##################################################"
310 echo "### setting group writability on authorized_user_ids and updating..."
311 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
312 monkeysphere-authentication update-users $(whoami)
314 echo "##################################################"
315 echo "### ssh connection test for server authentication denial..."
317 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
319 echo "##################################################"
320 echo "### setting other writability on authorized_user_ids and updating..."
321 chmod o+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 o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
328 monkeysphere-authentication update-users $(whoami)
330 # FIXME: addtest: remove admin as id-certifier and check ssh failure
332 # FIXME: addtest: add hostname on host key
333 # FIXME: addtest: revoke hostname on host key and check ssh failure
335 # addtest: revoke the host key and check ssh failure
336 # test to make sure things are OK after the previous tests:
339 echo "##################################################"
340 echo "### ssh connection test for server with revoked key..."
341 # generate the revocation certificate and feed it directly to the test
342 # user's keyring (we're not publishing to the keyservers)
343 monkeysphere-host revoke-key | gpg --import
347 ######################################################################
352 echo "##################################################"
353 echo " Monkeysphere basic tests completed successfully!"
354 echo "##################################################"