summaryrefslogtreecommitdiff
path: root/tests/basic
blob: ad72a2282dbd8e84af3b92daac70e337aae04b11 (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. # all subcommands in this script should complete without failure:
  12. set -e
  13. # piped commands should return the code of the first non-zero return
  14. set -o pipefail
  15. ## make sure that the right tools are installed to run the test. the
  16. ## test has *more* requirements than plain ol' monkeysphere:
  17. which socat >/dev/null || { echo "You must have socat installed to run this test." ; exit 1; }
  18. ## FIXME: other checks?
  19. ######################################################################
  20. ### FUNCTIONS
  21. # gpg command for test admin user
  22. gpgadmin() {
  23. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
  24. }
  25. # test ssh connection
  26. # first argument is expected return code from ssh connection
  27. ssh_test() {
  28. umask 0077
  29. CODE=${1:-0}
  30. # start the ssh daemon on the socket
  31. echo "##### starting ssh server..."
  32. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  33. SSHD_PID="$!"
  34. # wait until the socket is created before continuing
  35. while [ ! -S "$SOCKET" ] ; do
  36. sleep 1
  37. done
  38. set +e
  39. # make a client connection to the socket
  40. echo "##### starting ssh client..."
  41. ssh-agent bash -c \
  42. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
  43. RETURN="$?"
  44. # kill the sshd process if it's still running
  45. kill "$SSHD_PID"
  46. SSHD_PID=
  47. set -e
  48. echo "##### return $RETURN"
  49. if [ "$RETURN" = "$CODE" ] ; then
  50. echo "##### ssh connection test returned as desired"
  51. return 0
  52. else
  53. echo "##### ssh connection test failed. expected return code $CODE"
  54. return 1
  55. fi
  56. }
  57. failed_cleanup() {
  58. # FIXME: can we be more verbose here?
  59. echo 'FAILED!'
  60. read -p "press enter to cleanup and remove tmp:"
  61. cleanup
  62. }
  63. get_gpg_prng_arg() {
  64. if (gpg --quick-random --version >/dev/null 2>&1) ; then
  65. echo quick-random
  66. elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
  67. echo debug-quick-random
  68. fi
  69. }
  70. cleanup() {
  71. echo "### removing temp dir..."
  72. rm -rf "$TEMPDIR"
  73. if [ "$SSHD_PID" ] ; then
  74. echo "### killing off lingering sshd..."
  75. kill "$SSHD_PID"
  76. fi
  77. wait
  78. }
  79. SSHD_PID=
  80. ## setup trap
  81. trap failed_cleanup EXIT
  82. ######################################################################
  83. ### SETUP VARIABLES
  84. ## set up some variables to ensure that we're operating strictly in
  85. ## the tests, not system-wide:
  86. export TESTDIR=$(dirname "$0")
  87. # make temp dir
  88. TEMPDIR="$TESTDIR"/tmp
  89. if [ -e "$TEMPDIR" ] ; then
  90. echo "tempdir '$TEMPDIR' already exists."
  91. exit 1
  92. fi
  93. # Use the local copy of executables first, instead of system ones.
  94. # This should help us test without installing.
  95. export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
  96. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  97. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  98. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src/share
  99. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  100. export MONKEYSPHERE_CHECK_KEYSERVER=false
  101. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  102. export MONKEYSPHERE_CORE_KEYLENGTH=1024
  103. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  104. export SOCKET="$TEMPDIR"/ssh-socket
  105. # Make sure $DISPLAY is set to convince ssh and monkeysphere to fall
  106. # back on $SSH_ASKPASS. Make sure it's not set to the current actual
  107. # $DISPLAY (if one exists) because this test suite should not be doing
  108. # *anything* with any running X11 session.
  109. export DISPLAY=monkeys
  110. ######################################################################
  111. ### TEST KEY CONVERSION
  112. mkdir -p "$TEMPDIR"
  113. echo "##################################################"
  114. echo "### test key conversion..."
  115. export GNUPGHOME="$TEMPDIR"
  116. # generate a key
  117. gpg --batch --quick-random --gen-key <<EOF
  118. Key-Type: RSA
  119. Key-Length: 1024
  120. Key-Usage: sign
  121. Name-Real: testtest
  122. Expire-Date: 0
  123. %commit
  124. %echo done
  125. EOF
  126. # get the the key timestamp
  127. timestamp=$(gpg --list-key --with-colons --fixed-list-mode | \
  128. grep ^pub: | cut -d: -f6)
  129. # export the key to a file
  130. gpg --export-secret-key | openpgp2ssh > \
  131. "$TEMPDIR"/test.pem
  132. # reconvert key, and compare to key in gpg keyring
  133. diff -u \
  134. <(gpg --export-secret-key | hd) \
  135. <(PEM2OPENPGP_USAGE_FLAGS=sign,certify \
  136. PEM2OPENPGP_TIMESTAMP="$timestamp" pem2openpgp testtest < \
  137. "$TEMPDIR"/test.pem | hd )
  138. # clear out the temp dir
  139. rm -rf "$TEMPDIR"
  140. mkdir -p "$TEMPDIR"
  141. ######################################################################
  142. ### CONFIGURE ENVIRONMENTS
  143. # copy in admin and testuser home to tmp
  144. echo "##################################################"
  145. echo "### copying admin and testuser homes..."
  146. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  147. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  148. # set up environment for testuser
  149. export TESTHOME="$TEMPDIR"/testuser
  150. export GNUPGHOME="$TESTHOME"/.gnupg
  151. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  152. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  153. cat <<EOF >> "$TESTHOME"/.ssh/config
  154. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  155. IdentityFile $TESTHOME/.ssh/no-such-identity
  156. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  157. EOF
  158. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  159. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  160. EOF
  161. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  162. # set up sshd
  163. echo "##################################################"
  164. echo "### configuring sshd..."
  165. cp "$TESTDIR"/etc/ssh/sshd_config "$SSHD_CONFIG"
  166. # write the sshd_config
  167. cat <<EOF >> "$SSHD_CONFIG"
  168. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  169. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authentication/authorized_keys/%u
  170. EOF
  171. ######################################################################
  172. ### SERVER HOST SETUP
  173. # create a new host key
  174. echo "##################################################"
  175. echo "### testing host key generation..."
  176. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/host
  177. # add gpg.conf with quick-random
  178. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/host/gpg.conf
  179. echo | monkeysphere-host expert gen-key --length 1024 testhost
  180. # remove the host home for the next test
  181. rm -rf "$MONKEYSPHERE_SYSCONFIGDIR"/host
  182. # import host key
  183. echo "##################################################"
  184. echo "### testing host key importing..."
  185. ssh-keygen -b 1024 -t rsa -N '' -f "$TEMPDIR"/ssh_host_rsa_key
  186. monkeysphere-host expert import-key testhost < "$TEMPDIR"/ssh_host_rsa_key
  187. # change host key expiration
  188. echo "##################################################"
  189. echo "### setting host key expiration..."
  190. monkeysphere-host set-expire 1
  191. monkeysphere-host show-key
  192. # FIXME: how do we check that the expiration has really been set?
  193. echo "##################################################"
  194. echo "### getting host key fingerprint..."
  195. HOSTKEYID=$( monkeysphere-host show-key | grep '^OpenPGP fingerprint: ' | cut -f3 -d\ )
  196. # certify host key with the "Admin's Key".
  197. # (this would normally be done via keyservers)
  198. echo "##################################################"
  199. echo "### certifying server host key..."
  200. GNUPGHOME="$MONKEYSPHERE_SYSCONFIGDIR"/host gpg --armor --export "$HOSTKEYID" | gpgadmin --import
  201. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  202. # FIXME: add revoker?
  203. # FIXME: how can we test publish-key without flooding junk into the
  204. # keyservers?
  205. # FIXME: should we run "diagnostics" here to test setup?
  206. ######################################################################
  207. ### SERVER AUTHENTICATION SETUP
  208. # set up monkeysphere authentication
  209. echo "##################################################"
  210. echo "### setup monkeysphere authentication..."
  211. cp "$TESTDIR"/etc/monkeysphere/monkeysphere-authentication.conf "$TEMPDIR"/
  212. cat <<EOF >> "$TEMPDIR"/monkeysphere-authentication.conf
  213. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authentication/authorized_user_ids"
  214. EOF
  215. monkeysphere-authentication setup
  216. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSDATADIR"/authentication/sphere/gpg.conf
  217. # add admin as identity certifier for testhost
  218. echo "##################################################"
  219. echo "### adding admin as certifier..."
  220. echo y | monkeysphere-authentication add-id-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  221. # FIXME: should we run "diagnostics" here to test setup?
  222. ######################################################################
  223. ### TESTUSER SETUP
  224. # generate an auth subkey for the test user that expires in 2 days
  225. echo "##################################################"
  226. echo "### generating key for testuser..."
  227. monkeysphere gen-subkey --expire 2
  228. # add server key to testuser keychain
  229. echo "##################################################"
  230. echo "### export server key to testuser..."
  231. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  232. # teach the "server" about the testuser's key
  233. echo "##################################################"
  234. echo "### export testuser key to server..."
  235. gpg --export testuser | monkeysphere-authentication gpg-cmd --import
  236. # update authorized_keys for user
  237. echo "##################################################"
  238. echo "### update server authorized_keys file for this testuser..."
  239. monkeysphere-authentication update-users $(whoami)
  240. ######################################################################
  241. ### TESTS
  242. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  243. # the identity before connection. This should work in both directions!
  244. echo "##################################################"
  245. echo "### ssh connection test for success..."
  246. ssh_test
  247. # remove the testuser's authorized_user_ids file, update, and make
  248. # sure that the ssh authentication FAILS
  249. echo "##################################################"
  250. echo "### removing testuser authorized_user_ids and updating..."
  251. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{,.bak}
  252. monkeysphere-authentication update-users $(whoami)
  253. echo "##################################################"
  254. echo "### ssh connection test for server authentication denial..."
  255. ssh_test 255
  256. mv "$TESTHOME"/.monkeysphere/authorized_user_ids{.bak,}
  257. # put improper permissions on authorized_user_ids file, update, and
  258. # make sure ssh authentication FAILS
  259. echo "##################################################"
  260. echo "### setting group writability on authorized_user_ids and updating..."
  261. chmod g+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  262. monkeysphere-authentication update-users $(whoami)
  263. echo "##################################################"
  264. echo "### ssh connection test for server authentication denial..."
  265. ssh_test 255
  266. chmod g-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  267. echo "##################################################"
  268. echo "### setting other writability on authorized_user_ids and updating..."
  269. chmod o+w "$TESTHOME"/.monkeysphere/authorized_user_ids
  270. monkeysphere-authentication update-users $(whoami)
  271. echo "##################################################"
  272. echo "### ssh connection test for server authentication denial..."
  273. ssh_test 255
  274. chmod o-w "$TESTHOME"/.monkeysphere/authorized_user_ids
  275. # FIXME: addtest: remove admin as id-certifier and check ssh failure
  276. # FIXME: addtest: add hostname on host key
  277. # FIXME: addtest: revoke hostname on host key and check ssh failure
  278. # FIXME: addtest: revoke the host key and check ssh failure
  279. ######################################################################
  280. trap - EXIT
  281. echo "##################################################"
  282. echo " Monkeysphere basic tests completed successfully!"
  283. echo "##################################################"
  284. cleanup