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 # create a new host key
186 echo "##################################################"
187 echo "### testing host key generation..."
188 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
189 # add gpg.conf with quick-random
190 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
191 echo | monkeysphere-host expert gen-key --length 1024 testhost
193 # remove the host home for the next test
194 rm -rf "$MONKEYSPHERE_SYSCONFIGDIR"/host
197 echo "##################################################"
198 echo "### testing host key importing..."
199 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
200 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
202 # change host key expiration
203 echo "##################################################"
204 echo "### setting host key expiration..."
205 monkeysphere-host set-expire 1
206 monkeysphere-host show-key
207 # FIXME: how do we check that the expiration has really been set?
209 echo "##################################################"
210 echo "### getting host key fingerprint..."
211 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
213 # certify host key with the "Admin's Key".
214 # (this would normally be done via keyservers)
215 echo "##################################################"
216 echo "### certifying server host key..."
217 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
218 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
220 # FIXME: add revoker?
222 # FIXME: how can we test publish-key without flooding junk into the
225 # FIXME: should we run "diagnostics" here to test setup?
228 ######################################################################
229 ### SERVER AUTHENTICATION SETUP
231 # 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/authentication/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
242 echo "##################################################"
243 echo "### adding admin as certifier..."
244 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
246 # FIXME: should we run "diagnostics" here to test setup?
249 ######################################################################
252 # generate an auth subkey for the test user that expires in 2 days
253 echo "##################################################"
254 echo "### generating key for testuser..."
255 monkeysphere gen-subkey --expire 2
257 # add server key to testuser keychain
258 echo "##################################################"
259 echo "### export server key to testuser..."
260 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
262 # teach the "server" about the testuser's key
263 echo "##################################################"
264 echo "### export testuser key to server..."
265 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
267 # update authorized_keys for user
268 echo "##################################################"
269 echo "### update server authorized_keys file for this testuser..."
270 monkeysphere-authentication update-users $(whoami)
273 ######################################################################
276 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
277 # the identity before connection. This should work in both directions!
278 echo "##################################################"
279 echo "### ssh connection test for success..."
282 # remove the testuser's authorized_user_ids file, update, and make
283 # sure that the ssh authentication FAILS
284 echo "##################################################"
285 echo "### removing testuser authorized_user_ids and updating..."
286 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
287 monkeysphere-authentication update-users $(whoami)
288 echo "##################################################"
289 echo "### ssh connection test for server authentication denial..."
291 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
293 # put improper permissions on authorized_user_ids file, update, and
294 # make sure ssh authentication FAILS
295 echo "##################################################"
296 echo "### setting group writability on authorized_user_ids and updating..."
297 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
298 monkeysphere-authentication update-users $(whoami)
299 echo "##################################################"
300 echo "### ssh connection test for server authentication denial..."
302 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
303 echo "##################################################"
304 echo "### setting other writability on authorized_user_ids and updating..."
305 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
306 monkeysphere-authentication update-users $(whoami)
307 echo "##################################################"
308 echo "### ssh connection test for server authentication denial..."
310 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
312 # FIXME: addtest: remove admin as id-certifier and check ssh failure
314 # FIXME: addtest: add hostname on host key
315 # FIXME: addtest: revoke hostname on host key and check ssh failure
317 # FIXME: addtest: revoke the host key and check ssh failure
320 ######################################################################
324 echo "##################################################"
325 echo " Monkeysphere basic tests completed successfully!"
326 echo "##################################################"