summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 6d0a8888b989aaf1c92321208a92857c391ca3f5 (plain)
  1. #!/usr/bin/env bash
  2. # Tests to ensure that the monkeysphere is working
  3. # Authors:
  4. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. # Micah Anderson <micah@riseup.net>
  7. #
  8. # Copyright: 2008-2009
  9. # License: GPL v3 or later
  10. # these tests should all be able to run as a non-privileged user.
  11. # all subcommands in this script should complete without failure:
  12. set -e
  13. # piped commands should return the code of the first non-zero return
  14. set -o pipefail
  15. ## make sure that the right tools are installed to run the test. the
  16. ## test has *more* requirements than plain ol' monkeysphere:
  17. which socat || { echo "You must have socat installed to run this test." ; exit 1; }
  18. ## FIXME: other checks?
  19. # gpg command for test admin user
  20. gpgadmin() {
  21. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
  22. }
  23. # test ssh connection
  24. # first argument is expected return code from ssh connection
  25. ssh_test() {
  26. umask 0077
  27. CODE=${1:-0}
  28. # start the ssh daemon on the socket
  29. echo "##### starting ssh server..."
  30. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  31. SSHD_PID="$!"
  32. # wait until the socket is created before continuing
  33. while [ ! -S "$SOCKET" ] ; do
  34. sleep 1
  35. done
  36. set +e
  37. # make a client connection to the socket
  38. echo "##### starting ssh client..."
  39. ssh-agent bash -c \
  40. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
  41. RETURN="$?"
  42. # kill the sshd process if it's still running
  43. kill "$SSHD_PID"
  44. SSHD_PID=
  45. set -e
  46. echo "##### return $RETURN"
  47. if [ "$RETURN" = "$CODE" ] ; then
  48. echo "##### ssh connection test returned as desired"
  49. return 0
  50. else
  51. echo "##### ssh connection test failed. expected return code $CODE"
  52. return 1
  53. fi
  54. }
  55. failed_cleanup() {
  56. # FIXME: can we be more verbose here?
  57. echo 'FAILED!'
  58. read -p "press enter to cleanup and remove tmp:"
  59. cleanup
  60. }
  61. get_gpg_prng_arg() {
  62. if (gpg --quick-random --version >/dev/null 2>&1) ; then
  63. echo quick-random
  64. elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
  65. echo debug-quick-random
  66. fi
  67. }
  68. cleanup() {
  69. echo "### removing temp dir..."
  70. rm -rf "$TEMPDIR"
  71. if [ "$SSHD_PID" ] ; then
  72. echo "### killing off lingering sshd..."
  73. kill "$SSHD_PID"
  74. fi
  75. wait
  76. }
  77. SSHD_PID=
  78. ## setup trap
  79. trap failed_cleanup EXIT
  80. ### SETUP VARIABLES
  81. ## set up some variables to ensure that we're operating strictly in
  82. ## the tests, not system-wide:
  83. export TESTDIR=$(pwd)
  84. # make temp dir
  85. TEMPDIR="$TESTDIR"/tmp
  86. if [ -e "$TEMPDIR" ] ; then
  87. echo "tempdir '$TEMPDIR' already exists."
  88. exit 1
  89. fi
  90. mkdir "$TEMPDIR"
  91. # Use the local copy of executables first, instead of system ones.
  92. # This should help us test without installing.
  93. export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
  94. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  95. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  96. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
  97. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  98. export MONKEYSPHERE_CHECK_KEYSERVER=false
  99. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  100. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  101. export SOCKET="$TEMPDIR"/ssh-socket
  102. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  103. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  104. # $DISPLAY (if one exists) because this test suite should not be doing
  105. # *anything* with any running X11 session.
  106. export DISPLAY=monkeys
  107. ### CONFIGURE ENVIRONMENTS
  108. # copy in admin and testuser home to tmp
  109. echo "### copying admin and testuser homes..."
  110. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  111. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  112. # set up environment for testuser
  113. TESTHOME="$TEMPDIR"/testuser
  114. export GNUPGHOME="$TESTHOME"/.gnupg
  115. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  116. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  117. cat <<EOF >> "$TESTHOME"/.ssh/config
  118. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  119. IdentityFile $TESTHOME/.ssh/no-such-identity
  120. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  121. EOF
  122. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  123. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  124. EOF
  125. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  126. # set up sshd
  127. echo "### configuring sshd..."
  128. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  129. # write the sshd_config
  130. cat <<EOF >> "$SSHD_CONFIG"
  131. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  132. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
  133. EOF
  134. # set up monkeysphere-server
  135. echo "### configuring monkeysphere..."
  136. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
  137. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication
  138. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/authorized_keys
  139. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
  140. cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
  141. cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
  142. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
  143. EOF
  144. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  145. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/authentication/sphere/pubring.gpg
  146. keyring ${MONKEYSPHERE_SYSDATADIR}/host/pubring.gpg
  147. EOF
  148. ### SERVER TESTS
  149. # create a new host key
  150. echo "### generating server key..."
  151. # add gpg.conf with quick-random
  152. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  153. echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
  154. # remove the gpg.conf
  155. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  156. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  157. # certify it with the "Admin's Key".
  158. # (this would normally be done via keyservers)
  159. echo "### certifying server key..."
  160. monkeysphere-authentication expert gpg-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  161. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  162. # FIXME: how can we test publish-key without flooding junk into the
  163. # keyservers?
  164. # add admin as identity certifier for testhost
  165. echo "### adding admin as certifier..."
  166. echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  167. ### TESTUSER TESTS
  168. # generate an auth subkey for the test user that expires in 2 days
  169. echo "### generating key for testuser..."
  170. monkeysphere gen-subkey --expire 2
  171. # add server key to testuser keychain
  172. echo "### export server key to testuser..."
  173. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  174. # teach the "server" about the testuser's key
  175. echo "### export testuser key to server..."
  176. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  177. echo "### update server authorized_keys file for this testuser..."
  178. monkeysphere-authentication update-users $(whoami)
  179. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  180. # the identity before connection. This should work in both directions!
  181. echo "### ssh connection test for success..."
  182. ssh_test
  183. # remove the testuser's authorized_user_ids file, update, and make
  184. # sure that the ssh authentication FAILS
  185. echo "### removing testuser authorized_user_ids and updating..."
  186. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  187. monkeysphere-authentication update-users $(whoami)
  188. echo "### ssh connection test for server authentication denial..."
  189. ssh_test 255
  190. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  191. # put improper permissions on authorized_user_ids file, update, and
  192. # make sure ssh authentication FAILS
  193. echo "### setting group writability on authorized_user_ids and updating..."
  194. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  195. monkeysphere-authentication update-users $(whoami)
  196. echo "### ssh connection test for server authentication denial..."
  197. ssh_test 255
  198. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  199. echo "### setting other writability on authorized_user_ids and updating..."
  200. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  201. monkeysphere-authentication update-users $(whoami)
  202. echo "### ssh connection test for server authentication denial..."
  203. ssh_test 255
  204. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  205. trap - EXIT
  206. echo
  207. echo "Monkeysphere basic tests completed successfully!"
  208. echo
  209. cleanup