summaryrefslogtreecommitdiff
path: root/src/share/mh/import_key
blob: 040b41c57140862760b9e10dcac2189bdcb44f65 (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 sshKeyFile
  14. local hostName
  15. local domain
  16. local userID
  17. sshKeyFile="$1"
  18. hostName="$2"
  19. # check that key file specified
  20. if [ -z "$sshKeyFile" ] ; then
  21. failure "Must specify ssh key file to import, or specify '-' for stdin."
  22. fi
  23. # use the default hostname if not specified
  24. if [ -z "$hostName" ] ; then
  25. hostName=$(hostname -f) || failure "Could not determine hostname."
  26. # test that the domain is not obviously illegitimate
  27. domain=${foo##*.}
  28. case $domain in
  29. 'local'|'localdomain')
  30. failure "Host domain '$domain' is not legitimate. Aborting key import."
  31. ;;
  32. esac
  33. # test that there are at least two parts
  34. if (( $(echo "$hostName" | tr . ' ' | wc -w) < 2 )) ; then
  35. failure "Host name '$hostName' is not legitimate. Aborting key import."
  36. fi
  37. fi
  38. userID="ssh://${hostName}"
  39. # create host home
  40. mkdir -p "${MHDATADIR}"
  41. mkdir -p "${GNUPGHOME_HOST}"
  42. chmod 700 "${GNUPGHOME_HOST}"
  43. # import ssh key to a private key
  44. if [ "$sshKeyFile" = '-' ] ; then
  45. log verbose "importing ssh key from stdin..."
  46. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
  47. | gpg_host --import
  48. else
  49. log verbose "importing ssh key from file '$sshKeyFile'..."
  50. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$userID" \
  51. <"$sshKeyFile" \
  52. | gpg_host --import
  53. fi
  54. # load the new host fpr into the fpr variable. this is so we can
  55. # create the gpg pub key file. we have to do this from the secret key
  56. # ring since we obviously don't have the gpg pub key file yet, since
  57. # that's what we're trying to produce (see below).
  58. load_fingerprint_secret
  59. # export to gpg public key to file
  60. update_gpg_pub_file
  61. log info "host key imported:"
  62. # show info about new key
  63. show_key
  64. }