apt-key 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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=/usr/share/keyrings/ubuntu-master-keyring.gpg
  8. ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg
  9. REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg
  10. add_keys_with_verify_against_master_keyring() {
  11. ADD_KEYRING=$1
  12. MASTER=$2
  13. if [ ! -f "$ADD_KEYRING" ]; then
  14. echo "ERROR: '$ADD_KEYRING' not found"
  15. return
  16. fi
  17. if [ ! -f "$MASTER" ]; then
  18. echo "ERROR: '$MASTER' not found"
  19. return
  20. fi
  21. # when adding new keys, make sure that the archive-master-keyring
  22. # is honored. so:
  23. # all keys that are exported and have the name
  24. # "Ubuntu Archive Automatic Signing Key" must have a valid signature
  25. # from a key in the ubuntu-master-keyring
  26. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  27. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  28. for add_key in $add_keys; do
  29. for master_key in $master_keys; do
  30. if $GPG --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
  31. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export $add_key | $GPG --import
  32. fi
  33. done
  34. done
  35. }
  36. update() {
  37. if [ ! -f $ARCHIVE_KEYRING ]; then
  38. echo >&2 "ERROR: Can't find the archive-keyring"
  39. echo >&2 "Is the ubuntu-keyring package installed?"
  40. exit 1
  41. fi
  42. # add new keys, if no MASTER_KEYRING is used, use the traditional
  43. # way
  44. if [ -z "$MASTER_KEYRING" ]; then
  45. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  46. else
  47. add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
  48. fi
  49. # remove no-longer supported/used keys
  50. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  51. for key in $keys; do
  52. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  53. $GPG --quiet --batch --delete-key --yes ${key}
  54. fi
  55. done
  56. }
  57. usage() {
  58. echo "Usage: apt-key [command] [arguments]"
  59. echo
  60. echo "Manage apt's list of trusted keys"
  61. echo
  62. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  63. echo " apt-key del <keyid> - remove the key <keyid>"
  64. echo " apt-key export <keyid> - output the key <keyid>"
  65. echo " apt-key exportall - output all trusted keys"
  66. echo " apt-key update - update keys using the keyring package"
  67. echo " apt-key list - list keys"
  68. echo
  69. }
  70. command="$1"
  71. if [ -z "$command" ]; then
  72. usage
  73. exit 1
  74. fi
  75. shift
  76. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  77. echo >&2 "Warning: gnupg does not seem to be installed."
  78. echo >&2 "Warning: apt-key requires gnupg for most operations."
  79. echo >&2
  80. fi
  81. case "$command" in
  82. add)
  83. $GPG --quiet --batch --import "$1"
  84. echo "OK"
  85. ;;
  86. del|rm|remove)
  87. $GPG --quiet --batch --delete-key --yes "$1"
  88. echo "OK"
  89. ;;
  90. update)
  91. update
  92. ;;
  93. list)
  94. $GPG --batch --list-keys
  95. ;;
  96. finger*)
  97. $GPG --batch --fingerprint
  98. ;;
  99. export)
  100. $GPG --armor --export "$1"
  101. ;;
  102. exportall)
  103. $GPG --armor --export
  104. ;;
  105. adv*)
  106. echo "Executing: $GPG $*"
  107. $GPG $*
  108. ;;
  109. help)
  110. usage
  111. ;;
  112. *)
  113. usage
  114. exit 1
  115. ;;
  116. esac