apt-key.in 19 KB

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