122716Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362532Sbostic * Copyright (c) 1988, 1993 462532Sbostic * The Regents of the University of California. All rights reserved. 533731Sbostic * 642831Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822716Sdist 933731Sbostic # include "sendmail.h" 1022716Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef SMTP 13*68075Seric static char sccsid[] = "@(#)usersmtp.c 8.32 (Berkeley) 12/10/94 (with SMTP)"; 1433731Sbostic #else 15*68075Seric static char sccsid[] = "@(#)usersmtp.c 8.32 (Berkeley) 12/10/94 (without SMTP)"; 1633731Sbostic #endif 1733731Sbostic #endif /* not lint */ 1833731Sbostic 194684Seric # include <sysexits.h> 2021065Seric # include <errno.h> 214684Seric 2233731Sbostic # ifdef SMTP 234684Seric 244684Seric /* 259391Seric ** USERSMTP -- run SMTP protocol from the user end. 269391Seric ** 279391Seric ** This protocol is described in RFC821. 289391Seric */ 299391Seric 309391Seric #define REPLYTYPE(r) ((r) / 100) /* first digit of reply code */ 319391Seric #define REPLYCLASS(r) (((r) / 10) % 10) /* second digit of reply code */ 329391Seric #define SMTPCLOSING 421 /* "Service Shutting Down" */ 339391Seric 3414900Seric char SmtpMsgBuffer[MAXLINE]; /* buffer for commands */ 3510054Seric char SmtpReplyBuffer[MAXLINE]; /* buffer for replies */ 3621065Seric char SmtpError[MAXLINE] = ""; /* save failure error messages */ 3710054Seric int SmtpPid; /* pid of mailer */ 3864071Seric bool SmtpNeedIntro; /* need "while talking" in transcript */ 3958671Seric 4058671Seric #ifdef __STDC__ 4158671Seric extern smtpmessage(char *f, MAILER *m, MCI *mci, ...); 4258671Seric #endif 439391Seric /* 444865Seric ** SMTPINIT -- initialize SMTP. 454684Seric ** 464865Seric ** Opens the connection and sends the initial protocol. 474684Seric ** 484684Seric ** Parameters: 494865Seric ** m -- mailer to create connection to. 504865Seric ** pvp -- pointer to parameter vector to pass to 514865Seric ** the mailer. 524684Seric ** 534684Seric ** Returns: 5454967Seric ** none. 554684Seric ** 564684Seric ** Side Effects: 574865Seric ** creates connection and sends initial protocol. 584684Seric */ 594684Seric 6054967Seric smtpinit(m, mci, e) 614865Seric struct mailer *m; 6254967Seric register MCI *mci; 6353751Seric ENVELOPE *e; 644684Seric { 654865Seric register int r; 6658957Seric register char *p; 6760210Seric extern void esmtp_check(); 6859285Seric extern void helo_options(); 694684Seric 7063753Seric if (tTd(18, 1)) 7157379Seric { 7257379Seric printf("smtpinit "); 7364731Seric mci_dump(mci, FALSE); 7457379Seric } 7557379Seric 764865Seric /* 774865Seric ** Open the connection to the mailer. 784865Seric */ 794684Seric 8021065Seric SmtpError[0] = '\0'; 8157379Seric CurHostName = mci->mci_host; /* XXX UGLY XXX */ 8264071Seric SmtpNeedIntro = TRUE; 8354967Seric switch (mci->mci_state) 846051Seric { 8554967Seric case MCIS_ACTIVE: 8654967Seric /* need to clear old information */ 8754967Seric smtprset(m, mci, e); 8857734Seric /* fall through */ 8915139Seric 9054967Seric case MCIS_OPEN: 9154967Seric return; 9254967Seric 9354967Seric case MCIS_ERROR: 9454967Seric case MCIS_SSD: 9554967Seric /* shouldn't happen */ 9654967Seric smtpquit(m, mci, e); 9757734Seric /* fall through */ 9854967Seric 9954967Seric case MCIS_CLOSED: 10058151Seric syserr("451 smtpinit: state CLOSED"); 10154967Seric return; 10254967Seric 10354967Seric case MCIS_OPENING: 10454967Seric break; 1056051Seric } 1064796Seric 10754967Seric mci->mci_state = MCIS_OPENING; 10854967Seric 1094865Seric /* 1104865Seric ** Get the greeting message. 11114913Seric ** This should appear spontaneously. Give it five minutes to 11214886Seric ** happen. 1134865Seric */ 1144797Seric 11561093Seric SmtpPhase = mci->mci_phase = "client greeting"; 11653751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 11760210Seric r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check); 11864750Seric if (r < 0 || REPLYTYPE(r) == 4) 11952104Seric goto tempfail1; 12064750Seric if (REPLYTYPE(r) != 2) 12164750Seric goto unavailable; 1224684Seric 1234865Seric /* 1244976Seric ** Send the HELO command. 1257963Seric ** My mother taught me to always introduce myself. 1264976Seric */ 1274976Seric 12859285Seric if (bitnset(M_ESMTP, m->m_flags)) 12959285Seric mci->mci_flags |= MCIF_ESMTP; 13059285Seric 13159285Seric tryhelo: 13259285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 13359285Seric { 13459285Seric smtpmessage("EHLO %s", m, mci, MyHostName); 13561093Seric SmtpPhase = mci->mci_phase = "client EHLO"; 13659285Seric } 13759285Seric else 13859285Seric { 13959285Seric smtpmessage("HELO %s", m, mci, MyHostName); 14061093Seric SmtpPhase = mci->mci_phase = "client HELO"; 14159285Seric } 14253751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 14359285Seric r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 1448005Seric if (r < 0) 14552104Seric goto tempfail1; 1468005Seric else if (REPLYTYPE(r) == 5) 14759285Seric { 14859285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 14959285Seric { 15059285Seric /* try old SMTP instead */ 15159285Seric mci->mci_flags &= ~MCIF_ESMTP; 15259285Seric goto tryhelo; 15359285Seric } 15414913Seric goto unavailable; 15559285Seric } 1567963Seric else if (REPLYTYPE(r) != 2) 15752104Seric goto tempfail1; 1584976Seric 1594976Seric /* 16058957Seric ** Check to see if we actually ended up talking to ourself. 16158957Seric ** This means we didn't know about an alias or MX, or we managed 16258957Seric ** to connect to an echo server. 16358957Seric */ 16458957Seric 16559026Seric p = strchr(&SmtpReplyBuffer[4], ' '); 16658957Seric if (p != NULL) 16761707Seric *p = '\0'; 16867472Seric if (!bitnset(M_NOLOOPCHECK, m->m_flags) && 16967472Seric strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0) 17058957Seric { 17158957Seric syserr("553 %s config error: mail loops back to myself", 17258957Seric MyHostName); 17358957Seric mci->mci_exitstat = EX_CONFIG; 17458957Seric mci->mci_errno = 0; 17558957Seric smtpquit(m, mci, e); 17658957Seric return; 17758957Seric } 17858957Seric 17958957Seric /* 1809315Seric ** If this is expected to be another sendmail, send some internal 1819315Seric ** commands. 1829315Seric */ 1839315Seric 18410688Seric if (bitnset(M_INTERNAL, m->m_flags)) 1859315Seric { 1869315Seric /* tell it to be verbose */ 18753751Seric smtpmessage("VERB", m, mci); 18859285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 1899315Seric if (r < 0) 19052104Seric goto tempfail2; 1919315Seric } 1929315Seric 19365057Seric if (mci->mci_state != MCIS_CLOSED) 19465057Seric { 19565057Seric mci->mci_state = MCIS_OPEN; 19665057Seric return; 19765057Seric } 19853751Seric 19965057Seric /* got a 421 error code during startup */ 20065057Seric 20153751Seric tempfail1: 20253751Seric tempfail2: 20353751Seric mci->mci_exitstat = EX_TEMPFAIL; 20457379Seric if (mci->mci_errno == 0) 20557379Seric mci->mci_errno = errno; 20657379Seric if (mci->mci_state != MCIS_CLOSED) 20757379Seric smtpquit(m, mci, e); 20854967Seric return; 20953751Seric 21053751Seric unavailable: 21153751Seric mci->mci_exitstat = EX_UNAVAILABLE; 21253751Seric mci->mci_errno = errno; 21353751Seric smtpquit(m, mci, e); 21454967Seric return; 21553751Seric } 21659285Seric /* 21760210Seric ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 21860210Seric ** 21960210Seric ** 22060210Seric ** Parameters: 22160210Seric ** line -- the response line. 22267893Seric ** firstline -- set if this is the first line of the reply. 22360210Seric ** m -- the mailer. 22460210Seric ** mci -- the mailer connection info. 22560210Seric ** e -- the envelope. 22660210Seric ** 22760210Seric ** Returns: 22860210Seric ** none. 22960210Seric */ 23060210Seric 23160210Seric void 23267893Seric esmtp_check(line, firstline, m, mci, e) 23360210Seric char *line; 23467893Seric bool firstline; 23560210Seric MAILER *m; 23660210Seric register MCI *mci; 23760210Seric ENVELOPE *e; 23860210Seric { 23967893Seric while ((line = strchr(++line, 'E')) != NULL) 24067893Seric { 24167893Seric if (strncmp(line, "ESMTP ", 6) == 0) 24267893Seric { 24367893Seric mci->mci_flags |= MCIF_ESMTP; 24467893Seric break; 24567893Seric } 24667893Seric } 24760210Seric } 24860210Seric /* 24959285Seric ** HELO_OPTIONS -- process the options on a HELO line. 25059285Seric ** 25159285Seric ** Parameters: 25259285Seric ** line -- the response line. 25367893Seric ** firstline -- set if this is the first line of the reply. 25459285Seric ** m -- the mailer. 25559285Seric ** mci -- the mailer connection info. 25659285Seric ** e -- the envelope. 25759285Seric ** 25859285Seric ** Returns: 25959285Seric ** none. 26059285Seric */ 26153751Seric 26259285Seric void 26367893Seric helo_options(line, firstline, m, mci, e) 26459285Seric char *line; 26567893Seric bool firstline; 26659285Seric MAILER *m; 26759285Seric register MCI *mci; 26859285Seric ENVELOPE *e; 26959285Seric { 27059285Seric register char *p; 27159285Seric 27267971Seric if (firstline) 27367893Seric return; 27467893Seric 27559285Seric if (strlen(line) < 5) 27659285Seric return; 27759285Seric line += 4; 27859285Seric p = strchr(line, ' '); 27959285Seric if (p != NULL) 28059285Seric *p++ = '\0'; 28159285Seric if (strcasecmp(line, "size") == 0) 28259285Seric { 28359285Seric mci->mci_flags |= MCIF_SIZE; 28459285Seric if (p != NULL) 28559285Seric mci->mci_maxsize = atol(p); 28659285Seric } 28759285Seric else if (strcasecmp(line, "8bitmime") == 0) 28865870Seric { 28959285Seric mci->mci_flags |= MCIF_8BITMIME; 29065870Seric mci->mci_flags &= ~MCIF_7BIT; 29165870Seric } 29259285Seric else if (strcasecmp(line, "expn") == 0) 29359285Seric mci->mci_flags |= MCIF_EXPN; 29467963Seric else if (strcasecmp(line, "x-dsn-1") == 0) 29567880Seric mci->mci_flags |= MCIF_DSN; 29659285Seric } 29759285Seric /* 29859285Seric ** SMTPMAILFROM -- send MAIL command 29959285Seric ** 30059285Seric ** Parameters: 30159285Seric ** m -- the mailer. 30259285Seric ** mci -- the mailer connection structure. 30359285Seric ** e -- the envelope (including the sender to specify). 30459285Seric */ 30559285Seric 30653751Seric smtpmailfrom(m, mci, e) 30753751Seric struct mailer *m; 30854967Seric MCI *mci; 30953751Seric ENVELOPE *e; 31053751Seric { 31153751Seric int r; 31265494Seric char *bufp; 31367887Seric char *bodytype; 31453751Seric char buf[MAXNAME]; 31559285Seric char optbuf[MAXLINE]; 31653751Seric 31763753Seric if (tTd(18, 2)) 31857943Seric printf("smtpmailfrom: CurHost=%s\n", CurHostName); 31957943Seric 32059285Seric /* set up appropriate options to include */ 32164254Seric if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0) 32259285Seric sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 32359285Seric else 32459285Seric strcpy(optbuf, ""); 32559285Seric 32667887Seric bodytype = e->e_bodytype; 32767887Seric if (bitset(MCIF_8BITMIME, mci->mci_flags)) 32867417Seric { 32967887Seric if (bodytype == NULL && 33067887Seric bitset(MM_MIME8BIT, MimeMode) && 33167887Seric bitset(EF_HAS8BIT, e->e_flags) && 33267887Seric !bitnset(M_8BITS, m->m_flags)) 33367887Seric bodytype = "8BITMIME"; 33467887Seric if (bodytype != NULL) 33567417Seric { 33667417Seric strcat(optbuf, " BODY="); 33767887Seric strcat(optbuf, bodytype); 33867417Seric } 33967417Seric } 34067995Seric else if (bitnset(M_8BITS, m->m_flags) || 34167995Seric !bitset(EF_HAS8BIT, e->e_flags) || 34267995Seric (e->e_bodytype != NULL && 34367995Seric strcasecmp(e->e_bodytype, "7bit") == 0)) 34467887Seric { 34567887Seric /* just pass it through */ 34667887Seric } 34767887Seric else if (bitset(MM_CVTMIME, MimeMode) && 34867995Seric (e->e_bodytype != NULL || !bitset(MM_PASS8BIT, MimeMode))) 34967887Seric { 35067887Seric /* must convert from 8bit MIME format to 7bit encoded */ 35167887Seric mci->mci_flags |= MCIF_CVT8TO7; 35267887Seric } 35367887Seric else if (!bitset(MM_PASS8BIT, MimeMode)) 35467887Seric { 35567887Seric /* cannot just send a 8-bit version */ 35667887Seric usrerr("%s does not support 8BITMIME", mci->mci_host); 35767887Seric return EX_DATAERR; 35867887Seric } 35967417Seric 36067963Seric if (bitset(MCIF_DSN, mci->mci_flags)) 36167880Seric { 36267963Seric if (e->e_envid != NULL) 36367963Seric { 36467963Seric strcat(optbuf, " ENVID="); 36567963Seric strcat(optbuf, e->e_envid); 36667963Seric } 36767963Seric if (e->e_omts != NULL) 36867963Seric { 36967963Seric strcat(optbuf, " OMTS="); 37067963Seric strcat(optbuf, e->e_omts); 37167963Seric } 37267880Seric } 37367880Seric 3749315Seric /* 3754865Seric ** Send the MAIL command. 3764865Seric ** Designates the sender. 3774865Seric */ 3784796Seric 37953751Seric mci->mci_state = MCIS_ACTIVE; 38053751Seric 38159540Seric if (bitset(EF_RESPONSE, e->e_flags) && 38259540Seric !bitnset(M_NO_NULL_FROM, m->m_flags)) 38358680Seric (void) strcpy(buf, ""); 38458680Seric else 38558680Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 38665494Seric if (buf[0] == '<') 38765494Seric { 38865494Seric /* strip off <angle brackets> (put back on below) */ 38965494Seric bufp = &buf[strlen(buf) - 1]; 39065494Seric if (*bufp == '>') 39165494Seric *bufp = '\0'; 39265494Seric bufp = &buf[1]; 39365494Seric } 39465494Seric else 39565494Seric bufp = buf; 39667472Seric if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) || 39710688Seric !bitnset(M_FROMPATH, m->m_flags)) 3988436Seric { 39965494Seric smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf); 4008436Seric } 4018436Seric else 4028436Seric { 40359285Seric smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 40465494Seric *bufp == '@' ? ',' : ':', bufp, optbuf); 4058436Seric } 40661093Seric SmtpPhase = mci->mci_phase = "client MAIL"; 40753751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 40859285Seric r = reply(m, mci, e, TimeOuts.to_mail, NULL); 4098005Seric if (r < 0 || REPLYTYPE(r) == 4) 41053751Seric { 41153751Seric mci->mci_exitstat = EX_TEMPFAIL; 41253751Seric mci->mci_errno = errno; 41353751Seric smtpquit(m, mci, e); 41453751Seric return EX_TEMPFAIL; 41553751Seric } 4167963Seric else if (r == 250) 41753751Seric { 41853751Seric return EX_OK; 41953751Seric } 420*68075Seric else if (r == 501 || r == 553) 421*68075Seric { 422*68075Seric /* syntax error in arguments */ 423*68075Seric smtpquit(m, mci, e); 424*68075Seric return EX_DATAERR; 425*68075Seric } 4267963Seric else if (r == 552) 42753751Seric { 42853751Seric /* signal service unavailable */ 42953751Seric smtpquit(m, mci, e); 43053751Seric return EX_UNAVAILABLE; 43153751Seric } 43214913Seric 43358008Seric #ifdef LOG 43458020Seric if (LogLevel > 1) 43558008Seric { 43667860Seric syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s", 43767860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 43858008Seric } 43958008Seric #endif 44058008Seric 44114913Seric /* protocol error -- close up */ 44253751Seric smtpquit(m, mci, e); 44353751Seric return EX_PROTOCOL; 4444684Seric } 4454684Seric /* 4464976Seric ** SMTPRCPT -- designate recipient. 4474797Seric ** 4484797Seric ** Parameters: 4494865Seric ** to -- address of recipient. 45010175Seric ** m -- the mailer we are sending to. 45157379Seric ** mci -- the connection info for this transaction. 45257379Seric ** e -- the envelope for this transaction. 4534797Seric ** 4544797Seric ** Returns: 4554865Seric ** exit status corresponding to recipient status. 4564797Seric ** 4574797Seric ** Side Effects: 4584865Seric ** Sends the mail via SMTP. 4594797Seric */ 4604797Seric 46153751Seric smtprcpt(to, m, mci, e) 4624865Seric ADDRESS *to; 46310175Seric register MAILER *m; 46454967Seric MCI *mci; 46553751Seric ENVELOPE *e; 4664797Seric { 4674797Seric register int r; 46867880Seric char optbuf[MAXLINE]; 4694797Seric 47067880Seric strcpy(optbuf, ""); 47167880Seric if (bitset(MCIF_DSN, mci->mci_flags)) 47267880Seric { 47367963Seric bool firstone = TRUE; 47467963Seric 47567987Seric /* NOTIFY= parameter */ 47667880Seric strcat(optbuf, " NOTIFY="); 47767987Seric if (bitset(QPINGONSUCCESS, to->q_flags)) 47867880Seric { 47967987Seric strcat(optbuf, "SUCCESS"); 48067963Seric firstone = FALSE; 48167880Seric } 48267987Seric if (bitset(QPINGONFAILURE, to->q_flags)) 48367880Seric { 48467971Seric if (!firstone) 48567963Seric strcat(optbuf, ","); 48667987Seric strcat(optbuf, "FAILURE"); 48767963Seric firstone = FALSE; 48867880Seric } 48967963Seric if (bitset(QPINGONDELAY, to->q_flags)) 49067880Seric { 49167971Seric if (!firstone) 49267963Seric strcat(optbuf, ","); 49367963Seric strcat(optbuf, "DELAY"); 49467963Seric firstone = FALSE; 49567963Seric } 49667963Seric if (firstone) 49767963Seric strcat(optbuf, "NEVER"); 49867963Seric 49967987Seric /* RET= parameter */ 50067963Seric if (bitset(QHAS_RET_PARAM, to->q_flags)) 50167963Seric { 50267880Seric strcat(optbuf, " RET="); 50367963Seric if (bitset(QRET_HDRS, to->q_flags)) 50467963Seric strcat(optbuf, "HDRS"); 50567880Seric else 50667963Seric strcat(optbuf, "FULL"); 50767880Seric } 50867987Seric 50967987Seric /* ORCPT= parameter */ 51067987Seric if (to->q_orcpt != NULL) 51167987Seric { 51267987Seric strcat(optbuf, " ORCPT="); 51367987Seric strcat(optbuf, to->q_orcpt); 51467987Seric } 51567880Seric } 51667880Seric else if (bitset(QPINGONSUCCESS, to->q_flags)) 51767880Seric { 51867880Seric to->q_flags |= QRELAYED; 51967880Seric fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n", 52067880Seric to->q_paddr); 52167880Seric } 5224865Seric 52367880Seric smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf); 52467880Seric 52561093Seric SmtpPhase = mci->mci_phase = "client RCPT"; 52653751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 52759285Seric r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 52867880Seric setstatus(to, SmtpReplyBuffer); 5298005Seric if (r < 0 || REPLYTYPE(r) == 4) 5304865Seric return (EX_TEMPFAIL); 5317963Seric else if (REPLYTYPE(r) == 2) 5327963Seric return (EX_OK); 5337964Seric else if (r == 550 || r == 551 || r == 553) 5347964Seric return (EX_NOUSER); 5357964Seric else if (r == 552 || r == 554) 5367964Seric return (EX_UNAVAILABLE); 53758008Seric 53858008Seric #ifdef LOG 53958020Seric if (LogLevel > 1) 54058008Seric { 54167860Seric syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s", 54267860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 54358008Seric } 54458008Seric #endif 54558008Seric 5467964Seric return (EX_PROTOCOL); 5474797Seric } 5484797Seric /* 54910175Seric ** SMTPDATA -- send the data and clean up the transaction. 5504684Seric ** 5514684Seric ** Parameters: 5524865Seric ** m -- mailer being sent to. 5536980Seric ** e -- the envelope for this message. 5544684Seric ** 5554684Seric ** Returns: 5564976Seric ** exit status corresponding to DATA command. 5574684Seric ** 5584684Seric ** Side Effects: 5594865Seric ** none. 5604684Seric */ 5614684Seric 56263753Seric static jmp_buf CtxDataTimeout; 56363937Seric static int datatimeout(); 56463753Seric 56553740Seric smtpdata(m, mci, e) 5664865Seric struct mailer *m; 56754967Seric register MCI *mci; 5686980Seric register ENVELOPE *e; 5694684Seric { 5704684Seric register int r; 57163753Seric register EVENT *ev; 57263753Seric time_t timeout; 5734684Seric 5744797Seric /* 5754797Seric ** Send the data. 57610175Seric ** First send the command and check that it is ok. 57710175Seric ** Then send the data. 57810175Seric ** Follow it up with a dot to terminate. 57910175Seric ** Finally get the results of the transaction. 5804797Seric */ 5814797Seric 58210175Seric /* send the command and check ok to proceed */ 58353751Seric smtpmessage("DATA", m, mci); 58461093Seric SmtpPhase = mci->mci_phase = "client DATA 354"; 58553751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 58659285Seric r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 5878005Seric if (r < 0 || REPLYTYPE(r) == 4) 58857990Seric { 58957990Seric smtpquit(m, mci, e); 5904797Seric return (EX_TEMPFAIL); 59157990Seric } 5927963Seric else if (r == 554) 59357990Seric { 59457990Seric smtprset(m, mci, e); 5957963Seric return (EX_UNAVAILABLE); 59657990Seric } 5977963Seric else if (r != 354) 59857990Seric { 59958008Seric #ifdef LOG 60058020Seric if (LogLevel > 1) 60158008Seric { 60267860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s", 60367860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 60458008Seric } 60558008Seric #endif 60657990Seric smtprset(m, mci, e); 6077964Seric return (EX_PROTOCOL); 60857990Seric } 60910175Seric 61063757Seric /* 61163757Seric ** Set timeout around data writes. Make it at least large 61263757Seric ** enough for DNS timeouts on all recipients plus some fudge 61363757Seric ** factor. The main thing is that it should not be infinite. 61463757Seric */ 61563757Seric 61663753Seric if (setjmp(CtxDataTimeout) != 0) 61763753Seric { 61863753Seric mci->mci_errno = errno; 61963753Seric mci->mci_exitstat = EX_TEMPFAIL; 62063753Seric mci->mci_state = MCIS_ERROR; 62163753Seric syserr("451 timeout writing message to %s", mci->mci_host); 62263753Seric smtpquit(m, mci, e); 62363753Seric return EX_TEMPFAIL; 62463753Seric } 62563753Seric 62663787Seric timeout = e->e_msgsize / 16; 62763787Seric if (timeout < (time_t) 60) 62863787Seric timeout = (time_t) 60; 62963787Seric timeout += e->e_nrcpts * 90; 63063753Seric ev = setevent(timeout, datatimeout, 0); 63163753Seric 63267546Seric /* 63367546Seric ** Output the actual message. 63467546Seric */ 63567546Seric 63667936Seric (*e->e_puthdr)(mci, e->e_header, e, 0); 63767936Seric (*e->e_putbody)(mci, e, NULL, 0); 63810175Seric 63967546Seric /* 64067546Seric ** Cleanup after sending message. 64167546Seric */ 64267546Seric 64363753Seric clrevent(ev); 64463753Seric 64564718Seric if (ferror(mci->mci_out)) 64664718Seric { 64764718Seric /* error during processing -- don't send the dot */ 64864718Seric mci->mci_errno = EIO; 64964718Seric mci->mci_exitstat = EX_IOERR; 65064718Seric mci->mci_state = MCIS_ERROR; 65164718Seric smtpquit(m, mci, e); 65264718Seric return EX_IOERR; 65364718Seric } 65464718Seric 65510175Seric /* terminate the message */ 65653740Seric fprintf(mci->mci_out, ".%s", m->m_eol); 65763753Seric if (TrafficLogFile != NULL) 65863753Seric fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 65958120Seric if (Verbose) 66058151Seric nmessage(">>> ."); 66110175Seric 66210175Seric /* check for the results of the transaction */ 66361093Seric SmtpPhase = mci->mci_phase = "client DATA 250"; 66453751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 66559285Seric r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 66653751Seric if (r < 0) 66757990Seric { 66857990Seric smtpquit(m, mci, e); 6694797Seric return (EX_TEMPFAIL); 67057990Seric } 67153751Seric mci->mci_state = MCIS_OPEN; 67258917Seric e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 67353751Seric if (REPLYTYPE(r) == 4) 67453751Seric return (EX_TEMPFAIL); 6757963Seric else if (r == 250) 6767963Seric return (EX_OK); 6777963Seric else if (r == 552 || r == 554) 6787963Seric return (EX_UNAVAILABLE); 67958008Seric #ifdef LOG 68058020Seric if (LogLevel > 1) 68158008Seric { 68267860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s", 68367860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 68458008Seric } 68558008Seric #endif 6867964Seric return (EX_PROTOCOL); 6874684Seric } 68863753Seric 68963753Seric 69063753Seric static int 69163753Seric datatimeout() 69263753Seric { 69363753Seric longjmp(CtxDataTimeout, 1); 69463753Seric } 6954684Seric /* 6964865Seric ** SMTPQUIT -- close the SMTP connection. 6974865Seric ** 6984865Seric ** Parameters: 69915535Seric ** m -- a pointer to the mailer. 7004865Seric ** 7014865Seric ** Returns: 7024865Seric ** none. 7034865Seric ** 7044865Seric ** Side Effects: 7054865Seric ** sends the final protocol and closes the connection. 7064865Seric */ 7074865Seric 70853751Seric smtpquit(m, mci, e) 70953751Seric register MAILER *m; 71054967Seric register MCI *mci; 71153751Seric ENVELOPE *e; 7124865Seric { 71364822Seric bool oldSuprErrs = SuprErrs; 7144865Seric 71564822Seric /* 71664822Seric ** Suppress errors here -- we may be processing a different 71764822Seric ** job when we do the quit connection, and we don't want the 71864822Seric ** new job to be penalized for something that isn't it's 71964822Seric ** problem. 72064822Seric */ 72164822Seric 72264822Seric SuprErrs = TRUE; 72364822Seric 72454967Seric /* send the quit message if we haven't gotten I/O error */ 72553751Seric if (mci->mci_state != MCIS_ERROR) 7269391Seric { 72761093Seric SmtpPhase = "client QUIT"; 72853751Seric smtpmessage("QUIT", m, mci); 72959285Seric (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 73064822Seric SuprErrs = oldSuprErrs; 73153740Seric if (mci->mci_state == MCIS_CLOSED) 73264822Seric { 73364822Seric SuprErrs = oldSuprErrs; 73410159Seric return; 73564822Seric } 7369391Seric } 7379391Seric 73852676Seric /* now actually close the connection and pick up the zombie */ 73965194Seric (void) endmailer(mci, e, NULL); 74064822Seric 74164822Seric SuprErrs = oldSuprErrs; 7424865Seric } 7434865Seric /* 74454967Seric ** SMTPRSET -- send a RSET (reset) command 74554967Seric */ 74654967Seric 74754967Seric smtprset(m, mci, e) 74854967Seric register MAILER *m; 74954967Seric register MCI *mci; 75054967Seric ENVELOPE *e; 75154967Seric { 75254967Seric int r; 75354967Seric 75461093Seric SmtpPhase = "client RSET"; 75554967Seric smtpmessage("RSET", m, mci); 75659285Seric r = reply(m, mci, e, TimeOuts.to_rset, NULL); 75757734Seric if (r < 0) 75857734Seric mci->mci_state = MCIS_ERROR; 75954967Seric else if (REPLYTYPE(r) == 2) 76057734Seric { 76157734Seric mci->mci_state = MCIS_OPEN; 76257734Seric return; 76357734Seric } 76457734Seric smtpquit(m, mci, e); 76554967Seric } 76654967Seric /* 76758867Seric ** SMTPPROBE -- check the connection state 76854967Seric */ 76954967Seric 77058867Seric smtpprobe(mci) 77154967Seric register MCI *mci; 77254967Seric { 77354967Seric int r; 77454967Seric MAILER *m = mci->mci_mailer; 77554967Seric extern ENVELOPE BlankEnvelope; 77654967Seric ENVELOPE *e = &BlankEnvelope; 77754967Seric 77861093Seric SmtpPhase = "client probe"; 77958867Seric smtpmessage("RSET", m, mci); 78059285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 78158061Seric if (r < 0 || REPLYTYPE(r) != 2) 78254967Seric smtpquit(m, mci, e); 78354967Seric return r; 78454967Seric } 78554967Seric /* 7864684Seric ** REPLY -- read arpanet reply 7874684Seric ** 7884684Seric ** Parameters: 78910175Seric ** m -- the mailer we are reading the reply from. 79057379Seric ** mci -- the mailer connection info structure. 79157379Seric ** e -- the current envelope. 79257379Seric ** timeout -- the timeout for reads. 79359285Seric ** pfunc -- processing function for second and subsequent 79459285Seric ** lines of response -- if null, no special 79559285Seric ** processing is done. 7964684Seric ** 7974684Seric ** Returns: 7984684Seric ** reply code it reads. 7994684Seric ** 8004684Seric ** Side Effects: 8014684Seric ** flushes the mail file. 8024684Seric */ 8034684Seric 80459285Seric reply(m, mci, e, timeout, pfunc) 80553751Seric MAILER *m; 80654967Seric MCI *mci; 80753751Seric ENVELOPE *e; 80859285Seric time_t timeout; 80959285Seric void (*pfunc)(); 8104684Seric { 81159014Seric register char *bufp; 81259014Seric register int r; 81359285Seric bool firstline = TRUE; 81458957Seric char junkbuf[MAXLINE]; 81558957Seric 81657379Seric if (mci->mci_out != NULL) 81757379Seric (void) fflush(mci->mci_out); 8184684Seric 8197677Seric if (tTd(18, 1)) 8204796Seric printf("reply\n"); 8214796Seric 8227356Seric /* 8237356Seric ** Read the input line, being careful not to hang. 8247356Seric */ 8257356Seric 82659014Seric for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 8274684Seric { 8287356Seric register char *p; 82953751Seric extern time_t curtime(); 8304684Seric 8317685Seric /* actually do the read */ 83253751Seric if (e->e_xfp != NULL) 83353751Seric (void) fflush(e->e_xfp); /* for debugging */ 8347356Seric 83510054Seric /* if we are in the process of closing just give the code */ 83653740Seric if (mci->mci_state == MCIS_CLOSED) 83710054Seric return (SMTPCLOSING); 83810054Seric 83958680Seric if (mci->mci_out != NULL) 84058680Seric fflush(mci->mci_out); 84158680Seric 84210054Seric /* get the line from the other side */ 84361093Seric p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 84453751Seric mci->mci_lastuse = curtime(); 84553751Seric 84610054Seric if (p == NULL) 84710131Seric { 84863753Seric bool oldholderrs; 84910148Seric extern char MsgBuf[]; /* err.c */ 85010148Seric 85121065Seric /* if the remote end closed early, fake an error */ 85221065Seric if (errno == 0) 85321065Seric # ifdef ECONNRESET 85421065Seric errno = ECONNRESET; 85556795Seric # else /* ECONNRESET */ 85621065Seric errno = EPIPE; 85756795Seric # endif /* ECONNRESET */ 85821065Seric 85957379Seric mci->mci_errno = errno; 86057642Seric mci->mci_exitstat = EX_TEMPFAIL; 86163753Seric oldholderrs = HoldErrs; 86263753Seric HoldErrs = TRUE; 86363753Seric usrerr("451 reply: read error from %s", mci->mci_host); 86463753Seric 86510420Seric /* if debugging, pause so we can see state */ 86610420Seric if (tTd(18, 100)) 86710420Seric pause(); 86854967Seric mci->mci_state = MCIS_ERROR; 86953751Seric smtpquit(m, mci, e); 87063753Seric #ifdef XDEBUG 87163753Seric { 87263753Seric char wbuf[MAXLINE]; 87364082Seric char *p = wbuf; 87464082Seric if (e->e_to != NULL) 87564082Seric { 87664082Seric sprintf(p, "%s... ", e->e_to); 87764082Seric p += strlen(p); 87864082Seric } 87964082Seric sprintf(p, "reply(%s) during %s", 88064082Seric mci->mci_host, SmtpPhase); 88163753Seric checkfd012(wbuf); 88263753Seric } 88363753Seric #endif 88463753Seric HoldErrs = oldholderrs; 88510054Seric return (-1); 88610131Seric } 88758957Seric fixcrlf(bufp, TRUE); 88810054Seric 88963753Seric /* EHLO failure is not a real error */ 89063753Seric if (e->e_xfp != NULL && (bufp[0] == '4' || 89163753Seric (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 89214900Seric { 89314900Seric /* serious error -- log the previous command */ 89464071Seric if (SmtpNeedIntro) 89564071Seric { 89664071Seric /* inform user who we are chatting with */ 89764071Seric fprintf(CurEnv->e_xfp, 89864071Seric "... while talking to %s:\n", 89964071Seric CurHostName); 90064071Seric SmtpNeedIntro = FALSE; 90164071Seric } 90259014Seric if (SmtpMsgBuffer[0] != '\0') 90359014Seric fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 90459014Seric SmtpMsgBuffer[0] = '\0'; 90514900Seric 90614900Seric /* now log the message as from the other side */ 90758957Seric fprintf(e->e_xfp, "<<< %s\n", bufp); 90814900Seric } 90914900Seric 91014900Seric /* display the input for verbose mode */ 91158120Seric if (Verbose) 91259956Seric nmessage("050 %s", bufp); 9137356Seric 91459285Seric /* process the line */ 91567893Seric if (pfunc != NULL) 91667893Seric (*pfunc)(bufp, firstline, m, mci, e); 91759285Seric 91859285Seric firstline = FALSE; 91959285Seric 9207356Seric /* if continuation is required, we can go on */ 92159014Seric if (bufp[3] == '-') 9224684Seric continue; 9237356Seric 92459014Seric /* ignore improperly formated input */ 92559014Seric if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 92659014Seric continue; 92759014Seric 9287356Seric /* decode the reply code */ 92959014Seric r = atoi(bufp); 9307356Seric 9317356Seric /* extra semantics: 0xx codes are "informational" */ 93259014Seric if (r >= 100) 93359014Seric break; 93459014Seric } 9357356Seric 93659014Seric /* 93759014Seric ** Now look at SmtpReplyBuffer -- only care about the first 93859014Seric ** line of the response from here on out. 93959014Seric */ 94058061Seric 94159014Seric /* save temporary failure messages for posterity */ 94259014Seric if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 94359014Seric (void) strcpy(SmtpError, SmtpReplyBuffer); 9449391Seric 94559014Seric /* reply code 421 is "Service Shutting Down" */ 94659014Seric if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 94759014Seric { 94859014Seric /* send the quit protocol */ 94959014Seric mci->mci_state = MCIS_SSD; 95059014Seric smtpquit(m, mci, e); 9514684Seric } 95259014Seric 95359014Seric return (r); 9544684Seric } 9554796Seric /* 9564865Seric ** SMTPMESSAGE -- send message to server 9574796Seric ** 9584796Seric ** Parameters: 9594796Seric ** f -- format 96010175Seric ** m -- the mailer to control formatting. 9614796Seric ** a, b, c -- parameters 9624796Seric ** 9634796Seric ** Returns: 9644796Seric ** none. 9654796Seric ** 9664796Seric ** Side Effects: 96753740Seric ** writes message to mci->mci_out. 9684796Seric */ 9694796Seric 9704865Seric /*VARARGS1*/ 97157642Seric #ifdef __STDC__ 97257642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...) 97357642Seric #else 97457642Seric smtpmessage(f, m, mci, va_alist) 9754796Seric char *f; 97610175Seric MAILER *m; 97754967Seric MCI *mci; 97857642Seric va_dcl 97957642Seric #endif 9804796Seric { 98156852Seric VA_LOCAL_DECL 98256852Seric 98357135Seric VA_START(mci); 98456852Seric (void) vsprintf(SmtpMsgBuffer, f, ap); 98556852Seric VA_END; 98658680Seric 98758120Seric if (tTd(18, 1) || Verbose) 98858151Seric nmessage(">>> %s", SmtpMsgBuffer); 98963753Seric if (TrafficLogFile != NULL) 99063753Seric fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 99153740Seric if (mci->mci_out != NULL) 99258680Seric { 99353740Seric fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 99454967Seric m == NULL ? "\r\n" : m->m_eol); 99558680Seric } 99659149Seric else if (tTd(18, 1)) 99758725Seric { 99859149Seric printf("smtpmessage: NULL mci_out\n"); 99958725Seric } 10004796Seric } 10015182Seric 100256795Seric # endif /* SMTP */ 1003