Merge commit 'jrollins/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 # 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 mkdir "$TEMPDIR"
124
125 # Use the local copy of executables first, instead of system ones.
126 # This should help us test without installing.
127 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
128
129 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
130 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
131 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
132 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
133 export MONKEYSPHERE_CHECK_KEYSERVER=false
134 export MONKEYSPHERE_LOG_LEVEL=DEBUG
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 "### copying admin and testuser homes..."
152 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
153 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
154
155 # set up environment for testuser
156 TESTHOME="$TEMPDIR"/testuser
157 export GNUPGHOME="$TESTHOME"/.gnupg
158 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
159 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
160 cat <<EOF >> "$TESTHOME"/.ssh/config
161 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
162 IdentityFile $TESTHOME/.ssh/no-such-identity
163 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
164 EOF
165 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
166 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
167 EOF
168 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
169
170 # set up sshd
171 echo "##################################################"
172 echo "### configuring sshd..."
173 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
174 # write the sshd_config
175 cat <<EOF >> "$SSHD_CONFIG"
176 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
177 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
178 EOF
179
180
181 ######################################################################
182 ### SERVER HOST SETUP
183
184 # set up monkeysphere host
185 echo "##################################################"
186 echo "### configuring monkeysphere host..."
187 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
188
189 # create a new host key
190 echo "##################################################"
191 echo "### generating server host key..."
192 # add gpg.conf with quick-random
193 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
194 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
195 # remove the gpg.conf
196 rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
197
198 # FIXME: need to test import-key as well
199
200 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
201
202 # certify it with the "Admin's Key".
203 # (this would normally be done via keyservers)
204 echo "##################################################"
205 echo "### certifying server host key..."
206 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
207 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
208
209 # FIXME: add revoker?
210
211 # FIXME: how can we test publish-key without flooding junk into the
212 # keyservers?
213
214 # FIXME: should we run "diagnostics" here to test setup?
215
216
217 ######################################################################
218 ### SERVER AUTHENTICATION SETUP
219
220 # set up monkeysphere authentication
221 echo "##################################################"
222 echo "### setup monkeysphere authentication..."
223 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/{authorized_keys,core,sphere,tmp}
224 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
225 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
226 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
227 EOF
228 monkeysphere-authentication setup
229 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
230
231 # add admin as identity certifier for testhost
232 echo "##################################################"
233 echo "### adding admin as certifier..."
234 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
235
236 # FIXME: should we run "diagnostics" here to test setup?
237
238
239 ######################################################################
240 ### TESTUSER SETUP
241
242 # generate an auth subkey for the test user that expires in 2 days
243 echo "##################################################"
244 echo "### generating key for testuser..."
245 monkeysphere gen-subkey --expire 2
246
247 # add server key to testuser keychain
248 echo "##################################################"
249 echo "### export server key to testuser..."
250 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
251
252 # teach the "server" about the testuser's key
253 echo "##################################################"
254 echo "### export testuser key to server..."
255 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
256
257 # update authorized_keys for user
258 echo "##################################################"
259 echo "### update server authorized_keys file for this testuser..."
260 monkeysphere-authentication update-users $(whoami)
261
262
263 ######################################################################
264 ### TESTS
265
266 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
267 # the identity before connection.  This should work in both directions!
268 echo "##################################################"
269 echo "### ssh connection test for success..."
270 ssh_test
271
272 # remove the testuser's authorized_user_ids file, update, and make
273 # sure that the ssh authentication FAILS
274 echo "##################################################"
275 echo "### removing testuser authorized_user_ids and updating..."
276 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
277 monkeysphere-authentication update-users $(whoami)
278 echo "##################################################"
279 echo "### ssh connection test for server authentication denial..."
280 ssh_test 255
281 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
282
283 # put improper permissions on authorized_user_ids file, update, and
284 # make sure ssh authentication FAILS
285 echo "##################################################"
286 echo "### setting group writability on authorized_user_ids and updating..."
287 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
288 monkeysphere-authentication update-users $(whoami)
289 echo "##################################################"
290 echo "### ssh connection test for server authentication denial..."
291 ssh_test 255
292 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
293 echo "##################################################"
294 echo "### setting other writability on authorized_user_ids and updating..."
295 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
296 monkeysphere-authentication update-users $(whoami)
297 echo "##################################################"
298 echo "### ssh connection test for server authentication denial..."
299 ssh_test 255
300 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
301
302 # FIXME: addtest: remove admin as id-certifier and check ssh failure
303
304 # FIXME: addtest: revoke hostname on host key and check ssh failure
305
306 # FIXME: addtest: revoke the host key and check ssh failure
307
308
309 ######################################################################
310
311 trap - EXIT
312
313 echo "##################################################"
314 echo " Monkeysphere basic tests completed successfully!"
315 echo "##################################################"
316
317 cleanup