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