summaryrefslogtreecommitdiff
path: root/tests/basic
blob: bf6a364942d0fcbfb6b664609a19567dc368f6d6 (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 >/dev/null || { 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=$(dirname "$0")
  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/share
  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 "$TESTDIR"/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. ### SERVER HOST SETUP
  135. # set up monkeysphere host
  136. echo "### configuring monkeysphere host..."
  137. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
  138. # create a new host key
  139. echo "### generating server host key..."
  140. # add gpg.conf with quick-random
  141. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
  142. echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
  143. # remove the gpg.conf
  144. rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
  145. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  146. # certify it with the "Admin's Key".
  147. # (this would normally be done via keyservers)
  148. echo "### certifying server host key..."
  149. GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
  150. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  151. # FIXME: how can we test publish-key without flooding junk into the
  152. # keyservers?
  153. ### SERVER AUTHENTICATION TESTS
  154. # set up monkeysphere authentication
  155. echo "### configuring monkeysphere authentication..."
  156. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/{authorized_keys,core,sphere,tmp}
  157. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  158. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  159. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
  160. EOF
  161. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  162. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/authentication/sphere/pubring.gpg
  163. keyring ${MONKEYSPHERE_SYSDATADIR}/authentication/core/pubring.gpg
  164. EOF
  165. # setup server authentication
  166. echo "### setting up server authentication..."
  167. monkeysphere-authentication setup
  168. # add admin as identity certifier for testhost
  169. echo "### adding admin as certifier..."
  170. echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  171. ### TESTUSER TESTS
  172. # generate an auth subkey for the test user that expires in 2 days
  173. echo "### generating key for testuser..."
  174. monkeysphere gen-subkey --expire 2
  175. # add server key to testuser keychain
  176. echo "### export server key to testuser..."
  177. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  178. # teach the "server" about the testuser's key
  179. echo "### export testuser key to server..."
  180. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  181. echo "### update server authorized_keys file for this testuser..."
  182. monkeysphere-authentication update-users $(whoami)
  183. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  184. # the identity before connection. This should work in both directions!
  185. echo "### ssh connection test for success..."
  186. ssh_test
  187. # remove the testuser's authorized_user_ids file, update, and make
  188. # sure that the ssh authentication FAILS
  189. echo "### removing testuser authorized_user_ids and updating..."
  190. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  191. monkeysphere-authentication update-users $(whoami)
  192. echo "### ssh connection test for server authentication denial..."
  193. ssh_test 255
  194. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  195. # put improper permissions on authorized_user_ids file, update, and
  196. # make sure ssh authentication FAILS
  197. echo "### setting group writability on authorized_user_ids and updating..."
  198. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  199. monkeysphere-authentication update-users $(whoami)
  200. echo "### ssh connection test for server authentication denial..."
  201. ssh_test 255
  202. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  203. echo "### setting other writability on authorized_user_ids and updating..."
  204. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  205. monkeysphere-authentication update-users $(whoami)
  206. echo "### ssh connection test for server authentication denial..."
  207. ssh_test 255
  208. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  209. trap - EXIT
  210. echo
  211. echo "Monkeysphere basic tests completed successfully!"
  212. echo
  213. cleanup