use mkdtemp() instead of tempnam, free mkdtemp() data afterwards, return true in...
[ntk/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 eval $(apt-config shell TRUSTEDFILE Apt::GPGV::TrustedKeyring)
150 eval $(apt-config shell TRUSTEDFILE Dir::Etc::Trusted/f)
151 if [ -r "$TRUSTEDFILE" ]; then
152 GPG="$GPG --keyring $TRUSTEDFILE"
153 fi
154 GPG="$GPG --primary-keyring $TRUSTEDFILE"
155 TRUSTEDPARTS="/etc/apt/trusted.gpg.d"
156 eval $(apt-config shell TRUSTEDPARTS Dir::Etc::TrustedParts/d)
157 if [ -d "$TRUSTEDPARTS" ]; then
158 #echo "parts active"
159 for trusted in $(run-parts --list $TRUSTEDPARTS --regex '^.*\.gpg$'); do
160 #echo "part -> $trusted"
161 GPG="$GPG --keyring $trusted"
162 done
163 fi
164 fi
165 #echo "COMMAND: $GPG"
166
167 command="$1"
168 if [ -z "$command" ]; then
169 usage
170 exit 1
171 fi
172 shift
173
174 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
175 echo >&2 "Warning: gnupg does not seem to be installed."
176 echo >&2 "Warning: apt-key requires gnupg for most operations."
177 echo >&2
178 fi
179
180 case "$command" in
181 add)
182 $GPG --quiet --batch --import "$1"
183 echo "OK"
184 ;;
185 del|rm|remove)
186 $GPG --quiet --batch --delete-key --yes "$1"
187 echo "OK"
188 ;;
189 update)
190 update
191 ;;
192 net-update)
193 net_update
194 ;;
195 list)
196 $GPG --batch --list-keys
197 ;;
198 finger*)
199 $GPG --batch --fingerprint
200 ;;
201 export)
202 $GPG --armor --export "$1"
203 ;;
204 exportall)
205 $GPG --armor --export
206 ;;
207 adv*)
208 echo "Executing: $GPG $*"
209 $GPG $*
210 ;;
211 help)
212 usage
213 ;;
214 *)
215 usage
216 exit 1
217 ;;
218 esac