apt-key 5.1 KB

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