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