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