Skip to content

Commit

Permalink
refactor as per suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhangui committed Jan 25, 2021
1 parent 4e1ba81 commit 44a00c9
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 108 deletions.
37 changes: 0 additions & 37 deletions .travis.yml

This file was deleted.

10 changes: 6 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,8 @@ alloc.h substdio.h datetime.h now.h datetime.h triggerpull.h extra.h \
uidgid.h auto_qmail.h auto_uids.h auto_users.h date822fmt.h fmtqfn.h
./compile qmail-queue.c

# use tryidn21 for idn2.lib instead of tryidn2 else parallel compile race condition will
# break things for idn2.lib
hassmtputf8.h: \
tryidn2.c compile load conf-smtputf8
((./compile `grep -h -v "^#" conf-smtputf8` tryidn2.c \
Expand All @@ -1357,10 +1359,10 @@ tryidn2.c compile load conf-smtputf8

idn2.lib: \
tryidn2.c compile load conf-smtputf8
((./compile `grep -h -v "^#" conf-smtputf8` tryidn2.c \
&& ./load tryidn2 -lidn2) >/dev/null 2>&1 \
((./compile `grep -h -v "^#" conf-smtputf8` tryidn2.c -o tryidn21.o \
&& ./load tryidn21 -lidn2) >/dev/null 2>&1 \
&& echo "-lidn2" || echo "WARNING!!! Not linked with libidn2" 1>&2) > idn2.lib
rm -f tryidn2.o tryidn2
rm -f tryidn21.o tryidn21

utf8read.o: \
compile utf8read.c hassmtputf8.h stralloc.h case.h substdio.h subfd.h
Expand Down Expand Up @@ -1484,7 +1486,7 @@ compile qmail-smtpd.c sig.h readwrite.h stralloc.h gen_alloc.h \
substdio.h alloc.h auto_qmail.h control.h received.h constmap.h \
error.h ipme.h ip.h ipalloc.h ip.h gen_alloc.h ip.h qmail.h \
substdio.h str.h fmt.h scan.h byte.h case.h env.h now.h datetime.h \
exit.h rcpthosts.h timeoutread.h timeoutwrite.h commands.h hassmtputf8.h
exit.h rcpthosts.h timeoutread.h timeoutwrite.h commands.h
./compile qmail-smtpd.c

qmail-start: \
Expand Down
2 changes: 2 additions & 0 deletions qmail-control.9
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ control default used by
.I defaultdomain \fIme \fRqmail-inject
.I defaulthost \fIme \fRqmail-inject
.I databytes \fR0 \fRqmail-smtpd
.I smtputf8 \fR0 \fRqmail-smtpd
.I smtputf8 \fR0 \fRqmail-remote
.I doublebouncehost \fIme \fRqmail-send
.I doublebounceto \fRpostmaster \fRqmail-send
.I envnoathost \fIme \fRqmail-send
Expand Down
2 changes: 1 addition & 1 deletion qmail-remote.8
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ arguments to
The envelope sender address is listed as
.I sender\fP.

If the environment variable UTF8 is defined,
If the environment variable SMTPUTF8 is defined,
.B qmail-remote
will respect SMTPUTF8 and EAI addresses. If message is utf8,
.B qmail-remote
Expand Down
64 changes: 29 additions & 35 deletions qmail-remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
#include "hassmtputf8.h"
#ifdef SMTPUTF8
#include <idn2.h>
#endif
#include "utf8read.h"
#include "env.h"
#endif

#define HUGESMTPTEXT 5000

Expand All @@ -53,12 +53,11 @@ saa reciplist = {0};

struct ip_address partner;

#ifdef SMTPUTF8
static stralloc idnhost = { 0 };
static int smtputf8 = 0; /*- if remote has SMTPUTF8 capability */
static char *enable_utf8 = 0; /*- enable utf8 */
int flagutf8;
#endif
static int smtputf8 = 0; /*- if remote has SMTPUTF8 capability */
/* set by control file control/smtputf8. zero if SMTPUTF8 not #defined */
static int enable_smtputf8;
int flagutf8; /*- if sender, recipient headers, body have utf8 */

void out(s) char *s; { if (substdio_puts(subfdoutsmall,s) == -1) _exit(0); }
void zero() { if (substdio_put(subfdoutsmall,"\0",1) == -1) _exit(0); }
Expand Down Expand Up @@ -131,7 +130,7 @@ substdio smtpfrom = SUBSTDIO_FDBUF(saferead,-1,smtpfrombuf,sizeof(smtpfrombuf));

stralloc smtptext = {0};

static void get1(unsigned char *uc)
static void get(unsigned char *uc)
{
char *ch = (char *)uc;
substdio_get(&smtpfrom,ch,1);
Expand Down Expand Up @@ -167,13 +166,13 @@ unsigned long smtpcode()
if (!stralloc_copys(&smtptext,"")) temp_nomem();
if ((code = get3()) < 200) err = 1;
for (;;) {
get1((char *)&ch);
get((char *)&ch);
if (ch != ' ' && ch != '-') err = 1;
if (ch != '-') break;
while (ch != '\n') get1((char *)&ch);
while (ch != '\n') get((char *)&ch);
if (get3() != code) err = 1;
}
while (ch != '\n') get1((char *)&ch);
while (ch != '\n') get((char *)&ch);
return err ? 400 : code;
}

Expand Down Expand Up @@ -297,7 +296,11 @@ void smtp()
if (code >= 500) quit("DConnected to "," but my name was rejected");
if (code != 250) quit("ZConnected to "," but my name was rejected");
} else /* EHLO succeeded. Let's check SMTPUTF8 capa */
smtputf8 = get_capability("SMTPUTF8"); /*- did the remote server advertize SMTPUTF8 */
smtputf8 = get_capability("SMTPUTF8"); /*- did the remote server advertize SMTPUTF8 */
if (!flagutf8)
flagutf8 = utf8read();
if (enable_smtputf8 && flagutf8 && !smtputf8)
quit("DConnected to "," but server does not support internationalized email addresses");
#else
substdio_puts(&smtpto,"HELO ");
substdio_put(&smtpto,helohost.s,helohost.len);
Expand All @@ -308,12 +311,10 @@ void smtp()

substdio_puts(&smtpto,"MAIL FROM:<");
substdio_put(&smtpto,sender.s,sender.len);
#ifdef SMTPUTF8
if (enable_utf8 && (flagutf8 || utf8read()))
if (enable_smtputf8 && flagutf8)
substdio_puts(&smtpto,"> SMTPUTF8\r\n");
else
#endif
substdio_puts(&smtpto,">\r\n");
substdio_puts(&smtpto,">\r\n");
substdio_flush(&smtpto);
code = smtpcode();
if (code >= 500) quit("DConnected to "," but sender was rejected");
Expand Down Expand Up @@ -346,9 +347,7 @@ void smtp()
if (code >= 500) quit("D"," failed on DATA command");
if (code >= 400) quit("Z"," failed on DATA command");

#ifdef SMTPUTF8
if (enable_utf8 && header.len) substdio_put(&smtpto, header.s, header.len);
#endif
if (enable_smtputf8 && header.len) substdio_put(&smtpto, header.s, header.len);
blast();
code = smtpcode();
flagcritical = 0;
Expand All @@ -364,10 +363,8 @@ void addrmangle(stralloc *saout, char *s)
{
int j;

#ifdef SMTPUTF8
if (enable_utf8 && !flagutf8)
if (enable_smtputf8 && !flagutf8)
flagutf8 = containsutf8(s,str_len(s));
#endif

j = str_rchr(s,'@');
if (!s[j]) {
Expand All @@ -391,6 +388,9 @@ void getcontrols()
if (control_readint(&timeout,"control/timeoutremote") == -1) temp_control();
if (control_readint(&timeoutconnect,"control/timeoutconnect") == -1)
temp_control();
#ifdef SMTPUTF8
if (control_readint(&enable_smtputf8,"control/smtputf8") == -1) temp_control();
#endif
if (control_rldef(&helohost,"control/helohost",1,NULL) != 1)
temp_control();
switch(control_readfile(&routes,"control/smtproutes",0)) {
Expand All @@ -417,10 +417,6 @@ int main(int argc, char **argv)
if (chdir(auto_qmail) == -1) temp_chdir();
getcontrols();

#ifdef SMTPUTF8
enable_utf8 = env_get("UTF8");
#endif

if (!stralloc_copys(&host,argv[1])) temp_nomem();

relayhost = 0;
Expand All @@ -437,21 +433,22 @@ int main(int argc, char **argv)
relayhost[i] = 0;
}
if (!stralloc_copys(&host,relayhost)) temp_nomem();
}
#ifdef SMTPUTF8
} else
if (enable_utf8) {
char *asciihost = 0;
else
if (enable_smtputf8) {
char *asciihost = NULL;
if (!stralloc_0(&host)) temp_nomem();
switch (idn2_lookup_u8(host.s,(uint8_t**)&asciihost,IDN2_NFC_INPUT)) {
case IDN2_OK: break;
case IDN2_MALLOC: temp_nomem();
default: perm_dns();
}
if (!stralloc_copys(&idnhost,asciihost)) temp_nomem();
#endif
}
#endif


/*- addrmangle also sets flagutf8 */
addrmangle(&sender,argv[2]);

if (!saa_readyplus(&reciplist,0)) temp_nomem();
Expand All @@ -468,11 +465,8 @@ int main(int argc, char **argv)


random = now() + (getpid() << 16);
#ifdef SMTPUTF8
switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,enable_utf8 ? &idnhost : &host,random)) {
#else
switch (relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,&host,random)) {
#endif
i = relayhost ? dns_ip(&ip,&host) : dns_mxip(&ip,enable_smtputf8 ? &idnhost : &host,random);
switch (i) {
case DNS_MEM: temp_nomem();
case DNS_SOFT: temp_dns();
case DNS_HARD: perm_dns();
Expand Down
10 changes: 5 additions & 5 deletions qmail-smtpd.8
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ must be supplied several environment variables;
see
.BR tcp-environ(5) .

If the environment variable
.B UTF8
is non-empty
.B qmail-smtpd
offers RFC 5336 SMTP Email Address Internationalization support and will advertize the
capability in the EHLO greeting. Since qmail-smtpd is 8 bit clean, setting of UTF8 has no real
consequences except for displaying this setting in the received headers as \fBUTF8(E)SMTP\fR.
capability in the EHLO greeting. Since qmail-smtpd is 8 bit clean, setting of SMTPUTF8 has no real
consequences except for displaying this setting in the received headers as \fBUTF8SMTP\fR.

.B qmail-smtpd
is responsible for counting hops.
Expand Down Expand Up @@ -178,6 +175,9 @@ Number of seconds
.B qmail-smtpd
will wait for each new buffer of data from the remote SMTP client.
Default: 1200.
.TP 5
.I smtputf8
Non-zero value in this control file enables RFC6531 SMTP Extension for Internationalized Email.
.SH "SEE ALSO"
tcp-env(1),
tcp-environ(5),
Expand Down
29 changes: 8 additions & 21 deletions qmail-smtpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "timeoutread.h"
#include "timeoutwrite.h"
#include "commands.h"
#include "hassmtputf8.h"

#define MAXHOPS 100
unsigned int databytes = 0;
Expand All @@ -34,9 +33,8 @@ GEN_SAFE_TIMEOUTWRITE(safewrite,timeout,fd,_exit(1))

char ssoutbuf[512];
substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof(ssoutbuf));
#ifdef SMTPUTF8
int smtputf8 = 0, flagutf8 = 0;
#endif
int smtputf8; /*- set by mailfrom_parms */
int enable_smtputf8; /*- set by control/smtputf8 */

void flush() { substdio_flush(&ssout); }
void out(s) char *s; { substdio_puts(&ssout,s); }
Expand Down Expand Up @@ -120,6 +118,8 @@ void setup()
x = env_get("DATABYTES");
if (x) { scan_ulong(x,&u); databytes = u; }
if (!(databytes + 1)) --databytes;

if (control_readint(&enable_smtputf8, "control/smtputf8") == -1) die_control();

remoteip = env_get("TCPREMOTEIP");
if (!remoteip) remoteip = "unknown";
Expand Down Expand Up @@ -221,12 +221,11 @@ int flagbarf; /* defined if seenmail */
stralloc mailfrom = {0};
stralloc rcptto = {0};

#ifdef SMTPUTF8
stralloc mfparms = {0};
void mailfrom_parms(const char *arg)
{
int i;
int len;
size_t i;
size_t len;

len = str_len(arg);
mfparms.len=0;
Expand All @@ -242,7 +241,6 @@ void mailfrom_parms(const char *arg)
}
}
}
#endif

void smtp_helo(arg) char *arg;
{
Expand All @@ -252,12 +250,7 @@ void smtp_helo(arg) char *arg;
void smtp_ehlo(arg) char *arg;
{
smtp_greet("250-"); out("\r\n250-PIPELINING\r\n");
#ifdef SMTPUTF8
if (env_get("UTF8")) {
flagutf8 = 1;
out("250-SMTPUTF8\r\n");
}
#endif
if (enable_smtputf8) out("250-SMTPUTF8\r\n");
out("250 8BITMIME\r\n");
seenmail = 0; dohelo(arg);
}
Expand All @@ -269,9 +262,7 @@ void smtp_rset(arg) char *arg;
void smtp_mail(arg) char *arg;
{
if (!addrparse(arg)) { err_syntax(); return; }
#ifdef SMTPUTF8
if (flagutf8) mailfrom_parms(arg);
#endif
mailfrom_parms(arg);
flagbarf = bmfcheck();
seenmail = 1;
if (!stralloc_copys(&rcptto,"")) die_nomem();
Expand Down Expand Up @@ -409,11 +400,7 @@ void smtp_data(arg) char *arg; {
qp = qmail_qp(&qqt);
out("354 go ahead\r\n");

#ifdef SMTPUTF8
received(&qqt,smtputf8 ? "UTF8SMTP" : "SMTP",local,remoteip,remotehost,remoteinfo,fakehelo);
#else
received(&qqt,"SMTP",local,remoteip,remotehost,remoteinfo,fakehelo);
#endif
blast(&hops);
hops = (hops >= MAXHOPS);
if (hops) qmail_fail(&qqt);
Expand Down
Loading

0 comments on commit 44a00c9

Please sign in to comment.