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; }
32 ## FIXME: other checks?
34 ######################################################################
37 # gpg command for test admin user
39 chmod 0700 "$TEMPDIR"/admin
40 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
44 # first argument is expected return code from ssh connection
50 # start the ssh daemon on the socket
51 echo "##### starting ssh server..."
52 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
55 # wait until the socket is created before continuing
56 while [ ! -S "$SOCKET" ] ; do
62 # make a client connection to the socket
63 echo "##### starting ssh client..."
65 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
68 # kill the sshd process if it's still running
74 echo "##### return $RETURN"
75 if [ "$RETURN" = "$CODE" ] ; then
76 echo "##### ssh connection test returned as desired"
79 echo "##### ssh connection test failed. expected return code $CODE"
87 trap failed_cleanup EXIT
90 ######################################################################
93 ## set up some variables to ensure that we're operating strictly in
94 ## the tests, not system-wide:
98 # NOTE: /tmp can not be used as the temp dir here, since the
99 # permissions on /tmp are usually such that they will not pass the
100 # monkeysphere/ssh path permission checking. If you need to use a
101 # different location than the current source, please set $TMPDIR
102 # somewhere with tighter permissions.
104 mkdir -p "$TESTDIR"/tmp
105 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
107 # Use the local copy of executables first, instead of system ones.
108 # This should help us test without installing.
109 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
111 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
112 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
113 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
114 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
116 export MONKEYSPHERE_CHECK_KEYSERVER=false
117 # example.org does not respond to the HKP port, so this should cause
118 # any keyserver connection attempts that do happen (they shouldn't!)
119 # to hang, so we'll notice them:
120 export MONKEYSPHERE_KEYSERVER=example.org
122 export MONKEYSPHERE_LOG_LEVEL=DEBUG
123 export MONKEYSPHERE_CORE_KEYLENGTH=1024
124 export MONKEYSPHERE_PROMPT=false
126 export SSHD_CONFIG="$TEMPDIR"/sshd_config
127 export SOCKET="$TEMPDIR"/ssh-socket
129 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
130 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
131 # $DISPLAY (if one exists) because this test suite should not be doing
132 # *anything* with any running X11 session.
133 export DISPLAY=monkeys
135 ## make sure that the version number matches the debian changelog
136 ## (don't bother if this is being run from the tests).
138 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
140 echo "##################################################"
141 echo "### checking version string match..."
142 repver=$(monkeysphere version)
143 debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
144 if [ "$repver" = "$debver" ] ; then
145 echo "Versions match!"
147 printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
152 ######################################################################
153 ### CONFIGURE ENVIRONMENTS
155 # copy in admin and testuser home to tmp
157 echo "##################################################"
158 echo "### configuring testuser home..."
159 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
160 # set up environment for testuser
161 export TESTHOME="$TEMPDIR"/testuser
162 export GNUPGHOME="$TESTHOME"/.gnupg
163 chmod 0700 "$GNUPGHOME"
164 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
165 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
166 cat <<EOF >> "$TESTHOME"/.ssh/config
167 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
168 IdentityFile $TESTHOME/.ssh/no-such-identity
169 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
171 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
172 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
174 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
177 echo "##################################################"
178 echo "### configuring admin home..."
179 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
183 echo "##################################################"
184 echo "### configuring sshd..."
185 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
186 # write the sshd_config
187 cat <<EOF >> "$SSHD_CONFIG"
188 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
189 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
193 ######################################################################
194 ### SERVER HOST SETUP
198 echo "##################################################"
199 echo "### import host key..."
200 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
201 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
204 echo "##################################################"
205 echo "### getting host key fingerprint..."
206 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
209 # change host key expiration
211 echo "##################################################"
212 echo "### setting host key expiration..."
213 monkeysphere-host set-expire 1
214 # FIXME: how do we check that the expiration has really been set?
216 # certify host key with the "Admin's Key".
217 # (this would normally be done via keyservers)
219 echo "##################################################"
220 echo "### certifying server host key..."
221 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
222 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
224 # FIXME: add revoker?
226 # FIXME: how can we test publish-key without flooding junk into the
229 # FIXME: should we run "diagnostics" here to test setup?
232 ######################################################################
233 ### SERVER AUTHENTICATION SETUP
235 # set up monkeysphere authentication
237 echo "##################################################"
238 echo "### setup monkeysphere authentication..."
239 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
240 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
241 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
243 monkeysphere-authentication setup
244 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
246 # add admin as identity certifier for testhost
248 echo "##################################################"
249 echo "### adding admin as certifier..."
250 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
253 echo "##################################################"
254 echo "### list certifiers..."
255 monkeysphere-authentication list-certifiers
257 # FIXME: should we run "diagnostics" here to test setup?
259 ######################################################################
262 # generate an auth subkey for the test user that expires in 2 days
264 echo "##################################################"
265 echo "### generating key for testuser..."
266 monkeysphere gen-subkey
268 # add server key to testuser keychain
270 echo "##################################################"
271 echo "### export server key to testuser..."
272 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
274 # teach the "server" about the testuser's key
276 echo "##################################################"
277 echo "### export testuser key to server..."
278 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
280 # update authorized_keys for user
282 echo "##################################################"
283 echo "### update server authorized_keys file for this testuser..."
284 monkeysphere-authentication update-users $(whoami)
285 # FIXME: this is maybe not failing properly for:
286 # ms: improper group or other writability on path '/tmp'.
289 ######################################################################
292 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
293 # the identity before connection. This should work in both directions!
295 echo "##################################################"
296 echo "### ssh connection test for success..."
299 # remove the testuser's authorized_user_ids file, update, and make
300 # sure that the ssh authentication FAILS
302 echo "##################################################"
303 echo "### removing testuser authorized_user_ids and updating..."
304 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
305 monkeysphere-authentication update-users $(whoami)
307 echo "##################################################"
308 echo "### ssh connection test for server authentication denial..."
310 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
312 # put improper permissions on authorized_user_ids file, update, and
313 # make sure ssh authentication FAILS
315 echo "##################################################"
316 echo "### setting group writability on authorized_user_ids and updating..."
317 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
318 monkeysphere-authentication update-users $(whoami)
320 echo "##################################################"
321 echo "### ssh connection test for server authentication denial..."
323 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
325 echo "##################################################"
326 echo "### setting other writability on authorized_user_ids and updating..."
327 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
328 monkeysphere-authentication update-users $(whoami)
330 echo "##################################################"
331 echo "### ssh connection test for server authentication denial..."
333 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
334 monkeysphere-authentication update-users $(whoami)
336 # FIXME: addtest: remove admin as id-certifier and check ssh failure
338 # FIXME: addtest: add hostname on host key
339 # FIXME: addtest: revoke hostname on host key and check ssh failure
341 # addtest: revoke the host key and check ssh failure
342 # test to make sure things are OK after the previous tests:
345 echo "##################################################"
346 echo "### ssh connection test for server with revoked key..."
347 # generate the revocation certificate and feed it directly to the test
348 # user's keyring (we're not publishing to the keyservers)
349 monkeysphere-host revoke-key | gpg --import
353 ######################################################################
358 echo "##################################################"
359 echo " Monkeysphere basic tests completed successfully!"
360 echo "##################################################"