X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=ud-echelon;h=b2b4f0706c301a5a966393aca05689f908624cd3;hb=refs%2Fheads%2Ffordsa;hp=36329807b5f882da88be8843cc50d0b0bd3ef3a4;hpb=198bda079d03f7251b50f95e6a28c1fb046e616a;p=mirror%2Fuserdir-ldap.git diff --git a/ud-echelon b/ud-echelon index 3632980..b2b4f07 100755 --- a/ud-echelon +++ b/ud-echelon @@ -2,6 +2,7 @@ # -*- mode: python -*- import userdir_gpg, userdir_ldap, sys, traceback, time, ldap, os, getopt; import pwd +import email, email.parser from userdir_gpg import *; from userdir_ldap import *; @@ -10,10 +11,10 @@ EX_PERMFAIL = 65; # EX_DATAERR Debug = None; # Try to extract a key fingerprint from a PGP siged message -def TryGPG(Email): +def TryGPG(mail): # Try to get a pgp text try: - Msg = GetClearSig(Email); + Msg = GetClearSig(mail, lax_multipart=True); except: # Log an exception.. but continue. This is to deal with 'sort of' # PGP-MIME things @@ -25,34 +26,34 @@ def TryGPG(Email): if Msg[0].find("-----BEGIN PGP SIGNED MESSAGE-----") == -1: return None; - Res = GPGCheckSig(Msg[0]); + pgp = GPGCheckSig2(Msg[0]); # Failed to find a matching sig - if Res[0] != None: - S = "%s: %s -> PGP Checking failed '%s': %s %s\n" %(Now,MsgID,Email.getheader("From"),str(Res[0]),str(Res[2])); + if not pgp.ok: + S = "%s: %s -> PGP Checking failed '%s': %s %s\n" %(Now,MsgID,mail["From"],str(pgp.why),str(pgp.key_info)); ErrLog.write(S); return None; # Search for the matching key fingerprint - Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + Res[2][1]); + Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"keyFingerPrint=" + pgp.key_fpr); if len(Attrs) == 0: return None; if len(Attrs) != 1: raise Error, "Oddly your key fingerprint is assigned to more than one account.." - return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(Res[2][1])); + return (Attrs[0][1]["uid"][0],"PGP",FormatPGPKey(pgp.key_fpr)); # Try to guess the name from the email address -def TryMatcher(Email): - Sender = Email.getheader("From"); - if Sender == None: +def TryMatcher(mail): + Sender = mail["From"]; + if Sender is None: return None; # Split up the address and invoke the matcher routine UID = GetUID(l,SplitEmail(Sender)); - if UID[0] == None: - if UID[1] == None or len(UID[1]) == 0: + if UID[0] is None: + if UID[1] is None or len(UID[1]) == 0: return None; # Print out an error message @@ -71,7 +72,7 @@ for (switch, val) in options: Debug = ""; # Open the log files -if Debug == None: +if Debug is None: MainLog = open(Ech_MainLog,"a+",0); ErrLog = open(Ech_ErrorLog,"a+",0); else: @@ -87,15 +88,15 @@ try: # Get the email ErrType = EX_PERMFAIL; ErrMsg = "Failed to understand the email or find a signature:"; - Email = mimetools.Message(sys.stdin,0); - MsgID = Email.getheader("Message-ID"); - + mail = email.parser.Parser().parse(sys.stdin); + MsgID = mail["Message-ID"] + # Connect to the ldap server ErrType = EX_TEMPFAIL; ErrMsg = "An error occured while performing the LDAP lookup"; global l; - l = ldap.open(LDAPServer); - if Debug == None: + l = connectLDAP() + if Debug is None: F = open(PassDir+"/pass-"+pwd.getpwuid(os.getuid())[0],"r"); AccessPass = F.readline().strip().split(" ") l.simple_bind_s("uid="+AccessPass[0]+","+BaseDn,AccessPass[1]); @@ -106,28 +107,27 @@ try: # Try to decode ErrType = EX_TEMPFAIL; ErrMsg = "An error occured while trying GPG decoding"; - User = TryGPG(Email); - if User == None: + User = TryGPG(mail); + if User is None: ErrMsg = "An error occured while trying Matcher decoding"; - User = TryMatcher(Email); + User = TryMatcher(mail); # Get any mailing list information - List = Email.getheader("X-Mailing-List"); - if List == None: - List = "-"; + List = mail['X-Mailing-List'] + if not List: List = "-"; # Tada, write a log message - if User != None: + if User is not None: Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID); MainLog.write("%s %s %s\n"%(User[0],User[1],Msg)); Dn = "uid=" + User[0] + "," + BaseDn; Rec = [(ldap.MOD_REPLACE,"activity-%s"%(User[1]),Msg)]; - if Debug == None: + if Debug is None: l.modify_s(Dn,Rec); else: print Rec; else: - User = ("-","UKN",Email.getheader("From")); + User = ("-","UKN",mail["From"]); Msg = "[%s] \"%s\" \"%s\" \"%s\""%(Now,User[2],List,MsgID); MainLog.write("%s %s %s\n"%(User[0],User[1],Msg));