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:
17 # gpg command for test admin user
19 GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
23 # first argument is expected return code from ssh connection
29 # start the ssh daemon on the socket
30 echo "##### starting ssh server..."
31 socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
34 # wait until the socket is created before continuing
35 while [ ! -S "$SOCKET" ] ; do
41 # make a client connection to the socket
42 echo "##### starting ssh client..."
44 "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
47 # kill the sshd process if it's still running
52 echo "##### return $RETURN"
53 if [ "$RETURN" = "$CODE" ] ; then
54 echo "##### ssh connection test returned as desired"
57 echo "##### ssh connection test failed. expected return code $CODE"
63 # FIXME: can we be more verbose here?
65 read -p "press enter to cleanup and remove tmp:"
71 if (gpg --quick-random --version >/dev/null 2>&1) ; then
73 elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
74 echo debug-quick-random
79 echo "### removing temp dir..."
86 trap failed_cleanup EXIT
90 ## set up some variables to ensure that we're operating strictly in
91 ## the tests, not system-wide:
96 TEMPDIR="$TESTDIR"/tmp
97 if [ -e "$TEMPDIR" ] ; then
98 echo "tempdir '$TEMPDIR' already exists."
103 # Use the local copy of executables first, instead of system ones.
104 # This should help us test without installing.
105 export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
107 export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
108 export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
109 export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
110 export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
111 export MONKEYSPHERE_CHECK_KEYSERVER=false
112 export MONKEYSPHERE_LOG_LEVEL=DEBUG
114 export SSHD_CONFIG="$TEMPDIR"/sshd_config
115 export SOCKET="$TEMPDIR"/ssh-socket
118 export DISPLAY=monkeys
120 ### CONFIGURE ENVIRONMENTS
122 # copy in admin and testuser home to tmp
123 echo "### copying admin and testuser homes..."
124 cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
125 cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
127 # set up environment for testuser
128 TESTHOME="$TEMPDIR"/testuser
129 export GNUPGHOME="$TESTHOME"/.gnupg
130 export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
131 export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
132 cat <<EOF >> "$TESTHOME"/.ssh/config
133 UserKnownHostsFile $TESTHOME/.ssh/known_hosts
134 IdentityFile $TESTHOME/.ssh/no-such-identity
135 ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
137 cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
138 KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
140 get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
143 echo "### configuring sshd..."
144 cp etc/ssh/sshd_config "$SSHD_CONFIG"
145 # write the sshd_config
146 cat <<EOF >> "$SSHD_CONFIG"
147 HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
148 AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
151 # set up monkeysphere-server
152 echo "### configuring monkeysphere..."
153 mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
154 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
155 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
156 mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
157 cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
158 cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
159 AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
161 cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
162 primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
163 keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
169 # create a new host key
170 echo "### generating server key..."
171 # add gpg.conf with quick-random
172 get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
173 echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
174 # remove the gpg.conf
175 rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
177 HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
179 # certify it with the "Admin's Key".
180 # (this would normally be done via keyservers)
181 echo "### certifying server key..."
182 monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
183 echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
185 # FIXME: how can we test publish-key without flooding junk into the
188 # add admin as identity certifier for testhost
189 echo "### adding admin as certifier..."
190 echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
195 # generate an auth subkey for the test user that expires in 2 days
196 echo "### generating key for testuser..."
197 monkeysphere gen-subkey --expire 2
199 # add server key to testuser keychain
200 echo "### export server key to testuser..."
201 gpgadmin --armor --export "$HOSTKEYID" | gpg --import
203 # teach the "server" about the testuser's key
204 echo "### export testuser key to server..."
205 gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
206 echo "### update server authorized_keys file for this testuser..."
207 monkeysphere-server update-users $(whoami)
209 # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
210 # the identity before connection. This should work in both directions!
211 echo "### ssh connection test for success..."
214 # remove the testuser's authorized_user_ids file, update, and make
215 # sure that the ssh authentication FAILS
216 echo "### removing testuser authorized_user_ids and updating..."
217 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
218 monkeysphere-server update-users $(whoami)
219 echo "### ssh connection test for server authentication denial..."
221 mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
223 # put improper permissions on authorized_user_ids file, update, and
224 # make sure ssh authentication FAILS
225 echo "### setting group writability on authorized_user_ids and updating..."
226 chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
227 monkeysphere-server update-users $(whoami)
228 echo "### ssh connection test for server authentication denial..."
230 chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
231 echo "### setting other writability on authorized_user_ids and updating..."
232 chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
233 monkeysphere-server update-users $(whoami)
234 echo "### ssh connection test for server authentication denial..."
236 chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
242 echo "Monkeysphere basic tests completed successfully!"