Newer
Older
lprintf("%04d !POP3 UNSUPPORTED COMMAND from %s: '%s'"
,socket, user.alias, buf);
sockprintf(socket,"-ERR UNSUPPORTED COMMAND: %s",buf);
}
if(user.number)

rswindell
committed
logoutuserdat(&scfg,&user,time(NULL),client.time);
if(activity)
lprintf("%04d POP3 %s logged out from port %u on %s [%s]"
,socket, user.alias, ntohs(pop3.client_addr.sin_port), host_name, host_ip);
status(STATUS_WFC);
/* Free up resources here */

rswindell
committed
mail_close_socket(socket);
if(mail!=NULL)
freemail(mail);
smb_freemsgmem(&msg);
smb_close(&smb);
if(startup->options&MAIL_OPT_DEBUG_POP3)
lprintf("%04d POP3 session thread terminated", socket);
active_clients--;
update_clients();
client_off(socket);
thread_down();
}
BOOL rblchk(DWORD mail_addr_n, char* rbl_addr)
{
char name[256];
DWORD mail_addr;
mail_addr=ntohl(mail_addr_n);
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
,mail_addr&0xff
,(mail_addr>>8)&0xff
,(mail_addr>>16)&0xff
,(mail_addr>>24)&0xff
,rbl_addr
);
if(gethostbyname(name)==NULL)
return(FALSE);
return(TRUE);
}
static void smtp_thread(void* arg)
{
int i,j,x;
int rd;
char str[512];
char buf[1024],*p,*tp;
char hdrfield[512];
char alias_buf[128];
char reverse_path[128];
char date[64];
char month[16];
char rcpt_name[128];
char rcpt_addr[128];

rswindell
committed
char sender[128];
char sender_addr[128];
char relay_list[MAX_PATH+1];
char domain_list[MAX_PATH+1];
char host_name[128];
char host_ip[64];

rswindell
committed
char* telegram_buf;
int addr_len;
ushort xlat;
ushort nettype=NET_NONE;
uint usernum;
ulong crc;
ulong length;
ulong offset;
BOOL esmtp=FALSE;

rswindell
committed
BOOL telegram=FALSE;
char msgtxt_fname[MAX_PATH+1];
char rcptlst_fname[MAX_PATH+1];
FILE* spy=NULL;
SOCKET socket;
HOSTENT* host;
smb_t smb;
smbmsg_t msg;
smbmsg_t newmsg;
user_t user;

rswindell
committed
node_t node;
client_t client;
struct tm tm;
smtp_t smtp=*(smtp_t*)arg;
SOCKADDR_IN server_addr;
enum {
SMTP_STATE_INITIAL
,SMTP_STATE_HELO

rswindell
committed
,SMTP_STATE_MAIL
,SMTP_STATE_SEND
,SMTP_STATE_SOML
,SMTP_STATE_SAML
,SMTP_STATE_RCPT
,SMTP_STATE_DATA_HEADER
,SMTP_STATE_DATA_BODY
} state = SMTP_STATE_INITIAL;
thread_up();
free(arg);
socket=smtp.socket;
lprintf("%04d SMTP RX Session thread started", socket);
if(startup->inbound_sound[0] && !(startup->options&MAIL_OPT_MUTE))
PlaySound(startup->inbound_sound, NULL, SND_ASYNC|SND_FILENAME);
addr_len=sizeof(server_addr);
if((i=getsockname(socket, (struct sockaddr *)&server_addr,&addr_len))!=0) {

rswindell
committed
lprintf("%04d !SMTP ERROR %d (%d) getting address/port"
sockprintf(socket,"421 System error");
mail_close_socket(socket);
thread_down();

rswindell
committed
memset(&msg,0,sizeof(msg));
memset(&user,0,sizeof(user));
strcpy(host_ip,inet_ntoa(smtp.client_addr.sin_addr));
lprintf("%04d SMTP connection accepted from: %s port %u"
, socket, host_ip, ntohs(smtp.client_addr.sin_port));
if(startup->options&MAIL_OPT_NO_HOST_LOOKUP)
host=NULL;
else
host=gethostbyaddr ((char *)&smtp.client_addr.sin_addr
,sizeof(smtp.client_addr.sin_addr),AF_INET);
if(host!=NULL && host->h_name!=NULL)
sprintf(host_name,"%.*s",(int)sizeof(host_name)-1,host->h_name);
else
strcpy(host_name,"<no name>");
lprintf("%04d SMTP host name: %s", socket, host_name);
strcpy(hello_name,host_name);
if(trashcan(&scfg,host_ip,"ip")) {
lprintf("%04d !SMTP SERVER BLOCKED in ip.can: %s"
,socket, host_ip);
sockprintf(socket,"550 Access denied.");

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
if(trashcan(&scfg,host_name,"host")) {
lprintf("%04d !SMTP SERVER BLOCKED in host.can: %s"
,socket, host_name);
sockprintf(socket,"550 Access denied.");

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
/* SPAM Filters (mail-abuse.org) */
if(startup->options&MAIL_OPT_USE_RBL
&& rblchk(smtp.client_addr.sin_addr.s_addr
,"blackholes.mail-abuse.org"

rswindell
committed
lprintf("%04d !SMTP SPAM server filtered (RBL): %s [%s]"
,socket, host_name, host_ip);
spamlog(&scfg, "Listed on RBL (http://mail-abuse.org/rbl)"
,host_name, host_ip);
,"571 Mail from %s refused, see http://mail-abuse.org/rbl"

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
if(startup->options&MAIL_OPT_USE_DUL
&& rblchk(smtp.client_addr.sin_addr.s_addr
,"dialups.mail-abuse.org"

rswindell
committed
lprintf("%04d !SMTP SPAM server filtered (DUL): %s [%s]"
,socket, host_name, host_ip);
spamlog(&scfg, "Listed on DUL (http://mail-abuse.org/dul)"
,host_name, host_ip);
,"571 Mail from %s refused, see http://mail-abuse.org/dul"

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
if(startup->options&MAIL_OPT_USE_RSS
&& rblchk(smtp.client_addr.sin_addr.s_addr
,"relays.mail-abuse.org"
)==TRUE) {

rswindell
committed
lprintf("%04d !SMTP SPAM server filtered (RSS): %s [%s]"
,socket, host_name, host_ip);
spamlog(&scfg, "Listed on RSS (http://mail-abuse.org/rss)"
,host_name, host_ip);
,"571 Mail from %s refused, see http://mail-abuse.org/rss"

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
sprintf(rcptlst_fname,"%sSMTP%d.LST", scfg.data_dir, socket);
rcptlst=fopen(rcptlst_fname,"w+");
if(rcptlst==NULL) {

rswindell
committed
lprintf("%04d !SMTP ERROR %d creating recipient list: %s"
,socket, errno, rcptlst_fname);
sockprintf(socket,"421 System error");

rswindell
committed
mail_close_socket(socket);
thread_down();
return;
}
if(trashcan(&scfg,host_name,"smtpspy")
|| trashcan(&scfg,host_ip,"smtpspy")) {
sprintf(str,"%sSMTPSPY.TXT", scfg.data_dir);
spy=fopen(str,"a");
}
active_clients++;
update_clients();
/* Initialize client display */
client.size=sizeof(client);
client.time=time(NULL);
sprintf(client.addr,"%.*s",(int)sizeof(client.addr)-1,host_ip);
sprintf(client.host,"%.*s",(int)sizeof(client.host)-1,host_name);
client.port=ntohs(smtp.client_addr.sin_port);
client.protocol="SMTP";
client.user="<unknown>";
client_on(socket,&client);
sprintf(str,"SMTP: %s",host_ip);
status(str);
sockprintf(socket,"220 %s Synchronet SMTP Server for %s v%s Ready"
,scfg.sys_inetaddr,PLATFORM_DESC,MAIL_VERSION);
while(1) {
rd = sockreadline(socket, buf, sizeof(buf));
if(rd<1)
break;
truncsp(buf);
if(spy!=NULL)
fprintf(spy,"%s\n",buf);
if(state>=SMTP_STATE_DATA_HEADER) {
if(!strcmp(buf,".")) {

rswindell
committed
state=SMTP_STATE_HELO; /* RESET state machine here in case of error */
if(msgtxt==NULL) {
lprintf("%04d !SMTP NO MESSAGE TEXT FILE POINTER?", socket);
sockprintf(socket,SMTP_OK);

rswindell
committed
lprintf("%04d SMTP End of message (%lu bytes)", socket, ftell(msgtxt));
if(telegram==TRUE) {

rswindell
committed
length=filelength(fileno(msgtxt));
p=strchr(sender_addr,'@');
if(p==NULL || resolve_ip(p+1)!=smtp.client_addr.sin_addr.s_addr)
/* Append real IP and hostname if different */
sprintf(str,"\1n\1h%s [\1n%s\1h] (\1n%s\1h)\1n: "
,sender_addr,host_ip,host_name);
else
sprintf(str,"\1n\1h%s\1n: ",sender_addr);

rswindell
committed
if((telegram_buf=(char*)malloc(length+strlen(str)+1))==NULL) {
lprintf("%04d !SMTP ERROR allocating %lu bytes of memory for telegram from %s"
,socket,length+strlen(str)+1,sender_addr);
sockprintf(socket, "452 Insufficient system storage");

rswindell
committed
continue;

rswindell
committed
strcpy(telegram_buf,str);
if(fread(telegram_buf+strlen(str),1,length,msgtxt)!=length) {
lprintf("%04d !SMTP ERROR reading %lu bytes from telegram file"
,socket,length);
sockprintf(socket, "452 Insufficient system storage");
free(telegram_buf);
continue;
}
telegram_buf[length+strlen(str)]=0; /* Need ASCIIZ */

rswindell
committed
/* Send telegram to users */
rewind(rcptlst);
while(!feof(rcptlst)) {
if(fgets(str,sizeof(str)-1,rcptlst)==NULL)
break;
usernum=atoi(str);
if(fgets(rcpt_name,sizeof(rcpt_name)-1,rcptlst)==NULL)
break;

rswindell
committed
truncsp(rcpt_name);
if(fgets(rcpt_addr,sizeof(rcpt_addr)-1,rcptlst)==NULL)
break;

rswindell
committed
truncsp(rcpt_addr);
putsmsg(&scfg,usernum,telegram_buf);
lprintf("%04d SMTP Created telegram from %s to %s <%s>"
,socket, sender_addr, rcpt_name, rcpt_addr);

rswindell
committed
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
free(telegram_buf);
sockprintf(socket,SMTP_OK);
telegram=FALSE;
continue;
}
telegram=FALSE;
if(sender[0]==0) {
lprintf("%04d !SMTP MISSING mail header 'FROM' field", socket);
sockprintf(socket, "554 Mail header missing 'FROM' field");
continue;
}
nettype=NET_INTERNET;
smb_hfield(&msg, SENDER, (ushort)strlen(sender), sender);
smb_hfield(&msg, SENDERNETTYPE, sizeof(nettype), &nettype);
smb_hfield(&msg, SENDERNETADDR, (ushort)strlen(sender_addr), sender_addr);
rewind(msgtxt);
sprintf(smb.file,"%smail", scfg.data_dir);
smb.retry_time=scfg.smb_retry_time;
if((i=smb_open(&smb))!=0) {
lprintf("%04d !SMTP ERROR %d (%s) opening %s"
,socket, i, smb.last_error, smb.file);
sockprintf(socket, "452 Insufficient system storage");
continue;
}

rswindell
committed
if(smb_fgetlength(smb.shd_fp)<1) { /* Create it if it doesn't exist */
smb.status.max_crcs=scfg.mail_maxcrcs;
smb.status.max_age=scfg.mail_maxage;
smb.status.max_msgs=MAX_SYSMAIL;
smb.status.attr=SMB_EMAIL;
if((i=smb_create(&smb))!=0) {

rswindell
committed
lprintf("%04d !SMTP ERROR %d creating %s"
,socket, i, smb.file);
sockprintf(socket, "452 Insufficient system storage");

rswindell
committed
continue;
}
}

rswindell
committed
if((i=smb_locksmbhdr(&smb))!=0) {
smb_close(&smb);
lprintf("%04d !SMTP ERROR %d locking %s"
,socket, i, smb.file);
sockprintf(socket, "452 Insufficient system storage");
continue;
}

rswindell
committed
length=filelength(fileno(msgtxt))+2; /* +2 for translation string */
if((i=smb_open_da(&smb))!=0) {
smb_unlocksmbhdr(&smb);
smb_close(&smb);
lprintf("%04d !SMTP ERROR %d (%s) opening %s.sda"
,socket, i, smb.last_error, smb.file);
sockprintf(socket, "452 Insufficient system storage");
continue;
}

rswindell
committed
if(scfg.sys_misc&SM_FASTMAIL)
offset=smb_fallocdat(&smb,length,1);
else
offset=smb_allocdat(&smb,length,1);
smb_close_da(&smb);
smb_fseek(smb.sdt_fp,offset,SEEK_SET);
xlat=XLAT_NONE;
smb_fwrite(&xlat,2,smb.sdt_fp);
x=SDT_BLOCK_LEN-2; /* Don't read/write more than 255 */
while(!feof(msgtxt)) {
memset(buf,0,x);
j=fread(buf,1,x,msgtxt);
if(j<1)
break;
if((j!=x || feof(msgtxt)) && buf[j-1]=='\n' && buf[j-2]=='\r')
buf[j-1]=buf[j-2]=0;

rswindell
committed
for(i=0;i<j;i++)
crc=ucrc32(buf[i],crc);

rswindell
committed
smb_fwrite(buf,j,smb.sdt_fp);
x=SDT_BLOCK_LEN;
}
smb_fflush(smb.sdt_fp);
crc=~crc;

rswindell
committed
if(scfg.mail_maxcrcs) {
i=smb_addcrc(&smb,crc);
if(i) {
smb_freemsgdat(&smb,offset,length,1);
smb_unlocksmbhdr(&smb);
smb_close(&smb);
lprintf("%04d !SMTP DUPLICATE MESSAGE", socket);
sockprintf(socket, "554 Duplicate Message");
continue;
}
}

rswindell
committed
msg.hdr.offset=offset;

rswindell
committed
smb_dfield(&msg,TEXT_BODY,length);

rswindell
committed
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
smb_unlocksmbhdr(&smb);
if(msg.idx.subj==0) {
p="";
smb_hfield(&msg, SUBJECT, 0, p);
msg.idx.subj=crc16(p);
}
rewind(rcptlst);
while(!feof(rcptlst)) {
if((i=smb_copymsgmem(&newmsg,&msg))!=0) {
lprintf("%04d !SMTP ERROR %d copying message"
,socket, i);
break;
}
if(fgets(str,sizeof(str)-1,rcptlst)==NULL)
break;
usernum=atoi(str);
fgets(rcpt_name,sizeof(rcpt_name)-1,rcptlst);
if(fgets(rcpt_addr,sizeof(rcpt_addr)-1,rcptlst)==NULL)
break;
truncsp(rcpt_name);
truncsp(rcpt_addr);
snprintf(hdrfield,sizeof(hdrfield),
"Received: from %s (%s [%s])\r\n"
" by %s [%s] (Synchronet Mail Server for %s v%s) with %s\r\n"
" for %s; %s"
,host_name,hello_name,host_ip
,scfg.sys_inetaddr,inet_ntoa(server_addr.sin_addr)
,PLATFORM_DESC,MAIL_VERSION
,esmtp ? "ESMTP" : "SMTP"
,rcpt_addr,msgdate(msg.hdr.when_imported,date));
smb_hfield(&newmsg, RFC822HEADER, (ushort)strlen(hdrfield), hdrfield);
smb_hfield(&newmsg, RECIPIENT, (ushort)strlen(rcpt_name), rcpt_name);
if(rcpt_addr[0]=='#') { /* Local destination */
newmsg.idx.to=atoi(rcpt_addr+1);
smb_hfield(&newmsg, RECIPIENTEXT
,(ushort)strlen(rcpt_addr+1), rcpt_addr+1);
} else {
newmsg.idx.to=0;
nettype=NET_INTERNET;
smb_hfield(&newmsg, RECIPIENTNETTYPE, nettype, &nettype);
smb_hfield(&newmsg, RECIPIENTNETADDR
,(ushort)strlen(rcpt_addr), rcpt_addr);
}

rswindell
committed
i=smb_addmsghdr(&smb,&newmsg,SMB_SELFPACK);
smb_freemsgmem(&newmsg);
if(i!=0) {
lprintf("%04d !SMTP ERROR %d adding message header"
,socket, i);
break;

rswindell
committed
lprintf("%04d SMTP Created message #%ld from %s to %s <%s>"
,socket, newmsg.hdr.number, sender, rcpt_name, rcpt_addr);
if(usernum) {
sprintf(str,"\7\1n\1hOn %.24s\r\n\1m%s \1n\1msent you e-mail from: "
"\1h%s\1n\r\n"
,ctime((const time_t*)&newmsg.hdr.when_imported.time)
,sender,sender_addr);
if(!newmsg.idx.to) { /* Forwarding */
strcat(str,"\1mand it was automatically forwaded to: \1h");
strcat(str,user.netmail);
strcat(str,"\1n\r\n");
}
putsmsg(&scfg, usernum, str);

rswindell
committed
smb_close(&smb);
if(i) {
smb_freemsgdat(&smb,offset,length,1);
sockprintf(socket, "452 Insufficient system storage");
continue;
}
sockprintf(socket,SMTP_OK);
continue;
}
if(buf[0]==0 && state==SMTP_STATE_DATA_HEADER) {
state=SMTP_STATE_DATA_BODY; /* Null line separates header and body */
continue;
}
if(state==SMTP_STATE_DATA_BODY) {
p=buf;
if(*p=='.') p++; /* Transparency (RFC821 4.5.2) */
if(msgtxt!=NULL)
fprintf(msgtxt, "%s\r\n", p);
continue;
}
/* RFC822 Header parsing */
if(startup->options&MAIL_OPT_DEBUG_RX_HEADER)
lprintf("%04d SMTP %s",socket, buf);
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
if(!strnicmp(buf, "SUBJECT:",8)) {
p=buf+8;
while(*p && *p<=' ') p++;
smb_hfield(&msg, SUBJECT, (ushort)strlen(p), p);
strlwr(p);
msg.idx.subj=crc16(p);
continue;
}
if(!strnicmp(buf, "TO:",3)) {
p=buf+3;
while(*p && *p<=' ') p++;
if(*p=='<') {
p++;
tp=strrchr(p,'>');
if(tp) *tp=0;
}
continue;
}
if(!strnicmp(buf, "REPLY-TO:",9)) {
p=buf+9;
while(*p && *p<=' ') p++;
if(*p=='<') {
p++;
tp=strchr(p,'>');
if(tp) *tp=0;
}
nettype=NET_INTERNET;
smb_hfield(&msg, REPLYTONETTYPE, nettype, &nettype);
smb_hfield(&msg, REPLYTONETADDR, (ushort)strlen(p), p);
continue;
}
if(!strnicmp(buf, "FROM:",5)) {
p=strchr(buf+5,'<');
if(p) {
p++;
tp=strchr(p,'>');
if(tp) *tp=0;
sprintf(sender_addr,"%.*s",(int)sizeof(sender_addr)-1,p);
} else {
p=buf+5;
while(*p && *p<=' ') p++;
sprintf(sender_addr,"%.*s",(int)sizeof(sender_addr)-1,p);
}
p=buf+5;
while(*p && *p<=' ') p++;
if(*p=='"') {
p++;
tp=strchr(p,'"');
} else if(*p=='<') {
p++;
tp=strchr(p,'>');
} else
tp=strchr(p,'<');
if(tp) *tp=0;
truncsp(p);
sprintf(sender,"%.*s",(int)sizeof(sender)-1,p);
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
continue;
}
if(!strnicmp(buf, "DATE:",5)) {
p=buf+5;
while(*p && *p<=' ') p++;
/* lprintf("Sent: %s",p); */
memset(&tm,0,sizeof(tm));
while(*p && !isdigit(*p)) p++;
/* DAY */
tm.tm_mday=atoi(p);
while(*p && isdigit(*p)) p++;
/* MONTH */
while(*p && *p<=' ') p++;
sprintf(month,"%3.3s",p);
if(!stricmp(month,"jan"))
tm.tm_mon=0;
else if(!stricmp(month,"feb"))
tm.tm_mon=1;
else if(!stricmp(month,"mar"))
tm.tm_mon=2;
else if(!stricmp(month,"apr"))
tm.tm_mon=3;
else if(!stricmp(month,"may"))
tm.tm_mon=4;
else if(!stricmp(month,"jun"))
tm.tm_mon=5;
else if(!stricmp(month,"jul"))
tm.tm_mon=6;
else if(!stricmp(month,"aug"))
tm.tm_mon=7;
else if(!stricmp(month,"sep"))
tm.tm_mon=8;
else if(!stricmp(month,"oct"))
tm.tm_mon=9;
else if(!stricmp(month,"nov"))
tm.tm_mon=10;
else
tm.tm_mon=11;
p+=4;
/* YEAR */
tm.tm_year=atoi(p);
if(tm.tm_year>1900)
tm.tm_year-=1900;
while(*p && isdigit(*p)) p++;
/* HOUR */
while(*p && *p<=' ') p++;
tm.tm_hour=atoi(p);
while(*p && isdigit(*p)) p++;
/* MINUTE */
if(*p) p++;
tm.tm_min=atoi(p);
while(*p && isdigit(*p)) p++;
/* SECONDS */
if(*p) p++;
tm.tm_sec=atoi(p);
while(*p && isdigit(*p)) p++;
/* TIME ZONE */
while(*p && *p<=' ') p++;
if(*p && (isdigit(*p) || *p=='-')) { /* HHMM or -HHMM format */
sprintf(str,"%.*s",*p=='-'? 3:2,p);
msg.hdr.when_written.zone=atoi(str)*60;
p+=(*p=='-') ? 3:2;
msg.hdr.when_written.zone+=atoi(p);
}
else if(!strnicmp(p,"PDT",3))
msg.hdr.when_written.zone=(short)PDT;
else if(!strnicmp(p,"MDT",3))
msg.hdr.when_written.zone=(short)MDT;
else if(!strnicmp(p,"CDT",3))
msg.hdr.when_written.zone=(short)CDT;
else if(!strnicmp(p,"EDT",3))
msg.hdr.when_written.zone=(short)EDT;
else if(!strnicmp(p,"PST",3))
msg.hdr.when_written.zone=(short)PST;
else if(!strnicmp(p,"MST",3))
msg.hdr.when_written.zone=(short)MST;
else if(!strnicmp(p,"CST",3))
msg.hdr.when_written.zone=(short)CST;
else if(!strnicmp(p,"EST",3))
msg.hdr.when_written.zone=(short)EST;
msg.hdr.when_written.time=mktime(&tm);
continue;
}
if(!strnicmp(buf, "MESSAGE-ID:",11)) {
p=buf+11;
while(*p && *p<=' ') p++;
smb_hfield(&msg, RFC822MSGID, (ushort)strlen(p), p);
continue;
}
if(!strnicmp(buf, "IN-REPLY-TO:",12)) {
p=buf+12;
while(*p && *p<=' ') p++;
smb_hfield(&msg, RFC822REPLYID, (ushort)strlen(p), p);
continue;
}
/* Fall-through */
smb_hfield(&msg, RFC822HEADER, (ushort)strlen(buf), buf);
continue;
}
lprintf("%04d SMTP RX: %s", socket, buf);
if(!strnicmp(buf,"HELO",4)) {
p=buf+4;
while(*p && *p<=' ') p++;
sprintf(hello_name,"%.*s",(int)sizeof(hello_name)-1,p);
sockprintf(socket,"250 %s",scfg.sys_inetaddr);
esmtp=FALSE;
state=SMTP_STATE_HELO;

rswindell
committed
telegram=FALSE;
continue;
}
if(!strnicmp(buf,"EHLO",4)) {
p=buf+4;
while(*p && *p<=' ') p++;
sprintf(hello_name,"%.*s",(int)sizeof(hello_name)-1,p);
sockprintf(socket,"250 %s",scfg.sys_inetaddr);
esmtp=TRUE;
state=SMTP_STATE_HELO;

rswindell
committed
telegram=FALSE;
continue;
}
if(!stricmp(buf,"QUIT")) {
sockprintf(socket,"221 %s Service closing transmission channel",scfg.sys_inetaddr);
break;
}
if(!stricmp(buf,"NOOP")) {
sockprintf(socket, SMTP_OK);
continue;
}
if(state<SMTP_STATE_HELO) {
/* RFC 821 4.1.1 "The first command in a session must be the HELO command." */
lprintf("%04d !SMTP MISSING 'HELO' command",socket);
sockprintf(socket, SMTP_BADSEQ);
continue;
}
if(!stricmp(buf,"TURN")) {
sockprintf(socket,"502 command not supported");
continue;
}
if(!stricmp(buf,"RSET")) {
smb_freemsgmem(&msg);
memset(&msg,0,sizeof(smbmsg_t)); /* Initialize message header */
reverse_path[0]=0;
sockprintf(socket,SMTP_OK);
rewind(rcptlst);
chsize(fileno(rcptlst),0);
state=SMTP_STATE_HELO;

rswindell
committed
telegram=FALSE;
continue;
}
if(!strnicmp(buf,"MAIL FROM:",10)) {
smb_freemsgmem(&msg);
memset(&msg,0,sizeof(smbmsg_t)); /* Initialize message header */
memcpy(msg.hdr.id,"SHD\x1a",4);
msg.hdr.version=smb_ver();
msg.hdr.when_imported.time=time(NULL);
msg.hdr.when_imported.zone=scfg.sys_timezone;
p=buf+10;
while(*p && *p<=' ') p++;
sprintf(reverse_path,"%.*s",(int)sizeof(reverse_path)-1,p);

rswindell
committed
state=SMTP_STATE_MAIL;

rswindell
committed
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
/* Send a Message (Telegram) to a local ONLINE user */
if(!strnicmp(buf,"SEND FROM:",10)) {
p=buf+10;
while(*p && *p<=' ') p++;
sprintf(reverse_path,"%.*s",(int)sizeof(reverse_path)-1,p);
sockprintf(socket,SMTP_OK);
state=SMTP_STATE_SEND;
continue;
}
/* Send OR Mail a Message to a local user */
if(!strnicmp(buf,"SOML FROM:",10)) {
p=buf+10;
while(*p && *p<=' ') p++;
sprintf(reverse_path,"%.*s",(int)sizeof(reverse_path)-1,p);
sockprintf(socket,SMTP_OK);
state=SMTP_STATE_SOML;
continue;
}
/* Send AND Mail a Message to a local user */
if(!strnicmp(buf,"SAML FROM:",10)) {
p=buf+10;
while(*p && *p<=' ') p++;
sprintf(reverse_path,"%.*s",(int)sizeof(reverse_path)-1,p);
sockprintf(socket,SMTP_OK);
state=SMTP_STATE_SAML;
continue;
}
#if 0 /* No one uses this command */
if(!strnicmp(buf,"VRFY",4)) {
p=buf+4;
while(*p && *p<=' ') p++;
if(*p==0) {
sockprintf(socket,"550 No user specified.");
continue;
}
#endif
if(!strnicmp(buf,"RCPT TO:",8)) {

rswindell
committed
if(state<SMTP_STATE_MAIL) {
lprintf("%04d !SMTP MISSING 'MAIL' command",socket);
sockprintf(socket, SMTP_BADSEQ);
continue;
}

rswindell
committed
if(spy==NULL && trashcan(&scfg,reverse_path,"smtpspy")) {
sprintf(str,"%sSMTPSPY.TXT", scfg.data_dir);
spy=fopen(str,"a");
}
sprintf(str,"%.*s",(int)sizeof(str)-1,buf+8);
p=str;
while(*p && *p<=' ') p++;
if(*p=='<') p++; /* Skip '<' */
tp=strchr(str,'>'); /* Truncate '>' */
if(tp!=NULL) *tp=0;
tp=strrchr(str,'@');

rswindell
committed
if(state==SMTP_STATE_MAIL && tp!=NULL) {
sprintf(domain_list,"%sdomains.cfg",scfg.ctrl_dir);
sprintf(relay_list,"%srelay.cfg",scfg.ctrl_dir);
if(stricmp(tp+1,scfg.sys_inetaddr)!=0
&& resolve_ip(tp+1)!=server_addr.sin_addr.s_addr
&& findstr(&scfg,tp+1,domain_list)==FALSE) {
if(!findstr(&scfg,host_name,relay_list) &&
!findstr(&scfg,host_ip,relay_list)) {
lprintf("%04d !SMTP ILLEGAL RELAY ATTEMPT from %s [%s] to %s"
,socket, host_name, host_ip, tp+1);
sockprintf(socket, "550 Relay not allowed.");
continue;
}
lprintf("%04d SMTP Relaying to external mail service: %s"
,socket, tp+1);
fprintf(rcptlst,"0\n%.*s\n%.*s\n"
,(int)sizeof(rcpt_name)-1,p
,(int)sizeof(rcpt_addr)-1,p);
sockprintf(socket,SMTP_OK);

rswindell
committed
state=SMTP_STATE_RCPT;
*tp=0; /* truncate at '@' */
while(*p && !isalnum(*p)) p++; /* Skip '<' or '"' */
tp=strrchr(p,'"');
if(tp!=NULL) *tp=0; /* truncate at '"' */
p=alias(p,alias_buf);
/* RX by sysop alias */
if(!stricmp(p,"SYSOP") || !stricmp(p,scfg.sys_id)
|| !stricmp(p,"POSTMASTER") || !stricmp(p,scfg.sys_op))
usernum=1;
else if(startup->options&MAIL_OPT_ALLOW_RX_BY_NUMBER
&& isdigit(*p)) {
usernum=atoi(p); /* RX by user number */
/* verify usernum */
username(&scfg,usernum,str);
if(!str[0] || !stricmp(str,"DELETED USER"))
usernum=0;
p=str;
} else {
usernum=matchuser(&scfg,p); /* RX by "user alias", "user.alias" or "user_alias" */
if(!usernum) { /* RX by "real name", "real.name", or "sysop.alias" */
/* convert "user.name" to "user name" */
sprintf(rcpt_name,"%.*s",sizeof(rcpt_name)-1,p);
for(tp=rcpt_name;*tp;tp++)
if(*tp=='.') *tp=' ';
if(!stricmp(rcpt_name,scfg.sys_op))
usernum=1; /* RX by "sysop.alias" */
if(!usernum) /* RX by "real name" */
usernum=userdatdupe(&scfg, 0, U_NAME, LEN_NAME, p, FALSE);
if(!usernum) /* RX by "real.name" */
usernum=userdatdupe(&scfg, 0, U_NAME, LEN_NAME, rcpt_name, FALSE);
lprintf("%04d !SMTP UNKNOWN USER:%s", socket, buf+8);
sockprintf(socket, "550 Unknown User:%s", buf+8);
continue;
}
user.number=usernum;
if((i=getuserdat(&scfg, &user))!=0) {

rswindell
committed
lprintf("%04d !SMTP ERROR %d getting data on user #%u (%s)"
sockprintf(socket, "550 Unknown User:%s", buf+8);
continue;
}
if(user.misc&(DELETED|INACTIVE)) {

rswindell
committed
lprintf("%04d !SMTP DELETED or INACTIVE user #%u (%s)"
sockprintf(socket, "550 Unknown User:%s", buf+8);

rswindell
committed
if(state==SMTP_STATE_SEND) { /* Check if user online */
for(i=0;i<scfg.sys_nodes;i++) {
getnodedat(&scfg, i+1, &node, 0);
if(node.status==NODE_INUSE && node.useron==user.number
&& !(node.misc&NODE_POFF))
break;
}
if(i>=scfg.sys_nodes) {
lprintf("%04d !Attempt to send telegram to unavailable user #%u (%s)"
,socket, user.number, user.alias);
sockprintf(socket,"450 User unavailable");
continue;
}
}
if(state==SMTP_STATE_MAIL) {

rswindell
committed
if(useron.etoday>=cfg.level_emailperday[useron.level]
&& !(useron.rest&FLAG('Q'))) {
bputs(text[TooManyEmailsToday]);
continue;
}

rswindell
committed
} else
telegram=TRUE;
fprintf(rcptlst,"%u\n%.*s\n",user.number,(int)sizeof(rcpt_name)-1,p);
/* Forward to Internet */
tp=strrchr(user.netmail,'@');

rswindell
committed
if(!telegram
&& scfg.sys_misc&SM_FWDTONET
&& user.misc&NETMAIL
&& tp && strchr(tp,'.') && !strchr(tp,'/')
&& !strstr(tp,scfg.sys_inetaddr)) {
lprintf("%04d SMTP Forwarding to: %s"
,socket, user.netmail);
fprintf(rcptlst,"%s\n",user.netmail);
sockprintf(socket,"251 User not local; will forward to %s", user.netmail);
} else { /* Local (no-forward) */
fprintf(rcptlst,"#%u\n",usernum);
sockprintf(socket,SMTP_OK);
}

rswindell
committed
state=SMTP_STATE_RCPT;
continue;
}
if(!strnicmp(buf,"DATA",4)) {

rswindell
committed
if(state<SMTP_STATE_RCPT) {
lprintf("%04d !SMTP MISSING 'RCPT TO' command", socket);
sockprintf(socket, SMTP_BADSEQ);
continue;
}
if(msgtxt!=NULL)
fclose(msgtxt);
sprintf(msgtxt_fname,"%sSMTP%d.RX", scfg.data_dir, socket);
if((msgtxt=fopen(msgtxt_fname,"w+b"))==NULL) {
,socket, errno, msgtxt_fname);
sockprintf(socket, "452 Insufficient system storage");
continue;
}

rswindell
committed
/* These vars are potentially over-written by parsing an RFC822 header */
/* get sender_addr */
p=strchr(reverse_path,'<');
if(p==NULL)
p=reverse_path;
else
p++;
strcpy(sender_addr,p);
p=strchr(sender_addr,'>');
if(p!=NULL)
*p=0;
/* get sender */
strcpy(sender,sender_addr);
p=strchr(sender,'@');
if(p!=NULL)
*p=0;
else
sender[0]=0;
sockprintf(socket, "354 send the mail data, end with <CRLF>.<CRLF>");

rswindell
committed
if(telegram)
state=SMTP_STATE_DATA_BODY; /* No RFC headers in Telegrams */
else
state=SMTP_STATE_DATA_HEADER;
continue;
}
sockprintf(socket,"500 Syntax error");
lprintf("%04d !SMTP UNSUPPORTED COMMAND: '%s'", socket, buf);
}
/* Free up resources here */

rswindell
committed
mail_close_socket(socket);
smb_freemsgmem(&msg);
if(msgtxt!=NULL) {
fclose(msgtxt);
if(!(startup->options&MAIL_OPT_DEBUG_RX_BODY))
unlink(msgtxt_fname);
}
if(rcptlst!=NULL) {
fclose(rcptlst);
unlink(rcptlst_fname);
}
if(spy!=NULL)
fclose(spy);
status(STATUS_WFC);
lprintf("%04d SMTP RX Session thread terminated", socket);