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