summaryrefslogtreecommitdiff
path: root/src/share/ma/add_certifier
blob: 28c229aec25de30120beca3b6c4ef0ed50f9f72a (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=full
  29. local depth=1
  30. local keyID
  31. local importinfo
  32. local fingerprint
  33. local ltsignCommand
  34. local trustval
  35. PROMPT=true
  36. # get options
  37. while true ; do
  38. case "$1" in
  39. -n|--domain)
  40. domain="$2"
  41. shift 2
  42. ;;
  43. -t|--trust)
  44. trust="$2"
  45. shift 2
  46. ;;
  47. -d|--depth)
  48. depth="$2"
  49. shift 2
  50. ;;
  51. -y)
  52. PROMPT=false
  53. shift 1
  54. ;;
  55. *)
  56. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  57. failure "Unknown option '$1'.
  58. Type '$PGRM help' for usage."
  59. fi
  60. break
  61. ;;
  62. esac
  63. done
  64. keyID="$1"
  65. if [ -z "$keyID" ] ; then
  66. failure "You must specify the key ID of a key to add, or specify a file to read the key from."
  67. fi
  68. if [ -f "$keyID" ] ; then
  69. log info "Reading key from file '$keyID':"
  70. importinfo=$(gpg_sphere "--import" < "$keyID" 2>&1) || failure "could not read key from '$keyID'"
  71. # FIXME: if this is tried when the key database is not
  72. # up-to-date, i got these errors (using set -x):
  73. # ++ su -m monkeysphere -c '\''gpg --import'\''
  74. # Warning: using insecure memory!
  75. # gpg: key D21739E9: public key "Daniel Kahn Gillmor <dkg@fifthhorseman.net>" imported
  76. # gpg: Total number processed: 1
  77. # gpg: imported: 1 (RSA: 1)
  78. # gpg: can'\''t create `/var/monkeysphere/gnupg-host/pubring.gpg.tmp'\'': Permission denied
  79. # gpg: failed to rebuild keyring cache: Permission denied
  80. # gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
  81. # gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u
  82. # gpg: next trustdb check due at 2009-01-10'
  83. # + failure 'could not read key from '\''/root/dkg.gpg'\'''
  84. # + echo 'could not read key from '\''/root/dkg.gpg'\'''
  85. keyID=$(echo "$importinfo" | grep '^gpg: key ' | cut -f2 -d: | cut -f3 -d\ )
  86. if [ -z "$keyID" ] || [ $(echo "$keyID" | wc -l) -ne 1 ] ; then
  87. failure "There was not exactly one gpg key in the file."
  88. fi
  89. else
  90. # get the key from the key server
  91. log debug "retrieving key from keyserver..."
  92. gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  93. fi
  94. export keyID
  95. # get the full fingerprint of new certifier key
  96. log debug "getting fingerprint of certifier key..."
  97. fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
  98. | grep '^fpr:' | grep "$keyID" | cut -d: -f10)
  99. if [ -z "$fingerprint" ] ; then
  100. failure "Key '$keyID' not found."
  101. fi
  102. log info "key found:"
  103. gpg_sphere "--fingerprint 0x${fingerprint}!"
  104. if [ "$PROMPT" = true ] ; then
  105. echo "Are you sure you want to add the above key as a"
  106. read -p "certifier of users on this system? (y/N) " OK; OK=${OK:-N}
  107. if [ "${OK/y/Y}" != 'Y' ] ; then
  108. failure "Identity certifier not added."
  109. fi
  110. else
  111. log debug "adding key without prompting."
  112. fi
  113. # export the key to the core keyring so that the core can sign the
  114. # new certifier key
  115. log debug "exporting retrieved certifier key to core keyring..."
  116. gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
  117. case "$trust" in
  118. 'marginal')
  119. trustval=1
  120. ;;
  121. 'full')
  122. trustval=2
  123. ;;
  124. *)
  125. failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
  126. ;;
  127. esac
  128. # edit-key script to ltsign key
  129. # NOTE: *all* user IDs will be ltsigned
  130. ltsignCommand=$(cat <<EOF
  131. ltsign
  132. y
  133. $trustval
  134. $depth
  135. $domain
  136. y
  137. save
  138. EOF
  139. )
  140. # core ltsigns the newly imported certifier key
  141. log debug "executing core ltsign script..."
  142. if echo "$ltsignCommand" | \
  143. gpg_core --quiet --command-fd 0 --edit-key "0x${fingerprint}!" ; then
  144. # transfer the new sigs back to the sphere keyring
  145. gpg_core_sphere_sig_transfer
  146. # update the sphere trustdb
  147. log debug "updating sphere trustdb..."
  148. gpg_sphere "--check-trustdb"
  149. log info "Identity certifier added."
  150. else
  151. failure "Problem adding identify certifier."
  152. fi
  153. }