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