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