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