apt-key 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. usage() {
  137. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  138. echo
  139. echo "Manage apt's list of trusted keys"
  140. echo
  141. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  142. echo " apt-key del <keyid> - remove the key <keyid>"
  143. echo " apt-key export <keyid> - output the key <keyid>"
  144. echo " apt-key exportall - output all trusted keys"
  145. echo " apt-key update - update keys using the keyring package"
  146. echo " apt-key net-update - update keys using the network"
  147. echo " apt-key list - list keys"
  148. echo " apt-key finger - list fingerprints"
  149. echo " apt-key adv - pass advanced options to gpg (download key)"
  150. echo
  151. echo "If no specific keyring file is given the command applies to all keyring files."
  152. }
  153. while [ -n "$1" ]; do
  154. case "$1" in
  155. --keyring)
  156. shift
  157. TRUSTEDFILE="$1"
  158. if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
  159. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  160. else
  161. echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
  162. exit 1
  163. fi
  164. shift
  165. ;;
  166. --fakeroot)
  167. requires_root() { true; }
  168. shift
  169. ;;
  170. --*)
  171. echo >&2 "Unknown option: $1"
  172. usage
  173. exit 1;;
  174. *)
  175. break;;
  176. esac
  177. done
  178. if [ -z "$TRUSTEDFILE" ]; then
  179. TRUSTEDFILE="/etc/apt/trusted.gpg"
  180. eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
  181. eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
  182. if [ -r "$TRUSTEDFILE" ]; then
  183. GPG="$GPG --keyring $TRUSTEDFILE"
  184. fi
  185. GPG="$GPG --primary-keyring $TRUSTEDFILE"
  186. TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  187. eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
  188. if [ -d "$TRUSTEDPARTS" ]; then
  189. # strip / suffix as gpg will double-slash in that case (#665411)
  190. STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
  191. if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
  192. TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
  193. fi
  194. for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
  195. GPG="$GPG --keyring $trusted"
  196. done
  197. fi
  198. fi
  199. command="$1"
  200. if [ -z "$command" ]; then
  201. usage
  202. exit 1
  203. fi
  204. shift
  205. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  206. echo >&2 "Warning: gnupg does not seem to be installed."
  207. echo >&2 "Warning: apt-key requires gnupg for most operations."
  208. echo >&2
  209. fi
  210. case "$command" in
  211. add)
  212. requires_root
  213. init_keyring "$TRUSTEDFILE"
  214. $GPG --quiet --batch --import "$1"
  215. echo "OK"
  216. ;;
  217. del|rm|remove)
  218. requires_root
  219. init_keyring "$TRUSTEDFILE"
  220. $GPG --quiet --batch --delete-key --yes "$1"
  221. echo "OK"
  222. ;;
  223. update)
  224. init_keyring "$TRUSTEDFILE"
  225. update
  226. ;;
  227. net-update)
  228. init_keyring "$TRUSTEDFILE"
  229. net_update
  230. ;;
  231. list)
  232. init_keyring "$TRUSTEDFILE"
  233. $GPG --batch --list-keys
  234. ;;
  235. finger*)
  236. init_keyring "$TRUSTEDFILE"
  237. $GPG --batch --fingerprint
  238. ;;
  239. export)
  240. init_keyring "$TRUSTEDFILE"
  241. $GPG --armor --export "$1"
  242. ;;
  243. exportall)
  244. init_keyring "$TRUSTEDFILE"
  245. $GPG --armor --export
  246. ;;
  247. adv*)
  248. init_keyring "$TRUSTEDFILE"
  249. echo "Executing: $GPG $*"
  250. $GPG $*
  251. ;;
  252. help)
  253. usage
  254. ;;
  255. *)
  256. usage
  257. exit 1
  258. ;;
  259. esac