From 734c968030e367502664b4bddb75b6864e9e193e Mon Sep 17 00:00:00 2001 From: Vladimir Dubrovin <3proxy@3proxy.ru> Date: Wed, 18 Nov 2026 20:10:10 +0300 Subject: [PATCH] smtpp: a login name of a port alone names no destination --- src/common.c | 3 ++ src/smtpp.c | 4 ++ tests/cases/smtpp.py | 91 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 tests/cases/smtpp.py diff --git a/src/common.c b/src/common.c index e19fbb3..3571845 100644 --- a/src/common.c +++ b/src/common.c @@ -560,6 +560,9 @@ int parsehostname(char *hostname, struct clientparam *param, uint16_t port){ port = atoi(sp+1); *sp = ':'; } + /* A port alone overrides the port of a host already set; without + one it names no destination to connect to. */ + if(!param->hostname || !*param->hostname) return 1; *SAPORT(¶m->req) = htons(port); return 0; } diff --git a/src/smtpp.c b/src/smtpp.c index 6bde921..de5f7c1 100644 --- a/src/smtpp.c +++ b/src/smtpp.c @@ -197,6 +197,10 @@ void * smtppchild(struct clientparam* param) { } } + /* A login has to end with a host to connect to: what follows reads the + host name, and without one there is nowhere to go. */ + if(!param->hostname || !param->extusername){RETURN(669);} + #ifndef WITHMAIN { int action, reqbufsize, reqsize; diff --git a/tests/cases/smtpp.py b/tests/cases/smtpp.py new file mode 100644 index 0000000..37caa1d --- /dev/null +++ b/tests/cases/smtpp.py @@ -0,0 +1,91 @@ +"""The SMTP proxy login: what it may be sent, and what may not be done.""" + +import base64 +import socket + + +def recvline(sock): + """One reply line, shorter only when the peer stops sending.""" + data = b"" + while not data.endswith(b"\n"): + chunk = sock.recv(1) + if not chunk: + break + data += chunk + return data + + +def drain_ehlo(sock): + """Read the multi-line EHLO answer up to its last line.""" + while True: + line = recvline(sock) + if not line or line.startswith(b"250 "): + return + + +def run(t): + if not t.skip_unless("smtpp"): + return + sp = t.free_port() + pp = t.free_port() + t.start("smtpp_login", f""" + log + auth iponly + allow * + proxy -p{pp} + + flush + auth none + allow * + smtpp -p{sp} + """, ports=[pp, sp]) + + def auth_login(name): + """The reply the login ends with for the given name.""" + with socket.create_connection(("127.0.0.1", sp), t.timeout) as s: + s.settimeout(t.timeout) + recvline(s) + s.sendall(b"EHLO case\r\n") + drain_ehlo(s) + s.sendall(b"AUTH LOGIN\r\n") + recvline(s) + s.sendall(base64.b64encode(name) + b"\r\n") + reply = recvline(s) + # the name carries the destination, so a refusal can come with + # the username already, before any password is asked for + if not reply.startswith(b"334"): + return reply + s.sendall(base64.b64encode(b"pw") + b"\r\n") + return recvline(s) + + def auth_plain(name): + """The reply of a one-line AUTH PLAIN carrying the given name.""" + with socket.create_connection(("127.0.0.1", sp), t.timeout) as s: + s.settimeout(t.timeout) + recvline(s) + s.sendall(b"EHLO case\r\n") + drain_ehlo(s) + s.sendall(b"AUTH PLAIN " + + base64.b64encode(b"\x00" + name + b"\x00pw") + + b"\r\n") + return recvline(s) + + # A name of a port alone names no destination: the login is refused + # with a reply, and the daemon is left standing, its other listeners + # among them. + t.contains(auth_login(b"x@:25"), b"571", + "a login name of a port alone is refused") + t.contains(auth_plain(b"x@:25"), b"571", + "the same name over AUTH PLAIN is refused") + t.contains(auth_login(b"x@127.0.0.1:9"), b"571", + "a login name of a dead host is refused") + + with socket.create_connection(("127.0.0.1", sp), t.timeout) as s: + s.settimeout(t.timeout) + t.eq(True, recvline(s).startswith(b"220"), + "the SMTP proxy answers again after the refused logins") + try: + with socket.create_connection(("127.0.0.1", pp), t.timeout): + t.ok("the other services of the process keep accepting") + except OSError: + t.fail("the other services of the process keep accepting")