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
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
150 echo "##################################################"
151 echo "### copying admin and testuser homes..."
152 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
153 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
155 # set up environment for testuser
156 TESTHOME="$TEMPDIR"/testuser
157 export GNUPGHOME="$TESTHOME"/.gnupg
158 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
159 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
160 cat <<EOF >> "$TESTHOME"/.ssh/config
161 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
162 IdentityFile $TESTHOME/.ssh/no-such-identity
163 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
165 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
166 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
168 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
171 echo "##################################################"
172 echo "### configuring sshd..."
173 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
174 # write the sshd_config
175 cat <<EOF >> "$SSHD_CONFIG"
176 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
177 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
181 ######################################################################
182 ### SERVER HOST SETUP
184 # set up monkeysphere host
185 echo "##################################################"
186 echo "### configuring monkeysphere host..."
187 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
189 # create a new host key
190 echo "##################################################"
191 echo "### generating server host key..."
192 # add gpg.conf with quick-random
193 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
194 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
195 # remove the gpg.conf
196 rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
198 # FIXME: need to test import-key as well
200 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
202 # certify it with the "Admin's Key".
203 # (this would normally be done via keyservers)
204 echo "##################################################"
205 echo "### certifying server host key..."
206 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
207 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
209 # FIXME: add revoker?
211 # FIXME: how can we test publish-key without flooding junk into the
214 # FIXME: should we run "diagnostics" here to test setup?
217 ######################################################################
218 ### SERVER AUTHENTICATION SETUP
220 # set up monkeysphere authentication
221 echo "##################################################"
222 echo "### setup monkeysphere authentication..."
223 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
224 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
225 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
227 monkeysphere-authentication setup
228 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
230 # add admin as identity certifier for testhost
231 echo "##################################################"
232 echo "### adding admin as certifier..."
233 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
235 # FIXME: should we run "diagnostics" here to test setup?
238 ######################################################################
241 # generate an auth subkey for the test user that expires in 2 days
242 echo "##################################################"
243 echo "### generating key for testuser..."
244 monkeysphere gen-subkey --expire 2
246 # add server key to testuser keychain
247 echo "##################################################"
248 echo "### export server key to testuser..."
249 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
251 # teach the "server" about the testuser's key
252 echo "##################################################"
253 echo "### export testuser key to server..."
254 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
256 # update authorized_keys for user
257 echo "##################################################"
258 echo "### update server authorized_keys file for this testuser..."
259 monkeysphere-authentication update-users $(whoami)
262 ######################################################################
265 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
266 # the identity before connection. This should work in both directions!
267 echo "##################################################"
268 echo "### ssh connection test for success..."
271 # remove the testuser's authorized_user_ids file, update, and make
272 # sure that the ssh authentication FAILS
273 echo "##################################################"
274 echo "### removing testuser authorized_user_ids and updating..."
275 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
276 monkeysphere-authentication update-users $(whoami)
277 echo "##################################################"
278 echo "### ssh connection test for server authentication denial..."
280 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
282 # put improper permissions on authorized_user_ids file, update, and
283 # make sure ssh authentication FAILS
284 echo "##################################################"
285 echo "### setting group writability on authorized_user_ids and updating..."
286 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
287 monkeysphere-authentication update-users $(whoami)
288 echo "##################################################"
289 echo "### ssh connection test for server authentication denial..."
291 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
292 echo "##################################################"
293 echo "### setting other writability on authorized_user_ids and updating..."
294 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
295 monkeysphere-authentication update-users $(whoami)
296 echo "##################################################"
297 echo "### ssh connection test for server authentication denial..."
299 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
301 # FIXME: addtest: remove admin as id-certifier and check ssh failure
303 # FIXME: addtest: revoke hostname on host key and check ssh failure
305 # FIXME: addtest: revoke the host key and check ssh failure
308 ######################################################################
312 echo "##################################################"
313 echo " Monkeysphere basic tests completed successfully!"
314 echo "##################################################"