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