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