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