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