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