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