summaryrefslogtreecommitdiff
path: root/src/share/mh/import_key
blob: 386e02d4556d2f89a2a63a1d4124cfa4aafd3603 (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere host import-key subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2008-2009 and are all released under the GPL,
  11. # version 3 or later.
  12. import_key() {
  13. local hostName=$(hostname -f)
  14. local keyFile="/etc/ssh/ssh_host_rsa_key"
  15. local keyExpire
  16. local userID
  17. # check for presense of secret key
  18. # FIXME: is this the proper test to be doing here?
  19. fingerprint_server_key >/dev/null \
  20. && failure "An OpenPGP host key already exists."
  21. # get options
  22. while true ; do
  23. case "$1" in
  24. -f|--keyfile)
  25. keyFile="$2"
  26. shift 2
  27. ;;
  28. -e|--expire)
  29. keyExpire="$2"
  30. shift 2
  31. ;;
  32. *)
  33. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  34. failure "Unknown option '$1'.
  35. Type '$PGRM help' for usage."
  36. fi
  37. hostName="$1"
  38. shift
  39. ;;
  40. break
  41. ;;
  42. esac
  43. done
  44. if [ ! -f "$keyFile" ] ; then
  45. failure "SSH secret key file '$keyFile' not found."
  46. fi
  47. userID="ssh://${hostName}"
  48. # prompt about key expiration if not specified
  49. keyExpire=$(get_gpg_expiration "$keyExpire")
  50. echo "The following key parameters will be used for the host private key:"
  51. echo "Import: $keyFile"
  52. echo "Name-Real: $userID"
  53. echo "Expire-Date: $keyExpire"
  54. read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
  55. if [ ${OK/y/Y} != 'Y' ] ; then
  56. failure "aborting."
  57. fi
  58. log verbose "importing ssh key..."
  59. # translate ssh key to a private key
  60. (umask 077 && \
  61. pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
  62. # find the key fingerprint of the newly converted key
  63. fingerprint=$(fingerprint_server_key)
  64. # export host ownertrust to authentication keyring
  65. log verbose "setting ultimate owner trust for host key..."
  66. echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
  67. echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
  68. # export public key to file
  69. gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  70. log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  71. # show info about new key
  72. show_key
  73. }