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