]> git.saurik.com Git - apt.git/blob - cmdline/apt-key
tests: fix some problems travis encounters
[apt.git] / cmdline / apt-key
1 #!/bin/sh
2
3 set -e
4 unset GREP_OPTIONS
5
6 GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring"
7
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"
19 GPG_CMD="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg"
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
26 GPG="$GPG_CMD"
27
28 MASTER_KEYRING=""
29 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
30 eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
31 ARCHIVE_KEYRING_URI=""
32 #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
33 eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
34
35 ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
36 eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
37 REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
38 eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
39
40 requires_root() {
41 if [ "$(id -u)" -ne 0 ]; then
42 echo >&1 "ERROR: This command can only be used by root."
43 exit 1
44 fi
45 }
46
47 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
48 init_keyring() {
49 for path; do
50 if ! [ -e "$path" ]; then
51 touch -- "$path"
52 chmod 0644 -- "$path"
53 fi
54 done
55 }
56
57 add_keys_with_verify_against_master_keyring() {
58 ADD_KEYRING=$1
59 MASTER=$2
60
61 if [ ! -f "$ADD_KEYRING" ]; then
62 echo "ERROR: '$ADD_KEYRING' not found"
63 return
64 fi
65 if [ ! -f "$MASTER" ]; then
66 echo "ERROR: '$MASTER' not found"
67 return
68 fi
69
70 # when adding new keys, make sure that the archive-master-keyring
71 # is honored. so:
72 # all keys that are exported must have a valid signature
73 # from a key in the $distro-master-keyring
74 add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
75 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
76 for add_key in $add_keys; do
77 ADDED=0
78 for master_key in $master_keys; do
79 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
80 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
81 ADDED=1
82 fi
83 done
84 if [ $ADDED = 0 ]; then
85 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
86 fi
87 done
88 }
89
90 # update the current archive signing keyring from a network URI
91 # the archive-keyring keys needs to be signed with the master key
92 # (otherwise it does not make sense from a security POV)
93 net_update() {
94 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
95 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
96 exit 1
97 fi
98 requires_root
99 # in theory we would need to depend on wget for this, but this feature
100 # isn't useable in debian anyway as we have no keyring uri nor a master key
101 if ! which wget >/dev/null 2>&1; then
102 echo >&2 "ERROR: an installed wget is required for a network-based update"
103 exit 1
104 fi
105 if [ ! -d /var/lib/apt/keyrings ]; then
106 mkdir -p /var/lib/apt/keyrings
107 fi
108 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
109 old_mtime=0
110 if [ -e $keyring ]; then
111 old_mtime=$(stat -c %Y $keyring)
112 fi
113 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
114 if [ ! -e $keyring ]; then
115 return
116 fi
117 new_mtime=$(stat -c %Y $keyring)
118 if [ $new_mtime -ne $old_mtime ]; then
119 echo "Checking for new archive signing keys now"
120 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
121 fi
122 }
123
124 update() {
125 if [ ! -f $ARCHIVE_KEYRING ]; then
126 echo >&2 "ERROR: Can't find the archive-keyring"
127 echo >&2 "Is the debian-archive-keyring package installed?"
128 exit 1
129 fi
130 requires_root
131
132 # add new keys from the package;
133
134 # we do not use add_keys_with_verify_against_master_keyring here,
135 # because "update" is run on regular package updates. A
136 # attacker might as well replace the master-archive-keyring file
137 # in the package and add his own keys. so this check wouldn't
138 # add any security. we *need* this check on net-update though
139 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
140
141 if [ -r "$REMOVED_KEYS" ]; then
142 # remove no-longer supported/used keys
143 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
144 for key in $keys; do
145 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
146 $GPG --quiet --batch --delete-key --yes ${key}
147 fi
148 done
149 else
150 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
151 fi
152 }
153
154 remove_key_from_keyring() {
155 local GPG="$GPG_CMD --keyring $1"
156 # check if the key is in this keyring: the key id is in the 5 column at the end
157 if ! $GPG --with-colons --list-keys 2>&1 | grep -q "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+$2:"; then
158 return
159 fi
160 if [ ! -w "$1" ]; then
161 echo >&2 "Key ${2} is in keyring ${1}, but can't be removed as it is read only."
162 return
163 fi
164 # check if it is the only key in the keyring and if so remove the keyring alltogether
165 if [ '1' = "$($GPG --with-colons --list-keys | grep "^pub:[^:]*:[^:]*:[^:]*:[0-9A-F]\+:" | wc -l)" ]; then
166 mv -f "$1" "${1}~" # behave like gpg
167 return
168 fi
169 # we can't just modify pointed to files as these might be in /usr or something
170 local REALTARGET
171 if [ -L "$1" ]; then
172 REALTARGET="$(readlink -f "$1")"
173 mv -f "$1" "${1}.dpkg-tmp"
174 cp -a "$REALTARGET" "$1"
175 ls "$(dirname $1)"
176 fi
177 # delete the key from the keyring
178 $GPG --batch --delete-key --yes "$2"
179 if [ -n "$REALTARGET" ]; then
180 # the real backup is the old link, not the copy we made
181 mv -f "${1}.dpkg-tmp" "${1}~"
182 fi
183 }
184
185 remove_key() {
186 requires_root
187
188 # if a --keyring was given, just remove from there
189 if [ -n "$FORCED_KEYRING" ]; then
190 remove_key_from_keyring "$FORCED_KEYRING" "$1"
191 else
192 # otherwise all known keyrings are up for inspection
193 local TRUSTEDFILE="/etc/apt/trusted.gpg"
194 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
195 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
196 remove_key_from_keyring "$TRUSTEDFILE" "$1"
197 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
198 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
199 if [ -d "$TRUSTEDPARTS" ]; then
200 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
201 remove_key_from_keyring "$trusted" "$1"
202 done
203 fi
204 fi
205 echo "OK"
206 }
207
208
209 usage() {
210 echo "Usage: apt-key [--keyring file] [command] [arguments]"
211 echo
212 echo "Manage apt's list of trusted keys"
213 echo
214 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
215 echo " apt-key del <keyid> - remove the key <keyid>"
216 echo " apt-key export <keyid> - output the key <keyid>"
217 echo " apt-key exportall - output all trusted keys"
218 echo " apt-key update - update keys using the keyring package"
219 echo " apt-key net-update - update keys using the network"
220 echo " apt-key list - list keys"
221 echo " apt-key finger - list fingerprints"
222 echo " apt-key adv - pass advanced options to gpg (download key)"
223 echo
224 echo "If no specific keyring file is given the command applies to all keyring files."
225 }
226
227 while [ -n "$1" ]; do
228 case "$1" in
229 --keyring)
230 shift
231 TRUSTEDFILE="$1"
232 FORCED_KEYRING="$1"
233 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
234 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
235 else
236 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
237 exit 1
238 fi
239 shift
240 ;;
241 --fakeroot)
242 requires_root() { true; }
243 shift
244 ;;
245 --*)
246 echo >&2 "Unknown option: $1"
247 usage
248 exit 1;;
249 *)
250 break;;
251 esac
252 done
253
254 if [ -z "$TRUSTEDFILE" ]; then
255 TRUSTEDFILE="/etc/apt/trusted.gpg"
256 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
257 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
258 if [ -r "$TRUSTEDFILE" ]; then
259 GPG="$GPG --keyring $TRUSTEDFILE"
260 fi
261 GPG="$GPG --primary-keyring $TRUSTEDFILE"
262 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
263 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
264 if [ -d "$TRUSTEDPARTS" ]; then
265 # strip / suffix as gpg will double-slash in that case (#665411)
266 STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
267 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
268 TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
269 fi
270 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
271 GPG="$GPG --keyring $trusted"
272 done
273 fi
274 fi
275
276 command="$1"
277 if [ -z "$command" ]; then
278 usage
279 exit 1
280 fi
281 shift
282
283 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
284 echo >&2 "Warning: gnupg does not seem to be installed."
285 echo >&2 "Warning: apt-key requires gnupg for most operations."
286 echo >&2
287 fi
288
289 case "$command" in
290 add)
291 requires_root
292 init_keyring "$TRUSTEDFILE"
293 $GPG --quiet --batch --import "$1"
294 echo "OK"
295 ;;
296 del|rm|remove)
297 init_keyring "$TRUSTEDFILE"
298 remove_key "$1"
299 ;;
300 update)
301 init_keyring "$TRUSTEDFILE"
302 update
303 ;;
304 net-update)
305 init_keyring "$TRUSTEDFILE"
306 net_update
307 ;;
308 list)
309 init_keyring "$TRUSTEDFILE"
310 $GPG --batch --list-keys
311 ;;
312 finger*)
313 init_keyring "$TRUSTEDFILE"
314 $GPG --batch --fingerprint
315 ;;
316 export)
317 init_keyring "$TRUSTEDFILE"
318 $GPG --armor --export "$1"
319 ;;
320 exportall)
321 init_keyring "$TRUSTEDFILE"
322 $GPG --armor --export
323 ;;
324 adv*)
325 init_keyring "$TRUSTEDFILE"
326 echo "Executing: $GPG $*"
327 $GPG $*
328 ;;
329 help)
330 usage
331 ;;
332 *)
333 usage
334 exit 1
335 ;;
336 esac