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