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