summaryrefslogtreecommitdiff
path: root/tests/basic
blob: a3d32b49868631cf6711ebdcacee6973f81dbcc0 (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. ## FIXME: other checks?
  24. ######################################################################
  25. ### FUNCTIONS
  26. # gpg command for test admin user
  27. gpgadmin() {
  28. chmod 0700 "$TEMPDIR"/admin
  29. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
  30. }
  31. # test ssh connection
  32. # first argument is expected return code from ssh connection
  33. ssh_test() {
  34. umask 0077
  35. CODE=${1:-0}
  36. # start the ssh daemon on the socket
  37. echo "##### starting ssh server..."
  38. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  39. SSHD_PID="$!"
  40. # wait until the socket is created before continuing
  41. while [ ! -S "$SOCKET" ] ; do
  42. sleep 1
  43. done
  44. # make a client connection to the socket
  45. echo "##### starting ssh client..."
  46. ssh-agent bash -c \
  47. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true" \
  48. || RETURN="$?"
  49. # kill the sshd process if it's still running
  50. kill "$SSHD_PID"
  51. SSHD_PID=
  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 "##################################################"
  103. echo "### checking version string match..."
  104. repver=$(monkeysphere version)
  105. debver=$(head -n1 "$TESTDIR"/../packaging/debian/changelog | sed 's/.*(\([^-]*\)-.*/\1/')
  106. if [ "$repver" = "$debver" ] ; then
  107. echo "Versions match!"
  108. else
  109. printf "reported version string (%s) does not match debian changelog (%s)\n" "$repver" "$debver"
  110. exit 1
  111. fi
  112. fi
  113. ######################################################################
  114. ### CONFIGURE ENVIRONMENTS
  115. # copy in admin and testuser home to tmp
  116. echo
  117. echo "##################################################"
  118. echo "### configuring testuser home..."
  119. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  120. # set up environment for testuser
  121. export TESTHOME="$TEMPDIR"/testuser
  122. export GNUPGHOME="$TESTHOME"/.gnupg
  123. chmod 0700 "$GNUPGHOME"
  124. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  125. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  126. cat <<EOF >> "$TESTHOME"/.ssh/config
  127. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  128. IdentityFile $TESTHOME/.ssh/no-such-identity
  129. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  130. EOF
  131. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  132. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  133. EOF
  134. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  135. echo
  136. echo "##################################################"
  137. echo "### configuring admin home..."
  138. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  139. # set up sshd
  140. echo
  141. echo "##################################################"
  142. echo "### configuring sshd..."
  143. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  144. # write the sshd_config
  145. cat <<EOF >> "$SSHD_CONFIG"
  146. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  147. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  148. EOF
  149. ######################################################################
  150. ### SERVER HOST SETUP
  151. # import host key
  152. echo
  153. echo "##################################################"
  154. echo "### import host key..."
  155. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  156. monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
  157. echo
  158. echo "##################################################"
  159. echo "### getting host key fingerprint..."
  160. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  161. echo "$HOSTKEYID"
  162. # change host key expiration
  163. echo
  164. echo "##################################################"
  165. echo "### setting host key expiration..."
  166. monkeysphere-host set-expire 1
  167. # FIXME: how do we check that the expiration has really been set?
  168. # certify host key with the "Admin's Key".
  169. # (this would normally be done via keyservers)
  170. echo
  171. echo "##################################################"
  172. echo "### certifying server host key..."
  173. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
  174. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  175. # FIXME: add revoker?
  176. # FIXME: how can we test publish-key without flooding junk into the
  177. # keyservers?
  178. # FIXME: should we run "diagnostics" here to test setup?
  179. ######################################################################
  180. ### SERVER AUTHENTICATION SETUP
  181. # set up monkeysphere authentication
  182. echo
  183. echo "##################################################"
  184. echo "### setup monkeysphere authentication..."
  185. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  186. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  187. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  188. EOF
  189. monkeysphere-authentication setup
  190. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  191. # add admin as identity certifier for testhost
  192. echo
  193. echo "##################################################"
  194. echo "### adding admin as certifier..."
  195. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  196. echo
  197. echo "##################################################"
  198. echo "### list certifiers..."
  199. monkeysphere-authentication list-certifiers
  200. # FIXME: should we run "diagnostics" here to test setup?
  201. ######################################################################
  202. ### TESTUSER SETUP
  203. # generate an auth subkey for the test user that expires in 2 days
  204. echo
  205. echo "##################################################"
  206. echo "### generating key for testuser..."
  207. monkeysphere gen-subkey
  208. # add server key to testuser keychain
  209. echo
  210. echo "##################################################"
  211. echo "### export server key to testuser..."
  212. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  213. # teach the "server" about the testuser's key
  214. echo
  215. echo "##################################################"
  216. echo "### export testuser key to server..."
  217. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  218. # update authorized_keys for user
  219. echo
  220. echo "##################################################"
  221. echo "### update server authorized_keys file for this testuser..."
  222. monkeysphere-authentication update-users $(whoami)
  223. # FIXME: this is maybe not failing properly for:
  224. # ms: improper group or other writability on path '/tmp'.
  225. ######################################################################
  226. ### TESTS
  227. # connect to test sshd, using monkeysphere ssh-proxycommand to verify
  228. # the identity before connection. This should work in both directions!
  229. echo
  230. echo "##################################################"
  231. echo "### ssh connection test for success..."
  232. ssh_test
  233. # remove the testuser's authorized_user_ids file, update, and make
  234. # sure that the ssh authentication FAILS
  235. echo
  236. echo "##################################################"
  237. echo "### removing testuser authorized_user_ids and updating..."
  238. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  239. monkeysphere-authentication update-users $(whoami)
  240. echo
  241. echo "##################################################"
  242. echo "### ssh connection test for server authentication denial..."
  243. ssh_test 255
  244. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  245. # put improper permissions on authorized_user_ids file, update, and
  246. # make sure ssh authentication FAILS
  247. echo
  248. echo "##################################################"
  249. echo "### setting group writability on authorized_user_ids and updating..."
  250. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  251. monkeysphere-authentication update-users $(whoami)
  252. echo
  253. echo "##################################################"
  254. echo "### ssh connection test for server authentication denial..."
  255. ssh_test 255
  256. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  257. echo
  258. echo "##################################################"
  259. echo "### setting other writability on authorized_user_ids and updating..."
  260. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  261. monkeysphere-authentication update-users $(whoami)
  262. echo
  263. echo "##################################################"
  264. echo "### ssh connection test for server authentication denial..."
  265. ssh_test 255
  266. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  267. monkeysphere-authentication update-users $(whoami)
  268. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  269. # FIXME: addtest: add hostname on host key
  270. # FIXME: addtest: revoke hostname on host key and check ssh failure
  271. # addtest: revoke the host key and check ssh failure
  272. # test to make sure things are OK after the previous tests:
  273. ssh_test
  274. echo
  275. echo "##################################################"
  276. echo "### ssh connection test for server with revoked key..."
  277. # generate the revocation certificate and feed it directly to the test
  278. # user's keyring (we're not publishing to the keyservers)
  279. monkeysphere-host revoke-key | gpg --import
  280. ssh_test 255
  281. ######################################################################
  282. trap - EXIT
  283. echo
  284. echo "##################################################"
  285. echo " Monkeysphere basic tests completed successfully!"
  286. echo "##################################################"
  287. cleanup