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