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