3 """ Imports a keyring into the database """
4 # Copyright (C) 2007 Anthony Towns <aj@erisian.com.au>
5 # Copyright (C) 2009 Mark Hymers <mhy@debian.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 ################################################################################
26 from daklib.config import Config
27 from daklib.dbconn import *
32 ################################################################################
34 def get_uid_info(session):
37 q = session.execute("SELECT id, uid, name FROM uid")
38 for (keyid, uid, name) in q.fetchall():
39 byname[uid] = (keyid, name)
40 byid[keyid] = (uid, name)
44 def get_fingerprint_info(session):
46 q = session.execute("SELECT f.fingerprint, f.id, f.uid, f.keyring FROM fingerprint f")
47 for (fingerprint, fingerprint_id, uid, keyring) in q.fetchall():
48 fins[fingerprint] = (uid, fingerprint_id, keyring)
51 ################################################################################
53 def usage (exit_code=0):
54 print """Usage: dak import-keyring [OPTION]... [KEYRING]
55 -h, --help show this help and exit.
56 -L, --import-ldap-users generate uid entries for keyring from LDAP
57 -U, --generate-users FMT generate uid entries from keyring as FMT"""
61 ################################################################################
67 Arguments = [('h',"help","Import-Keyring::Options::Help"),
68 ('L',"import-ldap-users","Import-Keyring::Options::Import-Ldap-Users"),
69 ('U',"generate-users","Import-Keyring::Options::Generate-Users", "HasArg"),
72 for i in [ "help", "report-changes", "generate-users", "import-ldap-users" ]:
73 if not cnf.has_key("Import-Keyring::Options::%s" % (i)):
74 cnf["Import-Keyring::Options::%s" % (i)] = ""
76 keyring_names = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
80 Options = cnf.SubTree("Import-Keyring::Options")
85 if len(keyring_names) != 1:
88 ### Keep track of changes made
89 changes = [] # (uid, changes strings)
92 session = DBConn().session()
94 ### Cache all the existing fingerprint entries
95 db_fin_info = get_fingerprint_info(session)
99 keyringname = keyring_names[0]
100 keyring = get_keyring(keyringname, session)
102 print "E: Can't load keyring %s from database" % keyringname
105 keyring.load_keys(keyringname)
107 ### Generate new uid entries if they're needed (from LDAP or the keyring)
108 if Options["Generate-Users"]:
109 format = Options["Generate-Users"]
110 (desuid_byname, desuid_byid) = keyring.generate_users_from_keyring(Options["Generate-Users"], session)
111 elif Options["Import-Ldap-Users"]:
112 (desuid_byname, desuid_byid) = keyring.import_users_from_ldap(session)
114 (desuid_byname, desuid_byid) = ({}, {})
116 ### Cache all the existing uid entries
117 (db_uid_byname, db_uid_byid) = get_uid_info(session)
119 ### Update full names of applicable users
120 for keyid in desuid_byid.keys():
121 uid = (keyid, desuid_byid[keyid][0])
122 name = desuid_byid[keyid][1]
123 oname = db_uid_byid[keyid][1]
124 if name and oname != name:
125 changes.append((uid[1], "Full name: %s" % (name)))
126 session.execute("UPDATE uid SET name = :name WHERE id = :keyid",
127 {'name': name, 'keyid': keyid})
129 # The fingerprint table (fpr) points to a uid and a keyring.
130 # If the uid is being decided here (ldap/generate) we set it to it.
131 # Otherwise, if the fingerprint table already has a uid (which we've
132 # cached earlier), we preserve it.
133 # Otherwise we leave it as None
136 for z in keyring.keys.keys():
137 keyid = db_uid_byname.get(keyring.keys[z].get("uid", None), [None])[0]
139 keyid = db_fin_info.get(keyring.keys[z]["fingerprints"][0], [None])[0]
140 for y in keyring.keys[z]["fingerprints"]:
141 fpr[y] = (keyid, keyring.keyring_id)
143 # For any keys that used to be in this keyring, disassociate them.
144 # We don't change the uid, leaving that for historical info; if
145 # the id should change, it'll be set when importing another keyring.
147 for f,(u,fid,kr) in db_fin_info.iteritems():
148 if kr != keyring.keyring_id:
154 changes.append((db_uid_byid.get(u, [None])[0], "Removed key: %s" % (f)))
155 session.execute("""UPDATE fingerprint
157 source_acl_id = NULL,
158 binary_acl_id = NULL,
160 WHERE id = :fprid""", {'fprid': fid})
162 session.execute("""DELETE FROM binary_acl_map WHERE fingerprint_id = :fprid""", {'fprid': fid})
164 # For the keys in this keyring, add/update any fingerprints that've
169 newuiduid = db_uid_byid.get(newuid, [None])[0]
171 (olduid, oldfid, oldkid) = db_fin_info.get(f, [-1,-1,-1])
180 changes.append((newuiduid, "Added key: %s" % (f)))
183 fp.keyring_id = keyring.keyring_id
187 fp.binary_acl_id = keyring.default_binary_acl_id
188 fp.source_acl_id = keyring.default_source_acl_id
189 fp.default_binary_reject = keyring.default_binary_reject
193 for k in keyring.keyring_acl_map:
195 ba.fingerprint_id = fp.fingerprint_id
196 ba.architecture_id = k.architecture_id
201 if newuid and olduid != newuid:
203 changes.append((newuiduid, "Linked key: %s" % f))
204 changes.append((newuiduid, " (formerly belonging to %s)" % (db_uid_byid[olduid][0])))
206 changes.append((newuiduid, "Linked key: %s" % f))
207 changes.append((newuiduid, " (formerly unowned)"))
209 session.execute("UPDATE fingerprint SET uid = :uid WHERE id = :fpr",
210 {'uid': newuid, 'fpr': oldfid})
212 # Don't move a key from a keyring with a higher priority to a lower one
213 if oldkid != keyring.keyring_id:
219 oldkeyring = session.query(Keyring).filter_by(keyring_id=oldkid).one()
220 except NotFoundError:
221 print "ERROR: Cannot find old keyring with id %s" % oldkid
224 if oldkeyring.priority < keyring.priority:
227 # Only change the keyring if it won't result in a loss of permissions
229 session.execute("""DELETE FROM binary_acl_map WHERE fingerprint_id = :fprid""", {'fprid': oldfid})
231 session.execute("""UPDATE fingerprint
232 SET keyring = :keyring,
233 source_acl_id = :source_acl_id,
234 binary_acl_id = :binary_acl_id,
235 binary_reject = :binary_reject
237 {'keyring': keyring.keyring_id,
238 'source_acl_id': keyring.default_source_acl_id,
239 'binary_acl_id': keyring.default_binary_acl_id,
240 'binary_reject': keyring.default_binary_reject,
245 for k in keyring.keyring_acl_map:
247 ba.fingerprint_id = oldfid
248 ba.architecture_id = k.architecture_id
253 print "Key %s exists in both %s and %s keyrings. Not demoting." % (oldkeyring.keyring_name,
254 keyring.keyring_name)
261 for (k, v) in changes:
262 if k not in changesd:
264 changesd[k] += " %s\n" % (v)
266 keys = changesd.keys()
269 print "%s\n%s\n" % (k, changesd[k])
271 ################################################################################
273 if __name__ == '__main__':