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*67987Seric static char sccsid[] = "@(#)usersmtp.c 8.30 (Berkeley) 11/23/94 (with SMTP)"; 1433731Sbostic #else 15*67987Seric static char sccsid[] = "@(#)usersmtp.c 8.30 (Berkeley) 11/23/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 } 34067887Seric else if (bitnset(M_8BITS, m->m_flags)) 34167887Seric { 34267887Seric /* just pass it through */ 34367887Seric } 34467887Seric else if (bitset(MM_CVTMIME, MimeMode) && 34567887Seric (e->e_bodytype == NULL ? !bitset(MM_PASS8BIT, MimeMode) 34667887Seric : strcasecmp(e->e_bodytype, "7bit") != 0)) 34767887Seric { 34867887Seric /* must convert from 8bit MIME format to 7bit encoded */ 34967887Seric mci->mci_flags |= MCIF_CVT8TO7; 35067887Seric } 35167887Seric else if (!bitset(MM_PASS8BIT, MimeMode)) 35267887Seric { 35367887Seric /* cannot just send a 8-bit version */ 35467887Seric usrerr("%s does not support 8BITMIME", mci->mci_host); 35567887Seric return EX_DATAERR; 35667887Seric } 35767417Seric 35867963Seric if (bitset(MCIF_DSN, mci->mci_flags)) 35967880Seric { 36067963Seric if (e->e_envid != NULL) 36167963Seric { 36267963Seric strcat(optbuf, " ENVID="); 36367963Seric strcat(optbuf, e->e_envid); 36467963Seric } 36567963Seric if (e->e_omts != NULL) 36667963Seric { 36767963Seric strcat(optbuf, " OMTS="); 36867963Seric strcat(optbuf, e->e_omts); 36967963Seric } 37067880Seric } 37167880Seric 3729315Seric /* 3734865Seric ** Send the MAIL command. 3744865Seric ** Designates the sender. 3754865Seric */ 3764796Seric 37753751Seric mci->mci_state = MCIS_ACTIVE; 37853751Seric 37959540Seric if (bitset(EF_RESPONSE, e->e_flags) && 38059540Seric !bitnset(M_NO_NULL_FROM, m->m_flags)) 38158680Seric (void) strcpy(buf, ""); 38258680Seric else 38358680Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 38465494Seric if (buf[0] == '<') 38565494Seric { 38665494Seric /* strip off <angle brackets> (put back on below) */ 38765494Seric bufp = &buf[strlen(buf) - 1]; 38865494Seric if (*bufp == '>') 38965494Seric *bufp = '\0'; 39065494Seric bufp = &buf[1]; 39165494Seric } 39265494Seric else 39365494Seric bufp = buf; 39467472Seric if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) || 39510688Seric !bitnset(M_FROMPATH, m->m_flags)) 3968436Seric { 39765494Seric smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf); 3988436Seric } 3998436Seric else 4008436Seric { 40159285Seric smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 40265494Seric *bufp == '@' ? ',' : ':', bufp, optbuf); 4038436Seric } 40461093Seric SmtpPhase = mci->mci_phase = "client MAIL"; 40553751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 40659285Seric r = reply(m, mci, e, TimeOuts.to_mail, NULL); 4078005Seric if (r < 0 || REPLYTYPE(r) == 4) 40853751Seric { 40953751Seric mci->mci_exitstat = EX_TEMPFAIL; 41053751Seric mci->mci_errno = errno; 41153751Seric smtpquit(m, mci, e); 41253751Seric return EX_TEMPFAIL; 41353751Seric } 4147963Seric else if (r == 250) 41553751Seric { 41653751Seric mci->mci_exitstat = EX_OK; 41753751Seric return EX_OK; 41853751Seric } 4197963Seric else if (r == 552) 42053751Seric { 42153751Seric /* signal service unavailable */ 42253751Seric mci->mci_exitstat = EX_UNAVAILABLE; 42353751Seric smtpquit(m, mci, e); 42453751Seric return EX_UNAVAILABLE; 42553751Seric } 42614913Seric 42758008Seric #ifdef LOG 42858020Seric if (LogLevel > 1) 42958008Seric { 43067860Seric syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s", 43167860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 43258008Seric } 43358008Seric #endif 43458008Seric 43514913Seric /* protocol error -- close up */ 43653751Seric smtpquit(m, mci, e); 43753751Seric mci->mci_exitstat = EX_PROTOCOL; 43853751Seric return EX_PROTOCOL; 4394684Seric } 4404684Seric /* 4414976Seric ** SMTPRCPT -- designate recipient. 4424797Seric ** 4434797Seric ** Parameters: 4444865Seric ** to -- address of recipient. 44510175Seric ** m -- the mailer we are sending to. 44657379Seric ** mci -- the connection info for this transaction. 44757379Seric ** e -- the envelope for this transaction. 4484797Seric ** 4494797Seric ** Returns: 4504865Seric ** exit status corresponding to recipient status. 4514797Seric ** 4524797Seric ** Side Effects: 4534865Seric ** Sends the mail via SMTP. 4544797Seric */ 4554797Seric 45653751Seric smtprcpt(to, m, mci, e) 4574865Seric ADDRESS *to; 45810175Seric register MAILER *m; 45954967Seric MCI *mci; 46053751Seric ENVELOPE *e; 4614797Seric { 4624797Seric register int r; 46367880Seric char optbuf[MAXLINE]; 4644797Seric 46567880Seric strcpy(optbuf, ""); 46667880Seric if (bitset(MCIF_DSN, mci->mci_flags)) 46767880Seric { 46867963Seric bool firstone = TRUE; 46967963Seric 470*67987Seric /* NOTIFY= parameter */ 47167880Seric strcat(optbuf, " NOTIFY="); 472*67987Seric if (bitset(QPINGONSUCCESS, to->q_flags)) 47367880Seric { 474*67987Seric strcat(optbuf, "SUCCESS"); 47567963Seric firstone = FALSE; 47667880Seric } 477*67987Seric if (bitset(QPINGONFAILURE, to->q_flags)) 47867880Seric { 47967971Seric if (!firstone) 48067963Seric strcat(optbuf, ","); 481*67987Seric strcat(optbuf, "FAILURE"); 48267963Seric firstone = FALSE; 48367880Seric } 48467963Seric if (bitset(QPINGONDELAY, to->q_flags)) 48567880Seric { 48667971Seric if (!firstone) 48767963Seric strcat(optbuf, ","); 48867963Seric strcat(optbuf, "DELAY"); 48967963Seric firstone = FALSE; 49067963Seric } 49167963Seric if (firstone) 49267963Seric strcat(optbuf, "NEVER"); 49367963Seric 494*67987Seric /* RET= parameter */ 49567963Seric if (bitset(QHAS_RET_PARAM, to->q_flags)) 49667963Seric { 49767880Seric strcat(optbuf, " RET="); 49867963Seric if (bitset(QRET_HDRS, to->q_flags)) 49967963Seric strcat(optbuf, "HDRS"); 50067880Seric else 50167963Seric strcat(optbuf, "FULL"); 50267880Seric } 503*67987Seric 504*67987Seric /* ORCPT= parameter */ 505*67987Seric if (to->q_orcpt != NULL) 506*67987Seric { 507*67987Seric strcat(optbuf, " ORCPT="); 508*67987Seric strcat(optbuf, to->q_orcpt); 509*67987Seric } 51067880Seric } 51167880Seric else if (bitset(QPINGONSUCCESS, to->q_flags)) 51267880Seric { 51367880Seric to->q_flags |= QRELAYED; 51467880Seric fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n", 51567880Seric to->q_paddr); 51667880Seric } 5174865Seric 51867880Seric smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf); 51967880Seric 52061093Seric SmtpPhase = mci->mci_phase = "client RCPT"; 52153751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 52259285Seric r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 52367880Seric setstatus(to, SmtpReplyBuffer); 5248005Seric if (r < 0 || REPLYTYPE(r) == 4) 5254865Seric return (EX_TEMPFAIL); 5267963Seric else if (REPLYTYPE(r) == 2) 5277963Seric return (EX_OK); 5287964Seric else if (r == 550 || r == 551 || r == 553) 5297964Seric return (EX_NOUSER); 5307964Seric else if (r == 552 || r == 554) 5317964Seric return (EX_UNAVAILABLE); 53258008Seric 53358008Seric #ifdef LOG 53458020Seric if (LogLevel > 1) 53558008Seric { 53667860Seric syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s", 53767860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 53858008Seric } 53958008Seric #endif 54058008Seric 5417964Seric return (EX_PROTOCOL); 5424797Seric } 5434797Seric /* 54410175Seric ** SMTPDATA -- send the data and clean up the transaction. 5454684Seric ** 5464684Seric ** Parameters: 5474865Seric ** m -- mailer being sent to. 5486980Seric ** e -- the envelope for this message. 5494684Seric ** 5504684Seric ** Returns: 5514976Seric ** exit status corresponding to DATA command. 5524684Seric ** 5534684Seric ** Side Effects: 5544865Seric ** none. 5554684Seric */ 5564684Seric 55763753Seric static jmp_buf CtxDataTimeout; 55863937Seric static int datatimeout(); 55963753Seric 56053740Seric smtpdata(m, mci, e) 5614865Seric struct mailer *m; 56254967Seric register MCI *mci; 5636980Seric register ENVELOPE *e; 5644684Seric { 5654684Seric register int r; 56663753Seric register EVENT *ev; 56763753Seric time_t timeout; 5684684Seric 5694797Seric /* 5704797Seric ** Send the data. 57110175Seric ** First send the command and check that it is ok. 57210175Seric ** Then send the data. 57310175Seric ** Follow it up with a dot to terminate. 57410175Seric ** Finally get the results of the transaction. 5754797Seric */ 5764797Seric 57710175Seric /* send the command and check ok to proceed */ 57853751Seric smtpmessage("DATA", m, mci); 57961093Seric SmtpPhase = mci->mci_phase = "client DATA 354"; 58053751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 58159285Seric r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 5828005Seric if (r < 0 || REPLYTYPE(r) == 4) 58357990Seric { 58457990Seric smtpquit(m, mci, e); 5854797Seric return (EX_TEMPFAIL); 58657990Seric } 5877963Seric else if (r == 554) 58857990Seric { 58957990Seric smtprset(m, mci, e); 5907963Seric return (EX_UNAVAILABLE); 59157990Seric } 5927963Seric else if (r != 354) 59357990Seric { 59458008Seric #ifdef LOG 59558020Seric if (LogLevel > 1) 59658008Seric { 59767860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s", 59867860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 59958008Seric } 60058008Seric #endif 60157990Seric smtprset(m, mci, e); 6027964Seric return (EX_PROTOCOL); 60357990Seric } 60410175Seric 60563757Seric /* 60663757Seric ** Set timeout around data writes. Make it at least large 60763757Seric ** enough for DNS timeouts on all recipients plus some fudge 60863757Seric ** factor. The main thing is that it should not be infinite. 60963757Seric */ 61063757Seric 61163753Seric if (setjmp(CtxDataTimeout) != 0) 61263753Seric { 61363753Seric mci->mci_errno = errno; 61463753Seric mci->mci_exitstat = EX_TEMPFAIL; 61563753Seric mci->mci_state = MCIS_ERROR; 61663753Seric syserr("451 timeout writing message to %s", mci->mci_host); 61763753Seric smtpquit(m, mci, e); 61863753Seric return EX_TEMPFAIL; 61963753Seric } 62063753Seric 62163787Seric timeout = e->e_msgsize / 16; 62263787Seric if (timeout < (time_t) 60) 62363787Seric timeout = (time_t) 60; 62463787Seric timeout += e->e_nrcpts * 90; 62563753Seric ev = setevent(timeout, datatimeout, 0); 62663753Seric 62767546Seric /* 62867546Seric ** Output the actual message. 62967546Seric */ 63067546Seric 63167936Seric (*e->e_puthdr)(mci, e->e_header, e, 0); 63267936Seric (*e->e_putbody)(mci, e, NULL, 0); 63310175Seric 63467546Seric /* 63567546Seric ** Cleanup after sending message. 63667546Seric */ 63767546Seric 63863753Seric clrevent(ev); 63963753Seric 64064718Seric if (ferror(mci->mci_out)) 64164718Seric { 64264718Seric /* error during processing -- don't send the dot */ 64364718Seric mci->mci_errno = EIO; 64464718Seric mci->mci_exitstat = EX_IOERR; 64564718Seric mci->mci_state = MCIS_ERROR; 64664718Seric smtpquit(m, mci, e); 64764718Seric return EX_IOERR; 64864718Seric } 64964718Seric 65010175Seric /* terminate the message */ 65153740Seric fprintf(mci->mci_out, ".%s", m->m_eol); 65263753Seric if (TrafficLogFile != NULL) 65363753Seric fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 65458120Seric if (Verbose) 65558151Seric nmessage(">>> ."); 65610175Seric 65710175Seric /* check for the results of the transaction */ 65861093Seric SmtpPhase = mci->mci_phase = "client DATA 250"; 65953751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 66059285Seric r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 66153751Seric if (r < 0) 66257990Seric { 66357990Seric smtpquit(m, mci, e); 6644797Seric return (EX_TEMPFAIL); 66557990Seric } 66653751Seric mci->mci_state = MCIS_OPEN; 66758917Seric e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 66853751Seric if (REPLYTYPE(r) == 4) 66953751Seric return (EX_TEMPFAIL); 6707963Seric else if (r == 250) 6717963Seric return (EX_OK); 6727963Seric else if (r == 552 || r == 554) 6737963Seric return (EX_UNAVAILABLE); 67458008Seric #ifdef LOG 67558020Seric if (LogLevel > 1) 67658008Seric { 67767860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s", 67867860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 67958008Seric } 68058008Seric #endif 6817964Seric return (EX_PROTOCOL); 6824684Seric } 68363753Seric 68463753Seric 68563753Seric static int 68663753Seric datatimeout() 68763753Seric { 68863753Seric longjmp(CtxDataTimeout, 1); 68963753Seric } 6904684Seric /* 6914865Seric ** SMTPQUIT -- close the SMTP connection. 6924865Seric ** 6934865Seric ** Parameters: 69415535Seric ** m -- a pointer to the mailer. 6954865Seric ** 6964865Seric ** Returns: 6974865Seric ** none. 6984865Seric ** 6994865Seric ** Side Effects: 7004865Seric ** sends the final protocol and closes the connection. 7014865Seric */ 7024865Seric 70353751Seric smtpquit(m, mci, e) 70453751Seric register MAILER *m; 70554967Seric register MCI *mci; 70653751Seric ENVELOPE *e; 7074865Seric { 70864822Seric bool oldSuprErrs = SuprErrs; 7094865Seric 71064822Seric /* 71164822Seric ** Suppress errors here -- we may be processing a different 71264822Seric ** job when we do the quit connection, and we don't want the 71364822Seric ** new job to be penalized for something that isn't it's 71464822Seric ** problem. 71564822Seric */ 71664822Seric 71764822Seric SuprErrs = TRUE; 71864822Seric 71954967Seric /* send the quit message if we haven't gotten I/O error */ 72053751Seric if (mci->mci_state != MCIS_ERROR) 7219391Seric { 72261093Seric SmtpPhase = "client QUIT"; 72353751Seric smtpmessage("QUIT", m, mci); 72459285Seric (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 72564822Seric SuprErrs = oldSuprErrs; 72653740Seric if (mci->mci_state == MCIS_CLOSED) 72764822Seric { 72864822Seric SuprErrs = oldSuprErrs; 72910159Seric return; 73064822Seric } 7319391Seric } 7329391Seric 73352676Seric /* now actually close the connection and pick up the zombie */ 73465194Seric (void) endmailer(mci, e, NULL); 73564822Seric 73664822Seric SuprErrs = oldSuprErrs; 7374865Seric } 7384865Seric /* 73954967Seric ** SMTPRSET -- send a RSET (reset) command 74054967Seric */ 74154967Seric 74254967Seric smtprset(m, mci, e) 74354967Seric register MAILER *m; 74454967Seric register MCI *mci; 74554967Seric ENVELOPE *e; 74654967Seric { 74754967Seric int r; 74854967Seric 74961093Seric SmtpPhase = "client RSET"; 75054967Seric smtpmessage("RSET", m, mci); 75159285Seric r = reply(m, mci, e, TimeOuts.to_rset, NULL); 75257734Seric if (r < 0) 75357734Seric mci->mci_state = MCIS_ERROR; 75454967Seric else if (REPLYTYPE(r) == 2) 75557734Seric { 75657734Seric mci->mci_state = MCIS_OPEN; 75757734Seric return; 75857734Seric } 75957734Seric smtpquit(m, mci, e); 76054967Seric } 76154967Seric /* 76258867Seric ** SMTPPROBE -- check the connection state 76354967Seric */ 76454967Seric 76558867Seric smtpprobe(mci) 76654967Seric register MCI *mci; 76754967Seric { 76854967Seric int r; 76954967Seric MAILER *m = mci->mci_mailer; 77054967Seric extern ENVELOPE BlankEnvelope; 77154967Seric ENVELOPE *e = &BlankEnvelope; 77254967Seric 77361093Seric SmtpPhase = "client probe"; 77458867Seric smtpmessage("RSET", m, mci); 77559285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 77658061Seric if (r < 0 || REPLYTYPE(r) != 2) 77754967Seric smtpquit(m, mci, e); 77854967Seric return r; 77954967Seric } 78054967Seric /* 7814684Seric ** REPLY -- read arpanet reply 7824684Seric ** 7834684Seric ** Parameters: 78410175Seric ** m -- the mailer we are reading the reply from. 78557379Seric ** mci -- the mailer connection info structure. 78657379Seric ** e -- the current envelope. 78757379Seric ** timeout -- the timeout for reads. 78859285Seric ** pfunc -- processing function for second and subsequent 78959285Seric ** lines of response -- if null, no special 79059285Seric ** processing is done. 7914684Seric ** 7924684Seric ** Returns: 7934684Seric ** reply code it reads. 7944684Seric ** 7954684Seric ** Side Effects: 7964684Seric ** flushes the mail file. 7974684Seric */ 7984684Seric 79959285Seric reply(m, mci, e, timeout, pfunc) 80053751Seric MAILER *m; 80154967Seric MCI *mci; 80253751Seric ENVELOPE *e; 80359285Seric time_t timeout; 80459285Seric void (*pfunc)(); 8054684Seric { 80659014Seric register char *bufp; 80759014Seric register int r; 80859285Seric bool firstline = TRUE; 80958957Seric char junkbuf[MAXLINE]; 81058957Seric 81157379Seric if (mci->mci_out != NULL) 81257379Seric (void) fflush(mci->mci_out); 8134684Seric 8147677Seric if (tTd(18, 1)) 8154796Seric printf("reply\n"); 8164796Seric 8177356Seric /* 8187356Seric ** Read the input line, being careful not to hang. 8197356Seric */ 8207356Seric 82159014Seric for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 8224684Seric { 8237356Seric register char *p; 82453751Seric extern time_t curtime(); 8254684Seric 8267685Seric /* actually do the read */ 82753751Seric if (e->e_xfp != NULL) 82853751Seric (void) fflush(e->e_xfp); /* for debugging */ 8297356Seric 83010054Seric /* if we are in the process of closing just give the code */ 83153740Seric if (mci->mci_state == MCIS_CLOSED) 83210054Seric return (SMTPCLOSING); 83310054Seric 83458680Seric if (mci->mci_out != NULL) 83558680Seric fflush(mci->mci_out); 83658680Seric 83710054Seric /* get the line from the other side */ 83861093Seric p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 83953751Seric mci->mci_lastuse = curtime(); 84053751Seric 84110054Seric if (p == NULL) 84210131Seric { 84363753Seric bool oldholderrs; 84410148Seric extern char MsgBuf[]; /* err.c */ 84510148Seric 84621065Seric /* if the remote end closed early, fake an error */ 84721065Seric if (errno == 0) 84821065Seric # ifdef ECONNRESET 84921065Seric errno = ECONNRESET; 85056795Seric # else /* ECONNRESET */ 85121065Seric errno = EPIPE; 85256795Seric # endif /* ECONNRESET */ 85321065Seric 85457379Seric mci->mci_errno = errno; 85557642Seric mci->mci_exitstat = EX_TEMPFAIL; 85663753Seric oldholderrs = HoldErrs; 85763753Seric HoldErrs = TRUE; 85863753Seric usrerr("451 reply: read error from %s", mci->mci_host); 85963753Seric 86010420Seric /* if debugging, pause so we can see state */ 86110420Seric if (tTd(18, 100)) 86210420Seric pause(); 86354967Seric mci->mci_state = MCIS_ERROR; 86453751Seric smtpquit(m, mci, e); 86563753Seric #ifdef XDEBUG 86663753Seric { 86763753Seric char wbuf[MAXLINE]; 86864082Seric char *p = wbuf; 86964082Seric if (e->e_to != NULL) 87064082Seric { 87164082Seric sprintf(p, "%s... ", e->e_to); 87264082Seric p += strlen(p); 87364082Seric } 87464082Seric sprintf(p, "reply(%s) during %s", 87564082Seric mci->mci_host, SmtpPhase); 87663753Seric checkfd012(wbuf); 87763753Seric } 87863753Seric #endif 87963753Seric HoldErrs = oldholderrs; 88010054Seric return (-1); 88110131Seric } 88258957Seric fixcrlf(bufp, TRUE); 88310054Seric 88463753Seric /* EHLO failure is not a real error */ 88563753Seric if (e->e_xfp != NULL && (bufp[0] == '4' || 88663753Seric (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 88714900Seric { 88814900Seric /* serious error -- log the previous command */ 88964071Seric if (SmtpNeedIntro) 89064071Seric { 89164071Seric /* inform user who we are chatting with */ 89264071Seric fprintf(CurEnv->e_xfp, 89364071Seric "... while talking to %s:\n", 89464071Seric CurHostName); 89564071Seric SmtpNeedIntro = FALSE; 89664071Seric } 89759014Seric if (SmtpMsgBuffer[0] != '\0') 89859014Seric fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 89959014Seric SmtpMsgBuffer[0] = '\0'; 90014900Seric 90114900Seric /* now log the message as from the other side */ 90258957Seric fprintf(e->e_xfp, "<<< %s\n", bufp); 90314900Seric } 90414900Seric 90514900Seric /* display the input for verbose mode */ 90658120Seric if (Verbose) 90759956Seric nmessage("050 %s", bufp); 9087356Seric 90959285Seric /* process the line */ 91067893Seric if (pfunc != NULL) 91167893Seric (*pfunc)(bufp, firstline, m, mci, e); 91259285Seric 91359285Seric firstline = FALSE; 91459285Seric 9157356Seric /* if continuation is required, we can go on */ 91659014Seric if (bufp[3] == '-') 9174684Seric continue; 9187356Seric 91959014Seric /* ignore improperly formated input */ 92059014Seric if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 92159014Seric continue; 92259014Seric 9237356Seric /* decode the reply code */ 92459014Seric r = atoi(bufp); 9257356Seric 9267356Seric /* extra semantics: 0xx codes are "informational" */ 92759014Seric if (r >= 100) 92859014Seric break; 92959014Seric } 9307356Seric 93159014Seric /* 93259014Seric ** Now look at SmtpReplyBuffer -- only care about the first 93359014Seric ** line of the response from here on out. 93459014Seric */ 93558061Seric 93659014Seric /* save temporary failure messages for posterity */ 93759014Seric if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 93859014Seric (void) strcpy(SmtpError, SmtpReplyBuffer); 9399391Seric 94059014Seric /* reply code 421 is "Service Shutting Down" */ 94159014Seric if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 94259014Seric { 94359014Seric /* send the quit protocol */ 94459014Seric mci->mci_state = MCIS_SSD; 94559014Seric smtpquit(m, mci, e); 9464684Seric } 94759014Seric 94859014Seric return (r); 9494684Seric } 9504796Seric /* 9514865Seric ** SMTPMESSAGE -- send message to server 9524796Seric ** 9534796Seric ** Parameters: 9544796Seric ** f -- format 95510175Seric ** m -- the mailer to control formatting. 9564796Seric ** a, b, c -- parameters 9574796Seric ** 9584796Seric ** Returns: 9594796Seric ** none. 9604796Seric ** 9614796Seric ** Side Effects: 96253740Seric ** writes message to mci->mci_out. 9634796Seric */ 9644796Seric 9654865Seric /*VARARGS1*/ 96657642Seric #ifdef __STDC__ 96757642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...) 96857642Seric #else 96957642Seric smtpmessage(f, m, mci, va_alist) 9704796Seric char *f; 97110175Seric MAILER *m; 97254967Seric MCI *mci; 97357642Seric va_dcl 97457642Seric #endif 9754796Seric { 97656852Seric VA_LOCAL_DECL 97756852Seric 97857135Seric VA_START(mci); 97956852Seric (void) vsprintf(SmtpMsgBuffer, f, ap); 98056852Seric VA_END; 98158680Seric 98258120Seric if (tTd(18, 1) || Verbose) 98358151Seric nmessage(">>> %s", SmtpMsgBuffer); 98463753Seric if (TrafficLogFile != NULL) 98563753Seric fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 98653740Seric if (mci->mci_out != NULL) 98758680Seric { 98853740Seric fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 98954967Seric m == NULL ? "\r\n" : m->m_eol); 99058680Seric } 99159149Seric else if (tTd(18, 1)) 99258725Seric { 99359149Seric printf("smtpmessage: NULL mci_out\n"); 99458725Seric } 9954796Seric } 9965182Seric 99756795Seric # endif /* SMTP */ 998