3 # Tests to ensure that the monkeysphere is working
6 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 # Jameson Rollins <jrollins@fifthhorseman.net>
8 # Micah Anderson <micah@riseup.net>
10 # Copyright: 2008-2009
11 # License: GPL v3 or later
13 # these tests should all be able to run as a non-privileged user.
15 # all subcommands in this script should complete without failure:
17 # piped commands should return the code of the first non-zero return
20 ## make sure that the right tools are installed to run the test. the
21 ## test has *more* requirements than plain ol' monkeysphere:
22 which socat || { echo "You must have socat installed to run this test." ; exit 1; }
24 ## FIXME: other checks?
26 # gpg command for test admin user
28 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
32 # first argument is expected return code from ssh connection
38 # start the ssh daemon on the socket
39 echo "##### starting ssh server..."
40 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
43 # wait until the socket is created before continuing
44 while [ ! -S "$SOCKET" ] ; do
50 # make a client connection to the socket
51 echo "##### starting ssh client..."
53 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
56 # kill the sshd process if it's still running
62 echo "##### return $RETURN"
63 if [ "$RETURN" = "$CODE" ] ; then
64 echo "##### ssh connection test returned as desired"
67 echo "##### ssh connection test failed. expected return code $CODE"
73 # FIXME: can we be more verbose here?
75 read -p "press enter to cleanup and remove tmp:"
81 if (gpg --quick-random --version >/dev/null 2>&1) ; then
83 elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
84 echo debug-quick-random
89 echo "### removing temp dir..."
92 if [ "$SSHD_PID" ] ; then
93 echo "### killing off lingering sshd..."
103 trap failed_cleanup EXIT
107 ## set up some variables to ensure that we're operating strictly in
108 ## the tests, not system-wide:
110 export TESTDIR=$(pwd)
113 TEMPDIR="$TESTDIR"/tmp
114 if [ -e "$TEMPDIR" ] ; then
115 echo "tempdir '$TEMPDIR' already exists."
120 # Use the local copy of executables first, instead of system ones.
121 # This should help us test without installing.
122 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
124 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
125 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
126 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
127 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
128 export MONKEYSPHERE_CHECK_KEYSERVER=false
129 export MONKEYSPHERE_LOG_LEVEL=DEBUG
131 export SSHD_CONFIG="$TEMPDIR"/sshd_config
132 export SOCKET="$TEMPDIR"/ssh-socket
134 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
135 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
136 # $DISPLAY (if one exists) because this test suite should not be doing
137 # *anything* with any running X11 session.
138 export DISPLAY=monkeys
140 ### CONFIGURE ENVIRONMENTS
142 # copy in admin and testuser home to tmp
143 echo "### copying admin and testuser homes..."
144 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
145 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
147 # set up environment for testuser
148 TESTHOME="$TEMPDIR"/testuser
149 export GNUPGHOME="$TESTHOME"/.gnupg
150 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
151 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
152 cat <<EOF >> "$TESTHOME"/.ssh/config
153 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
154 IdentityFile $TESTHOME/.ssh/no-such-identity
155 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
157 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
158 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
160 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
163 echo "### configuring sshd..."
164 cp etc/ssh/sshd_config "$SSHD_CONFIG"
165 # write the sshd_config
166 cat <<EOF >> "$SSHD_CONFIG"
167 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
168 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
171 # set up monkeysphere-server
172 echo "### configuring monkeysphere..."
173 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
174 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication
175 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/authorized_keys
176 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
177 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
178 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
179 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
181 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
182 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/authentication/sphere/pubring.gpg
183 keyring ${MONKEYSPHERE_SYSDATADIR}/authentication/core/pubring.gpg
189 # create a new host key
190 echo "### generating server key..."
191 # add gpg.conf with quick-random
192 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
193 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
194 # remove the gpg.conf
195 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
197 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
199 # certify it with the "Admin's Key".
200 # (this would normally be done via keyservers)
201 echo "### certifying server key..."
202 monkeysphere-authentication expert gpg-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
203 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
205 # FIXME: how can we test publish-key without flooding junk into the
208 # add admin as identity certifier for testhost
209 echo "### adding admin as certifier..."
210 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
215 # generate an auth subkey for the test user that expires in 2 days
216 echo "### generating key for testuser..."
217 monkeysphere gen-subkey --expire 2
219 # add server key to testuser keychain
220 echo "### export server key to testuser..."
221 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
223 # teach the "server" about the testuser's key
224 echo "### export testuser key to server..."
225 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
226 echo "### update server authorized_keys file for this testuser..."
227 monkeysphere-authentication update-users $(whoami)
229 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
230 # the identity before connection. This should work in both directions!
231 echo "### ssh connection test for success..."
234 # remove the testuser's authorized_user_ids file, update, and make
235 # sure that the ssh authentication FAILS
236 echo "### removing testuser authorized_user_ids and updating..."
237 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
238 monkeysphere-authentication update-users $(whoami)
239 echo "### ssh connection test for server authentication denial..."
241 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
243 # put improper permissions on authorized_user_ids file, update, and
244 # make sure ssh authentication FAILS
245 echo "### setting group writability on authorized_user_ids and updating..."
246 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
247 monkeysphere-authentication update-users $(whoami)
248 echo "### ssh connection test for server authentication denial..."
250 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
251 echo "### setting other writability on authorized_user_ids and updating..."
252 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
253 monkeysphere-authentication update-users $(whoami)
254 echo "### ssh connection test for server authentication denial..."
256 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
262 echo "Monkeysphere basic tests completed successfully!"