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