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