apt-key 7.7 KB

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