122712Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362532Sbostic * Copyright (c) 1988, 1993 462532Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642829Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822712Sdist 933731Sbostic # include "sendmail.h" 1022712Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef SMTP 13*68583Seric static char sccsid[] = "@(#)srvrsmtp.c 8.60 (Berkeley) 03/22/95 (with SMTP)"; 1433731Sbostic #else 15*68583Seric static char sccsid[] = "@(#)srvrsmtp.c 8.60 (Berkeley) 03/22/95 (without SMTP)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 199339Seric # include <errno.h> 204549Seric 2133731Sbostic # ifdef SMTP 224556Seric 234549Seric /* 244549Seric ** SMTP -- run the SMTP protocol. 254549Seric ** 264549Seric ** Parameters: 274549Seric ** none. 284549Seric ** 294549Seric ** Returns: 304549Seric ** never. 314549Seric ** 324549Seric ** Side Effects: 334549Seric ** Reads commands from the input channel and processes 344549Seric ** them. 354549Seric */ 364549Seric 374549Seric struct cmd 384549Seric { 394549Seric char *cmdname; /* command name */ 404549Seric int cmdcode; /* internal code, see below */ 414549Seric }; 424549Seric 434549Seric /* values for cmdcode */ 444549Seric # define CMDERROR 0 /* bad command */ 454549Seric # define CMDMAIL 1 /* mail -- designate sender */ 464976Seric # define CMDRCPT 2 /* rcpt -- designate recipient */ 474549Seric # define CMDDATA 3 /* data -- send message text */ 489339Seric # define CMDRSET 4 /* rset -- reset state */ 499339Seric # define CMDVRFY 5 /* vrfy -- verify address */ 5058092Seric # define CMDEXPN 6 /* expn -- expand address */ 519339Seric # define CMDNOOP 7 /* noop -- do nothing */ 529339Seric # define CMDQUIT 8 /* quit -- close connection and die */ 539339Seric # define CMDHELO 9 /* helo -- be polite */ 5458092Seric # define CMDHELP 10 /* help -- give usage info */ 5558323Seric # define CMDEHLO 11 /* ehlo -- extended helo (RFC 1425) */ 5658092Seric /* non-standard commands */ 5758092Seric # define CMDONEX 16 /* onex -- sending one transaction only */ 5858092Seric # define CMDVERB 17 /* verb -- go into verbose mode */ 5964685Seric /* use this to catch and log "door handle" attempts on your system */ 6064685Seric # define CMDLOGBOGUS 23 /* bogus command that should be logged */ 6136230Skarels /* debugging-only commands, only enabled if SMTPDEBUG is defined */ 6258092Seric # define CMDDBGQSHOW 24 /* showq -- show send queue */ 6358092Seric # define CMDDBGDEBUG 25 /* debug -- set debug mode */ 644549Seric 654549Seric static struct cmd CmdTab[] = 664549Seric { 674549Seric "mail", CMDMAIL, 684976Seric "rcpt", CMDRCPT, 694549Seric "data", CMDDATA, 704549Seric "rset", CMDRSET, 714549Seric "vrfy", CMDVRFY, 7258092Seric "expn", CMDEXPN, 734549Seric "help", CMDHELP, 744549Seric "noop", CMDNOOP, 754549Seric "quit", CMDQUIT, 764976Seric "helo", CMDHELO, 7758323Seric "ehlo", CMDEHLO, 788544Seric "verb", CMDVERB, 799314Seric "onex", CMDONEX, 8036230Skarels /* 8136230Skarels * remaining commands are here only 8236230Skarels * to trap and log attempts to use them 8336230Skarels */ 849339Seric "showq", CMDDBGQSHOW, 858544Seric "debug", CMDDBGDEBUG, 8664685Seric "wiz", CMDLOGBOGUS, 874549Seric NULL, CMDERROR, 884549Seric }; 894549Seric 909378Seric bool OneXact = FALSE; /* one xaction only this run */ 9165017Seric char *CurSmtpClient; /* who's at the other end of channel */ 9211146Seric 9363937Seric static char *skipword(); 9466325Seric extern char RealUserName[]; 9563937Seric 9666325Seric 9766283Seric #define MAXBADCOMMANDS 25 /* maximum number of bad commands */ 9866283Seric 9955012Seric smtp(e) 10055012Seric register ENVELOPE *e; 1014549Seric { 1024549Seric register char *p; 1038544Seric register struct cmd *c; 1044549Seric char *cmd; 1055003Seric auto ADDRESS *vrfyqueue; 10612612Seric ADDRESS *a; 10758109Seric bool gotmail; /* mail command received */ 10858092Seric bool gothello; /* helo command received */ 10958092Seric bool vrfy; /* set if this is a vrfy command */ 11058323Seric char *protocol; /* sending protocol */ 11159016Seric char *sendinghost; /* sending hostname */ 11266005Seric char *peerhostname; /* name of SMTP peer or "localhost" */ 11358333Seric auto char *delimptr; 11458714Seric char *id; 11568433Seric int nrcpts = 0; /* number of RCPT commands */ 11663787Seric bool doublequeue; 11766283Seric int badcommands = 0; /* count of bad commands */ 1188544Seric char inp[MAXLINE]; 11957232Seric char cmdbuf[MAXLINE]; 1207124Seric extern char Version[]; 12124943Seric extern ENVELOPE BlankEnvelope; 1224549Seric 12359066Seric if (fileno(OutChannel) != fileno(stdout)) 1247363Seric { 1257363Seric /* arrange for debugging output to go to remote host */ 12659066Seric (void) dup2(fileno(OutChannel), fileno(stdout)); 1277363Seric } 12855012Seric settime(e); 12966005Seric peerhostname = RealHostName; 13066005Seric if (peerhostname == NULL) 13166005Seric peerhostname = "localhost"; 13266005Seric CurHostName = peerhostname; 13365017Seric CurSmtpClient = macvalue('_', e); 13465017Seric if (CurSmtpClient == NULL) 13566003Seric CurSmtpClient = CurHostName; 13665017Seric 13765017Seric setproctitle("server %s startup", CurSmtpClient); 13868529Seric expand("\201e", inp, sizeof inp, e); 13964496Seric if (BrokenSmtpPeers) 14064496Seric { 14166762Seric p = strchr(inp, '\n'); 14266762Seric if (p != NULL) 14366762Seric *p = '\0'; 14464496Seric message("220 %s", inp); 14564496Seric } 14664496Seric else 14764496Seric { 14866745Seric char *q = inp; 14966745Seric 15066745Seric while (q != NULL) 15166745Seric { 15266762Seric p = strchr(q, '\n'); 15366762Seric if (p != NULL) 15466762Seric *p++ = '\0'; 15566745Seric message("220-%s", q); 15666745Seric q = p; 15766745Seric } 15864496Seric message("220 ESMTP spoken here"); 15964496Seric } 16058330Seric protocol = NULL; 16159016Seric sendinghost = macvalue('s', e); 16258082Seric gothello = FALSE; 16358330Seric gotmail = FALSE; 1644549Seric for (;;) 1654549Seric { 16612612Seric /* arrange for backout */ 16765751Seric if (setjmp(TopFrame) > 0) 16859058Seric { 16965751Seric /* if() nesting is necessary for Cray UNICOS */ 17065751Seric if (InChild) 17165751Seric { 17265751Seric QuickAbort = FALSE; 17365751Seric SuprErrs = TRUE; 17465751Seric finis(); 17565751Seric } 17659058Seric } 17712612Seric QuickAbort = FALSE; 17812612Seric HoldErrs = FALSE; 17951951Seric LogUsrErrs = FALSE; 18063843Seric e->e_flags &= ~(EF_VRFYONLY|EF_GLOBALERRS); 18112612Seric 1827356Seric /* setup for the read */ 18355012Seric e->e_to = NULL; 1844577Seric Errors = 0; 1857275Seric (void) fflush(stdout); 1867356Seric 1877356Seric /* read the input line */ 18861093Seric SmtpPhase = "server cmd read"; 18961093Seric setproctitle("server %s cmd read", CurHostName); 19061093Seric p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand, 19161093Seric SmtpPhase); 1927356Seric 1937685Seric /* handle errors */ 1947356Seric if (p == NULL) 1957356Seric { 1964549Seric /* end of file, just die */ 19766017Seric disconnect(1, e); 19858151Seric message("421 %s Lost input channel from %s", 19965017Seric MyHostName, CurSmtpClient); 20055464Seric #ifdef LOG 20163843Seric if (LogLevel > (gotmail ? 1 : 19)) 20255464Seric syslog(LOG_NOTICE, "lost input channel from %s", 20365017Seric CurSmtpClient); 20455464Seric #endif 20558069Seric if (InChild) 20658069Seric ExitStat = EX_QUIT; 2074549Seric finis(); 2084549Seric } 2094549Seric 2104549Seric /* clean up end of line */ 2114558Seric fixcrlf(inp, TRUE); 2124549Seric 2134713Seric /* echo command to transcript */ 21455012Seric if (e->e_xfp != NULL) 21555012Seric fprintf(e->e_xfp, "<<< %s\n", inp); 2164713Seric 21759060Seric if (e->e_id == NULL) 21865058Seric setproctitle("%s: %.80s", CurSmtpClient, inp); 21959060Seric else 22065058Seric setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp); 22159060Seric 2224549Seric /* break off command */ 22358050Seric for (p = inp; isascii(*p) && isspace(*p); p++) 2244549Seric continue; 22557232Seric cmd = cmdbuf; 22658050Seric while (*p != '\0' && 22758050Seric !(isascii(*p) && isspace(*p)) && 22858050Seric cmd < &cmdbuf[sizeof cmdbuf - 2]) 22924981Seric *cmd++ = *p++; 23024981Seric *cmd = '\0'; 2314549Seric 23225691Seric /* throw away leading whitespace */ 23358050Seric while (isascii(*p) && isspace(*p)) 23425691Seric p++; 23525691Seric 2364549Seric /* decode command */ 2374549Seric for (c = CmdTab; c->cmdname != NULL; c++) 2384549Seric { 23933725Sbostic if (!strcasecmp(c->cmdname, cmdbuf)) 2404549Seric break; 2414549Seric } 2424549Seric 24351954Seric /* reset errors */ 24451954Seric errno = 0; 24551954Seric 2464549Seric /* process command */ 2474549Seric switch (c->cmdcode) 2484549Seric { 2494976Seric case CMDHELO: /* hello -- introduce yourself */ 25058323Seric case CMDEHLO: /* extended hello */ 25158323Seric if (c->cmdcode == CMDEHLO) 25258323Seric { 25358323Seric protocol = "ESMTP"; 25461093Seric SmtpPhase = "server EHLO"; 25558323Seric } 25658323Seric else 25758323Seric { 25858323Seric protocol = "SMTP"; 25961093Seric SmtpPhase = "server HELO"; 26058323Seric } 26167445Seric 26267445Seric /* check for valid domain name (re 1123 5.2.5) */ 26367445Seric if (*p == '\0') 26467445Seric { 26567445Seric message("501 %s requires domain address", 26667445Seric cmdbuf); 26767445Seric break; 26867445Seric } 26967445Seric else 27067445Seric { 27167445Seric register char *q; 27267445Seric 27367445Seric for (q = p; *q != '\0'; q++) 27467445Seric { 27567445Seric if (!isascii(*q)) 27667445Seric break; 27767445Seric if (isalnum(*q)) 27867445Seric continue; 27967445Seric if (strchr("[].-_#", *q) == NULL) 28067445Seric break; 28167445Seric } 28267445Seric if (*q != '\0') 28367445Seric { 28467445Seric message("501 Invalid domain name"); 28567445Seric break; 28667445Seric } 28767445Seric } 28867445Seric 28959016Seric sendinghost = newstr(p); 29060210Seric gothello = TRUE; 29160210Seric if (c->cmdcode != CMDEHLO) 29260239Seric { 29360239Seric /* print old message and be done with it */ 29460239Seric message("250 %s Hello %s, pleased to meet you", 29565017Seric MyHostName, CurSmtpClient); 29660210Seric break; 29760239Seric } 29860239Seric 29960239Seric /* print extended message and brag */ 30060239Seric message("250-%s Hello %s, pleased to meet you", 30166760Seric MyHostName, CurSmtpClient); 30258323Seric if (!bitset(PRIV_NOEXPN, PrivacyFlags)) 30358323Seric message("250-EXPN"); 30467417Seric message("250-8BITMIME"); 30564359Seric if (MaxMessageSize > 0) 30664359Seric message("250-SIZE %ld", MaxMessageSize); 30759271Seric else 30859271Seric message("250-SIZE"); 30968028Seric #ifdef DSN 31068559Seric message("250-X-DSN-3 (Unpublished draft of 12 Mar 1995)"); 31168028Seric #endif 31258323Seric message("250 HELP"); 3134976Seric break; 3144976Seric 3154549Seric case CMDMAIL: /* mail -- designate sender */ 31661093Seric SmtpPhase = "server MAIL"; 31724943Seric 3189314Seric /* check for validity of this command */ 31958789Seric if (!gothello) 32058082Seric { 32158957Seric /* set sending host to our known value */ 32259016Seric if (sendinghost == NULL) 32366005Seric sendinghost = peerhostname; 32458957Seric 32558789Seric if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags)) 32658821Seric { 32758789Seric message("503 Polite people say HELO first"); 32858821Seric break; 32958821Seric } 33058082Seric } 33158109Seric if (gotmail) 3324558Seric { 33358151Seric message("503 Sender already specified"); 33463843Seric if (InChild) 33563843Seric finis(); 3364558Seric break; 3374558Seric } 3389339Seric if (InChild) 3399339Seric { 34036230Skarels errno = 0; 34158151Seric syserr("503 Nested MAIL command: MAIL %s", p); 34258069Seric finis(); 3439339Seric } 3449339Seric 3459339Seric /* fork a subprocess to process this command */ 34655012Seric if (runinchild("SMTP-MAIL", e) > 0) 3479339Seric break; 34863753Seric if (!gothello) 34963753Seric { 35063753Seric auth_warning(e, 35166005Seric "Host %s didn't use HELO protocol", 35266005Seric peerhostname); 35363753Seric } 35465947Seric #ifdef PICKY_HELO_CHECK 35566005Seric if (strcasecmp(sendinghost, peerhostname) != 0 && 35666005Seric (strcasecmp(peerhostname, "localhost") != 0 || 35765823Seric strcasecmp(sendinghost, MyHostName) != 0)) 35865823Seric { 35965823Seric auth_warning(e, "Host %s claimed to be %s", 36066005Seric peerhostname, sendinghost); 36165823Seric } 36265947Seric #endif 36365823Seric 36458323Seric if (protocol == NULL) 36558323Seric protocol = "SMTP"; 36658323Seric define('r', protocol, e); 36759016Seric define('s', sendinghost, e); 36855012Seric initsys(e); 36959747Seric nrcpts = 0; 37068582Seric e->e_flags |= EF_LOGSENDER|EF_CLRQUEUE; 37165058Seric setproctitle("%s %s: %.80s", e->e_id, CurSmtpClient, inp); 3729339Seric 3739339Seric /* child -- go do the processing */ 3744549Seric p = skipword(p, "from"); 3754549Seric if (p == NULL) 3764549Seric break; 37757977Seric if (setjmp(TopFrame) > 0) 37858147Seric { 37958147Seric /* this failed -- undo work */ 38058147Seric if (InChild) 38159058Seric { 38259058Seric QuickAbort = FALSE; 38359058Seric SuprErrs = TRUE; 38463787Seric e->e_flags &= ~EF_FATALERRS; 38558147Seric finis(); 38659058Seric } 38757977Seric break; 38858147Seric } 38957977Seric QuickAbort = TRUE; 39058333Seric 39158333Seric /* must parse sender first */ 39258333Seric delimptr = NULL; 39358704Seric setsender(p, e, &delimptr, FALSE); 39458333Seric p = delimptr; 39558333Seric if (p != NULL && *p != '\0') 39658333Seric *p++ = '\0'; 39758333Seric 39866325Seric /* check for possible spoofing */ 39966325Seric if (RealUid != 0 && OpMode == MD_SMTP && 40067473Seric !bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) && 40167473Seric strcmp(e->e_from.q_user, RealUserName) != 0) 40266325Seric { 40366325Seric auth_warning(e, "%s owned process doing -bs", 40466325Seric RealUserName); 40566325Seric } 40666325Seric 40758333Seric /* now parse ESMTP arguments */ 40868560Seric e->e_msgsize = 0; 40966764Seric while (p != NULL && *p != '\0') 41058333Seric { 41158333Seric char *kp; 41266304Seric char *vp = NULL; 41358333Seric 41458333Seric /* locate the beginning of the keyword */ 41558333Seric while (isascii(*p) && isspace(*p)) 41658333Seric p++; 41758333Seric if (*p == '\0') 41858333Seric break; 41958333Seric kp = p; 42058333Seric 42158333Seric /* skip to the value portion */ 42258333Seric while (isascii(*p) && isalnum(*p) || *p == '-') 42358333Seric p++; 42458333Seric if (*p == '=') 42558333Seric { 42658333Seric *p++ = '\0'; 42758333Seric vp = p; 42858333Seric 42958333Seric /* skip to the end of the value */ 43058333Seric while (*p != '\0' && *p != ' ' && 43158333Seric !(isascii(*p) && iscntrl(*p)) && 43258333Seric *p != '=') 43358333Seric p++; 43458333Seric } 43558333Seric 43658333Seric if (*p != '\0') 43758333Seric *p++ = '\0'; 43858333Seric 43958333Seric if (tTd(19, 1)) 44066764Seric printf("MAIL: got arg %s=\"%s\"\n", kp, 44158333Seric vp == NULL ? "<null>" : vp); 44258333Seric 44368559Seric mail_esmtp_args(kp, vp, e); 44458333Seric } 44559284Seric 44668560Seric if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize) 44759284Seric { 44859284Seric usrerr("552 Message size exceeds fixed maximum message size (%ld)", 44959284Seric MaxMessageSize); 45059284Seric /* NOTREACHED */ 45159284Seric } 45258333Seric 45368560Seric if (!enoughspace(e->e_msgsize)) 45458333Seric { 45558333Seric message("452 Insufficient disk space; try again later"); 45658333Seric break; 45758333Seric } 45858151Seric message("250 Sender ok"); 45958147Seric gotmail = TRUE; 4604549Seric break; 4614549Seric 4624976Seric case CMDRCPT: /* rcpt -- designate recipient */ 46358850Seric if (!gotmail) 46458850Seric { 46558850Seric usrerr("503 Need MAIL before RCPT"); 46658850Seric break; 46758850Seric } 46861093Seric SmtpPhase = "server RCPT"; 46912612Seric if (setjmp(TopFrame) > 0) 47014785Seric { 47155012Seric e->e_flags &= ~EF_FATALERRS; 47212612Seric break; 47314785Seric } 47412612Seric QuickAbort = TRUE; 47551951Seric LogUsrErrs = TRUE; 47658093Seric 47759699Seric if (e->e_sendmode != SM_DELIVER) 47859699Seric e->e_flags |= EF_VRFYONLY; 47958919Seric 4804549Seric p = skipword(p, "to"); 4814549Seric if (p == NULL) 4824549Seric break; 48367880Seric a = parseaddr(p, NULLADDR, RF_COPYALL, ' ', &delimptr, e); 48412612Seric if (a == NULL) 48512612Seric break; 48667880Seric p = delimptr; 48767880Seric 48867880Seric /* now parse ESMTP arguments */ 48967880Seric while (p != NULL && *p != '\0') 49067880Seric { 49167880Seric char *kp; 49267880Seric char *vp = NULL; 49367880Seric 49467880Seric /* locate the beginning of the keyword */ 49567880Seric while (isascii(*p) && isspace(*p)) 49667880Seric p++; 49767880Seric if (*p == '\0') 49867880Seric break; 49967880Seric kp = p; 50067880Seric 50167880Seric /* skip to the value portion */ 50267880Seric while (isascii(*p) && isalnum(*p) || *p == '-') 50367880Seric p++; 50467880Seric if (*p == '=') 50567880Seric { 50667880Seric *p++ = '\0'; 50767880Seric vp = p; 50867880Seric 50967880Seric /* skip to the end of the value */ 51067880Seric while (*p != '\0' && *p != ' ' && 51167880Seric !(isascii(*p) && iscntrl(*p)) && 51267880Seric *p != '=') 51367880Seric p++; 51467880Seric } 51567880Seric 51667880Seric if (*p != '\0') 51767880Seric *p++ = '\0'; 51867880Seric 51967880Seric if (tTd(19, 1)) 52067880Seric printf("RCPT: got arg %s=\"%s\"\n", kp, 52167880Seric vp == NULL ? "<null>" : vp); 52267880Seric 52367963Seric rcpt_esmtp_args(a, kp, vp, e); 52467963Seric 52567880Seric } 52667980Seric 52767980Seric /* save in recipient list after ESMTP mods */ 52867980Seric a->q_flags |= QPRIMARY; 52967982Seric a = recipient(a, &e->e_sendqueue, 0, e); 53067980Seric 53112612Seric if (Errors != 0) 53212612Seric break; 53312612Seric 53412612Seric /* no errors during parsing, but might be a duplicate */ 53555012Seric e->e_to = p; 53612612Seric if (!bitset(QBADADDR, a->q_flags)) 53759747Seric { 53864718Seric message("250 Recipient ok%s", 53964718Seric bitset(QQUEUEUP, a->q_flags) ? 54064718Seric " (will queue)" : ""); 54159747Seric nrcpts++; 54259747Seric } 54312612Seric else 5444549Seric { 54512612Seric /* punt -- should keep message in ADDRESS.... */ 54658151Seric message("550 Addressee unknown"); 5474549Seric } 54855012Seric e->e_to = NULL; 5494549Seric break; 5504549Seric 5514549Seric case CMDDATA: /* data -- text of mail */ 55261093Seric SmtpPhase = "server DATA"; 55358109Seric if (!gotmail) 5544549Seric { 55558151Seric message("503 Need MAIL command"); 5564976Seric break; 5574549Seric } 55864718Seric else if (nrcpts <= 0) 5594549Seric { 56058151Seric message("503 Need RCPT (recipient)"); 5614976Seric break; 5624549Seric } 5634976Seric 56458929Seric /* check to see if we need to re-expand aliases */ 56563787Seric /* also reset QBADADDR on already-diagnosted addrs */ 56663787Seric doublequeue = FALSE; 56758929Seric for (a = e->e_sendqueue; a != NULL; a = a->q_next) 56858929Seric { 56958929Seric if (bitset(QVERIFIED, a->q_flags)) 57063787Seric { 57163787Seric /* need to re-expand aliases */ 57263787Seric doublequeue = TRUE; 57363787Seric } 57463787Seric if (bitset(QBADADDR, a->q_flags)) 57563787Seric { 57663787Seric /* make this "go away" */ 57763787Seric a->q_flags |= QDONTSEND; 57863787Seric a->q_flags &= ~QBADADDR; 57963787Seric } 58058929Seric } 58158929Seric 5824976Seric /* collect the text of the message */ 58324943Seric SmtpPhase = "collect"; 58467546Seric collect(InChannel, TRUE, doublequeue, NULL, e); 58564766Seric if (Errors != 0) 58664766Seric goto abortmessage; 58767131Seric 58868582Seric /* make sure we actually do delivery */ 58968582Seric e->e_flags &= ~EF_CLRQUEUE; 59068582Seric 59167131Seric /* from now on, we have to operate silently */ 59263965Seric HoldErrs = TRUE; 59367131Seric e->e_errormode = EM_MAIL; 5944976Seric 5958238Seric /* 5968238Seric ** Arrange to send to everyone. 5978238Seric ** If sending to multiple people, mail back 5988238Seric ** errors rather than reporting directly. 5998238Seric ** In any case, don't mail back errors for 6008238Seric ** anything that has happened up to 6018238Seric ** now (the other end will do this). 60210197Seric ** Truncate our transcript -- the mail has gotten 60310197Seric ** to us successfully, and if we have 60410197Seric ** to mail this back, it will be easier 60510197Seric ** on the reader. 6068238Seric ** Then send to everyone. 6078238Seric ** Finally give a reply code. If an error has 6088238Seric ** already been given, don't mail a 6098238Seric ** message back. 6109339Seric ** We goose error returns by clearing error bit. 6118238Seric */ 6128238Seric 61324943Seric SmtpPhase = "delivery"; 61455012Seric e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp); 61558714Seric id = e->e_id; 6164976Seric 61767131Seric if (doublequeue) 61859730Seric { 61967131Seric /* make sure it is in the queue */ 62067131Seric queueup(e, TRUE, FALSE); 62168035Seric if (e->e_sendmode == SM_QUEUE) 62268035Seric e->e_flags |= EF_KEEPQUEUE; 62359730Seric } 6248238Seric else 62558919Seric { 62667131Seric /* send to all recipients */ 62767131Seric sendall(e, SM_DEFAULT); 62867131Seric } 62967131Seric e->e_to = NULL; 63059747Seric 63167131Seric /* issue success message */ 63267131Seric message("250 %s Message accepted for delivery", id); 63364296Seric 63467131Seric /* if we just queued, poke it */ 63567131Seric if (doublequeue && e->e_sendmode != SM_QUEUE) 63667131Seric { 63767131Seric extern pid_t dowork(); 63867131Seric 63967131Seric unlockqueue(e); 64067131Seric (void) dowork(id, TRUE, TRUE, e); 64158919Seric } 64258883Seric 64359747Seric abortmessage: 6449339Seric /* if in a child, pop back to our parent */ 6459339Seric if (InChild) 6469339Seric finis(); 64724943Seric 64824943Seric /* clean up a bit */ 64958109Seric gotmail = FALSE; 65055012Seric dropenvelope(e); 65158179Seric CurEnv = e = newenvelope(e, CurEnv); 65255012Seric e->e_flags = BlankEnvelope.e_flags; 6534549Seric break; 6544549Seric 6554549Seric case CMDRSET: /* rset -- reset state */ 65658151Seric message("250 Reset state"); 65764359Seric e->e_flags |= EF_CLRQUEUE; 6589339Seric if (InChild) 6599339Seric finis(); 66058109Seric 66158109Seric /* clean up a bit */ 66258109Seric gotmail = FALSE; 66358109Seric dropenvelope(e); 66458179Seric CurEnv = e = newenvelope(e, CurEnv); 6659339Seric break; 6664549Seric 6674549Seric case CMDVRFY: /* vrfy -- verify address */ 66858092Seric case CMDEXPN: /* expn -- expand address */ 66958092Seric vrfy = c->cmdcode == CMDVRFY; 67058092Seric if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 67158092Seric PrivacyFlags)) 67258082Seric { 67358412Seric if (vrfy) 67467160Seric message("252 Cannot VRFY user; try RCPT to attempt delivery (or try finger)"); 67558412Seric else 67665192Seric message("502 Sorry, we do not allow this operation"); 67765017Seric #ifdef LOG 67865017Seric if (LogLevel > 5) 67965017Seric syslog(LOG_INFO, "%s: %s [rejected]", 68065017Seric CurSmtpClient, inp); 68165017Seric #endif 68258082Seric break; 68358082Seric } 68458082Seric else if (!gothello && 68558092Seric bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 68658092Seric PrivacyFlags)) 68758082Seric { 68858151Seric message("503 I demand that you introduce yourself first"); 68958082Seric break; 69058082Seric } 69158092Seric if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0) 6929339Seric break; 69355173Seric #ifdef LOG 69458020Seric if (LogLevel > 5) 69565017Seric syslog(LOG_INFO, "%s: %s", CurSmtpClient, inp); 69655173Seric #endif 6975003Seric vrfyqueue = NULL; 6987762Seric QuickAbort = TRUE; 69958092Seric if (vrfy) 70058092Seric e->e_flags |= EF_VRFYONLY; 70162373Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 70267615Seric p++; 70362373Seric if (*p == '\0') 70462373Seric { 70562373Seric message("501 Argument required"); 70662373Seric Errors++; 70762373Seric } 70862373Seric else 70962373Seric { 71067990Seric (void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e); 71162373Seric } 7127762Seric if (Errors != 0) 7139339Seric { 7149339Seric if (InChild) 7159339Seric finis(); 7167762Seric break; 7179339Seric } 71862373Seric if (vrfyqueue == NULL) 71962373Seric { 72062373Seric message("554 Nothing to %s", vrfy ? "VRFY" : "EXPN"); 72162373Seric } 7225003Seric while (vrfyqueue != NULL) 7235003Seric { 72463971Seric a = vrfyqueue; 72563971Seric while ((a = a->q_next) != NULL && 72663971Seric bitset(QDONTSEND|QBADADDR, a->q_flags)) 72763971Seric continue; 7287685Seric if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 72958151Seric printvrfyaddr(vrfyqueue, a == NULL); 73063847Seric vrfyqueue = vrfyqueue->q_next; 7315003Seric } 7329339Seric if (InChild) 7339339Seric finis(); 7344549Seric break; 7354549Seric 7364549Seric case CMDHELP: /* help -- give user info */ 7374577Seric help(p); 7384549Seric break; 7394549Seric 7404549Seric case CMDNOOP: /* noop -- do nothing */ 74164122Seric message("250 OK"); 7424549Seric break; 7434549Seric 7444549Seric case CMDQUIT: /* quit -- leave mail */ 74558151Seric message("221 %s closing connection", MyHostName); 74661051Seric 74766283Seric doquit: 74861051Seric /* avoid future 050 messages */ 74966017Seric disconnect(1, e); 75061051Seric 7519339Seric if (InChild) 7529339Seric ExitStat = EX_QUIT; 7534549Seric finis(); 7544549Seric 7558544Seric case CMDVERB: /* set verbose mode */ 75659957Seric if (bitset(PRIV_NOEXPN, PrivacyFlags)) 75759957Seric { 75859957Seric /* this would give out the same info */ 75959957Seric message("502 Verbose unavailable"); 76059957Seric break; 76159957Seric } 7628544Seric Verbose = TRUE; 76358734Seric e->e_sendmode = SM_DELIVER; 76459957Seric message("250 Verbose mode"); 7658544Seric break; 7668544Seric 7679314Seric case CMDONEX: /* doing one transaction only */ 7689378Seric OneXact = TRUE; 76959957Seric message("250 Only one transaction"); 7709314Seric break; 7719314Seric 77236230Skarels # ifdef SMTPDEBUG 7739339Seric case CMDDBGQSHOW: /* show queues */ 7746907Seric printf("Send Queue="); 77555012Seric printaddr(e->e_sendqueue, TRUE); 7765003Seric break; 7777275Seric 7787275Seric case CMDDBGDEBUG: /* set debug mode */ 7797676Seric tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 7807676Seric tTflag(p); 78158151Seric message("200 Debug set"); 7827275Seric break; 7837275Seric 78436230Skarels # else /* not SMTPDEBUG */ 78536230Skarels case CMDDBGQSHOW: /* show queues */ 78636230Skarels case CMDDBGDEBUG: /* set debug mode */ 78764685Seric # endif /* SMTPDEBUG */ 78864685Seric case CMDLOGBOGUS: /* bogus command */ 78936233Skarels # ifdef LOG 79058308Seric if (LogLevel > 0) 79164685Seric syslog(LOG_CRIT, 79258020Seric "\"%s\" command from %s (%s)", 79366005Seric c->cmdname, peerhostname, 79458755Seric anynet_ntoa(&RealHostAddr)); 79536233Skarels # endif 79636230Skarels /* FALL THROUGH */ 79736230Skarels 7984549Seric case CMDERROR: /* unknown command */ 79966283Seric if (++badcommands > MAXBADCOMMANDS) 80066283Seric { 80166283Seric message("421 %s Too many bad commands; closing connection", 80266283Seric MyHostName); 80366283Seric goto doquit; 80466283Seric } 80566283Seric 80658151Seric message("500 Command unrecognized"); 8074549Seric break; 8084549Seric 8094549Seric default: 81036230Skarels errno = 0; 81158151Seric syserr("500 smtp: unknown code %d", c->cmdcode); 8124549Seric break; 8134549Seric } 8144549Seric } 8154549Seric } 8164549Seric /* 8174549Seric ** SKIPWORD -- skip a fixed word. 8184549Seric ** 8194549Seric ** Parameters: 8204549Seric ** p -- place to start looking. 8214549Seric ** w -- word to skip. 8224549Seric ** 8234549Seric ** Returns: 8244549Seric ** p following w. 8254549Seric ** NULL on error. 8264549Seric ** 8274549Seric ** Side Effects: 8284549Seric ** clobbers the p data area. 8294549Seric */ 8304549Seric 8314549Seric static char * 8324549Seric skipword(p, w) 8334549Seric register char *p; 8344549Seric char *w; 8354549Seric { 8364549Seric register char *q; 83766005Seric char *firstp = p; 8384549Seric 8394549Seric /* find beginning of word */ 84058050Seric while (isascii(*p) && isspace(*p)) 8414549Seric p++; 8424549Seric q = p; 8434549Seric 8444549Seric /* find end of word */ 84558050Seric while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 8464549Seric p++; 84758050Seric while (isascii(*p) && isspace(*p)) 8484549Seric *p++ = '\0'; 8494549Seric if (*p != ':') 8504549Seric { 8514549Seric syntax: 85266005Seric message("501 Syntax error in parameters scanning \"%s\"", 85366005Seric firstp); 8544549Seric Errors++; 8554549Seric return (NULL); 8564549Seric } 8574549Seric *p++ = '\0'; 85858050Seric while (isascii(*p) && isspace(*p)) 8594549Seric p++; 8604549Seric 86162373Seric if (*p == '\0') 86262373Seric goto syntax; 86362373Seric 8644549Seric /* see if the input word matches desired word */ 86533725Sbostic if (strcasecmp(q, w)) 8664549Seric goto syntax; 8674549Seric 8684549Seric return (p); 8694549Seric } 8704577Seric /* 87168559Seric ** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line 87268559Seric ** 87368559Seric ** Parameters: 87468559Seric ** kp -- the parameter key. 87568559Seric ** vp -- the value of that parameter. 87668559Seric ** e -- the envelope. 87768559Seric ** 87868559Seric ** Returns: 87968559Seric ** none. 88068559Seric */ 88168559Seric 88268559Seric mail_esmtp_args(kp, vp, e) 88368559Seric char *kp; 88468559Seric char *vp; 88568559Seric ENVELOPE *e; 88668559Seric { 88768559Seric if (strcasecmp(kp, "size") == 0) 88868559Seric { 88968559Seric if (vp == NULL) 89068559Seric { 89168559Seric usrerr("501 SIZE requires a value"); 89268559Seric /* NOTREACHED */ 89368559Seric } 89468559Seric # ifdef __STDC__ 89568560Seric e->e_msgsize = strtoul(vp, (char **) NULL, 10); 89668559Seric # else 89768560Seric e->e_msgsize = strtol(vp, (char **) NULL, 10); 89868559Seric # endif 89968559Seric } 90068559Seric else if (strcasecmp(kp, "body") == 0) 90168559Seric { 90268559Seric if (vp == NULL) 90368559Seric { 90468559Seric usrerr("501 BODY requires a value"); 90568559Seric /* NOTREACHED */ 90668559Seric } 90768559Seric if (strcasecmp(vp, "8bitmime") == 0) 90868559Seric { 90968559Seric SevenBitInput = FALSE; 91068559Seric } 91168559Seric else if (strcasecmp(vp, "7bit") == 0) 91268559Seric { 91368559Seric SevenBitInput = TRUE; 91468559Seric } 91568559Seric else 91668559Seric { 91768559Seric usrerr("501 Unknown BODY type %s", 91868559Seric vp); 91968559Seric /* NOTREACHED */ 92068559Seric } 92168559Seric e->e_bodytype = newstr(vp); 92268559Seric } 92368559Seric else if (strcasecmp(kp, "envid") == 0) 92468559Seric { 92568559Seric if (vp == NULL) 92668559Seric { 92768559Seric usrerr("501 ENVID requires a value"); 92868559Seric /* NOTREACHED */ 92968559Seric } 930*68583Seric if (!xtextok(vp)) 931*68583Seric { 932*68583Seric usrerr("501 Syntax error in ENVID parameter value"); 933*68583Seric /* NOTREACHED */ 934*68583Seric } 935*68583Seric if (e->e_envid != NULL) 936*68583Seric { 937*68583Seric usrerr("501 Duplicate ENVID parameter"); 938*68583Seric /* NOTREACHED */ 939*68583Seric } 94068559Seric e->e_envid = newstr(vp); 94168559Seric } 94268559Seric else if (strcasecmp(kp, "ret") == 0) 94368559Seric { 94468559Seric if (vp == NULL) 94568559Seric { 94668559Seric usrerr("501 RET requires a value"); 94768559Seric /* NOTREACHED */ 94868559Seric } 949*68583Seric if (bitset(EF_RET_PARAM, e->e_flags)) 950*68583Seric { 951*68583Seric usrerr("501 Duplicate RET parameter"); 952*68583Seric /* NOTREACHED */ 953*68583Seric } 95468559Seric e->e_flags |= EF_RET_PARAM; 95568559Seric if (strcasecmp(vp, "hdrs") == 0) 95668559Seric e->e_flags |= EF_NO_BODY_RETN; 95768559Seric else if (strcasecmp(vp, "full") != 0) 95868559Seric { 95968559Seric usrerr("501 Bad argument \"%s\" to RET", vp); 96068559Seric /* NOTREACHED */ 96168559Seric } 96268559Seric } 96368559Seric else 96468559Seric { 96568559Seric usrerr("501 %s parameter unrecognized", kp); 96668559Seric /* NOTREACHED */ 96768559Seric } 96868559Seric } 96968559Seric /* 97067963Seric ** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line 97167963Seric ** 97267963Seric ** Parameters: 97367963Seric ** a -- the address corresponding to the To: parameter. 97467963Seric ** kp -- the parameter key. 97567963Seric ** vp -- the value of that parameter. 97667963Seric ** e -- the envelope. 97767963Seric ** 97867963Seric ** Returns: 97967963Seric ** none. 98067963Seric */ 98167963Seric 98267963Seric rcpt_esmtp_args(a, kp, vp, e) 98367963Seric ADDRESS *a; 98467963Seric char *kp; 98567963Seric char *vp; 98667963Seric ENVELOPE *e; 98767963Seric { 98867963Seric if (strcasecmp(kp, "notify") == 0) 98967963Seric { 99067963Seric char *p; 99167963Seric 99267963Seric if (vp == NULL) 99367963Seric { 99467963Seric usrerr("501 NOTIFY requires a value"); 99567963Seric /* NOTREACHED */ 99667963Seric } 99767963Seric a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY); 99867963Seric if (strcasecmp(vp, "never") == 0) 99967963Seric return; 100067963Seric for (p = vp; p != NULL; vp = p) 100167963Seric { 100267963Seric p = strchr(p, ','); 100367963Seric if (p != NULL) 100467963Seric *p++ = '\0'; 100567963Seric if (strcasecmp(vp, "success") == 0) 100667963Seric a->q_flags |= QPINGONSUCCESS; 100767963Seric else if (strcasecmp(vp, "failure") == 0) 100867963Seric a->q_flags |= QPINGONFAILURE; 100967963Seric else if (strcasecmp(vp, "delay") == 0) 101067963Seric a->q_flags |= QPINGONDELAY; 101167963Seric else 101267963Seric { 101367963Seric usrerr("501 Bad argument \"%s\" to NOTIFY", 101467963Seric vp); 101567963Seric /* NOTREACHED */ 101667963Seric } 101767963Seric } 101867963Seric } 101967963Seric else if (strcasecmp(kp, "orcpt") == 0) 102067963Seric { 102167963Seric if (vp == NULL) 102267963Seric { 102367963Seric usrerr("501 ORCPT requires a value"); 102467963Seric /* NOTREACHED */ 102567963Seric } 1026*68583Seric if (!xtextok(vp)) 1027*68583Seric { 1028*68583Seric usrerr("501 Syntax error in ORCPT parameter value"); 1029*68583Seric /* NOTREACHED */ 1030*68583Seric } 1031*68583Seric if (a->q_orcpt != NULL) 1032*68583Seric { 1033*68583Seric usrerr("501 Duplicate ORCPT parameter"); 1034*68583Seric /* NOTREACHED */ 1035*68583Seric } 103667963Seric a->q_orcpt = newstr(vp); 103767963Seric } 103867963Seric else 103967963Seric { 104067963Seric usrerr("501 %s parameter unrecognized", kp); 104167963Seric /* NOTREACHED */ 104267963Seric } 104367963Seric } 104467963Seric /* 104558151Seric ** PRINTVRFYADDR -- print an entry in the verify queue 104658151Seric ** 104758151Seric ** Parameters: 104858151Seric ** a -- the address to print 104958151Seric ** last -- set if this is the last one. 105058151Seric ** 105158151Seric ** Returns: 105258151Seric ** none. 105358151Seric ** 105458151Seric ** Side Effects: 105558151Seric ** Prints the appropriate 250 codes. 105658151Seric */ 105758151Seric 105858151Seric printvrfyaddr(a, last) 105958151Seric register ADDRESS *a; 106058151Seric bool last; 106158151Seric { 106258151Seric char fmtbuf[20]; 106358151Seric 106458151Seric strcpy(fmtbuf, "250"); 106558151Seric fmtbuf[3] = last ? ' ' : '-'; 106658151Seric 106759746Seric if (a->q_fullname == NULL) 106859746Seric { 106959746Seric if (strchr(a->q_user, '@') == NULL) 107059746Seric strcpy(&fmtbuf[4], "<%s@%s>"); 107159746Seric else 107259746Seric strcpy(&fmtbuf[4], "<%s>"); 107359746Seric message(fmtbuf, a->q_user, MyHostName); 107459746Seric } 107558151Seric else 107658151Seric { 107759746Seric if (strchr(a->q_user, '@') == NULL) 107859746Seric strcpy(&fmtbuf[4], "%s <%s@%s>"); 107959746Seric else 108059746Seric strcpy(&fmtbuf[4], "%s <%s>"); 108159746Seric message(fmtbuf, a->q_fullname, a->q_user, MyHostName); 108258151Seric } 108358151Seric } 108458151Seric /* 10854577Seric ** HELP -- implement the HELP command. 10864577Seric ** 10874577Seric ** Parameters: 10884577Seric ** topic -- the topic we want help for. 10894577Seric ** 10904577Seric ** Returns: 10914577Seric ** none. 10924577Seric ** 10934577Seric ** Side Effects: 10944577Seric ** outputs the help file to message output. 10954577Seric */ 10964577Seric 10974577Seric help(topic) 10984577Seric char *topic; 10994577Seric { 11004577Seric register FILE *hf; 11014577Seric int len; 11024577Seric char buf[MAXLINE]; 11034577Seric bool noinfo; 11044577Seric 11058269Seric if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL) 11064577Seric { 11074577Seric /* no help */ 110811931Seric errno = 0; 110958151Seric message("502 HELP not implemented"); 11104577Seric return; 11114577Seric } 11124577Seric 111349669Seric if (topic == NULL || *topic == '\0') 111449669Seric topic = "smtp"; 111549669Seric else 111649669Seric makelower(topic); 111749669Seric 11184577Seric len = strlen(topic); 11194577Seric noinfo = TRUE; 11204577Seric 11214577Seric while (fgets(buf, sizeof buf, hf) != NULL) 11224577Seric { 11234577Seric if (strncmp(buf, topic, len) == 0) 11244577Seric { 11254577Seric register char *p; 11264577Seric 112756795Seric p = strchr(buf, '\t'); 11284577Seric if (p == NULL) 11294577Seric p = buf; 11304577Seric else 11314577Seric p++; 11324577Seric fixcrlf(p, TRUE); 113358151Seric message("214-%s", p); 11344577Seric noinfo = FALSE; 11354577Seric } 11364577Seric } 11374577Seric 11384577Seric if (noinfo) 113958151Seric message("504 HELP topic unknown"); 11404577Seric else 114158151Seric message("214 End of HELP info"); 11424628Seric (void) fclose(hf); 11434577Seric } 11448544Seric /* 11459339Seric ** RUNINCHILD -- return twice -- once in the child, then in the parent again 11469339Seric ** 11479339Seric ** Parameters: 11489339Seric ** label -- a string used in error messages 11499339Seric ** 11509339Seric ** Returns: 11519339Seric ** zero in the child 11529339Seric ** one in the parent 11539339Seric ** 11549339Seric ** Side Effects: 11559339Seric ** none. 11569339Seric */ 11578544Seric 115855012Seric runinchild(label, e) 11599339Seric char *label; 116055012Seric register ENVELOPE *e; 11619339Seric { 11629339Seric int childpid; 11639339Seric 116416158Seric if (!OneXact) 11659339Seric { 116616158Seric childpid = dofork(); 116716158Seric if (childpid < 0) 116816158Seric { 116916158Seric syserr("%s: cannot fork", label); 117016158Seric return (1); 117116158Seric } 117216158Seric if (childpid > 0) 117316158Seric { 117416158Seric auto int st; 11759339Seric 117616158Seric /* parent -- wait for child to complete */ 117761093Seric setproctitle("server %s child wait", CurHostName); 117816158Seric st = waitfor(childpid); 117916158Seric if (st == -1) 118016158Seric syserr("%s: lost child", label); 118164948Seric else if (!WIFEXITED(st)) 118264948Seric syserr("%s: died on signal %d", 118364948Seric label, st & 0177); 11849339Seric 118516158Seric /* if we exited on a QUIT command, complete the process */ 118666017Seric if (WEXITSTATUS(st) == EX_QUIT) 118766017Seric { 118866017Seric disconnect(1, e); 118916158Seric finis(); 119066017Seric } 11919339Seric 119216158Seric return (1); 119316158Seric } 119416158Seric else 119516158Seric { 119616158Seric /* child */ 119716158Seric InChild = TRUE; 119825050Seric QuickAbort = FALSE; 119955012Seric clearenvelope(e, FALSE); 120016158Seric } 12019339Seric } 120215256Seric 120316158Seric /* open alias database */ 120460537Seric initmaps(FALSE, e); 120516158Seric 120616158Seric return (0); 12079339Seric } 12089339Seric 120956795Seric # endif /* SMTP */ 1210