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