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