summaryrefslogtreecommitdiff
path: root/tests/basic
blob: c85b449506165630049912c5159b711871cd518e (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. export DISPLAY=monkeys
  91. ### CONFIGURE ENVIRONMENTS
  92. # copy in admin and testuser home to tmp
  93. echo "### copying admin and testuser homes..."
  94. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  95. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  96. # set up environment for testuser
  97. TESTHOME="$TEMPDIR"/testuser
  98. export GNUPGHOME="$TESTHOME"/.gnupg
  99. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  100. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  101. cat <<EOF >> "$TESTHOME"/.ssh/config
  102. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  103. IdentityFile $TESTHOME/.ssh/no-such-identity
  104. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  105. EOF
  106. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  107. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  108. EOF
  109. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  110. # set up sshd
  111. echo "### configuring sshd..."
  112. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  113. # write the sshd_config
  114. cat <<EOF >> "$SSHD_CONFIG"
  115. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  116. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  117. EOF
  118. # set up monkeysphere-server
  119. echo "### configuring monkeysphere..."
  120. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
  121. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
  122. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
  123. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
  124. cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
  125. cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
  126. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  127. EOF
  128. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  129. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
  130. keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
  131. EOF
  132. ### SERVER TESTS
  133. # create a new host key
  134. echo "### generating server key..."
  135. # add gpg.conf with quick-random
  136. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  137. echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
  138. # remove the gpg.conf
  139. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  140. HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
  141. # certify it with the "Admin's Key".
  142. # (this would normally be done via keyservers)
  143. echo "### certifying server key..."
  144. monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  145. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  146. # FIXME: how can we test publish-key without flooding junk into the
  147. # keyservers?
  148. # add admin as identity certifier for testhost
  149. echo "### adding admin as certifier..."
  150. echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  151. ### TESTUSER TESTS
  152. # generate an auth subkey for the test user that expires in 2 days
  153. echo "### generating key for testuser..."
  154. monkeysphere gen-subkey --expire 2
  155. # add server key to testuser keychain
  156. echo "### export server key to testuser..."
  157. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  158. # teach the "server" about the testuser's key
  159. echo "### export testuser key to server..."
  160. gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
  161. echo "### update server authorized_keys file for this testuser..."
  162. monkeysphere-server update-users $(whoami)
  163. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  164. # the identity before connection. This should work in both directions!
  165. echo "### ssh connection test for success..."
  166. ssh_test
  167. # remove the testuser's authorized_user_ids file, update, and make
  168. # sure that the ssh authentication FAILS
  169. echo "### removing testuser authorized_user_ids and updating..."
  170. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  171. monkeysphere-server update-users $(whoami)
  172. echo "### ssh connection test for server authentication denial..."
  173. ssh_test 255
  174. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  175. # put improper permissions on authorized_user_ids file, update, and
  176. # make sure ssh authentication FAILS
  177. echo "### setting group writability on authorized_user_ids and updating..."
  178. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  179. monkeysphere-server update-users $(whoami)
  180. echo "### ssh connection test for server authentication denial..."
  181. ssh_test 255
  182. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  183. echo "### setting other writability on authorized_user_ids and updating..."
  184. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  185. monkeysphere-server update-users $(whoami)
  186. echo "### ssh connection test for server authentication denial..."
  187. ssh_test 255
  188. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  189. trap - EXIT
  190. echo
  191. echo "Monkeysphere basic tests completed successfully!"
  192. echo
  193. cleanup