def save_mdn(message, mdnContent): ''' Process the received MDN and check status of sent message ''' try: mdnMessage = email.message_from_string(mdnContent) mdnHeaders = '' for key in mdnMessage.keys(): mdnHeaders = mdnHeaders + '%s: %s\n'%(key, mdnMessage[key]) messageId = mdnMessage.get('message-id') if message.partner.mdn_sign and mdnMessage.get_content_type() != 'multipart/signed': models.Log.objects.create(message=message, status='W', text=_(u'Expected signed MDN but unsigned MDN returned')) mdnsigned = False if mdnMessage.get_content_type() == 'multipart/signed': models.Log.objects.create(message=message, status='S', text=_(u'Verifying the signed MDN with partner key %s'%message.partner.signature_key)) mdnsigned = True cert = str(message.partner.signature_key.certificate.path) ca_cert = cert if message.partner.signature_key.ca_cert: ca_cert = str(message.partner.signature_key.ca_cert.path) verify_cert = message.partner.signature_key.verify_cert main_boundary = '--' + mdnMessage.get_boundary() ### Extract the mssage and signature for part in mdnMessage.get_payload(): if part.get_content_type().lower() == "application/pkcs7-signature": sig = part else: mdnMessage = part ### check if signature is base64 encoded and if not encode try: raw_sig = sig.get_payload().encode('ascii').strip() except Exception,e: raw_sig = sig.get_payload().encode('base64').strip() ### Verify the signature using raw contents try: as2utils.verify_payload(mdnContent,None,cert,ca_cert,verify_cert) except Exception, e: ### Verify the signature using extracted signature and message try: as2utils.verify_payload(as2utils.extractpayload_fromstring1(mdnContent,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: ### Verify the signature using extracted signature and message without extra trailing new line in message try: as2utils.verify_payload(as2utils.extractpayload_fromstring2(mdnContent,main_boundary),raw_sig,cert,ca_cert,verify_cert) except Exception, e: raise as2utils.as2exception(_(u'MDN Signature Verification Error, exception message is %s' %e))
if (mdnStatus[1].strip() == 'processed'): models.Log.objects.create(message=message, status='S', text=_(u'Message has been successfully processed, verifying the MIC if present.')) if mdn.get('Received-Content-MIC') and message.mic: mdnMIC = mdn.get('Received-Content-MIC').split(','); if (message.mic != mdnMIC[0]): message.status = 'W' models.Log.objects.create(message=message, status='W', text=_(u'Message Integrity check failed, please validate message content with your partner')) else: message.status = 'S' models.Log.objects.create(message=message, status='S', text=_(u'File Transferred successfully to the partner')) else: message.status = 'S' models.Log.objects.create(message=message, status='S', text=_(u'File Transferred successfully to the partner')) run_postsend(message) else: raise as2utils.as2exception(_(u'Partner failed to process file. MDN status is %s'%mdn.get('Disposition'))) else: raise as2utils.as2exception(_(u'MDN report not found in the response')) finally: message.save() def run_postsend(message): ''' Execute command after successful send, can be used to notify successfule sends ''' command = message.partner.cmd_send if command: models.Log.objects.create(message=message, status='S', text=_(u'Exectute command post successful send')) command = Template(command) variables = {'filename':message.payload.name, 'sender':message.organization.as2_name, 'recevier':message.partner.as2_name, 'messageid':message.message_id} variables.update(dict(HeaderParser().parsestr(message.headers).items())) os.system(command.safe_substitute(variables))
), ) else: message.status = "S" models.Log.objects.create( message=message, status="S", text=_(u"File Transferred successfully to the partner") ) else: message.status = "S" models.Log.objects.create( message=message, status="S", text=_(u"File Transferred successfully to the partner") ) run_postsend(message) else: raise as2utils.as2exception( _(u"Partner failed to process file. MDN status is %s" % mdn.get("Disposition")) ) else: raise as2utils.as2exception(_(u"MDN report not found in the response")) finally: message.save() def run_postsend(message): """ Execute command after successful send, can be used to notify successfule sends """ command = message.partner.cmd_send if command: models.Log.objects.create(message=message, status="S", text=_(u"Exectute command post successful send")) command = Template(command) variables = { "filename": message.payload.name,