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