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