fixing check for test suite.
[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 # Copyright: 2008
9 # License: GPL v3 or later
10
11 # these tests should all be able to
12 # as a non-privileged user.
13
14 # all subcommands in this script should complete without failure:
15 set -e
16
17 ## make sure that the right tools are installed to run the test.  the
18 ## test has *more* requirements than plain ol' monkeysphere:
19
20 which socat || { echo "You must have socat installed to run this test." ; exit 1; }
21
22 ## FIXME: other checks?
23
24 # gpg command for test admin user
25 gpgadmin() {
26     GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
27 }
28
29 # test ssh connection
30 # first argument is expected return code from ssh connection
31 ssh_test() {
32     umask 0077
33
34     CODE=${1:-0}
35
36     # start the ssh daemon on the socket
37     echo "##### starting ssh server..."
38     socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
39     SSHD_PID="$!"
40
41     # wait until the socket is created before continuing
42     while [ ! -S "$SOCKET" ] ; do
43         sleep 1
44     done
45
46     set +e
47
48     # make a client connection to the socket
49     echo "##### starting ssh client..."
50     ssh-agent bash -c \
51         "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
52     RETURN="$?"
53
54     # kill the sshd process if it's still running
55     kill "$SSHD_PID"
56
57     set -e
58
59     echo "##### return $RETURN"
60     if [ "$RETURN" = "$CODE" ] ; then
61         echo "##### ssh connection test returned as desired"
62         return 0
63     else
64         echo "##### ssh connection test failed.  expected return code $CODE"
65         return 1
66     fi
67 }
68
69 failed_cleanup() {
70     # FIXME: can we be more verbose here?
71     echo 'FAILED!'
72     read -p "press enter to cleanup and remove tmp:"
73
74     cleanup
75 }
76
77 get_gpg_prng_arg() {
78     if (gpg --quick-random --version >/dev/null 2>&1) ; then
79         echo quick-random
80     elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
81         echo debug-quick-random
82     fi
83 }
84
85 cleanup() {
86     echo "### removing temp dir..."
87     rm -rf "$TEMPDIR"
88
89     wait
90 }
91
92 ## setup trap
93 trap failed_cleanup EXIT
94
95
96 ### SETUP VARIABLES
97 ## set up some variables to ensure that we're operating strictly in
98 ## the tests, not system-wide:
99
100 export TESTDIR=$(pwd)
101
102 # make temp dir
103 TEMPDIR="$TESTDIR"/tmp
104 if [ -e "$TEMPDIR" ] ; then
105     echo "tempdir '$TEMPDIR' already exists."
106     exit 1
107 fi
108 mkdir "$TEMPDIR"
109
110 # Use the local copy of executables first, instead of system ones.
111 # This should help us test without installing.
112 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
113
114 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
115 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
116 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
117 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
118 export MONKEYSPHERE_CHECK_KEYSERVER=false
119 export MONKEYSPHERE_LOG_LEVEL=DEBUG
120
121 export SSHD_CONFIG="$TEMPDIR"/sshd_config
122 export SOCKET="$TEMPDIR"/ssh-socket
123 export SSHD_PID=
124
125 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
126 # back on $SSH_ASKPASS.  Make sure it's not set to the current actual
127 # $DISPLAY (if one exists) because this test suite should not be doing
128 # *anything* with any running X11 session.
129 export DISPLAY=monkeys
130
131 ### CONFIGURE ENVIRONMENTS
132
133 # copy in admin and testuser home to tmp
134 echo "### copying admin and testuser homes..."
135 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
136 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
137
138 # set up environment for testuser
139 TESTHOME="$TEMPDIR"/testuser
140 export GNUPGHOME="$TESTHOME"/.gnupg
141 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
142 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
143 cat <<EOF >> "$TESTHOME"/.ssh/config
144 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
145 IdentityFile $TESTHOME/.ssh/no-such-identity
146 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
147 EOF
148 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
149 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
150 EOF
151 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
152
153 # set up sshd
154 echo "### configuring sshd..."
155 cp etc/ssh/sshd_config "$SSHD_CONFIG"
156 # write the sshd_config
157 cat <<EOF >> "$SSHD_CONFIG"
158 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
159 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
160 EOF
161
162 # set up monkeysphere-server
163 echo "### configuring monkeysphere..."
164 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
165 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
166 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
167 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
168 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
169 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
170 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
171 EOF
172 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
173 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
174 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
175 EOF
176
177
178 ### SERVER TESTS
179
180 # create a new host key
181 echo "### generating server key..."
182 # add gpg.conf with quick-random
183 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
184 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
185 # remove the gpg.conf
186 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
187
188 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
189
190 # certify it with the "Admin's Key".
191 # (this would normally be done via keyservers)
192 echo "### certifying server key..."
193 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
194 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
195
196 # FIXME: how can we test publish-key without flooding junk into the
197 # keyservers?
198
199 # add admin as identity certifier for testhost
200 echo "### adding admin as certifier..."
201 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
202
203
204 ### TESTUSER TESTS
205
206 # generate an auth subkey for the test user that expires in 2 days
207 echo "### generating key for testuser..."
208 monkeysphere gen-subkey --expire 2
209
210 # add server key to testuser keychain
211 echo "### export server key to testuser..."
212 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
213
214 # teach the "server" about the testuser's key
215 echo "### export testuser key to server..."
216 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
217 echo "### update server authorized_keys file for this testuser..."
218 monkeysphere-server update-users $(whoami)
219
220 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
221 # the identity before connection.  This should work in both directions!
222 echo "### ssh connection test for success..."
223 ssh_test
224
225 # remove the testuser's authorized_user_ids file, update, and make
226 # sure that the ssh authentication FAILS
227 echo "### removing testuser authorized_user_ids and updating..."
228 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
229 monkeysphere-server update-users $(whoami)
230 echo "### ssh connection test for server authentication denial..."
231 ssh_test 255
232 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
233
234 # put improper permissions on authorized_user_ids file, update, and
235 # make sure ssh authentication FAILS
236 echo "### setting group writability on authorized_user_ids and updating..."
237 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
238 monkeysphere-server update-users $(whoami)
239 echo "### ssh connection test for server authentication denial..."
240 ssh_test 255
241 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
242 echo "### setting other writability on authorized_user_ids and updating..."
243 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
244 monkeysphere-server update-users $(whoami)
245 echo "### ssh connection test for server authentication denial..."
246 ssh_test 255
247 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
248
249
250 trap - EXIT
251
252 echo
253 echo "Monkeysphere basic tests completed successfully!"
254 echo
255
256 cleanup