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