apt-key.in 21 KB

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