set DISPLAY in test suite so that passphrase prompting falls through.
[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 export DISPLAY=monkeys
119
120 ### CONFIGURE ENVIRONMENTS
121
122 # copy in admin and testuser home to tmp
123 echo "### copying admin and testuser homes..."
124 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
125 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
126
127 # set up environment for testuser
128 TESTHOME="$TEMPDIR"/testuser
129 export GNUPGHOME="$TESTHOME"/.gnupg
130 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
131 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
132 cat <<EOF >> "$TESTHOME"/.ssh/config
133 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
134 IdentityFile $TESTHOME/.ssh/no-such-identity
135 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
136 EOF
137 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
138 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
139 EOF
140 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
141
142 # set up sshd
143 echo "### configuring sshd..."
144 cp etc/ssh/sshd_config "$SSHD_CONFIG"
145 # write the sshd_config
146 cat <<EOF >> "$SSHD_CONFIG"
147 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
148 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
149 EOF
150
151 # set up monkeysphere-server
152 echo "### configuring monkeysphere..."
153 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
154 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
155 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
156 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
157 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
158 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
159 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
160 EOF
161 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
162 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
163 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
164 EOF
165
166
167 ### SERVER TESTS
168
169 # create a new host key
170 echo "### generating server key..."
171 # add gpg.conf with quick-random
172 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
173 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
174 # remove the gpg.conf
175 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
176
177 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
178
179 # certify it with the "Admin's Key".
180 # (this would normally be done via keyservers)
181 echo "### certifying server key..."
182 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
183 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
184
185 # FIXME: how can we test publish-key without flooding junk into the
186 # keyservers?
187
188 # add admin as identity certifier for testhost
189 echo "### adding admin as certifier..."
190 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
191
192
193 ### TESTUSER TESTS
194
195 # generate an auth subkey for the test user that expires in 2 days
196 echo "### generating key for testuser..."
197 monkeysphere gen-subkey --expire 2
198
199 # add server key to testuser keychain
200 echo "### export server key to testuser..."
201 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
202
203 # teach the "server" about the testuser's key
204 echo "### export testuser key to server..."
205 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
206 echo "### update server authorized_keys file for this testuser..."
207 monkeysphere-server update-users $(whoami)
208
209 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
210 # the identity before connection.  This should work in both directions!
211 echo "### ssh connection test for success..."
212 ssh_test
213
214 # remove the testuser's authorized_user_ids file, update, and make
215 # sure that the ssh authentication FAILS
216 echo "### removing testuser authorized_user_ids and updating..."
217 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
218 monkeysphere-server update-users $(whoami)
219 echo "### ssh connection test for server authentication denial..."
220 ssh_test 255
221 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
222
223 # put improper permissions on authorized_user_ids file, update, and
224 # make sure ssh authentication FAILS
225 echo "### setting group writability on authorized_user_ids and updating..."
226 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
227 monkeysphere-server update-users $(whoami)
228 echo "### ssh connection test for server authentication denial..."
229 ssh_test 255
230 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
231 echo "### setting other writability on authorized_user_ids and updating..."
232 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
233 monkeysphere-server update-users $(whoami)
234 echo "### ssh connection test for server authentication denial..."
235 ssh_test 255
236 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
237
238
239 trap - EXIT
240
241 echo
242 echo "Monkeysphere basic tests completed successfully!"
243 echo
244
245 cleanup