apt-key 6.9 KB

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