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