summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/import-key
blob: bbeb37fb2dbf8a370ed005b6b73ba61fe9a89752 (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. -f|--keyfile)
  23. keyFile="$2"
  24. shift 2
  25. ;;
  26. -e|--expire)
  27. keyExpire="$2"
  28. shift 2
  29. ;;
  30. *)
  31. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  32. failure "Unknown option '$1'.
  33. Type '$PGRM help' for usage."
  34. fi
  35. hostName="$1"
  36. shift
  37. ;;
  38. break
  39. ;;
  40. esac
  41. done
  42. if [ ! -f "$keyFile" ] ; then
  43. failure "SSH secret key file '$keyFile' not found."
  44. fi
  45. userID="ssh://${hostName}"
  46. # prompt about key expiration if not specified
  47. keyExpire=$(get_gpg_expiration "$keyExpire")
  48. echo "The following key parameters will be used for the host private key:"
  49. echo "Import: $keyFile"
  50. echo "Name-Real: $userID"
  51. echo "Expire-Date: $keyExpire"
  52. read -p "Import key? (Y/n) " OK; OK=${OK:=Y}
  53. if [ ${OK/y/Y} != 'Y' ] ; then
  54. failure "aborting."
  55. fi
  56. log verbose "importing ssh key..."
  57. # translate ssh key to a private key
  58. (umask 077 && \
  59. pem2openpgp "$userID" "$keyExpire" < "$sshKey" | gpg_host --import)
  60. # find the key fingerprint of the newly converted key
  61. fingerprint=$(fingerprint_server_key)
  62. # export host ownertrust to authentication keyring
  63. log verbose "setting ultimate owner trust for host key..."
  64. echo "${fingerprint}:6:" | gpg_host "--import-ownertrust"
  65. echo "${fingerprint}:6:" | gpg_authentication "--import-ownertrust"
  66. # export public key to file
  67. gpg_authentication "--export-options export-minimal --armor --export 0x${fingerprint}\!" > "${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  68. log info "SSH host public key in OpenPGP form: ${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  69. # show info about new key
  70. show_server_key