apt-key.in 20 KB

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