summaryrefslogtreecommitdiff
path: root/src/share/m/import_subkey
blob: f3ca957092bc7001e538f8ba02ff1714364463d3 (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere import-subkey 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 an existing ssh key as a gpg subkey
  13. import_subkey() {
  14. local sshKeyFile
  15. local keyID
  16. local gpgSecOut
  17. local fifoDir
  18. sshKeyFile="$1"
  19. shift
  20. # check that key file specified
  21. if [ -z "$sshKeyFile" ] ; then
  22. failure "Must specify ssh key file to import, or specify '-' for stdin."
  23. fi
  24. # check that the keyID is unique
  25. keyID=$(check_gpg_sec_key_id "$@")
  26. # check that an authentication subkey does not already exist
  27. check_gpg_authentication_subkey "$keyID"
  28. # setup the temp fifo dir for retrieving the key password
  29. log debug "creating password fifo..."
  30. fifoDir=$(msmktempdir)
  31. trap "rm -rf $fifoDir" EXIT
  32. (umask 077 && mkfifo "$fifoDir/pass")
  33. # import ssh key to as authentication subkey
  34. if [ "$sshKeyFile" = '-' ] ; then
  35. log verbose "importing ssh key from stdin..."
  36. ssh2openpgp \
  37. | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
  38. else
  39. log verbose "importing ssh key from file '$sshKeyFile'..."
  40. ssh2openpgp <"$sshKeyFile" \
  41. | gpg_user --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --import &
  42. fi
  43. # get the password if needed
  44. passphrase_prompt "Please enter your passphrase for $keyID: " "$fifoDir/pass"
  45. trap - EXIT
  46. rm -rf "$fifoDir"
  47. wait
  48. log verbose "done."
  49. }