apt-key 10 KB

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