3 # This script tries to match a list of email addresses to the ldap database
4 # uids. It makes use of the PGP key ring to determine matches
6 import re, time, ldap, getopt, sys;
7 from userdir_ldap import *;
8 from userdir_gpg import *;
10 AddressSplit = re.compile("(.*).*<([^@]*)@([^>]*)>");
12 # Import an an forward file
13 def ImportForward(File,EmailMap):
16 Line = F.readline().strip()
19 Split = Line.split(":")
23 Addr = Split[1].strip()
24 if EmailMap.has_key(Addr) and EmailMap[Addr] != Split[0]:
25 print "Dup Over Emap",Line,Split
27 EmailMap[Addr] = Split[0];
30 # Import an override file
31 def ImportOverride(File,OverMap):
39 Split = Line.split(":")
42 OverMap[Split[0]] = Split[1].strip()
45 (options, arguments) = getopt.getopt(sys.argv[1:], "o:f:")
47 # Popen GPG with the correct magic special options
48 Args = [GPGPath] + GPGBasicOptions + GPGKeyRings;
50 Args.append("--keyring");
52 Args = Args + GPGSearchOptions + [" 2> /dev/null"]
53 Keys = os.popen(" ".join(Args),"r")
57 # Fetch the key list and map to email address
58 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=*",\
59 ["uid","keyfingerprint"]);
62 if x[1].has_key("keyfingerprint") == 0 or x[1].has_key("uid") == 0:
64 for I in x[1]["keyfingerprint"]:
65 KFMap[I] = x[1]["uid"][0];
67 # Loop over the GPG key file mapping addresses to uids
75 print "Reading keyrings",
78 Line = Keys.readline();
82 Split = Line.split(":")
83 if len(Split) >= 8 and Split[0] == "pub":
84 if FingerPrint != None and UID != None:
86 Match = AddressSplit.match(x);
89 Groups = Match.groups();
90 Email = Groups[1]+'@'+Groups[2];
91 if UIDMap.has_key(Groups[1]):
92 UIDMap[Groups[1]].append(Email);
94 UIDMap[Groups[1]] = [Email];
95 if EmailMap.has_key(Email) and EmailMap[Email] != UID:
96 print "Dup Emap",Email
98 EmailMap[Email] = UID;
101 if len(Split) >= 11 and Split[0] == "fpr":
102 FingerPrint = Split[9];
103 if KFMap.has_key(FingerPrint) == 0:
104 print "Failed",FingerPrint;
107 UID = KFMap[FingerPrint];
108 if len(Split) >= 9 and Split[0] == "uid":
109 Emails.append(Split[9]);
112 # Process the override files
113 for (switch, val) in options:
115 ImportForward(val,EmailMap);
117 elif (switch == '-o'):
118 ImportOverride(val,EmailMap);
123 Line = sys.stdin.readline();
128 Split = Line.split("@")
132 # The address is in our domain, go directly
133 if Split[1] == EmailAppend:
134 if FinalMap.has_key(Line):
136 Split2 = Split[0].split("-")
137 FinalMap[Line] = Split2[0];
140 # Exists in the email map..
141 if EmailMap.has_key(Line):
142 if FinalMap.has_key(Line):
144 FinalMap[Line] = EmailMap[Line];
147 # Try again splitting off common address appendage modes
148 Split2 = Split[0].split("-")
149 Addr = Split2[0]+'@'+Split[1];
150 if EmailMap.has_key(Addr):
151 if FinalMap.has_key(Addr):
153 FinalMap[Line] = EmailMap[Addr];
157 if UIDMap.has_key(Split[0]):
158 print Line,UIDMap[Split[0]];
162 # Generate a reverse map and check for duplicates
164 for x in FinalMap.keys():
165 if Back.has_key(FinalMap[x]):
166 print "Dup",x,FinalMap[x],Back[FinalMap[x]];
167 Back[FinalMap[x]] = x;
169 # Print the forward map
170 for x in Back.keys():
171 print "%s: %s" % (x,Back[x]);