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