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