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 the TESTDIR is an absolute path, not a relative one.
21 export TESTDIR=$(cd $(dirname "$0") && pwd)
23 source "$TESTDIR"/common
25 ## make sure that the right tools are installed to run the test. the
26 ## test has *more* requirements than plain ol' monkeysphere:
27 which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
29 ## FIXME: other checks?
31 ######################################################################
34 # gpg command for test admin user
36 chmod 0700 "$TEMPDIR"/admin
37 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
41 # first argument is expected return code from ssh connection
47 # start the ssh daemon on the socket
48 echo "##### starting ssh server..."
49 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
52 # wait until the socket is created before continuing
53 while [ ! -S "$SOCKET" ] ; do
59 # make a client connection to the socket
60 echo "##### starting ssh client..."
62 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
65 # kill the sshd process if it's still running
71 echo "##### return $RETURN"
72 if [ "$RETURN" = "$CODE" ] ; then
73 echo "##### ssh connection test returned as desired"
76 echo "##### ssh connection test failed. expected return code $CODE"
84 trap failed_cleanup EXIT
87 ######################################################################
90 ## set up some variables to ensure that we're operating strictly in
91 ## the tests, not system-wide:
94 # FIXME: I believe that /tmp can not be used as the temp dir here,
95 # since the permissions on /tmp are usually such that they will not
96 # pass the monkeysphere/ssh path permission checking. at least this
98 #TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
99 TEMPDIR=$(mktemp -d "${TESTDIR}/tmp/monkeyspheretest.XXXXXXX")
101 # Use the local copy of executables first, instead of system ones.
102 # This should help us test without installing.
103 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
105 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
106 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
107 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
108 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
109 export MONKEYSPHERE_CHECK_KEYSERVER=false
110 export MONKEYSPHERE_LOG_LEVEL=DEBUG
111 export MONKEYSPHERE_CORE_KEYLENGTH=1024
113 export SSHD_CONFIG="$TEMPDIR"/sshd_config
114 export SOCKET="$TEMPDIR"/ssh-socket
116 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
117 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
118 # $DISPLAY (if one exists) because this test suite should not be doing
119 # *anything* with any running X11 session.
120 export DISPLAY=monkeys
123 ######################################################################
124 ### CONFIGURE ENVIRONMENTS
126 # copy in admin and testuser home to tmp
127 echo "##################################################"
128 echo "### configuring testuser home..."
129 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
130 # set up environment for testuser
131 export TESTHOME="$TEMPDIR"/testuser
132 export GNUPGHOME="$TESTHOME"/.gnupg
133 chmod 0700 "$GNUPGHOME"
134 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
135 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
136 cat <<EOF >> "$TESTHOME"/.ssh/config
137 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
138 IdentityFile $TESTHOME/.ssh/no-such-identity
139 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
141 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
142 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
144 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
146 echo "##################################################"
147 echo "### configuring admin home..."
148 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
151 echo "##################################################"
152 echo "### configuring sshd..."
153 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
154 # write the sshd_config
155 cat <<EOF >> "$SSHD_CONFIG"
156 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
157 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
161 ######################################################################
162 ### SERVER HOST SETUP
165 echo "##################################################"
166 echo "### testing host key importing..."
167 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
168 monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
170 # change host key expiration
171 echo "##################################################"
172 echo "### setting host key expiration..."
173 monkeysphere-host set-expire 1
174 monkeysphere-host show-key
175 # FIXME: how do we check that the expiration has really been set?
177 echo "##################################################"
178 echo "### getting host key fingerprint..."
179 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
182 # certify host key with the "Admin's Key".
183 # (this would normally be done via keyservers)
184 echo "##################################################"
185 echo "### certifying server host key..."
186 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
187 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
189 # FIXME: add revoker?
191 # FIXME: how can we test publish-key without flooding junk into the
194 # FIXME: should we run "diagnostics" here to test setup?
197 ######################################################################
198 ### SERVER AUTHENTICATION SETUP
200 # set up monkeysphere authentication
201 echo "##################################################"
202 echo "### setup monkeysphere authentication..."
203 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
204 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
205 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
207 monkeysphere-authentication setup
208 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
210 # add admin as identity certifier for testhost
211 echo "##################################################"
212 echo "### adding admin as certifier..."
213 monkeysphere-authentication add-id-certifier -y "$TEMPDIR"/admin/.gnupg/pubkey.gpg
215 # FIXME: should we run "diagnostics" here to test setup?
218 ######################################################################
221 # generate an auth subkey for the test user that expires in 2 days
222 echo "##################################################"
223 echo "### generating key for testuser..."
224 monkeysphere gen-subkey --expire 2
226 # add server key to testuser keychain
227 echo "##################################################"
228 echo "### export server key to testuser..."
229 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
231 # teach the "server" about the testuser's key
232 echo "##################################################"
233 echo "### export testuser key to server..."
234 gpg --export testuser | monkeysphere-authentication expert gpg-cmd --import
236 # update authorized_keys for user
237 echo "##################################################"
238 echo "### update server authorized_keys file for this testuser..."
239 monkeysphere-authentication update-users $(whoami)
240 # FIXME: NOT FAILING PROPERLY FOR:
241 # ms: improper group or other writability on path '/tmp'.
244 ######################################################################
247 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
248 # the identity before connection. This should work in both directions!
249 echo "##################################################"
250 echo "### ssh connection test for success..."
253 # remove the testuser's authorized_user_ids file, update, and make
254 # sure that the ssh authentication FAILS
255 echo "##################################################"
256 echo "### removing testuser authorized_user_ids and updating..."
257 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
258 monkeysphere-authentication update-users $(whoami)
259 echo "##################################################"
260 echo "### ssh connection test for server authentication denial..."
262 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
264 # put improper permissions on authorized_user_ids file, update, and
265 # make sure ssh authentication FAILS
266 echo "##################################################"
267 echo "### setting group writability on authorized_user_ids and updating..."
268 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
269 monkeysphere-authentication update-users $(whoami)
270 echo "##################################################"
271 echo "### ssh connection test for server authentication denial..."
273 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
274 echo "##################################################"
275 echo "### setting other writability on authorized_user_ids and updating..."
276 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
277 monkeysphere-authentication update-users $(whoami)
278 echo "##################################################"
279 echo "### ssh connection test for server authentication denial..."
281 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
283 # FIXME: addtest: remove admin as id-certifier and check ssh failure
285 # FIXME: addtest: add hostname on host key
286 # FIXME: addtest: revoke hostname on host key and check ssh failure
288 # FIXME: addtest: revoke the host key and check ssh failure
291 ######################################################################
295 echo "##################################################"
296 echo " Monkeysphere basic tests completed successfully!"
297 echo "##################################################"