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
12 # Try to extract a key fingerprint from a PGP siged message
14 # Try to get a pgp text
15 Msg = GetClearSig(Email);
16 if string.find(Msg[0],"-----BEGIN PGP SIGNED MESSAGE-----") == -1:
20 Res = GPGCheckSig(Msg[0]);
22 # Failed to find a matching sig
26 # Search for the matching key fingerprint
27 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=" + Res[2][1]);
31 raise Error, "Oddly your key fingerprint is assigned to more than one account.."
33 return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(Res[2][1]));
35 # Try to guess the name from the email address
36 def TryMatcher(Email):
37 Sender = Email.getheader("From");
41 # Split up the address and invoke the matcher routine
42 UID = GetUID(l,SplitEmail(Sender));
45 if UID[1] == None or len(UID[1]) == 0:
48 # Print out an error message
49 S = "%s: %s -> Address matching failed '%s'\n" %(Now,MsgID,Sender);
51 S = S + " " + x + "\n";
55 return (UID[0],"FROM",Sender);
59 MainLog = open(Ech_MainLog,"a+",0);
60 ErrLog = open(Ech_ErrorLog,"a+",0);
62 MainLog = open("/dev/stdout","a+",0);
63 ErrLog = open("/dev/stdout","a+",0);
65 # Start of main program
66 ErrMsg = "Indeterminate Error";
67 ErrType = EX_TEMPFAIL;
68 Now = time.strftime("%a, %d %b %Y %H:%M:%S",time.gmtime(time.time()));
72 ErrType = EX_PERMFAIL;
73 ErrMsg = "Failed to understand the email or find a signature:";
74 Email = mimetools.Message(sys.stdin,0);
75 MsgID = Email.getheader("Message-ID");
77 # Connect to the ldap server
78 ErrType = EX_TEMPFAIL;
79 ErrMsg = "An error occured while performing the LDAP lookup";
81 l = ldap.open(LDAPServer);
83 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
84 AccessPass = string.split(string.strip(F.readline())," ");
85 l.simple_bind_s("uid="+AccessPass[0]+","+BaseDn,AccessPass[1]);
88 l.simple_bind_s("","");
91 ErrType = EX_TEMPFAIL;
92 ErrMsg = "An error occured while trying GPG decoding";
95 ErrMsg = "An error occured while trying Matcher decoding";
96 User = TryMatcher(Email);
98 # Get any mailing list information
99 List = Email.getheader("X-Mailing-List");
103 # Tada, write a log message
105 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
106 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
107 Dn = "uid=" + User[0] + "," + BaseDn;
108 Rec = [(ldap.MOD_REPLACE,"activity-%s"%(User[1]),Msg)];
114 User = ("-","UKN",Email.getheader("From"));
115 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
116 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
120 S = "%s: %s -> %s\n" %(Now,MsgID,ErrMsg);
121 S = S + "==> %s: %s\n" %(sys.exc_type,sys.exc_value);
122 List = traceback.extract_tb(sys.exc_traceback);
125 S = S + " %s %s:%u: %s\n" %(x[2],x[0],x[1],x[3]);