summaryrefslogtreecommitdiff
path: root/tests/basic
blob: d79b64b1eb66563033f2caf8ddab74617df66bef (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_SERVER_CONFIG="$TEMPDIR"/monkeysphere-server.conf
  72. export MONKEYSPHERE_SYSSHAREDIR="$TESTDIR"/../src
  73. export MONKEYSPHERE_MONKEYSPHERE_USER=$(whoami)
  74. export MONKEYSPHERE_CHECK_KEYSERVER=false
  75. export MONKEYSPHERE_LOG_LEVEL=DEBUG
  76. export SSHD_CONFIG="$TEMPDIR"/sshd_config
  77. export SOCKET="$TEMPDIR"/ssh-socket
  78. export SSHD_PID=
  79. ### CONFIGURE ENVIRONMENTS
  80. # copy in admin and testuser home to tmp
  81. echo "### copying admin and testuser homes..."
  82. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  83. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  84. # set up environment for testuser
  85. TESTHOME="$TEMPDIR"/testuser
  86. export GNUPGHOME="$TESTHOME"/.gnupg
  87. export SSH_ASKPASS="$TESTHOME"/.ssh/askpass
  88. export MONKEYSPHERE_HOME="$TESTHOME"/.monkeysphere
  89. cat <<EOF >> "$TESTHOME"/.ssh/config
  90. UserKnownHostsFile $TESTHOME/.ssh/known_hosts
  91. IdentityFile $TESTHOME/.ssh/no-such-identity
  92. ProxyCommand $TESTHOME/.ssh/proxy-command %h %p $SOCKET
  93. EOF
  94. cat <<EOF >> "$MONKEYSPHERE_HOME"/monkeysphere.conf
  95. KNOWN_HOSTS=$TESTHOME/.ssh/known_hosts
  96. EOF
  97. get_gpg_prng_arg >> "$GNUPGHOME"/gpg.conf
  98. # set up sshd
  99. echo "### configuring sshd..."
  100. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  101. # write the sshd_config
  102. cat <<EOF >> "$SSHD_CONFIG"
  103. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  104. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  105. EOF
  106. # set up monkeysphere-server
  107. echo "### configuring monkeysphere..."
  108. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
  109. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
  110. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
  111. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/tmp
  112. cp etc/monkeysphere/monkeysphere-server.conf "$MONKEYSPHERE_SERVER_CONFIG"
  113. cat <<EOF >> "$MONKEYSPHERE_SERVER_CONFIG"
  114. AUTHORIZED_USER_IDS="$MONKEYSPHERE_HOME/authorized_user_ids"
  115. EOF
  116. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  117. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
  118. keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
  119. EOF
  120. ### SERVER TESTS
  121. # create a new host key
  122. echo "### generating server key..."
  123. # add gpg.conf with quick-random
  124. get_gpg_prng_arg >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  125. echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
  126. # remove the gpg.conf
  127. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  128. HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
  129. # certify it with the "Admin's Key".
  130. # (this would normally be done via keyservers)
  131. echo "### certifying server key..."
  132. monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  133. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  134. # FIXME: how can we test publish-key without flooding junk into the
  135. # keyservers?
  136. # add admin as identity certifier for testhost
  137. echo "### adding admin as certifier..."
  138. echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  139. ### TESTUSER TESTS
  140. # generate an auth subkey for the test user that expires in 2 days
  141. echo "### generating key for testuser..."
  142. monkeysphere gen-subkey --expire 2
  143. # add server key to testuser keychain
  144. echo "### export server key to testuser..."
  145. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  146. # teach the "server" about the testuser's key
  147. echo "### export testuser key to server..."
  148. gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
  149. echo "### update server authorized_keys file for this testuser..."
  150. monkeysphere-server update-users $(whoami)
  151. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  152. # the identity before connection. This should work in both directions!
  153. echo "### ssh connection test for success..."
  154. ssh_test
  155. # remove the testuser's authorized_user_ids file and update
  156. # authorized_keys file, this is to make sure that the ssh
  157. # authentication FAILS...
  158. echo "### removing testuser authorized_user_ids and reupdating authorized_keys..."
  159. rm -f "$TESTHOME"/.monkeysphere/authorized_user_ids
  160. monkeysphere-server update-users $(whoami)
  161. # make sure the user can NOT connect
  162. echo "### ssh connection test for server authentication denial..."
  163. ssh_test || ret="$?"
  164. if [ "$ret" != '255' ] ; then
  165. echo "### connection should have failed!"
  166. exit "$ret"
  167. fi
  168. trap - EXIT
  169. echo
  170. echo "Monkeysphere basic tests completed successfully!"
  171. echo
  172. cleanup