| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""A safer sendmail command""" |
| 5 |
|
| 6 |
> |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
from __future__ import division, absolute_import |
| 22 |
from __future__ import print_function, unicode_literals |
| 23 |
|
| 24 |
import argparse |
| 25 |
import copy |
| 26 |
import logging |
| 27 |
import logging.handlers |
| 28 |
import os |
| 29 |
import subprocess |
| 30 |
import sys |
| 31 |
|
| 32 |
__description__ = """nullmailer-rsendmail compatibility shim.""" |
| 33 |
|
| 34 |
|
| 35 |
def parse_args(): |
| 36 |
parser = argparse.ArgumentParser(description=__doc__, epilog=__description__) |
| 37 |
parser.add_argument( |
| 38 |
"--loglevel", |
| 39 |
dest="loglevel", |
| 40 |
default="WARNING", |
| 41 |
help="level should be sent to syslog (default: %(default)s)", |
| 42 |
) |
| 43 |
default_mta = "postfix" |
| 44 |
if "nullmailer" in os.path.realpath(sys.argv[0]): |
| 45 |
default_mta = "nullmailer" |
| 46 |
parser.add_argument( |
| 47 |
"--mta", |
| 48 |
choices=["postfix", "nullmailer"], |
| 49 |
default=default_mta, |
| 50 |
help="communication interface with parent MTA (default: %(default)s)", |
| 51 |
) |
| 52 |
parser.add_argument("--identity", help="path to SSH private key") |
| 53 |
parser.add_argument("--user", help="username for the SSH host") |
| 54 |
parser.add_argument("--host", help="remote email gateway running rsendmail") |
| 55 |
parser.add_argument("-f", dest="sender", help="enveloper sender") |
| 56 |
parser.add_argument( |
| 57 |
"-d", |
| 58 |
"-s", |
| 59 |
dest="nullmailer1", |
| 60 |
action="store_true", |
| 61 |
help="arguments passed by nullmailer1, triggers an exception", |
| 62 |
) |
| 63 |
parser.add_argument( |
| 64 |
"recipients", |
| 65 |
nargs="*", |
| 66 |
help="who to send the email to, may be parsed from MTA-specific headers instead", |
| 67 |
) |
| 68 |
args = parser.parse_args() |
| 69 |
if args.mta != "nullmailer" and not args.host: |
| 70 |
parser.error("a remote host should be provided as --host") |
| 71 |
if args.nullmailer1: |
| 72 |
parser.error("nullmailer 1 flags detected, aborting") |
| 73 |
return args |
| 74 |
|
| 75 |
|
| 76 |
def setupLogging(syslog): |
| 77 |
logger = logging.getLogger("") |
| 78 |
|
| 79 |
logger.setLevel("DEBUG") |
| 80 |
sl = logging.handlers.SysLogHandler(address="/dev/log", facility="mail") |
| 81 |
sl.setFormatter( |
| 82 |
logging.Formatter(sys.argv[0] + "[%(process)d]: %(levelname)s: %(message)s") |
| 83 |
) |
| 84 |
|
| 85 |
sl.setLevel(syslog.upper()) |
| 86 |
logger.addHandler(sl) |
| 87 |
|
| 88 |
|
| 89 |
def readNullMailer2Options(args, stream): |
| 90 |
"""read nullmailer configuration |
| 91 |
|
| 92 |
nullmailer sends its configuration on stdin with one option per |
| 93 |
line, in key=value pairs separated by equal signs. we parse from |
| 94 |
the ``stream`` argument to facilitate tests. |
| 95 |
|
| 96 |
store the resulting config in the ``args`` namespace. |
| 97 |
""" |
| 98 |
for line in stream: |
| 99 |
|
| 100 |
line = line.strip() |
| 101 |
logging.debug("got config from nullmailer: %s", line) |
| 102 |
if line == "": |
| 103 |
break |
| 104 |
if "=" in line: |
| 105 |
name, val = line.split("=", 1) |
| 106 |
setattr(args, name, val) |
| 107 |
else: |
| 108 |
logging.warning("unexpected config syntax from nullmailer: %s", line) |
| 109 |
|
| 110 |
|
| 111 |
def readNullMailer2Envelope(stream): |
| 112 |
"""read envelope information from nullmailer |
| 113 |
|
| 114 |
nullmailer sends addresses one per line. the first address is the |
| 115 |
"envelope sender address", who to send errors to and equivalent to |
| 116 |
the `-f` sendmail flag. then the remaining lines are recipients. |
| 117 |
|
| 118 |
extract the sender and recipients, which are return as a tuple. |
| 119 |
""" |
| 120 |
sender = None |
| 121 |
recipients = [] |
| 122 |
try: |
| 123 |
for line in stream: |
| 124 |
line = line.strip() |
| 125 |
logging.debug("got envelope from nullmailer: %s", line) |
| 126 |
|
| 127 |
if line == b"": |
| 128 |
break |
| 129 |
assert ( |
| 130 |
len(line) > 3 |
| 131 |
), "sanity check: emails are generally longer than three characters" |
| 132 |
|
| 133 |
if not sender: |
| 134 |
|
| 135 |
sender = line.decode(errors="backslashreplace") |
| 136 |
else: |
| 137 |
|
| 138 |
recipients.append(line.decode(errors="backslashreplace")) |
| 139 |
except OSError as e: |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if "read() should have returned a bytes object, not 'NoneType'" in str(e): |
| 145 |
logging.error( |
| 146 |
"cannot open file descriptor 3, are you running nullmailer 2.x?" |
| 147 |
) |
| 148 |
return sender, recipients |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
nullmailer_sysexits_map = { |
| 153 |
os.EX_OK: "EX_OK", |
| 154 |
|
| 155 |
|
| 156 |
255: "ERR_CONN_FAILED", |
| 157 |
|
| 158 |
os.EX_NOUSER: "ERR_MSG_REFUSED", |
| 159 |
os.EX_UNAVAILABLE: "ERR_EXEC_FAILED", |
| 160 |
|
| 161 |
|
| 162 |
os.EX_SOFTWARE: "ERR_USAGE", |
| 163 |
os.EX_OSERR: "ERR_EXEC_FAILED", |
| 164 |
os.EX_NOHOST: "ERR_NO_ADDRESS", |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
1: "ERR_USAGE", |
| 174 |
os.EX_TEMPFAIL: "ERR_MSG_TEMPFAIL", |
| 175 |
os.EX_NOPERM: "ERR_MSG_REFUSED", |
| 176 |
os.EX_USAGE: "ERR_USAGE", |
| 177 |
os.EX_DATAERR: "ERR_USAGE", |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
100: "ERR_USAGE", |
| 189 |
111: "ERR_EXEC_FAILED", |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
nullmailer_error_table = { |
| 194 |
|
| 195 |
"EX_OK": os.EX_OK, |
| 196 |
|
| 197 |
"ERR_USAGE": 1, |
| 198 |
"ERR_HOST_NOT_FOUND": 2, |
| 199 |
"ERR_NO_ADDRESS": 3, |
| 200 |
"ERR_GHBN_TEMP": 5, |
| 201 |
"ERR_SOCKET": 6, |
| 202 |
"ERR_CONN_REFUSED": 7, |
| 203 |
"ERR_CONN_TIMEDOUT": 8, |
| 204 |
"ERR_CONN_UNREACHABLE": 9, |
| 205 |
"ERR_CONN_FAILED": 10, |
| 206 |
"ERR_PROTO": 11, |
| 207 |
"ERR_MSG_OPEN": 12, |
| 208 |
"ERR_MSG_READ": 13, |
| 209 |
"ERR_MSG_WRITE": 14, |
| 210 |
"ERR_EXEC_FAILED": 15, |
| 211 |
"ERR_MSG_TEMPFAIL": 16, |
| 212 |
"ERR_UNKNOWN": 17, |
| 213 |
"ERR_CONFIG": 18, |
| 214 |
"ERR_BIND_FAILED": 19, |
| 215 |
|
| 216 |
"ERR_PERMANENT_FLAG": 32, |
| 217 |
"ERR_GHBN_FATAL": 33, |
| 218 |
"ERR_MSG_REFUSED": 34, |
| 219 |
"ERR_MSG_PERMFAIL": 35, |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
def main(): |
| 224 |
argv = copy.copy(sys.argv) |
| 225 |
args = parse_args() |
| 226 |
setupLogging(args.loglevel) |
| 227 |
|
| 228 |
logging.debug("config: %s", args) |
| 229 |
if args.mta == "nullmailer": |
| 230 |
logging.debug("argv: %s", argv) |
| 231 |
readNullMailer2Options(args, sys.stdin) |
| 232 |
logging.debug("modified config: %s", args) |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
message = open(3, mode="rb", buffering=0) |
| 237 |
args.sender, args.recipients = readNullMailer2Envelope(message) |
| 238 |
elif args.mta == "postfix": |
| 239 |
message = sys.stdin |
| 240 |
else: |
| 241 |
|
| 242 |
logging.error("unsupported MTA: %s", args.mta) |
| 243 |
return 1 |
| 244 |
|
| 245 |
|
| 246 |
command = ["ssh", args.host] |
| 247 |
if args.identity: |
| 248 |
command += ["-i", args.identity] |
| 249 |
if args.user: |
| 250 |
command += ["-l", args.user] |
| 251 |
|
| 252 |
|
| 253 |
remote_command = ["rsendmail"] |
| 254 |
if args.sender: |
| 255 |
remote_command += ["-f", args.sender] |
| 256 |
if args.recipients: |
| 257 |
remote_command += args.recipients |
| 258 |
command.append(" ".join(remote_command)) |
| 259 |
|
| 260 |
logging.info("sending email through %s", command) |
| 261 |
process = subprocess.Popen(command, shell=False, close_fds=False, stdin=message) |
| 262 |
status = process.wait() |
| 263 |
if status != 0: |
| 264 |
logging.error("command failed with exit status %d: %s", status, command) |
| 265 |
|
| 266 |
if args.mta == "nullmailer": |
| 267 |
|
| 268 |
return nullmailer_error_table[ |
| 269 |
nullmailer_sysexits_map.get(status, "ERR_UNKNOWN") |
| 270 |
] |
| 271 |
elif args.mta == "postfix": |
| 272 |
|
| 273 |
if status == 255: |
| 274 |
return os.EX_TEMPFAIL |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
else: |
| 280 |
return status |
| 281 |
else: |
| 282 |
|
| 283 |
return os.EX_SOFTWARE |
| 284 |
|
| 285 |
|
| 286 |
if __name__ == "__main__": |
| 287 |
try: |
| 288 |
sys.exit(main()) |
| 289 |
except Exception as e: |
| 290 |
logging.exception("got exception %s", e) |
| 291 |
sys.exit(1) |