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."
124 # Use the local copy of executables first, instead of system ones.
125 # This should help us test without installing.
126 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
128 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
129 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
130 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
131 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
132 export MONKEYSPHERE_CHECK_KEYSERVER=false
133 export MONKEYSPHERE_LOG_LEVEL=DEBUG
134 export MONKEYSPHERE_CORE_KEYLENGTH=1024
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
145 ######################################################################
146 ### TEST KEY CONVERSION
150 echo "##################################################"
151 echo "### test key conversion..."
152 export GNUPGHOME="$TEMPDIR"
154 gpg --batch --quick-random --gen-key <<EOF
164 # get the the key timestamp
165 timestamp=$(gpg --list-key --with-colons --fixed-list-mode | \
166 grep ^pub: | cut -d: -f6)
167 # export the key to a file
168 gpg --export-secret-key | openpgp2ssh > \
170 # reconvert key, and compare to key in gpg keyring
172 <(gpg --export-secret-key | hd) \
173 <(PEM2OPENPGP_USAGE_FLAGS=sign,certify \
174 PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest < \
175 "$TEMPDIR"/test.pem | hd )
177 # clear out the temp dir
181 ######################################################################
182 ### CONFIGURE ENVIRONMENTS
184 # copy in admin and testuser home to tmp
185 echo "##################################################"
186 echo "### copying admin and testuser homes..."
187 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
188 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
190 # set up environment for testuser
191 export TESTHOME="$TEMPDIR"/testuser
192 export GNUPGHOME="$TESTHOME"/.gnupg
193 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
194 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
195 cat <<EOF >> "$TESTHOME"/.ssh/config
196 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
197 IdentityFile $TESTHOME/.ssh/no-such-identity
198 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
200 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
201 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
203 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
206 echo "##################################################"
207 echo "### configuring sshd..."
208 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
209 # write the sshd_config
210 cat <<EOF >> "$SSHD_CONFIG"
211 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
212 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
216 ######################################################################
217 ### SERVER HOST SETUP
219 # create a new host key
220 echo "##################################################"
221 echo "### testing host key generation..."
222 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
223 # add gpg.conf with quick-random
224 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
225 echo | monkeysphere-host expert gen-key --length 1024 testhost
227 # remove the host home for the next test
228 rm -rf "$MONKEYSPHERE_SYSCONFIGDIR"/host
231 echo "##################################################"
232 echo "### testing host key importing..."
233 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
234 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
236 # change host key expiration
237 echo "##################################################"
238 echo "### setting host key expiration..."
239 monkeysphere-host set-expire 1
240 monkeysphere-host show-key
241 # FIXME: how do we check that the expiration has really been set?
243 echo "##################################################"
244 echo "### getting host key fingerprint..."
245 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
247 # certify host key with the "Admin's Key".
248 # (this would normally be done via keyservers)
249 echo "##################################################"
250 echo "### certifying server host key..."
251 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
252 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
254 # FIXME: add revoker?
256 # FIXME: how can we test publish-key without flooding junk into the
259 # FIXME: should we run "diagnostics" here to test setup?
262 ######################################################################
263 ### SERVER AUTHENTICATION SETUP
265 # set up monkeysphere authentication
266 echo "##################################################"
267 echo "### setup monkeysphere authentication..."
268 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
269 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
270 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
272 monkeysphere-authentication setup
273 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
275 # add admin as identity certifier for testhost
276 echo "##################################################"
277 echo "### adding admin as certifier..."
278 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
280 # FIXME: should we run "diagnostics" here to test setup?
283 ######################################################################
286 # generate an auth subkey for the test user that expires in 2 days
287 echo "##################################################"
288 echo "### generating key for testuser..."
289 monkeysphere gen-subkey --expire 2
291 # add server key to testuser keychain
292 echo "##################################################"
293 echo "### export server key to testuser..."
294 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
296 # teach the "server" about the testuser's key
297 echo "##################################################"
298 echo "### export testuser key to server..."
299 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
301 # update authorized_keys for user
302 echo "##################################################"
303 echo "### update server authorized_keys file for this testuser..."
304 monkeysphere-authentication update-users $(whoami)
307 ######################################################################
310 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
311 # the identity before connection. This should work in both directions!
312 echo "##################################################"
313 echo "### ssh connection test for success..."
316 # remove the testuser's authorized_user_ids file, update, and make
317 # sure that the ssh authentication FAILS
318 echo "##################################################"
319 echo "### removing testuser authorized_user_ids and updating..."
320 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
321 monkeysphere-authentication update-users $(whoami)
322 echo "##################################################"
323 echo "### ssh connection test for server authentication denial..."
325 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
327 # put improper permissions on authorized_user_ids file, update, and
328 # make sure ssh authentication FAILS
329 echo "##################################################"
330 echo "### setting group writability on authorized_user_ids and updating..."
331 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
332 monkeysphere-authentication update-users $(whoami)
333 echo "##################################################"
334 echo "### ssh connection test for server authentication denial..."
336 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
337 echo "##################################################"
338 echo "### setting other writability on authorized_user_ids and updating..."
339 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
340 monkeysphere-authentication update-users $(whoami)
341 echo "##################################################"
342 echo "### ssh connection test for server authentication denial..."
344 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
346 # FIXME: addtest: remove admin as id-certifier and check ssh failure
348 # FIXME: addtest: add hostname on host key
349 # FIXME: addtest: revoke hostname on host key and check ssh failure
351 # FIXME: addtest: revoke the host key and check ssh failure
354 ######################################################################
358 echo "##################################################"
359 echo " Monkeysphere basic tests completed successfully!"
360 echo "##################################################"