apt-key 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. # remove no-longer supported/used keys
  83. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  84. for key in $keys; do
  85. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  86. $GPG --quiet --batch --delete-key --yes ${key}
  87. fi
  88. done
  89. }
  90. usage() {
  91. echo "Usage: apt-key [command] [arguments]"
  92. echo
  93. echo "Manage apt's list of trusted keys"
  94. echo
  95. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  96. echo " apt-key del <keyid> - remove the key <keyid>"
  97. echo " apt-key export <keyid> - output the key <keyid>"
  98. echo " apt-key exportall - output all trusted keys"
  99. echo " apt-key update - update keys using the keyring package"
  100. echo " apt-key net-update - update keys using the network"
  101. echo " apt-key list - list keys"
  102. echo " apt-key finger - list fingerprints"
  103. echo " apt-key adv - pass advanced options to gpg (download key)"
  104. echo
  105. }
  106. command="$1"
  107. if [ -z "$command" ]; then
  108. usage
  109. exit 1
  110. fi
  111. shift
  112. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  113. echo >&2 "Warning: gnupg does not seem to be installed."
  114. echo >&2 "Warning: apt-key requires gnupg for most operations."
  115. echo >&2
  116. fi
  117. case "$command" in
  118. add)
  119. $GPG --quiet --batch --import "$1"
  120. echo "OK"
  121. ;;
  122. del|rm|remove)
  123. $GPG --quiet --batch --delete-key --yes "$1"
  124. echo "OK"
  125. ;;
  126. update)
  127. update
  128. ;;
  129. net-update)
  130. net_update
  131. ;;
  132. list)
  133. $GPG --batch --list-keys
  134. ;;
  135. finger*)
  136. $GPG --batch --fingerprint
  137. ;;
  138. export)
  139. $GPG --armor --export "$1"
  140. ;;
  141. exportall)
  142. $GPG --armor --export
  143. ;;
  144. adv*)
  145. echo "Executing: $GPG $*"
  146. $GPG $*
  147. ;;
  148. help)
  149. usage
  150. ;;
  151. *)
  152. usage
  153. exit 1
  154. ;;
  155. esac