apt-key 4.5 KB

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