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