]> git.saurik.com Git - apt.git/blob - cmdline/apt-key
merged debian/sid branch from git.debian.org
[apt.git] / cmdline / apt-key
1 #!/bin/sh
2
3 set -e
4 unset GREP_OPTIONS
5
6 # We don't use a secret keyring, of course, but gpg panics and
7 # implodes if there isn't one available
8 SECRETKEYRING="$(mktemp)"
9 trap "rm -f '${SECRETKEYRING}'" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
10 GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring ${SECRETKEYRING}"
11
12 if [ "$(id -u)" -eq 0 ]; then
13 # we could use a tmpfile here too, but creation of this tends to be time-consuming
14 eval $(apt-config shell TRUSTDBDIR Dir::Etc/d)
15 GPG_CMD="$GPG_CMD --trustdb-name ${TRUSTDBDIR}/trustdb.gpg"
16 fi
17
18 GPG="$GPG_CMD"
19
20
21 # ubuntu keyrings
22 MASTER_KEYRING=/usr/share/keyrings/ubuntu-master-keyring.gpg
23 ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg
24 REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg
25 ARCHIVE_KEYRING_URI=http://archive.ubuntu.com/ubuntu/project/ubuntu-archive-keyring.gpg
26 TMP_KEYRING=/var/lib/apt/keyrings/maybe-import-keyring.gpg
27
28 requires_root() {
29 if [ "$(id -u)" -ne 0 ]; then
30 echo >&1 "ERROR: This command can only be used by root."
31 exit 1
32 fi
33 }
34
35 # gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
36 init_keyring() {
37 for path; do
38 if ! [ -e "$path" ]; then
39 touch -- "$path"
40 chmod 0644 -- "$path"
41 fi
42 done
43 }
44
45 add_keys_with_verify_against_master_keyring() {
46 ADD_KEYRING=$1
47 MASTER=$2
48
49 if [ ! -f "$ADD_KEYRING" ]; then
50 echo "ERROR: '$ADD_KEYRING' not found"
51 return
52 fi
53 if [ ! -f "$MASTER" ]; then
54 echo "ERROR: '$MASTER' not found"
55 return
56 fi
57
58 # when adding new keys, make sure that the archive-master-keyring
59 # is honored. so:
60 # all keys that are exported must have a valid signature
61 # from a key in the $distro-master-keyring
62 add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
63 all_add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^[ps]ub | cut -d: -f5`
64 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
65
66 # ensure there are no colisions LP: #857472
67 for all_add_key in $all_add_keys; do
68 for master_key in $master_keys; do
69 if [ "$all_add_key" = "$master_key" ]; then
70 echo >&2 "Keyid collision for '$all_add_key' detected, operation aborted"
71 return 1
72 fi
73 done
74 done
75
76 for add_key in $add_keys; do
77 # export the add keyring one-by-one
78 rm -f $TMP_KEYRING
79 $GPG_CMD --keyring $ADD_KEYRING --output $TMP_KEYRING --export $add_key
80 # check if signed with the master key and only add in this case
81 ADDED=0
82 for master_key in $master_keys; do
83 if $GPG_CMD --keyring $MASTER --keyring $TMP_KEYRING --check-sigs --with-colons $add_key | grep '^sig:!:' | cut -d: -f5 | grep -q $master_key; then
84 $GPG --import $TMP_KEYRING
85 ADDED=1
86 fi
87 done
88 if [ $ADDED = 0 ]; then
89 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
90 fi
91 done
92 rm -f $TMP_KEYRING
93 }
94
95 # update the current archive signing keyring from a network URI
96 # the archive-keyring keys needs to be signed with the master key
97 # (otherwise it does not make sense from a security POV)
98 net_update() {
99 # Disabled for now as code is insecure (LP: #1013639 (and 857472, 1013128))
100 exit 1
101
102 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
103 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
104 exit 1
105 fi
106 requires_root
107 # in theory we would need to depend on wget for this, but this feature
108 # isn't useable in debian anyway as we have no keyring uri nor a master key
109 if ! which wget >/dev/null 2>&1; then
110 echo >&2 "ERROR: an installed wget is required for a network-based update"
111 exit 1
112 fi
113 if [ ! -d /var/lib/apt/keyrings ]; then
114 mkdir -p /var/lib/apt/keyrings
115 fi
116 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
117 old_mtime=0
118 if [ -e $keyring ]; then
119 old_mtime=$(stat -c %Y $keyring)
120 fi
121 (cd /var/lib/apt/keyrings; wget --timeout=90 -q -N $ARCHIVE_KEYRING_URI)
122 if [ ! -e $keyring ]; then
123 return
124 fi
125 new_mtime=$(stat -c %Y $keyring)
126 if [ $new_mtime -ne $old_mtime ]; then
127 echo "Checking for new archive signing keys now"
128 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
129 fi
130 }
131
132 update() {
133 if [ ! -f $ARCHIVE_KEYRING ]; then
134 echo >&2 "ERROR: Can't find the archive-keyring"
135 echo >&2 "Is the ubuntu-keyring package installed?"
136 exit 1
137 fi
138 requires_root
139
140 # add new keys from the package;
141
142 # we do not use add_keys_with_verify_against_master_keyring here,
143 # because "update" is run on regular package updates. A
144 # attacker might as well replace the master-archive-keyring file
145 # in the package and add his own keys. so this check wouldn't
146 # add any security. we *need* this check on net-update though
147 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
148
149 if [ -r "$REMOVED_KEYS" ]; then
150 # remove no-longer supported/used keys
151 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
152 for key in $keys; do
153 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
154 $GPG --quiet --batch --delete-key --yes ${key}
155 fi
156 done
157 else
158 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
159 fi
160 }
161
162
163 usage() {
164 echo "Usage: apt-key [--keyring file] [command] [arguments]"
165 echo
166 echo "Manage apt's list of trusted keys"
167 echo
168 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
169 echo " apt-key del <keyid> - remove the key <keyid>"
170 echo " apt-key export <keyid> - output the key <keyid>"
171 echo " apt-key exportall - output all trusted keys"
172 echo " apt-key update - update keys using the keyring package"
173 echo " apt-key net-update - update keys using the network"
174 echo " apt-key list - list keys"
175 echo " apt-key finger - list fingerprints"
176 echo " apt-key adv - pass advanced options to gpg (download key)"
177 echo
178 echo "If no specific keyring file is given the command applies to all keyring files."
179 }
180
181 # Determine on which keyring we want to work
182 if [ "$1" = "--keyring" ]; then
183 #echo "keyfile given"
184 shift
185 TRUSTEDFILE="$1"
186 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
187 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
188 else
189 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
190 exit 1
191 fi
192 shift
193 # otherwise use the default
194 else
195 #echo "generate list"
196 TRUSTEDFILE="/etc/apt/trusted.gpg"
197 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
198 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
199 if [ -r "$TRUSTEDFILE" ]; then
200 GPG="$GPG --keyring $TRUSTEDFILE"
201 fi
202 GPG="$GPG --primary-keyring $TRUSTEDFILE"
203 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
204 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
205 if [ -d "$TRUSTEDPARTS" ]; then
206 #echo "parts active"
207 for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
208 #echo "part -> $trusted"
209 GPG="$GPG --keyring $trusted"
210 done
211 fi
212 fi
213 #echo "COMMAND: $GPG"
214
215 command="$1"
216 if [ -z "$command" ]; then
217 usage
218 exit 1
219 fi
220 shift
221
222 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
223 echo >&2 "Warning: gnupg does not seem to be installed."
224 echo >&2 "Warning: apt-key requires gnupg for most operations."
225 echo >&2
226 fi
227
228 case "$command" in
229 add)
230 requires_root
231 init_keyring "$TRUSTEDFILE"
232 $GPG --quiet --batch --import "$1"
233 echo "OK"
234 ;;
235 del|rm|remove)
236 requires_root
237 init_keyring "$TRUSTEDFILE"
238 $GPG --quiet --batch --delete-key --yes "$1"
239 echo "OK"
240 ;;
241 update)
242 init_keyring "$TRUSTEDFILE"
243 update
244 ;;
245 net-update)
246 init_keyring "$TRUSTEDFILE"
247 net_update
248 ;;
249 list)
250 init_keyring "$TRUSTEDFILE"
251 $GPG --batch --list-keys
252 ;;
253 finger*)
254 init_keyring "$TRUSTEDFILE"
255 $GPG --batch --fingerprint
256 ;;
257 export)
258 init_keyring "$TRUSTEDFILE"
259 $GPG --armor --export "$1"
260 ;;
261 exportall)
262 init_keyring "$TRUSTEDFILE"
263 $GPG --armor --export
264 ;;
265 adv*)
266 init_keyring "$TRUSTEDFILE"
267 echo "Executing: $GPG $*"
268 $GPG $*
269 ;;
270 help)
271 usage
272 ;;
273 *)
274 usage
275 exit 1
276 ;;
277 esac