summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 83bf7536a04c4a57dbe6e67624b2b61c6f6a42f1 (plain)
  1. #!/usr/bin/env bash
  2. # Tests to ensure that the monkeysphere is working
  3. #
  4. # unset MONKEYSPHERE_TEST_NO_EXAMINE to examine
  5. # Authors:
  6. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  7. # Jameson Rollins <jrollins@fifthhorseman.net>
  8. # Micah Anderson <micah@riseup.net>
  9. #
  10. # Copyright: 2008-2009
  11. # License: GPL v3 or later
  12. # these tests should all be able to run as a non-privileged user.
  13. # put all the test output to stdout
  14. exec 2>&1
  15. # all subcommands in this script should complete without failure:
  16. set -e
  17. # piped commands should return the code of the first non-zero return
  18. set -o pipefail
  19. # make sure the TESTDIR is an absolute path, not a relative one.
  20. export TESTDIR=$(cd $(dirname "$0") && pwd)
  21. source "$TESTDIR"/common
  22. ## make sure that the right tools are installed to run the test. the
  23. ## test has *more* requirements than plain ol' monkeysphere:
  24. [ -f /usr/sbin/sshd ] || { echo "You must have sshd installed to run this test." ; exit 1; }
  25. which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
  26. perl -MCrypt::OpenSSL::RSA -e 1 2>/dev/null || { echo "You must have the perl module Crypt::OpenSSL::RSA installed to run this test.
  27. On debian-derived systems, you can set this up with:
  28. apt-get install libcrypt-openssl-rsa-perl" ; exit 1; }
  29. perl -MDigest::SHA -e 1 2>/dev/null || { echo "You must have the perl module Digest::SHA installed to run this test.
  30. On debian-derived systems, you can set this up with:
  31. apt-get install libdigest-sha-perl" ; exit 1; }
  32. ## FIXME: other checks?
  33. ######################################################################
  34. ### FUNCTIONS
  35. # gpg command for test admin user
  36. gpgadmin() {
  37. chmod 0700 "$TEMPDIR"/admin
  38. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg --no-tty "$@"
  39. }
  40. # test ssh connection
  41. # first argument is expected return code from ssh connection
  42. ssh_test() {
  43. local RETURN=0
  44. umask 0077
  45. CODE=${1:-0}
  46. # start the ssh daemon on the socket
  47. echo "##### starting ssh server..."
  48. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  49. SSHD_PID="$!"
  50. # wait until the socket is created before continuing
  51. while [ ! -S "$SOCKET" ] ; do
  52. sleep 1
  53. done
  54. # make a client connection to the socket
  55. echo "##### starting ssh client..."
  56. ssh-agent bash -c \
  57. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config ${target_hostname:-testhost} true" \
  58. || RETURN="$?"
  59. # kill the sshd process if it's still running
  60. kill "$SSHD_PID" || true
  61. wait
  62. SSHD_PID=
  63. if [ "$RETURN" = "$CODE" ] ; then
  64. echo "##### ssh connection test PASSED. returned: $RETURN"
  65. return 0
  66. else
  67. echo "##### ssh connection test FAILED. returned: $RETURN, expected: $CODE"
  68. return 1
  69. fi
  70. }
  71. SSHD_PID=
  72. ## setup trap
  73. trap failed_cleanup EXIT
  74. ######################################################################
  75. ### SETUP VARIABLES
  76. ## set up some variables to ensure that we're operating strictly in
  77. ## the tests, not system-wide:
  78. # set up temp dir
  79. # NOTE: /tmp can not be used as the temp dir here, since the
  80. # permissions on /tmp are usually such that they will not pass the
  81. # monkeysphere/ssh path permission checking. If you need to use a
  82. # different location than the current source, please set $TMPDIR
  83. # somewhere with tighter permissions.
  84. mkdir -p "$TESTDIR"/tmp
  85. TEMPDIR=$(mktemp -d "${TMPDIR:-$TESTDIR/tmp}/monkeyspheretest.XXXXXXX")
  86. # Use the local copy of executables first, instead of system ones.
  87. # This should help us test without installing.
  88. export PATH="$TESTDIR"/../src:"$PATH"
  89. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  90. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  91. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
  92. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  93. export MONKEYSPHERE_CHECK_KEYSERVER=false
  94. # example.org does not respond to the HKP port, so this should cause
  95. # any keyserver connection attempts that do happen (they shouldn't!)
  96. # to hang, so we'll notice them:
  97. export MONKEYSPHERE_KEYSERVER=example.org
  98. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  99. export MONKEYSPHERE_CORE_KEYLENGTH=1024
  100. export MONKEYSPHERE_PROMPT=false
  101. # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
  102. # into trying to use the user's key, instead of the testuser's key
  103. unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
  104. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  105. export SOCKET="$TEMPDIR"/ssh-socket
  106. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  107. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  108. # $DISPLAY (if one exists) because this test suite should not be doing
  109. # *anything* with any running X11 session.
  110. export DISPLAY=monkeys
  111. ######################################################################
  112. ### CONFIGURE ENVIRONMENTS
  113. # copy in admin and testuser home to tmp
  114. echo
  115. echo "##################################################"
  116. echo "### configuring testuser home..."
  117. (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
  118. # set up environment for testuser
  119. export TESTHOME="$TEMPDIR"/testuser
  120. export GNUPGHOME="$TESTHOME"/.gnupg
  121. chmod 0700 "$GNUPGHOME"
  122. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  123. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  124. cat <<EOF >> "$TESTHOME"/.ssh/config
  125. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  126. IdentityFile $TESTHOME/.ssh/no-such-identity
  127. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  128. EOF
  129. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  130. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  131. EOF
  132. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  133. echo
  134. echo "##################################################"
  135. echo "### configuring admin home..."
  136. (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
  137. # set up sshd
  138. echo
  139. echo "##################################################"
  140. echo "### configuring sshd..."
  141. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  142. # write the sshd_config
  143. cat <<EOF >> "$SSHD_CONFIG"
  144. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  145. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  146. EOF
  147. ######################################################################
  148. ### SERVER HOST SETUP
  149. # import host key
  150. echo
  151. echo "##################################################"
  152. echo "### import host key..."
  153. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  154. monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
  155. echo
  156. echo "##################################################"
  157. echo "### getting host key fingerprint..."
  158. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  159. echo "$HOSTKEYID"
  160. # change host key expiration
  161. echo
  162. echo "##################################################"
  163. echo "### setting host key expiration..."
  164. monkeysphere-host set-expire 1
  165. # FIXME: how do we check that the expiration has really been set?
  166. # certify host key with the "Admin's Key".
  167. # (this would normally be done via keyservers)
  168. echo
  169. echo "##################################################"
  170. echo "### certifying server host key..."
  171. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
  172. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  173. # FIXME: add revoker?
  174. # FIXME: how can we test publish-key without flooding junk into the
  175. # keyservers?
  176. # FIXME: should we run "diagnostics" here to test setup?
  177. ######################################################################
  178. ### SERVER AUTHENTICATION SETUP
  179. # set up monkeysphere authentication
  180. echo
  181. echo "##################################################"
  182. echo "### setup monkeysphere authentication..."
  183. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  184. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  185. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  186. EOF
  187. monkeysphere-authentication setup
  188. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  189. # add admin as identity certifier for testhost
  190. echo
  191. echo "##################################################"
  192. echo "### adding admin as certifier..."
  193. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  194. echo
  195. echo "##################################################"
  196. echo "### list certifiers..."
  197. monkeysphere-authentication list-certifiers
  198. # FIXME: should we run "diagnostics" here to test setup?
  199. ######################################################################
  200. ### TESTUSER SETUP
  201. # generate an auth subkey for the test user that expires in 2 days
  202. echo
  203. echo "##################################################"
  204. echo "### generating key for testuser..."
  205. monkeysphere gen-subkey
  206. # add server key to testuser keychain
  207. echo
  208. echo "##################################################"
  209. echo "### export server key to testuser..."
  210. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  211. # teach the "server" about the testuser's key
  212. echo
  213. echo "##################################################"
  214. echo "### export testuser key to server..."
  215. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  216. # update authorized_keys for user
  217. echo
  218. echo "##################################################"
  219. echo "### update server authorized_keys file for this testuser..."
  220. monkeysphere-authentication update-users $(whoami)
  221. # FIXME: this is maybe not failing properly for:
  222. # ms: improper group or other writability on path '/tmp'.
  223. ######################################################################
  224. ### TESTS
  225. # connect to test sshd, using monkeysphere ssh-proxycommand to verify
  226. # the identity before connection. This should work in both directions!
  227. echo
  228. echo "##################################################"
  229. echo "### ssh connection test for success..."
  230. ssh_test
  231. # remove the testuser's authorized_user_ids file, update, and make
  232. # sure that the ssh authentication FAILS
  233. echo
  234. echo "##################################################"
  235. echo "### removing testuser authorized_user_ids and updating..."
  236. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  237. monkeysphere-authentication update-users $(whoami)
  238. echo
  239. echo "##################################################"
  240. echo "### ssh connection test for failure..."
  241. ssh_test 255
  242. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  243. # put improper permissions on authorized_user_ids file, update, and
  244. # make sure ssh authentication FAILS
  245. echo
  246. echo "##################################################"
  247. echo "### setting group writability on authorized_user_ids and updating..."
  248. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  249. monkeysphere-authentication update-users $(whoami)
  250. echo
  251. echo "##################################################"
  252. echo "### ssh connection test for failure..."
  253. ssh_test 255
  254. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  255. echo
  256. echo "##################################################"
  257. echo "### setting other writability on authorized_user_ids and updating..."
  258. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  259. monkeysphere-authentication update-users $(whoami)
  260. echo
  261. echo "##################################################"
  262. echo "### ssh connection test for failure..."
  263. ssh_test 255
  264. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  265. monkeysphere-authentication update-users $(whoami)
  266. # test symlinks
  267. echo
  268. echo "##################################################"
  269. echo "### setup for symlink tests..."
  270. cp -a "$TESTHOME"/.monkeysphere{,.linktest}
  271. echo
  272. echo "##################################################"
  273. echo "### make authorized_user_ids an absolute symlink and updating..."
  274. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  275. ln -s "$TESTHOME"/.monkeysphere{.linktest,}/authorized_user_ids
  276. monkeysphere-authentication update-users $(whoami)
  277. echo
  278. echo "##################################################"
  279. echo "### ssh connection test for success..."
  280. ssh_test
  281. echo
  282. echo "##################################################"
  283. echo "### create bad permissions on link dir and updating..."
  284. chmod o+w "$TESTHOME"/.monkeysphere.linktest
  285. monkeysphere-authentication update-users $(whoami)
  286. echo
  287. echo "##################################################"
  288. echo "### ssh connection test for failure..."
  289. ssh_test 255
  290. chmod o-w "$TESTHOME"/.monkeysphere.linktest
  291. echo
  292. echo "##################################################"
  293. echo "### make authorized_user_ids a relative symlink and updating..."
  294. ln -sf ../.monkeysphere.linktest/authorized_user_ids "$TESTHOME"/.monkeysphere/authorized_user_ids
  295. monkeysphere-authentication update-users $(whoami)
  296. echo
  297. echo "##################################################"
  298. echo "### ssh connection test for success..."
  299. ssh_test
  300. echo
  301. echo "##################################################"
  302. echo "### create bad permissions on link dir updating..."
  303. chmod o+w "$TESTHOME"/.monkeysphere.linktest
  304. monkeysphere-authentication update-users $(whoami)
  305. echo
  306. echo "##################################################"
  307. echo "### ssh connection test for failure..."
  308. ssh_test 255
  309. chmod o-w "$TESTHOME"/.monkeysphere.linktest
  310. # FIXME: implement check of link path, and uncomment this test
  311. # echo
  312. # echo "##################################################"
  313. # echo "### create bad permissions on link dir and updating..."
  314. # chmod o+w "$TESTHOME"/.monkeysphere
  315. # monkeysphere-authentication update-users $(whoami)
  316. # echo
  317. # echo "##################################################"
  318. # echo "### ssh connection test for failure..."
  319. # ssh_test 255
  320. # chmod o-w "$TESTHOME"/.monkeysphere
  321. rm "$TESTHOME"/.monkeysphere/authorized_user_ids
  322. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  323. echo
  324. echo "##################################################"
  325. echo "### make .monkeysphere directory an absolute symlink and updating..."
  326. mv "$TESTHOME"/.monkeysphere{,.bak}
  327. ln -s "$TESTHOME"/.monkeysphere{.linktest,}
  328. monkeysphere-authentication update-users $(whoami)
  329. echo
  330. echo "##################################################"
  331. echo "### ssh connection test for success..."
  332. ssh_test
  333. echo
  334. echo "##################################################"
  335. echo "### create bad permissions on link dir and updating..."
  336. chmod o+w "$TESTHOME"/.monkeysphere.linktest
  337. monkeysphere-authentication update-users $(whoami)
  338. echo
  339. echo "##################################################"
  340. echo "### ssh connection test for failure..."
  341. ssh_test 255
  342. chmod o-w "$TESTHOME"/.monkeysphere.linktest
  343. echo
  344. echo "##################################################"
  345. echo "### make .monkeysphere directory a relative symlink and updating..."
  346. ln -sfn .monkeysphere{.linktest,}
  347. monkeysphere-authentication update-users $(whoami)
  348. echo
  349. echo "##################################################"
  350. echo "### ssh connection test for success..."
  351. ssh_test
  352. echo
  353. echo "##################################################"
  354. echo "### create bad permissions on link dir updating..."
  355. chmod o+w "$TESTHOME"/.monkeysphere.linktest
  356. monkeysphere-authentication update-users $(whoami)
  357. echo
  358. echo "##################################################"
  359. echo "### ssh connection test for failure..."
  360. ssh_test 255
  361. chmod o-w "$TESTHOME"/.monkeysphere.linktest
  362. rm "$TESTHOME"/.monkeysphere
  363. mv "$TESTHOME"/.monkeysphere{.bak,}
  364. # ensure we're back to normal:
  365. echo
  366. echo "##################################################"
  367. echo "### making sure we are back to normal..."
  368. monkeysphere-authentication update-users $(whoami)
  369. ssh_test
  370. echo
  371. echo "##################################################"
  372. echo "### ssh connection test directly to 'testhost2' without new name..."
  373. target_hostname=testhost2 ssh_test 255
  374. echo
  375. echo "##################################################"
  376. echo "### add hostname, certify by admin, import by user..."
  377. monkeysphere-host add-hostname testhost2
  378. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
  379. printf "y\ny\n" | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  380. echo
  381. echo "##################################################"
  382. echo "### ssh connection test with hostname 'testhost2' added..."
  383. gpgadmin --export "$HOSTKEYID" | gpg --import
  384. gpg --check-trustdb
  385. ssh_test
  386. echo
  387. echo "##################################################"
  388. echo "### ssh connection test directly to 'testhost2' ..."
  389. gpg --import <"$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg
  390. gpg --check-trustdb
  391. target_hostname=testhost2 ssh_test
  392. echo
  393. echo "##################################################"
  394. echo "### ssh connection test for failure with 'testhost2' revoked..."
  395. monkeysphere-host revoke-hostname testhost2
  396. gpg --import <"$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg
  397. gpg --check-trustdb
  398. target_hostname=testhost2 ssh_test 255
  399. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  400. # FIXME: addtest: how do we test that set-expire makes sense after new
  401. # hostnames have been added?
  402. # FIXME: addtest: revoke the host key and check ssh failure
  403. # test to make sure things are OK after the previous tests:
  404. echo
  405. echo "##################################################"
  406. echo "### settings reset, updating..."
  407. monkeysphere-authentication update-users $(whoami)
  408. echo
  409. echo "##################################################"
  410. echo "### ssh connection test for success..."
  411. ssh_test
  412. echo
  413. echo "##################################################"
  414. echo "### revoking host key..."
  415. # generate the revocation certificate and feed it directly to the test
  416. # user's keyring (we're not publishing to the keyservers)
  417. monkeysphere-host revoke-key | gpg --import
  418. echo
  419. echo "##################################################"
  420. echo "### ssh connection test for failure..."
  421. ssh_test 255
  422. ######################################################################
  423. trap - EXIT
  424. echo
  425. echo "##################################################"
  426. echo " Monkeysphere basic tests completed successfully!"
  427. echo "##################################################"
  428. cleanup