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