ad72a2282dbd8e84af3b92daac70e337aae04b11
[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 # all subcommands in this script should complete without failure:
16 set -e
17 # piped commands should return the code of the first non-zero return
18 set -o pipefail
19
20 ## make sure that the right tools are installed to run the test.  the
21 ## test has *more* requirements than plain ol' monkeysphere:
22 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
23
24 ## FIXME: other checks?
25
26 ######################################################################
27 ### FUNCTIONS
28
29 # gpg command for test admin user
30 gpgadmin() {
31     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
32 }
33
34 # test ssh connection
35 # first argument is expected return code from ssh connection
36 ssh_test() {
37     umask 0077
38
39     CODE=${1:-0}
40
41     # start the ssh daemon on the socket
42     echo "##### starting ssh server..."
43     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
44     SSHD_PID="$!"
45
46     # wait until the socket is created before continuing
47     while [ ! -S "$SOCKET" ] ; do
48         sleep 1
49     done
50
51     set +e
52
53     # make a client connection to the socket
54     echo "##### starting ssh client..."
55     ssh-agent bash -c \
56         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
57     RETURN="$?"
58
59     # kill the sshd process if it's still running
60     kill "$SSHD_PID"
61     SSHD_PID=
62
63     set -e
64
65     echo "##### return $RETURN"
66     if [ "$RETURN" = "$CODE" ] ; then
67         echo "##### ssh connection test returned as desired"
68         return 0
69     else
70         echo "##### ssh connection test failed.  expected return code $CODE"
71         return 1
72     fi
73 }
74
75 failed_cleanup() {
76     # FIXME: can we be more verbose here?
77     echo 'FAILED!'
78     read -p "press enter to cleanup and remove tmp:"
79
80     cleanup
81 }
82
83 get_gpg_prng_arg() {
84     if (gpg --quick-random --version >/dev/null 2>&1) ; then
85         echo quick-random
86     elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
87         echo debug-quick-random
88     fi
89 }
90
91 cleanup() {
92     echo "### removing temp dir..."
93     rm -rf "$TEMPDIR"
94
95     if [ "$SSHD_PID" ] ; then
96         echo "### killing off lingering sshd..."
97         kill "$SSHD_PID"
98     fi
99
100     wait
101 }
102
103 SSHD_PID=
104
105 ## setup trap
106 trap failed_cleanup EXIT
107
108
109 ######################################################################
110 ### SETUP VARIABLES
111
112 ## set up some variables to ensure that we're operating strictly in
113 ## the tests, not system-wide:
114
115 export TESTDIR=$(dirname "$0")
116
117 # make temp dir
118 TEMPDIR="$TESTDIR"/tmp
119 if [ -e "$TEMPDIR" ] ; then
120     echo "tempdir '$TEMPDIR' already exists."
121     exit 1
122 fi
123
124 # Use the local copy of executables first, instead of system ones.
125 # This should help us test without installing.
126 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
127
128 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
129 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
130 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
131 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
132 export MONKEYSPHERE_CHECK_KEYSERVER=false
133 export MONKEYSPHERE_LOG_LEVEL=DEBUG
134 export MONKEYSPHERE_CORE_KEYLENGTH=1024
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 ### TEST KEY CONVERSION
147
148 mkdir -p "$TEMPDIR"
149
150 echo "##################################################"
151 echo "### test key conversion..."
152 export GNUPGHOME="$TEMPDIR"
153 # generate a key
154 gpg --batch --quick-random --gen-key <<EOF
155 Key-Type: RSA
156 Key-Length: 1024
157 Key-Usage: sign
158 Name-Real: testtest
159 Expire-Date: 0
160
161 %commit
162 %echo done
163 EOF
164 # get the the key timestamp
165 timestamp=$(gpg --list-key --with-colons --fixed-list-mode | \
166     grep ^pub: | cut -d: -f6)
167 # export the key to a file
168 gpg --export-secret-key | openpgp2ssh > \
169     "$TEMPDIR"/test.pem
170 # reconvert key, and compare to key in gpg keyring
171 diff -u \
172     <(gpg --export-secret-key | hd) \
173     <(PEM2OPENPGP_USAGE_FLAGS=sign,certify \
174     PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest < \
175     "$TEMPDIR"/test.pem | hd )
176
177 # clear out the temp dir
178 rm -rf "$TEMPDIR"
179 mkdir -p "$TEMPDIR"
180
181 ######################################################################
182 ### CONFIGURE ENVIRONMENTS
183
184 # copy in admin and testuser home to tmp
185 echo "##################################################"
186 echo "### copying admin and testuser homes..."
187 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
188 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
189
190 # set up environment for testuser
191 export TESTHOME="$TEMPDIR"/testuser
192 export GNUPGHOME="$TESTHOME"/.gnupg
193 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
194 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
195 cat <<EOF >> "$TESTHOME"/.ssh/config
196 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
197 IdentityFile $TESTHOME/.ssh/no-such-identity
198 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
199 EOF
200 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
201 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
202 EOF
203 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
204
205 # set up sshd
206 echo "##################################################"
207 echo "### configuring sshd..."
208 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
209 # write the sshd_config
210 cat <<EOF >> "$SSHD_CONFIG"
211 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
212 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
213 EOF
214
215
216 ######################################################################
217 ### SERVER HOST SETUP
218
219 # create a new host key
220 echo "##################################################"
221 echo "### testing host key generation..."
222 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
223 # add gpg.conf with quick-random
224 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
225 echo | monkeysphere-host expert gen-key --length 1024 testhost
226
227 # remove the host home for the next test
228 rm -rf "$MONKEYSPHERE_SYSCONFIGDIR"/host
229
230 # import host key
231 echo "##################################################"
232 echo "### testing host key importing..."
233 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
234 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
235
236 # change host key expiration
237 echo "##################################################"
238 echo "### setting host key expiration..."
239 monkeysphere-host set-expire 1
240 monkeysphere-host show-key
241 # FIXME: how do we check that the expiration has really been set?
242
243 echo "##################################################"
244 echo "### getting host key fingerprint..."
245 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
246
247 # certify host key with the "Admin's Key".
248 # (this would normally be done via keyservers)
249 echo "##################################################"
250 echo "### certifying server host key..."
251 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
252 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
253
254 # FIXME: add revoker?
255
256 # FIXME: how can we test publish-key without flooding junk into the
257 # keyservers?
258
259 # FIXME: should we run "diagnostics" here to test setup?
260
261
262 ######################################################################
263 ### SERVER AUTHENTICATION SETUP
264
265 # set up monkeysphere authentication
266 echo "##################################################"
267 echo "### setup monkeysphere authentication..."
268 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
269 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
270 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
271 EOF
272 monkeysphere-authentication setup
273 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
274
275 # add admin as identity certifier for testhost
276 echo "##################################################"
277 echo "### adding admin as certifier..."
278 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
279
280 # FIXME: should we run "diagnostics" here to test setup?
281
282
283 ######################################################################
284 ### TESTUSER SETUP
285
286 # generate an auth subkey for the test user that expires in 2 days
287 echo "##################################################"
288 echo "### generating key for testuser..."
289 monkeysphere gen-subkey --expire 2
290
291 # add server key to testuser keychain
292 echo "##################################################"
293 echo "### export server key to testuser..."
294 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
295
296 # teach the "server" about the testuser's key
297 echo "##################################################"
298 echo "### export testuser key to server..."
299 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
300
301 # update authorized_keys for user
302 echo "##################################################"
303 echo "### update server authorized_keys file for this testuser..."
304 monkeysphere-authentication update-users $(whoami)
305
306
307 ######################################################################
308 ### TESTS
309
310 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
311 # the identity before connection.  This should work in both directions!
312 echo "##################################################"
313 echo "### ssh connection test for success..."
314 ssh_test
315
316 # remove the testuser's authorized_user_ids file, update, and make
317 # sure that the ssh authentication FAILS
318 echo "##################################################"
319 echo "### removing testuser authorized_user_ids and updating..."
320 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
321 monkeysphere-authentication update-users $(whoami)
322 echo "##################################################"
323 echo "### ssh connection test for server authentication denial..."
324 ssh_test 255
325 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
326
327 # put improper permissions on authorized_user_ids file, update, and
328 # make sure ssh authentication FAILS
329 echo "##################################################"
330 echo "### setting group writability on authorized_user_ids and updating..."
331 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
332 monkeysphere-authentication update-users $(whoami)
333 echo "##################################################"
334 echo "### ssh connection test for server authentication denial..."
335 ssh_test 255
336 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
337 echo "##################################################"
338 echo "### setting other writability on authorized_user_ids and updating..."
339 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
340 monkeysphere-authentication update-users $(whoami)
341 echo "##################################################"
342 echo "### ssh connection test for server authentication denial..."
343 ssh_test 255
344 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
345
346 # FIXME: addtest: remove admin as id-certifier and check ssh failure
347
348 # FIXME: addtest: add hostname on host key
349 # FIXME: addtest: revoke hostname on host key and check ssh failure
350
351 # FIXME: addtest: revoke the host key and check ssh failure
352
353
354 ######################################################################
355
356 trap - EXIT
357
358 echo "##################################################"
359 echo " Monkeysphere basic tests completed successfully!"
360 echo "##################################################"
361
362 cleanup