apt-key 8.1 KB

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