From cc31355b9901117b6145a14009060a1d4a8bccb0 Mon Sep 17 00:00:00 2001 From: manuel Date: Wed, 4 Apr 2018 22:21:48 +0200 Subject: add X-UD-Smtp-Session and remove ip from authed clients --- qmail-smtpd.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- received.c | 31 +++++++++++++++++++++++++++++++ received.h | 1 + 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/qmail-smtpd.c b/qmail-smtpd.c index 95993eb..6b4262f 100644 --- a/qmail-smtpd.c +++ b/qmail-smtpd.c @@ -1,3 +1,4 @@ +#include #include "sig.h" #include "readwrite.h" #include "stralloc.h" @@ -594,7 +595,7 @@ void mailfrom_parms(arg) char *arg; if (case_starts(mfparms.s,"SMTPUTF8")) smtputf8 = 1; #endif if (case_starts(mfparms.s,"SIZE=")) if (mailfrom_size(mfparms.s+5)) { flagsize = 1; return; } - if (case_starts(mfparms.s,"AUTH=")) mailfrom_auth(mfparms.s+5,mfparms.len-5); + if (case_starts(mfparms.s,"AUTH=")) mailfrom_auth(mfparms.s+5,mfparms.len-5); if (!stralloc_copys(&mfparms,"")) die_nomem; } else @@ -603,6 +604,55 @@ void mailfrom_parms(arg) char *arg; } } +#define V4MAPPREFIX "::ffff:" +static const stralloc *client_get_session_id() +{ + static stralloc buf = {0}, base64_buf = {0}; + const char *tmp; + struct timeval tv; + uint64_t timestamp; + unsigned int i; + unsigned long port; + int family = AF_INET; + + if (base64_buf.s) + return &base64_buf; + + if (!stralloc_ready(&buf, 24)) die_nomem(); + + /* add lowest 48 bits of the timestamp. this gives us a bit less than + 9 years until it wraps */ + gettimeofday(&tv, NULL); + timestamp = tv.tv_usec + (long long)tv.tv_sec * 1000ULL*1000ULL; + for (i = 0; i < 48; i += 8) + buf.s[buf.len++] = (timestamp >> i) & 0xff; + + if ((tmp = getenv("TCPREMOTEPORT")) != NULL && scan_ulong(tmp, &port)) + { + buf.s[buf.len] = port & 0xff; + buf.s[buf.len+1] = (port >> 8) & 0xff; + } + buf.len += 2; + + family = ((tmp = getenv("PROTO")) && strcmp(tmp, "TCP6") == 0) + ? AF_INET6 : AF_INET; + if ((tmp = getenv("TCPREMOTEIP"))) + { + if (family == AF_INET6 && !strncmp(tmp, V4MAPPREFIX, strlen(V4MAPPREFIX))) + { + tmp += strlen(V4MAPPREFIX); + family = AF_INET; + } + (void)inet_pton(family, tmp, buf.s + buf.len); + buf.len += (family == AF_INET) ? 4 : 16; + } + + if (b64encode(&buf, &base64_buf) < 0) die_nomem(); + if (!stralloc_0(&base64_buf)) die_nomem(); + if (!env_put2("SMTPSESSION", base64_buf.s)) die_nomem(); + return &base64_buf; +} + void smtp_helo(arg) char *arg; { if(!spp_helo(arg)) return; @@ -878,7 +928,10 @@ void smtp_data(arg) char *arg; { protocol = utf8proto.s; } - received(&qqt,protocol,local,remoteip,remotehost,remoteinfo,fakehelo); + if (flagauth) + received_authed(&qqt,protocol,local,remoteinfo,client_get_session_id()->s); + else + received(&qqt,protocol,local,remoteip,remotehost,remoteinfo,fakehelo); qmail_put(&qqt,sppheaders.s,sppheaders.len); /* set in qmail-spp.c */ spp_rset(); blast(&hops); diff --git a/received.c b/received.c index 07706d5..f38545e 100644 --- a/received.c +++ b/received.c @@ -69,3 +69,34 @@ char *helo; datetime_tai(&dt,now()); qmail_put(qqt,buf,date822fmt(buf,&dt)); } + +/* "Received: by silverton.berkeley.edu with SMTP; 26 Sep 1995 04:46:54 -0000\n" + * "X-UD-Smtp-Session: user@sessionid */ + +void received_authed(qqt,protocol,local,remoteinfo,remotesession) +struct qmail *qqt; +char *protocol; +char *local; +char *remoteinfo; +char *remotesession; +{ + struct datetime dt; + + qmail_puts(qqt,"Received: by "); + safeput(qqt,local); + qmail_puts(qqt," with "); + qmail_puts(qqt,protocol); + qmail_puts(qqt,"; "); + datetime_tai(&dt,now()); + qmail_put(qqt,buf,date822fmt(buf,&dt)); + if (remoteinfo || remotesession) { + qmail_puts(qqt,"X-UD-Smtp-Session: "); + if (remoteinfo) { + safeput(qqt,remoteinfo); + qmail_puts(qqt,"@"); + } + if (remotesession) + qmail_puts(qqt,remotesession); + qmail_put(qqt,"\n",1); + } +} diff --git a/received.h b/received.h index 4e39dda..7c98c28 100644 --- a/received.h +++ b/received.h @@ -2,5 +2,6 @@ #define RECEIVED_H extern void received(); +extern void received_authed(); #endif -- cgit v1.2.3