apt-key.in 11 KB

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