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