apt-key.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #!/bin/sh
  2. set -e
  3. unset GREP_OPTIONS
  4. APT_DIR="/"
  5. eval $(apt-config shell APT_DIR Dir)
  6. MASTER_KEYRING='&keyring-master-filename;'
  7. eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
  8. ARCHIVE_KEYRING='&keyring-filename;'
  9. eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
  10. REMOVED_KEYS='&keyring-removed-filename;'
  11. eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
  12. ARCHIVE_KEYRING_URI='&keyring-uri;'
  13. eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
  14. TMP_KEYRING=${APT_DIR}/var/lib/apt/keyrings/maybe-import-keyring.gpg
  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. add_keys_with_verify_against_master_keyring() {
  23. ADD_KEYRING=$1
  24. MASTER=$2
  25. if [ ! -f "$ADD_KEYRING" ]; then
  26. echo >&2 "ERROR: '$ADD_KEYRING' not found"
  27. return
  28. fi
  29. if [ ! -f "$MASTER" ]; then
  30. echo >&2 "ERROR: '$MASTER' not found"
  31. return
  32. fi
  33. # when adding new keys, make sure that the archive-master-keyring
  34. # is honored. so:
  35. # all keys that are exported must have a valid signature
  36. # from a key in the $distro-master-keyring
  37. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  38. all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
  39. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  40. # ensure there are no colisions LP: #857472
  41. for all_add_key in $all_add_keys; do
  42. for master_key in $master_keys; do
  43. if [ "$all_add_key" = "$master_key" ]; then
  44. echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
  45. return 1
  46. fi
  47. done
  48. done
  49. for add_key in $add_keys; do
  50. # export the add keyring one-by-one
  51. rm -f $TMP_KEYRING
  52. $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
  53. # check if signed with the master key and only add in this case
  54. ADDED=0
  55. for master_key in $master_keys; do
  56. if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then
  57. $GPG --import $TMP_KEYRING
  58. ADDED=1
  59. fi
  60. done
  61. if [ $ADDED = 0 ]; then
  62. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  63. fi
  64. done
  65. rm -f $TMP_KEYRING
  66. }
  67. # update the current archive signing keyring from a network URI
  68. # the archive-keyring keys needs to be signed with the master key
  69. # (otherwise it does not make sense from a security POV)
  70. net_update() {
  71. # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
  72. APT_KEY_NET_UPDATE_ENABLED=""
  73. eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled)
  74. if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then
  75. exit 1
  76. fi
  77. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  78. echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
  79. exit 1
  80. fi
  81. # in theory we would need to depend on wget for this, but this feature
  82. # isn't useable in debian anyway as we have no keyring uri nor a master key
  83. if ! which wget >/dev/null 2>&1; then
  84. echo >&2 "ERROR: an installed wget is required for a network-based update"
  85. exit 1
  86. fi
  87. if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then
  88. mkdir -p ${APT_DIR}/var/lib/apt/keyrings
  89. fi
  90. keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI)
  91. old_mtime=0
  92. if [ -e $keyring ]; then
  93. old_mtime=$(stat -c %Y $keyring)
  94. fi
  95. (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
  96. if [ ! -e $keyring ]; then
  97. return
  98. fi
  99. new_mtime=$(stat -c %Y $keyring)
  100. if [ $new_mtime -ne $old_mtime ]; then
  101. aptkey_echo "Checking for new archive signing keys now"
  102. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  103. fi
  104. }
  105. update() {
  106. if [ ! -f $ARCHIVE_KEYRING ]; then
  107. echo >&2 "ERROR: Can't find the archive-keyring"
  108. echo >&2 "Is the &keyring-package; package installed?"
  109. exit 1
  110. fi
  111. # add new keys from the package;
  112. # we do not use add_keys_with_verify_against_master_keyring here,
  113. # because "update" is run on regular package updates. A
  114. # attacker might as well replace the master-archive-keyring file
  115. # in the package and add his own keys. so this check wouldn't
  116. # add any security. we *need* this check on net-update though
  117. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  118. if [ -r "$REMOVED_KEYS" ]; then
  119. # remove no-longer supported/used keys
  120. $GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5 | while read key; do
  121. foreach_keyring_do 'remove_key_from_keyring' "$key"
  122. done
  123. else
  124. echo >&2 "Warning: removed keys keyring $REMOVED_KEYS missing or not readable"
  125. fi
  126. }
  127. remove_key_from_keyring() {
  128. local KEYRINGFILE="$1"
  129. shift
  130. # non-existent keyrings have by definition no keys
  131. if [ ! -e "$KEYRINGFILE" ]; then
  132. return
  133. fi
  134. local GPG="$GPG_CMD --keyring $KEYRINGFILE"
  135. while [ -n "$1" ]; do
  136. local KEY="$1"
  137. shift
  138. # check if the key is in this keyring: the key id is in the 5 column at the end
  139. if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]*${KEY}:"; then
  140. continue
  141. fi
  142. if [ ! -w "$KEYRINGFILE" ]; then
  143. echo >&2 "Key ${KEY} is in keyring ${KEYRINGFILE}, but can't be removed as it is read only."
  144. continue
  145. fi
  146. # check if it is the only key in the keyring and if so remove the keyring altogether
  147. if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
  148. mv -f "$KEYRINGFILE" "${KEYRINGFILE}~" # behave like gpg
  149. return
  150. fi
  151. # we can't just modify pointed to files as these might be in /usr or something
  152. local REALTARGET
  153. if [ -L "$KEYRINGFILE" ]; then
  154. REALTARGET="$(readlink -f "$KEYRINGFILE")"
  155. mv -f "$KEYRINGFILE" "${KEYRINGFILE}.dpkg-tmp"
  156. cp -a "$REALTARGET" "$KEYRINGFILE"
  157. fi
  158. # delete the key from the keyring
  159. $GPG --batch --delete-key --yes "$KEY"
  160. if [ -n "$REALTARGET" ]; then
  161. # the real backup is the old link, not the copy we made
  162. mv -f "${KEYRINGFILE}.dpkg-tmp" "${KEYRINGFILE}~"
  163. fi
  164. done
  165. }
  166. foreach_keyring_do() {
  167. local ACTION="$1"
  168. shift
  169. # if a --keyring was given, just remove from there
  170. if [ -n "$FORCED_KEYRING" ]; then
  171. $ACTION "$FORCED_KEYRING" "$@"
  172. else
  173. # otherwise all known keyrings are up for inspection
  174. if [ -s "$TRUSTEDFILE" ]; then
  175. $ACTION "$TRUSTEDFILE" "$@"
  176. fi
  177. local TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  178. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  179. if [ -d "$TRUSTEDPARTS" ]; then
  180. # strip / suffix as gpg will double-slash in that case (#665411)
  181. local STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
  182. if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
  183. TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
  184. fi
  185. for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
  186. if [ -s "$trusted" ]; then
  187. $ACTION "$trusted" "$@"
  188. fi
  189. done
  190. fi
  191. fi
  192. }
  193. list_keys_from_keyring() {
  194. local KEYRINGFILE="$1"
  195. shift
  196. # don't show the error message if this keyring doesn't include the key
  197. $GPG_CMD --keyring "$KEYRINGFILE" --batch --list-keys "$@" 2>/dev/null || true
  198. }
  199. fingerprint_keys_from_keyring() {
  200. local KEYRINGFILE="$1"
  201. shift
  202. # don't show the error message if this keyring doesn't include the fingerprint
  203. $GPG_CMD --keyring "$KEYRINGFILE" --batch --fingerprint "$@" 2>/dev/null || true
  204. }
  205. import_keys_from_keyring() {
  206. local IMPORT="$1"
  207. local KEYRINGFILE="$2"
  208. $GPG_CMD --keyring "$KEYRINGFILE" --batch --import "$IMPORT" >/dev/null 2>&1
  209. }
  210. setup_merged_keyring() {
  211. if [ -z "$FORCED_KEYRING" ]; then
  212. local TRUSTEDFILE_BAK="$TRUSTEDFILE"
  213. TRUSTEDFILE='/dev/null'
  214. foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
  215. TRUSTEDFILE="$TRUSTEDFILE_BAK"
  216. # mark it as non-writeable so users get errors if gnupg tries to modify it
  217. if [ -s "${GPGHOMEDIR}/pubring.gpg" ]; then
  218. chmod -w "${GPGHOMEDIR}/pubring.gpg"
  219. GPG="$GPG --keyring ${GPGHOMEDIR}/pubring.gpg"
  220. fi
  221. fi
  222. if [ -r "$TRUSTEDFILE" ]; then
  223. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  224. fi
  225. }
  226. usage() {
  227. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  228. echo
  229. echo "Manage apt's list of trusted keys"
  230. echo
  231. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  232. echo " apt-key del <keyid> - remove the key <keyid>"
  233. echo " apt-key export <keyid> - output the key <keyid>"
  234. echo " apt-key exportall - output all trusted keys"
  235. echo " apt-key update - update keys using the keyring package"
  236. echo " apt-key net-update - update keys using the network"
  237. echo " apt-key list - list keys"
  238. echo " apt-key finger - list fingerprints"
  239. echo " apt-key adv - pass advanced options to gpg (download key)"
  240. echo
  241. echo "If no specific keyring file is given the command applies to all keyring files."
  242. }
  243. while [ -n "$1" ]; do
  244. case "$1" in
  245. --keyring)
  246. shift
  247. TRUSTEDFILE="$1"
  248. FORCED_KEYRING="$1"
  249. shift
  250. ;;
  251. --fakeroot)
  252. requires_root() { true; }
  253. shift
  254. ;;
  255. --quiet)
  256. aptkey_echo() { true; }
  257. shift
  258. ;;
  259. --*)
  260. echo >&2 "Unknown option: $1"
  261. usage
  262. exit 1;;
  263. *)
  264. break;;
  265. esac
  266. done
  267. if [ -z "$TRUSTEDFILE" ]; then
  268. TRUSTEDFILE="/etc/apt/trusted.gpg"
  269. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  270. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  271. fi
  272. command="$1"
  273. if [ -z "$command" ]; then
  274. usage
  275. exit 1
  276. fi
  277. shift
  278. if [ "$command" != "help" ]; then
  279. eval $(apt-config shell GPG_EXE Apt::Key::gpgcommand)
  280. if [ -n "$GPG_EXE" ] && which "$GPG_EXE" >/dev/null 2>&1; then
  281. true
  282. elif which gpg >/dev/null 2>&1; then
  283. GPG_EXE="gpg"
  284. elif which gpg2 >/dev/null 2>&1; then
  285. GPG_EXE="gpg2"
  286. else
  287. echo >&2 "Error: gnupg or gnupg2 do not seem to be installed,"
  288. echo >&2 "Error: but apt-key requires gnupg or gnupg2 for operation."
  289. echo >&2
  290. exit 255
  291. fi
  292. GPG_CMD="$GPG_EXE --ignore-time-conflict --no-options --no-default-keyring"
  293. # gpg needs (in different versions more or less) files to function correctly,
  294. # so we give it its own homedir and generate some valid content for it
  295. GPGHOMEDIR="$(mktemp -d)"
  296. CURRENTTRAP="${CURRENTTRAP} rm -rf '${GPGHOMEDIR}';"
  297. trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
  298. chmod 700 "$GPGHOMEDIR"
  299. # We don't use a secret keyring, of course, but gpg panics and
  300. # implodes if there isn't one available - and writeable for imports
  301. SECRETKEYRING="${GPGHOMEDIR}/secring.gpg"
  302. touch $SECRETKEYRING
  303. GPG_CMD="$GPG_CMD --homedir $GPGHOMEDIR"
  304. # create the trustdb with an (empty) dummy keyring
  305. # older gpgs required it, newer gpgs even warn that it isn't needed,
  306. # but require it nonetheless for some commands, so we just play safe
  307. # here for the foreseeable future and create a dummy one
  308. $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING >/dev/null 2>&1
  309. # tell gpg that it shouldn't try to maintain a trustdb file
  310. GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always"
  311. GPG="$GPG_CMD"
  312. # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
  313. if ! [ -e "$TRUSTEDFILE" ]; then
  314. if [ -w "$(dirname "$TRUSTEDFILE")" ]; then
  315. touch -- "$TRUSTEDFILE"
  316. chmod 0644 -- "$TRUSTEDFILE"
  317. fi
  318. fi
  319. fi
  320. case "$command" in
  321. add)
  322. requires_root
  323. setup_merged_keyring
  324. $GPG --quiet --batch --import "$@"
  325. aptkey_echo "OK"
  326. ;;
  327. del|rm|remove)
  328. requires_root
  329. foreach_keyring_do 'remove_key_from_keyring' "$@"
  330. aptkey_echo "OK"
  331. ;;
  332. update)
  333. requires_root
  334. setup_merged_keyring
  335. update
  336. ;;
  337. net-update)
  338. requires_root
  339. setup_merged_keyring
  340. net_update
  341. ;;
  342. list)
  343. foreach_keyring_do 'list_keys_from_keyring' "$@"
  344. ;;
  345. finger*)
  346. foreach_keyring_do 'fingerprint_keys_from_keyring' "$@"
  347. ;;
  348. export|exportall)
  349. foreach_keyring_do 'import_keys_from_keyring' "${GPGHOMEDIR}/pubring.gpg"
  350. $GPG_CMD --keyring "${GPGHOMEDIR}/pubring.gpg" --armor --export "$@"
  351. ;;
  352. adv*)
  353. setup_merged_keyring
  354. aptkey_echo "Executing: $GPG $*"
  355. $GPG "$@"
  356. ;;
  357. help)
  358. usage
  359. ;;
  360. *)
  361. usage
  362. exit 1
  363. ;;
  364. esac