summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 5e233aa0014ec9d9b801daa6c0beb837f27f500f (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 MONKEYSPHERE_CORE_KEYLENGTH=1024
  104. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  105. export SOCKET="$TEMPDIR"/ssh-socket
  106. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  107. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  108. # $DISPLAY (if one exists) because this test suite should not be doing
  109. # *anything* with any running X11 session.
  110. export DISPLAY=monkeys
  111. ######################################################################
  112. ### CONFIGURE ENVIRONMENTS
  113. # copy in admin and testuser home to tmp
  114. echo "##################################################"
  115. echo "### copying admin and testuser homes..."
  116. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  117. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  118. # set up environment for testuser
  119. TESTHOME="$TEMPDIR"/testuser
  120. export GNUPGHOME="$TESTHOME"/.gnupg
  121. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  122. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  123. cat <<EOF >> "$TESTHOME"/.ssh/config
  124. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  125. IdentityFile $TESTHOME/.ssh/no-such-identity
  126. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  127. EOF
  128. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  129. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  130. EOF
  131. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  132. # set up sshd
  133. echo "##################################################"
  134. echo "### configuring sshd..."
  135. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  136. # write the sshd_config
  137. cat <<EOF >> "$SSHD_CONFIG"
  138. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  139. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
  140. EOF
  141. ######################################################################
  142. ### SERVER HOST SETUP
  143. # set up monkeysphere host
  144. echo "##################################################"
  145. echo "### configuring monkeysphere host..."
  146. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
  147. # create a new host key
  148. echo "##################################################"
  149. echo "### generating server host key..."
  150. # add gpg.conf with quick-random
  151. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
  152. echo | monkeysphere-host expert gen-key --length 1024 --expire 0 testhost
  153. # remove the gpg.conf
  154. rm "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
  155. # FIXME: need to test import-key as well
  156. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  157. # certify it with the "Admin's Key".
  158. # (this would normally be done via keyservers)
  159. echo "##################################################"
  160. echo "### certifying server host key..."
  161. GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
  162. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  163. # FIXME: add revoker?
  164. # FIXME: how can we test publish-key without flooding junk into the
  165. # keyservers?
  166. # FIXME: should we run "diagnostics" here to test setup?
  167. ######################################################################
  168. ### SERVER AUTHENTICATION SETUP
  169. # set up monkeysphere authentication
  170. echo "##################################################"
  171. echo "### setup monkeysphere authentication..."
  172. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  173. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  174. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
  175. EOF
  176. monkeysphere-authentication setup
  177. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  178. # add admin as identity certifier for testhost
  179. echo "##################################################"
  180. echo "### adding admin as certifier..."
  181. echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  182. # FIXME: should we run "diagnostics" here to test setup?
  183. ######################################################################
  184. ### TESTUSER SETUP
  185. # generate an auth subkey for the test user that expires in 2 days
  186. echo "##################################################"
  187. echo "### generating key for testuser..."
  188. monkeysphere gen-subkey --expire 2
  189. # add server key to testuser keychain
  190. echo "##################################################"
  191. echo "### export server key to testuser..."
  192. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  193. # teach the "server" about the testuser's key
  194. echo "##################################################"
  195. echo "### export testuser key to server..."
  196. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  197. # update authorized_keys for user
  198. echo "##################################################"
  199. echo "### update server authorized_keys file for this testuser..."
  200. monkeysphere-authentication update-users $(whoami)
  201. ######################################################################
  202. ### TESTS
  203. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  204. # the identity before connection. This should work in both directions!
  205. echo "##################################################"
  206. echo "### ssh connection test for success..."
  207. ssh_test
  208. # remove the testuser's authorized_user_ids file, update, and make
  209. # sure that the ssh authentication FAILS
  210. echo "##################################################"
  211. echo "### removing testuser authorized_user_ids and updating..."
  212. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  213. monkeysphere-authentication update-users $(whoami)
  214. echo "##################################################"
  215. echo "### ssh connection test for server authentication denial..."
  216. ssh_test 255
  217. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  218. # put improper permissions on authorized_user_ids file, update, and
  219. # make sure ssh authentication FAILS
  220. echo "##################################################"
  221. echo "### setting group writability on authorized_user_ids and updating..."
  222. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  223. monkeysphere-authentication update-users $(whoami)
  224. echo "##################################################"
  225. echo "### ssh connection test for server authentication denial..."
  226. ssh_test 255
  227. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  228. echo "##################################################"
  229. echo "### setting other writability on authorized_user_ids and updating..."
  230. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  231. monkeysphere-authentication update-users $(whoami)
  232. echo "##################################################"
  233. echo "### ssh connection test for server authentication denial..."
  234. ssh_test 255
  235. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  236. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  237. # FIXME: addtest: revoke hostname on host key and check ssh failure
  238. # FIXME: addtest: revoke the host key and check ssh failure
  239. ######################################################################
  240. trap - EXIT
  241. echo "##################################################"
  242. echo " Monkeysphere basic tests completed successfully!"
  243. echo "##################################################"
  244. cleanup