bcade42efead44942cdd5eec2303a8afcec9068e
[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 # FIXME: I believe that /tmp can not be used as the temp dir here,
95 # since the permissions on /tmp are usually such that they will not
96 # pass the monkeysphere/ssh path permission checking.  at least this
97 # is my experience.
98 #TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
99 TEMPDIR=$(mktemp -d "${TESTDIR}/tmp/monkeyspheretest.XXXXXXX")
100
101 # Use the local copy of executables first, instead of system ones.
102 # This should help us test without installing.
103 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
104
105 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
106 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
107 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
108 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
109 export MONKEYSPHERE_CHECK_KEYSERVER=false
110 export MONKEYSPHERE_LOG_LEVEL=DEBUG
111 export MONKEYSPHERE_CORE_KEYLENGTH=1024
112
113 export SSHD_CONFIG="$TEMPDIR"/sshd_config
114 export SOCKET="$TEMPDIR"/ssh-socket
115
116 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
117 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
118 # $DISPLAY (if one exists) because this test suite should not be doing
119 # *anything* with any running X11 session.
120 export DISPLAY=monkeys
121
122
123 ######################################################################
124 ### CONFIGURE ENVIRONMENTS
125
126 # copy in admin and testuser home to tmp
127 echo "##################################################"
128 echo "### configuring testuser home..."
129 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
130 # set up environment for testuser
131 export TESTHOME="$TEMPDIR"/testuser
132 export GNUPGHOME="$TESTHOME"/.gnupg
133 chmod 0700 "$GNUPGHOME"
134 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
135 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
136 cat <<EOF >> "$TESTHOME"/.ssh/config
137 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
138 IdentityFile $TESTHOME/.ssh/no-such-identity
139 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
140 EOF
141 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
142 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
143 EOF
144 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
145
146 echo "##################################################"
147 echo "### configuring admin home..."
148 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
149
150 # set up sshd
151 echo "##################################################"
152 echo "### configuring sshd..."
153 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
154 # write the sshd_config
155 cat <<EOF >> "$SSHD_CONFIG"
156 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
157 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
158 EOF
159
160
161 ######################################################################
162 ### SERVER HOST SETUP
163
164 # import host key
165 echo "##################################################"
166 echo "### testing host key importing..."
167 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
168 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
169
170 # change host key expiration
171 echo "##################################################"
172 echo "### setting host key expiration..."
173 monkeysphere-host set-expire 1
174 monkeysphere-host show-key
175 # FIXME: how do we check that the expiration has really been set?
176
177 echo "##################################################"
178 echo "### getting host key fingerprint..."
179 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\  )
180 echo "$HOSTKEYID"
181
182 # certify host key with the "Admin's Key".
183 # (this would normally be done via keyservers)
184 echo "##################################################"
185 echo "### certifying server host key..."
186 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
187 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
188
189 # FIXME: add revoker?
190
191 # FIXME: how can we test publish-key without flooding junk into the
192 # keyservers?
193
194 # FIXME: should we run "diagnostics" here to test setup?
195
196
197 ######################################################################
198 ### SERVER AUTHENTICATION SETUP
199
200 # set up monkeysphere authentication
201 echo "##################################################"
202 echo "### setup monkeysphere authentication..."
203 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
204 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
205 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
206 EOF
207 monkeysphere-authentication setup
208 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
209
210 # add admin as identity certifier for testhost
211 echo "##################################################"
212 echo "### adding admin as certifier..."
213 monkeysphere-authentication add-id-certifier -y "$TEMPDIR"/admin/.gnupg/pubkey.gpg
214
215 # FIXME: should we run "diagnostics" here to test setup?
216
217
218 ######################################################################
219 ### TESTUSER SETUP
220
221 # generate an auth subkey for the test user that expires in 2 days
222 echo "##################################################"
223 echo "### generating key for testuser..."
224 monkeysphere gen-subkey --expire 2
225
226 # add server key to testuser keychain
227 echo "##################################################"
228 echo "### export server key to testuser..."
229 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
230
231 # teach the "server" about the testuser's key
232 echo "##################################################"
233 echo "### export testuser key to server..."
234 gpg --export testuser | monkeysphere-authentication expert gpg-cmd --import
235
236 # update authorized_keys for user
237 echo "##################################################"
238 echo "### update server authorized_keys file for this testuser..."
239 monkeysphere-authentication update-users $(whoami)
240 # FIXME: NOT FAILING PROPERLY FOR:
241 # ms: improper group or other writability on path '/tmp'.
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