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