summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 5907154735f849486d958230299c8100a78aa6f8 (plain)
  1. #!/usr/bin/env bash
  2. # Tests to ensure that the monkeysphere is working
  3. #
  4. # unset MONKEYSPHERE_TEST_NO_EXAMINE to examine
  5. # Authors:
  6. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  7. # Jameson Rollins <jrollins@fifthhorseman.net>
  8. # Micah Anderson <micah@riseup.net>
  9. #
  10. # Copyright: 2008-2009
  11. # License: GPL v3 or later
  12. # these tests should all be able to run as a non-privileged user.
  13. # put all the test output to stdout
  14. exec 2>&1
  15. # all subcommands in this script should complete without failure:
  16. set -e
  17. # piped commands should return the code of the first non-zero return
  18. set -o pipefail
  19. # make sure the TESTDIR is an absolute path, not a relative one.
  20. export TESTDIR=$(cd $(dirname "$0") && pwd)
  21. source "$TESTDIR"/common
  22. ## make sure that the right tools are installed to run the test. the
  23. ## test has *more* requirements than plain ol' monkeysphere:
  24. [ -f /usr/sbin/sshd ] || { echo "You must have sshd installed to run this test." ; exit 1; }
  25. which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
  26. perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.
  27. On debian-derived systems, you can set this up with:
  28. apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
  29. perl -MDigest::SHA1 -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA1 installed to run this test.
  30. On debian-derived systems, you can set this up with:
  31. apt-get install libdigest-sha1-perl" ; exit 1; }
  32. ## FIXME: other checks?
  33. ######################################################################
  34. ### FUNCTIONS
  35. # gpg command for test admin user
  36. gpgadmin() {
  37. chmod 0700 "$TEMPDIR"/admin
  38. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
  39. }
  40. # test ssh connection
  41. # first argument is expected return code from ssh connection
  42. ssh_test() {
  43. local RETURN=0
  44. umask 0077
  45. CODE=${1:-0}
  46. # start the ssh daemon on the socket
  47. echo "##### starting ssh server..."
  48. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  49. SSHD_PID="$!"
  50. # wait until the socket is created before continuing
  51. while [ ! -S "$SOCKET" ] ; do
  52. sleep 1
  53. done
  54. # make a client connection to the socket
  55. echo "##### starting ssh client..."
  56. ssh-agent bash -c \
  57. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true" \
  58. || RETURN="$?"
  59. # kill the sshd process if it's still running
  60. kill "$SSHD_PID"
  61. SSHD_PID=
  62. if [ "$RETURN" = "$CODE" ] ; then
  63. echo "##### ssh connection test PASSED. returned: $RETURN"
  64. return 0
  65. else
  66. echo "##### ssh connection test FAILED. returned: $RETURN, expected: $CODE"
  67. return 1
  68. fi
  69. }
  70. SSHD_PID=
  71. ## setup trap
  72. trap failed_cleanup EXIT
  73. ######################################################################
  74. ### SETUP VARIABLES
  75. ## set up some variables to ensure that we're operating strictly in
  76. ## the tests, not system-wide:
  77. # set up temp dir
  78. # NOTE: /tmp can not be used as the temp dir here, since the
  79. # permissions on /tmp are usually such that they will not pass the
  80. # monkeysphere/ssh path permission checking. If you need to use a
  81. # different location than the current source, please set $TMPDIR
  82. # somewhere with tighter permissions.
  83. mkdir -p "$TESTDIR"/tmp
  84. TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
  85. # Use the local copy of executables first, instead of system ones.
  86. # This should help us test without installing.
  87. export PATH="$TESTDIR"/../src:"$PATH"
  88. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  89. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  90. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
  91. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  92. export MONKEYSPHERE_CHECK_KEYSERVER=false
  93. # example.org does not respond to the HKP port, so this should cause
  94. # any keyserver connection attempts that do happen (they shouldn't!)
  95. # to hang, so we'll notice them:
  96. export MONKEYSPHERE_KEYSERVER=example.org
  97. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  98. export MONKEYSPHERE_CORE_KEYLENGTH=1024
  99. export MONKEYSPHERE_PROMPT=false
  100. # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
  101. # into trying to use the user's key, instead of the testuser's key
  102. unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
  103. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  104. export SOCKET="$TEMPDIR"/ssh-socket
  105. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  106. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  107. # $DISPLAY (if one exists) because this test suite should not be doing
  108. # *anything* with any running X11 session.
  109. export DISPLAY=monkeys
  110. ######################################################################
  111. ### CONFIGURE ENVIRONMENTS
  112. # copy in admin and testuser home to tmp
  113. echo
  114. echo "##################################################"
  115. echo "### configuring testuser home..."
  116. (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
  117. # set up environment for testuser
  118. export TESTHOME="$TEMPDIR"/testuser
  119. export GNUPGHOME="$TESTHOME"/.gnupg
  120. chmod 0700 "$GNUPGHOME"
  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. echo
  133. echo "##################################################"
  134. echo "### configuring admin home..."
  135. (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
  136. # set up sshd
  137. echo
  138. echo "##################################################"
  139. echo "### configuring sshd..."
  140. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  141. # write the sshd_config
  142. cat <<EOF >> "$SSHD_CONFIG"
  143. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  144. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  145. EOF
  146. ######################################################################
  147. ### SERVER HOST SETUP
  148. # import host key
  149. echo
  150. echo "##################################################"
  151. echo "### import host key..."
  152. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  153. monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
  154. echo
  155. echo "##################################################"
  156. echo "### getting host key fingerprint..."
  157. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  158. echo "$HOSTKEYID"
  159. # change host key expiration
  160. echo
  161. echo "##################################################"
  162. echo "### setting host key expiration..."
  163. monkeysphere-host set-expire 1
  164. # FIXME: how do we check that the expiration has really been set?
  165. # certify host key with the "Admin's Key".
  166. # (this would normally be done via keyservers)
  167. echo
  168. echo "##################################################"
  169. echo "### certifying server host key..."
  170. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg 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 "##################################################"
  181. echo "### setup monkeysphere authentication..."
  182. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  183. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  184. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  185. EOF
  186. monkeysphere-authentication setup
  187. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  188. # add admin as identity certifier for testhost
  189. echo
  190. echo "##################################################"
  191. echo "### adding admin as certifier..."
  192. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  193. echo
  194. echo "##################################################"
  195. echo "### list certifiers..."
  196. monkeysphere-authentication list-certifiers
  197. # FIXME: should we run "diagnostics" here to test setup?
  198. ######################################################################
  199. ### TESTUSER SETUP
  200. # generate an auth subkey for the test user that expires in 2 days
  201. echo
  202. echo "##################################################"
  203. echo "### generating key for testuser..."
  204. monkeysphere gen-subkey
  205. # add server key to testuser keychain
  206. echo
  207. echo "##################################################"
  208. echo "### export server key to testuser..."
  209. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  210. # teach the "server" about the testuser's key
  211. echo
  212. echo "##################################################"
  213. echo "### export testuser key to server..."
  214. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  215. # update authorized_keys for user
  216. echo
  217. echo "##################################################"
  218. echo "### update server authorized_keys file for this testuser..."
  219. monkeysphere-authentication update-users $(whoami)
  220. # FIXME: this is maybe not failing properly for:
  221. # ms: improper group or other writability on path '/tmp'.
  222. ######################################################################
  223. ### TESTS
  224. # connect to test sshd, using monkeysphere ssh-proxycommand to verify
  225. # the identity before connection. This should work in both directions!
  226. echo
  227. echo "##################################################"
  228. echo "### ssh connection test for success..."
  229. ssh_test
  230. # remove the testuser's authorized_user_ids file, update, and make
  231. # sure that the ssh authentication FAILS
  232. echo
  233. echo "##################################################"
  234. echo "### removing testuser authorized_user_ids and updating..."
  235. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  236. monkeysphere-authentication update-users $(whoami)
  237. echo
  238. echo "##################################################"
  239. echo "### ssh connection test for server authentication denial..."
  240. ssh_test 255
  241. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  242. # put improper permissions on authorized_user_ids file, update, and
  243. # make sure ssh authentication FAILS
  244. echo
  245. echo "##################################################"
  246. echo "### setting group writability on authorized_user_ids and updating..."
  247. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  248. monkeysphere-authentication update-users $(whoami)
  249. echo
  250. echo "##################################################"
  251. echo "### ssh connection test for server authentication denial..."
  252. ssh_test 255
  253. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  254. echo
  255. echo "##################################################"
  256. echo "### setting other writability on authorized_user_ids and updating..."
  257. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  258. monkeysphere-authentication update-users $(whoami)
  259. echo
  260. echo "##################################################"
  261. echo "### ssh connection test for server authentication denial..."
  262. ssh_test 255
  263. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  264. monkeysphere-authentication update-users $(whoami)
  265. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  266. # FIXME: addtest: add hostname on host key
  267. # FIXME: addtest: revoke hostname on host key and check ssh failure
  268. # addtest: revoke the host key and check ssh failure
  269. # test to make sure things are OK after the previous tests:
  270. ssh_test
  271. echo
  272. echo "##################################################"
  273. echo "### ssh connection test for server with revoked key..."
  274. # generate the revocation certificate and feed it directly to the test
  275. # user's keyring (we're not publishing to the keyservers)
  276. monkeysphere-host revoke-key | gpg --import
  277. ssh_test 255
  278. ######################################################################
  279. trap - EXIT
  280. echo
  281. echo "##################################################"
  282. echo " Monkeysphere basic tests completed successfully!"
  283. echo "##################################################"
  284. cleanup