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