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