apt-key.in 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. #!/bin/sh
  2. set -e
  3. unset GREP_OPTIONS
  4. export IFS="$(printf "\n\b")"
  5. MASTER_KEYRING='&keyring-master-filename;'
  6. eval "$(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)"
  7. ARCHIVE_KEYRING='&keyring-filename;'
  8. eval "$(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)"
  9. REMOVED_KEYS='&keyring-removed-filename;'
  10. eval "$(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)"
  11. ARCHIVE_KEYRING_URI='&keyring-uri;'
  12. eval "$(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)"
  13. aptkey_echo() { echo "$@"; }
  14. requires_root() {
  15. if [ "$(id -u)" -ne 0 ]; then
  16. echo >&2 "ERROR: This command can only be used by root."
  17. exit 1
  18. fi
  19. }
  20. command_available() {
  21. # command -v "$1" >/dev/null 2>&1 # not required by policy, see #747320
  22. # which "$1" >/dev/null 2>&1 # is in debianutils (essential) but not on non-debian systems
  23. local OLDIFS="$IFS"
  24. IFS=:
  25. for p in $PATH; do
  26. if [ -x "${p}/${1}" ]; then
  27. IFS="$OLDIFS"
  28. return 0
  29. fi
  30. done
  31. IFS="$OLDIFS"
  32. return 1
  33. }
  34. escape_shell() {
  35. echo "$@" | sed -e "s#'#'\"'\"'#g"
  36. }
  37. get_fingerprints_of_keyring() {
  38. aptkey_execute "$GPG_SH" --keyring "$1" --with-colons --fingerprint | while read publine; do
  39. # search for a public key
  40. if [ "${publine%%:*}" != 'pub' ]; then continue; fi
  41. # search for the associated fingerprint (should be the very next line)
  42. while read fprline; do
  43. if [ "${fprline%%:*}" = 'sub' ]; then break; # should never happen
  44. elif [ "${fprline%%:*}" != 'fpr' ]; then continue; fi
  45. echo "$fprline" | cut -d':' -f 10
  46. done
  47. # order in the keyring shouldn't be important
  48. done | sort
  49. }
  50. add_keys_with_verify_against_master_keyring() {
  51. ADD_KEYRING="$1"
  52. MASTER="$2"
  53. if [ ! -f "$ADD_KEYRING" ]; then
  54. echo >&2 "ERROR: '$ADD_KEYRING' not found"
  55. return
  56. fi
  57. if [ ! -f "$MASTER" ]; then
  58. echo >&2 "ERROR: '$MASTER' not found"
  59. return
  60. fi
  61. # when adding new keys, make sure that the archive-master-keyring
  62. # is honored. so:
  63. # all keys that are exported must have a valid signature
  64. # from a key in the $distro-master-keyring
  65. add_keys="$(get_fingerprints_of_keyring "$ADD_KEYRING")"
  66. all_add_keys="$(aptkey_execute "$GPG_SH" --keyring "$ADD_KEYRING" --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5)"
  67. master_keys="$(aptkey_execute "$GPG_SH" --keyring "$MASTER" --with-colons --list-keys | grep ^pub | cut -d: -f5)"
  68. # ensure there are no colisions LP: #857472
  69. for all_add_key in $all_add_keys; do
  70. for master_key in $master_keys; do
  71. if [ "$all_add_key" = "$master_key" ]; then
  72. echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
  73. return 1
  74. fi
  75. done
  76. done
  77. for add_key in $add_keys; do
  78. # export the add keyring one-by-one
  79. local TMP_KEYRING="${GPGHOMEDIR}/tmp-keyring.gpg"
  80. aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --output "$TMP_KEYRING" --export "$add_key"
  81. if ! aptkey_execute "$GPG_SH" --batch --yes --keyring "$TMP_KEYRING" --import "$MASTER" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  82. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  83. false
  84. fi
  85. # check if signed with the master key and only add in this case
  86. ADDED=0
  87. for master_key in $master_keys; do
  88. if aptkey_execute "$GPG_SH" --keyring "$TMP_KEYRING" --check-sigs --with-colons "$add_key" \
  89. | grep '^sig:!:' | cut -d: -f5 | grep -q "$master_key"; then
  90. aptkey_execute "$GPG_SH" --batch --yes --keyring "$ADD_KEYRING" --export "$add_key" \
  91. | aptkey_execute "$GPG" --batch --yes --import
  92. ADDED=1
  93. fi
  94. done
  95. if [ $ADDED = 0 ]; then
  96. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  97. fi
  98. rm -f "${TMP_KEYRING}"
  99. done
  100. }
  101. # update the current archive signing keyring from a network URI
  102. # the archive-keyring keys needs to be signed with the master key
  103. # (otherwise it does not make sense from a security POV)
  104. net_update() {
  105. local APT_DIR='/'
  106. eval $(apt-config shell APT_DIR Dir)
  107. # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
  108. APT_KEY_NET_UPDATE_ENABLED=""
  109. eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
  110. if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
  111. exit 1
  112. fi
  113. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  114. echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
  115. exit 1
  116. fi
  117. # in theory we would need to depend on wget for this, but this feature
  118. # isn't useable in debian anyway as we have no keyring uri nor a master key
  119. if ! command_available 'wget'; then
  120. echo >&2 "ERROR: an installed wget is required for a network-based update"
  121. exit 1
  122. fi
  123. if [ ! -d "${APT_DIR}/var/lib/apt/keyrings" ]; then
  124. mkdir -p "${APT_DIR}/var/lib/apt/keyrings"
  125. fi
  126. keyring="${APT_DIR}/var/lib/apt/keyrings/$(basename "$ARCHIVE_KEYRING_URI")"
  127. old_mtime=0
  128. if [ -e $keyring ]; then
  129. old_mtime=$(stat -c %Y "$keyring")
  130. fi
  131. (cd "${APT_DIR}/var/lib/apt/keyrings"; wget --timeout=90 -q -N "$ARCHIVE_KEYRING_URI")
  132. if [ ! -e "$keyring" ]; then
  133. return
  134. fi
  135. new_mtime=$(stat -c %Y "$keyring")
  136. if [ $new_mtime -ne $old_mtime ]; then
  137. aptkey_echo "Checking for new archive signing keys now"
  138. add_keys_with_verify_against_master_keyring "$keyring" "$MASTER_KEYRING"
  139. fi
  140. }
  141. update() {
  142. if [ ! -f "$ARCHIVE_KEYRING" ]; then
  143. echo >&2 "ERROR: Can't find the archive-keyring"
  144. echo >&2 "Is the &keyring-package; package installed?"
  145. exit 1
  146. fi
  147. # add new keys from the package;
  148. # we do not use add_keys_with_verify_against_master_keyring here,
  149. # because "update" is run on regular package updates. A
  150. # attacker might as well replace the master-archive-keyring file
  151. # in the package and add his own keys. so this check wouldn't
  152. # add any security. we *need* this check on net-update though
  153. import_keyring_into_keyring "$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
  154. if [ -r "$REMOVED_KEYS" ]; then
  155. # remove no-longer supported/used keys
  156. get_fingerprints_of_keyring "$REMOVED_KEYS" | while read key; do
  157. foreach_keyring_do 'remove_key_from_keyring' "$key"
  158. done
  159. else
  160. echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
  161. fi
  162. }
  163. remove_key_from_keyring() {
  164. local KEYRINGFILE="$1"
  165. shift
  166. # non-existent keyrings have by definition no keys
  167. if [ ! -e "$KEYRINGFILE" ]; then
  168. return
  169. fi
  170. for KEY in "$@"; do
  171. local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst"
  172. get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS"
  173. # strip leading 0x, if present:
  174. KEY="${KEY#0x}"
  175. # check if the key is in this keyring
  176. if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
  177. continue
  178. fi
  179. if [ ! -w "$KEYRINGFILE" ]; then
  180. echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
  181. continue
  182. fi
  183. # check if it is the only key in the keyring and if so remove the keyring altogether
  184. if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
  185. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
  186. return
  187. fi
  188. # we can't just modify pointed to files as these might be in /usr or something
  189. local REALTARGET
  190. if [ -L "$KEYRINGFILE" ]; then
  191. REALTARGET="$(readlink -f "$KEYRINGFILE")"
  192. mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
  193. cp -a "$REALTARGET" "$KEYRINGFILE"
  194. fi
  195. # delete the key from the keyring
  196. aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
  197. if [ -n "$REALTARGET" ]; then
  198. # the real backup is the old link, not the copy we made
  199. mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
  200. fi
  201. done
  202. }
  203. foreach_keyring_do() {
  204. local ACTION="$1"
  205. shift
  206. # if a --keyring was given, just work on this one
  207. if [ -n "$FORCED_KEYRING" ]; then
  208. $ACTION "$FORCED_KEYRING" "$@"
  209. else
  210. # otherwise all known keyrings are up for inspection
  211. if [ -s "$TRUSTEDFILE" ]; then
  212. $ACTION "$TRUSTEDFILE" "$@"
  213. fi
  214. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  215. eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
  216. if [ -d "$TRUSTEDPARTS" ]; then
  217. # strip / suffix as gpg will double-slash in that case (#665411)
  218. local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
  219. if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
  220. TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
  221. fi
  222. for trusted in $(find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -regex '^.*\.gpg$' | sort); do
  223. if [ -s "$trusted" ]; then
  224. $ACTION "$trusted" "$@"
  225. fi
  226. done
  227. fi
  228. fi
  229. }
  230. run_cmd_on_keyring() {
  231. local KEYRINGFILE="$1"
  232. shift
  233. # fingerprint and co will fail if key isn't in this keyring
  234. aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true
  235. }
  236. import_keyring_into_keyring() {
  237. local FROM="${1:-${GPGHOMEDIR}/pubring.gpg}"
  238. local TO="${2:-${GPGHOMEDIR}/pubring.gpg}"
  239. shift 2
  240. rm -f "${GPGHOMEDIR}/gpgoutput.log"
  241. # the idea is simple: We take keys from one keyring and copy it to another
  242. # we do this with so many checks in between to ensure that WE control the
  243. # creation, so we know that the (potentially) created $TO keyring is a
  244. # simple keyring rather than a keybox as gpg2 would create it which in turn
  245. # can't be read by gpgv.
  246. # BEWARE: This is designed more in the way to work with the current
  247. # callers, than to have a well defined it would be easy to add new callers to.
  248. if [ ! -s "$TO" ]; then
  249. if [ -s "$FROM" ]; then
  250. if [ -z "$2" ]; then
  251. if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
  252. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  253. false
  254. else
  255. chmod 0644 -- "$TO"
  256. fi
  257. else
  258. create_new_keyring "$TO"
  259. fi
  260. else
  261. create_new_keyring "$TO"
  262. fi
  263. elif [ -s "$FROM" ]; then
  264. local EXPORTLIMIT="$1"
  265. if [ -n "$1$2" ]; then shift; fi
  266. if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
  267. | aptkey_execute "$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  268. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  269. false
  270. fi
  271. fi
  272. }
  273. merge_all_trusted_keyrings_into_pubring() {
  274. # does the same as:
  275. # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
  276. # but without using gpg, just cat and find
  277. local PUBRING="${GPGHOMEDIR}/pubring.gpg"
  278. # if a --keyring was given, just use this one
  279. if [ -n "$FORCED_KEYRING" ]; then
  280. if [ -s "$FORCED_KEYRING" ]; then
  281. cp --dereference "$FORCED_KEYRING" "$PUBRING"
  282. fi
  283. else
  284. # otherwise all known keyrings are merged
  285. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  286. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  287. if [ -d "$TRUSTEDPARTS" ]; then
  288. # ignore errors mostly for non-existing $TRUSTEDFILE
  289. {
  290. cat "$TRUSTEDFILE" || true
  291. for parts in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do
  292. cat "$parts" || true
  293. done
  294. } > "$PUBRING" 2>/dev/null
  295. elif [ -s "$TRUSTEDFILE" ]; then
  296. cp --dereference "$TRUSTEDFILE" "$PUBRING"
  297. fi
  298. fi
  299. if [ ! -s "$PUBRING" ]; then
  300. touch "$PUBRING"
  301. fi
  302. }
  303. import_keys_from_keyring() {
  304. import_keyring_into_keyring "$1" "$2"
  305. }
  306. merge_keys_into_keyrings() {
  307. import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only'
  308. }
  309. merge_back_changes() {
  310. if [ -n "$FORCED_KEYRING" ]; then
  311. # if the keyring was forced merge is already done
  312. return
  313. fi
  314. if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
  315. # merge all updated keys
  316. foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
  317. fi
  318. # look for keys which were added or removed
  319. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
  320. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
  321. comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
  322. # key isn't part of new keyring, so remove
  323. cut -f 2 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  324. if [ -z "$key" ]; then continue; fi
  325. foreach_keyring_do 'remove_key_from_keyring' "$key"
  326. done
  327. # key is only part of new keyring, so we need to import it
  328. cut -f 1 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  329. if [ -z "$key" ]; then continue; fi
  330. import_keyring_into_keyring '' "$TRUSTEDFILE" "$key"
  331. done
  332. }
  333. setup_merged_keyring() {
  334. if [ -n "$FORCED_KEYID" ]; then
  335. merge_all_trusted_keyrings_into_pubring
  336. FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg"
  337. TRUSTEDFILE="${FORCED_KEYRING}"
  338. echo "#!/bin/sh
  339. exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  340. GPG="${GPGHOMEDIR}/gpg.1.sh"
  341. # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
  342. import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true
  343. elif [ -z "$FORCED_KEYRING" ]; then
  344. merge_all_trusted_keyrings_into_pubring
  345. if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
  346. cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  347. else
  348. touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  349. fi
  350. echo "#!/bin/sh
  351. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  352. GPG="${GPGHOMEDIR}/gpg.1.sh"
  353. else
  354. create_new_keyring "$TRUSTEDFILE"
  355. echo "#!/bin/sh
  356. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  357. GPG="${GPGHOMEDIR}/gpg.1.sh"
  358. fi
  359. }
  360. create_new_keyring() {
  361. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  362. if ! [ -e "$TRUSTEDFILE" ]; then
  363. if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
  364. touch -- "$TRUSTEDFILE"
  365. chmod 0644 -- "$TRUSTEDFILE"
  366. fi
  367. fi
  368. }
  369. aptkey_execute() { sh "$@"; }
  370. usage() {
  371. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  372. echo
  373. echo "Manage apt's list of trusted keys"
  374. echo
  375. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  376. echo " apt-key del <keyid> - remove the key <keyid>"
  377. echo " apt-key export <keyid> - output the key <keyid>"
  378. echo " apt-key exportall - output all trusted keys"
  379. echo " apt-key update - update keys using the keyring package"
  380. echo " apt-key net-update - update keys using the network"
  381. echo " apt-key list - list keys"
  382. echo " apt-key finger - list fingerprints"
  383. echo " apt-key adv - pass advanced options to gpg (download key)"
  384. echo
  385. echo "If no specific keyring file is given the command applies to all keyring files."
  386. }
  387. while [ -n "$1" ]; do
  388. case "$1" in
  389. --keyring)
  390. shift
  391. TRUSTEDFILE="$1"
  392. FORCED_KEYRING="$1"
  393. ;;
  394. --keyid)
  395. shift
  396. FORCED_KEYID="$1"
  397. ;;
  398. --secret-keyring)
  399. shift
  400. FORCED_SECRET_KEYRING="$1"
  401. ;;
  402. --readonly)
  403. merge_back_changes() { true; }
  404. create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; }
  405. ;;
  406. --fakeroot)
  407. requires_root() { true; }
  408. ;;
  409. --quiet)
  410. aptkey_echo() { true; }
  411. ;;
  412. --debug1)
  413. # some cmds like finger redirect stderr to /dev/null …
  414. aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; }
  415. ;;
  416. --debug2)
  417. # … other more complicated ones pipe gpg into gpg.
  418. aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; }
  419. ;;
  420. --*)
  421. echo >&2 "Unknown option: $1"
  422. usage
  423. exit 1;;
  424. *)
  425. break;;
  426. esac
  427. shift
  428. done
  429. if [ -z "$TRUSTEDFILE" ]; then
  430. TRUSTEDFILE="/etc/apt/trusted.gpg"
  431. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  432. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  433. fi
  434. command="$1"
  435. if [ -z "$command" ]; then
  436. usage
  437. exit 1
  438. fi
  439. shift
  440. create_gpg_home() {
  441. # gpg needs (in different versions more or less) files to function correctly,
  442. # so we give it its own homedir and generate some valid content for it later on
  443. if [ -n "$TMPDIR" ]; then
  444. # tmpdir is a directory and current user has rwx access to it
  445. # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
  446. if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
  447. unset TMPDIR
  448. fi
  449. fi
  450. GPGHOMEDIR="$(mktemp -d)"
  451. CURRENTTRAP="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';"
  452. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  453. chmod 700 "$GPGHOMEDIR"
  454. }
  455. prepare_gpg_home() {
  456. # crude detection if we are called from a maintainerscript where the
  457. # package depends on gnupg or not. We accept recommends here as
  458. # well as the script hopefully uses apt-key optionally then like e.g.
  459. # debian-archive-keyring for (upgrade) cleanup did
  460. if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  461. if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then
  462. cat >&2 <<EOF
  463. Warning: The $DPKG_MAINTSCRIPT_NAME maintainerscript of the package $DPKG_MAINTSCRIPT_PACKAGE
  464. Warning: seems to use apt-key (provided by apt) without depending on gnupg or gnupg2.
  465. Warning: This will BREAK in the future and should be fixed by the package maintainer(s).
  466. Note: Check first if apt-key functionality is needed at all - it probably isn't!
  467. EOF
  468. fi
  469. fi
  470. eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
  471. if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then
  472. true
  473. elif command_available 'gpg'; then
  474. GPG_EXE="gpg"
  475. elif command_available 'gpg2'; then
  476. GPG_EXE="gpg2"
  477. else
  478. echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
  479. echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
  480. echo >&2
  481. exit 255
  482. fi
  483. create_gpg_home
  484. # create the trustdb with an (empty) dummy keyring
  485. # older gpgs required it, newer gpgs even warn that it isn't needed,
  486. # but require it nonetheless for some commands, so we just play safe
  487. # here for the foreseeable future and create a dummy one
  488. touch "${GPGHOMEDIR}/empty.gpg"
  489. if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
  490. --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  491. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  492. false
  493. fi
  494. # now tell gpg that it shouldn't try to maintain this trustdb file
  495. echo "#!/bin/sh
  496. exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
  497. --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
  498. GPG_SH="${GPGHOMEDIR}/gpg.0.sh"
  499. GPG="$GPG_SH"
  500. # We don't usually need a secret keyring, of course, but
  501. # for advanced operations, we might really need a secret keyring after all
  502. if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
  503. if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  504. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  505. false
  506. fi
  507. else
  508. # and then, there are older versions of gpg which panic and implode
  509. # if there isn't one available - and writeable for imports
  510. # and even if not output is littered with the creation of a secring,
  511. # so lets call import once to have it create what it wants in silence
  512. echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true
  513. fi
  514. }
  515. if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
  516. prepare_gpg_home
  517. fi
  518. case "$command" in
  519. add)
  520. requires_root
  521. setup_merged_keyring
  522. aptkey_execute "$GPG" --quiet --batch --import "$@"
  523. merge_back_changes
  524. aptkey_echo "OK"
  525. ;;
  526. del|rm|remove)
  527. requires_root
  528. foreach_keyring_do 'remove_key_from_keyring' "$@"
  529. aptkey_echo "OK"
  530. ;;
  531. update)
  532. requires_root
  533. setup_merged_keyring
  534. update
  535. merge_back_changes
  536. ;;
  537. net-update)
  538. requires_root
  539. setup_merged_keyring
  540. net_update
  541. merge_back_changes
  542. ;;
  543. list)
  544. foreach_keyring_do 'run_cmd_on_keyring' --list-keys "$@"
  545. ;;
  546. finger*)
  547. foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@"
  548. ;;
  549. export|exportall)
  550. merge_all_trusted_keyrings_into_pubring
  551. aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
  552. ;;
  553. adv*)
  554. setup_merged_keyring
  555. aptkey_echo "Executing: $GPG $*"
  556. aptkey_execute "$GPG" "$@"
  557. merge_back_changes
  558. ;;
  559. verify)
  560. GPGV=''
  561. eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
  562. if [ -n "$GPGV" ] && command_available "$GPGV"; then true;
  563. elif command_available 'gpgv'; then GPGV='gpgv';
  564. elif command_available 'gpgv2'; then GPGV='gpgv2';
  565. else
  566. echo >&2 'ERROR: gpgv or gpgv2 required for verification'
  567. exit 29
  568. fi
  569. # for a forced keyid we need gpg --export, so full wrapping required
  570. if [ -n "$FORCED_KEYID" ]; then
  571. prepare_gpg_home
  572. else
  573. create_gpg_home
  574. fi
  575. setup_merged_keyring
  576. if [ -n "$FORCED_KEYRING" ]; then
  577. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
  578. else
  579. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"
  580. fi
  581. ;;
  582. help)
  583. usage
  584. ;;
  585. *)
  586. usage
  587. exit 1
  588. ;;
  589. esac