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