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