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