summaryrefslogtreecommitdiff
path: root/src/share/mh/import_key
blob: 0f362b8ce8aff7f1c95d8b56da9b9b3e963ce6ee (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. prompt_userid_exists "$serviceName"
  25. # check that the service name is well formatted
  26. check_service_name "$serviceName"
  27. # create host home
  28. mkdir -p "${MHDATADIR}"
  29. mkdir -p "${GNUPGHOME_HOST}"
  30. chmod 700 "${GNUPGHOME_HOST}"
  31. # import pem-encoded key to an OpenPGP private key
  32. if [ "$keyFile" = '-' ] ; then
  33. log verbose "importing key from stdin..."
  34. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
  35. | gpg_host --import
  36. else
  37. log verbose "importing key from file '$keyFile'..."
  38. PEM2OPENPGP_USAGE_FLAGS=authenticate pem2openpgp "$serviceName" \
  39. <"$keyFile" \
  40. | gpg_host --import
  41. fi
  42. # export to OpenPGP public key to file
  43. update_pgp_pub_file
  44. log info "host key imported:"
  45. # show info about new key
  46. show_key "$serviceName"
  47. }