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