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