apt-key 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 --keyring /etc/apt/trusted.gpg"
  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. fi
  51. if [ ! -d /var/lib/apt/keyrings ]; then
  52. mkdir -p /var/lib/apt/keyrings
  53. fi
  54. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  55. old_mtime=0
  56. if [ -e $keyring ]; then
  57. old_mtime=$(stat -c %Y $keyring)
  58. fi
  59. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  60. if [ ! -e $keyring ]; then
  61. return
  62. fi
  63. new_mtime=$(stat -c %Y $keyring)
  64. if [ $new_mtime -ne $old_mtime ]; then
  65. echo "Checking for new archive signing keys now"
  66. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  67. fi
  68. }
  69. update() {
  70. if [ ! -f $ARCHIVE_KEYRING ]; then
  71. echo >&2 "ERROR: Can't find the archive-keyring"
  72. echo >&2 "Is the debian-archive-keyring package installed?"
  73. exit 1
  74. fi
  75. # add new keys from the package;
  76. # we do not use add_keys_with_verify_against_master_keyring here,
  77. # because "update" is run on regular package updates. A
  78. # attacker might as well replace the master-archive-keyring file
  79. # in the package and add his own keys. so this check wouldn't
  80. # add any security. we *need* this check on net-update though
  81. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  82. if [ -r "$REMOVED_KEYS" ]; then
  83. # remove no-longer supported/used keys
  84. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  85. for key in $keys; do
  86. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  87. $GPG --quiet --batch --delete-key --yes ${key}
  88. fi
  89. done
  90. else
  91. echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
  92. fi
  93. }
  94. usage() {
  95. echo "Usage: apt-key [command] [arguments]"
  96. echo
  97. echo "Manage apt's list of trusted keys"
  98. echo
  99. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  100. echo " apt-key del <keyid> - remove the key <keyid>"
  101. echo " apt-key export <keyid> - output the key <keyid>"
  102. echo " apt-key exportall - output all trusted keys"
  103. echo " apt-key update - update keys using the keyring package"
  104. echo " apt-key net-update - update keys using the network"
  105. echo " apt-key list - list keys"
  106. echo " apt-key finger - list fingerprints"
  107. echo " apt-key adv - pass advanced options to gpg (download key)"
  108. echo
  109. }
  110. command="$1"
  111. if [ -z "$command" ]; then
  112. usage
  113. exit 1
  114. fi
  115. shift
  116. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  117. echo >&2 "Warning: gnupg does not seem to be installed."
  118. echo >&2 "Warning: apt-key requires gnupg for most operations."
  119. echo >&2
  120. fi
  121. case "$command" in
  122. add)
  123. $GPG --quiet --batch --import "$1"
  124. echo "OK"
  125. ;;
  126. del|rm|remove)
  127. $GPG --quiet --batch --delete-key --yes "$1"
  128. echo "OK"
  129. ;;
  130. update)
  131. update
  132. ;;
  133. net-update)
  134. net_update
  135. ;;
  136. list)
  137. $GPG --batch --list-keys
  138. ;;
  139. finger*)
  140. $GPG --batch --fingerprint
  141. ;;
  142. export)
  143. $GPG --armor --export "$1"
  144. ;;
  145. exportall)
  146. $GPG --armor --export
  147. ;;
  148. adv*)
  149. echo "Executing: $GPG $*"
  150. $GPG $*
  151. ;;
  152. help)
  153. usage
  154. ;;
  155. *)
  156. usage
  157. exit 1
  158. ;;
  159. esac