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