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 export TESTDIR=$(dirname "$0")
22 source "$TESTDIR"/common
24 ## make sure that the right tools are installed to run the test. the
25 ## test has *more* requirements than plain ol' monkeysphere:
26 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
28 ## FIXME: other checks?
30 ######################################################################
33 # gpg command for test admin user
35 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
39 # first argument is expected return code from ssh connection
45 # start the ssh daemon on the socket
46 echo "##### starting ssh server..."
47 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
50 # wait until the socket is created before continuing
51 while [ ! -S "$SOCKET" ] ; do
57 # make a client connection to the socket
58 echo "##### starting ssh client..."
60 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
63 # kill the sshd process if it's still running
69 echo "##### return $RETURN"
70 if [ "$RETURN" = "$CODE" ] ; then
71 echo "##### ssh connection test returned as desired"
74 echo "##### ssh connection test failed. expected return code $CODE"
82 trap failed_cleanup EXIT
85 ######################################################################
88 ## set up some variables to ensure that we're operating strictly in
89 ## the tests, not system-wide:
92 mkdir -p "$TESTDIR"/tmp
93 TEMPDIR=$(mktemp -d ${TMPDIR:-$(cd "$TESTDIR" && printf "%s" $(pwd)/tmp)}/monkeyspheretest.XXXXXXX)
95 # Use the local copy of executables first, instead of system ones.
96 # This should help us test without installing.
97 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
99 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
100 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
101 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
102 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
103 export MONKEYSPHERE_CHECK_KEYSERVER=false
104 export MONKEYSPHERE_LOG_LEVEL=DEBUG
105 export MONKEYSPHERE_CORE_KEYLENGTH=1024
107 export SSHD_CONFIG="$TEMPDIR"/sshd_config
108 export SOCKET="$TEMPDIR"/ssh-socket
110 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
111 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
112 # $DISPLAY (if one exists) because this test suite should not be doing
113 # *anything* with any running X11 session.
114 export DISPLAY=monkeys
117 ######################################################################
118 ### CONFIGURE ENVIRONMENTS
120 # copy in admin and testuser home to tmp
121 echo "##################################################"
122 echo "### copying admin and testuser homes..."
123 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
124 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
126 # set up environment for testuser
127 export TESTHOME="$TEMPDIR"/testuser
128 export GNUPGHOME="$TESTHOME"/.gnupg
129 chmod 0700 "$GNUPGHOME"
130 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
131 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
132 cat <<EOF >> "$TESTHOME"/.ssh/config
133 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
134 IdentityFile $TESTHOME/.ssh/no-such-identity
135 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
137 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
138 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
140 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
143 echo "##################################################"
144 echo "### configuring sshd..."
145 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
146 # write the sshd_config
147 cat <<EOF >> "$SSHD_CONFIG"
148 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
149 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
153 ######################################################################
154 ### SERVER HOST SETUP
156 # create a new host key
157 echo "##################################################"
158 echo "### testing host key generation..."
159 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
160 # add gpg.conf with quick-random
161 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
162 echo | monkeysphere-host expert gen-key --length 1024 testhost
164 # remove the host home for the next test
165 rm -rf "$MONKEYSPHERE_SYSCONFIGDIR"/host
168 echo "##################################################"
169 echo "### testing host key importing..."
170 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
171 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
173 # change host key expiration
174 echo "##################################################"
175 echo "### setting host key expiration..."
176 monkeysphere-host set-expire 1
177 monkeysphere-host show-key
178 # FIXME: how do we check that the expiration has really been set?
180 echo "##################################################"
181 echo "### getting host key fingerprint..."
182 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
184 # certify host key with the "Admin's Key".
185 # (this would normally be done via keyservers)
186 echo "##################################################"
187 echo "### certifying server host key..."
188 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
189 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
191 # FIXME: add revoker?
193 # FIXME: how can we test publish-key without flooding junk into the
196 # FIXME: should we run "diagnostics" here to test setup?
199 ######################################################################
200 ### SERVER AUTHENTICATION SETUP
202 # set up monkeysphere authentication
203 echo "##################################################"
204 echo "### setup monkeysphere authentication..."
205 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
206 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
207 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
209 monkeysphere-authentication setup
210 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
212 # add admin as identity certifier for testhost
213 echo "##################################################"
214 echo "### adding admin as certifier..."
215 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
217 # FIXME: should we run "diagnostics" here to test setup?
220 ######################################################################
223 # generate an auth subkey for the test user that expires in 2 days
224 echo "##################################################"
225 echo "### generating key for testuser..."
226 monkeysphere gen-subkey --expire 2
228 # add server key to testuser keychain
229 echo "##################################################"
230 echo "### export server key to testuser..."
231 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
233 # teach the "server" about the testuser's key
234 echo "##################################################"
235 echo "### export testuser key to server..."
236 gpg --export testuser | monkeysphere-authentication expert gpg-cmd --import
238 # update authorized_keys for user
239 echo "##################################################"
240 echo "### update server authorized_keys file for this testuser..."
241 monkeysphere-authentication update-users $(whoami)
244 ######################################################################
247 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
248 # the identity before connection. This should work in both directions!
249 echo "##################################################"
250 echo "### ssh connection test for success..."
253 # remove the testuser's authorized_user_ids file, update, and make
254 # sure that the ssh authentication FAILS
255 echo "##################################################"
256 echo "### removing testuser authorized_user_ids and updating..."
257 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
258 monkeysphere-authentication update-users $(whoami)
259 echo "##################################################"
260 echo "### ssh connection test for server authentication denial..."
262 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
264 # put improper permissions on authorized_user_ids file, update, and
265 # make sure ssh authentication FAILS
266 echo "##################################################"
267 echo "### setting group writability on authorized_user_ids and updating..."
268 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
269 monkeysphere-authentication update-users $(whoami)
270 echo "##################################################"
271 echo "### ssh connection test for server authentication denial..."
273 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
274 echo "##################################################"
275 echo "### setting other writability on authorized_user_ids and updating..."
276 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
277 monkeysphere-authentication update-users $(whoami)
278 echo "##################################################"
279 echo "### ssh connection test for server authentication denial..."
281 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
283 # FIXME: addtest: remove admin as id-certifier and check ssh failure
285 # FIXME: addtest: add hostname on host key
286 # FIXME: addtest: revoke hostname on host key and check ssh failure
288 # FIXME: addtest: revoke the host key and check ssh failure
291 ######################################################################
295 echo "##################################################"
296 echo " Monkeysphere basic tests completed successfully!"
297 echo "##################################################"