summaryrefslogtreecommitdiff
path: root/tests/basic
blob: fd4f6736059f684543ae61241d63accf4c7a6735 (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. # example.org does not respond to the HKP port, so this should cause
  85. # any keyserver connection attempts that do happen (they shouldn't!)
  86. # to hang, so we'll notice them:
  87. export MONKEYSPHERE_KEYSERVER=example.org
  88. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  89. export MONKEYSPHERE_CORE_KEYLENGTH=1024
  90. export MONKEYSPHERE_PROMPT=false
  91. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  92. export SOCKET="$TEMPDIR"/ssh-socket
  93. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  94. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  95. # $DISPLAY (if one exists) because this test suite should not be doing
  96. # *anything* with any running X11 session.
  97. export DISPLAY=monkeys
  98. ## make sure that the version number matches the debian changelog
  99. ## (don't bother if this is being run from the tests).
  100. if [ -f "$TESTDIR"/../packaging/debian/changelog ]; then
  101. echo "##################################################"
  102. echo "### checking version string match..."
  103. repver=$(monkeysphere version)
  104. debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
  105. if [ "$repver" = "$debver" ] ; then
  106. echo "Versions match!"
  107. else
  108. printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
  109. exit 1
  110. fi
  111. fi
  112. ######################################################################
  113. ### CONFIGURE ENVIRONMENTS
  114. # copy in admin and testuser home to tmp
  115. echo "##################################################"
  116. echo "### configuring testuser home..."
  117. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  118. # set up environment for testuser
  119. export TESTHOME="$TEMPDIR"/testuser
  120. export GNUPGHOME="$TESTHOME"/.gnupg
  121. chmod 0700 "$GNUPGHOME"
  122. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  123. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  124. cat <<EOF >> "$TESTHOME"/.ssh/config
  125. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  126. IdentityFile $TESTHOME/.ssh/no-such-identity
  127. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  128. EOF
  129. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  130. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  131. EOF
  132. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  133. echo "##################################################"
  134. echo "### configuring admin home..."
  135. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  136. # set up sshd
  137. echo "##################################################"
  138. echo "### configuring sshd..."
  139. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  140. # write the sshd_config
  141. cat <<EOF >> "$SSHD_CONFIG"
  142. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  143. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  144. EOF
  145. ######################################################################
  146. ### SERVER HOST SETUP
  147. # import host key
  148. echo "##################################################"
  149. echo "### import host key..."
  150. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  151. monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
  152. echo "##################################################"
  153. echo "### getting host key fingerprint..."
  154. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  155. echo "$HOSTKEYID"
  156. # change host key expiration
  157. echo "##################################################"
  158. echo "### setting host key expiration..."
  159. monkeysphere-host set-expire 1
  160. # FIXME: how do we check that the expiration has really been set?
  161. # certify host key with the "Admin's Key".
  162. # (this would normally be done via keyservers)
  163. echo "##################################################"
  164. echo "### certifying server host key..."
  165. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
  166. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  167. # FIXME: add revoker?
  168. # FIXME: how can we test publish-key without flooding junk into the
  169. # keyservers?
  170. # FIXME: should we run "diagnostics" here to test setup?
  171. ######################################################################
  172. ### SERVER AUTHENTICATION SETUP
  173. # set up monkeysphere authentication
  174. echo "##################################################"
  175. echo "### setup monkeysphere authentication..."
  176. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  177. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  178. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  179. EOF
  180. monkeysphere-authentication setup
  181. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  182. # add admin as identity certifier for testhost
  183. echo "##################################################"
  184. echo "### adding admin as certifier..."
  185. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  186. echo "##################################################"
  187. echo "### list certifiers..."
  188. monkeysphere-authentication list-certifiers
  189. # FIXME: should we run "diagnostics" here to test setup?
  190. ######################################################################
  191. ### TESTUSER SETUP
  192. # generate an auth subkey for the test user that expires in 2 days
  193. echo "##################################################"
  194. echo "### generating key for testuser..."
  195. monkeysphere gen-subkey
  196. # add server key to testuser keychain
  197. echo "##################################################"
  198. echo "### export server key to testuser..."
  199. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  200. # teach the "server" about the testuser's key
  201. echo "##################################################"
  202. echo "### export testuser key to server..."
  203. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  204. # update authorized_keys for user
  205. echo "##################################################"
  206. echo "### update server authorized_keys file for this testuser..."
  207. monkeysphere-authentication update-users $(whoami)
  208. # FIXME: this is maybe not failing properly for:
  209. # ms: improper group or other writability on path '/tmp'.
  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. monkeysphere-authentication update-users $(whoami)
  246. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  247. # FIXME: addtest: add hostname on host key
  248. # FIXME: addtest: revoke hostname on host key and check ssh failure
  249. # addtest: revoke the host key and check ssh failure
  250. # test to make sure things are OK after the previous tests:
  251. ssh_test
  252. echo "##################################################"
  253. echo "### ssh connection test for server with revoked key..."
  254. # generate the revocation certificate and feed it directly to the test
  255. # user's keyring (we're not publishing to the keyservers)
  256. monkeysphere-host revoke-key | gpg --import
  257. ssh_test 255
  258. ######################################################################
  259. trap - EXIT
  260. echo "##################################################"
  261. echo " Monkeysphere basic tests completed successfully!"
  262. echo "##################################################"
  263. cleanup