apt-key 8.1 KB

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