]> git.saurik.com Git - apt.git/blame - cmdline/apt-key
* apt-pkg/deb/dpkgpm.cc:
[apt.git] / cmdline / apt-key
CommitLineData
b3d44315
MV
1#!/bin/sh
2
3set -e
dac074b0 4unset GREP_OPTIONS
b3d44315 5
4a242c5b
MV
6# We don't use a secret keyring, of course, but gpg panics and
7# implodes if there isn't one available
285feb3c
DK
8SECRETKEYRING="$(mktemp)"
9trap "rm -f '${SECRETKEYRING}'" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
331eea38 10GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring ${SECRETKEYRING}"
248ec5ab 11
d4e80f1f 12if [ "$(id -u)" -eq 0 ]; then
285feb3c 13 # we could use a tmpfile here too, but creation of this tends to be time-consuming
248ec5ab
DK
14 GPG_CMD="$GPG_CMD --trustdb-name /etc/apt/trustdb.gpg"
15fi
16
46e39c8e 17GPG="$GPG_CMD"
4a242c5b 18
7fbe42c0 19MASTER_KEYRING=""
848ecfa4 20ARCHIVE_KEYRING_URI=""
7fbe42c0 21#MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
848ecfa4
MV
22#ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
23
7ef96224
MV
24ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
25REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
4a242c5b 26
00c6e1a3
MV
27requires_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
7fbe42c0 34add_keys_with_verify_against_master_keyring() {
8c56b1e0
MV
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:
3a341a1d
MV
49 # all keys that are exported must have a valid signature
50 # from a key in the $distro-master-keyring
8c56b1e0
MV
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
c9bb37c7 54 ADDED=0
8c56b1e0 55 for master_key in $master_keys; do
c9bb37c7 56 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
3916f941 57 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
c9bb37c7 58 ADDED=1
8c56b1e0 59 fi
7fbe42c0 60 done
c9bb37c7
MV
61 if [ $ADDED = 0 ]; then
62 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
63 fi
8c56b1e0 64 done
7fbe42c0 65}
4a242c5b 66
848ecfa4
MV
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)
70net_update() {
71 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
00c6e1a3 72 echo >&2 "ERROR: Your distribution is not supported in net-update as no uri for the archive-keyring is set"
46e39c8e
MV
73 exit 1
74 fi
00c6e1a3 75 requires_root
46e39c8e
MV
76 # in theory we would need to depend on wget for this, but this feature
77 # isn't useable in debian anyway as we have no keyring uri nor a master key
78 if ! which wget >/dev/null 2>&1; then
00c6e1a3 79 echo >&2 "ERROR: an installed wget is required for a network-based update"
46e39c8e 80 exit 1
848ecfa4
MV
81 fi
82 if [ ! -d /var/lib/apt/keyrings ]; then
83 mkdir -p /var/lib/apt/keyrings
84 fi
20ba2505 85 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
93886541
MV
86 old_mtime=0
87 if [ -e $keyring ]; then
20ba2505 88 old_mtime=$(stat -c %Y $keyring)
93886541 89 fi
848ecfa4 90 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
93886541
MV
91 if [ ! -e $keyring ]; then
92 return
93 fi
20ba2505 94 new_mtime=$(stat -c %Y $keyring)
93886541 95 if [ $new_mtime -ne $old_mtime ]; then
8dd0686e 96 echo "Checking for new archive signing keys now"
93886541
MV
97 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
98 fi
848ecfa4
MV
99}
100
4a242c5b
MV
101update() {
102 if [ ! -f $ARCHIVE_KEYRING ]; then
103 echo >&2 "ERROR: Can't find the archive-keyring"
5fdd72f2 104 echo >&2 "Is the debian-archive-keyring package installed?"
4a242c5b
MV
105 exit 1
106 fi
00c6e1a3 107 requires_root
4a242c5b 108
3a341a1d
MV
109 # add new keys from the package;
110
111 # we do not use add_keys_with_verify_against_master_keyring here,
1171258a 112 # because "update" is run on regular package updates. A
3a341a1d
MV
113 # attacker might as well replace the master-archive-keyring file
114 # in the package and add his own keys. so this check wouldn't
115 # add any security. we *need* this check on net-update though
116 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
4a242c5b 117
364af2ef
MV
118 if [ -r "$REMOVED_KEYS" ]; then
119 # remove no-longer supported/used keys
120 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
121 for key in $keys; do
122 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
123 $GPG --quiet --batch --delete-key --yes ${key}
124 fi
125 done
126 else
127 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
128 fi
4a242c5b
MV
129}
130
7fbe42c0 131
b3d44315 132usage() {
46e39c8e 133 echo "Usage: apt-key [--keyring file] [command] [arguments]"
b3d44315
MV
134 echo
135 echo "Manage apt's list of trusted keys"
136 echo
137 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
138 echo " apt-key del <keyid> - remove the key <keyid>"
bf6d5b42
OS
139 echo " apt-key export <keyid> - output the key <keyid>"
140 echo " apt-key exportall - output all trusted keys"
4a242c5b 141 echo " apt-key update - update keys using the keyring package"
848ecfa4 142 echo " apt-key net-update - update keys using the network"
b3d44315 143 echo " apt-key list - list keys"
a8cabc8f
LB
144 echo " apt-key finger - list fingerprints"
145 echo " apt-key adv - pass advanced options to gpg (download key)"
b3d44315 146 echo
46e39c8e 147 echo "If no specific keyring file is given the command applies to all keyring files."
b3d44315
MV
148}
149
46e39c8e
MV
150# Determine on which keyring we want to work
151if [ "$1" = "--keyring" ]; then
152 #echo "keyfile given"
153 shift
154 TRUSTEDFILE="$1"
9120d409 155 if [ -r "$TRUSTEDFILE" ] || [ "$2" = 'add' ]; then
46e39c8e
MV
156 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
157 else
158 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
159 exit 1
160 fi
161 shift
162# otherwise use the default
163else
164 #echo "generate list"
165 TRUSTEDFILE="/etc/apt/trusted.gpg"
6520109c 166 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
1f8b2599 167 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
46e39c8e
MV
168 if [ -r "$TRUSTEDFILE" ]; then
169 GPG="$GPG --keyring $TRUSTEDFILE"
170 fi
171 GPG="$GPG --primary-keyring $TRUSTEDFILE"
172 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
1f8b2599 173 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
46e39c8e
MV
174 if [ -d "$TRUSTEDPARTS" ]; then
175 #echo "parts active"
176 for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
177 #echo "part -> $trusted"
178 GPG="$GPG --keyring $trusted"
179 done
180 fi
181fi
182#echo "COMMAND: $GPG"
183
b3d44315
MV
184command="$1"
185if [ -z "$command" ]; then
186 usage
187 exit 1
188fi
189shift
190
191if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
192 echo >&2 "Warning: gnupg does not seem to be installed."
193 echo >&2 "Warning: apt-key requires gnupg for most operations."
194 echo >&2
195fi
196
b3d44315
MV
197case "$command" in
198 add)
00c6e1a3 199 requires_root
b3d44315
MV
200 $GPG --quiet --batch --import "$1"
201 echo "OK"
202 ;;
203 del|rm|remove)
00c6e1a3 204 requires_root
b3d44315
MV
205 $GPG --quiet --batch --delete-key --yes "$1"
206 echo "OK"
207 ;;
4a242c5b
MV
208 update)
209 update
210 ;;
848ecfa4
MV
211 net-update)
212 net_update
213 ;;
b3d44315
MV
214 list)
215 $GPG --batch --list-keys
216 ;;
217 finger*)
218 $GPG --batch --fingerprint
219 ;;
bf6d5b42
OS
220 export)
221 $GPG --armor --export "$1"
222 ;;
223 exportall)
224 $GPG --armor --export
225 ;;
b3d44315
MV
226 adv*)
227 echo "Executing: $GPG $*"
228 $GPG $*
229 ;;
230 help)
231 usage
232 ;;
233 *)
234 usage
235 exit 1
236 ;;
237esac