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; }
30 ## FIXME: other checks?
32 ######################################################################
35 # gpg command for test admin user
37 chmod 0700 "$TEMPDIR"/admin
38 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
42 # first argument is expected return code from ssh connection
48 # start the ssh daemon on the socket
49 echo "##### starting ssh server..."
50 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
53 # wait until the socket is created before continuing
54 while [ ! -S "$SOCKET" ] ; do
60 # make a client connection to the socket
61 echo "##### starting ssh client..."
63 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
66 # kill the sshd process if it's still running
72 echo "##### return $RETURN"
73 if [ "$RETURN" = "$CODE" ] ; then
74 echo "##### ssh connection test returned as desired"
77 echo "##### ssh connection test failed. expected return code $CODE"
85 trap failed_cleanup EXIT
88 ######################################################################
91 ## set up some variables to ensure that we're operating strictly in
92 ## the tests, not system-wide:
96 # NOTE: /tmp can not be used as the temp dir here, since the
97 # permissions on /tmp are usually such that they will not pass the
98 # monkeysphere/ssh path permission checking. If you need to use a
99 # different location than the current source, please set $TMPDIR
100 # somewhere with tighter permissions.
102 mkdir -p "$TESTDIR"/tmp
103 TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
105 # Use the local copy of executables first, instead of system ones.
106 # This should help us test without installing.
107 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
109 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
110 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
111 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
112 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
113 export MONKEYSPHERE_CHECK_KEYSERVER=false
114 export MONKEYSPHERE_LOG_LEVEL=DEBUG
115 export MONKEYSPHERE_CORE_KEYLENGTH=1024
116 export MONKEYSPHERE_PROMPT=false
118 export SSHD_CONFIG="$TEMPDIR"/sshd_config
119 export SOCKET="$TEMPDIR"/ssh-socket
121 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
122 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
123 # $DISPLAY (if one exists) because this test suite should not be doing
124 # *anything* with any running X11 session.
125 export DISPLAY=monkeys
127 ## make sure that the version number matches the debian changelog
128 ## (don't bother if this is being run from the tests).
130 if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
131 echo "##################################################"
132 echo "### checking version string match..."
133 repver=$(monkeysphere version)
134 debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
135 if [ "$repver" = "$debver" ] ; then
136 echo "Versions match!"
138 printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
143 ######################################################################
144 ### CONFIGURE ENVIRONMENTS
146 # copy in admin and testuser home to tmp
147 echo "##################################################"
148 echo "### configuring testuser home..."
149 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
150 # set up environment for testuser
151 export TESTHOME="$TEMPDIR"/testuser
152 export GNUPGHOME="$TESTHOME"/.gnupg
153 chmod 0700 "$GNUPGHOME"
154 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
155 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
156 cat <<EOF >> "$TESTHOME"/.ssh/config
157 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
158 IdentityFile $TESTHOME/.ssh/no-such-identity
159 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
161 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
162 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
164 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
166 echo "##################################################"
167 echo "### configuring admin home..."
168 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
171 echo "##################################################"
172 echo "### configuring sshd..."
173 cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
174 # write the sshd_config
175 cat <<EOF >> "$SSHD_CONFIG"
176 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
177 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
181 ######################################################################
182 ### SERVER HOST SETUP
185 echo "##################################################"
186 echo "### import host key..."
187 ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
188 monkeysphere-host import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
190 echo "##################################################"
191 echo "### getting host key fingerprint..."
192 HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
195 # change host key expiration
196 echo "##################################################"
197 echo "### setting host key expiration..."
198 monkeysphere-host set-expire 1
199 # FIXME: how do we check that the expiration has really been set?
201 # certify host key with the "Admin's Key".
202 # (this would normally be done via keyservers)
203 echo "##################################################"
204 echo "### certifying server host key..."
205 GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
206 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
208 # FIXME: add revoker?
210 # FIXME: how can we test publish-key without flooding junk into the
213 # FIXME: should we run "diagnostics" here to test setup?
216 ######################################################################
217 ### SERVER AUTHENTICATION SETUP
219 # set up monkeysphere authentication
220 echo "##################################################"
221 echo "### setup monkeysphere authentication..."
222 cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
223 cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
224 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
226 monkeysphere-authentication setup
227 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
229 # add admin as identity certifier for testhost
230 echo "##################################################"
231 echo "### adding admin as certifier..."
232 monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
234 echo "##################################################"
235 echo "### list certifiers..."
236 monkeysphere-authentication list-certifiers
238 # FIXME: should we run "diagnostics" here to test setup?
240 ######################################################################
243 # generate an auth subkey for the test user that expires in 2 days
244 echo "##################################################"
245 echo "### generating key for testuser..."
246 monkeysphere gen-subkey --expire 2
248 # add server key to testuser keychain
249 echo "##################################################"
250 echo "### export server key to testuser..."
251 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
253 # teach the "server" about the testuser's key
254 echo "##################################################"
255 echo "### export testuser key to server..."
256 gpg --export testuser | monkeysphere-authentication gpg-cmd --import
258 # update authorized_keys for user
259 echo "##################################################"
260 echo "### update server authorized_keys file for this testuser..."
261 monkeysphere-authentication update-users $(whoami)
262 # FIXME: this is maybe not failing properly for:
263 # ms: improper group or other writability on path '/tmp'.
266 ######################################################################
269 # connect to test sshd, using monkeysphere ssh-proxycommand to verify
270 # the identity before connection. This should work in both directions!
271 echo "##################################################"
272 echo "### ssh connection test for success..."
275 # remove the testuser's authorized_user_ids file, update, and make
276 # sure that the ssh authentication FAILS
277 echo "##################################################"
278 echo "### removing testuser authorized_user_ids and updating..."
279 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
280 monkeysphere-authentication update-users $(whoami)
281 echo "##################################################"
282 echo "### ssh connection test for server authentication denial..."
284 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
286 # put improper permissions on authorized_user_ids file, update, and
287 # make sure ssh authentication FAILS
288 echo "##################################################"
289 echo "### setting group writability on authorized_user_ids and updating..."
290 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
291 monkeysphere-authentication update-users $(whoami)
292 echo "##################################################"
293 echo "### ssh connection test for server authentication denial..."
295 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
296 echo "##################################################"
297 echo "### setting other writability on authorized_user_ids and updating..."
298 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
299 monkeysphere-authentication update-users $(whoami)
300 echo "##################################################"
301 echo "### ssh connection test for server authentication denial..."
303 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
305 # FIXME: addtest: remove admin as id-certifier and check ssh failure
307 # FIXME: addtest: add hostname on host key
308 # FIXME: addtest: revoke hostname on host key and check ssh failure
310 # FIXME: addtest: revoke the host key and check ssh failure
313 ######################################################################
317 echo "##################################################"
318 echo " Monkeysphere basic tests completed successfully!"
319 echo "##################################################"