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