make the keyring locations in apt-key configurable
[ntk/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
155 usage() {
156 echo "Usage: apt-key [--keyring file] [command] [arguments]"
157 echo
158 echo "Manage apt's list of trusted keys"
159 echo
160 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
161 echo " apt-key del <keyid> - remove the key <keyid>"
162 echo " apt-key export <keyid> - output the key <keyid>"
163 echo " apt-key exportall - output all trusted keys"
164 echo " apt-key update - update keys using the keyring package"
165 echo " apt-key net-update - update keys using the network"
166 echo " apt-key list - list keys"
167 echo " apt-key finger - list fingerprints"
168 echo " apt-key adv - pass advanced options to gpg (download key)"
169 echo
170 echo "If no specific keyring file is given the command applies to all keyring files."
171 }
172
173 while [ -n "$1" ]; do
174 case "$1" in
175 --keyring)
176 shift
177 TRUSTEDFILE="$1"
178 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ] || [ "$2" = 'adv' ]; then
179 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
180 else
181 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
182 exit 1
183 fi
184 shift
185 ;;
186 --fakeroot)
187 requires_root() { true; }
188 shift
189 ;;
190 --*)
191 echo >&2 "Unknown option: $1"
192 usage
193 exit 1;;
194 *)
195 break;;
196 esac
197 done
198
199 if [ -z "$TRUSTEDFILE" ]; then
200 TRUSTEDFILE="/etc/apt/trusted.gpg"
201 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
202 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
203 if [ -r "$TRUSTEDFILE" ]; then
204 GPG="$GPG --keyring $TRUSTEDFILE"
205 fi
206 GPG="$GPG --primary-keyring $TRUSTEDFILE"
207 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
208 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
209 if [ -d "$TRUSTEDPARTS" ]; then
210 # strip / suffix as gpg will double-slash in that case (#665411)
211 STRIPPED_TRUSTEDPARTS="${TRUSTEDPARTS%/}"
212 if [ "${STRIPPED_TRUSTEDPARTS}/" = "$TRUSTEDPARTS" ]; then
213 TRUSTEDPARTS="$STRIPPED_TRUSTEDPARTS"
214 fi
215 for trusted in $(run-parts --list "$TRUSTEDPARTS" --regex '^.*\.gpg$'); do
216 GPG="$GPG --keyring $trusted"
217 done
218 fi
219 fi
220
221 command="$1"
222 if [ -z "$command" ]; then
223 usage
224 exit 1
225 fi
226 shift
227
228 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
229 echo >&2 "Warning: gnupg does not seem to be installed."
230 echo >&2 "Warning: apt-key requires gnupg for most operations."
231 echo >&2
232 fi
233
234 case "$command" in
235 add)
236 requires_root
237 init_keyring "$TRUSTEDFILE"
238 $GPG --quiet --batch --import "$1"
239 echo "OK"
240 ;;
241 del|rm|remove)
242 requires_root
243 init_keyring "$TRUSTEDFILE"
244 $GPG --quiet --batch --delete-key --yes "$1"
245 echo "OK"
246 ;;
247 update)
248 init_keyring "$TRUSTEDFILE"
249 update
250 ;;
251 net-update)
252 init_keyring "$TRUSTEDFILE"
253 net_update
254 ;;
255 list)
256 init_keyring "$TRUSTEDFILE"
257 $GPG --batch --list-keys
258 ;;
259 finger*)
260 init_keyring "$TRUSTEDFILE"
261 $GPG --batch --fingerprint
262 ;;
263 export)
264 init_keyring "$TRUSTEDFILE"
265 $GPG --armor --export "$1"
266 ;;
267 exportall)
268 init_keyring "$TRUSTEDFILE"
269 $GPG --armor --export
270 ;;
271 adv*)
272 init_keyring "$TRUSTEDFILE"
273 echo "Executing: $GPG $*"
274 $GPG $*
275 ;;
276 help)
277 usage
278 ;;
279 *)
280 usage
281 exit 1
282 ;;
283 esac