cmdline/apt-key: fix in the trusted key import script
[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 #MASTER_KEYRING=/usr/share/keyrings/debian-master-keyring.gpg
14 ARCHIVE_KEYRING=/usr/share/keyrings/debian-archive-keyring.gpg
15 REMOVED_KEYS=/usr/share/keyrings/debian-archive-removed-keys.gpg
16
17 add_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 $ARCHIVE_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() {
52 if [ ! -f $ARCHIVE_KEYRING ]; then
53 echo >&2 "ERROR: Can't find the archive-keyring"
54 echo >&2 "Is the debian-archive-keyring package installed?"
55 exit 1
56 fi
57
58 # add new keys, if no MASTER_KEYRING is used, use the traditional
59 # way
60 if [ -z "$MASTER_KEYRING" ]; then
61 $GPG_CMD --quiet --batch --keyring $ARCHIVE_KEYRING --export | $GPG --import
62 else
63 add_keys_with_verify_against_master_keyring $ARCHIVE_KEYRING $MASTER_KEYRING
64 fi
65
66 # remove no-longer supported/used keys
67 keys=`$GPG_CMD --keyring $REMOVED_KEYS --with-colons --list-keys | grep ^pub | cut -d: -f5`
68 for key in $keys; do
69 if $GPG --list-keys --with-colons | grep ^pub | cut -d: -f5 | grep -q $key; then
70 $GPG --quiet --batch --delete-key --yes ${key}
71 fi
72 done
73 }
74
75
76 usage() {
77 echo "Usage: apt-key [command] [arguments]"
78 echo
79 echo "Manage apt's list of trusted keys"
80 echo
81 echo " apt-key add <file> - add the key contained in <file> ('-' for stdin)"
82 echo " apt-key del <keyid> - remove the key <keyid>"
83 echo " apt-key export <keyid> - output the key <keyid>"
84 echo " apt-key exportall - output all trusted keys"
85 echo " apt-key update - update keys using the keyring package"
86 echo " apt-key list - list keys"
87 echo
88 }
89
90 command="$1"
91 if [ -z "$command" ]; then
92 usage
93 exit 1
94 fi
95 shift
96
97 if [ "$command" != "help" ] && ! which gpg >/dev/null 2>&1; then
98 echo >&2 "Warning: gnupg does not seem to be installed."
99 echo >&2 "Warning: apt-key requires gnupg for most operations."
100 echo >&2
101 fi
102
103 case "$command" in
104 add)
105 $GPG --quiet --batch --import "$1"
106 echo "OK"
107 ;;
108 del|rm|remove)
109 $GPG --quiet --batch --delete-key --yes "$1"
110 echo "OK"
111 ;;
112 update)
113 update
114 ;;
115 list)
116 $GPG --batch --list-keys
117 ;;
118 finger*)
119 $GPG --batch --fingerprint
120 ;;
121 export)
122 $GPG --armor --export "$1"
123 ;;
124 exportall)
125 $GPG --armor --export
126 ;;
127 adv*)
128 echo "Executing: $GPG $*"
129 $GPG $*
130 ;;
131 help)
132 usage
133 ;;
134 *)
135 usage
136 exit 1
137 ;;
138 esac