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