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