]>
Commit | Line | Data |
---|---|---|
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 | ||
9 | GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg" | |
10 | GPG="$GPG_CMD --keyring /etc/apt/trusted.gpg" | |
11 | ||
12 | ||
13 | MASTER_KEYRING=/usr/share/keyrings/ubuntu-master-keyring.gpg | |
14 | ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg | |
15 | REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg | |
16 | ARCHIVE_KEYRING_URI=http://archive.ubuntu.com/ubuntu/project/ubuntu-archive-keyring.gpg | |
17 | ||
18 | add_keys_with_verify_against_master_keyring() { | |
19 | ADD_KEYRING=$1 | |
20 | MASTER=$2 | |
21 | ||
22 | if [ ! -f "$ADD_KEYRING" ]; then | |
23 | echo "ERROR: '$ADD_KEYRING' not found" | |
24 | return | |
25 | fi | |
26 | if [ ! -f "$MASTER" ]; then | |
27 | echo "ERROR: '$MASTER' not found" | |
28 | return | |
29 | fi | |
30 | ||
31 | # when adding new keys, make sure that the archive-master-keyring | |
32 | # is honored. so: | |
33 | # all keys that are exported must have a valid signature | |
34 | # from a key in the $distro-master-keyring | |
35 | add_keys=`$GPG_CMD --keyring $ADD_KEYRING --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
36 | master_keys=`$GPG_CMD --keyring $MASTER --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
37 | for add_key in $add_keys; do | |
38 | ADDED=0 | |
39 | for master_key in $master_keys; do | |
40 | if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then | |
41 | $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import | |
42 | ADDED=1 | |
43 | fi | |
44 | done | |
45 | if [ $ADDED = 0 ]; then | |
46 | echo >&2 "Key '$add_key' not added. It is not signed with a master key" | |
47 | fi | |
48 | done | |
49 | } | |
50 | ||
51 | # update the current archive signing keyring from a network URI | |
52 | # the archive-keyring keys needs to be signed with the master key | |
53 | # (otherwise it does not make sense from a security POV) | |
54 | net_update() { | |
55 | if [ -z "$ARCHIVE_KEYRING_URI" ]; then | |
56 | echo "ERROR: no location for the archive-keyring given" | |
57 | fi | |
58 | if [ ! -d /var/lib/apt/keyrings ]; then | |
59 | mkdir -p /var/lib/apt/keyrings | |
60 | fi | |
61 | keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING) | |
62 | old_mtime=0 | |
63 | if [ -e $keyring ]; then | |
64 | old_mtime=$(stat -c %Y $keyring) | |
65 | fi | |
66 | (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI) | |
67 | if [ ! -e $keyring ]; then | |
68 | return | |
69 | fi | |
70 | new_mtime=$(stat -c %Y $keyring) | |
71 | if [ $new_mtime -ne $old_mtime ]; then | |
72 | echo "Checking for new archive signing keys now" | |
73 | add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING | |
74 | fi | |
75 | } | |
76 | ||
77 | update() { | |
78 | if [ ! -f $ARCHIVE_KEYRING ]; then | |
79 | echo >&2 "ERROR: Can't find the archive-keyring" | |
80 | echo >&2 "Is the ubuntu-keyring package installed?" | |
81 | exit 1 | |
82 | fi | |
83 | ||
84 | # add new keys from the package; | |
85 | ||
86 | # we do not use add_keys_with_verify_against_master_keyring here, | |
87 | # because we "update" is run on regular package updates. A | |
88 | # attacker might as well replace the master-archive-keyring file | |
89 | # in the package and add his own keys. so this check wouldn't | |
90 | # add any security. we *need* this check on net-update though | |
91 | $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import | |
92 | ||
93 | # remove no-longer supported/used keys | |
94 | keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5` | |
95 | for key in $keys; do | |
96 | if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then | |
97 | $GPG --quiet --batch --delete-key --yes ${key} | |
98 | fi | |
99 | done | |
100 | } | |
101 | ||
102 | ||
103 | usage() { | |
104 | echo "Usage: apt-key [command] [arguments]" | |
105 | echo | |
106 | echo "Manage apt's list of trusted keys" | |
107 | echo | |
108 | echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)" | |
109 | echo " apt-key del <keyid> - remove the key <keyid>" | |
110 | echo " apt-key export <keyid> - output the key <keyid>" | |
111 | echo " apt-key exportall - output all trusted keys" | |
112 | echo " apt-key update - update keys using the keyring package" | |
113 | echo " apt-key net-update - update keys using the network" | |
114 | echo " apt-key list - list keys" | |
115 | echo " apt-key finger - list fingerprints" | |
116 | echo " apt-key adv - pass advanced options to gpg (download key)" | |
117 | echo | |
118 | } | |
119 | ||
120 | command="$1" | |
121 | if [ -z "$command" ]; then | |
122 | usage | |
123 | exit 1 | |
124 | fi | |
125 | shift | |
126 | ||
127 | if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then | |
128 | echo >&2 "Warning: gnupg does not seem to be installed." | |
129 | echo >&2 "Warning: apt-key requires gnupg for most operations." | |
130 | echo >&2 | |
131 | fi | |
132 | ||
133 | case "$command" in | |
134 | add) | |
135 | $GPG --quiet --batch --import "$1" | |
136 | echo "OK" | |
137 | ;; | |
138 | del|rm|remove) | |
139 | $GPG --quiet --batch --delete-key --yes "$1" | |
140 | echo "OK" | |
141 | ;; | |
142 | update) | |
143 | update | |
144 | ;; | |
145 | net-update) | |
146 | net_update | |
147 | ;; | |
148 | list) | |
149 | $GPG --batch --list-keys | |
150 | ;; | |
151 | finger*) | |
152 | $GPG --batch --fingerprint | |
153 | ;; | |
154 | export) | |
155 | $GPG --armor --export "$1" | |
156 | ;; | |
157 | exportall) | |
158 | $GPG --armor --export | |
159 | ;; | |
160 | adv*) | |
161 | echo "Executing: $GPG $*" | |
162 | $GPG $* | |
163 | ;; | |
164 | help) | |
165 | usage | |
166 | ;; | |
167 | *) | |
168 | usage | |
169 | exit 1 | |
170 | ;; | |
171 | esac |