<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import os , sys <EOL> import pytz <EOL> import xml . etree . ElementTree as ET <EOL> import zipfile <EOL> import logging <EOL> from datetime import datetime <EOL> from email import message_from_file <EOL> from stat import S_ISREG <EOL> from cStringIO import StringIO <EOL> from time import timezone <EOL> from django . db import Error <EOL> from django . conf import settings <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from dmarc . models import Reporter , Report , Record , Result <EOL> class Command ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> args = '<STR_LIT>' <EOL> help = '<STR_LIT>' <EOL> def handle ( self , * args , ** options ) : <EOL> """<STR_LIT>""" <EOL> logger = logging . getLogger ( __name__ ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> supress_errors = False <EOL> dmarc_iszipfile = False <EOL> dmarc_xml = '<STR_LIT>' <EOL> dmarc_file = '<STR_LIT>' <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> if args [ <NUM_LIT:0> ] == '<STR_LIT:->' : <EOL> supress_errors = True <EOL> email_msg = StringIO ( ) <EOL> for line in sys . stdin : <EOL> email_msg . write ( line ) <EOL> email_msg . seek ( <NUM_LIT:0> ) <EOL> if email_msg : <EOL> dmarcemail = message_from_file ( email_msg ) <EOL> for mimepart in dmarcemail . walk ( ) : <EOL> if mimepart . get_content_type ( ) == '<STR_LIT>' or mimepart . get_content_type ( ) == '<STR_LIT>' or mimepart . get_content_type ( ) == '<STR_LIT>' : <EOL> dmarc_zip = StringIO ( ) <EOL> dmarc_zip . write ( mimepart . get_payload ( decode = True ) ) <EOL> dmarc_zip . seek ( <NUM_LIT:0> ) <EOL> ZipFile = zipfile . ZipFile ( dmarc_zip , '<STR_LIT:r>' ) <EOL> files = ZipFile . infolist ( ) <EOL> for f in files : <EOL> dmarc_xml = ZipFile . read ( f ) <EOL> ZipFile . close ( ) <EOL> dmarc_iszipfile = True <EOL> else : <EOL> dmarc_file = args [ <NUM_LIT:0> ] <EOL> if os . path . exists ( dmarc_file ) : <EOL> msg = "<STR_LIT>" % dmarc_file <EOL> logger . debug ( msg ) <EOL> else : <EOL> msg = "<STR_LIT>" % dmarc_file <EOL> logger . error ( msg ) <EOL> raise CommandError ( msg ) <EOL> mode = os . stat ( dmarc_file ) . st_mode <EOL> if not S_ISREG ( mode ) : <EOL> msg = "<STR_LIT>" % dmarc_file <EOL> logger . error ( msg ) <EOL> raise CommandError ( msg ) <EOL> msg = "<STR_LIT>" % dmarc_file <EOL> logger . debug ( msg ) <EOL> if zipfile . is_zipfile ( dmarc_file ) : <EOL> dmarc_iszipfile = True <EOL> ZipFile = zipfile . ZipFile ( dmarc_file , '<STR_LIT:r>' ) <EOL> files = ZipFile . infolist ( ) <EOL> for f in files : <EOL> dmarc_xml = ZipFile . read ( f ) <EOL> ZipFile . close ( ) <EOL> else : <EOL> msg = "<STR_LIT>" <EOL> logger . error ( msg ) <EOL> raise CommandError ( msg ) <EOL> tz_utc = pytz . timezone ( '<STR_LIT>' ) <EOL> report_xml = '<STR_LIT>' <EOL> if dmarc_iszipfile : <EOL> report_xml = dmarc_xml <EOL> root = ET . fromstring ( dmarc_xml ) <EOL> else : <EOL> tree = ET . parse ( dmarc_file ) <EOL> report_xml_stringio = StringIO ( ) <EOL> tree . write ( report_xml_stringio , encoding = "<STR_LIT:utf-8>" , xml_declaration = True ) <EOL> report_xml_stringio . seek ( <NUM_LIT:0> ) <EOL> report_xml = report_xml_stringio . readlines ( ) <EOL> root = tree . getroot ( ) <EOL> report_metadata = root . findall ( '<STR_LIT>' ) <EOL> org_name = None <EOL> email = None <EOL> report_id = None <EOL> report_begin = None <EOL> report_end = None <EOL> for node in report_metadata [ <NUM_LIT:0> ] : <EOL> if node . tag == '<STR_LIT>' : <EOL> org_name = node . text <EOL> if node . tag == '<STR_LIT:email>' : <EOL> email = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> report_id = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> report_begin = node . find ( '<STR_LIT>' ) . text <EOL> report_end = node . find ( '<STR_LIT:end>' ) . text <EOL> if org_name is None : <EOL> msg = "<STR_LIT>" <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> if report_id is None : <EOL> msg = "<STR_LIT>" % org_name <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> try : <EOL> reporter = Reporter . objects . get ( org_name = org_name ) <EOL> except ObjectDoesNotExist : <EOL> try : <EOL> reporter = Reporter . objects . create ( org_name = org_name , email = email ) <EOL> except Error as e : <EOL> msg = "<STR_LIT>" % ( org_name , e ) <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> policy_published = root . findall ( '<STR_LIT>' ) <EOL> policy_domain = None <EOL> policy_adkim = '<STR_LIT:r>' <EOL> policy_aspf = '<STR_LIT:r>' <EOL> policy_p = '<STR_LIT:none>' <EOL> policy_sp = '<STR_LIT:none>' <EOL> policy_pct = <NUM_LIT:0> <EOL> for node in policy_published [ <NUM_LIT:0> ] : <EOL> if node . tag == '<STR_LIT>' : <EOL> policy_domain = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> policy_adkim = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> policy_aspf = node . text <EOL> if node . tag == '<STR_LIT:p>' : <EOL> policy_p = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> policy_sp = node . text <EOL> if node . tag == '<STR_LIT>' : <EOL> policy_pct = int ( node . text ) <EOL> report = Report ( ) <EOL> report . report_id = report_id <EOL> report . reporter = reporter <EOL> report_date_begin = datetime . fromtimestamp ( float ( report_begin ) ) . replace ( tzinfo = tz_utc ) <EOL> try : <EOL> report_date_begin = datetime . fromtimestamp ( float ( report_begin ) ) . replace ( tzinfo = tz_utc ) <EOL> report_date_end = datetime . fromtimestamp ( float ( report_end ) ) . replace ( tzinfo = tz_utc ) <EOL> except : <EOL> msg = "<STR_LIT>" <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> report . date_begin = report_date_begin <EOL> report . date_end = report_date_end <EOL> report . policy_domain = policy_domain <EOL> report . policy_adkim = policy_adkim <EOL> report . policy_aspf = policy_aspf <EOL> report . policy_p = policy_p <EOL> report . policy_sp = policy_sp <EOL> report . policy_pct = policy_pct <EOL> report . report_xml = report_xml <EOL> try : <EOL> report . save ( ) <EOL> except Error as e : <EOL> msg = "<STR_LIT>" % ( report_id , e ) <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> for node in root . findall ( '<STR_LIT>' ) : <EOL> source_ip = None <EOL> recordcount = <NUM_LIT:0> <EOL> policyevaluated_disposition = None <EOL> policyevaluated_dkim = None <EOL> policyevaluated_spf = None <EOL> policyevaluated_reasontype = '<STR_LIT>' <EOL> policyevaluated_reasoncomment = '<STR_LIT>' <EOL> identifier_headerfrom = None <EOL> row = node . find ( '<STR_LIT>' ) <EOL> source_ip = row . find ( '<STR_LIT>' ) . text <EOL> if row . find ( '<STR_LIT:count>' ) is not None : <EOL> recordcount = int ( row . find ( '<STR_LIT:count>' ) . text ) <EOL> else : <EOL> recordcount = <NUM_LIT:0> <EOL> policyevaluated = row . find ( '<STR_LIT>' ) <EOL> policyevaluated_disposition = policyevaluated . find ( '<STR_LIT>' ) . text <EOL> policyevaluated_dkim = policyevaluated . find ( '<STR_LIT>' ) . text <EOL> policyevaluated_spf = policyevaluated . find ( '<STR_LIT>' ) . text <EOL> if policyevaluated . find ( '<STR_LIT>' ) is not None : <EOL> reason = policyevaluated . find ( '<STR_LIT>' ) <EOL> if reason . find ( '<STR_LIT:type>' ) is not None : <EOL> policyevaluated_reasontype = reason . find ( '<STR_LIT:type>' ) . text <EOL> if reason . find ( '<STR_LIT>' ) is not None : <EOL> if reason . find ( '<STR_LIT>' ) . text is not None : <EOL> policyevaluated_reasoncomment = reason . find ( '<STR_LIT>' ) . text <EOL> identifiers = node . find ( '<STR_LIT>' ) <EOL> identifier_headerfrom = identifiers . find ( '<STR_LIT>' ) . text <EOL> if len ( source_ip ) == <NUM_LIT:0> : <EOL> msg = "<STR_LIT>" <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> record = Record ( ) <EOL> record . report = report <EOL> record . source_ip = source_ip <EOL> record . recordcount = recordcount <EOL> record . policyevaluated_disposition = policyevaluated_disposition <EOL> record . policyevaluated_dkim = policyevaluated_dkim <EOL> record . policyevaluated_spf = policyevaluated_spf <EOL> record . policyevaluated_reasontype = policyevaluated_reasontype <EOL> record . policyevaluated_reasoncomment = policyevaluated_reasoncomment <EOL> record . identifier_headerfrom = identifier_headerfrom <EOL> record . save ( ) <EOL> try : <EOL> record . save ( ) <EOL> except Error as e : <EOL> msg = "<STR_LIT>" % e <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) <EOL> auth_results = node . find ( '<STR_LIT>' ) <EOL> for resulttype in auth_results : <EOL> result_domain = resulttype . find ( '<STR_LIT>' ) . text <EOL> if result_domain is None : <EOL> result_domain = '<STR_LIT>' <EOL> result_result = resulttype . find ( '<STR_LIT:result>' ) . text <EOL> result = Result ( ) <EOL> result . record = record <EOL> result . record_type = resulttype . tag <EOL> result . domain = result_domain <EOL> result . result = result_result <EOL> try : <EOL> result . save ( ) <EOL> except Error as e : <EOL> msg = "<STR_LIT>" % ( resulttype . tag , result_domain , e . message ) <EOL> logger . error ( msg ) <EOL> if not supress_errors : <EOL> raise CommandError ( msg ) </s>
