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