apt-key.in 11 KB

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