3 import userdir_gpg, userdir_ldap, sys, traceback, time, ldap, os, getopt;
5 import email, email.parser
6 from userdir_gpg import *;
7 from userdir_ldap import *;
10 EX_PERMFAIL = 65; # EX_DATAERR
13 # Try to extract a key fingerprint from a PGP siged message
15 # Try to get a pgp text
17 Msg = GetClearSig(mail, lax_multipart=True);
19 # Log an exception.. but continue. This is to deal with 'sort of'
21 S = "%s: %s -> %s\n" %(Now,MsgID,ErrMsg);
22 S = S + " %s: %s\n" %(sys.exc_type,sys.exc_value);
26 if Msg[0].find("-----BEGIN PGP SIGNED MESSAGE-----") == -1:
29 pgp = GPGCheckSig2(Msg[0]);
31 # Failed to find a matching sig
33 S = "%s: %s -> PGP Checking failed '%s': %s %s\n" %(Now,MsgID,mail["From"],str(pgp.why),str(pgp.key_info));
37 # Search for the matching key fingerprint
38 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + pgp.key_fpr);
42 raise Error, "Oddly your key fingerprint is assigned to more than one account.."
44 return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(pgp.key_fpr));
46 # Try to guess the name from the email address
48 Sender = mail["From"];
52 # Split up the address and invoke the matcher routine
53 UID = GetUID(l,SplitEmail(Sender));
56 if UID[1] == None or len(UID[1]) == 0:
59 # Print out an error message
60 S = "%s: %s -> Address matching failed '%s'\n" %(Now,MsgID,Sender);
62 S = S + " " + x + "\n";
66 return (UID[0],"FROM",Sender);
69 (options, arguments) = getopt.getopt(sys.argv[1:], "dr")
70 for (switch, val) in options:
76 MainLog = open(Ech_MainLog,"a+",0);
77 ErrLog = open(Ech_ErrorLog,"a+",0);
79 MainLog = open("/dev/stdout","a+",0);
80 ErrLog = open("/dev/stdout","a+",0);
82 # Start of main program
83 ErrMsg = "Indeterminate Error";
84 ErrType = EX_TEMPFAIL;
85 Now = time.strftime("%a, %d %b %Y %H:%M:%S",time.gmtime(time.time()));
89 ErrType = EX_PERMFAIL;
90 ErrMsg = "Failed to understand the email or find a signature:";
91 mail = email.parser.Parser().parse(sys.stdin);
92 MsgID = mail["Message-ID"]
94 # Connect to the ldap server
95 ErrType = EX_TEMPFAIL;
96 ErrMsg = "An error occured while performing the LDAP lookup";
100 F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r");
101 AccessPass = F.readline().strip().split(" ")
102 l.simple_bind_s("uid="+AccessPass[0]+","+BaseDn,AccessPass[1]);
105 l.simple_bind_s("","");
108 ErrType = EX_TEMPFAIL;
109 ErrMsg = "An error occured while trying GPG decoding";
112 ErrMsg = "An error occured while trying Matcher decoding";
113 User = TryMatcher(mail);
115 # Get any mailing list information
116 List = mail['X-Mailing-List']
117 if not List: List = "-";
119 # Tada, write a log message
121 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
122 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
123 Dn = "uid=" + User[0] + "," + BaseDn;
124 Rec = [(ldap.MOD_REPLACE,"activity-%s"%(User[1]),Msg)];
130 User = ("-","UKN",mail["From"]);
131 Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID);
132 MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));
136 S = "%s: %s -> %s\n" %(Now,MsgID,ErrMsg);
137 S = S + "==> %s: %s\n" %(sys.exc_type,sys.exc_value);
138 List = traceback.extract_tb(sys.exc_traceback);
141 S = S + " %s %s:%u: %s\n" %(x[2],x[0],x[1],x[3]);