3 # Sync fingerprint and uid tables with a debian.org LDAP DB
4 # Copyright (C) 2003 James Troup <james@nocrew.org>
5 # $Id: emilie,v 1.1 2003-02-11 18:09:38 troup Exp $
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 ################################################################################
23 import commands, ldap, pg, re, sys, time;
25 import db_access, utils;
27 ################################################################################
32 re_gpg_fingerprint = re.compile(r"^\s+Key fingerprint = (.*)$", re.MULTILINE);
33 re_debian_address = re.compile(r"^.*<(.*)@debian\.org>$", re.MULTILINE);
35 ################################################################################
37 def usage(exit_code=0):
38 print """Usage: emilie
39 Syncs fingerprint and uid tables with a debian.org LDAP DB
41 -h, --help show this help and exit."""
44 ################################################################################
46 def get_ldap_value(entry, value):
47 ret = entry.get(value);
51 # FIXME: what about > 0 ?
57 Cnf = utils.get_conf()
58 Arguments = [('h',"help","Emilie::Options::Help")];
60 if not Cnf.has_key("Emilie::Options::%s" % (i)):
61 Cnf["Emilie::Options::%s" % (i)] = "";
63 apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
65 Options = Cnf.SubTree("Emilie::Options")
69 projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
70 db_access.init(Cnf, projectB);
72 #before = time.time();
73 #sys.stderr.write("[Getting info from the LDAP server...");
74 LDAPDn = Cnf["Emilie::LDAPDn"];
75 LDAPServer = Cnf["Emilie::LDAPServer"];
76 l = ldap.open(LDAPServer);
77 l.simple_bind_s("","");
78 Attrs = l.search_s(LDAPDn, ldap.SCOPE_ONELEVEL,
79 "(&(keyfingerprint=*)(gidnumber=%s))" % (Cnf["Julia::ValidGID"]),
80 ["uid", "keyfingerprint"]);
81 #sys.stderr.write("done. (%d seconds)]\n" % (int(time.time()-before)));
84 projectB.query("BEGIN WORK");
90 q = projectB.query("""
91 SELECT f.fingerprint, f.id, u.uid FROM fingerprint f, uid u WHERE f.uid = u.id
92 UNION SELECT f.fingerprint, f.id, null FROM fingerprint f where f.uid is null""");
93 for i in q.getresult():
94 (fingerprint, fingerprint_id, uid) = i;
95 db_fin_uid[fingerprint] = (uid, fingerprint_id);
99 fingerprints = entry["keyfingerprint"];
100 uid = entry["uid"][0];
101 uid_id = db_access.get_or_set_uid_id(uid);
102 for fingerprint in fingerprints:
103 ldap_fin_uid_id[fingerprint] = (uid, uid_id);
104 if db_fin_uid.has_key(fingerprint):
105 (existing_uid, fingerprint_id) = db_fin_uid[fingerprint];
107 q = projectB.query("UPDATE fingerprint SET uid = %s WHERE id = %s" % (uid_id, fingerprint_id));
108 print "Assigning %s to 0x%s." % (uid, fingerprint);
110 if existing_uid != uid:
111 utils.fubar("%s has %s in LDAP, but projectB says it should be %s." % (uid, fingerprint, existing_uid));
113 # Try to update people who sign with non-primary key
114 q = projectB.query("SELECT fingerprint, id FROM fingerprint WHERE uid is null");
115 for i in q.getresult():
116 (fingerprint, fingerprint_id) = i;
117 cmd = "gpg --no-default-keyring --keyring=%s --keyring=%s --fingerprint %s" \
118 % (Cnf["Dinstall::PGPKeyring"], Cnf["Dinstall::GPGKeyring"],
120 (result, output) = commands.getstatusoutput(cmd);
122 m = re_gpg_fingerprint.search(output);
125 utils.fubar("0x%s: No fingerprint found in gpg output but it returned 0?\n%s" % (fingerprint, utils.prefix_multi_line_string(output, " [GPG output:] ")));
126 primary_key = m.group(1);
127 primary_key = primary_key.replace(" ","");
128 if not ldap_fin_uid_id.has_key(primary_key):
129 utils.fubar("0x%s (from 0x%s): no UID found in LDAP" % (primary_key, fingerprint));
130 (uid, uid_id) = ldap_fin_uid_id[primary_key];
131 q = projectB.query("UPDATE fingerprint SET uid = %s WHERE id = %s" % (uid_id, fingerprint_id));
132 print "Assigning %s to 0x%s." % (uid, fingerprint);
135 for keyring in Cnf.ValueList("Emilie::ExtraKeyrings"):
136 extra_keyrings += " --keyring=%s" % (keyring);
137 cmd = "gpg --keyring=%s --keyring=%s %s --list-key %s" \
138 % (Cnf["Dinstall::PGPKeyring"], Cnf["Dinstall::GPGKeyring"],
139 extra_keyrings, fingerprint);
140 (result, output) = commands.getstatusoutput(cmd);
142 cmd = "gpg --keyserver=%s --allow-non-selfsigned-uid --recv-key %s" % (Cnf["Emilie::KeyServer"], fingerprint);
143 (result, output) = commands.getstatusoutput(cmd);
145 print "0x%s: NOT found on keyserver." % (fingerprint);
151 cmd = "gpg --list-key %s" % (fingerprint);
152 (result, output) = commands.getstatusoutput(cmd);
154 print "0x%s: --list-key returned error after --recv-key didn't." % (fingerprint);
159 m = re_debian_address.search(output);
161 guess_uid = m.group(1);
164 name = " ".join(output.split('\n')[0].split()[3:]);
165 print "0x%s -> %s -> %s" % (fingerprint, name, guess_uid);
166 # FIXME: make me optionally non-interactive
167 # FIXME: default to the guessed ID
170 uid = utils.our_raw_input("Map to which UID ? ");
171 Attrs = l.search_s(LDAPDn,ldap.SCOPE_ONELEVEL,"(uid=%s)" % (uid), ["cn","mn","sn"])
173 print "That UID doesn't exist in LDAP!"
177 name = " ".join([get_ldap_value(entry, "cn"),
178 get_ldap_value(entry, "mn"),
179 get_ldap_value(entry, "sn")]);
180 prompt = "Map to %s - %s (y/N) ? " % (uid, name.replace(" "," "));
181 yn = utils.our_raw_input(prompt).lower();
183 uid_id = db_access.get_or_set_uid_id(uid);
184 projectB.query("UPDATE fingerprint SET uid = %s WHERE id = %s" % (uid_id, fingerprint_id));
185 print "Assigning %s to 0x%s." % (uid, fingerprint);
188 projectB.query("COMMIT WORK");
190 ############################################################
192 if __name__ == '__main__':