cleanup of how ssh_test return code is captured in tests/basic
[monkeysphere.git] / tests / basic
1 #!/usr/bin/env bash
2
3 # Tests to ensure that the monkeysphere is working
4
5 # Authors: 
6 #   Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 #   Jameson Rollins <jrollins@fifthhorseman.net>
8 #   Micah Anderson <micah@riseup.net> 
9 #
10 # Copyright: 2008-2009
11 # License: GPL v3 or later
12
13 # these tests should all be able to run as a non-privileged user.
14
15 # put all the test output to stdout
16 exec 2>&1
17 # all subcommands in this script should complete without failure:
18 set -e
19 # piped commands should return the code of the first non-zero return
20 set -o pipefail
21
22 # make sure the TESTDIR is an absolute path, not a relative one.
23 export TESTDIR=$(cd $(dirname "$0") && pwd)
24
25 source "$TESTDIR"/common
26
27 ## make sure that the right tools are installed to run the test.  the
28 ## test has *more* requirements than plain ol' monkeysphere:
29 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
30
31
32 ## FIXME: other checks?
33
34 ######################################################################
35 ### FUNCTIONS
36
37 # gpg command for test admin user
38 gpgadmin() {
39     chmod 0700 "$TEMPDIR"/admin
40     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
41 }
42
43 # test ssh connection
44 # first argument is expected return code from ssh connection
45 ssh_test() {
46     umask 0077
47
48     CODE=${1:-0}
49
50     # start the ssh daemon on the socket
51     echo "##### starting ssh server..."
52     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
53     SSHD_PID="$!"
54
55     # wait until the socket is created before continuing
56     while [ ! -S "$SOCKET" ] ; do
57         sleep 1
58     done
59
60     # make a client connection to the socket
61     echo "##### starting ssh client..."
62     ssh-agent bash -c \
63         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true" \
64         || RETURN="$?"
65
66     # kill the sshd process if it's still running
67     kill "$SSHD_PID"
68     SSHD_PID=
69
70     echo "##### return $RETURN"
71     if [ "$RETURN" = "$CODE" ] ; then
72         echo "##### ssh connection test returned as desired"
73         return 0
74     else
75         echo "##### ssh connection test failed.  expected return code $CODE"
76         return 1
77     fi
78 }
79
80 SSHD_PID=
81
82 ## setup trap
83 trap failed_cleanup EXIT
84
85
86 ######################################################################
87 ### SETUP VARIABLES
88
89 ## set up some variables to ensure that we're operating strictly in
90 ## the tests, not system-wide:
91
92 # set up temp dir
93
94 # NOTE: /tmp can not be used as the temp dir here, since the
95 # permissions on /tmp are usually such that they will not pass the
96 # monkeysphere/ssh path permission checking.  If you need to use a
97 # different location than the current source, please set $TMPDIR
98 # somewhere with tighter permissions.
99
100 mkdir -p "$TESTDIR"/tmp
101 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
102
103 # Use the local copy of executables first, instead of system ones.
104 # This should help us test without installing.
105 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
106
107 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
108 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
109 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
110 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
111
112 export MONKEYSPHERE_CHECK_KEYSERVER=false
113 # example.org does not respond to the HKP port, so this should cause
114 # any keyserver connection attempts that do happen (they shouldn't!)
115 # to hang, so we'll notice them:
116 export MONKEYSPHERE_KEYSERVER=example.org
117
118 export MONKEYSPHERE_LOG_LEVEL=DEBUG
119 export MONKEYSPHERE_CORE_KEYLENGTH=1024
120 export MONKEYSPHERE_PROMPT=false
121
122 export SSHD_CONFIG="$TEMPDIR"/sshd_config
123 export SOCKET="$TEMPDIR"/ssh-socket
124
125 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
126 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
127 # $DISPLAY (if one exists) because this test suite should not be doing
128 # *anything* with any running X11 session.
129 export DISPLAY=monkeys
130
131 ## make sure that the version number matches the debian changelog
132 ## (don't bother if this is being run from the tests).
133
134 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
135     echo
136     echo "##################################################"
137     echo "### checking version string match..."
138     repver=$(monkeysphere version)
139     debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
140     if [ "$repver" = "$debver" ] ; then
141         echo "Versions match!"
142     else
143         printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
144         exit 1
145     fi
146 fi
147
148 ######################################################################
149 ### CONFIGURE ENVIRONMENTS
150
151 # copy in admin and testuser home to tmp
152 echo
153 echo "##################################################"
154 echo "### configuring testuser home..."
155 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
156 # set up environment for testuser
157 export TESTHOME="$TEMPDIR"/testuser
158 export GNUPGHOME="$TESTHOME"/.gnupg
159 chmod 0700 "$GNUPGHOME"
160 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
161 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
162 cat <<EOF >> "$TESTHOME"/.ssh/config
163 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
164 IdentityFile $TESTHOME/.ssh/no-such-identity
165 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
166 EOF
167 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
168 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
169 EOF
170 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
171
172 echo
173 echo "##################################################"
174 echo "### configuring admin home..."
175 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
176
177 # set up sshd
178 echo
179 echo "##################################################"
180 echo "### configuring sshd..."
181 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
182 # write the sshd_config
183 cat <<EOF >> "$SSHD_CONFIG"
184 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
185 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
186 EOF
187
188
189 ######################################################################
190 ### SERVER HOST SETUP
191
192 # import host key
193 echo
194 echo "##################################################"
195 echo "### import host key..."
196 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
197 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
198
199 echo
200 echo "##################################################"
201 echo "### getting host key fingerprint..."
202 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
203 echo "$HOSTKEYID"
204
205 # change host key expiration
206 echo
207 echo "##################################################"
208 echo "### setting host key expiration..."
209 monkeysphere-host set-expire 1
210 # FIXME: how do we check that the expiration has really been set?
211
212 # certify host key with the "Admin's Key".
213 # (this would normally be done via keyservers)
214 echo
215 echo "##################################################"
216 echo "### certifying server host key..."
217 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
218 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
219
220 # FIXME: add revoker?
221
222 # FIXME: how can we test publish-key without flooding junk into the
223 # keyservers?
224
225 # FIXME: should we run "diagnostics" here to test setup?
226
227
228 ######################################################################
229 ### SERVER AUTHENTICATION SETUP
230
231 # set up monkeysphere authentication
232 echo
233 echo "##################################################"
234 echo "### setup monkeysphere authentication..."
235 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
236 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
237 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
238 EOF
239 monkeysphere-authentication setup
240 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
241
242 # add admin as identity certifier for testhost
243 echo
244 echo "##################################################"
245 echo "### adding admin as certifier..."
246 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
247
248 echo
249 echo "##################################################"
250 echo "### list certifiers..."
251 monkeysphere-authentication list-certifiers
252
253 # FIXME: should we run "diagnostics" here to test setup?
254
255 ######################################################################
256 ### TESTUSER SETUP
257
258 # generate an auth subkey for the test user that expires in 2 days
259 echo
260 echo "##################################################"
261 echo "### generating key for testuser..."
262 monkeysphere gen-subkey
263
264 # add server key to testuser keychain
265 echo
266 echo "##################################################"
267 echo "### export server key to testuser..."
268 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
269
270 # teach the "server" about the testuser's key
271 echo
272 echo "##################################################"
273 echo "### export testuser key to server..."
274 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
275
276 # update authorized_keys for user
277 echo
278 echo "##################################################"
279 echo "### update server authorized_keys file for this testuser..."
280 monkeysphere-authentication update-users $(whoami)
281 # FIXME: this is maybe not failing properly for:
282 # ms: improper group or other writability on path '/tmp'.
283
284
285 ######################################################################
286 ### TESTS
287
288 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
289 # the identity before connection.  This should work in both directions!
290 echo
291 echo "##################################################"
292 echo "### ssh connection test for success..."
293 ssh_test
294
295 # remove the testuser's authorized_user_ids file, update, and make
296 # sure that the ssh authentication FAILS
297 echo
298 echo "##################################################"
299 echo "### removing testuser authorized_user_ids and updating..."
300 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
301 monkeysphere-authentication update-users $(whoami)
302 echo
303 echo "##################################################"
304 echo "### ssh connection test for server authentication denial..."
305 ssh_test 255
306 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
307
308 # put improper permissions on authorized_user_ids file, update, and
309 # make sure ssh authentication FAILS
310 echo
311 echo "##################################################"
312 echo "### setting group writability on authorized_user_ids and updating..."
313 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
314 monkeysphere-authentication update-users $(whoami)
315 echo
316 echo "##################################################"
317 echo "### ssh connection test for server authentication denial..."
318 ssh_test 255
319 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
320 echo
321 echo "##################################################"
322 echo "### setting other writability on authorized_user_ids and updating..."
323 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
324 monkeysphere-authentication update-users $(whoami)
325 echo
326 echo "##################################################"
327 echo "### ssh connection test for server authentication denial..."
328 ssh_test 255
329 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
330 monkeysphere-authentication update-users $(whoami)
331
332 # FIXME: addtest: remove admin as id-certifier and check ssh failure
333
334 # FIXME: addtest: add hostname on host key
335 # FIXME: addtest: revoke hostname on host key and check ssh failure
336
337 # addtest: revoke the host key and check ssh failure
338 # test to make sure things are OK after the previous tests:
339 ssh_test
340 echo
341 echo "##################################################"
342 echo "### ssh connection test for server with revoked key..."
343 # generate the revocation certificate and feed it directly to the test
344 # user's keyring (we're not publishing to the keyservers)
345 monkeysphere-host revoke-key | gpg --import
346 ssh_test 255
347
348
349 ######################################################################
350
351 trap - EXIT
352
353 echo
354 echo "##################################################"
355 echo " Monkeysphere basic tests completed successfully!"
356 echo "##################################################"
357
358 cleanup