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