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