move away from using MONKEYSPHERE_SERVER_CONFIG in test suite.
[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_SYSSHAREDIR="$TESTDIR"/../src
95 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
96 export MONKEYSPHERE_CHECK_KEYSERVER=false
97 export MONKEYSPHERE_LOG_LEVEL=DEBUG
98
99 export SSHD_CONFIG="$TEMPDIR"/sshd_config
100 export SOCKET="$TEMPDIR"/ssh-socket
101 export SSHD_PID=
102
103
104 ### CONFIGURE ENVIRONMENTS
105
106 # copy in admin and testuser home to tmp
107 echo "### copying admin and testuser homes..."
108 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
109 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
110
111 # set up environment for testuser
112 TESTHOME="$TEMPDIR"/testuser
113 export GNUPGHOME="$TESTHOME"/.gnupg
114 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
115 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
116 cat <<EOF >> "$TESTHOME"/.ssh/config
117 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
118 IdentityFile $TESTHOME/.ssh/no-such-identity
119 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
120 EOF
121 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
122 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
123 EOF
124 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
125
126 # set up sshd
127 echo "### configuring sshd..."
128 cp etc/ssh/sshd_config "$SSHD_CONFIG"
129 # write the sshd_config
130 cat <<EOF >> "$SSHD_CONFIG"
131 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
132 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
133 EOF
134
135 # set up monkeysphere-server
136 echo "### configuring monkeysphere..."
137 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
138 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
139 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
140 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
141 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
142 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
143 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
144 EOF
145 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
146 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
147 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
148 EOF
149
150
151 ### SERVER TESTS
152
153 # create a new host key
154 echo "### generating server key..."
155 # add gpg.conf with quick-random
156 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
157 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
158 # remove the gpg.conf
159 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
160
161 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\  )
162
163 # certify it with the "Admin's Key".
164 # (this would normally be done via keyservers)
165 echo "### certifying server key..."
166 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
167 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
168
169 # FIXME: how can we test publish-key without flooding junk into the
170 # keyservers?
171
172 # add admin as identity certifier for testhost
173 echo "### adding admin as certifier..."
174 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
175
176
177 ### TESTUSER TESTS
178
179 # generate an auth subkey for the test user that expires in 2 days
180 echo "### generating key for testuser..."
181 monkeysphere gen-subkey --expire 2
182
183 # add server key to testuser keychain
184 echo "### export server key to testuser..."
185 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
186
187 # teach the "server" about the testuser's key
188 echo "### export testuser key to server..."
189 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
190 echo "### update server authorized_keys file for this testuser..."
191 monkeysphere-server update-users $(whoami)
192
193 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
194 # the identity before connection.  This should work in both directions!
195 echo "### ssh connection test for success..."
196 ssh_test
197
198 # remove the testuser's authorized_user_ids file and update
199 # authorized_keys file, this is to make sure that the ssh
200 # authentication FAILS...
201 echo "### removing testuser authorized_user_ids and reupdating authorized_keys..."
202 rm -f "$TESTHOME"/.monkeysphere/authorized_user_ids
203 monkeysphere-server update-users $(whoami)
204
205 # make sure the user can NOT connect
206 echo "### ssh connection test for server authentication denial..."
207 ssh_test || ret="$?"
208 if [ "$ret" != '255' ] ; then
209     echo "### connection should have failed!"
210     exit "$ret"
211 fi
212
213 trap - EXIT
214
215 echo
216 echo "Monkeysphere basic tests completed successfully!"
217 echo
218
219 cleanup