apt-key 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/bin/sh
  2. set -e
  3. unset GREP_OPTIONS
  4. # We don't use a secret keyring, of course, but gpg panics and
  5. # implodes if there isn't one available
  6. GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
  7. GPG="$GPG_CMD --keyring /etc/apt/trusted.gpg"
  8. MASTER_KEYRING=/usr/share/keyrings/ubuntu-master-keyring.gpg
  9. ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg
  10. REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg
  11. ARCHIVE_KEYRING_URI=http://archive.ubuntu.com/ubuntu/project/ubuntu-archive-keyring.gpg
  12. add_keys_with_verify_against_master_keyring() {
  13. ADD_KEYRING=$1
  14. MASTER=$2
  15. if [ ! -f "$ADD_KEYRING" ]; then
  16. echo "ERROR: '$ADD_KEYRING' not found"
  17. return
  18. fi
  19. if [ ! -f "$MASTER" ]; then
  20. echo "ERROR: '$MASTER' not found"
  21. return
  22. fi
  23. # when adding new keys, make sure that the archive-master-keyring
  24. # is honored. so:
  25. # all keys that are exported must have a valid signature
  26. # from a key in the $distro-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 from the package;
  74. # we do not use add_keys_with_verify_against_master_keyring here,
  75. # because we "update" is run on regular package updates. A
  76. # attacker might as well replace the master-archive-keyring file
  77. # in the package and add his own keys. so this check wouldn't
  78. # add any security. we *need* this check on net-update though
  79. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  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 " apt-key finger - list fingerprints"
  101. echo " apt-key adv - pass advanced options to gpg (download key)"
  102. echo
  103. }
  104. command="$1"
  105. if [ -z "$command" ]; then
  106. usage
  107. exit 1
  108. fi
  109. shift
  110. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  111. echo >&2 "Warning: gnupg does not seem to be installed."
  112. echo >&2 "Warning: apt-key requires gnupg for most operations."
  113. echo >&2
  114. fi
  115. case "$command" in
  116. add)
  117. $GPG --quiet --batch --import "$1"
  118. echo "OK"
  119. ;;
  120. del|rm|remove)
  121. $GPG --quiet --batch --delete-key --yes "$1"
  122. echo "OK"
  123. ;;
  124. update)
  125. update
  126. ;;
  127. net-update)
  128. net_update
  129. ;;
  130. list)
  131. $GPG --batch --list-keys
  132. ;;
  133. finger*)
  134. $GPG --batch --fingerprint
  135. ;;
  136. export)
  137. $GPG --armor --export "$1"
  138. ;;
  139. exportall)
  140. $GPG --armor --export
  141. ;;
  142. adv*)
  143. echo "Executing: $GPG $*"
  144. $GPG $*
  145. ;;
  146. help)
  147. usage
  148. ;;
  149. *)
  150. usage
  151. exit 1
  152. ;;
  153. esac