summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 99a881b35febba53ec3940606a4a186e9b8865dd (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. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authentication/{authorized_keys,core,sphere,tmp}
  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