]> git.saurik.com Git - apt.git/blob - cmdline/apt-key
apt-pkg/packagemanager.cc: more debug output for debug::pkgPackageManager
[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 GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
9 GPG="$GPG_CMD"
10
11 MASTER_KEYRING=""
12 ARCHIVE_KEYRING_URI=""
13 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
14 #ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
15
16 ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
17 REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
18
19 add_keys_with_verify_against_master_keyring() {
20 ADD_KEYRING=$1
21 MASTER=$2
22
23 if [ ! -f "$ADD_KEYRING" ]; then
24 echo "ERROR: '$ADD_KEYRING' not found"
25 return
26 fi
27 if [ ! -f "$MASTER" ]; then
28 echo "ERROR: '$MASTER' not found"
29 return
30 fi
31
32 # when adding new keys, make sure that the archive-master-keyring
33 # is honored. so:
34 # all keys that are exported must have a valid signature
35 # from a key in the $distro-master-keyring
36 add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5`
37 master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5`
38 for add_key in $add_keys; do
39 ADDED=0
40 for master_key in $master_keys; do
41 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
42 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
43 ADDED=1
44 fi
45 done
46 if [ $ADDED = 0 ]; then
47 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
48 fi
49 done
50 }
51
52 # update the current archive signing keyring from a network URI
53 # the archive-keyring keys needs to be signed with the master key
54 # (otherwise it does not make sense from a security POV)
55 net_update() {
56 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
57 echo "ERROR: no location for the archive-keyring given"
58 exit 1
59 fi
60 # in theory we would need to depend on wget for this, but this feature
61 # isn't useable in debian anyway as we have no keyring uri nor a master key
62 if ! which wget >/dev/null 2>&1; then
63 echo "ERROR: an installed wget is required for a network-based update"
64 exit 1
65 fi
66 if [ ! -d /var/lib/apt/keyrings ]; then
67 mkdir -p /var/lib/apt/keyrings
68 fi
69 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
70 old_mtime=0
71 if [ -e $keyring ]; then
72 old_mtime=$(stat -c %Y $keyring)
73 fi
74 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
75 if [ ! -e $keyring ]; then
76 return
77 fi
78 new_mtime=$(stat -c %Y $keyring)
79 if [ $new_mtime -ne $old_mtime ]; then
80 echo "Checking for new archive signing keys now"
81 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
82 fi
83 }
84
85 update() {
86 if [ ! -f $ARCHIVE_KEYRING ]; then
87 echo >&2 "ERROR: Can't find the archive-keyring"
88 echo >&2 "Is the debian-archive-keyring package installed?"
89 exit 1
90 fi
91
92 # add new keys from the package;
93
94 # we do not use add_keys_with_verify_against_master_keyring here,
95 # because "update" is run on regular package updates. A
96 # attacker might as well replace the master-archive-keyring file
97 # in the package and add his own keys. so this check wouldn't
98 # add any security. we *need* this check on net-update though
99 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
100
101 if [ -r "$REMOVED_KEYS" ]; then
102 # remove no-longer supported/used keys
103 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
104 for key in $keys; do
105 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
106 $GPG --quiet --batch --delete-key --yes ${key}
107 fi
108 done
109 else
110 echo "Warning: removed keys keyring $REMOVED_KEYS missing or not readable" >&2
111 fi
112 }
113
114
115 usage() {
116 echo "Usage: apt-key [--keyring file] [command] [arguments]"
117 echo
118 echo "Manage apt's list of trusted keys"
119 echo
120 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
121 echo " apt-key del <keyid> - remove the key <keyid>"
122 echo " apt-key export <keyid> - output the key <keyid>"
123 echo " apt-key exportall - output all trusted keys"
124 echo " apt-key update - update keys using the keyring package"
125 echo " apt-key net-update - update keys using the network"
126 echo " apt-key list - list keys"
127 echo " apt-key finger - list fingerprints"
128 echo " apt-key adv - pass advanced options to gpg (download key)"
129 echo
130 echo "If no specific keyring file is given the command applies to all keyring files."
131 }
132
133 # Determine on which keyring we want to work
134 if [ "$1" = "--keyring" ]; then
135 #echo "keyfile given"
136 shift
137 TRUSTEDFILE="$1"
138 if [ -r "$TRUSTEDFILE" ]; then
139 GPG="$GPG --keyring $TRUSTEDFILE --primary-keyring $TRUSTEDFILE"
140 else
141 echo >&2 "Error: The specified keyring »$TRUSTEDFILE« is missing or not readable"
142 exit 1
143 fi
144 shift
145 # otherwise use the default
146 else
147 #echo "generate list"
148 TRUSTEDFILE="/etc/apt/trusted.gpg"
149 if [ -r "$TRUSTEDFILE" ]; then
150 GPG="$GPG --keyring $TRUSTEDFILE"
151 fi
152 GPG="$GPG --primary-keyring $TRUSTEDFILE"
153 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
154 if [ -d "$TRUSTEDPARTS" ]; then
155 #echo "parts active"
156 for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
157 #echo "part -> $trusted"
158 GPG="$GPG --keyring $trusted"
159 done
160 fi
161 fi
162 #echo "COMMAND: $GPG"
163
164 command="$1"
165 if [ -z "$command" ]; then
166 usage
167 exit 1
168 fi
169 shift
170
171 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
172 echo >&2 "Warning: gnupg does not seem to be installed."
173 echo >&2 "Warning: apt-key requires gnupg for most operations."
174 echo >&2
175 fi
176
177 case "$command" in
178 add)
179 $GPG --quiet --batch --import "$1"
180 echo "OK"
181 ;;
182 del|rm|remove)
183 $GPG --quiet --batch --delete-key --yes "$1"
184 echo "OK"
185 ;;
186 update)
187 update
188 ;;
189 net-update)
190 net_update
191 ;;
192 list)
193 $GPG --batch --list-keys
194 ;;
195 finger*)
196 $GPG --batch --fingerprint
197 ;;
198 export)
199 $GPG --armor --export "$1"
200 ;;
201 exportall)
202 $GPG --armor --export
203 ;;
204 adv*)
205 echo "Executing: $GPG $*"
206 $GPG $*
207 ;;
208 help)
209 usage
210 ;;
211 *)
212 usage
213 exit 1
214 ;;
215 esac