apt-key 4.8 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 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. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  53. old_mtime=0
  54. if [ -e $keyring ]; then
  55. old_mtime=$(stat -c %Y $keyring)
  56. fi
  57. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  58. if [ ! -e $keyring ]; then
  59. return
  60. fi
  61. new_mtime=$(stat -c %Y $keyring)
  62. if [ $new_mtime -ne $old_mtime ]; then
  63. echo "Checking for new archive signing keys now"
  64. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  65. fi
  66. }
  67. update() {
  68. if [ ! -f $ARCHIVE_KEYRING ]; then
  69. echo >&2 "ERROR: Can't find the archive-keyring"
  70. echo >&2 "Is the ubuntu-keyring package installed?"
  71. exit 1
  72. fi
  73. # add new keys, if no MASTER_KEYRING is used, use the traditional
  74. # way
  75. if [ -z "$MASTER_KEYRING" ]; then
  76. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  77. else
  78. add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
  79. fi
  80. # remove no-longer supported/used keys
  81. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  82. for key in $keys; do
  83. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  84. $GPG --quiet --batch --delete-key --yes ${key}
  85. fi
  86. done
  87. }
  88. usage() {
  89. echo "Usage: apt-key [command] [arguments]"
  90. echo
  91. echo "Manage apt's list of trusted keys"
  92. echo
  93. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  94. echo " apt-key del <keyid> - remove the key <keyid>"
  95. echo " apt-key export <keyid> - output the key <keyid>"
  96. echo " apt-key exportall - output all trusted keys"
  97. echo " apt-key update - update keys using the keyring package"
  98. echo " apt-key net-update - update keys using the network"
  99. echo " apt-key list - list keys"
  100. echo
  101. }
  102. command="$1"
  103. if [ -z "$command" ]; then
  104. usage
  105. exit 1
  106. fi
  107. shift
  108. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  109. echo >&2 "Warning: gnupg does not seem to be installed."
  110. echo >&2 "Warning: apt-key requires gnupg for most operations."
  111. echo >&2
  112. fi
  113. case "$command" in
  114. add)
  115. $GPG --quiet --batch --import "$1"
  116. echo "OK"
  117. ;;
  118. del|rm|remove)
  119. $GPG --quiet --batch --delete-key --yes "$1"
  120. echo "OK"
  121. ;;
  122. update)
  123. update
  124. ;;
  125. net-update)
  126. net_update
  127. ;;
  128. list)
  129. $GPG --batch --list-keys
  130. ;;
  131. finger*)
  132. $GPG --batch --fingerprint
  133. ;;
  134. export)
  135. $GPG --armor --export "$1"
  136. ;;
  137. exportall)
  138. $GPG --armor --export
  139. ;;
  140. adv*)
  141. echo "Executing: $GPG $*"
  142. $GPG $*
  143. ;;
  144. help)
  145. usage
  146. ;;
  147. *)
  148. usage
  149. exit 1
  150. ;;
  151. esac