apt-key 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. ADDED=0
  30. for master_key in $master_keys; do
  31. if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
  32. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export $add_key | $GPG --import
  33. ADDED=1
  34. fi
  35. done
  36. if [ $ADDED = 0 ]; then
  37. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  38. fi
  39. done
  40. }
  41. update() {
  42. if [ ! -f $ARCHIVE_KEYRING ]; then
  43. echo >&2 "ERROR: Can't find the archive-keyring"
  44. echo >&2 "Is the ubuntu-keyring package installed?"
  45. exit 1
  46. fi
  47. # add new keys, if no MASTER_KEYRING is used, use the traditional
  48. # way
  49. if [ -z "$MASTER_KEYRING" ]; then
  50. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  51. else
  52. add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
  53. fi
  54. # remove no-longer supported/used keys
  55. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  56. for key in $keys; do
  57. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  58. $GPG --quiet --batch --delete-key --yes ${key}
  59. fi
  60. done
  61. }
  62. usage() {
  63. echo "Usage: apt-key [command] [arguments]"
  64. echo
  65. echo "Manage apt's list of trusted keys"
  66. echo
  67. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  68. echo " apt-key del <keyid> - remove the key <keyid>"
  69. echo " apt-key export <keyid> - output the key <keyid>"
  70. echo " apt-key exportall - output all trusted keys"
  71. echo " apt-key update - update keys using the keyring package"
  72. echo " apt-key list - list keys"
  73. echo
  74. }
  75. command="$1"
  76. if [ -z "$command" ]; then
  77. usage
  78. exit 1
  79. fi
  80. shift
  81. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  82. echo >&2 "Warning: gnupg does not seem to be installed."
  83. echo >&2 "Warning: apt-key requires gnupg for most operations."
  84. echo >&2
  85. fi
  86. case "$command" in
  87. add)
  88. $GPG --quiet --batch --import "$1"
  89. echo "OK"
  90. ;;
  91. del|rm|remove)
  92. $GPG --quiet --batch --delete-key --yes "$1"
  93. echo "OK"
  94. ;;
  95. update)
  96. update
  97. ;;
  98. list)
  99. $GPG --batch --list-keys
  100. ;;
  101. finger*)
  102. $GPG --batch --fingerprint
  103. ;;
  104. export)
  105. $GPG --armor --export "$1"
  106. ;;
  107. exportall)
  108. $GPG --armor --export
  109. ;;
  110. adv*)
  111. echo "Executing: $GPG $*"
  112. $GPG $*
  113. ;;
  114. help)
  115. usage
  116. ;;
  117. *)
  118. usage
  119. exit 1
  120. ;;
  121. esac