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 >/dev/null || { 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=$(dirname "$0")
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/share
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 "$TESTDIR"/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
172 ### SERVER HOST SETUP
174 # set up monkeysphere host
175 echo "### configuring monkeysphere host..."
176 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
178 # create a new host key
179 echo "### generating server host key..."
180 # add gpg.conf with quick-random
181 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
182 echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
183 # remove the gpg.conf
184 rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
186 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
188 # certify it with the "Admin's Key".
189 # (this would normally be done via keyservers)
190 echo "### certifying server host key..."
191 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
192 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
194 # FIXME: how can we test publish-key without flooding junk into the
198 ### SERVER AUTHENTICATION TESTS
200 # set up monkeysphere authentication
201 echo "### configuring monkeysphere authentication..."
202 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/{authorized_keys,core,sphere,tmp}
203 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
204 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
205 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
208 # setup server authentication
209 echo "### setting up server authentication..."
210 monkeysphere-authentication setup
211 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
213 # add admin as identity certifier for testhost
214 echo "### adding admin as certifier..."
215 echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
220 # generate an auth subkey for the test user that expires in 2 days
221 echo "### generating key for testuser..."
222 monkeysphere gen-subkey --expire 2
224 # add server key to testuser keychain
225 echo "### export server key to testuser..."
226 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
228 # teach the "server" about the testuser's key
229 echo "### export testuser key to server..."
230 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
231 echo "### update server authorized_keys file for this testuser..."
232 monkeysphere-authentication update-users $(whoami)
234 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
235 # the identity before connection. This should work in both directions!
236 echo "### ssh connection test for success..."
239 # remove the testuser's authorized_user_ids file, update, and make
240 # sure that the ssh authentication FAILS
241 echo "### removing testuser authorized_user_ids and updating..."
242 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
243 monkeysphere-authentication update-users $(whoami)
244 echo "### ssh connection test for server authentication denial..."
246 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
248 # put improper permissions on authorized_user_ids file, update, and
249 # make sure ssh authentication FAILS
250 echo "### setting group writability on authorized_user_ids and updating..."
251 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
252 monkeysphere-authentication update-users $(whoami)
253 echo "### ssh connection test for server authentication denial..."
255 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
256 echo "### setting other writability on authorized_user_ids and updating..."
257 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
258 monkeysphere-authentication update-users $(whoami)
259 echo "### ssh connection test for server authentication denial..."
261 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
267 echo "Monkeysphere basic tests completed successfully!"