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