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