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 # gpg command for test admin user
28 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
32 # first argument is expected return code from ssh connection
38 # start the ssh daemon on the socket
39 echo "##### starting ssh server..."
40 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
43 # wait until the socket is created before continuing
44 while [ ! -S "$SOCKET" ] ; do
50 # make a client connection to the socket
51 echo "##### starting ssh client..."
53 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
56 # kill the sshd process if it's still running
62 echo "##### return $RETURN"
63 if [ "$RETURN" = "$CODE" ] ; then
64 echo "##### ssh connection test returned as desired"
67 echo "##### ssh connection test failed. expected return code $CODE"
73 # FIXME: can we be more verbose here?
75 read -p "press enter to cleanup and remove tmp:"
81 if (gpg --quick-random --version >/dev/null 2>&1) ; then
83 elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
84 echo debug-quick-random
89 echo "### removing temp dir..."
92 if [ "$SSHD_PID" ] ; then
93 echo "### killing off lingering sshd..."
103 trap failed_cleanup EXIT
107 ## set up some variables to ensure that we're operating strictly in
108 ## the tests, not system-wide:
110 export TESTDIR=$(dirname "$0")
113 TEMPDIR="$TESTDIR"/tmp
114 if [ -e "$TEMPDIR" ] ; then
115 echo "tempdir '$TEMPDIR' already exists."
120 # Use the local copy of executables first, instead of system ones.
121 # This should help us test without installing.
122 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
124 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
125 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
126 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
127 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
128 export MONKEYSPHERE_CHECK_KEYSERVER=false
129 export MONKEYSPHERE_LOG_LEVEL=DEBUG
131 export SSHD_CONFIG="$TEMPDIR"/sshd_config
132 export SOCKET="$TEMPDIR"/ssh-socket
134 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
135 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
136 # $DISPLAY (if one exists) because this test suite should not be doing
137 # *anything* with any running X11 session.
138 export DISPLAY=monkeys
141 ### CONFIGURE ENVIRONMENTS
143 # copy in admin and testuser home to tmp
144 echo "##################################################"
145 echo "### copying admin and testuser homes..."
146 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
147 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
149 # set up environment for testuser
150 TESTHOME="$TEMPDIR"/testuser
151 export GNUPGHOME="$TESTHOME"/.gnupg
152 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
153 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
154 cat <<EOF >> "$TESTHOME"/.ssh/config
155 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
156 IdentityFile $TESTHOME/.ssh/no-such-identity
157 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
159 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
160 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
162 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
165 echo "##################################################"
166 echo "### configuring sshd..."
167 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
168 # write the sshd_config
169 cat <<EOF >> "$SSHD_CONFIG"
170 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
171 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
175 ### SERVER HOST SETUP
177 # set up monkeysphere host
178 echo "##################################################"
179 echo "### configuring monkeysphere host..."
180 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
182 # create a new host key
183 echo "##################################################"
184 echo "### generating server host key..."
185 # add gpg.conf with quick-random
186 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
187 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
188 # remove the gpg.conf
189 rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
191 # FIXME: need to test import-key as well
193 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
195 # certify it with the "Admin's Key".
196 # (this would normally be done via keyservers)
197 echo "##################################################"
198 echo "### certifying server host key..."
199 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
200 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
202 # FIXME: how can we test publish-key without flooding junk into the
205 # FIXME: should we run "diagnostics" here to test setup?
208 ### SERVER AUTHENTICATION SETUP
210 # set up monkeysphere authentication
211 echo "##################################################"
212 echo "### setup monkeysphere authentication..."
213 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/{authorized_keys,core,sphere,tmp}
214 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
215 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
216 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
218 monkeysphere-authentication setup
219 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
221 # add admin as identity certifier for testhost
222 echo "##################################################"
223 echo "### adding admin as certifier..."
224 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
226 # FIXME: should we run "diagnostics" here to test setup?
231 # generate an auth subkey for the test user that expires in 2 days
232 echo "##################################################"
233 echo "### generating key for testuser..."
234 monkeysphere gen-subkey --expire 2
236 # add server key to testuser keychain
237 echo "##################################################"
238 echo "### export server key to testuser..."
239 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
241 # teach the "server" about the testuser's key
242 echo "##################################################"
243 echo "### export testuser key to server..."
244 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
246 # update authorized_keys for user
247 echo "##################################################"
248 echo "### update server authorized_keys file for this testuser..."
249 monkeysphere-authentication update-users $(whoami)
254 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
255 # the identity before connection. This should work in both directions!
256 echo "##################################################"
257 echo "### ssh connection test for success..."
260 # remove the testuser's authorized_user_ids file, update, and make
261 # sure that the ssh authentication FAILS
262 echo "##################################################"
263 echo "### removing testuser authorized_user_ids and updating..."
264 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
265 monkeysphere-authentication update-users $(whoami)
266 echo "##################################################"
267 echo "### ssh connection test for server authentication denial..."
269 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
271 # put improper permissions on authorized_user_ids file, update, and
272 # make sure ssh authentication FAILS
273 echo "##################################################"
274 echo "### setting group writability on authorized_user_ids and updating..."
275 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
276 monkeysphere-authentication update-users $(whoami)
277 echo "##################################################"
278 echo "### ssh connection test for server authentication denial..."
280 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
281 echo "##################################################"
282 echo "### setting other writability on authorized_user_ids and updating..."
283 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
284 monkeysphere-authentication update-users $(whoami)
285 echo "##################################################"
286 echo "### ssh connection test for server authentication denial..."
288 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
290 # FIXME: addtest: remove admin as id-certifier and check ssh failure
292 # FIXME: addtest: revoke hostname on host key and check ssh failure
294 # FIXME: addtest: revoke the host key and check ssh failure
298 echo "##################################################"
299 echo " Monkeysphere basic tests completed successfully!"
300 echo "##################################################"