summaryrefslogtreecommitdiff
path: root/tests/basic
blob: 3ec4a21657ca39acfbae7de30d38559f2e3806cb (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 ( 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. # copy in admin and testuser home to tmp
  66. echo "### copying admin and testuser homes..."
  67. cp -a "$TESTDIR"/home/admin "$TEMPDIR"/
  68. cp -a "$TESTDIR"/home/testuser "$TEMPDIR"/
  69. cat <<EOF >> "$TEMPDIR"/testuser/.ssh/config
  70. UserKnownHostsFile $TEMPDIR/testuser/.ssh/known_hosts
  71. IdentityFile $TEMPDIR/testuser/.ssh/no-such-identity
  72. ProxyCommand $TEMPDIR/testuser/.ssh/proxy-command %h %p $SOCKET
  73. EOF
  74. cat <<EOF >> "$TEMPDIR"/testuser/.monkeysphere/monkeysphere.conf
  75. KNOWN_HOSTS=$TEMPDIR/testuser/.ssh/known_hosts
  76. EOF
  77. # set up a simple default monkeysphere-server.conf
  78. cat <<EOF >> "$TEMPDIR"/monkeysphere-server.conf
  79. AUTHORIZED_USER_IDS="$TEMPDIR/testuser/.monkeysphere/authorized_user_ids"
  80. EOF
  81. ### SERVER TESTS
  82. # setup monkeysphere temp gnupghome directories
  83. mkdir -p -m 750 "$MONKEYSPHERE_SYSDATADIR"/gnupg-host
  84. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication
  85. mkdir -p -m 700 "$MONKEYSPHERE_SYSDATADIR"/authorized_keys
  86. cat <<EOF > "$MONKEYSPHERE_SYSDATADIR"/gnupg-authentication/gpg.conf
  87. primary-keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-authentication/pubring.gpg
  88. keyring ${MONKEYSPHERE_SYSDATADIR}/gnupg-host/pubring.gpg
  89. EOF
  90. # create a new host key
  91. echo "### generating server key..."
  92. # add gpg.conf with quick-random
  93. echo "quick-random" >> "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  94. echo | monkeysphere-server gen-key --length 1024 --expire 0 testhost
  95. # remove the gpg.conf
  96. rm "$MONKEYSPHERE_SYSCONFIGDIR"/gnupg-host/gpg.conf
  97. HOSTKEYID=$( monkeysphere-server show-key | tail -n1 | cut -f3 -d\ )
  98. # certify it with the "Admin's Key".
  99. # (this would normally be done via keyservers)
  100. echo "### certifying server key..."
  101. monkeysphere-server gpg-authentication-cmd "--armor --export $HOSTKEYID" | gpgadmin --import
  102. echo y | gpgadmin --command-fd 0 --sign-key "$HOSTKEYID"
  103. # FIXME: how can we test publish-key without flooding junk into the
  104. # keyservers?
  105. # add admin as identity certifier for testhost
  106. echo "### adding admin as certifier..."
  107. echo y | monkeysphere-server add-identity-certifier "$TEMPDIR"/admin/.gnupg/pubkey.gpg
  108. # initialize base sshd_config
  109. cp etc/ssh/sshd_config "$SSHD_CONFIG"
  110. # write the sshd_config
  111. cat <<EOF >> "$SSHD_CONFIG"
  112. HostKey ${MONKEYSPHERE_SYSDATADIR}/ssh_host_rsa_key
  113. AuthorizedKeysFile ${MONKEYSPHERE_SYSDATADIR}/authorized_keys/%u
  114. EOF
  115. # launch test sshd with the new host key.
  116. echo "### starting sshd..."
  117. launch_sshd
  118. ### TESTUSER TESTS
  119. # generate an auth subkey for the test user
  120. echo "### generating key for testuser..."
  121. export GNUPGHOME="$TEMPDIR"/testuser/.gnupg
  122. export SSH_ASKPASS="$TEMPDIR"/testuser/.ssh/askpass
  123. export MONKEYSPHERE_HOME="$TEMPDIR"/testuser/.monkeysphere
  124. monkeysphere gen-subkey --expire 0
  125. # add server key to testuser keychain
  126. echo "### export server key to testuser..."
  127. gpgadmin --armor --export "$HOSTKEYID" | gpg --import
  128. # teach the "server" about the testuser's key
  129. echo "### export testuser key to server..."
  130. gpg --export testuser | monkeysphere-server gpg-authentication-cmd --import
  131. echo "### update server authorized_keys file for this testuser..."
  132. monkeysphere-server update-users "$USER"
  133. # connect to test sshd, using monkeysphere-ssh-proxycommand to verify
  134. # the identity before connection. This should work in both directions!
  135. echo "### testuser connecting to sshd socket..."
  136. ssh_test
  137. # kill the previous sshd process if it's still running
  138. kill "$SSHD_PID"
  139. # now remove the testuser's authorized_user_ids file and reupdate
  140. # authorized_keys file...
  141. echo "### removing testuser authorized_user_ids and reupdating authorized_keys..."
  142. rm -f "$TEMPDIR"/testuser/.monkeysphere/authorized_user_ids
  143. monkeysphere-server update-users "$USER"
  144. # restart the sshd
  145. echo "### restarting sshd..."
  146. launch_sshd
  147. # and make sure the user can no longer connect
  148. echo "### testuser attempting to connect to sshd socket..."
  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