]> git.decadent.org.uk Git - dak.git/blob - emilie
Add emilie
[dak.git] / emilie
1 #!/usr/bin/env python
2
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 $
6
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.
11
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.
16
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
20
21 ################################################################################
22
23 import commands, ldap, pg, re, sys, time;
24 import apt_pkg;
25 import db_access, utils;
26
27 ################################################################################
28
29 Cnf = None;
30 projectB = None;
31
32 re_gpg_fingerprint = re.compile(r"^\s+Key fingerprint = (.*)$", re.MULTILINE);
33 re_debian_address = re.compile(r"^.*<(.*)@debian\.org>$", re.MULTILINE);
34
35 ################################################################################
36
37 def usage(exit_code=0):
38     print """Usage: emilie
39 Syncs fingerprint and uid tables with a debian.org LDAP DB
40
41   -h, --help                show this help and exit."""
42     sys.exit(exit_code)
43
44 ################################################################################
45
46 def get_ldap_value(entry, value):
47     ret = entry.get(value);
48     if not ret:
49         return "";
50     else:
51         # FIXME: what about > 0 ?
52         return ret[0];
53
54 def main():
55     global Cnf, projectB;
56
57     Cnf = utils.get_conf()
58     Arguments = [('h',"help","Emilie::Options::Help")];
59     for i in [ "help" ]:
60         if not Cnf.has_key("Emilie::Options::%s" % (i)):
61             Cnf["Emilie::Options::%s" % (i)] = "";
62
63     apt_pkg.ParseCommandLine(Cnf, Arguments, sys.argv);
64
65     Options = Cnf.SubTree("Emilie::Options")
66     if Options["Help"]:
67         usage();
68
69     projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
70     db_access.init(Cnf, projectB);
71
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)));
82
83
84     projectB.query("BEGIN WORK");
85
86
87     # Sync LDAP with DB
88     db_fin_uid = {};
89     ldap_fin_uid_id = {};
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);
96
97     for i in Attrs:
98         entry = i[1];
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];
106                 if not existing_uid:
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);
109                 else:
110                     if existing_uid != uid:
111                         utils.fubar("%s has %s in LDAP, but projectB says it should be %s." % (uid, fingerprint, existing_uid));
112
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"],
119                  fingerprint);
120         (result, output) = commands.getstatusoutput(cmd);
121         if result == 0:
122             m = re_gpg_fingerprint.search(output);
123             if not m:
124                 print 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);
133         else:
134             extra_keyrings = "";
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);
141             if result != 0:
142                 cmd = "gpg --keyserver=%s --allow-non-selfsigned-uid --recv-key %s" % (Cnf["Emilie::KeyServer"], fingerprint);
143                 (result, output) = commands.getstatusoutput(cmd);
144                 if result != 0:
145                     print "0x%s: NOT found on keyserver." % (fingerprint);
146                     print cmd
147                     print result
148                     print output
149                     continue;
150                 else:
151                     cmd = "gpg --list-key %s" % (fingerprint);
152                     (result, output) = commands.getstatusoutput(cmd);
153                     if result != 0:
154                         print "0x%s: --list-key returned error after --recv-key didn't." % (fingerprint);
155                         print cmd
156                         print result
157                         print output
158                         continue;
159             m = re_debian_address.search(output);
160             if m:
161                 guess_uid = m.group(1);
162             else:
163                 guess_uid = "???";
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
168             uid = None;
169             while not uid:
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"])
172                 if not Attrs:
173                     print "That UID doesn't exist in LDAP!"
174                     uid = None;
175                 else:
176                     entry = Attrs[0][1];
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();
182                     if yn == "y":
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);
186                     else:
187                         uid = None;
188     projectB.query("COMMIT WORK");
189
190 ############################################################
191
192 if __name__ == '__main__':
193     main()