]> git.saurik.com Git - apt.git/blame - cmdline/apt-key
ensure that testcases exiting because of set -e aren't successful
[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
MV
28MASTER_KEYRING=""
29#MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
80f3aeb0
DK
30eval $(apt-config shell MASTER_KEYRING APT::Key::MasterKeyring)
31ARCHIVE_KEYRING_URI=""
848ecfa4 32#ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
80f3aeb0 33eval $(apt-config shell ARCHIVE_KEYRING_URI APT::Key::ArchiveKeyringURI)
848ecfa4 34
7ef96224 35ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
80f3aeb0 36eval $(apt-config shell ARCHIVE_KEYRING APT::Key::ArchiveKeyring)
7ef96224 37REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
80f3aeb0 38eval $(apt-config shell REMOVED_KEYS APT::Key::RemovedKeys)
4a242c5b 39
00c6e1a3
MV
40requires_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
5de34668
JK
47# gpg defaults to mode 0600 for new keyrings. Create one with 0644 instead.
48init_keyring() {
49 for path; do
50 if ! [ -e "$path" ]; then
51 touch -- "$path"
52 chmod 0644 -- "$path"
53 fi
54 done
55}
56
7fbe42c0 57add_keys_with_verify_against_master_keyring() {
8c56b1e0
MV
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:
3a341a1d
MV
72 # all keys that are exported must have a valid signature
73 # from a key in the $distro-master-keyring
8c56b1e0
MV
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
c9bb37c7 77 ADDED=0
8c56b1e0 78 for master_key in $master_keys; do
c9bb37c7 79 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
3916f941 80 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
c9bb37c7 81 ADDED=1
8c56b1e0 82 fi
7fbe42c0 83 done
c9bb37c7
MV
84 if [ $ADDED = 0 ]; then
85 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
86 fi
8c56b1e0 87 done
7fbe42c0 88}
4a242c5b 89
848ecfa4
MV
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)
93net_update() {
94 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
00c6e1a3 95 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
46e39c8e
MV
96 exit 1
97 fi
00c6e1a3 98 requires_root
46e39c8e
MV
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
00c6e1a3 102 echo >&2 "ERROR: an installed wget is required for a network-based update"
46e39c8e 103 exit 1
848ecfa4
MV
104 fi
105 if [ ! -d /var/lib/apt/keyrings ]; then
106 mkdir -p /var/lib/apt/keyrings
107 fi
20ba2505 108 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
93886541
MV
109 old_mtime=0
110 if [ -e $keyring ]; then
20ba2505 111 old_mtime=$(stat -c %Y $keyring)
93886541 112 fi
848ecfa4 113 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
93886541
MV
114 if [ ! -e $keyring ]; then
115 return
116 fi
20ba2505 117 new_mtime=$(stat -c %Y $keyring)
93886541 118 if [ $new_mtime -ne $old_mtime ]; then
8dd0686e 119 echo "Checking for new archive signing keys now"
93886541
MV
120 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
121 fi
848ecfa4
MV
122}
123
4a242c5b
MV
124update() {
125 if [ ! -f $ARCHIVE_KEYRING ]; then
126 echo >&2 "ERROR: Can't find the archive-keyring"
5fdd72f2 127 echo >&2 "Is the debian-archive-keyring package installed?"
4a242c5b
MV
128 exit 1
129 fi
00c6e1a3 130 requires_root
4a242c5b 131
3a341a1d
MV
132 # add new keys from the package;
133
134 # we do not use add_keys_with_verify_against_master_keyring here,
1171258a 135 # because "update" is run on regular package updates. A
3a341a1d
MV
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
4a242c5b 140
364af2ef
MV
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
4a242c5b
MV
152}
153
7fbe42c0 154
b3d44315 155usage() {
46e39c8e 156 echo "Usage: apt-key [--keyring file] [command] [arguments]"
b3d44315
MV
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>"
bf6d5b42
OS
162 echo " apt-key export <keyid> - output the key <keyid>"
163 echo " apt-key exportall - output all trusted keys"
4a242c5b 164 echo " apt-key update - update keys using the keyring package"
848ecfa4 165 echo " apt-key net-update - update keys using the network"
b3d44315 166 echo " apt-key list - list keys"
a8cabc8f
LB
167 echo " apt-key finger - list fingerprints"
168 echo " apt-key adv - pass advanced options to gpg (download key)"
b3d44315 169 echo
46e39c8e 170 echo "If no specific keyring file is given the command applies to all keyring files."
b3d44315
MV
171}
172
3dc55197
DK
173while [ -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
197done
198
199if [ -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
59f46f3a
DK
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
3dc55197
DK
216 GPG="$GPG --keyring $trusted"
217 done
218 fi
46e39c8e 219fi
46e39c8e 220
b3d44315
MV
221command="$1"
222if [ -z "$command" ]; then
223 usage
224 exit 1
225fi
226shift
227
228if [ "$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
232fi
233
b3d44315
MV
234case "$command" in
235 add)
00c6e1a3 236 requires_root
5de34668 237 init_keyring "$TRUSTEDFILE"
b3d44315
MV
238 $GPG --quiet --batch --import "$1"
239 echo "OK"
240 ;;
241 del|rm|remove)
00c6e1a3 242 requires_root
5de34668 243 init_keyring "$TRUSTEDFILE"
b3d44315
MV
244 $GPG --quiet --batch --delete-key --yes "$1"
245 echo "OK"
246 ;;
4a242c5b 247 update)
5de34668 248 init_keyring "$TRUSTEDFILE"
4a242c5b
MV
249 update
250 ;;
848ecfa4 251 net-update)
5de34668 252 init_keyring "$TRUSTEDFILE"
848ecfa4
MV
253 net_update
254 ;;
b3d44315 255 list)
5de34668 256 init_keyring "$TRUSTEDFILE"
b3d44315
MV
257 $GPG --batch --list-keys
258 ;;
259 finger*)
5de34668 260 init_keyring "$TRUSTEDFILE"
b3d44315
MV
261 $GPG --batch --fingerprint
262 ;;
bf6d5b42 263 export)
5de34668 264 init_keyring "$TRUSTEDFILE"
bf6d5b42
OS
265 $GPG --armor --export "$1"
266 ;;
267 exportall)
5de34668 268 init_keyring "$TRUSTEDFILE"
bf6d5b42
OS
269 $GPG --armor --export
270 ;;
b3d44315 271 adv*)
5de34668 272 init_keyring "$TRUSTEDFILE"
b3d44315
MV
273 echo "Executing: $GPG $*"
274 $GPG $*
275 ;;
276 help)
277 usage
278 ;;
279 *)
280 usage
281 exit 1
282 ;;
283esac