apt-key.in 11 KB

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