apt-key.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. #!/bin/sh
  2. set -e
  3. unset GREP_OPTIONS
  4. GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring"
  5. # gpg needs (in different versions more or less) files to function correctly,
  6. # so we give it its own homedir and generate some valid content for it
  7. GPGHOMEDIR="$(mktemp -d)"
  8. CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
  9. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  10. chmod 700 "$GPGHOMEDIR"
  11. # We don't use a secret keyring, of course, but gpg panics and
  12. # implodes if there isn't one available - and writeable for imports
  13. SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
  14. touch $SECRETKEYRING
  15. GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR"
  16. # create the trustdb with an (empty) dummy keyring
  17. # older gpgs required it, newer gpgs even warn that it isn't needed,
  18. # but require it nonetheless for some commands, so we just play safe
  19. # here for the foreseeable future and create a dummy one
  20. $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1
  21. # tell gpg that it shouldn't try to maintain a trustdb file
  22. GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always"
  23. GPG="$GPG_CMD"
  24. APT_DIR="/"
  25. eval $(apt-config shell APT_DIR Dir)
  26. MASTER_KEYRING='&keyring-master-filename;'
  27. eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
  28. ARCHIVE_KEYRING='&keyring-filename;'
  29. eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
  30. REMOVED_KEYS='&keyring-removed-filename;'
  31. eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
  32. ARCHIVE_KEYRING_URI='&keyring-uri;'
  33. eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
  34. TMP_KEYRING=${APT_DIR}/var/lib/apt/keyrings/maybe-import-keyring.gpg
  35. aptkey_echo() { echo "$@"; }
  36. requires_root() {
  37. if [ "$(id -u)" -ne 0 ]; then
  38. echo >&2 "ERROR: This command can only be used by root."
  39. exit 1
  40. fi
  41. }
  42. add_keys_with_verify_against_master_keyring() {
  43. ADD_KEYRING=$1
  44. MASTER=$2
  45. if [ ! -f "$ADD_KEYRING" ]; then
  46. echo >&2 "ERROR: '$ADD_KEYRING' not found"
  47. return
  48. fi
  49. if [ ! -f "$MASTER" ]; then
  50. echo >&2 "ERROR: '$MASTER' not found"
  51. return
  52. fi
  53. # when adding new keys, make sure that the archive-master-keyring
  54. # is honored. so:
  55. # all keys that are exported must have a valid signature
  56. # from a key in the $distro-master-keyring
  57. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  58. all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
  59. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  60. # ensure there are no colisions LP: #857472
  61. for all_add_key in $all_add_keys; do
  62. for master_key in $master_keys; do
  63. if [ "$all_add_key" = "$master_key" ]; then
  64. echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
  65. return 1
  66. fi
  67. done
  68. done
  69. for add_key in $add_keys; do
  70. # export the add keyring one-by-one
  71. rm -f $TMP_KEYRING
  72. $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
  73. # check if signed with the master key and only add in this case
  74. ADDED=0
  75. for master_key in $master_keys; do
  76. if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then
  77. $GPG --import $TMP_KEYRING
  78. ADDED=1
  79. fi
  80. done
  81. if [ $ADDED = 0 ]; then
  82. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  83. fi
  84. done
  85. rm -f $TMP_KEYRING
  86. }
  87. # update the current archive signing keyring from a network URI
  88. # the archive-keyring keys needs to be signed with the master key
  89. # (otherwise it does not make sense from a security POV)
  90. net_update() {
  91. # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
  92. APT_KEY_NET_UPDATE_ENABLED=""
  93. eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
  94. if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
  95. exit 1
  96. fi
  97. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  98. echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
  99. exit 1
  100. fi
  101. # in theory we would need to depend on wget for this, but this feature
  102. # isn't useable in debian anyway as we have no keyring uri nor a master key
  103. if ! which wget >/dev/null 2>&1; then
  104. echo >&2 "ERROR: an installed wget is required for a network-based update"
  105. exit 1
  106. fi
  107. if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
  108. mkdir -p ${APT_DIR}/var/lib/apt/keyrings
  109. fi
  110. keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
  111. old_mtime=0
  112. if [ -e $keyring ]; then
  113. old_mtime=$(stat -c %Y $keyring)
  114. fi
  115. (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
  116. if [ ! -e $keyring ]; then
  117. return
  118. fi
  119. new_mtime=$(stat -c %Y $keyring)
  120. if [ $new_mtime -ne $old_mtime ]; then
  121. aptkey_echo "Checking for new archive signing keys now"
  122. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  123. fi
  124. }
  125. update() {
  126. if [ ! -f $ARCHIVE_KEYRING ]; then
  127. echo >&2 "ERROR: Can't find the archive-keyring"
  128. echo >&2 "Is the &keyring-package; package installed?"
  129. exit 1
  130. fi
  131. # add new keys from the package;
  132. # we do not use add_keys_with_verify_against_master_keyring here,
  133. # because "update" is run on regular package updates. A
  134. # attacker might as well replace the master-archive-keyring file
  135. # in the package and add his own keys. so this check wouldn't
  136. # add any security. we *need* this check on net-update though
  137. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  138. if [ -r "$REMOVED_KEYS" ]; then
  139. # remove no-longer supported/used keys
  140. $GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5 | while read key; do
  141. foreach_keyring_do 'remove_key_from_keyring' "$key"
  142. done
  143. else
  144. echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
  145. fi
  146. }
  147. remove_key_from_keyring() {
  148. local KEYRINGFILE="$1"
  149. shift
  150. # non-existent keyrings have by definition no keys
  151. if [ ! -e "$KEYRINGFILE" ]; then
  152. return
  153. fi
  154. local GPG="$GPG_CMD --keyring $KEYRINGFILE"
  155. while [ -n "$1" ]; do
  156. local KEY="$1"
  157. shift
  158. # check if the key is in this keyring: the key id is in the 5 column at the end
  159. if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]*${KEY}:"; then
  160. continue
  161. fi
  162. if [ ! -w "$KEYRINGFILE" ]; then
  163. echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
  164. continue
  165. fi
  166. # check if it is the only key in the keyring and if so remove the keyring altogether
  167. if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
  168. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
  169. return
  170. fi
  171. # we can't just modify pointed to files as these might be in /usr or something
  172. local REALTARGET
  173. if [ -L "$KEYRINGFILE" ]; then
  174. REALTARGET="$(readlink -f "$KEYRINGFILE")"
  175. mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
  176. cp -a "$REALTARGET" "$KEYRINGFILE"
  177. fi
  178. # delete the key from the keyring
  179. $GPG --batch --delete-key --yes "$KEY"
  180. if [ -n "$REALTARGET" ]; then
  181. # the real backup is the old link, not the copy we made
  182. mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
  183. fi
  184. done
  185. }
  186. foreach_keyring_do() {
  187. local ACTION="$1"
  188. shift
  189. # if a --keyring was given, just remove from there
  190. if [ -n "$FORCED_KEYRING" ]; then
  191. $ACTION "$FORCED_KEYRING" "$@"
  192. else
  193. # otherwise all known keyrings are up for inspection
  194. if [ -s "$TRUSTEDFILE" ]; then
  195. $ACTION "$TRUSTEDFILE" "$@"
  196. fi
  197. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  198. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  199. if [ -d "$TRUSTEDPARTS" ]; then
  200. # strip / suffix as gpg will double-slash in that case (#665411)
  201. local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
  202. if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
  203. TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
  204. fi
  205. for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
  206. if [ -s "$trusted" ]; then
  207. $ACTION "$trusted" "$@"
  208. fi
  209. done
  210. fi
  211. fi
  212. }
  213. list_keys_from_keyring() {
  214. local KEYRINGFILE="$1"
  215. shift
  216. # don't show the error message if this keyring doesn't include the key
  217. $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev/null || true
  218. }
  219. fingerprint_keys_from_keyring() {
  220. local KEYRINGFILE="$1"
  221. shift
  222. # don't show the error message if this keyring doesn't include the fingerprint
  223. $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev/null || true
  224. }
  225. import_keys_from_keyring() {
  226. local IMPORT="$1"
  227. local KEYRINGFILE="$2"
  228. $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev/null 2>&1
  229. }
  230. setup_merged_keyring() {
  231. local TRUSTEDFILE_BAK="$TRUSTEDFILE"
  232. TRUSTEDFILE='/dev/null'
  233. foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
  234. TRUSTEDFILE="$TRUSTEDFILE_BAK"
  235. # mark it as non-writeable so users get errors if gnupg tries to modify it
  236. if [ -s "${GPGHOMEDIR}/trusted.gpg" ]; then
  237. chmod -w "${GPGHOMEDIR}/trusted.gpg"
  238. GPG="$GPG --keyring ${GPGHOMEDIR}/trusted.gpg"
  239. fi
  240. if [ -r "$TRUSTEDFILE" ]; then
  241. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  242. fi
  243. }
  244. usage() {
  245. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  246. echo
  247. echo "Manage apt's list of trusted keys"
  248. echo
  249. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  250. echo " apt-key del <keyid> - remove the key <keyid>"
  251. echo " apt-key export <keyid> - output the key <keyid>"
  252. echo " apt-key exportall - output all trusted keys"
  253. echo " apt-key update - update keys using the keyring package"
  254. echo " apt-key net-update - update keys using the network"
  255. echo " apt-key list - list keys"
  256. echo " apt-key finger - list fingerprints"
  257. echo " apt-key adv - pass advanced options to gpg (download key)"
  258. echo
  259. echo "If no specific keyring file is given the command applies to all keyring files."
  260. }
  261. while [ -n "$1" ]; do
  262. case "$1" in
  263. --keyring)
  264. shift
  265. TRUSTEDFILE="$1"
  266. FORCED_KEYRING="$1"
  267. shift
  268. ;;
  269. --fakeroot)
  270. requires_root() { true; }
  271. shift
  272. ;;
  273. --quiet)
  274. aptkey_echo() { true; }
  275. shift
  276. ;;
  277. --*)
  278. echo >&2 "Unknown option: $1"
  279. usage
  280. exit 1;;
  281. *)
  282. break;;
  283. esac
  284. done
  285. if [ -z "$TRUSTEDFILE" ]; then
  286. TRUSTEDFILE="/etc/apt/trusted.gpg"
  287. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  288. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  289. fi
  290. command="$1"
  291. if [ -z "$command" ]; then
  292. usage
  293. exit 1
  294. fi
  295. shift
  296. if [ "$command" != "help" ]; then
  297. if ! which gpg >/dev/null 2>&1; then
  298. echo >&2 "Warning: gnupg does not seem to be installed."
  299. echo >&2 "Warning: apt-key requires gnupg for most operations."
  300. echo >&2
  301. fi
  302. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  303. if ! [ -e "$TRUSTEDFILE" ]; then
  304. if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
  305. touch -- "$TRUSTEDFILE"
  306. chmod 0644 -- "$TRUSTEDFILE"
  307. fi
  308. fi
  309. fi
  310. case "$command" in
  311. add)
  312. requires_root
  313. setup_merged_keyring
  314. $GPG --quiet --batch --import "$@"
  315. aptkey_echo "OK"
  316. ;;
  317. del|rm|remove)
  318. requires_root
  319. foreach_keyring_do 'remove_key_from_keyring' "$@"
  320. aptkey_echo "OK"
  321. ;;
  322. update)
  323. requires_root
  324. setup_merged_keyring
  325. update
  326. ;;
  327. net-update)
  328. requires_root
  329. setup_merged_keyring
  330. net_update
  331. ;;
  332. list)
  333. foreach_keyring_do 'list_keys_from_keyring' "$@"
  334. ;;
  335. finger*)
  336. foreach_keyring_do 'fingerprint_keys_from_keyring' "$@"
  337. ;;
  338. export|exportall)
  339. foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/trusted.gpg"
  340. $GPG_CMD --keyring "${GPGHOMEDIR}/trusted.gpg" --armor --export "$@"
  341. ;;
  342. adv*)
  343. setup_merged_keyring
  344. aptkey_echo "Executing: $GPG $*"
  345. $GPG "$@"
  346. ;;
  347. help)
  348. usage
  349. ;;
  350. *)
  351. usage
  352. exit 1
  353. ;;
  354. esac