4 # Copyright (c) 1999-2000 Jason Gunthorpe <jgg@debian.org>
5 # Copyright (c) 2004 Joey Schulze <joey@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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 # This script tries to match key fingerprints from a keyring with user
22 # name in a directory. When an unassigned key is found a heuristic match
23 # against the keys given cn/sn and the directory is performed to try to get
24 # a matching. Generally this works about 90% of the time, matching is fairly
25 # strict. In the event a non-match a fuzzy sounds-alike search is performed
26 # and the results printed to aide the user.
28 # GPG is automatically invoked with the correct magic special options,
29 # pass the names of all the valid key rings on the command line.
31 # The output report will list what actions were taken. Keys that are present
32 # in the directory but not in the key ring will be removed from the
35 import re, time, ldap, getopt, sys, pwd, os;
36 from userdir_ldap import *;
37 from userdir_gpg import *;
39 # This map deals with people who put the wrong sort of stuff in their pgp
44 # Read the override file into the unknown map. The override file is a list
45 # of colon delimited entires mapping PGP email addresess to local users
46 def LoadOverride(File):
47 List = open(File,"r");
49 Line = List.readline();
52 Split = re.split("[:\n]",Line);
53 UnknownMap[Split[0]] = Split[1].strip()
56 AdminUser = pwd.getpwuid(os.getuid())[0];
57 (options, arguments) = getopt.getopt(sys.argv[1:], "au:m:n")
58 for (switch, val) in options:
61 elif (switch == '-m'):
63 elif (switch == '-a'):
65 if len(arguments) == 0:
66 print "Give some keyrings to probe";
69 # Main program starts here
71 # Connect to the ldap server
73 l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
75 l = ldap.open(LDAPServer);
76 l.simple_bind_s("","");
78 # Download the existing key list and put it into a map
79 print "Fetching key list..",
81 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=*",["keyFingerPrint","uid"]);
86 # Sense a bad fingerprint.. Slapd has problems, it will store a null
87 # value that ldapsearch doesn't show up.. detect and remove
88 if len(x[1]["keyFingerPrint"]) == 0 or x[1]["keyFingerPrint"][0] == "":
90 print "Fixing bad fingerprint for",x[1]["uid"][0],
93 l.modify_s("uid="+x[1]["uid"][0]+","+BaseDn,\
94 [(ldap.MOD_DELETE,"keyFingerPrint",None)]);
96 for I in x[1]["keyFingerPrint"]:
97 KeyMap[I] = [x[1]["uid"][0],0];
98 if KeyCount.has_key(x[1]["uid"][0]):
99 KeyCount[x[1]["uid"][0]] = KeyCount[x[1]["uid"][0]] + 1;
101 KeyCount[x[1]["uid"][0]] = 1;
107 # Popen GPG with the correct magic special options
108 Args = [GPGPath] + GPGBasicOptions;
110 Args.append("--keyring");
111 if x.find("/") == -1:
115 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
116 Keys = os.popen(" ".join(Args),"r");
118 # Loop over the GPG key file
123 Line = Keys.readline();
127 Split = Line.split(":")
128 if len(Split) < 8 or Split[0] != "pub":
132 Line2 = Keys.readline();
135 Split2 = Line2.split(":");
136 if len(Split2) < 11 or Split2[0] != "fpr":
142 if SeenKeys.has_key(Split2[9]):
143 print "Dup key 0x",Split2[9],"belonging to",KeyMap[Split2[9]][0];
145 SeenKeys[Split2[9]] = None;
147 if KeyMap.has_key(Split2[9]):
148 Ignored = Ignored + 1;
149 # print "Ignoring keyID",Split2[9],"belonging to",KeyMap[Split2[9]][0];
150 KeyMap[Split2[9]][1] = 1;
153 UID = GetUID(l,SplitEmail(Split[9]),UnknownMap);
155 print "None for",SplitEmail(Split[9]),"'%s'"%(Split[9]);
157 for x in UID[1]: print x;
158 print "MISSING 0x" + Split2[9];
162 Rec = [(ldap.MOD_ADD,"keyFingerPrint",Split2[9])];
163 Dn = "uid=" + UID + "," + BaseDn;
164 print "Adding key 0x"+Split2[9],"to",UID;
165 if KeyCount.has_key(UID):
166 KeyCount[UID] = KeyCount[UID] + 1;
173 # Send the modify request
175 Outstanding = Outstanding + 1;
176 Outstanding = FlushOutstanding(l,Outstanding,1);
180 FlushOutstanding(l,Outstanding);
182 if Keys.close() != None:
183 raise "Error","GPG failed"
185 print Ignored,"keys already in the directory (ignored)";
187 # Look for unmatched keys
188 for x in KeyMap.keys():
189 if KeyMap[x][1] == 0:
190 print "key 0x%s belonging to %s removed"%(x,KeyMap[x][0]);
191 if KeyCount.has_key(KeyMap[x][0]) :
192 KeyCount[KeyMap[x][0]] = KeyCount[KeyMap[x][0]] - 1
193 if KeyCount[KeyMap[x][0]] <= 0:
194 print "**",KeyMap[x][0],"no longer has any keys";
196 l.modify_s("uid="+KeyMap[x][0]+","+BaseDn,\
197 [(ldap.MOD_DELETE,"keyFingerPrint",x)]);