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