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
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
82 echo "##### return $RETURN"
83 if [ "$RETURN" = "$CODE" ] ; then
84 echo "##### ssh connection test returned as desired"
87 echo "##### ssh connection test failed. expected return code $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:"$TESTDIR"/../src/keytrans:"$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 export SSHD_CONFIG="$TEMPDIR"/sshd_config
135 export SOCKET="$TEMPDIR"/ssh-socket
137 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
138 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
139 # $DISPLAY (if one exists) because this test suite should not be doing
140 # *anything* with any running X11 session.
141 export DISPLAY=monkeys
143 ## make sure that the version number matches the debian changelog
144 ## (don't bother if this is being run from the tests).
146 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
148 echo "##################################################"
149 echo "### checking version string match..."
150 repver=$(monkeysphere version)
151 debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
152 if [ "$repver" = "$debver" ] ; then
153 echo "Versions match!"
155 printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
160 ######################################################################
161 ### CONFIGURE ENVIRONMENTS
163 # copy in admin and testuser home to tmp
165 echo "##################################################"
166 echo "### configuring testuser home..."
167 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
168 # set up environment for testuser
169 export TESTHOME="$TEMPDIR"/testuser
170 export GNUPGHOME="$TESTHOME"/.gnupg
171 chmod 0700 "$GNUPGHOME"
172 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
173 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
174 cat <<EOF >> "$TESTHOME"/.ssh/config
175 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
176 IdentityFile $TESTHOME/.ssh/no-such-identity
177 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
179 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
180 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
182 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
185 echo "##################################################"
186 echo "### configuring admin home..."
187 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
191 echo "##################################################"
192 echo "### configuring sshd..."
193 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
194 # write the sshd_config
195 cat <<EOF >> "$SSHD_CONFIG"
196 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
197 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
201 ######################################################################
202 ### SERVER HOST SETUP
206 echo "##################################################"
207 echo "### import host key..."
208 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
209 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
212 echo "##################################################"
213 echo "### getting host key fingerprint..."
214 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
217 # change host key expiration
219 echo "##################################################"
220 echo "### setting host key expiration..."
221 monkeysphere-host set-expire 1
222 # FIXME: how do we check that the expiration has really been set?
224 # certify host key with the "Admin's Key".
225 # (this would normally be done via keyservers)
227 echo "##################################################"
228 echo "### certifying server host key..."
229 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
230 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
232 # FIXME: add revoker?
234 # FIXME: how can we test publish-key without flooding junk into the
237 # FIXME: should we run "diagnostics" here to test setup?
240 ######################################################################
241 ### SERVER AUTHENTICATION SETUP
243 # set up monkeysphere authentication
245 echo "##################################################"
246 echo "### setup monkeysphere authentication..."
247 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
248 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
249 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
251 monkeysphere-authentication setup
252 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
254 # add admin as identity certifier for testhost
256 echo "##################################################"
257 echo "### adding admin as certifier..."
258 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
261 echo "##################################################"
262 echo "### list certifiers..."
263 monkeysphere-authentication list-certifiers
265 # FIXME: should we run "diagnostics" here to test setup?
267 ######################################################################
270 # generate an auth subkey for the test user that expires in 2 days
272 echo "##################################################"
273 echo "### generating key for testuser..."
274 monkeysphere gen-subkey
276 # add server key to testuser keychain
278 echo "##################################################"
279 echo "### export server key to testuser..."
280 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
282 # teach the "server" about the testuser's key
284 echo "##################################################"
285 echo "### export testuser key to server..."
286 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
288 # update authorized_keys for user
290 echo "##################################################"
291 echo "### update server authorized_keys file for this testuser..."
292 monkeysphere-authentication update-users $(whoami)
293 # FIXME: this is maybe not failing properly for:
294 # ms: improper group or other writability on path '/tmp'.
297 ######################################################################
300 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
301 # the identity before connection. This should work in both directions!
303 echo "##################################################"
304 echo "### ssh connection test for success..."
307 # remove the testuser's authorized_user_ids file, update, and make
308 # sure that the ssh authentication FAILS
310 echo "##################################################"
311 echo "### removing testuser authorized_user_ids and updating..."
312 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
313 monkeysphere-authentication update-users $(whoami)
315 echo "##################################################"
316 echo "### ssh connection test for server authentication denial..."
318 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
320 # put improper permissions on authorized_user_ids file, update, and
321 # make sure ssh authentication FAILS
323 echo "##################################################"
324 echo "### setting group writability on authorized_user_ids and updating..."
325 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
326 monkeysphere-authentication update-users $(whoami)
328 echo "##################################################"
329 echo "### ssh connection test for server authentication denial..."
331 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
333 echo "##################################################"
334 echo "### setting other writability on authorized_user_ids and updating..."
335 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
336 monkeysphere-authentication update-users $(whoami)
338 echo "##################################################"
339 echo "### ssh connection test for server authentication denial..."
341 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
342 monkeysphere-authentication update-users $(whoami)
344 # FIXME: addtest: remove admin as id-certifier and check ssh failure
346 # FIXME: addtest: add hostname on host key
347 # FIXME: addtest: revoke hostname on host key and check ssh failure
349 # addtest: revoke the host key and check ssh failure
350 # test to make sure things are OK after the previous tests:
353 echo "##################################################"
354 echo "### ssh connection test for server with revoked key..."
355 # generate the revocation certificate and feed it directly to the test
356 # user's keyring (we're not publishing to the keyservers)
357 monkeysphere-host revoke-key | gpg --import
361 ######################################################################
366 echo "##################################################"
367 echo " Monkeysphere basic tests completed successfully!"
368 echo "##################################################"