]>
Commit | Line | Data |
---|---|---|
b3d44315 MV |
1 | #!/bin/sh |
2 | ||
3 | set -e | |
dac074b0 | 4 | unset GREP_OPTIONS |
b3d44315 | 5 | |
f9e64e7b | 6 | GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring" |
248ec5ab | 7 | |
f9e64e7b DK |
8 | # gpg needs a trustdb to function, but it can't be invalid (not even empty) |
9 | # so we create a temporary directory to store our fresh readable trustdb in | |
10 | TRUSTDBDIR="$(mktemp -d)" | |
11 | CURRENTTRAP="${CURRENTTRAP} rm -rf '${TRUSTDBDIR}';" | |
12 | trap "${CURRENTTRAP}" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM | |
13 | chmod 700 "$TRUSTDBDIR" | |
14 | # We also don't use a secret keyring, of course, but gpg panics and | |
15 | # implodes if there isn't one available - and writeable for imports | |
16 | SECRETKEYRING="${TRUSTDBDIR}/secring.gpg" | |
17 | touch $SECRETKEYRING | |
18 | GPG_CMD="$GPG_CMD --secret-keyring $SECRETKEYRING" | |
c0a01322 | 19 | GPG_CMD="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg" |
f9e64e7b DK |
20 | |
21 | # now create the trustdb with an (empty) dummy keyring | |
22 | $GPG_CMD --quiet --check-trustdb --keyring $SECRETKEYRING | |
23 | # and make sure that gpg isn't trying to update the file | |
24 | GPG_CMD="$GPG_CMD --no-auto-check-trustdb --trust-model always" | |
25 | ||
46e39c8e | 26 | GPG="$GPG_CMD" |
4a242c5b | 27 | |
5acf154d MV |
28 | APT_DIR="/" |
29 | eval $(apt-config shell APT_DIR Dir) | |
30 | ||
5b2c6ddc | 31 | MASTER_KEYRING='&keyring-master-filename;' |
80f3aeb0 | 32 | eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring) |
5b2c6ddc | 33 | ARCHIVE_KEYRING='&keyring-filename;' |
80f3aeb0 | 34 | eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring) |
5b2c6ddc | 35 | REMOVED_KEYS='&keyring-removed-filename;' |
80f3aeb0 | 36 | eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys) |
5b2c6ddc | 37 | ARCHIVE_KEYRING_URI='&keyring-uri;' |
f87338d2 | 38 | eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI) |
5acf154d | 39 | TMP_KEYRING=${APT_DIR}/var/lib/apt/keyrings/maybe-import-keyring.gpg |
4a242c5b | 40 | |
00c6e1a3 MV |
41 | requires_root() { |
42 | if [ "$(id -u)" -ne 0 ]; then | |
43 | echo >&1 "ERROR: This command can only be used by root." | |
44 | exit 1 | |
45 | fi | |
46 | } | |
47 | ||
5de34668 JK |
48 | # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead. |
49 | init_keyring() { | |
50 | for path; do | |
51 | if ! [ -e "$path" ]; then | |
52 | touch -- "$path" | |
53 | chmod 0644 -- "$path" | |
54 | fi | |
55 | done | |
56 | } | |
57 | ||
7fbe42c0 | 58 | add_keys_with_verify_against_master_keyring() { |
8c56b1e0 MV |
59 | ADD_KEYRING=$1 |
60 | MASTER=$2 | |
f87338d2 | 61 | |
8c56b1e0 MV |
62 | if [ ! -f "$ADD_KEYRING" ]; then |
63 | echo "ERROR: '$ADD_KEYRING' not found" | |
64 | return | |
65 | fi | |
66 | if [ ! -f "$MASTER" ]; then | |
67 | echo "ERROR: '$MASTER' not found" | |
68 | return | |
69 | fi | |
70 | ||
71 | # when adding new keys, make sure that the archive-master-keyring | |
72 | # is honored. so: | |
3a341a1d MV |
73 | # all keys that are exported must have a valid signature |
74 | # from a key in the $distro-master-keyring | |
8c56b1e0 | 75 | add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5` |
f87338d2 | 76 | all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5` |
8c56b1e0 | 77 | master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5` |
f87338d2 DK |
78 | |
79 | # ensure there are no colisions LP: #857472 | |
80 | for all_add_key in $all_add_keys; do | |
81 | for master_key in $master_keys; do | |
82 | if [ "$all_add_key" = "$master_key" ]; then | |
83 | echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted" | |
84 | return 1 | |
85 | fi | |
86 | done | |
87 | done | |
88 | ||
8c56b1e0 | 89 | for add_key in $add_keys; do |
f87338d2 DK |
90 | # export the add keyring one-by-one |
91 | rm -f $TMP_KEYRING | |
92 | $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key | |
93 | # check if signed with the master key and only add in this case | |
94 | ADDED=0 | |
8c56b1e0 | 95 | for master_key in $master_keys; do |
f87338d2 DK |
96 | if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then |
97 | $GPG --import $TMP_KEYRING | |
c9bb37c7 | 98 | ADDED=1 |
8c56b1e0 | 99 | fi |
7fbe42c0 | 100 | done |
c9bb37c7 MV |
101 | if [ $ADDED = 0 ]; then |
102 | echo >&2 "Key '$add_key' not added. It is not signed with a master key" | |
103 | fi | |
8c56b1e0 | 104 | done |
f87338d2 | 105 | rm -f $TMP_KEYRING |
7fbe42c0 | 106 | } |
4a242c5b | 107 | |
848ecfa4 MV |
108 | # update the current archive signing keyring from a network URI |
109 | # the archive-keyring keys needs to be signed with the master key | |
110 | # (otherwise it does not make sense from a security POV) | |
111 | net_update() { | |
f87338d2 | 112 | # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128)) |
5acf154d MV |
113 | APT_KEY_NET_UPDATE_ENABLED="" |
114 | eval $(apt-config shell APT_KEY_NET_UPDATE_ENABLED APT::Key::Net-Update-Enabled) | |
115 | if [ -z "$APT_KEY_NET_UPDATE_ENABLED" ]; then | |
116 | exit 1 | |
117 | fi | |
f87338d2 | 118 | |
848ecfa4 | 119 | if [ -z "$ARCHIVE_KEYRING_URI" ]; then |
00c6e1a3 | 120 | echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set" |
46e39c8e MV |
121 | exit 1 |
122 | fi | |
00c6e1a3 | 123 | requires_root |
46e39c8e MV |
124 | # in theory we would need to depend on wget for this, but this feature |
125 | # isn't useable in debian anyway as we have no keyring uri nor a master key | |
126 | if ! which wget >/dev/null 2>&1; then | |
00c6e1a3 | 127 | echo >&2 "ERROR: an installed wget is required for a network-based update" |
46e39c8e | 128 | exit 1 |
848ecfa4 | 129 | fi |
5acf154d MV |
130 | if [ ! -d ${APT_DIR}/var/lib/apt/keyrings ]; then |
131 | mkdir -p ${APT_DIR}/var/lib/apt/keyrings | |
848ecfa4 | 132 | fi |
e5543ea5 | 133 | keyring=${APT_DIR}/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING_URI) |
93886541 MV |
134 | old_mtime=0 |
135 | if [ -e $keyring ]; then | |
20ba2505 | 136 | old_mtime=$(stat -c %Y $keyring) |
93886541 | 137 | fi |
5acf154d | 138 | (cd ${APT_DIR}/var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI) |
93886541 MV |
139 | if [ ! -e $keyring ]; then |
140 | return | |
141 | fi | |
20ba2505 | 142 | new_mtime=$(stat -c %Y $keyring) |
93886541 | 143 | if [ $new_mtime -ne $old_mtime ]; then |
8dd0686e | 144 | echo "Checking for new archive signing keys now" |
93886541 MV |
145 | add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING |
146 | fi | |
848ecfa4 MV |
147 | } |
148 | ||
4a242c5b MV |
149 | update() { |
150 | if [ ! -f $ARCHIVE_KEYRING ]; then | |
151 | echo >&2 "ERROR: Can't find the archive-keyring" | |
5b2c6ddc | 152 | echo >&2 "Is the &keyring-package; package installed?" |
4a242c5b MV |
153 | exit 1 |
154 | fi | |
00c6e1a3 | 155 | requires_root |
4a242c5b | 156 | |
3a341a1d MV |
157 | # add new keys from the package; |
158 | ||
159 | # we do not use add_keys_with_verify_against_master_keyring here, | |
1171258a | 160 | # because "update" is run on regular package updates. A |
3a341a1d MV |
161 | # attacker might as well replace the master-archive-keyring file |
162 | # in the package and add his own keys. so this check wouldn't | |
163 | # add any security. we *need* this check on net-update though | |
164 | $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import | |
4a242c5b | 165 | |
364af2ef MV |
166 | if [ -r "$REMOVED_KEYS" ]; then |
167 | # remove no-longer supported/used keys | |
168 | keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
169 | for key in $keys; do | |
170 | if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then | |
171 | $GPG --quiet --batch --delete-key --yes ${key} | |
172 | fi | |
173 | done | |
174 | else | |
175 | echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2 | |
176 | fi | |
4a242c5b MV |
177 | } |
178 | ||
04937adc DK |
179 | remove_key_from_keyring() { |
180 | local GPG="$GPG_CMD --keyring $1" | |
181 | # check if the key is in this keyring: the key id is in the 5 column at the end | |
182 | if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+$2:"; then | |
183 | return | |
184 | fi | |
185 | if [ ! -w "$1" ]; then | |
186 | echo >&2 "Key ${2} is in keyring ${1}, but can't be removed as it is read only." | |
187 | return | |
188 | fi | |
189 | # check if it is the only key in the keyring and if so remove the keyring alltogether | |
190 | if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then | |
191 | mv -f "$1" "${1}~" # behave like gpg | |
192 | return | |
193 | fi | |
194 | # we can't just modify pointed to files as these might be in /usr or something | |
195 | local REALTARGET | |
196 | if [ -L "$1" ]; then | |
197 | REALTARGET="$(readlink -f "$1")" | |
198 | mv -f "$1" "${1}.dpkg-tmp" | |
199 | cp -a "$REALTARGET" "$1" | |
200 | ls "$(dirname $1)" | |
201 | fi | |
202 | # delete the key from the keyring | |
203 | $GPG --batch --delete-key --yes "$2" | |
204 | if [ -n "$REALTARGET" ]; then | |
205 | # the real backup is the old link, not the copy we made | |
206 | mv -f "${1}.dpkg-tmp" "${1}~" | |
207 | fi | |
208 | } | |
209 | ||
210 | remove_key() { | |
211 | requires_root | |
212 | ||
213 | # if a --keyring was given, just remove from there | |
214 | if [ -n "$FORCED_KEYRING" ]; then | |
215 | remove_key_from_keyring "$FORCED_KEYRING" "$1" | |
216 | else | |
217 | # otherwise all known keyrings are up for inspection | |
218 | local TRUSTEDFILE="/etc/apt/trusted.gpg" | |
219 | eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring) | |
220 | eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f) | |
221 | remove_key_from_keyring "$TRUSTEDFILE" "$1" | |
222 | TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
223 | eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) | |
224 | if [ -d "$TRUSTEDPARTS" ]; then | |
225 | for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do | |
226 | remove_key_from_keyring "$trusted" "$1" | |
227 | done | |
228 | fi | |
229 | fi | |
230 | echo "OK" | |
231 | } | |
232 | ||
7fbe42c0 | 233 | |
b3d44315 | 234 | usage() { |
46e39c8e | 235 | echo "Usage: apt-key [--keyring file] [command] [arguments]" |
b3d44315 MV |
236 | echo |
237 | echo "Manage apt's list of trusted keys" | |
238 | echo | |
239 | echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)" | |
240 | echo " apt-key del <keyid> - remove the key <keyid>" | |
bf6d5b42 OS |
241 | echo " apt-key export <keyid> - output the key <keyid>" |
242 | echo " apt-key exportall - output all trusted keys" | |
4a242c5b | 243 | echo " apt-key update - update keys using the keyring package" |
848ecfa4 | 244 | echo " apt-key net-update - update keys using the network" |
b3d44315 | 245 | echo " apt-key list - list keys" |
a8cabc8f LB |
246 | echo " apt-key finger - list fingerprints" |
247 | echo " apt-key adv - pass advanced options to gpg (download key)" | |
b3d44315 | 248 | echo |
46e39c8e | 249 | echo "If no specific keyring file is given the command applies to all keyring files." |
b3d44315 MV |
250 | } |
251 | ||
3dc55197 DK |
252 | while [ -n "$1" ]; do |
253 | case "$1" in | |
254 | --keyring) | |
255 | shift | |
256 | TRUSTEDFILE="$1" | |
04937adc | 257 | FORCED_KEYRING="$1" |
3dc55197 DK |
258 | if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then |
259 | GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE" | |
260 | else | |
261 | echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable" | |
262 | exit 1 | |
263 | fi | |
264 | shift | |
265 | ;; | |
266 | --fakeroot) | |
267 | requires_root() { true; } | |
268 | shift | |
269 | ;; | |
270 | --*) | |
271 | echo >&2 "Unknown option: $1" | |
272 | usage | |
273 | exit 1;; | |
274 | *) | |
275 | break;; | |
276 | esac | |
277 | done | |
278 | ||
279 | if [ -z "$TRUSTEDFILE" ]; then | |
280 | TRUSTEDFILE="/etc/apt/trusted.gpg" | |
281 | eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring) | |
282 | eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f) | |
283 | if [ -r "$TRUSTEDFILE" ]; then | |
284 | GPG="$GPG --keyring $TRUSTEDFILE" | |
285 | fi | |
286 | GPG="$GPG --primary-keyring $TRUSTEDFILE" | |
287 | TRUSTEDPARTS="/etc/apt/trusted.gpg.d" | |
288 | eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d) | |
289 | if [ -d "$TRUSTEDPARTS" ]; then | |
59f46f3a DK |
290 | # strip / suffix as gpg will double-slash in that case (#665411) |
291 | STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}" | |
292 | if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then | |
293 | TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS" | |
294 | fi | |
295 | for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do | |
3dc55197 DK |
296 | GPG="$GPG --keyring $trusted" |
297 | done | |
298 | fi | |
46e39c8e | 299 | fi |
46e39c8e | 300 | |
b3d44315 MV |
301 | command="$1" |
302 | if [ -z "$command" ]; then | |
303 | usage | |
304 | exit 1 | |
305 | fi | |
306 | shift | |
307 | ||
308 | if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then | |
309 | echo >&2 "Warning: gnupg does not seem to be installed." | |
310 | echo >&2 "Warning: apt-key requires gnupg for most operations." | |
311 | echo >&2 | |
312 | fi | |
313 | ||
b3d44315 MV |
314 | case "$command" in |
315 | add) | |
00c6e1a3 | 316 | requires_root |
5de34668 | 317 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
318 | $GPG --quiet --batch --import "$1" |
319 | echo "OK" | |
320 | ;; | |
321 | del|rm|remove) | |
5de34668 | 322 | init_keyring "$TRUSTEDFILE" |
04937adc | 323 | remove_key "$1" |
b3d44315 | 324 | ;; |
4a242c5b | 325 | update) |
5de34668 | 326 | init_keyring "$TRUSTEDFILE" |
4a242c5b MV |
327 | update |
328 | ;; | |
848ecfa4 | 329 | net-update) |
5de34668 | 330 | init_keyring "$TRUSTEDFILE" |
848ecfa4 MV |
331 | net_update |
332 | ;; | |
b3d44315 | 333 | list) |
5de34668 | 334 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
335 | $GPG --batch --list-keys |
336 | ;; | |
337 | finger*) | |
5de34668 | 338 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
339 | $GPG --batch --fingerprint |
340 | ;; | |
bf6d5b42 | 341 | export) |
5de34668 | 342 | init_keyring "$TRUSTEDFILE" |
bf6d5b42 OS |
343 | $GPG --armor --export "$1" |
344 | ;; | |
345 | exportall) | |
5de34668 | 346 | init_keyring "$TRUSTEDFILE" |
bf6d5b42 OS |
347 | $GPG --armor --export |
348 | ;; | |
b3d44315 | 349 | adv*) |
5de34668 | 350 | init_keyring "$TRUSTEDFILE" |
b3d44315 MV |
351 | echo "Executing: $GPG $*" |
352 | $GPG $* | |
353 | ;; | |
354 | help) | |
355 | usage | |
356 | ;; | |
357 | *) | |
358 | usage | |
359 | exit 1 | |
360 | ;; | |
361 | esac |