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