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