Fix auto-migrated code
- Use b'' (byte strings) where appropriate. - Fix indentation. - Replace python2.x references with python3.x.
This commit is contained in:
parent
5f02223ec7
commit
b2a01c15b0
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
PYTHON = python2.7
|
PYTHON = python3.8
|
||||||
|
|
||||||
.PHONY: test pre-clean clean
|
.PHONY: test pre-clean clean
|
||||||
|
|
||||||
|
|
|
@ -255,7 +255,7 @@ def decrypt_inline_with_attachments( payloads, success, message = None ):
|
||||||
# There was no encrypted payload found, so the original payload is attached
|
# There was no encrypted payload found, so the original payload is attached
|
||||||
message.attach(payload)
|
message.attach(payload)
|
||||||
|
|
||||||
return message, success
|
return message, success
|
||||||
|
|
||||||
def decrypt_inline_without_attachments( decrypted_message ):
|
def decrypt_inline_without_attachments( decrypted_message ):
|
||||||
|
|
||||||
|
@ -379,9 +379,9 @@ def gpg_encrypt( raw_message, recipients ):
|
||||||
raw_message_mime['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
|
raw_message_mime['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
|
||||||
|
|
||||||
if 'Content-Transfer-Encoding' in raw_message_mime:
|
if 'Content-Transfer-Encoding' in raw_message_mime:
|
||||||
raw_message_mime.replace_header('Content-Transfer-Encoding', '8BIT')
|
raw_message_mime.replace_header('Content-Transfer-Encoding', '8BIT')
|
||||||
else:
|
else:
|
||||||
raw_message_mime['Content-Transfer-Encoding'] = '8BIT'
|
raw_message_mime['Content-Transfer-Encoding'] = '8BIT'
|
||||||
|
|
||||||
encrypted_payloads = encrypt_all_payloads_mime( raw_message_mime, gpg_to_cmdline_mime )
|
encrypted_payloads = encrypt_all_payloads_mime( raw_message_mime, gpg_to_cmdline_mime )
|
||||||
raw_message_mime.set_payload( encrypted_payloads )
|
raw_message_mime.set_payload( encrypted_payloads )
|
||||||
|
@ -396,9 +396,9 @@ def gpg_encrypt( raw_message, recipients ):
|
||||||
raw_message_inline['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
|
raw_message_inline['X-GPG-Mailgate'] = 'Encrypted by GPG Mailgate'
|
||||||
|
|
||||||
if 'Content-Transfer-Encoding' in raw_message_inline:
|
if 'Content-Transfer-Encoding' in raw_message_inline:
|
||||||
raw_message_inline.replace_header('Content-Transfer-Encoding', '8BIT')
|
raw_message_inline.replace_header('Content-Transfer-Encoding', '8BIT')
|
||||||
else:
|
else:
|
||||||
raw_message_inline['Content-Transfer-Encoding'] = '8BIT'
|
raw_message_inline['Content-Transfer-Encoding'] = '8BIT'
|
||||||
|
|
||||||
encrypted_payloads = encrypt_all_payloads_inline( raw_message_inline, gpg_to_cmdline_inline )
|
encrypted_payloads = encrypt_all_payloads_inline( raw_message_inline, gpg_to_cmdline_inline )
|
||||||
raw_message_inline.set_payload( encrypted_payloads )
|
raw_message_inline.set_payload( encrypted_payloads )
|
||||||
|
@ -632,8 +632,8 @@ def send_msg( message, recipients ):
|
||||||
log("Sending email to: <%s>" % '> <'.join( recipients ))
|
log("Sending email to: <%s>" % '> <'.join( recipients ))
|
||||||
relay = (cfg['relay']['host'], int(cfg['relay']['port']))
|
relay = (cfg['relay']['host'], int(cfg['relay']['port']))
|
||||||
smtp = smtplib.SMTP(relay[0], relay[1])
|
smtp = smtplib.SMTP(relay[0], relay[1])
|
||||||
if 'relay' in cfg and 'starttls' in cfg['relay'] and cfg['relay']['starttls'] == 'yes':
|
if 'relay' in cfg and 'starttls' in cfg['relay'] and cfg['relay']['starttls'] == 'yes':
|
||||||
smtp.starttls()
|
smtp.starttls()
|
||||||
smtp.sendmail( from_addr, recipients, message )
|
smtp.sendmail( from_addr, recipients, message )
|
||||||
else:
|
else:
|
||||||
log("No recipient found")
|
log("No recipient found")
|
||||||
|
@ -658,7 +658,7 @@ def sort_recipients( raw_message, from_addr, to_addrs ):
|
||||||
return
|
return
|
||||||
|
|
||||||
first_payload = first_payload.get_payload(decode=True)
|
first_payload = first_payload.get_payload(decode=True)
|
||||||
if "-----BEGIN PGP MESSAGE-----" in first_payload and "-----END PGP MESSAGE-----" in first_payload:
|
if b"-----BEGIN PGP MESSAGE-----" in first_payload and b"-----END PGP MESSAGE-----" in first_payload:
|
||||||
if verbose:
|
if verbose:
|
||||||
log("Message is already encrypted as PGP/INLINE. Encryption aborted.")
|
log("Message is already encrypted as PGP/INLINE. Encryption aborted.")
|
||||||
send_msg(raw_message.as_string(), recipients_left)
|
send_msg(raw_message.as_string(), recipients_left)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/local/bin/python2
|
#!/usr/local/bin/python3.8
|
||||||
|
|
||||||
#
|
#
|
||||||
# gpg-mailgate
|
# gpg-mailgate
|
||||||
|
@ -32,7 +32,7 @@ from time import sleep
|
||||||
RELAY_SCRIPT = "test/relay.py"
|
RELAY_SCRIPT = "test/relay.py"
|
||||||
CONFIG_FILE = "test/gpg-mailgate.conf"
|
CONFIG_FILE = "test/gpg-mailgate.conf"
|
||||||
|
|
||||||
PYTHON_BIN = "python2.7"
|
PYTHON_BIN = "python3.8"
|
||||||
|
|
||||||
def build_config(config):
|
def build_config(config):
|
||||||
cp = configparser.ConfigParser()
|
cp = configparser.ConfigParser()
|
||||||
|
@ -55,7 +55,7 @@ def build_config(config):
|
||||||
cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7")
|
cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7")
|
||||||
cp.set("enc_keymap", "bob@disposlab", "19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67")
|
cp.set("enc_keymap", "bob@disposlab", "19CF4B47ECC9C47AFA84D4BD96F39FDA0E31BB67")
|
||||||
|
|
||||||
logging.debug("Created config with keyhome=%s, cert_path=%s and relay at port %d" %
|
logging.debug("Created config with keyhome=%s, cert_path=%s and relay at port %s" %
|
||||||
(config["gpg_keyhome"], config["smime_certpath"], config["port"]))
|
(config["gpg_keyhome"], config["smime_certpath"], config["port"]))
|
||||||
return cp
|
return cp
|
||||||
|
|
||||||
|
@ -128,14 +128,14 @@ logging.basicConfig(filename = config.get("tests", "e2e_log"),
|
||||||
# Get raw values of log and date formats because they
|
# Get raw values of log and date formats because they
|
||||||
# contain %-sequences and we don't want them to be expanded
|
# contain %-sequences and we don't want them to be expanded
|
||||||
# by the ConfigParser.
|
# by the ConfigParser.
|
||||||
format = config.get("tests", "e2e_log_format", True),
|
format = config.get("tests", "e2e_log_format", raw=True),
|
||||||
datefmt = config.get("tests", "e2e_log_datefmt", True),
|
datefmt = config.get("tests", "e2e_log_datefmt", raw=True),
|
||||||
level = logging.DEBUG)
|
level = logging.DEBUG)
|
||||||
|
|
||||||
config_path = os.getcwd() + "/" + CONFIG_FILE
|
config_path = os.getcwd() + "/" + CONFIG_FILE
|
||||||
|
|
||||||
write_test_config(config_path,
|
write_test_config(config_path,
|
||||||
port = config.getint("relay", "port"),
|
port = config.get("relay", "port"),
|
||||||
gpg_keyhome = config.get("dirs", "keys"),
|
gpg_keyhome = config.get("dirs", "keys"),
|
||||||
smime_certpath = config.get("dirs", "certs"),
|
smime_certpath = config.get("dirs", "certs"),
|
||||||
log_file = config.get("tests", "lacre_log"))
|
log_file = config.get("tests", "lacre_log"))
|
||||||
|
|
|
@ -14,38 +14,43 @@ import socket
|
||||||
|
|
||||||
EXIT_UNAVAILABLE = 1
|
EXIT_UNAVAILABLE = 1
|
||||||
|
|
||||||
|
ENCODING = 'utf-8'
|
||||||
|
|
||||||
BUFFER_SIZE = 4096
|
BUFFER_SIZE = 4096
|
||||||
EOM = "\r\n.\r\n"
|
EOM = "\r\n.\r\n"
|
||||||
LAST_LINE = -3
|
LAST_LINE = -3
|
||||||
|
|
||||||
|
|
||||||
def welcome(msg):
|
def welcome(msg):
|
||||||
return "220 %s\r\n" % (msg)
|
return b"220 %b\r\n" % (msg)
|
||||||
|
|
||||||
def ok(msg = "OK"):
|
def ok(msg = b"OK"):
|
||||||
return "250 %s\r\n" % (msg)
|
return b"250 %b\r\n" % (msg)
|
||||||
|
|
||||||
def bye():
|
def bye():
|
||||||
return "251 Bye"
|
return b"251 Bye"
|
||||||
|
|
||||||
def provide_message():
|
def provide_message():
|
||||||
return "354 Enter a message, ending it with a '.' on a line by itself\r\n"
|
return b"354 Enter a message, ending it with a '.' on a line by itself\r\n"
|
||||||
|
|
||||||
def receive_and_confirm(session):
|
def receive_and_confirm(session):
|
||||||
session.recv(BUFFER_SIZE)
|
session.recv(BUFFER_SIZE)
|
||||||
session.sendall(ok())
|
session.sendall(ok())
|
||||||
|
|
||||||
|
def localhost_at(port):
|
||||||
|
return ('127.0.0.1', port)
|
||||||
|
|
||||||
def serve(port):
|
def serve(port):
|
||||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
s.bind(('', port))
|
s.bind(localhost_at(port))
|
||||||
s.listen(1)
|
s.listen(1)
|
||||||
except socket.error as e:
|
except socket.error as e:
|
||||||
print("Cannot connect", e)
|
print("Cannot connect", e)
|
||||||
sys.exit(EXIT_UNAVAILABLE)
|
sys.exit(EXIT_UNAVAILABLE)
|
||||||
|
|
||||||
(conn, addr) = s.accept()
|
(conn, addr) = s.accept()
|
||||||
conn.sendall(welcome("TEST SERVER"))
|
conn.sendall(welcome(b"TEST SERVER"))
|
||||||
|
|
||||||
receive_and_confirm(conn) # Ignore HELO/EHLO
|
receive_and_confirm(conn) # Ignore HELO/EHLO
|
||||||
receive_and_confirm(conn) # Ignore sender address
|
receive_and_confirm(conn) # Ignore sender address
|
||||||
|
@ -57,8 +62,8 @@ def serve(port):
|
||||||
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
|
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
|
||||||
message = ''
|
message = ''
|
||||||
while not message.endswith(EOM):
|
while not message.endswith(EOM):
|
||||||
message += conn.recv(BUFFER_SIZE)
|
message += conn.recv(BUFFER_SIZE).decode(ENCODING)
|
||||||
conn.sendall(ok("OK, id=test"))
|
conn.sendall(ok(b"OK, id=test"))
|
||||||
|
|
||||||
conn.recv(BUFFER_SIZE)
|
conn.recv(BUFFER_SIZE)
|
||||||
conn.sendall(bye())
|
conn.sendall(bye())
|
||||||
|
@ -68,13 +73,13 @@ def serve(port):
|
||||||
# Trim EOM marker as we're only interested in the message body.
|
# Trim EOM marker as we're only interested in the message body.
|
||||||
return message[:-len(EOM)]
|
return message[:-len(EOM)]
|
||||||
|
|
||||||
def error(msg):
|
def error(msg, exit_code):
|
||||||
print("ERROR: %s" % (msg))
|
print("ERROR: %s" % (msg))
|
||||||
sys.exit(1)
|
sys.exit(exit_code)
|
||||||
|
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
error("Usage: relay.py PORT_NUMBER")
|
error("Usage: relay.py PORT_NUMBER", EXIT_UNAVAILABLE)
|
||||||
|
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
body = serve(port)
|
body = serve(port)
|
||||||
|
|
Loading…
Reference in New Issue