summaryrefslogtreecommitdiff
path: root/src/subcommands/ma/add_certifier
blob: 0c3c647663db387a98ed70e88574f7b8e9ca4cdc (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere authentication add-certifier 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. # retrieve key from web of trust, import it into the host keyring, and
  13. # ltsign the key in the host keyring so that it may certify other keys
  14. add_certifier() {
  15. local domain
  16. local trust
  17. local depth
  18. local keyID
  19. local fingerprint
  20. local ltsignCommand
  21. local trustval
  22. # set default values for trust depth and domain
  23. domain=
  24. trust=full
  25. depth=1
  26. # get options
  27. while true ; do
  28. case "$1" in
  29. -n|--domain)
  30. domain="$2"
  31. shift 2
  32. ;;
  33. -t|--trust)
  34. trust="$2"
  35. shift 2
  36. ;;
  37. -d|--depth)
  38. depth="$2"
  39. shift 2
  40. ;;
  41. *)
  42. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  43. failure "Unknown option '$1'.
  44. Type '$PGRM help' for usage."
  45. fi
  46. break
  47. ;;
  48. esac
  49. done
  50. keyID="$1"
  51. if [ -z "$keyID" ] ; then
  52. failure "You must specify the key ID of a key to add, or specify a file to read the key from."
  53. fi
  54. if [ -f "$keyID" ] ; then
  55. echo "Reading key from file '$keyID':"
  56. importinfo=$(gpg_sphere "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
  57. # FIXME: if this is tried when the key database is not
  58. # up-to-date, i got these errors (using set -x):
  59. # ++ su -m monkeysphere -c '\''gpg --import'\''
  60. # Warning: using insecure memory!
  61. # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
  62. # gpg: Total number processed: 1
  63. # gpg: imported: 1 (RSA: 1)
  64. # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
  65. # gpg: failed to rebuild keyring cache: Permission denied
  66. # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
  67. # gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
  68. # gpg: next trustdb check due at 2009-01-10'
  69. # + failure 'could not read key from '\''/root/dkg.gpg'\'''
  70. # + echo 'could not read key from '\''/root/dkg.gpg'\'''
  71. keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
  72. if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
  73. failure "Expected there to be a single gpg key in the file."
  74. fi
  75. else
  76. # get the key from the key server
  77. gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  78. fi
  79. export keyID
  80. # get the full fingerprint of a key ID
  81. fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" | \
  82. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  83. if [ -z "$fingerprint" ] ; then
  84. failure "Key '$keyID' not found."
  85. fi
  86. echo
  87. echo "key found:"
  88. gpg_sphere "--fingerprint 0x${fingerprint}!"
  89. echo "Are you sure you want to add the above key as a"
  90. read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
  91. if [ "${OK/y/Y}" != 'Y' ] ; then
  92. failure "Identity certifier not added."
  93. fi
  94. # export the key to the host keyring
  95. gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
  96. if [ "$trust" = marginal ]; then
  97. trustval=1
  98. elif [ "$trust" = full ]; then
  99. trustval=2
  100. else
  101. failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
  102. fi
  103. # ltsign command
  104. # NOTE: *all* user IDs will be ltsigned
  105. ltsignCommand=$(cat <<EOF
  106. ltsign
  107. y
  108. $trustval
  109. $depth
  110. $domain
  111. y
  112. save
  113. EOF
  114. )
  115. # ltsign the key
  116. if echo "$ltsignCommand" | \
  117. gpg_core --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
  118. # update the trustdb for the authentication keyring
  119. gpg_sphere "--check-trustdb"
  120. echo
  121. echo "Identity certifier added."
  122. else
  123. failure "Problem adding identify certifier."
  124. fi
  125. }