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