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