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 string, 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 = string.strip(F.readline());
19 Split = string.split(Line,":");
23 Addr = string.strip(Split[1]);
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):
37 Line = string.strip(Line);
39 Split = string.split(Line,":");
42 OverMap[Split[0]] = string.strip(Split[1]);
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(string.join(Args," "),"r");
55 l = ldap.open(LDAPServer);
56 l.simple_bind_s("","");
58 # Fetch the key list and map to email address
59 PasswdAttrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=*",\
60 ["uid","keyfingerprint"]);
63 if x[1].has_key("keyfingerprint") == 0 or x[1].has_key("uid") == 0:
65 for I in x[1]["keyfingerprint"]:
66 KFMap[I] = x[1]["uid"][0];
68 # Loop over the GPG key file mapping addresses to uids
76 print "Reading keyrings",
79 Line = Keys.readline();
83 Split = string.split(Line,":");
84 if len(Split) >= 8 and Split[0] == "pub":
85 if FingerPrint != None and UID != None:
87 Match = AddressSplit.match(x);
90 Groups = Match.groups();
91 Email = Groups[1]+'@'+Groups[2];
92 if UIDMap.has_key(Groups[1]):
93 UIDMap[Groups[1]].append(Email);
95 UIDMap[Groups[1]] = [Email];
96 if EmailMap.has_key(Email) and EmailMap[Email] != UID:
97 print "Dup Emap",Email
99 EmailMap[Email] = UID;
102 if len(Split) >= 11 and Split[0] == "fpr":
103 FingerPrint = Split[9];
104 if KFMap.has_key(FingerPrint) == 0:
105 print "Failed",FingerPrint;
108 UID = KFMap[FingerPrint];
109 if len(Split) >= 9 and Split[0] == "uid":
110 Emails.append(Split[9]);
113 # Process the override files
114 for (switch, val) in options:
116 ImportForward(val,EmailMap);
118 elif (switch == '-o'):
119 ImportOverride(val,EmailMap);
124 Line = sys.stdin.readline();
127 Line = string.strip(Line);
129 Split = string.split(Line,"@");
133 # The address is in our domain, go directly
134 if Split[1] == EmailAppend:
135 if FinalMap.has_key(Line):
137 Split2 = string.split(Split[0],"-");
138 FinalMap[Line] = Split2[0];
141 # Exists in the email map..
142 if EmailMap.has_key(Line):
143 if FinalMap.has_key(Line):
145 FinalMap[Line] = EmailMap[Line];
148 # Try again splitting off common address appendage modes
149 Split2 = string.split(Split[0],"-");
150 Addr = Split2[0]+'@'+Split[1];
151 if EmailMap.has_key(Addr):
152 if FinalMap.has_key(Addr):
154 FinalMap[Line] = EmailMap[Addr];
158 if UIDMap.has_key(Split[0]):
159 print Line,UIDMap[Split[0]];
163 # Generate a reverse map and check for duplicates
165 for x in FinalMap.keys():
166 if Back.has_key(FinalMap[x]):
167 print "Dup",x,FinalMap[x],Back[FinalMap[x]];
168 Back[FinalMap[x]] = x;
170 # Print the forward map
171 for x in Back.keys():
172 print "%s: %s" % (x,Back[x]);