couple of modifications to the test:
[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:"$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 # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
133 # into trying to use the user's key, instead of the testuser's key
134 unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
135
136 export SSHD_CONFIG="$TEMPDIR"/sshd_config
137 export SOCKET="$TEMPDIR"/ssh-socket
138
139 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
140 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
141 # $DISPLAY (if one exists) because this test suite should not be doing
142 # *anything* with any running X11 session.
143 export DISPLAY=monkeys
144
145
146 ######################################################################
147 ### CONFIGURE ENVIRONMENTS
148
149 # copy in admin and testuser home to tmp
150 echo
151 echo "##################################################"
152 echo "### configuring testuser home..."
153 (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
154
155 # set up environment for testuser
156 export TESTHOME="$TEMPDIR"/testuser
157 export GNUPGHOME="$TESTHOME"/.gnupg
158 chmod 0700 "$GNUPGHOME"
159 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
160 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
161 cat <<EOF >> "$TESTHOME"/.ssh/config
162 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
163 IdentityFile $TESTHOME/.ssh/no-such-identity
164 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
165 EOF
166 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
167 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
168 EOF
169 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
170
171 echo
172 echo "##################################################"
173 echo "### configuring admin home..."
174 (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
175
176 # set up sshd
177 echo
178 echo "##################################################"
179 echo "### configuring sshd..."
180 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
181 # write the sshd_config
182 cat <<EOF >> "$SSHD_CONFIG"
183 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
184 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
185 EOF
186
187
188 ######################################################################
189 ### SERVER HOST SETUP
190
191 # import host key
192 echo
193 echo "##################################################"
194 echo "### import host key..."
195 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
196 monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
197
198 echo
199 echo "##################################################"
200 echo "### getting host key fingerprint..."
201 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
202 echo "$HOSTKEYID"
203
204 # change host key expiration
205 echo
206 echo "##################################################"
207 echo "### setting host key expiration..."
208 monkeysphere-host set-expire 1
209 # FIXME: how do we check that the expiration has really been set?
210
211 # certify host key with the "Admin's Key".
212 # (this would normally be done via keyservers)
213 echo
214 echo "##################################################"
215 echo "### certifying server host key..."
216 < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
217 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
218
219 # FIXME: add revoker?
220
221 # FIXME: how can we test publish-key without flooding junk into the
222 # keyservers?
223
224 # FIXME: should we run "diagnostics" here to test setup?
225
226
227 ######################################################################
228 ### SERVER AUTHENTICATION SETUP
229
230 # set up monkeysphere authentication
231 echo
232 echo "##################################################"
233 echo "### setup monkeysphere authentication..."
234 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
235 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
236 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
237 EOF
238 monkeysphere-authentication setup
239 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
240
241 # add admin as identity certifier for testhost
242 echo
243 echo "##################################################"
244 echo "### adding admin as certifier..."
245 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
246
247 echo
248 echo "##################################################"
249 echo "### list certifiers..."
250 monkeysphere-authentication list-certifiers
251
252 # FIXME: should we run "diagnostics" here to test setup?
253
254 ######################################################################
255 ### TESTUSER SETUP
256
257 # generate an auth subkey for the test user that expires in 2 days
258 echo
259 echo "##################################################"
260 echo "### generating key for testuser..."
261 monkeysphere gen-subkey
262
263 # add server key to testuser keychain
264 echo
265 echo "##################################################"
266 echo "### export server key to testuser..."
267 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
268
269 # teach the "server" about the testuser's key
270 echo
271 echo "##################################################"
272 echo "### export testuser key to server..."
273 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
274
275 # update authorized_keys for user
276 echo
277 echo "##################################################"
278 echo "### update server authorized_keys file for this testuser..."
279 monkeysphere-authentication update-users $(whoami)
280 # FIXME: this is maybe not failing properly for:
281 # ms: improper group or other writability on path '/tmp'.
282
283 ######################################################################
284 ### TESTS
285
286 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
287 # the identity before connection.  This should work in both directions!
288 echo
289 echo "##################################################"
290 echo "### ssh connection test for success..."
291 ssh_test
292
293 # remove the testuser's authorized_user_ids file, update, and make
294 # sure that the ssh authentication FAILS
295 echo
296 echo "##################################################"
297 echo "### removing testuser authorized_user_ids and updating..."
298 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
299 monkeysphere-authentication update-users $(whoami)
300 echo
301 echo "##################################################"
302 echo "### ssh connection test for server authentication denial..."
303 ssh_test 255
304 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
305
306 # put improper permissions on authorized_user_ids file, update, and
307 # make sure ssh authentication FAILS
308 echo
309 echo "##################################################"
310 echo "### setting group writability on authorized_user_ids and updating..."
311 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
312 monkeysphere-authentication update-users $(whoami)
313 echo
314 echo "##################################################"
315 echo "### ssh connection test for server authentication denial..."
316 ssh_test 255
317 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
318 echo
319 echo "##################################################"
320 echo "### setting other writability on authorized_user_ids and updating..."
321 chmod o+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 o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
328 monkeysphere-authentication update-users $(whoami)
329
330 # FIXME: addtest: remove admin as id-certifier and check ssh failure
331
332 # FIXME: addtest: add hostname on host key
333 # FIXME: addtest: revoke hostname on host key and check ssh failure
334
335 # addtest: revoke the host key and check ssh failure
336 # test to make sure things are OK after the previous tests:
337 ssh_test
338 echo
339 echo "##################################################"
340 echo "### ssh connection test for server with revoked key..."
341 # generate the revocation certificate and feed it directly to the test
342 # user's keyring (we're not publishing to the keyservers)
343 monkeysphere-host revoke-key | gpg --import
344 ssh_test 255
345
346
347 ######################################################################
348
349 trap - EXIT
350
351 echo
352 echo "##################################################"
353 echo " Monkeysphere basic tests completed successfully!"
354 echo "##################################################"
355
356 cleanup