summaryrefslogtreecommitdiff
path: root/src/share/mh/import_key
blob: fea3d277094479b95b2c570a70cd85aa59d44175 (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-2010 and are all released under the GPL,
  11. # version 3 or later.
  12. import_key() {
  13. local keyFile="$1"
  14. local serviceName="$2"
  15. # check that key file specified
  16. if [ -z "$keyFile" ] ; then
  17. failure "Must specify PEM-encoded key file to import, or specify '-' for stdin."
  18. fi
  19. # fail if hostname not specified
  20. if [ -z "$serviceName" ] ; then
  21. failure "You must specify a service name for use in the OpenPGP certificate user ID."
  22. fi
  23. # test that a key with that user ID does not already exist
  24. check_key_userid "$serviceName" "$serviceName" && \
  25. failure "A key with service name '$serviceName' already exists."
  26. # check that the service name is well formatted
  27. check_service_name "$serviceName"
  28. # create host home
  29. mkdir -p "${MHDATADIR}"
  30. mkdir -p "${GNUPGHOME_HOST}"
  31. chmod 700 "${GNUPGHOME_HOST}"
  32. # import pem-encoded key to an OpenPGP private key
  33. if [ "$keyFile" = '-' ] ; then
  34. log verbose "importing key from stdin..."
  35. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
  36. | gpg_host --import
  37. else
  38. log verbose "importing key from file '$keyFile'..."
  39. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
  40. <"$keyFile" \
  41. | gpg_host --import
  42. fi
  43. # export to gpg public key to file
  44. update_gpg_pub_file
  45. log info "host key imported:"
  46. # show info about new key
  47. show_key "$serviceName"
  48. }