You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

217 lines
6.5 KiB

  1. #!/bin/sh
  2. set -e
  3. unset GREP_OPTIONS
  4. # We don't use a secret keyring, of course, but gpg panics and
  5. # implodes if there isn't one available
  6. GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
  7. GPG="$GPG_CMD"
  8. MASTER_KEYRING=""
  9. ARCHIVE_KEYRING_URI=""
  10. #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
  11. #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
  12. ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
  13. REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
  14. add_keys_with_verify_against_master_keyring() {
  15. ADD_KEYRING=$1
  16. MASTER=$2
  17. if [ ! -f "$ADD_KEYRING" ]; then
  18. echo "ERROR: '$ADD_KEYRING' not found"
  19. return
  20. fi
  21. if [ ! -f "$MASTER" ]; then
  22. echo "ERROR: '$MASTER' not found"
  23. return
  24. fi
  25. # when adding new keys, make sure that the archive-master-keyring
  26. # is honored. so:
  27. # all keys that are exported must have a valid signature
  28. # from a key in the $distro-master-keyring
  29. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  30. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  31. for add_key in $add_keys; do
  32. ADDED=0
  33. for master_key in $master_keys; do
  34. if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
  35. $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
  36. ADDED=1
  37. fi
  38. done
  39. if [ $ADDED = 0 ]; then
  40. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  41. fi
  42. done
  43. }
  44. # update the current archive signing keyring from a network URI
  45. # the archive-keyring keys needs to be signed with the master key
  46. # (otherwise it does not make sense from a security POV)
  47. net_update() {
  48. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  49. echo "ERROR: no location for the archive-keyring given"
  50. exit 1
  51. fi
  52. # in theory we would need to depend on wget for this, but this feature
  53. # isn't useable in debian anyway as we have no keyring uri nor a master key
  54. if ! which wget >/dev/null 2>&1; then
  55. echo "ERROR: an installed wget is required for a network-based update"
  56. exit 1
  57. fi
  58. if [ ! -d /var/lib/apt/keyrings ]; then
  59. mkdir -p /var/lib/apt/keyrings
  60. fi
  61. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  62. old_mtime=0
  63. if [ -e $keyring ]; then
  64. old_mtime=$(stat -c %Y $keyring)
  65. fi
  66. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  67. if [ ! -e $keyring ]; then
  68. return
  69. fi
  70. new_mtime=$(stat -c %Y $keyring)
  71. if [ $new_mtime -ne $old_mtime ]; then
  72. echo "Checking for new archive signing keys now"
  73. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  74. fi
  75. }
  76. update() {
  77. if [ ! -f $ARCHIVE_KEYRING ]; then
  78. echo >&2 "ERROR: Can't find the archive-keyring"
  79. echo >&2 "Is the debian-archive-keyring package installed?"
  80. exit 1
  81. fi
  82. # add new keys from the package;
  83. # we do not use add_keys_with_verify_against_master_keyring here,
  84. # because "update" is run on regular package updates. A
  85. # attacker might as well replace the master-archive-keyring file
  86. # in the package and add his own keys. so this check wouldn't
  87. # add any security. we *need* this check on net-update though
  88. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  89. if [ -r "$REMOVED_KEYS" ]; then
  90. # remove no-longer supported/used keys
  91. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  92. for key in $keys; do
  93. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  94. $GPG --quiet --batch --delete-key --yes ${key}
  95. fi
  96. done
  97. else
  98. echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
  99. fi
  100. }
  101. usage() {
  102. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  103. echo
  104. echo "Manage apt's list of trusted keys"
  105. echo
  106. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  107. echo " apt-key del <keyid> - remove the key <keyid>"
  108. echo " apt-key export <keyid> - output the key <keyid>"
  109. echo " apt-key exportall - output all trusted keys"
  110. echo " apt-key update - update keys using the keyring package"
  111. echo " apt-key net-update - update keys using the network"
  112. echo " apt-key list - list keys"
  113. echo " apt-key finger - list fingerprints"
  114. echo " apt-key adv - pass advanced options to gpg (download key)"
  115. echo
  116. echo "If no specific keyring file is given the command applies to all keyring files."
  117. }
  118. # Determine on which keyring we want to work
  119. if [ "$1" = "--keyring" ]; then
  120. #echo "keyfile given"
  121. shift
  122. TRUSTEDFILE="$1"
  123. if [ -r "$TRUSTEDFILE" ]; then
  124. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  125. else
  126. echo >&2 "Error: The specified keyring ┬╗$TRUSTEDFILE┬ź is missing or not readable"
  127. exit 1
  128. fi
  129. shift
  130. # otherwise use the default
  131. else
  132. #echo "generate list"
  133. TRUSTEDFILE="/etc/apt/trusted.gpg"
  134. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  135. if [ -r "$TRUSTEDFILE" ]; then
  136. GPG="$GPG --keyring $TRUSTEDFILE"
  137. fi
  138. GPG="$GPG --primary-keyring $TRUSTEDFILE"
  139. TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  140. if [ -d "$TRUSTEDPARTS" ]; then
  141. #echo "parts active"
  142. for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
  143. #echo "part -> $trusted"
  144. GPG="$GPG --keyring $trusted"
  145. done
  146. fi
  147. fi
  148. #echo "COMMAND: $GPG"
  149. command="$1"
  150. if [ -z "$command" ]; then
  151. usage
  152. exit 1
  153. fi
  154. shift
  155. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  156. echo >&2 "Warning: gnupg does not seem to be installed."
  157. echo >&2 "Warning: apt-key requires gnupg for most operations."
  158. echo >&2
  159. fi
  160. case "$command" in
  161. add)
  162. $GPG --quiet --batch --import "$1"
  163. echo "OK"
  164. ;;
  165. del|rm|remove)
  166. $GPG --quiet --batch --delete-key --yes "$1"
  167. echo "OK"
  168. ;;
  169. update)
  170. update
  171. ;;
  172. net-update)
  173. net_update
  174. ;;
  175. list)
  176. $GPG --batch --list-keys
  177. ;;
  178. finger*)
  179. $GPG --batch --fingerprint
  180. ;;
  181. export)
  182. $GPG --armor --export "$1"
  183. ;;
  184. exportall)
  185. $GPG --armor --export
  186. ;;
  187. adv*)
  188. echo "Executing: $GPG $*"
  189. $GPG $*
  190. ;;
  191. help)
  192. usage
  193. ;;
  194. *)
  195. usage
  196. exit 1
  197. ;;
  198. esac