Fix bug causing messages to have extra characters or failing to encode properly.
This commit is contained in:
parent
9bd2222f98
commit
89a055a043
2 changed files with 50 additions and 27 deletions
|
@ -14,10 +14,11 @@ def public_keys( keyhome ):
|
|||
return keys
|
||||
|
||||
class GPGEncryptor:
|
||||
def __init__(self, keyhome, recipients = None):
|
||||
def __init__(self, keyhome, recipients = None, charset = None):
|
||||
self._keyhome = keyhome
|
||||
self._message = ''
|
||||
self._recipients = list()
|
||||
self._charset = charset
|
||||
if recipients != None:
|
||||
self._recipients.extend(recipients)
|
||||
|
||||
|
@ -30,5 +31,16 @@ class GPGEncryptor:
|
|||
return encdata
|
||||
|
||||
def _command(self):
|
||||
cmd = "/usr/bin/gpg --trust-model always --homedir %s --batch --yes --pgp7 --no-secmem-warning -a -e -r %s" % (self._keyhome, ' -r '.join(self._recipients))
|
||||
return cmd.split()
|
||||
cmd = ["/usr/bin/gpg", "--trust-model", "always", "--homedir", self._keyhome, "--batch", "--yes", "--pgp7", "--no-secmem-warning", "-a", "-e"]
|
||||
|
||||
# add recipients
|
||||
for recipient in self._recipients:
|
||||
cmd.append("-r")
|
||||
cmd.append(recipient)
|
||||
|
||||
# add on the charset, if set
|
||||
if self._charset:
|
||||
cmd.append("--comment")
|
||||
cmd.append('Charset: ' + self._charset)
|
||||
|
||||
return cmd
|
||||
|
|
|
@ -9,6 +9,12 @@ import GnuPG
|
|||
import smtplib
|
||||
import sys
|
||||
|
||||
def appendLog(msg):
|
||||
if cfg.has_key('logging') and cfg['logging'].has_key('file'):
|
||||
log = open(cfg['logging']['file'], 'a')
|
||||
log.write(msg + "\n")
|
||||
log.close()
|
||||
|
||||
# Read configuration from /etc/gpg-mailgate.conf
|
||||
_cfg = RawConfigParser()
|
||||
_cfg.read('/etc/gpg-mailgate.conf')
|
||||
|
@ -27,39 +33,47 @@ to_addrs = sys.argv[1:]
|
|||
def send_msg( message, recipients = None ):
|
||||
if recipients == None:
|
||||
recipients = to_addrs
|
||||
if cfg.has_key('logging') and cfg['logging'].has_key('file'):
|
||||
log = open(cfg['logging']['file'], 'a')
|
||||
log.write("Sending email to: <%s>\n" % '> <'.join( recipients ))
|
||||
log.close()
|
||||
appendLog("Sending email to: <%s>" % '> <'.join( recipients ))
|
||||
relay = (cfg['relay']['host'], int(cfg['relay']['port']))
|
||||
smtp = smtplib.SMTP(relay[0], relay[1])
|
||||
smtp.sendmail( from_addr, recipients, message.as_string() )
|
||||
|
||||
def encrypt_payload( payload, gpg_to_cmdline ):
|
||||
gpg = GnuPG.GPGEncryptor( cfg['gpg']['keyhome'], gpg_to_cmdline )
|
||||
gpg = GnuPG.GPGEncryptor( cfg['gpg']['keyhome'], gpg_to_cmdline, payload.get_content_charset() )
|
||||
raw_payload = payload.get_payload(decode=True)
|
||||
gpg.update( raw_payload )
|
||||
if "-----BEGIN PGP MESSAGE-----" in raw_payload and "-----END PGP MESSAGE-----" in raw_payload:
|
||||
return payload
|
||||
payload.set_payload( gpg.encrypt() )
|
||||
if payload['Content-Disposition']:
|
||||
payload.replace_header( 'Content-Disposition', re.sub(r'filename="([^"]+)"', r'filename="\1.pgp"', payload['Content-Disposition']) )
|
||||
if payload['Content-Type']:
|
||||
payload.replace_header( 'Content-Type', re.sub(r'name="([^"]+)"', r'name="\1.pgp"', payload['Content-Type']) )
|
||||
if 'name="' in payload['Content-Type']:
|
||||
payload.replace_header( 'Content-Type', re.sub(r'^[a-z/]+;', r'application/octet-stream;', payload['Content-Type']) )
|
||||
payload.set_payload( "\n".join( filter( lambda x:re.search(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$',x), payload.get_payload().split("\n") ) ) )
|
||||
|
||||
isAttachment = payload.get_param( 'attachment', None, 'Content-Disposition' ) is not None
|
||||
|
||||
if isAttachment:
|
||||
filename = payload.get_filename()
|
||||
|
||||
if filename:
|
||||
pgpFilename = filename + ".pgp"
|
||||
|
||||
if payload.get('Content-Disposition') is not None:
|
||||
payload.set_param( 'filename', pgpFilename, 'Content-Disposition' )
|
||||
if payload.get('Content-Type') is not None:
|
||||
if payload.get_param( 'name' ) is not None:
|
||||
payload.set_param( 'name', pgpFilename )
|
||||
|
||||
payload.set_payload( "\n".join( filter( lambda x:re.search(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$',x), payload.get_payload().split("\n") ) ) )
|
||||
|
||||
if payload.get('Content-Transfer-Encoding') is not None:
|
||||
payload['Content-Transfer-Encoding'] = "quoted-printable"
|
||||
|
||||
return payload
|
||||
|
||||
def encrypt_all_payloads( payloads, gpg_to_cmdline ):
|
||||
def encrypt_all_payloads( message, gpg_to_cmdline ):
|
||||
encrypted_payloads = list()
|
||||
if type( payloads ) == str:
|
||||
msg = email.message.Message()
|
||||
msg.set_payload( payloads )
|
||||
return encrypt_payload( msg, gpg_to_cmdline ).as_string()
|
||||
for payload in payloads:
|
||||
if type( message.get_payload() ) == str:
|
||||
return encrypt_payload( message, gpg_to_cmdline ).get_payload()
|
||||
for payload in message.get_payload():
|
||||
if( type( payload.get_payload() ) == list ):
|
||||
encrypted_payloads.append( encrypt_all_payloads( payload.get_payload(), gpg_to_cmdline ) )
|
||||
encrypted_payloads.extend( encrypt_all_payloads( payload, gpg_to_cmdline ) )
|
||||
else:
|
||||
encrypted_payloads.append( [encrypt_payload( payload, gpg_to_cmdline )] )
|
||||
return sum(encrypted_payloads, [])
|
||||
|
@ -94,10 +108,7 @@ if gpg_to == list():
|
|||
if ungpg_to != list():
|
||||
send_msg( raw_message, ungpg_to )
|
||||
|
||||
if cfg.has_key('logging') and cfg['logging'].has_key('file'):
|
||||
log = open(cfg['logging']['file'], 'a')
|
||||
log.write("Encrypting email to: %s\n" % ' '.join( map(lambda x: x[0], gpg_to) ))
|
||||
log.close()
|
||||
appendLog("Encrypting email to: %s" % ' '.join( map(lambda x: x[0], gpg_to) ))
|
||||
|
||||
if cfg['default'].has_key('add_header') and cfg['default']['add_header'] == 'yes':
|
||||
raw_message['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
|
||||
|
@ -108,7 +119,7 @@ for rcpt in gpg_to:
|
|||
gpg_to_smtp.append(rcpt[0])
|
||||
gpg_to_cmdline.extend(rcpt[1].split(','))
|
||||
|
||||
encrypted_payloads = encrypt_all_payloads( raw_message.get_payload(), gpg_to_cmdline )
|
||||
encrypted_payloads = encrypt_all_payloads( raw_message, gpg_to_cmdline )
|
||||
raw_message.set_payload( encrypted_payloads )
|
||||
|
||||
send_msg( raw_message, gpg_to_smtp )
|
||||
|
|
Loading…
Reference in a new issue