apt-key.in 22 KB

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