summaryrefslogtreecommitdiff
path: root/src/subcommands/mh/import-key
blob: 9ba51d24dfb26f99c1d9b600784ba5311ba56ab9 (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. -f|--keyfile)
  24. keyFile="$2"
  25. shift 2
  26. ;;
  27. -e|--expire)
  28. keyExpire="$2"
  29. shift 2
  30. ;;
  31. *)
  32. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  33. failure "Unknown option '$1'.
  34. Type '$PGRM help' for usage."
  35. fi
  36. hostName="$1"
  37. shift
  38. ;;
  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
  72. }