summaryrefslogtreecommitdiff
path: root/src/share/ma/add_certifier
blob: 60a4f9d408d07b528020007a71a81a5f77d36944 (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. # This function adds a certifier whose signatures will be used to
  6. # calculate validity of keys used to connect to user accounts on the
  7. # server. The specified certifier key is first retrieved from the Web
  8. # of Trust with the monkeysphere-user-controlled gpg_sphere keyring.
  9. # Once then new key is retrieved, it is imported into the core
  10. # keyring. The gpg_core then ltsigns the key with the desired trust
  11. # level, and then the key is exported back to the gpg_sphere keyring.
  12. # The gpg_sphere has ultimate owner trust of the core key, so the core
  13. # ltsigs on the new certifier key can then be used by gpg_sphere
  14. # calculate validity for keys inserted in the authorized_keys file.
  15. #
  16. # This is all to keep the monkeysphere user that connects to the
  17. # keyservers from accessing the core secret key.
  18. #
  19. # The monkeysphere scripts are written by:
  20. # Jameson Rollins <jrollins@finestructure.net>
  21. # Jamie McClelland <jm@mayfirst.org>
  22. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  23. #
  24. # They are Copyright 2008-2009, and are all released under the GPL,
  25. # version 3 or later.
  26. add_certifier() {
  27. local domain
  28. local trust
  29. local depth
  30. local keyID
  31. local fingerprint
  32. local ltsignCommand
  33. local trustval
  34. # set default values for trust depth and domain
  35. domain=
  36. trust=full
  37. depth=1
  38. # get options
  39. while true ; do
  40. case "$1" in
  41. -n|--domain)
  42. domain="$2"
  43. shift 2
  44. ;;
  45. -t|--trust)
  46. trust="$2"
  47. shift 2
  48. ;;
  49. -d|--depth)
  50. depth="$2"
  51. shift 2
  52. ;;
  53. *)
  54. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  55. failure "Unknown option '$1'.
  56. Type '$PGRM help' for usage."
  57. fi
  58. break
  59. ;;
  60. esac
  61. done
  62. keyID="$1"
  63. if [ -z "$keyID" ] ; then
  64. failure "You must specify the key ID of a key to add, or specify a file to read the key from."
  65. fi
  66. if [ -f "$keyID" ] ; then
  67. log info "Reading key from file '$keyID':"
  68. importinfo=$(gpg_sphere "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
  69. # FIXME: if this is tried when the key database is not
  70. # up-to-date, i got these errors (using set -x):
  71. # ++ su -m monkeysphere -c '\''gpg --import'\''
  72. # Warning: using insecure memory!
  73. # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
  74. # gpg: Total number processed: 1
  75. # gpg: imported: 1 (RSA: 1)
  76. # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
  77. # gpg: failed to rebuild keyring cache: Permission denied
  78. # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
  79. # gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
  80. # gpg: next trustdb check due at 2009-01-10'
  81. # + failure 'could not read key from '\''/root/dkg.gpg'\'''
  82. # + echo 'could not read key from '\''/root/dkg.gpg'\'''
  83. keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
  84. if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
  85. failure "Expected there to be a single gpg key in the file."
  86. fi
  87. else
  88. # get the key from the key server
  89. gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  90. fi
  91. export keyID
  92. # get the full fingerprint of a key ID
  93. fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" | \
  94. grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  95. if [ -z "$fingerprint" ] ; then
  96. failure "Key '$keyID' not found."
  97. fi
  98. log info -e "\nkey found:"
  99. gpg_sphere "--fingerprint 0x${fingerprint}!"
  100. echo "Are you sure you want to add the above key as a"
  101. read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
  102. if [ "${OK/y/Y}" != 'Y' ] ; then
  103. failure "Identity certifier not added."
  104. fi
  105. # export the key to the core keyring so that the core can sign the
  106. # new certifier key
  107. gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
  108. case "$trust" in
  109. 'marginal')
  110. trustval=1
  111. ;;
  112. 'full')
  113. trustval=2
  114. ;;
  115. *)
  116. failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
  117. ;;
  118. esac
  119. # this is the gpg "script" that gpg --edit-key will execute for the
  120. # core to sign certifier.
  121. # NOTE: *all* user IDs will be ltsigned
  122. ltsignCommand=$(cat <<EOF
  123. ltsign
  124. y
  125. $trustval
  126. $depth
  127. $domain
  128. y
  129. save
  130. EOF
  131. )
  132. # core ltsigns the newly imported certifier key
  133. if echo "$ltsignCommand" | \
  134. gpg_core --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
  135. # transfer the new sigs back to the sphere keyring
  136. gpg_core_sphere_sig_transfer
  137. # update the sphere trustdb
  138. gpg_sphere "--check-trustdb"
  139. log info -e "\nIdentity certifier added."
  140. else
  141. failure "Problem adding identify certifier."
  142. fi
  143. }