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