apt-key 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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'
  7. if [ "$(id -u)" -eq 0 ]; then
  8. GPG_CMD="$GPG_CMD --trustdb-name /etc/apt/trustdb.gpg"
  9. fi
  10. GPG="$GPG_CMD"
  11. MASTER_KEYRING=""
  12. ARCHIVE_KEYRING_URI=""
  13. #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
  14. #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
  15. ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
  16. REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
  17. add_keys_with_verify_against_master_keyring() {
  18. ADD_KEYRING=$1
  19. MASTER=$2
  20. if [ ! -f "$ADD_KEYRING" ]; then
  21. echo "ERROR: '$ADD_KEYRING' not found"
  22. return
  23. fi
  24. if [ ! -f "$MASTER" ]; then
  25. echo "ERROR: '$MASTER' not found"
  26. return
  27. fi
  28. # when adding new keys, make sure that the archive-master-keyring
  29. # is honored. so:
  30. # all keys that are exported must have a valid signature
  31. # from a key in the $distro-master-keyring
  32. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  33. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  34. for add_key in $add_keys; do
  35. ADDED=0
  36. for master_key in $master_keys; do
  37. if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
  38. $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
  39. ADDED=1
  40. fi
  41. done
  42. if [ $ADDED = 0 ]; then
  43. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  44. fi
  45. done
  46. }
  47. # update the current archive signing keyring from a network URI
  48. # the archive-keyring keys needs to be signed with the master key
  49. # (otherwise it does not make sense from a security POV)
  50. net_update() {
  51. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  52. echo "ERROR: no location for the archive-keyring given"
  53. exit 1
  54. fi
  55. # in theory we would need to depend on wget for this, but this feature
  56. # isn't useable in debian anyway as we have no keyring uri nor a master key
  57. if ! which wget >/dev/null 2>&1; then
  58. echo "ERROR: an installed wget is required for a network-based update"
  59. exit 1
  60. fi
  61. if [ ! -d /var/lib/apt/keyrings ]; then
  62. mkdir -p /var/lib/apt/keyrings
  63. fi
  64. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  65. old_mtime=0
  66. if [ -e $keyring ]; then
  67. old_mtime=$(stat -c %Y $keyring)
  68. fi
  69. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  70. if [ ! -e $keyring ]; then
  71. return
  72. fi
  73. new_mtime=$(stat -c %Y $keyring)
  74. if [ $new_mtime -ne $old_mtime ]; then
  75. echo "Checking for new archive signing keys now"
  76. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  77. fi
  78. }
  79. update() {
  80. if [ ! -f $ARCHIVE_KEYRING ]; then
  81. echo >&2 "ERROR: Can't find the archive-keyring"
  82. echo >&2 "Is the debian-archive-keyring package installed?"
  83. exit 1
  84. fi
  85. # add new keys from the package;
  86. # we do not use add_keys_with_verify_against_master_keyring here,
  87. # because "update" is run on regular package updates. A
  88. # attacker might as well replace the master-archive-keyring file
  89. # in the package and add his own keys. so this check wouldn't
  90. # add any security. we *need* this check on net-update though
  91. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  92. if [ -r "$REMOVED_KEYS" ]; then
  93. # remove no-longer supported/used keys
  94. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  95. for key in $keys; do
  96. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  97. $GPG --quiet --batch --delete-key --yes ${key}
  98. fi
  99. done
  100. else
  101. echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
  102. fi
  103. }
  104. usage() {
  105. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  106. echo
  107. echo "Manage apt's list of trusted keys"
  108. echo
  109. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  110. echo " apt-key del <keyid> - remove the key <keyid>"
  111. echo " apt-key export <keyid> - output the key <keyid>"
  112. echo " apt-key exportall - output all trusted keys"
  113. echo " apt-key update - update keys using the keyring package"
  114. echo " apt-key net-update - update keys using the network"
  115. echo " apt-key list - list keys"
  116. echo " apt-key finger - list fingerprints"
  117. echo " apt-key adv - pass advanced options to gpg (download key)"
  118. echo
  119. echo "If no specific keyring file is given the command applies to all keyring files."
  120. }
  121. # Determine on which keyring we want to work
  122. if [ "$1" = "--keyring" ]; then
  123. #echo "keyfile given"
  124. shift
  125. TRUSTEDFILE="$1"
  126. if [ -r "$TRUSTEDFILE" ]; then
  127. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  128. else
  129. echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
  130. exit 1
  131. fi
  132. shift
  133. # otherwise use the default
  134. else
  135. #echo "generate list"
  136. TRUSTEDFILE="/etc/apt/trusted.gpg"
  137. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  138. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  139. if [ -r "$TRUSTEDFILE" ]; then
  140. GPG="$GPG --keyring $TRUSTEDFILE"
  141. fi
  142. GPG="$GPG --primary-keyring $TRUSTEDFILE"
  143. TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  144. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  145. if [ -d "$TRUSTEDPARTS" ]; then
  146. #echo "parts active"
  147. for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
  148. #echo "part -> $trusted"
  149. GPG="$GPG --keyring $trusted"
  150. done
  151. fi
  152. fi
  153. #echo "COMMAND: $GPG"
  154. command="$1"
  155. if [ -z "$command" ]; then
  156. usage
  157. exit 1
  158. fi
  159. shift
  160. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  161. echo >&2 "Warning: gnupg does not seem to be installed."
  162. echo >&2 "Warning: apt-key requires gnupg for most operations."
  163. echo >&2
  164. fi
  165. case "$command" in
  166. add)
  167. $GPG --quiet --batch --import "$1"
  168. echo "OK"
  169. ;;
  170. del|rm|remove)
  171. $GPG --quiet --batch --delete-key --yes "$1"
  172. echo "OK"
  173. ;;
  174. update)
  175. update
  176. ;;
  177. net-update)
  178. net_update
  179. ;;
  180. list)
  181. $GPG --batch --list-keys
  182. ;;
  183. finger*)
  184. $GPG --batch --fingerprint
  185. ;;
  186. export)
  187. $GPG --armor --export "$1"
  188. ;;
  189. exportall)
  190. $GPG --armor --export
  191. ;;
  192. adv*)
  193. echo "Executing: $GPG $*"
  194. $GPG $*
  195. ;;
  196. help)
  197. usage
  198. ;;
  199. *)
  200. usage
  201. exit 1
  202. ;;
  203. esac