apt-key 4.8 KB

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