forked from Disroot/gpg-lacre
Fix bug causing messages to have extra characters or failing to encode properly.
This commit is contained in:
parent
7110b9a303
commit
4aa366dea5
2 changed files with 40 additions and 17 deletions
|
@ -33,5 +33,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
|
||||
|
|
|
@ -45,6 +45,7 @@ def send_msg( message, recipients = None ):
|
|||
smtp.sendmail( from_addr, recipients, message.as_string() )
|
||||
|
||||
def encrypt_payload( payload, gpg_to_cmdline ):
|
||||
gpg = GnuPG.GPGEncryptor( cfg['gpg']['keyhome'], gpg_to_cmdline, payload.get_content_charset() )
|
||||
raw_payload = payload.get_payload(decode=True)
|
||||
if "-----BEGIN PGP MESSAGE-----" in raw_payload and "-----END PGP MESSAGE-----" in raw_payload:
|
||||
return payload
|
||||
|
@ -53,24 +54,35 @@ def encrypt_payload( payload, gpg_to_cmdline ):
|
|||
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']) )
|
||||
|
||||
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, [])
|
||||
|
@ -126,7 +138,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