summaryrefslogtreecommitdiff
path: root/src/share/ma/add_certifier
blob: a48db6481909a070d7e73c9defc9bbe25538c63b (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" \
  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:' | grep "$keyID" | cut -d: -f10)
  110. log info "key found:"
  111. gpg_sphere "--fingerprint 0x${fingerprint}!"
  112. if [ "$PROMPT" = "true" ] ; then
  113. echo "Are you sure you want to add the above key as a certifier"
  114. read -p "of users on this system? (Y/n) " OK; OK=${OK:-Y}
  115. if [ "${OK/y/Y}" != 'Y' ] ; then
  116. failure "Identity certifier not added."
  117. fi
  118. else
  119. log debug "adding key without prompting."
  120. fi
  121. fi
  122. # export the key to the core keyring so that the core can sign the
  123. # new certifier key
  124. log debug "loading key into core keyring..."
  125. gpg_sphere "--export 0x${fingerprint}!" | gpg_core --import
  126. # edit-key script to ltsign key
  127. # NOTE: *all* user IDs will be ltsigned
  128. ltsignCommand=$(cat <<EOF
  129. ltsign
  130. y
  131. $trustval
  132. $depth
  133. $domain
  134. y
  135. save
  136. EOF
  137. )
  138. # core ltsigns the newly imported certifier key
  139. log debug "executing core ltsign script..."
  140. if echo "$ltsignCommand" | \
  141. gpg_core --command-fd 0 --edit-key "0x${fingerprint}!" ; then
  142. # transfer the new sigs back to the sphere keyring
  143. gpg_core_sphere_sig_transfer
  144. # update the sphere trustdb
  145. log debug "updating sphere trustdb..."
  146. gpg_sphere "--check-trustdb" 2>&1 | log debug
  147. log info "Identity certifier added."
  148. else
  149. failure "Problem adding identify certifier."
  150. fi
  151. }