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
12 # as a non-privileged user.
14 # all subcommands in this script should complete without failure:
18 ## make sure that the right tools are installed to run the test. the
19 ## test has *more* requirements than plain ol' monkeysphere:
21 which socat || { echo "You must have socat installed to run this test." ; exit 1; }
23 ## FIXME: other checks?
25 # gpg command for test admin user
27 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
31 # first argument is expected return code from ssh connection
37 # start the ssh daemon on the socket
38 echo "##### starting ssh server..."
39 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
42 # wait until the socket is created before continuing
43 while [ ! -S "$SOCKET" ] ; do
49 # make a client connection to the socket
50 echo "##### starting ssh client..."
52 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
55 # 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..."
94 trap failed_cleanup EXIT
98 ## set up some variables to ensure that we're operating strictly in
99 ## the tests, not system-wide:
101 export TESTDIR=$(pwd)
104 TEMPDIR="$TESTDIR"/tmp
105 if [ -e "$TEMPDIR" ] ; then
106 echo "tempdir '$TEMPDIR' already exists."
111 # Use the local copy of executables first, instead of system ones.
112 # This should help us test without installing.
113 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
115 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
116 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
117 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
118 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
119 export MONKEYSPHERE_CHECK_KEYSERVER=false
120 export MONKEYSPHERE_LOG_LEVEL=DEBUG
122 export SSHD_CONFIG="$TEMPDIR"/sshd_config
123 export SOCKET="$TEMPDIR"/ssh-socket
126 # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
127 # back on $SSH_ASKPASS. Make sure it's not set to the current actual
128 # $DISPLAY (if one exists) because this test suite should not be doing
129 # *anything* with any running X11 session.
130 export DISPLAY=monkeys
132 ### CONFIGURE ENVIRONMENTS
134 # copy in admin and testuser home to tmp
135 echo "### copying admin and testuser homes..."
136 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
137 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
139 # set up environment for testuser
140 TESTHOME="$TEMPDIR"/testuser
141 export GNUPGHOME="$TESTHOME"/.gnupg
142 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
143 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
144 cat <<EOF >> "$TESTHOME"/.ssh/config
145 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
146 IdentityFile $TESTHOME/.ssh/no-such-identity
147 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
149 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
150 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
152 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
155 echo "### configuring sshd..."
156 cp etc/ssh/sshd_config "$SSHD_CONFIG"
157 # write the sshd_config
158 cat <<EOF >> "$SSHD_CONFIG"
159 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
160 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
163 # set up monkeysphere-server
164 echo "### configuring monkeysphere..."
165 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
166 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
167 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
168 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
169 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
170 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
171 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
173 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
174 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
175 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
181 # create a new host key
182 echo "### generating server key..."
183 # add gpg.conf with quick-random
184 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
185 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
186 # remove the gpg.conf
187 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
189 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
191 # certify it with the "Admin's Key".
192 # (this would normally be done via keyservers)
193 echo "### certifying server key..."
194 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
195 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
197 # FIXME: how can we test publish-key without flooding junk into the
200 # add admin as identity certifier for testhost
201 echo "### adding admin as certifier..."
202 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
207 # generate an auth subkey for the test user that expires in 2 days
208 echo "### generating key for testuser..."
209 monkeysphere gen-subkey --expire 2
211 # add server key to testuser keychain
212 echo "### export server key to testuser..."
213 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
215 # teach the "server" about the testuser's key
216 echo "### export testuser key to server..."
217 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
218 echo "### update server authorized_keys file for this testuser..."
219 monkeysphere-server update-users $(whoami)
221 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
222 # the identity before connection. This should work in both directions!
223 echo "### ssh connection test for success..."
226 # remove the testuser's authorized_user_ids file, update, and make
227 # sure that the ssh authentication FAILS
228 echo "### removing testuser authorized_user_ids and updating..."
229 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
230 monkeysphere-server update-users $(whoami)
231 echo "### ssh connection test for server authentication denial..."
233 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
235 # put improper permissions on authorized_user_ids file, update, and
236 # make sure ssh authentication FAILS
237 echo "### setting group writability on authorized_user_ids and updating..."
238 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
239 monkeysphere-server update-users $(whoami)
240 echo "### ssh connection test for server authentication denial..."
242 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
243 echo "### setting other writability on authorized_user_ids and updating..."
244 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
245 monkeysphere-server update-users $(whoami)
246 echo "### ssh connection test for server authentication denial..."
248 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
254 echo "Monkeysphere basic tests completed successfully!"