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