|
|
@ -2,11 +2,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
import time
|
|
|
|
import os
|
|
|
|
import os
|
|
|
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
from pwd import getpwnam
|
|
|
|
import tempfile
|
|
|
|
import tempfile
|
|
|
|
import shlex
|
|
|
|
import shlex
|
|
|
|
import subprocess
|
|
|
|
import subprocess
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
import requests
|
|
|
|
import requests
|
|
|
|
|
|
|
|
from socrate import system
|
|
|
|
import sys
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import traceback
|
|
|
|
|
|
|
|
|
|
|
@ -14,6 +17,7 @@ import traceback
|
|
|
|
FETCHMAIL = """
|
|
|
|
FETCHMAIL = """
|
|
|
|
fetchmail -N \
|
|
|
|
fetchmail -N \
|
|
|
|
--idfile /data/fetchids --uidl \
|
|
|
|
--idfile /data/fetchids --uidl \
|
|
|
|
|
|
|
|
--pidfile /dev/shm/fetchmail.pid \
|
|
|
|
--sslcertck --sslcertpath /etc/ssl/certs \
|
|
|
|
--sslcertck --sslcertpath /etc/ssl/certs \
|
|
|
|
-f {}
|
|
|
|
-f {}
|
|
|
|
"""
|
|
|
|
"""
|
|
|
@ -24,7 +28,9 @@ poll "{host}" proto {protocol} port {port}
|
|
|
|
user "{username}" password "{password}"
|
|
|
|
user "{username}" password "{password}"
|
|
|
|
is "{user_email}"
|
|
|
|
is "{user_email}"
|
|
|
|
smtphost "{smtphost}"
|
|
|
|
smtphost "{smtphost}"
|
|
|
|
|
|
|
|
{folders}
|
|
|
|
{options}
|
|
|
|
{options}
|
|
|
|
|
|
|
|
{lmtp}
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -48,26 +54,37 @@ def fetchmail(fetchmailrc):
|
|
|
|
|
|
|
|
|
|
|
|
def run(debug):
|
|
|
|
def run(debug):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
fetches = requests.get("http://" + os.environ.get("HOST_ADMIN", "admin") + "/internal/fetch").json()
|
|
|
|
os.environ["SMTP_ADDRESS"] = system.get_host_address_from_environment("SMTP", "smtp")
|
|
|
|
smtphost, smtpport = extract_host_port(os.environ.get("HOST_SMTP", "smtp"), None)
|
|
|
|
os.environ["ADMIN_ADDRESS"] = system.get_host_address_from_environment("ADMIN", "admin")
|
|
|
|
|
|
|
|
fetches = requests.get(f"http://{os.environ['ADMIN_ADDRESS']}/internal/fetch").json()
|
|
|
|
|
|
|
|
smtphost, smtpport = extract_host_port(os.environ["SMTP_ADDRESS"], None)
|
|
|
|
if smtpport is None:
|
|
|
|
if smtpport is None:
|
|
|
|
smtphostport = smtphost
|
|
|
|
smtphostport = smtphost
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
smtphostport = "%s/%d" % (smtphost, smtpport)
|
|
|
|
smtphostport = "%s/%d" % (smtphost, smtpport)
|
|
|
|
|
|
|
|
os.environ["LMTP_ADDRESS"] = system.get_host_address_from_environment("LMTP", "imap:2525")
|
|
|
|
|
|
|
|
lmtphost, lmtpport = extract_host_port(os.environ["LMTP_ADDRESS"], None)
|
|
|
|
|
|
|
|
if lmtpport is None:
|
|
|
|
|
|
|
|
lmtphostport = lmtphost
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
lmtphostport = "%s/%d" % (lmtphost, lmtpport)
|
|
|
|
for fetch in fetches:
|
|
|
|
for fetch in fetches:
|
|
|
|
fetchmailrc = ""
|
|
|
|
fetchmailrc = ""
|
|
|
|
options = "options antispam 501, 504, 550, 553, 554"
|
|
|
|
options = "options antispam 501, 504, 550, 553, 554"
|
|
|
|
options += " ssl" if fetch["tls"] else ""
|
|
|
|
options += " ssl" if fetch["tls"] else ""
|
|
|
|
options += " keep" if fetch["keep"] else " fetchall"
|
|
|
|
options += " keep" if fetch["keep"] else " fetchall"
|
|
|
|
|
|
|
|
folders = "folders %s" % ((','.join('"' + item + '"' for item in fetch['folders'])) if fetch['folders'] else '"INBOX"')
|
|
|
|
fetchmailrc += RC_LINE.format(
|
|
|
|
fetchmailrc += RC_LINE.format(
|
|
|
|
user_email=escape_rc_string(fetch["user_email"]),
|
|
|
|
user_email=escape_rc_string(fetch["user_email"]),
|
|
|
|
protocol=fetch["protocol"],
|
|
|
|
protocol=fetch["protocol"],
|
|
|
|
host=escape_rc_string(fetch["host"]),
|
|
|
|
host=escape_rc_string(fetch["host"]),
|
|
|
|
port=fetch["port"],
|
|
|
|
port=fetch["port"],
|
|
|
|
smtphost=smtphostport,
|
|
|
|
smtphost=smtphostport if fetch['scan'] else lmtphostport,
|
|
|
|
username=escape_rc_string(fetch["username"]),
|
|
|
|
username=escape_rc_string(fetch["username"]),
|
|
|
|
password=escape_rc_string(fetch["password"]),
|
|
|
|
password=escape_rc_string(fetch["password"]),
|
|
|
|
options=options
|
|
|
|
options=options,
|
|
|
|
|
|
|
|
folders=folders,
|
|
|
|
|
|
|
|
lmtp='' if fetch['scan'] else 'lmtp',
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if debug:
|
|
|
|
if debug:
|
|
|
|
print(fetchmailrc)
|
|
|
|
print(fetchmailrc)
|
|
|
@ -86,7 +103,7 @@ def run(debug):
|
|
|
|
user_info in error_message):
|
|
|
|
user_info in error_message):
|
|
|
|
print(error_message)
|
|
|
|
print(error_message)
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
requests.post("http://" + os.environ.get("HOST_ADMIN", "admin") + "/internal/fetch/{}".format(fetch["id"]),
|
|
|
|
requests.post("http://" + os.environ["ADMIN_ADDRESS"] + "/internal/fetch/{}".format(fetch["id"]),
|
|
|
|
json=error_message.split("\n")[0]
|
|
|
|
json=error_message.split("\n")[0]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
@ -94,6 +111,13 @@ def run(debug):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
|
|
id_fetchmail = getpwnam('fetchmail')
|
|
|
|
|
|
|
|
Path('/data/fetchids').touch()
|
|
|
|
|
|
|
|
os.chown("/data/fetchids", id_fetchmail.pw_uid, id_fetchmail.pw_gid)
|
|
|
|
|
|
|
|
os.chown("/data/", id_fetchmail.pw_uid, id_fetchmail.pw_gid)
|
|
|
|
|
|
|
|
os.chmod("/data/fetchids", 0o700)
|
|
|
|
|
|
|
|
os.setgid(id_fetchmail.pw_gid)
|
|
|
|
|
|
|
|
os.setuid(id_fetchmail.pw_uid)
|
|
|
|
while True:
|
|
|
|
while True:
|
|
|
|
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 60)))
|
|
|
|
time.sleep(int(os.environ.get("FETCHMAIL_DELAY", 60)))
|
|
|
|
run(os.environ.get("DEBUG", None) == "True")
|
|
|
|
run(os.environ.get("DEBUG", None) == "True")
|
|
|
|