apt-key.in 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 [ -z "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then
  144. echo >&2 "Warning: 'apt-key update' is deprecated and should not be used anymore!"
  145. if [ -z "$ARCHIVE_KEYRING" ]; then
  146. echo >&2 "Note: In your distribution this command is a no-op and can therefore be removed safely."
  147. exit 0
  148. fi
  149. fi
  150. if [ ! -f "$ARCHIVE_KEYRING" ]; then
  151. echo >&2 "ERROR: Can't find the archive-keyring"
  152. echo >&2 "Is the &keyring-package; package installed?"
  153. exit 1
  154. fi
  155. # add new keys from the package;
  156. # we do not use add_keys_with_verify_against_master_keyring here,
  157. # because "update" is run on regular package updates. A
  158. # attacker might as well replace the master-archive-keyring file
  159. # in the package and add his own keys. so this check wouldn't
  160. # add any security. we *need* this check on net-update though
  161. import_keyring_into_keyring "$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
  162. if [ -r "$REMOVED_KEYS" ]; then
  163. # remove no-longer supported/used keys
  164. get_fingerprints_of_keyring "$REMOVED_KEYS" | while read key; do
  165. foreach_keyring_do 'remove_key_from_keyring' "$key"
  166. done
  167. else
  168. echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
  169. fi
  170. }
  171. remove_key_from_keyring() {
  172. local KEYRINGFILE="$1"
  173. shift
  174. # non-existent keyrings have by definition no keys
  175. if [ ! -e "$KEYRINGFILE" ]; then
  176. return
  177. fi
  178. for KEY in "$@"; do
  179. local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst"
  180. get_fingerprints_of_keyring "$KEYRINGFILE" > "$FINGERPRINTS"
  181. # strip leading 0x, if present:
  182. KEY="$(echo "${KEY#0x}" | tr -d ' ')"
  183. # check if the key is in this keyring
  184. if ! grep -iq "^[0-9A-F]*${KEY}$" "$FINGERPRINTS"; then
  185. continue
  186. fi
  187. if [ ! -w "$KEYRINGFILE" ]; then
  188. echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
  189. continue
  190. fi
  191. # check if it is the only key in the keyring and if so remove the keyring altogether
  192. if [ '1' = "$(uniq "$FINGERPRINTS" | wc -l)" ]; then
  193. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
  194. return
  195. fi
  196. # we can't just modify pointed to files as these might be in /usr or something
  197. local REALTARGET
  198. if [ -L "$KEYRINGFILE" ]; then
  199. REALTARGET="$(readlink -f "$KEYRINGFILE")"
  200. mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
  201. cp -a "$REALTARGET" "$KEYRINGFILE"
  202. fi
  203. # delete the key from the keyring
  204. aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch --delete-keys --yes "$KEY"
  205. if [ -n "$REALTARGET" ]; then
  206. # the real backup is the old link, not the copy we made
  207. mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
  208. fi
  209. done
  210. }
  211. foreach_keyring_do() {
  212. local ACTION="$1"
  213. shift
  214. # if a --keyring was given, just work on this one
  215. if [ -n "$FORCED_KEYRING" ]; then
  216. $ACTION "$FORCED_KEYRING" "$@"
  217. else
  218. # otherwise all known keyrings are up for inspection
  219. if [ -s "$TRUSTEDFILE" ]; then
  220. $ACTION "$TRUSTEDFILE" "$@"
  221. fi
  222. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  223. eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
  224. if [ -d "$TRUSTEDPARTS" ]; then
  225. TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")"
  226. local TRUSTEDPARTSLIST="$(cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg')"
  227. for trusted in $(echo "$TRUSTEDPARTSLIST" | sort); do
  228. if [ -s "$trusted" ]; then
  229. $ACTION "$trusted" "$@"
  230. fi
  231. done
  232. fi
  233. fi
  234. }
  235. run_cmd_on_keyring() {
  236. local KEYRINGFILE="$1"
  237. shift
  238. # fingerprint and co will fail if key isn't in this keyring
  239. aptkey_execute "$GPG_SH" --keyring "$KEYRINGFILE" --batch "$@" 2>/dev/null || true
  240. }
  241. import_keyring_into_keyring() {
  242. local FROM="${1:-${GPGHOMEDIR}/pubring.gpg}"
  243. local TO="${2:-${GPGHOMEDIR}/pubring.gpg}"
  244. shift 2
  245. rm -f "${GPGHOMEDIR}/gpgoutput.log"
  246. # the idea is simple: We take keys from one keyring and copy it to another
  247. # we do this with so many checks in between to ensure that WE control the
  248. # creation, so we know that the (potentially) created $TO keyring is a
  249. # simple keyring rather than a keybox as gpg2 would create it which in turn
  250. # can't be read by gpgv.
  251. # BEWARE: This is designed more in the way to work with the current
  252. # callers, than to have a well defined it would be easy to add new callers to.
  253. if [ ! -s "$TO" ]; then
  254. if [ -s "$FROM" ]; then
  255. if [ -z "$2" ]; then
  256. if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${1:+"$1"} > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
  257. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  258. false
  259. else
  260. chmod 0644 -- "$TO"
  261. fi
  262. else
  263. create_new_keyring "$TO"
  264. fi
  265. else
  266. create_new_keyring "$TO"
  267. fi
  268. elif [ -s "$FROM" ]; then
  269. local EXPORTLIMIT="$1"
  270. if [ -n "$1$2" ]; then shift; fi
  271. if ! aptkey_execute "$GPG_SH" --keyring "$FROM" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
  272. | aptkey_execute "$GPG_SH" --keyring "$TO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  273. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  274. false
  275. fi
  276. fi
  277. }
  278. merge_all_trusted_keyrings_into_pubring() {
  279. # does the same as:
  280. # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
  281. # but without using gpg, just cat and find
  282. local PUBRING="$(readlink -f "${GPGHOMEDIR}/pubring.gpg")"
  283. # if a --keyring was given, just use this one
  284. if [ -n "$FORCED_KEYRING" ]; then
  285. if [ -s "$FORCED_KEYRING" ]; then
  286. cp --dereference "$FORCED_KEYRING" "$PUBRING"
  287. fi
  288. else
  289. # otherwise all known keyrings are merged
  290. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  291. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  292. if [ -d "$TRUSTEDPARTS" ]; then
  293. rm -f "$PUBRING"
  294. if [ -s "$TRUSTEDFILE" ]; then
  295. cat "$TRUSTEDFILE" > "$PUBRING"
  296. fi
  297. TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")"
  298. (cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 -name '*.gpg' -exec cat {} + >> "$PUBRING";)
  299. elif [ -s "$TRUSTEDFILE" ]; then
  300. cp --dereference "$TRUSTEDFILE" "$PUBRING"
  301. fi
  302. fi
  303. if [ ! -s "$PUBRING" ]; then
  304. touch "$PUBRING"
  305. fi
  306. }
  307. import_keys_from_keyring() {
  308. import_keyring_into_keyring "$1" "$2"
  309. }
  310. merge_keys_into_keyrings() {
  311. import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only'
  312. }
  313. merge_back_changes() {
  314. if [ -n "$FORCED_KEYRING" ]; then
  315. # if the keyring was forced merge is already done
  316. return
  317. fi
  318. if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
  319. # merge all updated keys
  320. foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
  321. fi
  322. # look for keys which were added or removed
  323. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
  324. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
  325. comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
  326. # key isn't part of new keyring, so remove
  327. cut -f 2 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  328. if [ -z "$key" ]; then continue; fi
  329. foreach_keyring_do 'remove_key_from_keyring' "$key"
  330. done
  331. # key is only part of new keyring, so we need to import it
  332. cut -f 1 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  333. if [ -z "$key" ]; then continue; fi
  334. import_keyring_into_keyring '' "$TRUSTEDFILE" "$key"
  335. done
  336. }
  337. setup_merged_keyring() {
  338. if [ -n "$FORCED_KEYID" ]; then
  339. merge_all_trusted_keyrings_into_pubring
  340. FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg"
  341. TRUSTEDFILE="${FORCED_KEYRING}"
  342. echo "#!/bin/sh
  343. exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  344. GPG="${GPGHOMEDIR}/gpg.1.sh"
  345. # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
  346. import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true
  347. elif [ -z "$FORCED_KEYRING" ]; then
  348. merge_all_trusted_keyrings_into_pubring
  349. if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
  350. cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  351. else
  352. touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  353. fi
  354. echo "#!/bin/sh
  355. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  356. GPG="${GPGHOMEDIR}/gpg.1.sh"
  357. else
  358. create_new_keyring "$TRUSTEDFILE"
  359. echo "#!/bin/sh
  360. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  361. GPG="${GPGHOMEDIR}/gpg.1.sh"
  362. fi
  363. }
  364. create_new_keyring() {
  365. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  366. if ! [ -e "$TRUSTEDFILE" ]; then
  367. if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
  368. touch -- "$TRUSTEDFILE"
  369. chmod 0644 -- "$TRUSTEDFILE"
  370. fi
  371. fi
  372. }
  373. aptkey_execute() { sh "$@"; }
  374. usage() {
  375. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  376. echo
  377. echo "Manage apt's list of trusted keys"
  378. echo
  379. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  380. echo " apt-key del <keyid> - remove the key <keyid>"
  381. echo " apt-key export <keyid> - output the key <keyid>"
  382. echo " apt-key exportall - output all trusted keys"
  383. echo " apt-key update - update keys using the keyring package"
  384. echo " apt-key net-update - update keys using the network"
  385. echo " apt-key list - list keys"
  386. echo " apt-key finger - list fingerprints"
  387. echo " apt-key adv - pass advanced options to gpg (download key)"
  388. echo
  389. echo "If no specific keyring file is given the command applies to all keyring files."
  390. }
  391. while [ -n "$1" ]; do
  392. case "$1" in
  393. --keyring)
  394. shift
  395. TRUSTEDFILE="$1"
  396. FORCED_KEYRING="$1"
  397. ;;
  398. --keyid)
  399. shift
  400. FORCED_KEYID="$1"
  401. ;;
  402. --secret-keyring)
  403. shift
  404. FORCED_SECRET_KEYRING="$1"
  405. ;;
  406. --readonly)
  407. merge_back_changes() { true; }
  408. create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; }
  409. ;;
  410. --fakeroot)
  411. requires_root() { true; }
  412. ;;
  413. --quiet)
  414. aptkey_echo() { true; }
  415. ;;
  416. --debug1)
  417. # some cmds like finger redirect stderr to /dev/null …
  418. aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; }
  419. ;;
  420. --debug2)
  421. # … other more complicated ones pipe gpg into gpg.
  422. aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; }
  423. ;;
  424. --*)
  425. echo >&2 "Unknown option: $1"
  426. usage
  427. exit 1;;
  428. *)
  429. break;;
  430. esac
  431. shift
  432. done
  433. if [ -z "$TRUSTEDFILE" ]; then
  434. TRUSTEDFILE="/etc/apt/trusted.gpg"
  435. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  436. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  437. fi
  438. command="$1"
  439. if [ -z "$command" ]; then
  440. usage
  441. exit 1
  442. fi
  443. shift
  444. cleanup_gpg_home() {
  445. if [ -z "$GPGHOMEDIR" ]; then return; fi
  446. if command_available 'gpgconf'; then
  447. GNUPGHOME="${GPGHOMEDIR}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true
  448. fi
  449. rm -rf "$GPGHOMEDIR"
  450. }
  451. create_gpg_home() {
  452. # gpg needs (in different versions more or less) files to function correctly,
  453. # so we give it its own homedir and generate some valid content for it later on
  454. if [ -n "$TMPDIR" ]; then
  455. # tmpdir is a directory and current user has rwx access to it
  456. # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
  457. if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
  458. unset TMPDIR
  459. fi
  460. fi
  461. GPGHOMEDIR="$(mktemp -d)"
  462. CURRENTTRAP="${CURRENTTRAP} cleanup_gpg_home;"
  463. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  464. if [ -z "$GPGHOMEDIR" ]; then
  465. echo "ERROR: Could not create temporary gpg home directory in apt-key ($TMPDIR)"
  466. exit 28
  467. fi
  468. chmod 700 "$GPGHOMEDIR"
  469. }
  470. prepare_gpg_home() {
  471. # crude detection if we are called from a maintainerscript where the
  472. # package depends on gnupg or not. We accept recommends here as
  473. # well as the script hopefully uses apt-key optionally then like e.g.
  474. # debian-archive-keyring for (upgrade) cleanup did
  475. if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ] && [ -z "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then
  476. if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then
  477. cat >&2 <<EOF
  478. Warning: The $DPKG_MAINTSCRIPT_NAME maintainerscript of the package $DPKG_MAINTSCRIPT_PACKAGE
  479. Warning: seems to use apt-key (provided by apt) without depending on gnupg or gnupg2.
  480. Warning: This will BREAK in the future and should be fixed by the package maintainer(s).
  481. Note: Check first if apt-key functionality is needed at all - it probably isn't!
  482. EOF
  483. fi
  484. fi
  485. eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
  486. if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then
  487. true
  488. elif command_available 'gpg'; then
  489. GPG_EXE="gpg"
  490. elif command_available 'gpg2'; then
  491. GPG_EXE="gpg2"
  492. elif command_available 'gpg1'; then
  493. GPG_EXE="gpg1"
  494. else
  495. echo >&2 "Error: gnupg, gnupg2 and gnupg1 do not seem to be installed,"
  496. echo >&2 "Error: but apt-key requires gnupg, gnupg2 or gnupg1 for this operation."
  497. echo >&2
  498. exit 255
  499. fi
  500. create_gpg_home
  501. # create the trustdb with an (empty) dummy keyring
  502. # older gpgs required it, newer gpgs even warn that it isn't needed,
  503. # but require it nonetheless for some commands, so we just play safe
  504. # here for the foreseeable future and create a dummy one
  505. touch "${GPGHOMEDIR}/empty.gpg"
  506. if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
  507. --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  508. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  509. false
  510. fi
  511. # now tell gpg that it shouldn't try to maintain this trustdb file
  512. echo "#!/bin/sh
  513. exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
  514. --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
  515. GPG_SH="${GPGHOMEDIR}/gpg.0.sh"
  516. GPG="$GPG_SH"
  517. # We don't usually need a secret keyring, of course, but
  518. # for advanced operations, we might really need a secret keyring after all
  519. if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
  520. if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  521. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  522. false
  523. fi
  524. else
  525. # and then, there are older versions of gpg which panic and implode
  526. # if there isn't one available - and writeable for imports
  527. # and even if not output is littered with the creation of a secring,
  528. # so lets call import once to have it create what it wants in silence
  529. echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true
  530. fi
  531. }
  532. warn_on_script_usage() {
  533. if [ -n "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then
  534. return
  535. fi
  536. # (Maintainer) scripts should not be using apt-key
  537. if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  538. echo >&2 "Warning: apt-key should not be used in scripts (called from $DPKG_MAINTSCRIPT_NAME maintainerscript of the package ${DPKG_MAINTSCRIPT_PACKAGE})"
  539. elif [ ! -t 1 ]; then
  540. echo >&2 "Warning: apt-key output should not be parsed (stdout is not a terminal)"
  541. fi
  542. }
  543. if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
  544. prepare_gpg_home
  545. fi
  546. case "$command" in
  547. add)
  548. warn_on_script_usage
  549. requires_root
  550. setup_merged_keyring
  551. aptkey_execute "$GPG" --quiet --batch --import "$@"
  552. merge_back_changes
  553. aptkey_echo "OK"
  554. ;;
  555. del|rm|remove)
  556. # no script warning here as removing 'add' usage needs 'del' for cleanup
  557. requires_root
  558. foreach_keyring_do 'remove_key_from_keyring' "$@"
  559. aptkey_echo "OK"
  560. ;;
  561. update)
  562. warn_on_script_usage
  563. requires_root
  564. setup_merged_keyring
  565. update
  566. merge_back_changes
  567. ;;
  568. net-update)
  569. requires_root
  570. setup_merged_keyring
  571. net_update
  572. merge_back_changes
  573. ;;
  574. list|finger*)
  575. warn_on_script_usage
  576. foreach_keyring_do 'run_cmd_on_keyring' --fingerprint "$@"
  577. ;;
  578. export|exportall)
  579. warn_on_script_usage
  580. merge_all_trusted_keyrings_into_pubring
  581. aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
  582. ;;
  583. adv*)
  584. warn_on_script_usage
  585. setup_merged_keyring
  586. aptkey_echo "Executing: $GPG $*"
  587. aptkey_execute "$GPG" "$@"
  588. merge_back_changes
  589. ;;
  590. verify)
  591. GPGV=''
  592. eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
  593. if [ -n "$GPGV" ] && command_available "$GPGV"; then true;
  594. elif command_available 'gpgv'; then GPGV='gpgv';
  595. elif command_available 'gpgv2'; then GPGV='gpgv2';
  596. elif command_available 'gpgv1'; then GPGV='gpgv1';
  597. else
  598. echo >&2 'ERROR: gpgv, gpgv2 or gpgv1 required for verification'
  599. exit 29
  600. fi
  601. # for a forced keyid we need gpg --export, so full wrapping required
  602. if [ -n "$FORCED_KEYID" ]; then
  603. prepare_gpg_home
  604. else
  605. create_gpg_home
  606. fi
  607. setup_merged_keyring
  608. if [ -n "$FORCED_KEYRING" ]; then
  609. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${FORCED_KEYRING}" --ignore-time-conflict "$@"
  610. else
  611. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"
  612. fi
  613. ;;
  614. help)
  615. usage
  616. ;;
  617. *)
  618. usage
  619. exit 1
  620. ;;
  621. esac