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