apt-key 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
  7. GPG="$GPG_CMD"
  8. MASTER_KEYRING=/usr/share/keyrings/ubuntu-master-keyring.gpg
  9. ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg
  10. REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg
  11. ARCHIVE_KEYRING_URI=http://archive.ubuntu.com/ubuntu/project/ubuntu-archive-keyring.gpg
  12. add_keys_with_verify_against_master_keyring() {
  13. ADD_KEYRING=$1
  14. MASTER=$2
  15. if [ ! -f "$ADD_KEYRING" ]; then
  16. echo "ERROR: '$ADD_KEYRING' not found"
  17. return
  18. fi
  19. if [ ! -f "$MASTER" ]; then
  20. echo "ERROR: '$MASTER' not found"
  21. return
  22. fi
  23. # when adding new keys, make sure that the archive-master-keyring
  24. # is honored. so:
  25. # all keys that are exported must have a valid signature
  26. # from a key in the $distro-master-keyring
  27. add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
  28. master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
  29. for add_key in $add_keys; do
  30. ADDED=0
  31. for master_key in $master_keys; do
  32. if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
  33. $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
  34. ADDED=1
  35. fi
  36. done
  37. if [ $ADDED = 0 ]; then
  38. echo >&2 "Key '$add_key' not added. It is not signed with a master key"
  39. fi
  40. done
  41. }
  42. # update the current archive signing keyring from a network URI
  43. # the archive-keyring keys needs to be signed with the master key
  44. # (otherwise it does not make sense from a security POV)
  45. net_update() {
  46. if [ -z "$ARCHIVE_KEYRING_URI" ]; then
  47. echo "ERROR: no location for the archive-keyring given"
  48. exit 1
  49. fi
  50. # in theory we would need to depend on wget for this, but this feature
  51. # isn't useable in debian anyway as we have no keyring uri nor a master key
  52. if ! which wget >/dev/null 2>&1; then
  53. echo "ERROR: an installed wget is required for a network-based update"
  54. exit 1
  55. fi
  56. if [ ! -d /var/lib/apt/keyrings ]; then
  57. mkdir -p /var/lib/apt/keyrings
  58. fi
  59. keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
  60. old_mtime=0
  61. if [ -e $keyring ]; then
  62. old_mtime=$(stat -c %Y $keyring)
  63. fi
  64. (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
  65. if [ ! -e $keyring ]; then
  66. return
  67. fi
  68. new_mtime=$(stat -c %Y $keyring)
  69. if [ $new_mtime -ne $old_mtime ]; then
  70. echo "Checking for new archive signing keys now"
  71. add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
  72. fi
  73. }
  74. update() {
  75. if [ ! -f $ARCHIVE_KEYRING ]; then
  76. echo >&2 "ERROR: Can't find the archive-keyring"
  77. echo >&2 "Is the ubuntu-keyring package installed?"
  78. exit 1
  79. fi
  80. # add new keys from the package;
  81. # we do not use add_keys_with_verify_against_master_keyring here,
  82. # because we "update" is run on regular package updates. A
  83. # attacker might as well replace the master-archive-keyring file
  84. # in the package and add his own keys. so this check wouldn't
  85. # add any security. we *need* this check on net-update though
  86. $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
  87. if [ -r "$REMOVED_KEYS" ]; then
  88. # remove no-longer supported/used keys
  89. keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
  90. for key in $keys; do
  91. if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
  92. $GPG --quiet --batch --delete-key --yes ${key}
  93. fi
  94. done
  95. else
  96. echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
  97. fi
  98. }
  99. usage() {
  100. echo "Usage: apt-key [--keyring file] [command] [arguments]"
  101. echo
  102. echo "Manage apt's list of trusted keys"
  103. echo
  104. echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
  105. echo " apt-key del <keyid> - remove the key <keyid>"
  106. echo " apt-key export <keyid> - output the key <keyid>"
  107. echo " apt-key exportall - output all trusted keys"
  108. echo " apt-key update - update keys using the keyring package"
  109. echo " apt-key net-update - update keys using the network"
  110. echo " apt-key list - list keys"
  111. echo " apt-key finger - list fingerprints"
  112. echo " apt-key adv - pass advanced options to gpg (download key)"
  113. echo
  114. echo "If no specific keyring file is given the command applies to all keyring files."
  115. }
  116. # Determine on which keyring we want to work
  117. if [ "$1" = "--keyring" ]; then
  118. #echo "keyfile given"
  119. shift
  120. TRUSTEDFILE="$1"
  121. if [ -r "$TRUSTEDFILE" ]; then
  122. GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
  123. else
  124. echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
  125. exit 1
  126. fi
  127. shift
  128. # otherwise use the default
  129. else
  130. #echo "generate list"
  131. TRUSTEDFILE="/etc/apt/trusted.gpg"
  132. if [ -r "$TRUSTEDFILE" ]; then
  133. GPG="$GPG --keyring $TRUSTEDFILE"
  134. fi
  135. GPG="$GPG --primary-keyring $TRUSTEDFILE"
  136. TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
  137. if [ -d "$TRUSTEDPARTS" ]; then
  138. #echo "parts active"
  139. for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
  140. #echo "part -> $trusted"
  141. GPG="$GPG --keyring $trusted"
  142. done
  143. fi
  144. fi
  145. #echo "COMMAND: $GPG"
  146. command="$1"
  147. if [ -z "$command" ]; then
  148. usage
  149. exit 1
  150. fi
  151. shift
  152. if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
  153. echo >&2 "Warning: gnupg does not seem to be installed."
  154. echo >&2 "Warning: apt-key requires gnupg for most operations."
  155. echo >&2
  156. fi
  157. case "$command" in
  158. add)
  159. $GPG --quiet --batch --import "$1"
  160. echo "OK"
  161. ;;
  162. del|rm|remove)
  163. $GPG --quiet --batch --delete-key --yes "$1"
  164. echo "OK"
  165. ;;
  166. update)
  167. update
  168. ;;
  169. net-update)
  170. net_update
  171. ;;
  172. list)
  173. $GPG --batch --list-keys
  174. ;;
  175. finger*)
  176. $GPG --batch --fingerprint
  177. ;;
  178. export)
  179. $GPG --armor --export "$1"
  180. ;;
  181. exportall)
  182. $GPG --armor --export
  183. ;;
  184. adv*)
  185. echo "Executing: $GPG $*"
  186. $GPG $*
  187. ;;
  188. help)
  189. usage
  190. ;;
  191. *)
  192. usage
  193. exit 1
  194. ;;
  195. esac