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