summaryrefslogtreecommitdiff
path: root/tests/basic
blob: f38c9ac89119b15006cfd44d0d29e89c366e6e8a (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:"$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. # unset SUBKEYS_FOR_AGENT variable which, if set, would confuse the
  99. # into trying to use the user's key, instead of the testuser's key
  100. unset MONKEYSPHERE_SUBKEYS_FOR_AGENT
  101. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  102. export SOCKET="$TEMPDIR"/ssh-socket
  103. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  104. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  105. # $DISPLAY (if one exists) because this test suite should not be doing
  106. # *anything* with any running X11 session.
  107. export DISPLAY=monkeys
  108. ######################################################################
  109. ### CONFIGURE ENVIRONMENTS
  110. # copy in admin and testuser home to tmp
  111. echo
  112. echo "##################################################"
  113. echo "### configuring testuser home..."
  114. (cd "$TESTDIR"/home && find testuser | cpio -pdu "$TEMPDIR")
  115. # set up environment for testuser
  116. export TESTHOME="$TEMPDIR"/testuser
  117. export GNUPGHOME="$TESTHOME"/.gnupg
  118. chmod 0700 "$GNUPGHOME"
  119. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  120. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  121. cat <<EOF >> "$TESTHOME"/.ssh/config
  122. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  123. IdentityFile $TESTHOME/.ssh/no-such-identity
  124. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  125. EOF
  126. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  127. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  128. EOF
  129. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  130. echo
  131. echo "##################################################"
  132. echo "### configuring admin home..."
  133. (cd "$TESTDIR"/home && find admin | cpio -pdu "$TEMPDIR")
  134. # set up sshd
  135. echo
  136. echo "##################################################"
  137. echo "### configuring sshd..."
  138. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  139. # write the sshd_config
  140. cat <<EOF >> "$SSHD_CONFIG"
  141. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  142. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  143. EOF
  144. ######################################################################
  145. ### SERVER HOST SETUP
  146. # import host key
  147. echo
  148. echo "##################################################"
  149. echo "### import host key..."
  150. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  151. monkeysphere-host import-key "$TEMPDIR"/ssh_host_rsa_key testhost
  152. echo
  153. echo "##################################################"
  154. echo "### getting host key fingerprint..."
  155. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  156. echo "$HOSTKEYID"
  157. # change host key expiration
  158. echo
  159. echo "##################################################"
  160. echo "### setting host key expiration..."
  161. monkeysphere-host set-expire 1
  162. # FIXME: how do we check that the expiration has really been set?
  163. # certify host key with the "Admin's Key".
  164. # (this would normally be done via keyservers)
  165. echo
  166. echo "##################################################"
  167. echo "### certifying server host key..."
  168. < "$MONKEYSPHERE_SYSCONFIGDIR"/ssh_host_rsa_key.pub.gpg gpgadmin --import
  169. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  170. # FIXME: add revoker?
  171. # FIXME: how can we test publish-key without flooding junk into the
  172. # keyservers?
  173. # FIXME: should we run "diagnostics" here to test setup?
  174. ######################################################################
  175. ### SERVER AUTHENTICATION SETUP
  176. # set up monkeysphere authentication
  177. echo
  178. echo "##################################################"
  179. echo "### setup monkeysphere authentication..."
  180. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  181. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  182. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  183. EOF
  184. monkeysphere-authentication setup
  185. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  186. # add admin as identity certifier for testhost
  187. echo
  188. echo "##################################################"
  189. echo "### adding admin as certifier..."
  190. monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  191. echo
  192. echo "##################################################"
  193. echo "### list certifiers..."
  194. monkeysphere-authentication list-certifiers
  195. # FIXME: should we run "diagnostics" here to test setup?
  196. ######################################################################
  197. ### TESTUSER SETUP
  198. # generate an auth subkey for the test user that expires in 2 days
  199. echo
  200. echo "##################################################"
  201. echo "### generating key for testuser..."
  202. monkeysphere gen-subkey
  203. # add server key to testuser keychain
  204. echo
  205. echo "##################################################"
  206. echo "### export server key to testuser..."
  207. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  208. # teach the "server" about the testuser's key
  209. echo
  210. echo "##################################################"
  211. echo "### export testuser key to server..."
  212. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  213. # update authorized_keys for user
  214. echo
  215. echo "##################################################"
  216. echo "### update server authorized_keys file for this testuser..."
  217. monkeysphere-authentication update-users $(whoami)
  218. # FIXME: this is maybe not failing properly for:
  219. # ms: improper group or other writability on path '/tmp'.
  220. ######################################################################
  221. ### TESTS
  222. # connect to test sshd, using monkeysphere ssh-proxycommand to verify
  223. # the identity before connection. This should work in both directions!
  224. echo
  225. echo "##################################################"
  226. echo "### ssh connection test for success..."
  227. ssh_test
  228. # remove the testuser's authorized_user_ids file, update, and make
  229. # sure that the ssh authentication FAILS
  230. echo
  231. echo "##################################################"
  232. echo "### removing testuser authorized_user_ids and updating..."
  233. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  234. monkeysphere-authentication update-users $(whoami)
  235. echo
  236. echo "##################################################"
  237. echo "### ssh connection test for server authentication denial..."
  238. ssh_test 255
  239. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  240. # put improper permissions on authorized_user_ids file, update, and
  241. # make sure ssh authentication FAILS
  242. echo
  243. echo "##################################################"
  244. echo "### setting group writability on authorized_user_ids and updating..."
  245. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  246. monkeysphere-authentication update-users $(whoami)
  247. echo
  248. echo "##################################################"
  249. echo "### ssh connection test for server authentication denial..."
  250. ssh_test 255
  251. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  252. echo
  253. echo "##################################################"
  254. echo "### setting other writability on authorized_user_ids and updating..."
  255. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  256. monkeysphere-authentication update-users $(whoami)
  257. echo
  258. echo "##################################################"
  259. echo "### ssh connection test for server authentication denial..."
  260. ssh_test 255
  261. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  262. monkeysphere-authentication update-users $(whoami)
  263. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  264. # FIXME: addtest: add hostname on host key
  265. # FIXME: addtest: revoke hostname on host key and check ssh failure
  266. # addtest: revoke the host key and check ssh failure
  267. # test to make sure things are OK after the previous tests:
  268. ssh_test
  269. echo
  270. echo "##################################################"
  271. echo "### ssh connection test for server with revoked key..."
  272. # generate the revocation certificate and feed it directly to the test
  273. # user's keyring (we're not publishing to the keyservers)
  274. monkeysphere-host revoke-key | gpg --import
  275. ssh_test 255
  276. ######################################################################
  277. trap - EXIT
  278. echo
  279. echo "##################################################"
  280. echo " Monkeysphere basic tests completed successfully!"
  281. echo "##################################################"
  282. cleanup