3 import userdir_gpg, userdir_ldap, sys, traceback, time, ldap, posix;
5 from userdir_gpg import *;
6 from userdir_ldap import *;
9 EX_PERMFAIL = 65; # EX_DATAERR
11 # Try to extract a key fingerprint from a PGP siged message
13 # Try to get a pgp text
14 Msg = GetClearSig(Email);
15 if string.find(Msg[0],"-----BEGIN PGP SIGNED MESSAGE-----") == -1:
18 Res = GPGCheckSig(Msg[0]);
20 # Failed to find a matching sig
24 # Search for the matching key fingerprint
25 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=" + Res[2][1]);
29 raise Error, "Oddly your key fingerprint is assigned to more than one account.."
31 return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(Res[2][1]));
33 # Convert the PGP name string to a uid value
34 def GetUID(l,Name,UnknownMap = {}):
35 # Crack up the email address into a best guess first/middle/last name
36 (cn,mn,sn) = NameSplit(re.sub('["]','',Name[0]))
38 # Brackets anger the ldap searcher
39 cn = re.sub('[(")]','?',cn);
40 sn = re.sub('[(")]','?',sn);
42 # First check the unknown map for the email address
43 if UnknownMap.has_key(Name[1] + '@' + Name[2]):
44 Stat = "unknown map hit for "+str(Name);
45 return (UnknownMap[Name[1] + '@' + Name[2]],[Stat]);
47 # Then the cruft component (ie there was no email address to match)
48 if UnknownMap.has_key(Name[2]):
49 Stat = "unknown map hit for"+str(Name);
50 return (UnknownMap[Name[2]],[Stat]);
52 # Search for a possible first/last name hit
54 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(&(cn=%s)(sn=%s))"%(cn,sn),["uid"]);
55 except ldap.FILTER_ERROR:
56 Stat = "Filter failure: (&(cn=%s)(sn=%s))"%(cn,sn);
59 # Try matching on the email address
62 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"emailforward=%s"%(Name[2]),["uid"]);
63 except ldap.FILTER_ERROR:
66 # Hmm, more than one/no return
68 # Key claims a local address
69 if Name[2] == EmailAppend:
71 # Pull out the record for the claimed user
72 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(uid=%s)"%(Name[1]),["uid","sn","cn"]);
74 # We require the UID surname to be someplace in the key name, this
75 # deals with special purpose keys like 'James Troup (Alternate Debian key)'
76 # Some people put their names backwards on their key too.. check that as well
77 if len(Attrs) == 1 and \
78 (string.find(string.lower(sn),string.lower(Attrs[0][1]["sn"][0])) != -1 or \
79 string.find(string.lower(cn),string.lower(Attrs[0][1]["sn"][0])) != -1):
80 Stat = EmailAppend+" hit for "+str(Name);
81 return (Name[1],[Stat]);
83 # Attempt to give some best guess suggestions for use in editing the
85 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"(sn~=%s)"%(sn),["uid","sn","cn"]);
89 Stat = ["None for %s"%(str(Name))];
91 Stat.append("But might be: %s %s <%s@debian.org>"%(x[1]["cn"][0],x[1]["sn"][0],x[1]["uid"][0]));
94 return (Attrs[0][1]["uid"][0],None);
98 # Try to guess the name from the email address
99 def TryMatcher(Email):
100 Sender = Email.getheader("From");
104 # Split up the address and invoke the matcher routine
105 UID = GetUID(l,SplitEmail(Sender));
108 if UID[1] == None or len(UID[1]) == 0:
111 # Print out an error message
112 S = "%s: %s -> Address matching failed '%s'\n" %(Now,MsgID,Sender);
114 S = S + " " + x + "\n";
118 return (UID[0],"FROM",Sender);
121 MainLog = open(Ech_MainLog,"a+",0);
122 ErrLog = open(Ech_ErrorLog,"a+",0);
124 # Start of main program
125 ErrMsg = "Indeterminate Error";
126 ErrType = EX_TEMPFAIL;
127 Now = time.strftime("%a, %d %b %Y %H:%M:%S",time.gmtime(time.time()));
131 ErrType = EX_PERMFAIL;
132 ErrMsg = "Failed to understand the email or find a signature:";
133 Email = mimetools.Message(sys.stdin,0);
134 MsgID = Email.getheader("Message-ID");
136 # Connect to the ldap server
137 ErrType = EX_TEMPFAIL;
138 ErrMsg = "An error occured while performing the LDAP lookup";
140 l = ldap.open(LDAPServer);
141 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
142 AccessPass = string.split(string.strip(F.readline())," ");
143 l.simple_bind_s("uid="+AccessPass[0]+","+BaseDn,AccessPass[1]);
147 ErrType = EX_TEMPFAIL;
148 ErrMsg = "An error occured while trying GPG decoding";
149 User = TryGPG(Email);
151 ErrMsg = "An error occured while trying Matcher decoding";
152 User = TryMatcher(Email);
154 # Get any mailing list information
155 List = Email.getheader("X-Mailing-List");
159 # Tada, write a log message
161 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
162 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
163 Dn = "uid=" + User[0] + "," + BaseDn;
164 Rec = [(ldap.MOD_REPLACE,"activity-%s"%(User[1]),Msg)];
167 User = ("-","UKN",Email.getheader("From"));
168 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
169 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
173 S = "%s: %s -> %s\n" %(Now,MsgID,ErrMsg);
174 S = S + "==> %s: %s\n" %(sys.exc_type,sys.exc_value);
175 List = traceback.extract_tb(sys.exc_traceback);
178 S = S + " %s %s:%u: %s\n" %(x[2],x[0],x[1],x[3]);