summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/import-key
blob: d8ab9df51b8c726c5da6b54ad487292f906300c7 (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. import_key() {
  12. local hostName=$(hostname -f)
  13. local keyFile="/etc/ssh/ssh_host_rsa_key"
  14. local keyExpire
  15. local userID
  16. # check for presense of secret key
  17. # FIXME: is this the proper test to be doing here?
  18. fingerprint_server_key >/dev/null \
  19. && failure "An OpenPGP host key already exists."
  20. # get options
  21. while true ; do
  22. case "$1" in
  23. -h|--hostname)
  24. hostName="$2"
  25. shift 2
  26. ;;
  27. -f|--keyfile)
  28. keyFile="$2"
  29. shift 2
  30. ;;
  31. -e|--expire)
  32. keyExpire="$2"
  33. shift 2
  34. ;;
  35. *)
  36. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  37. failure "Unknown option '$1'.
  38. Type '$PGRM help' for usage."
  39. fi
  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. }