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