summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 9f4d02ecc638063746c7dba41e06d40a48a9fa40 (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. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
  71. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  72. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
  73. keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
  74. EOF
  75. # create a new host key
  76. echo "### generating server key..."
  77. # add gpg.conf with quick-random
  78. echo "quick-random" >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  79. echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
  80. # remove the gpg.conf
  81. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  82. HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
  83. # certify it with the "Admin's Key".
  84. # (this would normally be done via keyservers)
  85. echo "### certifying server key..."
  86. monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  87. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  88. # FIXME: how can we test publish-key without flooding junk into the
  89. # keyservers?
  90. # add admin as identity certifier for testhost
  91. echo "### adding admin as certifier..."
  92. echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  93. # initialize base sshd_config
  94. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  95. # write the sshd_config
  96. cat <<EOF >> "$SSHD_CONFIG"
  97. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  98. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  99. EOF
  100. # launch test sshd with the new host key.
  101. echo "### starting sshd..."
  102. socat EXEC:"/usr/sbin/sshd -f ${SSHD_CONFIG} -i -D -e" "UNIX-LISTEN:${SOCKET}" 2> "$TEMPDIR"/sshd.log &
  103. export SSHD_PID=$!
  104. ### TESTUSER TESTS
  105. # generate an auth subkey for the test user
  106. echo "### generating key for testuser..."
  107. export GNUPGHOME="$TEMPDIR"/testuser/.gnupg
  108. export SSH_ASKPASS="$TEMPDIR"/testuser/.ssh/askpass
  109. export MONKEYSPHERE_HOME="$TEMPDIR"/testuser/.monkeysphere
  110. monkeysphere gen-subkey --expire 0
  111. # add server key to testuser keychain
  112. echo "### export server key to testuser..."
  113. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  114. # teach the "server" about the testuser's key
  115. echo "### export testuser key to server..."
  116. gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
  117. echo "### update server authorized_keys file for testuser..."
  118. monkeysphere-server update-users testuser
  119. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  120. # the identity before connection. This should work in both directions!
  121. echo "### testuser connecting to sshd socket..."
  122. ssh-agent bash -c \
  123. "monkeysphere subkey-to-ssh-agent && ssh -F $TEMPDIR/testuser/.ssh/config testhost"
  124. trap - EXIT
  125. cleanup