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