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