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