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