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