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