apt-key 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/project/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 must have a valid signature
  25. # from a key in the $distro-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 $ADD_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 the current archive signing keyring from a network URI
  42. # the archive-keyring keys needs to be signed with the master key
  43. # (otherwise it does not make sense from a security POV)
  44. net_update() {
  45. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  46. echo "ERROR: no location for the archive-keyring given"
  47. fi
  48. if [ ! -d /var/lib/apt/keyrings ]; then
  49. mkdir -p /var/lib/apt/keyrings
  50. fi
  51. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  52. old_mtime=0
  53. if [ -e $keyring ]; then
  54. old_mtime=$(stat -c %Y $keyring)
  55. fi
  56. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  57. if [ ! -e $keyring ]; then
  58. return
  59. fi
  60. new_mtime=$(stat -c %Y $keyring)
  61. if [ $new_mtime -ne $old_mtime ]; then
  62. echo "Checking for new archive signing keys now"
  63. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  64. fi
  65. }
  66. update() {
  67. if [ ! -f $ARCHIVE_KEYRING ]; then
  68. echo >&2 "ERROR: Can't find the archive-keyring"
  69. echo >&2 "Is the ubuntu-keyring package installed?"
  70. exit 1
  71. fi
  72. # add new keys from the package;
  73. # we do not use add_keys_with_verify_against_master_keyring here,
  74. # because we "update" is run on regular package updates. A
  75. # attacker might as well replace the master-archive-keyring file
  76. # in the package and add his own keys. so this check wouldn't
  77. # add any security. we *need* this check on net-update though
  78. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  79. # remove no-longer supported/used keys
  80. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  81. for key in $keys; do
  82. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  83. $GPG --quiet --batch --delete-key --yes ${key}
  84. fi
  85. done
  86. }
  87. usage() {
  88. echo "Usage: apt-key [command] [arguments]"
  89. echo
  90. echo "Manage apt's list of trusted keys"
  91. echo
  92. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  93. echo " apt-key del <keyid> - remove the key <keyid>"
  94. echo " apt-key export <keyid> - output the key <keyid>"
  95. echo " apt-key exportall - output all trusted keys"
  96. echo " apt-key update - update keys using the keyring package"
  97. echo " apt-key net-update - update keys using the network"
  98. echo " apt-key list - list keys"
  99. echo
  100. }
  101. command="$1"
  102. if [ -z "$command" ]; then
  103. usage
  104. exit 1
  105. fi
  106. shift
  107. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  108. echo >&2 "Warning: gnupg does not seem to be installed."
  109. echo >&2 "Warning: apt-key requires gnupg for most operations."
  110. echo >&2
  111. fi
  112. case "$command" in
  113. add)
  114. $GPG --quiet --batch --import "$1"
  115. echo "OK"
  116. ;;
  117. del|rm|remove)
  118. $GPG --quiet --batch --delete-key --yes "$1"
  119. echo "OK"
  120. ;;
  121. update)
  122. update
  123. ;;
  124. net-update)
  125. net_update
  126. ;;
  127. list)
  128. $GPG --batch --list-keys
  129. ;;
  130. finger*)
  131. $GPG --batch --fingerprint
  132. ;;
  133. export)
  134. $GPG --armor --export "$1"
  135. ;;
  136. exportall)
  137. $GPG --armor --export
  138. ;;
  139. adv*)
  140. echo "Executing: $GPG $*"
  141. $GPG $*
  142. ;;
  143. help)
  144. usage
  145. ;;
  146. *)
  147. usage
  148. exit 1
  149. ;;
  150. esac