summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 18e301936d52b0fc8c464742c8030420a00b6b00 (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. # set up temp dir
  69. # NOTE: /tmp can not be used as the temp dir here, since the
  70. # permissions on /tmp are usually such that they will not pass the
  71. # monkeysphere/ssh path permission checking. If you need to use a
  72. # different location than the current source, please set $TMPDIR
  73. # somewhere with tighter permissions.
  74. mkdir -p "$TESTDIR"/tmp
  75. TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
  76. # Use the local copy of executables first, instead of system ones.
  77. # This should help us test without installing.
  78. export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
  79. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  80. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  81. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
  82. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  83. export MONKEYSPHERE_CHECK_KEYSERVER=false
  84. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  85. export MONKEYSPHERE_CORE_KEYLENGTH=1024
  86. export MONKEYSPHERE_PROMPT=false
  87. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  88. export SOCKET="$TEMPDIR"/ssh-socket
  89. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  90. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  91. # $DISPLAY (if one exists) because this test suite should not be doing
  92. # *anything* with any running X11 session.
  93. export DISPLAY=monkeys
  94. ######################################################################
  95. ### CONFIGURE ENVIRONMENTS
  96. # copy in admin and testuser home to tmp
  97. echo "##################################################"
  98. echo "### configuring testuser home..."
  99. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  100. # set up environment for testuser
  101. export TESTHOME="$TEMPDIR"/testuser
  102. export GNUPGHOME="$TESTHOME"/.gnupg
  103. chmod 0700 "$GNUPGHOME"
  104. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  105. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  106. cat <<EOF >> "$TESTHOME"/.ssh/config
  107. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  108. IdentityFile $TESTHOME/.ssh/no-such-identity
  109. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  110. EOF
  111. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  112. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  113. EOF
  114. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  115. echo "##################################################"
  116. echo "### configuring admin home..."
  117. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  118. # set up sshd
  119. echo "##################################################"
  120. echo "### configuring sshd..."
  121. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  122. # write the sshd_config
  123. cat <<EOF >> "$SSHD_CONFIG"
  124. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  125. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
  126. EOF
  127. ######################################################################
  128. ### SERVER HOST SETUP
  129. # import host key
  130. echo "##################################################"
  131. echo "### import host key..."
  132. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  133. monkeysphere-host import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
  134. echo "##################################################"
  135. echo "### getting host key fingerprint..."
  136. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  137. echo "$HOSTKEYID"
  138. # change host key expiration
  139. echo "##################################################"
  140. echo "### setting host key expiration..."
  141. monkeysphere-host set-expire 1
  142. # FIXME: how do we check that the expiration has really been set?
  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/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. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  168. echo "##################################################"
  169. echo "### list certifiers..."
  170. monkeysphere-authentication list-certifiers
  171. # FIXME: should we run "diagnostics" here to test setup?
  172. ######################################################################
  173. ### TESTUSER SETUP
  174. # generate an auth subkey for the test user that expires in 2 days
  175. echo "##################################################"
  176. echo "### generating key for testuser..."
  177. monkeysphere gen-subkey --expire 2
  178. # add server key to testuser keychain
  179. echo "##################################################"
  180. echo "### export server key to testuser..."
  181. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  182. # teach the "server" about the testuser's key
  183. echo "##################################################"
  184. echo "### export testuser key to server..."
  185. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  186. # update authorized_keys for user
  187. echo "##################################################"
  188. echo "### update server authorized_keys file for this testuser..."
  189. monkeysphere-authentication update-users $(whoami)
  190. # FIXME: this is maybe not failing properly for:
  191. # ms: improper group or other writability on path '/tmp'.
  192. ######################################################################
  193. ### TESTS
  194. # connect to test sshd, using monkeysphere ssh-proxycommand to verify
  195. # the identity before connection. This should work in both directions!
  196. echo "##################################################"
  197. echo "### ssh connection test for success..."
  198. ssh_test
  199. # remove the testuser's authorized_user_ids file, update, and make
  200. # sure that the ssh authentication FAILS
  201. echo "##################################################"
  202. echo "### removing testuser authorized_user_ids and updating..."
  203. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  204. monkeysphere-authentication update-users $(whoami)
  205. echo "##################################################"
  206. echo "### ssh connection test for server authentication denial..."
  207. ssh_test 255
  208. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  209. # put improper permissions on authorized_user_ids file, update, and
  210. # make sure ssh authentication FAILS
  211. echo "##################################################"
  212. echo "### setting group writability on authorized_user_ids and updating..."
  213. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  214. monkeysphere-authentication update-users $(whoami)
  215. echo "##################################################"
  216. echo "### ssh connection test for server authentication denial..."
  217. ssh_test 255
  218. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  219. echo "##################################################"
  220. echo "### setting other writability on authorized_user_ids and updating..."
  221. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  222. monkeysphere-authentication update-users $(whoami)
  223. echo "##################################################"
  224. echo "### ssh connection test for server authentication denial..."
  225. ssh_test 255
  226. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  227. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  228. # FIXME: addtest: add hostname on host key
  229. # FIXME: addtest: revoke hostname on host key and check ssh failure
  230. # FIXME: addtest: revoke the host key and check ssh failure
  231. ######################################################################
  232. trap - EXIT
  233. echo "##################################################"
  234. echo " Monkeysphere basic tests completed successfully!"
  235. echo "##################################################"
  236. cleanup