3 import userdir_gpg, userdir_ldap, sys, traceback, time, ldap, posix, getopt;
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:
19 Res = GPGCheckSig(Msg[0]);
21 # Failed to find a matching sig
23 S = "%s: %s -> PGP Checking failed '%s': %s %s\n" %(Now,MsgID,Email.getheader("From"),str(Res[0]),str(Res[2]));
27 # Search for the matching key fingerprint
28 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyfingerprint=" + Res[2][1]);
32 raise Error, "Oddly your key fingerprint is assigned to more than one account.."
34 return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(Res[2][1]));
36 # Try to guess the name from the email address
37 def TryMatcher(Email):
38 Sender = Email.getheader("From");
42 # Split up the address and invoke the matcher routine
43 UID = GetUID(l,SplitEmail(Sender));
46 if UID[1] == None or len(UID[1]) == 0:
49 # Print out an error message
50 S = "%s: %s -> Address matching failed '%s'\n" %(Now,MsgID,Sender);
52 S = S + " " + x + "\n";
56 return (UID[0],"FROM",Sender);
59 (options, arguments) = getopt.getopt(sys.argv[1:], "dr")
60 for (switch, val) in options:
66 MainLog = open(Ech_MainLog,"a+",0);
67 ErrLog = open(Ech_ErrorLog,"a+",0);
69 MainLog = open("/dev/stdout","a+",0);
70 ErrLog = open("/dev/stdout","a+",0);
72 # Start of main program
73 ErrMsg = "Indeterminate Error";
74 ErrType = EX_TEMPFAIL;
75 Now = time.strftime("%a, %d %b %Y %H:%M:%S",time.gmtime(time.time()));
79 ErrType = EX_PERMFAIL;
80 ErrMsg = "Failed to understand the email or find a signature:";
81 Email = mimetools.Message(sys.stdin,0);
82 MsgID = Email.getheader("Message-ID");
84 # Connect to the ldap server
85 ErrType = EX_TEMPFAIL;
86 ErrMsg = "An error occured while performing the LDAP lookup";
88 l = ldap.open(LDAPServer);
90 F = open(PassDir+"/pass-"+pwd.getpwuid(posix.getuid())[0],"r");
91 AccessPass = string.split(string.strip(F.readline())," ");
92 l.simple_bind_s("uid="+AccessPass[0]+","+BaseDn,AccessPass[1]);
95 l.simple_bind_s("","");
98 ErrType = EX_TEMPFAIL;
99 ErrMsg = "An error occured while trying GPG decoding";
100 User = TryGPG(Email);
102 ErrMsg = "An error occured while trying Matcher decoding";
103 User = TryMatcher(Email);
105 # Get any mailing list information
106 List = Email.getheader("X-Mailing-List");
110 # Tada, write a log message
112 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
113 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
114 Dn = "uid=" + User[0] + "," + BaseDn;
115 Rec = [(ldap.MOD_REPLACE,"activity-%s"%(User[1]),Msg)];
121 User = ("-","UKN",Email.getheader("From"));
122 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
123 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
127 S = "%s: %s -> %s\n" %(Now,MsgID,ErrMsg);
128 S = S + "==> %s: %s\n" %(sys.exc_type,sys.exc_value);
129 List = traceback.extract_tb(sys.exc_traceback);
132 S = S + " %s %s:%u: %s\n" %(x[2],x[0],x[1],x[3]);