Escape fetchmail parameters properly

master
Pierre Jaury 8 years ago
parent 55d5121816
commit 709869d4ba

@ -4,6 +4,7 @@ import sqlite3
import time import time
import os import os
import tempfile import tempfile
import shlex
FETCHMAIL = """ FETCHMAIL = """
@ -14,19 +15,24 @@ fetchmail -N \
RC_LINE = """ RC_LINE = """
poll {host} proto {protocol} port {port} poll "{host}" proto {protocol} port {port}
user "{username}" password "{password}" user "{username}" password "{password}"
smtphost "smtp" smtphost "smtp"
smtpname {user_email} smtpname "{user_email}"
{options} {options}
""" """
def escape_rc_string(arg):
return arg.replace("\\", "\\\\").replace('"', '\\"')
def fetchmail(fetchmailrc): def fetchmail(fetchmailrc):
print(fetchmailrc)
with tempfile.NamedTemporaryFile() as handler: with tempfile.NamedTemporaryFile() as handler:
handler.write(fetchmailrc.encode("utf8")) handler.write(fetchmailrc.encode("utf8"))
handler.flush() handler.flush()
os.system(FETCHMAIL.format(handler.name)) os.system(FETCHMAIL.format(shlex.quote(handler.name)))
def run(cursor): def run(cursor):
@ -39,12 +45,12 @@ def run(cursor):
user_email, protocol, host, port, tls, username, password = line user_email, protocol, host, port, tls, username, password = line
options = "options ssl" if tls else "" options = "options ssl" if tls else ""
fetchmailrc += RC_LINE.format( fetchmailrc += RC_LINE.format(
user_email=user_email, user_email=escape_rc_string(user_email),
protocol=protocol, protocol=protocol,
host=host, host=escape_rc_string(host),
port=port, port=port,
username=username, username=escape_rc_string(username),
password=password, password=escape_rc_string(password),
options=options options=options
) )
fetchmail(fetchmailrc) fetchmail(fetchmailrc)

Loading…
Cancel
Save