summaryrefslogtreecommitdiff
path: root/tests/basic
blob: f6fa7fcbf1fed13bf33871594a2636da689b99d8 (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. # Copyright: 2008
  7. # License: GPL v3 or later
  8. # these tests should all be able to
  9. # as a non-privileged user.
  10. # all subcommands in this script should complete without failure:
  11. set -e
  12. # gpg command for test admin user
  13. gpgadmin() {
  14. GNUPGHOME="$TEMPDIR"/admin/.gnupg gpg "$@"
  15. }
  16. ssh_test() {
  17. umask 0077
  18. # start the ssh daemon on the socket
  19. echo "##### starting ssh server..."
  20. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  21. SSHD_PID="$!"
  22. # wait until the socket is created before continuing
  23. while [ ! -S "$SOCKET" ] ; do
  24. sleep 1
  25. done
  26. # make a client connection to the socket
  27. echo "##### starting ssh client..."
  28. ssh-agent bash -c \
  29. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost true"
  30. RETURN="$?"
  31. # kill the previous sshd process if it's still running
  32. kill "$SSHD_PID"
  33. return "$RETURN"
  34. }
  35. failed_cleanup() {
  36. # FIXME: can we be more verbose here?
  37. echo 'FAILED!'
  38. read -p "press enter to cleanup and remove tmp:"
  39. cleanup
  40. }
  41. get_gpg_prng_arg() {
  42. if (gpg --quick-random --version >/dev/null 2>&1) ; then
  43. echo quick-random
  44. elif (gpg --debug-quick-random --version >/dev/null 2>&1) ; then
  45. echo debug-quick-random
  46. fi
  47. }
  48. cleanup() {
  49. echo "### removing temp dir..."
  50. rm -rf "$TEMPDIR"
  51. wait
  52. }
  53. ## setup trap
  54. trap failed_cleanup EXIT
  55. ### SETUP VARIABLES
  56. ## set up some variables to ensure that we're operating strictly in
  57. ## the tests, not system-wide:
  58. export TESTDIR=$(pwd)
  59. # make temp dir
  60. TEMPDIR="$TESTDIR"/tmp
  61. if [ -e "$TEMPDIR" ] ; then
  62. echo "tempdir '$TEMPDIR' already exists."
  63. exit 1
  64. fi
  65. mkdir "$TEMPDIR"
  66. # Use the local copy of executables first, instead of system ones.
  67. # This should help us test without installing.
  68. export PATH="$TESTDIR"/../src:"$TESTDIR"/../src/keytrans:"$PATH"
  69. export MONKEYSPHERE_SYSDATADIR="$TEMPDIR"
  70. export MONKEYSPHERE_SYSCONFIGDIR="$TEMPDIR"
  71. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
  72. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  73. export MONKEYSPHERE_CHECK_KEYSERVER=false
  74. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  75. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  76. export SOCKET="$TEMPDIR"/ssh-socket
  77. export SSHD_PID=
  78. ### CONFIGURE ENVIRONMENTS
  79. # copy in admin and testuser home to tmp
  80. echo "### copying admin and testuser homes..."
  81. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  82. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  83. # set up environment for testuser
  84. TESTHOME="$TEMPDIR"/testuser
  85. export GNUPGHOME="$TESTHOME"/.gnupg
  86. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  87. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  88. cat <<EOF >> "$TESTHOME"/.ssh/config
  89. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  90. IdentityFile $TESTHOME/.ssh/no-such-identity
  91. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  92. EOF
  93. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  94. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  95. EOF
  96. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  97. # set up sshd
  98. echo "### configuring sshd..."
  99. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  100. # write the sshd_config
  101. cat <<EOF >> "$SSHD_CONFIG"
  102. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  103. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  104. EOF
  105. # set up monkeysphere-server
  106. echo "### configuring monkeysphere..."
  107. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
  108. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
  109. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
  110. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
  111. cp etc/monkeysphere/monkeysphere-server.conf "$TEMPDIR"/monkeysphere-server.conf
  112. cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
  113. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  114. EOF
  115. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  116. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
  117. keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
  118. EOF
  119. ### SERVER TESTS
  120. # create a new host key
  121. echo "### generating server key..."
  122. # add gpg.conf with quick-random
  123. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  124. echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
  125. # remove the gpg.conf
  126. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  127. HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
  128. # certify it with the "Admin's Key".
  129. # (this would normally be done via keyservers)
  130. echo "### certifying server key..."
  131. monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  132. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  133. # FIXME: how can we test publish-key without flooding junk into the
  134. # keyservers?
  135. # add admin as identity certifier for testhost
  136. echo "### adding admin as certifier..."
  137. echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  138. ### TESTUSER TESTS
  139. # generate an auth subkey for the test user that expires in 2 days
  140. echo "### generating key for testuser..."
  141. monkeysphere gen-subkey --expire 2
  142. # add server key to testuser keychain
  143. echo "### export server key to testuser..."
  144. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  145. # teach the "server" about the testuser's key
  146. echo "### export testuser key to server..."
  147. gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
  148. echo "### update server authorized_keys file for this testuser..."
  149. monkeysphere-server update-users $(whoami)
  150. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  151. # the identity before connection. This should work in both directions!
  152. echo "### ssh connection test for success..."
  153. ssh_test
  154. # remove the testuser's authorized_user_ids file and update
  155. # authorized_keys file, this is to make sure that the ssh
  156. # authentication FAILS...
  157. echo "### removing testuser authorized_user_ids and reupdating authorized_keys..."
  158. rm -f "$TESTHOME"/.monkeysphere/authorized_user_ids
  159. monkeysphere-server update-users $(whoami)
  160. # make sure the user can NOT connect
  161. echo "### ssh connection test for server authentication denial..."
  162. ssh_test || ret="$?"
  163. if [ "$ret" != '255' ] ; then
  164. echo "### connection should have failed!"
  165. exit "$ret"
  166. fi
  167. trap - EXIT
  168. echo
  169. echo "Monkeysphere basic tests completed successfully!"
  170. echo
  171. cleanup