apt-key 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if [ -r "$REMOVED_KEYS" ]; then
  81. # remove no-longer supported/used keys
  82. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  83. for key in $keys; do
  84. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  85. $GPG --quiet --batch --delete-key --yes ${key}
  86. fi
  87. done
  88. else
  89. echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
  90. fi
  91. }
  92. usage() {
  93. echo "Usage: apt-key [command] [arguments]"
  94. echo
  95. echo "Manage apt's list of trusted keys"
  96. echo
  97. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  98. echo " apt-key del <keyid> - remove the key <keyid>"
  99. echo " apt-key export <keyid> - output the key <keyid>"
  100. echo " apt-key exportall - output all trusted keys"
  101. echo " apt-key update - update keys using the keyring package"
  102. echo " apt-key net-update - update keys using the network"
  103. echo " apt-key list - list keys"
  104. echo " apt-key finger - list fingerprints"
  105. echo " apt-key adv - pass advanced options to gpg (download key)"
  106. echo
  107. }
  108. command="$1"
  109. if [ -z "$command" ]; then
  110. usage
  111. exit 1
  112. fi
  113. shift
  114. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  115. echo >&2 "Warning: gnupg does not seem to be installed."
  116. echo >&2 "Warning: apt-key requires gnupg for most operations."
  117. echo >&2
  118. fi
  119. case "$command" in
  120. add)
  121. $GPG --quiet --batch --import "$1"
  122. echo "OK"
  123. ;;
  124. del|rm|remove)
  125. $GPG --quiet --batch --delete-key --yes "$1"
  126. echo "OK"
  127. ;;
  128. update)
  129. update
  130. ;;
  131. net-update)
  132. net_update
  133. ;;
  134. list)
  135. $GPG --batch --list-keys
  136. ;;
  137. finger*)
  138. $GPG --batch --fingerprint
  139. ;;
  140. export)
  141. $GPG --armor --export "$1"
  142. ;;
  143. exportall)
  144. $GPG --armor --export
  145. ;;
  146. adv*)
  147. echo "Executing: $GPG $*"
  148. $GPG $*
  149. ;;
  150. help)
  151. usage
  152. ;;
  153. *)
  154. usage
  155. exit 1
  156. ;;
  157. esac