apt-key.in 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. apt_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. apt_error "Keyring '$ADD_KEYRING' to be added not found"
  56. return
  57. fi
  58. if [ ! -f "$MASTER" ]; then
  59. apt_error "Master-Keyring '$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. apt_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. apt_error 'wget is required for a network-based update, but it is not installed'
  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. apt_error "Can't find the archive-keyring (Is the &keyring-package; package installed?)"
  152. exit 1
  153. fi
  154. # add new keys from the package;
  155. # we do not use add_keys_with_verify_against_master_keyring here,
  156. # because "update" is run on regular package updates. A
  157. # attacker might as well replace the master-archive-keyring file
  158. # in the package and add his own keys. so this check wouldn't
  159. # add any security. we *need* this check on net-update though
  160. import_keyring_into_keyring "$ARCHIVE_KEYRING" '' && cat "${GPGHOMEDIR}/gpgoutput.log"
  161. if [ -r "$REMOVED_KEYS" ]; then
  162. # remove no-longer supported/used keys
  163. get_fingerprints_of_keyring "$(dearmor_filename "$REMOVED_KEYS")" | while read key; do
  164. foreach_keyring_do 'remove_key_from_keyring' "$key"
  165. done
  166. else
  167. apt_warn "Removed keys keyring '$REMOVED_KEYS' missing or not readable"
  168. fi
  169. }
  170. remove_key_from_keyring() {
  171. local KEYRINGFILE="$1"
  172. shift
  173. # non-existent keyrings have by definition no keys
  174. if [ ! -e "$KEYRINGFILE" ]; then
  175. return
  176. fi
  177. local FINGERPRINTS="${GPGHOMEDIR}/keyringfile.keylst"
  178. local DEARMOR="$(dearmor_filename "$KEYRINGFILE")"
  179. get_fingerprints_of_keyring "$DEARMOR" > "$FINGERPRINTS"
  180. for KEY in "$@"; do
  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. apt_warn "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 "$DEARMOR" ]; then
  199. REALTARGET="$(readlink -f "$DEARMOR")"
  200. mv -f "$DEARMOR" "${DEARMOR}.dpkg-tmp"
  201. cp -a "$REALTARGET" "$DEARMOR"
  202. fi
  203. # delete the key from the keyring
  204. aptkey_execute "$GPG_SH" --keyring "$DEARMOR" --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 "${DEARMOR}.dpkg-tmp" "${DEARMOR}~"
  208. fi
  209. if [ "$DEARMOR" != "$KEYRINGFILE" ]; then
  210. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~"
  211. create_new_keyring "$KEYRINGFILE"
  212. aptkey_execute "$GPG_SH" --keyring "$DEARMOR" --armor --export > "$KEYRINGFILE"
  213. fi
  214. get_fingerprints_of_keyring "$DEARMOR" > "$FINGERPRINTS"
  215. done
  216. }
  217. accessible_file_exists() {
  218. if ! test -s "$1"; then
  219. return 1
  220. fi
  221. if test -r "$1"; then
  222. return 0
  223. fi
  224. apt_warn "The key(s) in the keyring $1 are ignored as the file is not readable by user '$USER' executing apt-key."
  225. return 1
  226. }
  227. foreach_keyring_do() {
  228. local ACTION="$1"
  229. shift
  230. # if a --keyring was given, just work on this one
  231. if [ -n "$FORCED_KEYRING" ]; then
  232. $ACTION "$TRUSTEDFILE" "$@"
  233. else
  234. # otherwise all known keyrings are up for inspection
  235. if accessible_file_exists "$TRUSTEDFILE"; then
  236. $ACTION "$TRUSTEDFILE" "$@"
  237. fi
  238. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  239. eval "$(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)"
  240. if [ -d "$TRUSTEDPARTS" ]; then
  241. TRUSTEDPARTS="$(readlink -f "$TRUSTEDPARTS")"
  242. local TRUSTEDPARTSLIST="$(cd /; find "$TRUSTEDPARTS" -mindepth 1 -maxdepth 1 \( -name '*.gpg' -o -name '*.asc' \))"
  243. for trusted in $(echo "$TRUSTEDPARTSLIST" | sort); do
  244. if accessible_file_exists "$trusted"; then
  245. $ACTION "$trusted" "$@"
  246. fi
  247. done
  248. fi
  249. fi
  250. }
  251. list_keys_in_keyring() {
  252. local KEYRINGFILE="$1"
  253. shift
  254. # fingerprint and co will fail if key isn't in this keyring
  255. aptkey_execute "$GPG_SH" --keyring "$(dearmor_filename "$KEYRINGFILE")" "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2> "${GPGHOMEDIR}/gpgoutput.err" || true
  256. if [ ! -s "${GPGHOMEDIR}/gpgoutput.log" ]; then
  257. return
  258. fi
  259. # we fake gpg header here to refer to the real asc file rather than a temp file
  260. if [ "${KEYRINGFILE##*.}" = 'asc' ]; then
  261. if expr match "$(sed -n '2p' "${GPGHOMEDIR}/gpgoutput.log")" '^-\+$' >/dev/null 2>&1; then
  262. echo "$KEYRINGFILE"
  263. echo "$KEYRINGFILE" | sed 's#[^-]#-#g'
  264. sed '1,2d' "${GPGHOMEDIR}/gpgoutput.log" || true
  265. else
  266. cat "${GPGHOMEDIR}/gpgoutput.log"
  267. fi
  268. else
  269. cat "${GPGHOMEDIR}/gpgoutput.log"
  270. fi
  271. if [ -s "${GPGHOMEDIR}/gpgoutput.err" ]; then
  272. cat >&2 "${GPGHOMEDIR}/gpgoutput.err"
  273. fi
  274. }
  275. export_key_from_to() {
  276. local FROM="$1"
  277. local TO="$2"
  278. shift 2
  279. if ! aptkey_execute "$GPG_SH" --keyring "$(dearmor_filename "$FROM")" --export "$@" > "$TO" 2> "${GPGHOMEDIR}/gpgoutput.log"; then
  280. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  281. false
  282. else
  283. chmod 0644 -- "$TO"
  284. fi
  285. }
  286. import_keyring_into_keyring() {
  287. local FROM="${1:-${GPGHOMEDIR}/pubring.gpg}"
  288. local TO="${2:-${GPGHOMEDIR}/pubring.gpg}"
  289. shift 2
  290. rm -f "${GPGHOMEDIR}/gpgoutput.log"
  291. # the idea is simple: We take keys from one keyring and copy it to another
  292. # we do this with so many checks in between to ensure that WE control the
  293. # creation, so we know that the (potentially) created $TO keyring is a
  294. # simple keyring rather than a keybox as gpg2 would create it which in turn
  295. # can't be read by gpgv.
  296. # BEWARE: This is designed more in the way to work with the current
  297. # callers, than to have a well defined it would be easy to add new callers to.
  298. if [ ! -s "$TO" ]; then
  299. if [ -s "$FROM" ]; then
  300. if [ -z "$2" ]; then
  301. local OPTS
  302. if [ "${TO##*.}" = 'asc' ]; then
  303. OPTS='--armor'
  304. fi
  305. export_key_from_to "$(dearmor_filename "$FROM")" "$TO" $OPTS ${1:+"$1"}
  306. else
  307. create_new_keyring "$TO"
  308. fi
  309. else
  310. create_new_keyring "$TO"
  311. fi
  312. elif [ -s "$FROM" ]; then
  313. local EXPORTLIMIT="$1"
  314. if [ -n "$1$2" ]; then shift; fi
  315. local DEARMORTO="$(dearmor_filename "$TO")"
  316. if ! aptkey_execute "$GPG_SH" --keyring "$(dearmor_filename "$FROM")" --export ${EXPORTLIMIT:+"$EXPORTLIMIT"} \
  317. | aptkey_execute "$GPG_SH" --keyring "$DEARMORTO" --batch --import "$@" > "${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  318. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  319. false
  320. fi
  321. if [ "$DEARMORTO" != "$TO" ]; then
  322. export_key_from_to "$DEARMORTO" "${DEARMORTO}.asc" --armor
  323. if ! cmp -s "$TO" "${DEARMORTO}.asc" 2>/dev/null; then
  324. cp -a "$TO" "${TO}~"
  325. mv -f "${DEARMORTO}.asc" "$TO"
  326. fi
  327. fi
  328. fi
  329. }
  330. dearmor_keyring() {
  331. # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=831409#67
  332. # The awk script is more complex through to skip surrounding garbage and
  333. # to support multiple keys in one file (old gpgs generate version headers
  334. # which get printed with the original and hence result in garbage input for base64
  335. awk '/^-----BEGIN/{ x = 1; }
  336. /^$/{ if (x == 1) { x = 2; }; }
  337. /^[^=-]/{ if (x == 2) { print $0; }; }
  338. /^-----END/{ x = 0; }' | base64 -d
  339. }
  340. dearmor_filename() {
  341. if [ "${1##*.}" = 'asc' ]; then
  342. local trusted="${GPGHOMEDIR}/${1##*/}.gpg"
  343. if [ -s "$1" ]; then
  344. dearmor_keyring < "$1" > "$trusted"
  345. fi
  346. echo "$trusted"
  347. elif [ "${1##*.}" = 'gpg' ]; then
  348. echo "$1"
  349. elif [ "$(head -n 1 "$1" 2>/dev/null)" = '-----BEGIN PGP PUBLIC KEY BLOCK-----' ]; then
  350. local trusted="${GPGHOMEDIR}/${1##*/}.gpg"
  351. dearmor_keyring < "$1" > "$trusted"
  352. echo "$trusted"
  353. else
  354. echo "$1"
  355. fi
  356. }
  357. catfile() {
  358. cat "$(dearmor_filename "$1")" >> "$2"
  359. }
  360. merge_all_trusted_keyrings_into_pubring() {
  361. # does the same as:
  362. # foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
  363. # but without using gpg, just cat and find
  364. local PUBRING="$(readlink -f "${GPGHOMEDIR}")/pubring.gpg"
  365. rm -f "$PUBRING"
  366. touch "$PUBRING"
  367. foreach_keyring_do 'catfile' "$PUBRING"
  368. }
  369. import_keys_from_keyring() {
  370. import_keyring_into_keyring "$1" "$2"
  371. }
  372. merge_keys_into_keyrings() {
  373. import_keyring_into_keyring "$2" "$1" '' --import-options 'merge-only'
  374. }
  375. merge_back_changes() {
  376. if [ -n "$FORCED_KEYRING" ]; then
  377. # if the keyring was forced merge is already done
  378. if [ "$FORCED_KEYRING" != "$TRUSTEDFILE" ]; then
  379. mv -f "$FORCED_KEYRING" "${FORCED_KEYRING}~"
  380. export_key_from_to "$TRUSTEDFILE" "$FORCED_KEYRING" --armor
  381. fi
  382. return
  383. fi
  384. if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
  385. # merge all updated keys
  386. foreach_keyring_do 'merge_keys_into_keyrings' "${GPGHOMEDIR}/pubring.gpg"
  387. fi
  388. # look for keys which were added or removed
  389. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.orig.gpg" > "${GPGHOMEDIR}/pubring.orig.keylst"
  390. get_fingerprints_of_keyring "${GPGHOMEDIR}/pubring.gpg" > "${GPGHOMEDIR}/pubring.keylst"
  391. comm -3 "${GPGHOMEDIR}/pubring.keylst" "${GPGHOMEDIR}/pubring.orig.keylst" > "${GPGHOMEDIR}/pubring.diff"
  392. # key isn't part of new keyring, so remove
  393. cut -f 2 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  394. if [ -z "$key" ]; then continue; fi
  395. foreach_keyring_do 'remove_key_from_keyring' "$key"
  396. done
  397. # key is only part of new keyring, so we need to import it
  398. cut -f 1 "${GPGHOMEDIR}/pubring.diff" | while read key; do
  399. if [ -z "$key" ]; then continue; fi
  400. import_keyring_into_keyring '' "$TRUSTEDFILE" "$key"
  401. done
  402. }
  403. setup_merged_keyring() {
  404. if [ -n "$FORCED_KEYID" ]; then
  405. merge_all_trusted_keyrings_into_pubring
  406. FORCED_KEYRING="${GPGHOMEDIR}/forcedkeyid.gpg"
  407. TRUSTEDFILE="${FORCED_KEYRING}"
  408. echo "#!/bin/sh
  409. exec sh '($(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  410. GPG="${GPGHOMEDIR}/gpg.1.sh"
  411. # ignore error as this "just" means we haven't found the forced keyid and the keyring will be empty
  412. import_keyring_into_keyring '' "$TRUSTEDFILE" "$FORCED_KEYID" || true
  413. elif [ -z "$FORCED_KEYRING" ]; then
  414. merge_all_trusted_keyrings_into_pubring
  415. if [ -r "${GPGHOMEDIR}/pubring.gpg" ]; then
  416. cp -a "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  417. else
  418. touch "${GPGHOMEDIR}/pubring.gpg" "${GPGHOMEDIR}/pubring.orig.gpg"
  419. fi
  420. echo "#!/bin/sh
  421. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${GPGHOMEDIR}/pubring.gpg")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  422. GPG="${GPGHOMEDIR}/gpg.1.sh"
  423. else
  424. TRUSTEDFILE="$(dearmor_filename "$FORCED_KEYRING")"
  425. create_new_keyring "$TRUSTEDFILE"
  426. echo "#!/bin/sh
  427. exec sh '$(escape_shell "${GPG}")' --keyring '$(escape_shell "${TRUSTEDFILE}")' \"\$@\"" > "${GPGHOMEDIR}/gpg.1.sh"
  428. GPG="${GPGHOMEDIR}/gpg.1.sh"
  429. fi
  430. }
  431. create_new_keyring() {
  432. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  433. if ! [ -e "$1" ]; then
  434. if [ -w "$(dirname "$1")" ]; then
  435. touch -- "$1"
  436. chmod 0644 -- "$1"
  437. fi
  438. fi
  439. }
  440. aptkey_execute() { sh "$@"; }
  441. usage() {
  442. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  443. echo
  444. echo "Manage apt's list of trusted keys"
  445. echo
  446. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  447. echo " apt-key del <keyid> - remove the key <keyid>"
  448. echo " apt-key export <keyid> - output the key <keyid>"
  449. echo " apt-key exportall - output all trusted keys"
  450. echo " apt-key update - update keys using the keyring package"
  451. echo " apt-key net-update - update keys using the network"
  452. echo " apt-key list - list keys"
  453. echo " apt-key finger - list fingerprints"
  454. echo " apt-key adv - pass advanced options to gpg (download key)"
  455. echo
  456. echo "If no specific keyring file is given the command applies to all keyring files."
  457. }
  458. while [ -n "$1" ]; do
  459. case "$1" in
  460. --keyring)
  461. shift
  462. TRUSTEDFILE="$1"
  463. FORCED_KEYRING="$1"
  464. ;;
  465. --keyid)
  466. shift
  467. FORCED_KEYID="$1"
  468. ;;
  469. --secret-keyring)
  470. shift
  471. FORCED_SECRET_KEYRING="$1"
  472. ;;
  473. --readonly)
  474. merge_back_changes() { true; }
  475. create_new_keyring() { if [ ! -r "$FORCED_KEYRING" ]; then TRUSTEDFILE='/dev/null'; FORCED_KEYRING="$TRUSTEDFILE"; fi; }
  476. ;;
  477. --fakeroot)
  478. requires_root() { true; }
  479. ;;
  480. --quiet)
  481. aptkey_echo() { true; }
  482. ;;
  483. --debug1)
  484. # some cmds like finger redirect stderr to /dev/null …
  485. aptkey_execute() { echo 'EXEC:' "$@"; sh "$@"; }
  486. ;;
  487. --debug2)
  488. # … other more complicated ones pipe gpg into gpg.
  489. aptkey_execute() { echo >&2 'EXEC:' "$@"; sh "$@"; }
  490. ;;
  491. --*)
  492. echo >&2 "Unknown option: $1"
  493. usage
  494. exit 1;;
  495. *)
  496. break;;
  497. esac
  498. shift
  499. done
  500. if [ -z "$TRUSTEDFILE" ]; then
  501. TRUSTEDFILE="/etc/apt/trusted.gpg"
  502. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  503. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  504. fi
  505. command="$1"
  506. if [ -z "$command" ]; then
  507. usage
  508. exit 1
  509. fi
  510. shift
  511. find_gpgv_status_fd() {
  512. while [ -n "$1" ]; do
  513. if [ "$1" = '--status-fd' ]; then
  514. shift
  515. echo "$1"
  516. break
  517. fi
  518. shift
  519. done
  520. }
  521. GPGSTATUSFD="$(find_gpgv_status_fd "$@")"
  522. apt_warn() {
  523. if [ -z "$GPGHOMEDIR" ]; then
  524. echo >&2 'W:' "$@"
  525. else
  526. echo 'W:' "$@" > "${GPGHOMEDIR}/aptwarnings.log"
  527. fi
  528. if [ -n "$GPGSTATUSFD" ]; then
  529. echo >&${GPGSTATUSFD} '[APTKEY:] WARNING' "$@"
  530. fi
  531. }
  532. apt_error() {
  533. if [ -z "$GPGHOMEDIR" ]; then
  534. echo >&2 'E:' "$@"
  535. else
  536. echo 'E:' "$@" > "${GPGHOMEDIR}/aptwarnings.log"
  537. fi
  538. if [ -n "$GPGSTATUSFD" ]; then
  539. echo >&${GPGSTATUSFD} '[APTKEY:] ERROR' "$@"
  540. fi
  541. }
  542. cleanup_gpg_home() {
  543. if [ -z "$GPGHOMEDIR" ]; then return; fi
  544. if [ -s "$GPGHOMEDIR/aptwarnings.log" ]; then
  545. cat >&2 "$GPGHOMEDIR/aptwarnings.log"
  546. fi
  547. if command_available 'gpgconf'; then
  548. GNUPGHOME="${GPGHOMEDIR}" gpgconf --kill gpg-agent >/dev/null 2>&1 || true
  549. fi
  550. rm -rf "$GPGHOMEDIR"
  551. }
  552. create_gpg_home() {
  553. # gpg needs (in different versions more or less) files to function correctly,
  554. # so we give it its own homedir and generate some valid content for it later on
  555. if [ -n "$TMPDIR" ]; then
  556. # tmpdir is a directory and current user has rwx access to it
  557. # same tests as in apt-pkg/contrib/fileutl.cc GetTempDir()
  558. if [ ! -d "$TMPDIR" ] || [ ! -r "$TMPDIR" ] || [ ! -w "$TMPDIR" ] || [ ! -x "$TMPDIR" ]; then
  559. unset TMPDIR
  560. fi
  561. fi
  562. GPGHOMEDIR="$(mktemp -d)"
  563. CURRENTTRAP="${CURRENTTRAP} cleanup_gpg_home;"
  564. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  565. if [ -z "$GPGHOMEDIR" ]; then
  566. apt_error "Could not create temporary gpg home directory in $TMPDIR (wrong permissions?)"
  567. exit 28
  568. fi
  569. chmod 700 "$GPGHOMEDIR"
  570. }
  571. prepare_gpg_home() {
  572. # crude detection if we are called from a maintainerscript where the
  573. # package depends on gnupg or not. We accept recommends here as
  574. # well as the script hopefully uses apt-key optionally then like e.g.
  575. # debian-archive-keyring for (upgrade) cleanup did
  576. if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ] && [ -z "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then
  577. if ! dpkg-query --show --showformat '${Pre-Depends}${Depends}${Recommends}\n' "$DPKG_MAINTSCRIPT_PACKAGE" 2>/dev/null | grep -q gnupg; then
  578. cat >&2 <<EOF
  579. Warning: The $DPKG_MAINTSCRIPT_NAME maintainerscript of the package $DPKG_MAINTSCRIPT_PACKAGE
  580. Warning: seems to use apt-key (provided by apt) without depending on gnupg or gnupg2.
  581. Warning: This will BREAK in the future and should be fixed by the package maintainer(s).
  582. Note: Check first if apt-key functionality is needed at all - it probably isn't!
  583. EOF
  584. fi
  585. fi
  586. eval "$(apt-config shell GPG_EXE Apt::Key::gpgcommand)"
  587. if [ -n "$GPG_EXE" ] && command_available "$GPG_EXE"; then
  588. true
  589. elif command_available 'gpg'; then
  590. GPG_EXE="gpg"
  591. elif command_available 'gpg2'; then
  592. GPG_EXE="gpg2"
  593. elif command_available 'gpg1'; then
  594. GPG_EXE="gpg1"
  595. else
  596. apt_error 'gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation'
  597. exit 255
  598. fi
  599. create_gpg_home
  600. # create the trustdb with an (empty) dummy keyring
  601. # older gpgs required it, newer gpgs even warn that it isn't needed,
  602. # but require it nonetheless for some commands, so we just play safe
  603. # here for the foreseeable future and create a dummy one
  604. touch "${GPGHOMEDIR}/empty.gpg"
  605. if ! "$GPG_EXE" --ignore-time-conflict --no-options --no-default-keyring \
  606. --homedir "$GPGHOMEDIR" --quiet --check-trustdb --keyring "${GPGHOMEDIR}/empty.gpg" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  607. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  608. false
  609. fi
  610. # now tell gpg that it shouldn't try to maintain this trustdb file
  611. echo "#!/bin/sh
  612. exec '$(escape_shell "${GPG_EXE}")' --ignore-time-conflict --no-options --no-default-keyring \\
  613. --homedir '$(escape_shell "${GPGHOMEDIR}")' --no-auto-check-trustdb --trust-model always \"\$@\"" > "${GPGHOMEDIR}/gpg.0.sh"
  614. GPG_SH="${GPGHOMEDIR}/gpg.0.sh"
  615. GPG="$GPG_SH"
  616. # We don't usually need a secret keyring, of course, but
  617. # for advanced operations, we might really need a secret keyring after all
  618. if [ -n "$FORCED_SECRET_KEYRING" ] && [ -r "$FORCED_SECRET_KEYRING" ]; then
  619. if ! aptkey_execute "$GPG" -v --batch --import "$FORCED_SECRET_KEYRING" >"${GPGHOMEDIR}/gpgoutput.log" 2>&1; then
  620. cat >&2 "${GPGHOMEDIR}/gpgoutput.log"
  621. false
  622. fi
  623. else
  624. # and then, there are older versions of gpg which panic and implode
  625. # if there isn't one available - and writeable for imports
  626. # and even if not output is littered with the creation of a secring,
  627. # so lets call import once to have it create what it wants in silence
  628. echo -n | aptkey_execute "$GPG" --batch --import >/dev/null 2>&1 || true
  629. fi
  630. }
  631. warn_on_script_usage() {
  632. if [ -n "$APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE" ]; then
  633. return
  634. fi
  635. # (Maintainer) scripts should not be using apt-key
  636. if [ -n "$DPKG_MAINTSCRIPT_PACKAGE" ]; then
  637. echo >&2 "Warning: apt-key should not be used in scripts (called from $DPKG_MAINTSCRIPT_NAME maintainerscript of the package ${DPKG_MAINTSCRIPT_PACKAGE})"
  638. elif [ ! -t 1 ]; then
  639. echo >&2 "Warning: apt-key output should not be parsed (stdout is not a terminal)"
  640. fi
  641. }
  642. if [ "$command" != 'help' ] && [ "$command" != 'verify' ]; then
  643. prepare_gpg_home
  644. fi
  645. case "$command" in
  646. add)
  647. warn_on_script_usage
  648. requires_root
  649. setup_merged_keyring
  650. aptkey_execute "$GPG" --quiet --batch --import "$@"
  651. merge_back_changes
  652. aptkey_echo "OK"
  653. ;;
  654. del|rm|remove)
  655. # no script warning here as removing 'add' usage needs 'del' for cleanup
  656. requires_root
  657. foreach_keyring_do 'remove_key_from_keyring' "$@"
  658. aptkey_echo "OK"
  659. ;;
  660. update)
  661. warn_on_script_usage
  662. requires_root
  663. setup_merged_keyring
  664. update
  665. merge_back_changes
  666. ;;
  667. net-update)
  668. requires_root
  669. setup_merged_keyring
  670. net_update
  671. merge_back_changes
  672. ;;
  673. list|finger*)
  674. warn_on_script_usage
  675. foreach_keyring_do 'list_keys_in_keyring' --fingerprint "$@"
  676. ;;
  677. export|exportall)
  678. warn_on_script_usage
  679. merge_all_trusted_keyrings_into_pubring
  680. aptkey_execute "$GPG_SH" --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
  681. ;;
  682. adv*)
  683. warn_on_script_usage
  684. setup_merged_keyring
  685. aptkey_echo "Executing: $GPG" "$@"
  686. aptkey_execute "$GPG" "$@"
  687. merge_back_changes
  688. ;;
  689. verify)
  690. GPGV=''
  691. eval $(apt-config shell GPGV Apt::Key::gpgvcommand)
  692. if [ -n "$GPGV" ] && command_available "$GPGV"; then true;
  693. elif command_available 'gpgv'; then GPGV='gpgv';
  694. elif command_available 'gpgv2'; then GPGV='gpgv2';
  695. elif command_available 'gpgv1'; then GPGV='gpgv1';
  696. else
  697. apt_error 'gpgv, gpgv2 or gpgv1 required for verification, but neither seems installed'
  698. exit 29
  699. fi
  700. # for a forced keyid we need gpg --export, so full wrapping required
  701. if [ -n "$FORCED_KEYID" ]; then
  702. prepare_gpg_home
  703. else
  704. create_gpg_home
  705. fi
  706. setup_merged_keyring
  707. if [ -n "$FORCED_KEYRING" ]; then
  708. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "$(dearmor_filename "${FORCED_KEYRING}")" --ignore-time-conflict "$@"
  709. else
  710. "$GPGV" --homedir "${GPGHOMEDIR}" --keyring "${GPGHOMEDIR}/pubring.gpg" --ignore-time-conflict "$@"
  711. fi
  712. ;;
  713. help)
  714. usage
  715. ;;
  716. *)
  717. usage
  718. exit 1
  719. ;;
  720. esac