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