apt-key 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. else
  131. echo "generate list"
  132. TRUSTEDFILE="/etc/apt/trusted.gpg"
  133. if [ -r "$TRUSTEDFILE" ]; then
  134. GPG="$GPG --keyring $TRUSTEDFILE"
  135. fi
  136. GPG="$GPG --primary-keyring $TRUSTEDFILE"
  137. TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  138. if [ -d "$TRUSTEDPARTS" ]; then
  139. echo "parts active"
  140. for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
  141. echo "part -> $trusted"
  142. GPG="$GPG --keyring $trusted"
  143. done
  144. fi
  145. fi
  146. echo "COMMAND: $GPG"
  147. command="$1"
  148. if [ -z "$command" ]; then
  149. usage
  150. exit 1
  151. fi
  152. shift
  153. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  154. echo >&2 "Warning: gnupg does not seem to be installed."
  155. echo >&2 "Warning: apt-key requires gnupg for most operations."
  156. echo >&2
  157. fi
  158. case "$command" in
  159. add)
  160. $GPG --quiet --batch --import "$1"
  161. echo "OK"
  162. ;;
  163. del|rm|remove)
  164. $GPG --quiet --batch --delete-key --yes "$1"
  165. echo "OK"
  166. ;;
  167. update)
  168. update
  169. ;;
  170. net-update)
  171. net_update
  172. ;;
  173. list)
  174. $GPG --batch --list-keys
  175. ;;
  176. finger*)
  177. $GPG --batch --fingerprint
  178. ;;
  179. export)
  180. $GPG --armor --export "$1"
  181. ;;
  182. exportall)
  183. $GPG --armor --export
  184. ;;
  185. adv*)
  186. echo "Executing: $GPG $*"
  187. $GPG $*
  188. ;;
  189. help)
  190. usage
  191. ;;
  192. *)
  193. usage
  194. exit 1
  195. ;;
  196. esac