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