summaryrefslogtreecommitdiff
path: root/src/share/ma/add_certifier
blob: bd3819006ef27b72a6e4b74417877ef176cc728c (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 fingerprint
  32. local ltsignCommand
  33. local trustval
  34. # get options
  35. while true ; do
  36. case "$1" in
  37. -n|--domain)
  38. domain="$2"
  39. shift 2
  40. ;;
  41. -t|--trust)
  42. trust="$2"
  43. shift 2
  44. ;;
  45. -d|--depth)
  46. depth="$2"
  47. shift 2
  48. ;;
  49. -)
  50. break
  51. ;;
  52. *)
  53. if [ "$(echo "$1" | cut -c 1)" = '-' ] ; then
  54. failure "Unknown option '$1'.
  55. Type '$PGRM help' for usage."
  56. fi
  57. break
  58. ;;
  59. esac
  60. done
  61. keyID="$1"
  62. # check that key ID or file is specified
  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. # check the trust value
  67. case "$trust" in
  68. 'marginal')
  69. trustval=1
  70. ;;
  71. 'full')
  72. trustval=2
  73. ;;
  74. *)
  75. failure "Trust value requested ('$trust') was unclear (only 'marginal' or 'full' are supported)."
  76. ;;
  77. esac
  78. # if file is specified
  79. if [ -f "$keyID" -o "$keyID" = '-' ] ; then
  80. # load the key from stdin
  81. if [ "$keyID" = '-' ] ; then
  82. # make a temporary file to hold the key from stdin
  83. keyID=$(msmktempfile)
  84. trap "rm -f $keyID" EXIT
  85. log verbose "reading key from stdin..."
  86. cat > "$keyID"
  87. # load the key from the file
  88. elif [ -f "$keyID" ] ; then
  89. log verbose "reading key from file '$keyID'..."
  90. fi
  91. # check the key is ok as monkeysphere user before loading
  92. log debug "checking keys in file..."
  93. fingerprint=$(su_monkeysphere_user \
  94. ". ${SYSSHAREDIR}/common; list_primary_fingerprints" < "$keyID")
  95. if [ $(printf "%s" "$fingerprint" | egrep -c '^[A-F0-9]{40}$') -ne 1 ] ; then
  96. failure "There was not exactly one gpg key in the file."
  97. fi
  98. # load the key
  99. gpg_sphere "--import" <"$keyID" 2>/dev/null \
  100. || failure "could not read key from '$keyID'"
  101. # else, get the key from the keyserver
  102. else
  103. log verbose "searching keyserver $KEYSERVER for keyID $keyID..."
  104. gpg_sphere "--keyserver $KEYSERVER --recv-key '0x${keyID}!'" \
  105. || failure "Could not receive a key with this ID from the '$KEYSERVER' keyserver."
  106. # get the full fingerprint of new certifier key
  107. log debug "getting fingerprint of certifier key..."
  108. fingerprint=$(gpg_sphere "--list-key --with-colons --with-fingerprint 0x${keyID}!" \
  109. | grep '^fpr:' | cut -d: -f10)
  110. # test that there is only a single fingerprint
  111. if (( $(echo "$fingerprint" | wc -l) != 1 )) ; then
  112. cat <<EOF
  113. More than one fingerprint found:
  114. $fingerprint
  115. Please use a more specific key ID.
  116. EOF
  117. failure
  118. fi
  119. log info "key found:"
  120. gpg_sphere "--fingerprint 0x${fingerprint}!"
  121. if [ "$PROMPT" != "false" ] ; then
  122. printf "Are you sure you want to add the above key as a certifier\nof users on this system? (Y/n) " >&2
  123. read OK; OK=${OK:-Y}
  124. if [ "${OK/y/Y}" != 'Y' ] ; then
  125. failure "Identity certifier not added."
  126. fi
  127. else
  128. log debug "adding key without prompting."
  129. fi
  130. fi
  131. # export the key to the core keyring so that the core can sign the
  132. # new certifier key
  133. log debug "loading key into core keyring..."
  134. gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
  135. # edit-key script to ltsign key
  136. # NOTE: *all* user IDs will be ltsigned
  137. ltsignCommand="ltsign
  138. y
  139. $trustval
  140. $depth
  141. $domain
  142. y
  143. save"
  144. # end script
  145. # core ltsigns the newly imported certifier key
  146. log debug "executing core ltsign script..."
  147. if echo "$ltsignCommand" | \
  148. gpg_core --command-fd 0 --edit-key "0x${fingerprint}!" ; then
  149. # transfer the new sigs back to the sphere keyring
  150. gpg_core_sphere_sig_transfer
  151. # update the sphere trustdb
  152. log debug "updating sphere trustdb..."
  153. gpg_sphere "--check-trustdb" 2>&1 | log debug
  154. log info "Identity certifier added."
  155. else
  156. failure "Problem adding identify certifier."
  157. fi
  158. }