]> git.saurik.com Git - apt.git/blame_incremental - cmdline/apt-key
merged net-update fixes from apt--mvo
[apt.git] / cmdline / apt-key
... / ...
CommitLineData
1#!/bin/sh
2
3set -e
4
5# We don't use a secret keyring, of course, but gpg panics and
6# implodes if there isn't one available
7
8GPG_CMD="gpg --ignore-time-conflict --no-options --no-default-keyring --secret-keyring /etc/apt/secring.gpg --trustdb-name /etc/apt/trustdb.gpg"
9GPG="$GPG_CMD --keyring /etc/apt/trusted.gpg"
10
11
12MASTER_KEYRING=/usr/share/keyrings/ubuntu-master-keyring.gpg
13ARCHIVE_KEYRING=/usr/share/keyrings/ubuntu-archive-keyring.gpg
14REMOVED_KEYS=/usr/share/keyrings/ubuntu-archive-removed-keys.gpg
15ARCHIVE_KEYRING_URI=http://archive.ubuntu.com/ubuntu/ubuntu-archive-keyring.gpg
16
17add_keys_with_verify_against_master_keyring() {
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:
32 # all keys that are exported and have the name
33 # "Ubuntu Archive Automatic Signing Key" must have a valid signature
34 # from a key in the ubuntu-master-keyring
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
38 ADDED=0
39 for master_key in $master_keys; do
40 if $GPG_CMD --keyring $ADD_KEYRING --list-sigs --with-colons $add_key | grep ^sig | cut -d: -f5 | grep -q $master_key; then
41 $GPG_CMD --quiet --batch --keyring $ADD_KEYRING --export $add_key | $GPG --import
42 ADDED=1
43 fi
44 done
45 if [ $ADDED = 0 ]; then
46 echo >&2 "Key '$add_key' not added. It is not signed with a master key"
47 fi
48 done
49}
50
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
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
66 (cd /var/lib/apt/keyrings; wget -q -N $ARCHIVE_KEYRING_URI)
67 if [ ! -e $keyring ]; then
68 return
69 fi
70 new_mtime=$(stat -c %Y $keyring)
71 if [ $new_mtime -ne $old_mtime ]; then
72 add_keys_with_verify_against_master_keyring $keyring $MASTER_KEYRING
73 fi
74}
75
76update() {
77 if [ ! -f $ARCHIVE_KEYRING ]; then
78 echo >&2 "ERROR: Can't find the archive-keyring"
79 echo >&2 "Is the ubuntu-keyring package installed?"
80 exit 1
81 fi
82
83 # add new keys, if no MASTER_KEYRING is used, use the traditional
84 # way
85 if [ -z "$MASTER_KEYRING" ]; then
86 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
87 else
88 add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
89 fi
90
91 # remove no-longer supported/used keys
92 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
93 for key in $keys; do
94 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
95 $GPG --quiet --batch --delete-key --yes ${key}
96 fi
97 done
98}
99
100
101usage() {
102 echo "Usage: apt-key [command] [arguments]"
103 echo
104 echo "Manage apt's list of trusted keys"
105 echo
106 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
107 echo " apt-key del <keyid> - remove the key <keyid>"
108 echo " apt-key export <keyid> - output the key <keyid>"
109 echo " apt-key exportall - output all trusted keys"
110 echo " apt-key update - update keys using the keyring package"
111 echo " apt-key net-update - update keys using the network"
112 echo " apt-key list - list keys"
113 echo
114}
115
116command="$1"
117if [ -z "$command" ]; then
118 usage
119 exit 1
120fi
121shift
122
123if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
124 echo >&2 "Warning: gnupg does not seem to be installed."
125 echo >&2 "Warning: apt-key requires gnupg for most operations."
126 echo >&2
127fi
128
129case "$command" in
130 add)
131 $GPG --quiet --batch --import "$1"
132 echo "OK"
133 ;;
134 del|rm|remove)
135 $GPG --quiet --batch --delete-key --yes "$1"
136 echo "OK"
137 ;;
138 update)
139 update
140 ;;
141 net-update)
142 net_update
143 ;;
144 list)
145 $GPG --batch --list-keys
146 ;;
147 finger*)
148 $GPG --batch --fingerprint
149 ;;
150 export)
151 $GPG --armor --export "$1"
152 ;;
153 exportall)
154 $GPG --armor --export
155 ;;
156 adv*)
157 echo "Executing: $GPG $*"
158 $GPG $*
159 ;;
160 help)
161 usage
162 ;;
163 *)
164 usage
165 exit 1
166 ;;
167esac