]> git.saurik.com Git - apt.git/blame - cmdline/apt-key
cmdline/apt-key: import from the correct keyring (ADD_KEYRING) in add_keys_with_verif...
[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:
35 # all keys that are exported and have the name
36 # "Ubuntu Archive Automatic Signing Key" must have a valid signature
37 # from a key in the ubuntu-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
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
64 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
65 add_keys_with_verify_against_master_keyring /var/lib/apt/keyrings/$(basename $ARCHIVE_KEYRING) $MASTER_KEYRING
66}
67
4a242c5b
MV
68update() {
69 if [ ! -f $ARCHIVE_KEYRING ]; then
70 echo >&2 "ERROR: Can't find the archive-keyring"
5fdd72f2 71 echo >&2 "Is the debian-archive-keyring package installed?"
4a242c5b
MV
72 exit 1
73 fi
74
7fbe42c0
MV
75 # add new keys, if no MASTER_KEYRING is used, use the traditional
76 # way
77 if [ -z "$MASTER_KEYRING" ]; then
78 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
79 else
8c56b1e0 80 add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
7fbe42c0 81 fi
4a242c5b 82
7fbe42c0 83 # remove no-longer supported/used keys
3036f1e4 84 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
4a242c5b 85 for key in $keys; do
3036f1e4 86 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
32133629 87 $GPG --quiet --batch --delete-key --yes ${key}
4a242c5b
MV
88 fi
89 done
90}
91
7fbe42c0 92
b3d44315
MV
93usage() {
94 echo "Usage: apt-key [command] [arguments]"
95 echo
96 echo "Manage apt's list of trusted keys"
97 echo
98 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
99 echo " apt-key del <keyid> - remove the key <keyid>"
bf6d5b42
OS
100 echo " apt-key export <keyid> - output the key <keyid>"
101 echo " apt-key exportall - output all trusted keys"
4a242c5b 102 echo " apt-key update - update keys using the keyring package"
848ecfa4 103 echo " apt-key net-update - update keys using the network"
b3d44315
MV
104 echo " apt-key list - list keys"
105 echo
106}
107
108command="$1"
109if [ -z "$command" ]; then
110 usage
111 exit 1
112fi
113shift
114
115if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
116 echo >&2 "Warning: gnupg does not seem to be installed."
117 echo >&2 "Warning: apt-key requires gnupg for most operations."
118 echo >&2
119fi
120
b3d44315
MV
121case "$command" in
122 add)
123 $GPG --quiet --batch --import "$1"
124 echo "OK"
125 ;;
126 del|rm|remove)
127 $GPG --quiet --batch --delete-key --yes "$1"
128 echo "OK"
129 ;;
4a242c5b
MV
130 update)
131 update
132 ;;
848ecfa4
MV
133 net-update)
134 net_update
135 ;;
b3d44315
MV
136 list)
137 $GPG --batch --list-keys
138 ;;
139 finger*)
140 $GPG --batch --fingerprint
141 ;;
bf6d5b42
OS
142 export)
143 $GPG --armor --export "$1"
144 ;;
145 exportall)
146 $GPG --armor --export
147 ;;
b3d44315
MV
148 adv*)
149 echo "Executing: $GPG $*"
150 $GPG $*
151 ;;
152 help)
153 usage
154 ;;
155 *)
156 usage
157 exit 1
158 ;;
159esac