From 14dcc4229d8c7a557d586720b91a34eb13f82eb8 Mon Sep 17 00:00:00 2001 From: Matthew Borger Date: Tue, 2 Feb 2016 13:14:31 +0000 Subject: [PATCH 1/7] Added option to save generated emails to a given folder instead of sending via SMTP. --- doc/pius.1 | 2 ++ libpius/mailer.py | 62 ++++++++++++++++++++++++++--------------------- pius | 7 +++++- 3 files changed, 43 insertions(+), 28 deletions(-) mode change 100755 => 100644 pius diff --git a/doc/pius.1 b/doc/pius.1 index 84adc3a..85db04a 100644 --- a/doc/pius.1 +++ b/doc/pius.1 @@ -47,6 +47,8 @@ Hostname of SMTP server. [default: \fIlocalhost\fP] Use the pexpect module for signing and drop to the gpg shell for entering the passphrase. [default: false] .IP "\fB\-I\fP, \fB\-\-import\fP" Also import the unsigned keys from the keyring into the default keyring. Ignored if \fB\-r\fP is not specified, or if it's the same as the default keyring. +.IP "\fB\-L\fP, \fB\-\-save\-to\-mail\-dir\fP" +Instead of calling SMTP, save the email to this directory. Useful for signing from an air gapped machine. The saved email files can be sent using your own MTA such as sendmail or mailx. .IP "\fB\-m\fP \fIFROM\-EMAIL\fP, \fB\-\-mail=\fP\fIFROM\-EMAIL\fP" Email the encrypted, signed keys to the respective email addresses using \fIFROM\-EMAIL\fP as the sender. See also \fB\-H\fP and \fB\-P\fP. .IP "\fB\-M\fP \fIFILE\fP, \fB\-\-mail\-text=\fP\fIFILE\fP" diff --git a/libpius/mailer.py b/libpius/mailer.py index 55231aa..8390aaa 100644 --- a/libpius/mailer.py +++ b/libpius/mailer.py @@ -3,6 +3,7 @@ import os import smtplib import socket +import os from email import message, quoprimime from email.utils import formatdate @@ -21,7 +22,7 @@ class PiusMailer(object): def __init__(self, mail, display_name, host, port, user, tls, no_mime, - override, msg_text, tmp_dir): + override, msg_text, tmp_dir, local_mail_dir): self.mail = mail self.display_name = display_name self.host = host @@ -33,6 +34,7 @@ def __init__(self, mail, display_name, host, port, user, tls, no_mime, self.address_override = override self.message_text = msg_text self.tmp_dir = tmp_dir + self.local_mail_dir = local_mail_dir @staticmethod def add_options(parser): @@ -258,36 +260,42 @@ def _send_mail(self, to, msg): msg['From'] = self.mail if self.address_override: msg['To'] = self.address_override + env_to = [msg['To']] else: msg['To'] = to + env_to = [msg['To'], self.mail] msg['Date'] = formatdate(localtime=True) - try: - smtp = smtplib.SMTP(self.host, self.port) - if self.tls: - # NOTE WELL: SECURITY IMPORTANT NOTE! - # In python 2.6 if you attempt to starttls() and the server doesn't - # understand an exception is raised. However before that, it just - # carried on and one could attempt to auth over a plain-text session. - # This is BAD! - # - # So, in order be secure on older pythons we ehlo() and then check the - # response before attempting startls. - smtp.ehlo() - if not smtp.has_extn('STARTTLS'): - # Emulate 2.6 behavior - raise smtplib.SMTPException('Server does not support STARTTLS') - smtp.starttls() - # must re-ehlo after STARTTLS - smtp.ehlo() - # Don't want to send auth information unless we're TLS'd - if self.user: - smtp.login(self.user, self.password) - if self.address_override: - env_to = self.address_override - else: - # BCC the user... - env_to = [msg['To'], self.mail] + if self.local_mail_dir: + if not os.path.isdir(self.local_mail_dir): + os.mkdir(self.local_mail_dir) + if not self.address_ovrride: + msg['Bcc'] = self.mail + email = open(os.path.join(self.local_mail_dir, msg['To']), 'w') + email.write(str(msg)) + email.close() + else: + try: + smtp = smtplib.SMTP(self.host, self.port) + if self.tls: + # NOTE WELL: SECURITY IMPORTANT NOTE! + # In python 2.6 if you attempt to starttls() and the server doesn't + # understand an exception is raised. However before that, it just + # carried on # and one could attempt to auth over a plain-text session. + # This is BAD! + # + # So, in order be secure on older pythons we ehlo() and then check the + # response before attempting startls. + smtp.ehlo() + if not smtp.has_extn('STARTTLS'): + # Emulate 2.6 behavior + raise smtplib.SMTPException('Server does not support STARTTLS') + smtp.starttls() + # must re-ehlo after STARTTLS + smtp.ehlo() + # Don't want to send auth information unless we're TLS'd + if self.user: + smtp.login(self.user, self.password) smtp.sendmail(self.mail, env_to, msg.as_string()) smtp.quit() diff --git a/pius b/pius old mode 100755 new mode 100644 index 2363e82..5db2f84 --- a/pius +++ b/pius @@ -158,6 +158,10 @@ def main(): ' into the default keyring. Ignored if -r is not' ' specified, or if it\'s the same as the default' ' keyring.') + parser.add_option('-L', '--save-to-mail-dir', dest='local_mail_dir', + metavar='FILE', + help='Instead of calling SMTP, save' + ' the email to this directory.') parser.add_option('-m', '--mail', dest='mail', metavar='EMAIL', nargs=1, type='email', help='Email the encrypted, signed keys to the' @@ -243,7 +247,8 @@ def main(): options.mail_no_pgp_mime, options.mail_override, options.mail_text, - options.tmp_dir + options.tmp_dir, + options.local_mail_dir ) else: mailer = None From ea8af9259bb5d6ad76a184266ee9f65f248a130b Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Tue, 10 Apr 2018 15:37:27 -0700 Subject: [PATCH 2/7] Delete redundant import --- libpius/mailer.py | 1 - 1 file changed, 1 deletion(-) diff --git a/libpius/mailer.py b/libpius/mailer.py index 8390aaa..a3780f8 100644 --- a/libpius/mailer.py +++ b/libpius/mailer.py @@ -3,7 +3,6 @@ import os import smtplib import socket -import os from email import message, quoprimime from email.utils import formatdate From 5f03ccbcf23859c1a33608dc220b1670d7e2f545 Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Tue, 10 Apr 2018 15:40:03 -0700 Subject: [PATCH 3/7] Fix typo --- libpius/mailer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libpius/mailer.py b/libpius/mailer.py index a3780f8..08fdda1 100644 --- a/libpius/mailer.py +++ b/libpius/mailer.py @@ -268,7 +268,7 @@ def _send_mail(self, to, msg): if self.local_mail_dir: if not os.path.isdir(self.local_mail_dir): os.mkdir(self.local_mail_dir) - if not self.address_ovrride: + if not self.address_override: msg['Bcc'] = self.mail email = open(os.path.join(self.local_mail_dir, msg['To']), 'w') email.write(str(msg)) From ed1bff14b3741c63696ee5eb19e673244636ba62 Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Tue, 10 Apr 2018 22:49:40 +0000 Subject: [PATCH 4/7] Fix permissions --- pius | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 pius diff --git a/pius b/pius old mode 100644 new mode 100755 From 3a763095ca18a8052c0918c278bd9931001c230e Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Wed, 11 Apr 2018 00:40:46 +0000 Subject: [PATCH 5/7] Add back deleted lines --- libpius/mailer.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libpius/mailer.py b/libpius/mailer.py index 08fdda1..45e328b 100644 --- a/libpius/mailer.py +++ b/libpius/mailer.py @@ -295,6 +295,11 @@ def _send_mail(self, to, msg): # Don't want to send auth information unless we're TLS'd if self.user: smtp.login(self.user, self.password) + if self.address_override: + env_to = self.address_override + else: + # BCC the user... + env_to = [msg['To'], self.mail] smtp.sendmail(self.mail, env_to, msg.as_string()) smtp.quit() From 6e63ff5ab5c8bedefadb8a05d7bac034562608e2 Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Wed, 11 Apr 2018 02:00:02 +0000 Subject: [PATCH 6/7] Fix indentation --- libpius/mailer.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/libpius/mailer.py b/libpius/mailer.py index 45e328b..c52e6c0 100644 --- a/libpius/mailer.py +++ b/libpius/mailer.py @@ -301,9 +301,9 @@ def _send_mail(self, to, msg): # BCC the user... env_to = [msg['To'], self.mail] - smtp.sendmail(self.mail, env_to, msg.as_string()) - smtp.quit() - except smtplib.SMTPException as emsg: - raise MailSendError(emsg) - except socket.error as emsg: - raise MailSendError(emsg) + smtp.sendmail(self.mail, env_to, msg.as_string()) + smtp.quit() + except smtplib.SMTPException as emsg: + raise MailSendError(emsg) + except socket.error as emsg: + raise MailSendError(emsg) From d3d6beb0541205ece85ef7a001af4b80ea70687a Mon Sep 17 00:00:00 2001 From: Cody Brownstein Date: Wed, 11 Apr 2018 02:02:03 +0000 Subject: [PATCH 7/7] Use more descriptive metavar --- pius | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pius b/pius index 5db2f84..8f6bf0f 100755 --- a/pius +++ b/pius @@ -159,7 +159,7 @@ def main(): ' specified, or if it\'s the same as the default' ' keyring.') parser.add_option('-L', '--save-to-mail-dir', dest='local_mail_dir', - metavar='FILE', + metavar='DIRECTORY', help='Instead of calling SMTP, save' ' the email to this directory.') parser.add_option('-m', '--mail', dest='mail', metavar='EMAIL', nargs=1,