add list-certifiers to the basic test
[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 ## FIXME: other checks?
30
31 ######################################################################
32 ### FUNCTIONS
33
34 # gpg command for test admin user
35 gpgadmin() {
36     chmod 0700 "$TEMPDIR"/admin
37     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
38 }
39
40 # test ssh connection
41 # first argument is expected return code from ssh connection
42 ssh_test() {
43     umask 0077
44
45     CODE=${1:-0}
46
47     # start the ssh daemon on the socket
48     echo "##### starting ssh server..."
49     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
50     SSHD_PID="$!"
51
52     # wait until the socket is created before continuing
53     while [ ! -S "$SOCKET" ] ; do
54         sleep 1
55     done
56
57     set +e
58
59     # make a client connection to the socket
60     echo "##### starting ssh client..."
61     ssh-agent bash -c \
62         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
63     RETURN="$?"
64
65     # kill the sshd process if it's still running
66     kill "$SSHD_PID"
67     SSHD_PID=
68
69     set -e
70
71     echo "##### return $RETURN"
72     if [ "$RETURN" = "$CODE" ] ; then
73         echo "##### ssh connection test returned as desired"
74         return 0
75     else
76         echo "##### ssh connection test failed.  expected return code $CODE"
77         return 1
78     fi
79 }
80
81 SSHD_PID=
82
83 ## setup trap
84 trap failed_cleanup EXIT
85
86
87 ######################################################################
88 ### SETUP VARIABLES
89
90 ## set up some variables to ensure that we're operating strictly in
91 ## the tests, not system-wide:
92
93 # set up temp dir
94
95 # NOTE: /tmp can not be used as the temp dir here, since the
96 # permissions on /tmp are usually such that they will not pass the
97 # monkeysphere/ssh path permission checking.  If you need to use a
98 # different location than the current source, please set $TMPDIR
99 # somewhere with tighter permissions.
100
101 mkdir -p "$TESTDIR"/tmp
102 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
103
104 # Use the local copy of executables first, instead of system ones.
105 # This should help us test without installing.
106 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
107
108 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
109 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
110 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
111 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
112 export MONKEYSPHERE_CHECK_KEYSERVER=false
113 export MONKEYSPHERE_LOG_LEVEL=DEBUG
114 export MONKEYSPHERE_CORE_KEYLENGTH=1024
115
116 export SSHD_CONFIG="$TEMPDIR"/sshd_config
117 export SOCKET="$TEMPDIR"/ssh-socket
118
119 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
120 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
121 # $DISPLAY (if one exists) because this test suite should not be doing
122 # *anything* with any running X11 session.
123 export DISPLAY=monkeys
124
125
126 ######################################################################
127 ### CONFIGURE ENVIRONMENTS
128
129 # copy in admin and testuser home to tmp
130 echo "##################################################"
131 echo "### configuring testuser home..."
132 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
133 # set up environment for testuser
134 export TESTHOME="$TEMPDIR"/testuser
135 export GNUPGHOME="$TESTHOME"/.gnupg
136 chmod 0700 "$GNUPGHOME"
137 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
138 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
139 cat <<EOF >> "$TESTHOME"/.ssh/config
140 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
141 IdentityFile $TESTHOME/.ssh/no-such-identity
142 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
143 EOF
144 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
145 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
146 EOF
147 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
148
149 echo "##################################################"
150 echo "### configuring admin home..."
151 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
152
153 # set up sshd
154 echo "##################################################"
155 echo "### configuring sshd..."
156 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
157 # write the sshd_config
158 cat <<EOF >> "$SSHD_CONFIG"
159 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
160 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
161 EOF
162
163
164 ######################################################################
165 ### SERVER HOST SETUP
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 echo "$HOSTKEYID"
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/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 monkeysphere-authentication add-id-certifier -y "$TEMPDIR"/admin/.gnupg/pubkey.gpg
217
218 echo "##################################################"
219 echo "### list certifiers..."
220 monkeysphere-authentication list-certifiers
221
222 # FIXME: should we run "diagnostics" here to test setup?
223
224 ######################################################################
225 ### TESTUSER SETUP
226
227 # generate an auth subkey for the test user that expires in 2 days
228 echo "##################################################"
229 echo "### generating key for testuser..."
230 monkeysphere gen-subkey --expire 2
231
232 # add server key to testuser keychain
233 echo "##################################################"
234 echo "### export server key to testuser..."
235 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
236
237 # teach the "server" about the testuser's key
238 echo "##################################################"
239 echo "### export testuser key to server..."
240 gpg --export testuser | monkeysphere-authentication expert gpg-cmd --import
241
242 # update authorized_keys for user
243 echo "##################################################"
244 echo "### update server authorized_keys file for this testuser..."
245 monkeysphere-authentication update-users $(whoami)
246 # FIXME: NOT FAILING PROPERLY FOR:
247 # ms: improper group or other writability on path '/tmp'.
248
249
250 ######################################################################
251 ### TESTS
252
253 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
254 # the identity before connection.  This should work in both directions!
255 echo "##################################################"
256 echo "### ssh connection test for success..."
257 ssh_test
258
259 # remove the testuser's authorized_user_ids file, update, and make
260 # sure that the ssh authentication FAILS
261 echo "##################################################"
262 echo "### removing testuser authorized_user_ids and updating..."
263 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
264 monkeysphere-authentication update-users $(whoami)
265 echo "##################################################"
266 echo "### ssh connection test for server authentication denial..."
267 ssh_test 255
268 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
269
270 # put improper permissions on authorized_user_ids file, update, and
271 # make sure ssh authentication FAILS
272 echo "##################################################"
273 echo "### setting group writability on authorized_user_ids and updating..."
274 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
275 monkeysphere-authentication update-users $(whoami)
276 echo "##################################################"
277 echo "### ssh connection test for server authentication denial..."
278 ssh_test 255
279 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
280 echo "##################################################"
281 echo "### setting other writability on authorized_user_ids and updating..."
282 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
283 monkeysphere-authentication update-users $(whoami)
284 echo "##################################################"
285 echo "### ssh connection test for server authentication denial..."
286 ssh_test 255
287 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
288
289 # FIXME: addtest: remove admin as id-certifier and check ssh failure
290
291 # FIXME: addtest: add hostname on host key
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
297 ######################################################################
298
299 trap - EXIT
300
301 echo "##################################################"
302 echo " Monkeysphere basic tests completed successfully!"
303 echo "##################################################"
304
305 cleanup