apt-key 7.1 KB

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