3 # Tests to ensure that the monkeysphere is working
6 # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
7 # Jameson Rollins <jrollins@fifthhorseman.net>
9 # License: GPL v3 or later
11 # these tests should all be able to run as a non-privileged user.
13 # all subcommands in this script should complete without failure:
15 # piped commands should return the code of the first non-zero return
18 ## make sure that the right tools are installed to run the test. the
19 ## test has *more* requirements than plain ol' monkeysphere:
20 which socat || { echo "You must have socat installed to run this test." ; exit 1; }
22 ## FIXME: other checks?
24 # gpg command for test admin user
26 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
30 # first argument is expected return code from ssh connection
36 # start the ssh daemon on the socket
37 echo "##### starting ssh server..."
38 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
41 # wait until the socket is created before continuing
42 while [ ! -S "$SOCKET" ] ; do
48 # make a client connection to the socket
49 echo "##### starting ssh client..."
51 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
54 # kill the sshd process if it's still running
60 echo "##### return $RETURN"
61 if [ "$RETURN" = "$CODE" ] ; then
62 echo "##### ssh connection test returned as desired"
65 echo "##### ssh connection test failed. expected return code $CODE"
71 # FIXME: can we be more verbose here?
73 read -p "press enter to cleanup and remove tmp:"
79 if (gpg --quick-random --version >/dev/null 2>&1) ; then
81 elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
82 echo debug-quick-random
87 echo "### removing temp dir..."
90 if [ "$SSHD_PID" ] ; then
91 echo "### killing off lingering sshd..."
101 trap failed_cleanup EXIT
105 ## set up some variables to ensure that we're operating strictly in
106 ## the tests, not system-wide:
108 export TESTDIR=$(pwd)
111 TEMPDIR="$TESTDIR"/tmp
112 if [ -e "$TEMPDIR" ] ; then
113 echo "tempdir '$TEMPDIR' already exists."
118 # Use the local copy of executables first, instead of system ones.
119 # This should help us test without installing.
120 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
122 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
123 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
124 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
125 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
126 export MONKEYSPHERE_CHECK_KEYSERVER=false
127 export MONKEYSPHERE_LOG_LEVEL=DEBUG
129 export SSHD_CONFIG="$TEMPDIR"/sshd_config
130 export SOCKET="$TEMPDIR"/ssh-socket
132 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
133 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
134 # $DISPLAY (if one exists) because this test suite should not be doing
135 # *anything* with any running X11 session.
136 export DISPLAY=monkeys
138 ### CONFIGURE ENVIRONMENTS
140 # copy in admin and testuser home to tmp
141 echo "### copying admin and testuser homes..."
142 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
143 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
145 # set up environment for testuser
146 TESTHOME="$TEMPDIR"/testuser
147 export GNUPGHOME="$TESTHOME"/.gnupg
148 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
149 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
150 cat <<EOF >> "$TESTHOME"/.ssh/config
151 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
152 IdentityFile $TESTHOME/.ssh/no-such-identity
153 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
155 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
156 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
158 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
161 echo "### configuring sshd..."
162 cp etc/ssh/sshd_config "$SSHD_CONFIG"
163 # write the sshd_config
164 cat <<EOF >> "$SSHD_CONFIG"
165 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
166 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
169 # set up monkeysphere-server
170 echo "### configuring monkeysphere..."
171 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
172 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
173 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
174 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
175 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
176 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
177 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
179 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
180 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
181 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
187 # create a new host key
188 echo "### generating server key..."
189 # add gpg.conf with quick-random
190 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
191 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
192 # remove the gpg.conf
193 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
195 HOSTKEYID=$( monkeysphere-server show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
197 # certify it with the "Admin's Key".
198 # (this would normally be done via keyservers)
199 echo "### certifying server key..."
200 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
201 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
203 # FIXME: how can we test publish-key without flooding junk into the
206 # add admin as identity certifier for testhost
207 echo "### adding admin as certifier..."
208 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
213 # generate an auth subkey for the test user that expires in 2 days
214 echo "### generating key for testuser..."
215 monkeysphere gen-subkey --expire 2
217 # add server key to testuser keychain
218 echo "### export server key to testuser..."
219 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
221 # teach the "server" about the testuser's key
222 echo "### export testuser key to server..."
223 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
224 echo "### update server authorized_keys file for this testuser..."
225 monkeysphere-server update-users $(whoami)
227 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
228 # the identity before connection. This should work in both directions!
229 echo "### ssh connection test for success..."
232 # remove the testuser's authorized_user_ids file, update, and make
233 # sure that the ssh authentication FAILS
234 echo "### removing testuser authorized_user_ids and updating..."
235 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
236 monkeysphere-server update-users $(whoami)
237 echo "### ssh connection test for server authentication denial..."
239 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
241 # put improper permissions on authorized_user_ids file, update, and
242 # make sure ssh authentication FAILS
243 echo "### setting group writability on authorized_user_ids and updating..."
244 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
245 monkeysphere-server update-users $(whoami)
246 echo "### ssh connection test for server authentication denial..."
248 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
249 echo "### setting other writability on authorized_user_ids and updating..."
250 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
251 monkeysphere-server update-users $(whoami)
252 echo "### ssh connection test for server authentication denial..."
254 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
260 echo "Monkeysphere basic tests completed successfully!"