apt-key.in 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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. # check if the key is in this keyring
  174. if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
  175. continue
  176. fi
  177. if [ ! -w "$KEYRINGFILE" ]; then
  178. echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
  179. continue
  180. fi
  181. # check if it is the only key in the keyring and if so remove the keyring altogether
  182. if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
  183. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
  184. return
  185. fi
  186. # we can't just modify pointed to files as these might be in /usr or something
  187. local REALTARGET
  188. if [ -L "$KEYRINGFILE" ]; then
  189. REALTARGET="$(readlink -f "$KEYRINGFILE")"
  190. mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
  191. cp -a "$REALTARGET" "$KEYRINGFILE"
  192. fi
  193. # delete the key from the keyring
  194. aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
  195. if [ -n "$REALTARGET" ]; then
  196. # the real backup is the old link, not the copy we made
  197. mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
  198. fi
  199. done
  200. }
  201. foreach_keyring_do() {
  202. local ACTION="$1"
  203. shift
  204. # if a --keyring was given, just work on this one
  205. if [ -n "$FORCED_KEYRING" ]; then
  206. $ACTION "$FORCED_KEYRING" "$@"
  207. else
  208. # otherwise all known keyrings are up for inspection
  209. if [ -s "$TRUSTEDFILE" ]; then
  210. $ACTION "$TRUSTEDFILE" "$@"
  211. fi
  212. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  213. eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
  214. if [ -d "$TRUSTEDPARTS" ]; then
  215. # strip / suffix as gpg will double-slash in that case (#665411)
  216. local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
  217. if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
  218. TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
  219. fi
  220. for trusted in $(find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -regex '^.*\.gpg$' | 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="${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. # ignore errors mostly for non-existing $TRUSTEDFILE
  287. {
  288. cat "$TRUSTEDFILE" || true
  289. for parts in $(find -L "$TRUSTEDPARTS" -type f -name '*.gpg'); do
  290. cat "$parts" || true
  291. done
  292. } > "$PUBRING" 2>/dev/null
  293. elif [ -s "$TRUSTEDFILE" ]; then
  294. cp --dereference "$TRUSTEDFILE" "$PUBRING"
  295. fi
  296. fi
  297. if [ ! -s "$PUBRING" ]; then
  298. touch "$PUBRING"
  299. fi
  300. }
  301. import_keys_from_keyring() {
  302. import_keyring_into_keyring "$1" "$2"
  303. }
  304. merge_keys_into_keyrings() {
  305. import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only'
  306. }
  307. merge_back_changes() {
  308. if [ -n "$FORCED_KEYRING" ]; then
  309. # if the keyring was forced merge is already done
  310. return
  311. fi
  312. if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
  313. # merge all updated keys
  314. foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
  315. fi
  316. # look for keys which were added or removed
  317. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
  318. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
  319. comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
  320. # key isn't part of new keyring, so remove
  321. cut -f 2 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  322. if [ -z "$key" ]; then continue; fi
  323. foreach_keyring_do 'remove_key_from_keyring' "$key"
  324. done
  325. # key is only part of new keyring, so we need to import it
  326. cut -f 1 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  327. if [ -z "$key" ]; then continue; fi
  328. import_keyring_into_keyring '' "$TRUSTEDFILE" "$key"
  329. done
  330. }
  331. setup_merged_keyring() {
  332. if [ -n "$FORCED_KEYID" ]; then
  333. merge_all_trusted_keyrings_into_pubring
  334. FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg"
  335. TRUSTEDFILE="${FORCED_KEYRING}"
  336. echo "#!/bin/sh
  337. exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  338. GPG="${GPGHOMEDIR}/gpg.1.sh"
  339. # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
  340. import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true
  341. elif [ -z "$FORCED_KEYRING" ]; then
  342. merge_all_trusted_keyrings_into_pubring
  343. if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
  344. cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  345. else
  346. touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  347. fi
  348. echo "#!/bin/sh
  349. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  350. GPG="${GPGHOMEDIR}/gpg.1.sh"
  351. else
  352. create_new_keyring "$TRUSTEDFILE"
  353. echo "#!/bin/sh
  354. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  355. GPG="${GPGHOMEDIR}/gpg.1.sh"
  356. fi
  357. }
  358. create_new_keyring() {
  359. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  360. if ! [ -e "$TRUSTEDFILE" ]; then
  361. if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
  362. touch -- "$TRUSTEDFILE"
  363. chmod 0644 -- "$TRUSTEDFILE"
  364. fi
  365. fi
  366. }
  367. aptkey_execute() { sh "$@"; }
  368. usage() {
  369. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  370. echo
  371. echo "Manage apt's list of trusted keys"
  372. echo
  373. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  374. echo " apt-key del <keyid> - remove the key <keyid>"
  375. echo " apt-key export <keyid> - output the key <keyid>"
  376. echo " apt-key exportall - output all trusted keys"
  377. echo " apt-key update - update keys using the keyring package"
  378. echo " apt-key net-update - update keys using the network"
  379. echo " apt-key list - list keys"
  380. echo " apt-key finger - list fingerprints"
  381. echo " apt-key adv - pass advanced options to gpg (download key)"
  382. echo
  383. echo "If no specific keyring file is given the command applies to all keyring files."
  384. }
  385. while [ -n "$1" ]; do
  386. case "$1" in
  387. --keyring)
  388. shift
  389. TRUSTEDFILE="$1"
  390. FORCED_KEYRING="$1"
  391. ;;
  392. --keyid)
  393. shift
  394. FORCED_KEYID="$1"
  395. ;;
  396. --secret-keyring)
  397. shift
  398. FORCED_SECRET_KEYRING="$1"
  399. ;;
  400. --readonly)
  401. merge_back_changes() { true; }
  402. create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; }
  403. ;;
  404. --fakeroot)
  405. requires_root() { true; }
  406. ;;
  407. --quiet)
  408. aptkey_echo() { true; }
  409. ;;
  410. --debug1)
  411. # some cmds like finger redirect stderr to /dev/null …
  412. aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; }
  413. ;;
  414. --debug2)
  415. # … other more complicated ones pipe gpg into gpg.
  416. aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; }
  417. ;;
  418. --*)
  419. echo >&2 "Unknown option: $1"
  420. usage
  421. exit 1;;
  422. *)
  423. break;;
  424. esac
  425. shift
  426. done
  427. if [ -z "$TRUSTEDFILE" ]; then
  428. TRUSTEDFILE="/etc/apt/trusted.gpg"
  429. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  430. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  431. fi
  432. command="$1"
  433. if [ -z "$command" ]; then
  434. usage
  435. exit 1
  436. fi
  437. shift
  438. create_gpg_home() {
  439. # gpg needs (in different versions more or less) files to function correctly,
  440. # so we give it its own homedir and generate some valid content for it later on
  441. if [ -n "$TMPDIR" ]; then
  442. # tmpdir is a directory and current user has rwx access to it
  443. # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
  444. if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
  445. unset TMPDIR
  446. fi
  447. fi
  448. GPGHOMEDIR="$(mktemp -d)"
  449. CURRENTTRAP="${CURRENTTRAP} rm -rf '$(escape_shell "${GPGHOMEDIR}")';"
  450. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  451. chmod 700 "$GPGHOMEDIR"
  452. }
  453. prepare_gpg_home() {
  454. eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
  455. if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then
  456. true
  457. elif command_available 'gpg'; then
  458. GPG_EXE="gpg"
  459. elif command_available 'gpg2'; then
  460. GPG_EXE="gpg2"
  461. else
  462. echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
  463. echo >&2 "Error: but apt-key requires gnupg or gnupg2 for this operation."
  464. echo >&2
  465. exit 255
  466. fi
  467. create_gpg_home
  468. # create the trustdb with an (empty) dummy keyring
  469. # older gpgs required it, newer gpgs even warn that it isn't needed,
  470. # but require it nonetheless for some commands, so we just play safe
  471. # here for the foreseeable future and create a dummy one
  472. touch "${GPGHOMEDIR}/empty.gpg"
  473. if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
  474. --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  475. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  476. false
  477. fi
  478. # now tell gpg that it shouldn't try to maintain this trustdb file
  479. echo "#!/bin/sh
  480. exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
  481. --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
  482. GPG_SH="${GPGHOMEDIR}/gpg.0.sh"
  483. GPG="$GPG_SH"
  484. # We don't usually need a secret keyring, of course, but
  485. # for advanced operations, we might really need a secret keyring after all
  486. if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
  487. if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  488. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  489. false
  490. fi
  491. else
  492. # and then, there are older versions of gpg which panic and implode
  493. # if there isn't one available - and writeable for imports
  494. # and even if not output is littered with the creation of a secring,
  495. # so lets call import once to have it create what it wants in silence
  496. echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true
  497. fi
  498. }
  499. if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
  500. prepare_gpg_home
  501. fi
  502. case "$command" in
  503. add)
  504. requires_root
  505. setup_merged_keyring
  506. aptkey_execute "$GPG" --quiet --batch --import "$@"
  507. merge_back_changes
  508. aptkey_echo "OK"
  509. ;;
  510. del|rm|remove)
  511. requires_root
  512. foreach_keyring_do 'remove_key_from_keyring' "$@"
  513. aptkey_echo "OK"
  514. ;;
  515. update)
  516. requires_root
  517. setup_merged_keyring
  518. update
  519. merge_back_changes
  520. ;;
  521. net-update)
  522. requires_root
  523. setup_merged_keyring
  524. net_update
  525. merge_back_changes
  526. ;;
  527. list)
  528. foreach_keyring_do 'run_cmd_on_keyring' --list-keys "$@"
  529. ;;
  530. finger*)
  531. foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@"
  532. ;;
  533. export|exportall)
  534. merge_all_trusted_keyrings_into_pubring
  535. aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
  536. ;;
  537. adv*)
  538. setup_merged_keyring
  539. aptkey_echo "Executing: $GPG $*"
  540. aptkey_execute "$GPG" "$@"
  541. merge_back_changes
  542. ;;
  543. verify)
  544. GPGV=''
  545. eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
  546. if [ -n "$GPGV" ] && command_available "$GPGV"; then true;
  547. elif command_available 'gpgv'; then GPGV='gpgv';
  548. elif command_available 'gpgv2'; then GPGV='gpgv2';
  549. else
  550. echo >&2 'ERROR: gpgv or gpgv2 required for verification'
  551. exit 29
  552. fi
  553. # for a forced keyid we need gpg --export, so full wrapping required
  554. if [ -n "$FORCED_KEYID" ]; then
  555. prepare_gpg_home
  556. else
  557. create_gpg_home
  558. fi
  559. setup_merged_keyring
  560. if [ -n "$FORCED_KEYRING" ]; then
  561. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
  562. else
  563. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"
  564. fi
  565. ;;
  566. help)
  567. usage
  568. ;;
  569. *)
  570. usage
  571. exit 1
  572. ;;
  573. esac