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 # all subcommands in this script should complete without failure:
17 # piped commands should return the code of the first non-zero return
20 ## make sure that the right tools are installed to run the test. the
21 ## test has *more* requirements than plain ol' monkeysphere:
22 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
24 ## FIXME: other checks?
26 ######################################################################
29 # gpg command for test admin user
31 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
35 # first argument is expected return code from ssh connection
41 # start the ssh daemon on the socket
42 echo "##### starting ssh server..."
43 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
46 # wait until the socket is created before continuing
47 while [ ! -S "$SOCKET" ] ; do
53 # make a client connection to the socket
54 echo "##### starting ssh client..."
56 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
59 # kill the sshd process if it's still running
65 echo "##### return $RETURN"
66 if [ "$RETURN" = "$CODE" ] ; then
67 echo "##### ssh connection test returned as desired"
70 echo "##### ssh connection test failed. expected return code $CODE"
76 # FIXME: can we be more verbose here?
78 read -p "press enter to cleanup and remove tmp:"
84 if (gpg --quick-random --version >/dev/null 2>&1) ; then
86 elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
87 echo debug-quick-random
92 echo "### removing temp dir..."
95 if [ "$SSHD_PID" ] ; then
96 echo "### killing off lingering sshd..."
106 trap failed_cleanup EXIT
109 ######################################################################
112 ## set up some variables to ensure that we're operating strictly in
113 ## the tests, not system-wide:
115 export TESTDIR=$(dirname "$0")
118 TEMPDIR="$TESTDIR"/tmp
119 if [ -e "$TEMPDIR" ] ; then
120 echo "tempdir '$TEMPDIR' already exists."
125 # Use the local copy of executables first, instead of system ones.
126 # This should help us test without installing.
127 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
129 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
130 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
131 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
132 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
133 export MONKEYSPHERE_CHECK_KEYSERVER=false
134 export MONKEYSPHERE_LOG_LEVEL=DEBUG
135 export MONKEYSPHERE_CORE_KEYLENGTH=1024
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
151 echo "##################################################"
152 echo "### copying admin and testuser homes..."
153 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
154 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
156 # set up environment for testuser
157 TESTHOME="$TEMPDIR"/testuser
158 export GNUPGHOME="$TESTHOME"/.gnupg
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 sshd..."
174 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
175 # write the sshd_config
176 cat <<EOF >> "$SSHD_CONFIG"
177 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
178 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
182 ######################################################################
183 ### SERVER HOST SETUP
185 # set up monkeysphere host
186 echo "##################################################"
187 echo "### configuring monkeysphere host..."
188 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
190 # create a new host key
191 echo "##################################################"
192 echo "### generating server host key..."
193 # add gpg.conf with quick-random
194 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
195 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
196 # remove the gpg.conf
197 rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
199 # FIXME: need to test import-key as well
201 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
203 # certify it with the "Admin's Key".
204 # (this would normally be done via keyservers)
205 echo "##################################################"
206 echo "### certifying server host key..."
207 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
208 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
210 # FIXME: add revoker?
212 # FIXME: how can we test publish-key without flooding junk into the
215 # FIXME: should we run "diagnostics" here to test setup?
218 ######################################################################
219 ### SERVER AUTHENTICATION SETUP
221 # set up monkeysphere authentication
222 echo "##################################################"
223 echo "### setup monkeysphere authentication..."
224 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
225 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
226 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
228 monkeysphere-authentication setup
229 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
231 # add admin as identity certifier for testhost
232 echo "##################################################"
233 echo "### adding admin as certifier..."
234 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
236 # FIXME: should we run "diagnostics" here to test setup?
239 ######################################################################
242 # generate an auth subkey for the test user that expires in 2 days
243 echo "##################################################"
244 echo "### generating key for testuser..."
245 monkeysphere gen-subkey --expire 2
247 # add server key to testuser keychain
248 echo "##################################################"
249 echo "### export server key to testuser..."
250 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
252 # teach the "server" about the testuser's key
253 echo "##################################################"
254 echo "### export testuser key to server..."
255 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
257 # update authorized_keys for user
258 echo "##################################################"
259 echo "### update server authorized_keys file for this testuser..."
260 monkeysphere-authentication update-users $(whoami)
263 ######################################################################
266 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
267 # the identity before connection. This should work in both directions!
268 echo "##################################################"
269 echo "### ssh connection test for success..."
272 # remove the testuser's authorized_user_ids file, update, and make
273 # sure that the ssh authentication FAILS
274 echo "##################################################"
275 echo "### removing testuser authorized_user_ids and updating..."
276 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
277 monkeysphere-authentication update-users $(whoami)
278 echo "##################################################"
279 echo "### ssh connection test for server authentication denial..."
281 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
283 # put improper permissions on authorized_user_ids file, update, and
284 # make sure ssh authentication FAILS
285 echo "##################################################"
286 echo "### setting group writability on authorized_user_ids and updating..."
287 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
288 monkeysphere-authentication update-users $(whoami)
289 echo "##################################################"
290 echo "### ssh connection test for server authentication denial..."
292 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
293 echo "##################################################"
294 echo "### setting other writability on authorized_user_ids and updating..."
295 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
296 monkeysphere-authentication update-users $(whoami)
297 echo "##################################################"
298 echo "### ssh connection test for server authentication denial..."
300 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
302 # FIXME: addtest: remove admin as id-certifier and check ssh failure
304 # FIXME: addtest: revoke hostname on host key and check ssh failure
306 # FIXME: addtest: revoke the host key and check ssh failure
309 ######################################################################
313 echo "##################################################"
314 echo " Monkeysphere basic tests completed successfully!"
315 echo "##################################################"