Fix auto-migrated code

- Use b'' (byte strings) where appropriate.

- Fix indentation.

- Replace python2.x references with python3.x.
This commit is contained in:
Piotr F. Mieszkowski 2022-01-09 15:01:38 +01:00
parent 5f02223ec7
commit b2a01c15b0
4 changed files with 34 additions and 29 deletions

View File

@ -1,4 +1,4 @@
PYTHON = python2.7
PYTHON = python3.8
.PHONY: test pre-clean clean

View File

@ -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
message.attach(payload)
return message, success
return message, success
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'
if 'Content-Transfer-Encoding' in raw_message_mime:
raw_message_mime.replace_header('Content-Transfer-Encoding', '8BIT')
else:
raw_message_mime['Content-Transfer-Encoding'] = '8BIT'
raw_message_mime.replace_header('Content-Transfer-Encoding', '8BIT')
else:
raw_message_mime['Content-Transfer-Encoding'] = '8BIT'
encrypted_payloads = encrypt_all_payloads_mime( raw_message_mime, gpg_to_cmdline_mime )
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'
if 'Content-Transfer-Encoding' in raw_message_inline:
raw_message_inline.replace_header('Content-Transfer-Encoding', '8BIT')
else:
raw_message_inline['Content-Transfer-Encoding'] = '8BIT'
raw_message_inline.replace_header('Content-Transfer-Encoding', '8BIT')
else:
raw_message_inline['Content-Transfer-Encoding'] = '8BIT'
encrypted_payloads = encrypt_all_payloads_inline( raw_message_inline, gpg_to_cmdline_inline )
raw_message_inline.set_payload( encrypted_payloads )
@ -632,8 +632,8 @@ def send_msg( message, recipients ):
log("Sending email to: <%s>" % '> <'.join( recipients ))
relay = (cfg['relay']['host'], int(cfg['relay']['port']))
smtp = smtplib.SMTP(relay[0], relay[1])
if 'relay' in cfg and 'starttls' in cfg['relay'] and cfg['relay']['starttls'] == 'yes':
smtp.starttls()
if 'relay' in cfg and 'starttls' in cfg['relay'] and cfg['relay']['starttls'] == 'yes':
smtp.starttls()
smtp.sendmail( from_addr, recipients, message )
else:
log("No recipient found")
@ -658,7 +658,7 @@ def sort_recipients( raw_message, from_addr, to_addrs ):
return
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:
log("Message is already encrypted as PGP/INLINE. Encryption aborted.")
send_msg(raw_message.as_string(), recipients_left)

View File

@ -1,4 +1,4 @@
#!/usr/local/bin/python2
#!/usr/local/bin/python3.8
#
# gpg-mailgate
@ -32,7 +32,7 @@ from time import sleep
RELAY_SCRIPT = "test/relay.py"
CONFIG_FILE = "test/gpg-mailgate.conf"
PYTHON_BIN = "python2.7"
PYTHON_BIN = "python3.8"
def build_config(config):
cp = configparser.ConfigParser()
@ -55,7 +55,7 @@ def build_config(config):
cp.set("enc_keymap", "alice@disposlab", "1CD245308F0963D038E88357973CF4D9387C44D7")
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"]))
return cp
@ -128,14 +128,14 @@ logging.basicConfig(filename = config.get("tests", "e2e_log"),
# Get raw values of log and date formats because they
# contain %-sequences and we don't want them to be expanded
# by the ConfigParser.
format = config.get("tests", "e2e_log_format", True),
datefmt = config.get("tests", "e2e_log_datefmt", True),
format = config.get("tests", "e2e_log_format", raw=True),
datefmt = config.get("tests", "e2e_log_datefmt", raw=True),
level = logging.DEBUG)
config_path = os.getcwd() + "/" + CONFIG_FILE
write_test_config(config_path,
port = config.getint("relay", "port"),
port = config.get("relay", "port"),
gpg_keyhome = config.get("dirs", "keys"),
smime_certpath = config.get("dirs", "certs"),
log_file = config.get("tests", "lacre_log"))

View File

@ -14,38 +14,43 @@ import socket
EXIT_UNAVAILABLE = 1
ENCODING = 'utf-8'
BUFFER_SIZE = 4096
EOM = "\r\n.\r\n"
LAST_LINE = -3
def welcome(msg):
return "220 %s\r\n" % (msg)
return b"220 %b\r\n" % (msg)
def ok(msg = "OK"):
return "250 %s\r\n" % (msg)
def ok(msg = b"OK"):
return b"250 %b\r\n" % (msg)
def bye():
return "251 Bye"
return b"251 Bye"
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):
session.recv(BUFFER_SIZE)
session.sendall(ok())
def localhost_at(port):
return ('127.0.0.1', port)
def serve(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('', port))
s.bind(localhost_at(port))
s.listen(1)
except socket.error as e:
print("Cannot connect", e)
sys.exit(EXIT_UNAVAILABLE)
(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 sender address
@ -57,8 +62,8 @@ def serve(port):
# Consume until we get <CR><LF>.<CR><LF>, the end-of-message marker.
message = ''
while not message.endswith(EOM):
message += conn.recv(BUFFER_SIZE)
conn.sendall(ok("OK, id=test"))
message += conn.recv(BUFFER_SIZE).decode(ENCODING)
conn.sendall(ok(b"OK, id=test"))
conn.recv(BUFFER_SIZE)
conn.sendall(bye())
@ -68,13 +73,13 @@ def serve(port):
# Trim EOM marker as we're only interested in the message body.
return message[:-len(EOM)]
def error(msg):
def error(msg, exit_code):
print("ERROR: %s" % (msg))
sys.exit(1)
sys.exit(exit_code)
if len(sys.argv) < 2:
error("Usage: relay.py PORT_NUMBER")
error("Usage: relay.py PORT_NUMBER", EXIT_UNAVAILABLE)
port = int(sys.argv[1])
body = serve(port)