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