* French manpage translation update
[ntk/apt.git] / cmdline / apt-key
CommitLineData
b3d44315
MV
1#!/bin/sh
2
3set -e
dac074b0 4unset GREP_OPTIONS
b3d44315 5
4a242c5b
MV
6# We don't use a secret keyring, of course, but gpg panics and
7# implodes if there isn't one available
9129f2af 8GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
46e39c8e 9GPG="$GPG_CMD"
4a242c5b 10
7fbe42c0 11MASTER_KEYRING=""
848ecfa4 12ARCHIVE_KEYRING_URI=""
7fbe42c0 13#MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
848ecfa4
MV
14#ARCHIVE_KEYRING_URI=http://ftp.debian.org/debian/debian-archive-keyring.gpg
15
7ef96224
MV
16ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
17REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
4a242c5b 18
7fbe42c0 19add_keys_with_verify_against_master_keyring() {
8c56b1e0
MV
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:
3a341a1d
MV
34 # all keys that are exported must have a valid signature
35 # from a key in the $distro-master-keyring
8c56b1e0
MV
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
c9bb37c7 39 ADDED=0
8c56b1e0 40 for master_key in $master_keys; do
c9bb37c7 41 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
3916f941 42 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
c9bb37c7 43 ADDED=1
8c56b1e0 44 fi
7fbe42c0 45 done
c9bb37c7
MV
46 if [ $ADDED = 0 ]; then
47 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
48 fi
8c56b1e0 49 done
7fbe42c0 50}
4a242c5b 51
848ecfa4
MV
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)
55net_update() {
56 if [ -z "$ARCHIVE_KEYRING_URI" ]; then
46e39c8e
MV
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
848ecfa4
MV
65 fi
66 if [ ! -d /var/lib/apt/keyrings ]; then
67 mkdir -p /var/lib/apt/keyrings
68 fi
20ba2505 69 keyring=/var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING)
93886541
MV
70 old_mtime=0
71 if [ -e $keyring ]; then
20ba2505 72 old_mtime=$(stat -c %Y $keyring)
93886541 73 fi
848ecfa4 74 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
93886541
MV
75 if [ ! -e $keyring ]; then
76 return
77 fi
20ba2505 78 new_mtime=$(stat -c %Y $keyring)
93886541 79 if [ $new_mtime -ne $old_mtime ]; then
8dd0686e 80 echo "Checking for new archive signing keys now"
93886541
MV
81 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
82 fi
848ecfa4
MV
83}
84
4a242c5b
MV
85update() {
86 if [ ! -f $ARCHIVE_KEYRING ]; then
87 echo >&2 "ERROR: Can't find the archive-keyring"
5fdd72f2 88 echo >&2 "Is the debian-archive-keyring package installed?"
4a242c5b
MV
89 exit 1
90 fi
91
3a341a1d
MV
92 # add new keys from the package;
93
94 # we do not use add_keys_with_verify_against_master_keyring here,
1171258a 95 # because "update" is run on regular package updates. A
3a341a1d
MV
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
4a242c5b 100
364af2ef
MV
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
4a242c5b
MV
112}
113
7fbe42c0 114
b3d44315 115usage() {
46e39c8e 116 echo "Usage: apt-key [--keyring file] [command] [arguments]"
b3d44315
MV
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>"
bf6d5b42
OS
122 echo " apt-key export <keyid> - output the key <keyid>"
123 echo " apt-key exportall - output all trusted keys"
4a242c5b 124 echo " apt-key update - update keys using the keyring package"
848ecfa4 125 echo " apt-key net-update - update keys using the network"
b3d44315 126 echo " apt-key list - list keys"
a8cabc8f
LB
127 echo " apt-key finger - list fingerprints"
128 echo " apt-key adv - pass advanced options to gpg (download key)"
b3d44315 129 echo
46e39c8e 130 echo "If no specific keyring file is given the command applies to all keyring files."
b3d44315
MV
131}
132
46e39c8e
MV
133# Determine on which keyring we want to work
134if [ "$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
146else
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
161fi
162#echo "COMMAND: $GPG"
163
b3d44315
MV
164command="$1"
165if [ -z "$command" ]; then
166 usage
167 exit 1
168fi
169shift
170
171if [ "$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
175fi
176
b3d44315
MV
177case "$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 ;;
4a242c5b
MV
186 update)
187 update
188 ;;
848ecfa4
MV
189 net-update)
190 net_update
191 ;;
b3d44315
MV
192 list)
193 $GPG --batch --list-keys
194 ;;
195 finger*)
196 $GPG --batch --fingerprint
197 ;;
bf6d5b42
OS
198 export)
199 $GPG --armor --export "$1"
200 ;;
201 exportall)
202 $GPG --armor --export
203 ;;
b3d44315
MV
204 adv*)
205 echo "Executing: $GPG $*"
206 $GPG $*
207 ;;
208 help)
209 usage
210 ;;
211 *)
212 usage
213 exit 1
214 ;;
215esac