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*68560Seric static char sccsid[] = "@(#)srvrsmtp.c 8.58 (Berkeley) 03/21/95 (with SMTP)"; 1433731Sbostic #else 15*68560Seric static char sccsid[] = "@(#)srvrsmtp.c 8.58 (Berkeley) 03/21/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; 37065089Seric e->e_flags |= EF_LOGSENDER; 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 */ 408*68560Seric 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 446*68560Seric 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 453*68560Seric 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 58867131Seric /* from now on, we have to operate silently */ 58963965Seric HoldErrs = TRUE; 59067131Seric e->e_errormode = EM_MAIL; 5914976Seric 5928238Seric /* 5938238Seric ** Arrange to send to everyone. 5948238Seric ** If sending to multiple people, mail back 5958238Seric ** errors rather than reporting directly. 5968238Seric ** In any case, don't mail back errors for 5978238Seric ** anything that has happened up to 5988238Seric ** now (the other end will do this). 59910197Seric ** Truncate our transcript -- the mail has gotten 60010197Seric ** to us successfully, and if we have 60110197Seric ** to mail this back, it will be easier 60210197Seric ** on the reader. 6038238Seric ** Then send to everyone. 6048238Seric ** Finally give a reply code. If an error has 6058238Seric ** already been given, don't mail a 6068238Seric ** message back. 6079339Seric ** We goose error returns by clearing error bit. 6088238Seric */ 6098238Seric 61024943Seric SmtpPhase = "delivery"; 61155012Seric e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp); 61258714Seric id = e->e_id; 6134976Seric 61467131Seric if (doublequeue) 61559730Seric { 61667131Seric /* make sure it is in the queue */ 61767131Seric queueup(e, TRUE, FALSE); 61868035Seric if (e->e_sendmode == SM_QUEUE) 61968035Seric e->e_flags |= EF_KEEPQUEUE; 62059730Seric } 6218238Seric else 62258919Seric { 62367131Seric /* send to all recipients */ 62467131Seric sendall(e, SM_DEFAULT); 62567131Seric } 62667131Seric e->e_to = NULL; 62759747Seric 62867131Seric /* issue success message */ 62967131Seric message("250 %s Message accepted for delivery", id); 63064296Seric 63167131Seric /* if we just queued, poke it */ 63267131Seric if (doublequeue && e->e_sendmode != SM_QUEUE) 63367131Seric { 63467131Seric extern pid_t dowork(); 63567131Seric 63667131Seric unlockqueue(e); 63767131Seric (void) dowork(id, TRUE, TRUE, e); 63858919Seric } 63958883Seric 64059747Seric abortmessage: 6419339Seric /* if in a child, pop back to our parent */ 6429339Seric if (InChild) 6439339Seric finis(); 64424943Seric 64524943Seric /* clean up a bit */ 64658109Seric gotmail = FALSE; 64755012Seric dropenvelope(e); 64858179Seric CurEnv = e = newenvelope(e, CurEnv); 64955012Seric e->e_flags = BlankEnvelope.e_flags; 6504549Seric break; 6514549Seric 6524549Seric case CMDRSET: /* rset -- reset state */ 65358151Seric message("250 Reset state"); 65464359Seric e->e_flags |= EF_CLRQUEUE; 6559339Seric if (InChild) 6569339Seric finis(); 65758109Seric 65858109Seric /* clean up a bit */ 65958109Seric gotmail = FALSE; 66058109Seric dropenvelope(e); 66158179Seric CurEnv = e = newenvelope(e, CurEnv); 6629339Seric break; 6634549Seric 6644549Seric case CMDVRFY: /* vrfy -- verify address */ 66558092Seric case CMDEXPN: /* expn -- expand address */ 66658092Seric vrfy = c->cmdcode == CMDVRFY; 66758092Seric if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 66858092Seric PrivacyFlags)) 66958082Seric { 67058412Seric if (vrfy) 67167160Seric message("252 Cannot VRFY user; try RCPT to attempt delivery (or try finger)"); 67258412Seric else 67365192Seric message("502 Sorry, we do not allow this operation"); 67465017Seric #ifdef LOG 67565017Seric if (LogLevel > 5) 67665017Seric syslog(LOG_INFO, "%s: %s [rejected]", 67765017Seric CurSmtpClient, inp); 67865017Seric #endif 67958082Seric break; 68058082Seric } 68158082Seric else if (!gothello && 68258092Seric bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 68358092Seric PrivacyFlags)) 68458082Seric { 68558151Seric message("503 I demand that you introduce yourself first"); 68658082Seric break; 68758082Seric } 68858092Seric if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0) 6899339Seric break; 69055173Seric #ifdef LOG 69158020Seric if (LogLevel > 5) 69265017Seric syslog(LOG_INFO, "%s: %s", CurSmtpClient, inp); 69355173Seric #endif 6945003Seric vrfyqueue = NULL; 6957762Seric QuickAbort = TRUE; 69658092Seric if (vrfy) 69758092Seric e->e_flags |= EF_VRFYONLY; 69862373Seric while (*p != '\0' && isascii(*p) && isspace(*p)) 69967615Seric p++; 70062373Seric if (*p == '\0') 70162373Seric { 70262373Seric message("501 Argument required"); 70362373Seric Errors++; 70462373Seric } 70562373Seric else 70662373Seric { 70767990Seric (void) sendtolist(p, NULLADDR, &vrfyqueue, 0, e); 70862373Seric } 7097762Seric if (Errors != 0) 7109339Seric { 7119339Seric if (InChild) 7129339Seric finis(); 7137762Seric break; 7149339Seric } 71562373Seric if (vrfyqueue == NULL) 71662373Seric { 71762373Seric message("554 Nothing to %s", vrfy ? "VRFY" : "EXPN"); 71862373Seric } 7195003Seric while (vrfyqueue != NULL) 7205003Seric { 72163971Seric a = vrfyqueue; 72263971Seric while ((a = a->q_next) != NULL && 72363971Seric bitset(QDONTSEND|QBADADDR, a->q_flags)) 72463971Seric continue; 7257685Seric if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 72658151Seric printvrfyaddr(vrfyqueue, a == NULL); 72763847Seric vrfyqueue = vrfyqueue->q_next; 7285003Seric } 7299339Seric if (InChild) 7309339Seric finis(); 7314549Seric break; 7324549Seric 7334549Seric case CMDHELP: /* help -- give user info */ 7344577Seric help(p); 7354549Seric break; 7364549Seric 7374549Seric case CMDNOOP: /* noop -- do nothing */ 73864122Seric message("250 OK"); 7394549Seric break; 7404549Seric 7414549Seric case CMDQUIT: /* quit -- leave mail */ 74258151Seric message("221 %s closing connection", MyHostName); 74361051Seric 74466283Seric doquit: 74561051Seric /* avoid future 050 messages */ 74666017Seric disconnect(1, e); 74761051Seric 7489339Seric if (InChild) 7499339Seric ExitStat = EX_QUIT; 7504549Seric finis(); 7514549Seric 7528544Seric case CMDVERB: /* set verbose mode */ 75359957Seric if (bitset(PRIV_NOEXPN, PrivacyFlags)) 75459957Seric { 75559957Seric /* this would give out the same info */ 75659957Seric message("502 Verbose unavailable"); 75759957Seric break; 75859957Seric } 7598544Seric Verbose = TRUE; 76058734Seric e->e_sendmode = SM_DELIVER; 76159957Seric message("250 Verbose mode"); 7628544Seric break; 7638544Seric 7649314Seric case CMDONEX: /* doing one transaction only */ 7659378Seric OneXact = TRUE; 76659957Seric message("250 Only one transaction"); 7679314Seric break; 7689314Seric 76936230Skarels # ifdef SMTPDEBUG 7709339Seric case CMDDBGQSHOW: /* show queues */ 7716907Seric printf("Send Queue="); 77255012Seric printaddr(e->e_sendqueue, TRUE); 7735003Seric break; 7747275Seric 7757275Seric case CMDDBGDEBUG: /* set debug mode */ 7767676Seric tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 7777676Seric tTflag(p); 77858151Seric message("200 Debug set"); 7797275Seric break; 7807275Seric 78136230Skarels # else /* not SMTPDEBUG */ 78236230Skarels case CMDDBGQSHOW: /* show queues */ 78336230Skarels case CMDDBGDEBUG: /* set debug mode */ 78464685Seric # endif /* SMTPDEBUG */ 78564685Seric case CMDLOGBOGUS: /* bogus command */ 78636233Skarels # ifdef LOG 78758308Seric if (LogLevel > 0) 78864685Seric syslog(LOG_CRIT, 78958020Seric "\"%s\" command from %s (%s)", 79066005Seric c->cmdname, peerhostname, 79158755Seric anynet_ntoa(&RealHostAddr)); 79236233Skarels # endif 79336230Skarels /* FALL THROUGH */ 79436230Skarels 7954549Seric case CMDERROR: /* unknown command */ 79666283Seric if (++badcommands > MAXBADCOMMANDS) 79766283Seric { 79866283Seric message("421 %s Too many bad commands; closing connection", 79966283Seric MyHostName); 80066283Seric goto doquit; 80166283Seric } 80266283Seric 80358151Seric message("500 Command unrecognized"); 8044549Seric break; 8054549Seric 8064549Seric default: 80736230Skarels errno = 0; 80858151Seric syserr("500 smtp: unknown code %d", c->cmdcode); 8094549Seric break; 8104549Seric } 8114549Seric } 8124549Seric } 8134549Seric /* 8144549Seric ** SKIPWORD -- skip a fixed word. 8154549Seric ** 8164549Seric ** Parameters: 8174549Seric ** p -- place to start looking. 8184549Seric ** w -- word to skip. 8194549Seric ** 8204549Seric ** Returns: 8214549Seric ** p following w. 8224549Seric ** NULL on error. 8234549Seric ** 8244549Seric ** Side Effects: 8254549Seric ** clobbers the p data area. 8264549Seric */ 8274549Seric 8284549Seric static char * 8294549Seric skipword(p, w) 8304549Seric register char *p; 8314549Seric char *w; 8324549Seric { 8334549Seric register char *q; 83466005Seric char *firstp = p; 8354549Seric 8364549Seric /* find beginning of word */ 83758050Seric while (isascii(*p) && isspace(*p)) 8384549Seric p++; 8394549Seric q = p; 8404549Seric 8414549Seric /* find end of word */ 84258050Seric while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 8434549Seric p++; 84458050Seric while (isascii(*p) && isspace(*p)) 8454549Seric *p++ = '\0'; 8464549Seric if (*p != ':') 8474549Seric { 8484549Seric syntax: 84966005Seric message("501 Syntax error in parameters scanning \"%s\"", 85066005Seric firstp); 8514549Seric Errors++; 8524549Seric return (NULL); 8534549Seric } 8544549Seric *p++ = '\0'; 85558050Seric while (isascii(*p) && isspace(*p)) 8564549Seric p++; 8574549Seric 85862373Seric if (*p == '\0') 85962373Seric goto syntax; 86062373Seric 8614549Seric /* see if the input word matches desired word */ 86233725Sbostic if (strcasecmp(q, w)) 8634549Seric goto syntax; 8644549Seric 8654549Seric return (p); 8664549Seric } 8674577Seric /* 86868559Seric ** MAIL_ESMTP_ARGS -- process ESMTP arguments from MAIL line 86968559Seric ** 87068559Seric ** Parameters: 87168559Seric ** kp -- the parameter key. 87268559Seric ** vp -- the value of that parameter. 87368559Seric ** e -- the envelope. 87468559Seric ** 87568559Seric ** Returns: 87668559Seric ** none. 87768559Seric */ 87868559Seric 87968559Seric mail_esmtp_args(kp, vp, e) 88068559Seric char *kp; 88168559Seric char *vp; 88268559Seric ENVELOPE *e; 88368559Seric { 88468559Seric if (strcasecmp(kp, "size") == 0) 88568559Seric { 88668559Seric if (vp == NULL) 88768559Seric { 88868559Seric usrerr("501 SIZE requires a value"); 88968559Seric /* NOTREACHED */ 89068559Seric } 89168559Seric # ifdef __STDC__ 892*68560Seric e->e_msgsize = strtoul(vp, (char **) NULL, 10); 89368559Seric # else 894*68560Seric e->e_msgsize = strtol(vp, (char **) NULL, 10); 89568559Seric # endif 89668559Seric } 89768559Seric else if (strcasecmp(kp, "body") == 0) 89868559Seric { 89968559Seric if (vp == NULL) 90068559Seric { 90168559Seric usrerr("501 BODY requires a value"); 90268559Seric /* NOTREACHED */ 90368559Seric } 90468559Seric if (strcasecmp(vp, "8bitmime") == 0) 90568559Seric { 90668559Seric SevenBitInput = FALSE; 90768559Seric } 90868559Seric else if (strcasecmp(vp, "7bit") == 0) 90968559Seric { 91068559Seric SevenBitInput = TRUE; 91168559Seric } 91268559Seric else 91368559Seric { 91468559Seric usrerr("501 Unknown BODY type %s", 91568559Seric vp); 91668559Seric /* NOTREACHED */ 91768559Seric } 91868559Seric e->e_bodytype = newstr(vp); 91968559Seric } 92068559Seric else if (strcasecmp(kp, "envid") == 0) 92168559Seric { 92268559Seric if (vp == NULL) 92368559Seric { 92468559Seric usrerr("501 ENVID requires a value"); 92568559Seric /* NOTREACHED */ 92668559Seric } 92768559Seric e->e_envid = newstr(vp); 92868559Seric } 92968559Seric else if (strcasecmp(kp, "ret") == 0) 93068559Seric { 93168559Seric if (vp == NULL) 93268559Seric { 93368559Seric usrerr("501 RET requires a value"); 93468559Seric /* NOTREACHED */ 93568559Seric } 93668559Seric e->e_flags |= EF_RET_PARAM; 93768559Seric if (strcasecmp(vp, "hdrs") == 0) 93868559Seric e->e_flags |= EF_NO_BODY_RETN; 93968559Seric else if (strcasecmp(vp, "full") != 0) 94068559Seric { 94168559Seric usrerr("501 Bad argument \"%s\" to RET", vp); 94268559Seric /* NOTREACHED */ 94368559Seric } 94468559Seric } 94568559Seric else 94668559Seric { 94768559Seric usrerr("501 %s parameter unrecognized", kp); 94868559Seric /* NOTREACHED */ 94968559Seric } 95068559Seric } 95168559Seric /* 95267963Seric ** RCPT_ESMTP_ARGS -- process ESMTP arguments from RCPT line 95367963Seric ** 95467963Seric ** Parameters: 95567963Seric ** a -- the address corresponding to the To: parameter. 95667963Seric ** kp -- the parameter key. 95767963Seric ** vp -- the value of that parameter. 95867963Seric ** e -- the envelope. 95967963Seric ** 96067963Seric ** Returns: 96167963Seric ** none. 96267963Seric */ 96367963Seric 96467963Seric rcpt_esmtp_args(a, kp, vp, e) 96567963Seric ADDRESS *a; 96667963Seric char *kp; 96767963Seric char *vp; 96867963Seric ENVELOPE *e; 96967963Seric { 97067963Seric if (strcasecmp(kp, "notify") == 0) 97167963Seric { 97267963Seric char *p; 97367963Seric 97467963Seric if (vp == NULL) 97567963Seric { 97667963Seric usrerr("501 NOTIFY requires a value"); 97767963Seric /* NOTREACHED */ 97867963Seric } 97967963Seric a->q_flags &= ~(QPINGONSUCCESS|QPINGONFAILURE|QPINGONDELAY); 98067963Seric if (strcasecmp(vp, "never") == 0) 98167963Seric return; 98267963Seric for (p = vp; p != NULL; vp = p) 98367963Seric { 98467963Seric p = strchr(p, ','); 98567963Seric if (p != NULL) 98667963Seric *p++ = '\0'; 98767963Seric if (strcasecmp(vp, "success") == 0) 98867963Seric a->q_flags |= QPINGONSUCCESS; 98967963Seric else if (strcasecmp(vp, "failure") == 0) 99067963Seric a->q_flags |= QPINGONFAILURE; 99167963Seric else if (strcasecmp(vp, "delay") == 0) 99267963Seric a->q_flags |= QPINGONDELAY; 99367963Seric else 99467963Seric { 99567963Seric usrerr("501 Bad argument \"%s\" to NOTIFY", 99667963Seric vp); 99767963Seric /* NOTREACHED */ 99867963Seric } 99967963Seric } 100067963Seric } 100167963Seric else if (strcasecmp(kp, "orcpt") == 0) 100267963Seric { 100367963Seric if (vp == NULL) 100467963Seric { 100567963Seric usrerr("501 ORCPT requires a value"); 100667963Seric /* NOTREACHED */ 100767963Seric } 100867963Seric a->q_orcpt = newstr(vp); 100967963Seric } 101067963Seric else 101167963Seric { 101267963Seric usrerr("501 %s parameter unrecognized", kp); 101367963Seric /* NOTREACHED */ 101467963Seric } 101567963Seric } 101667963Seric /* 101758151Seric ** PRINTVRFYADDR -- print an entry in the verify queue 101858151Seric ** 101958151Seric ** Parameters: 102058151Seric ** a -- the address to print 102158151Seric ** last -- set if this is the last one. 102258151Seric ** 102358151Seric ** Returns: 102458151Seric ** none. 102558151Seric ** 102658151Seric ** Side Effects: 102758151Seric ** Prints the appropriate 250 codes. 102858151Seric */ 102958151Seric 103058151Seric printvrfyaddr(a, last) 103158151Seric register ADDRESS *a; 103258151Seric bool last; 103358151Seric { 103458151Seric char fmtbuf[20]; 103558151Seric 103658151Seric strcpy(fmtbuf, "250"); 103758151Seric fmtbuf[3] = last ? ' ' : '-'; 103858151Seric 103959746Seric if (a->q_fullname == NULL) 104059746Seric { 104159746Seric if (strchr(a->q_user, '@') == NULL) 104259746Seric strcpy(&fmtbuf[4], "<%s@%s>"); 104359746Seric else 104459746Seric strcpy(&fmtbuf[4], "<%s>"); 104559746Seric message(fmtbuf, a->q_user, MyHostName); 104659746Seric } 104758151Seric else 104858151Seric { 104959746Seric if (strchr(a->q_user, '@') == NULL) 105059746Seric strcpy(&fmtbuf[4], "%s <%s@%s>"); 105159746Seric else 105259746Seric strcpy(&fmtbuf[4], "%s <%s>"); 105359746Seric message(fmtbuf, a->q_fullname, a->q_user, MyHostName); 105458151Seric } 105558151Seric } 105658151Seric /* 10574577Seric ** HELP -- implement the HELP command. 10584577Seric ** 10594577Seric ** Parameters: 10604577Seric ** topic -- the topic we want help for. 10614577Seric ** 10624577Seric ** Returns: 10634577Seric ** none. 10644577Seric ** 10654577Seric ** Side Effects: 10664577Seric ** outputs the help file to message output. 10674577Seric */ 10684577Seric 10694577Seric help(topic) 10704577Seric char *topic; 10714577Seric { 10724577Seric register FILE *hf; 10734577Seric int len; 10744577Seric char buf[MAXLINE]; 10754577Seric bool noinfo; 10764577Seric 10778269Seric if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL) 10784577Seric { 10794577Seric /* no help */ 108011931Seric errno = 0; 108158151Seric message("502 HELP not implemented"); 10824577Seric return; 10834577Seric } 10844577Seric 108549669Seric if (topic == NULL || *topic == '\0') 108649669Seric topic = "smtp"; 108749669Seric else 108849669Seric makelower(topic); 108949669Seric 10904577Seric len = strlen(topic); 10914577Seric noinfo = TRUE; 10924577Seric 10934577Seric while (fgets(buf, sizeof buf, hf) != NULL) 10944577Seric { 10954577Seric if (strncmp(buf, topic, len) == 0) 10964577Seric { 10974577Seric register char *p; 10984577Seric 109956795Seric p = strchr(buf, '\t'); 11004577Seric if (p == NULL) 11014577Seric p = buf; 11024577Seric else 11034577Seric p++; 11044577Seric fixcrlf(p, TRUE); 110558151Seric message("214-%s", p); 11064577Seric noinfo = FALSE; 11074577Seric } 11084577Seric } 11094577Seric 11104577Seric if (noinfo) 111158151Seric message("504 HELP topic unknown"); 11124577Seric else 111358151Seric message("214 End of HELP info"); 11144628Seric (void) fclose(hf); 11154577Seric } 11168544Seric /* 11179339Seric ** RUNINCHILD -- return twice -- once in the child, then in the parent again 11189339Seric ** 11199339Seric ** Parameters: 11209339Seric ** label -- a string used in error messages 11219339Seric ** 11229339Seric ** Returns: 11239339Seric ** zero in the child 11249339Seric ** one in the parent 11259339Seric ** 11269339Seric ** Side Effects: 11279339Seric ** none. 11289339Seric */ 11298544Seric 113055012Seric runinchild(label, e) 11319339Seric char *label; 113255012Seric register ENVELOPE *e; 11339339Seric { 11349339Seric int childpid; 11359339Seric 113616158Seric if (!OneXact) 11379339Seric { 113816158Seric childpid = dofork(); 113916158Seric if (childpid < 0) 114016158Seric { 114116158Seric syserr("%s: cannot fork", label); 114216158Seric return (1); 114316158Seric } 114416158Seric if (childpid > 0) 114516158Seric { 114616158Seric auto int st; 11479339Seric 114816158Seric /* parent -- wait for child to complete */ 114961093Seric setproctitle("server %s child wait", CurHostName); 115016158Seric st = waitfor(childpid); 115116158Seric if (st == -1) 115216158Seric syserr("%s: lost child", label); 115364948Seric else if (!WIFEXITED(st)) 115464948Seric syserr("%s: died on signal %d", 115564948Seric label, st & 0177); 11569339Seric 115716158Seric /* if we exited on a QUIT command, complete the process */ 115866017Seric if (WEXITSTATUS(st) == EX_QUIT) 115966017Seric { 116066017Seric disconnect(1, e); 116116158Seric finis(); 116266017Seric } 11639339Seric 116416158Seric return (1); 116516158Seric } 116616158Seric else 116716158Seric { 116816158Seric /* child */ 116916158Seric InChild = TRUE; 117025050Seric QuickAbort = FALSE; 117155012Seric clearenvelope(e, FALSE); 117216158Seric } 11739339Seric } 117415256Seric 117516158Seric /* open alias database */ 117660537Seric initmaps(FALSE, e); 117716158Seric 117816158Seric return (0); 11799339Seric } 11809339Seric 118156795Seric # endif /* SMTP */ 1182