122716Sdist /* 268839Seric * Copyright (c) 1983, 1995 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*69881Seric static char sccsid[] = "@(#)usersmtp.c 8.55 (Berkeley) 06/13/95 (with SMTP)"; 1433731Sbostic #else 15*69881Seric static char sccsid[] = "@(#)usersmtp.c 8.55 (Berkeley) 06/13/95 (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 4069748Seric extern void smtpmessage __P((char *f, MAILER *m, MCI *mci, ...)); 419391Seric /* 424865Seric ** SMTPINIT -- initialize SMTP. 434684Seric ** 444865Seric ** Opens the connection and sends the initial protocol. 454684Seric ** 464684Seric ** Parameters: 474865Seric ** m -- mailer to create connection to. 484865Seric ** pvp -- pointer to parameter vector to pass to 494865Seric ** the mailer. 504684Seric ** 514684Seric ** Returns: 5254967Seric ** none. 534684Seric ** 544684Seric ** Side Effects: 554865Seric ** creates connection and sends initial protocol. 564684Seric */ 574684Seric 5869748Seric void 5954967Seric smtpinit(m, mci, e) 604865Seric struct mailer *m; 6154967Seric register MCI *mci; 6253751Seric ENVELOPE *e; 634684Seric { 644865Seric register int r; 6558957Seric register char *p; 6660210Seric extern void esmtp_check(); 6759285Seric extern void helo_options(); 684684Seric 6963753Seric if (tTd(18, 1)) 7057379Seric { 7157379Seric printf("smtpinit "); 7264731Seric mci_dump(mci, FALSE); 7357379Seric } 7457379Seric 754865Seric /* 764865Seric ** Open the connection to the mailer. 774865Seric */ 784684Seric 7921065Seric SmtpError[0] = '\0'; 8057379Seric CurHostName = mci->mci_host; /* XXX UGLY XXX */ 8168100Seric if (CurHostName == NULL) 8268100Seric CurHostName = MyHostName; 8364071Seric SmtpNeedIntro = TRUE; 8454967Seric switch (mci->mci_state) 856051Seric { 8654967Seric case MCIS_ACTIVE: 8754967Seric /* need to clear old information */ 8854967Seric smtprset(m, mci, e); 8957734Seric /* fall through */ 9015139Seric 9154967Seric case MCIS_OPEN: 9254967Seric return; 9354967Seric 9454967Seric case MCIS_ERROR: 9554967Seric case MCIS_SSD: 9654967Seric /* shouldn't happen */ 9754967Seric smtpquit(m, mci, e); 9857734Seric /* fall through */ 9954967Seric 10054967Seric case MCIS_CLOSED: 10158151Seric syserr("451 smtpinit: state CLOSED"); 10254967Seric return; 10354967Seric 10454967Seric case MCIS_OPENING: 10554967Seric break; 1066051Seric } 1074796Seric 10854967Seric mci->mci_state = MCIS_OPENING; 10954967Seric 1104865Seric /* 1114865Seric ** Get the greeting message. 11214913Seric ** This should appear spontaneously. Give it five minutes to 11314886Seric ** happen. 1144865Seric */ 1154797Seric 11661093Seric SmtpPhase = mci->mci_phase = "client greeting"; 11753751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 11860210Seric r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check); 11964750Seric if (r < 0 || REPLYTYPE(r) == 4) 12052104Seric goto tempfail1; 12164750Seric if (REPLYTYPE(r) != 2) 12264750Seric goto unavailable; 1234684Seric 1244865Seric /* 1254976Seric ** Send the HELO command. 1267963Seric ** My mother taught me to always introduce myself. 1274976Seric */ 1284976Seric 12959285Seric if (bitnset(M_ESMTP, m->m_flags)) 13059285Seric mci->mci_flags |= MCIF_ESMTP; 13159285Seric 13259285Seric tryhelo: 13359285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 13459285Seric { 13559285Seric smtpmessage("EHLO %s", m, mci, MyHostName); 13661093Seric SmtpPhase = mci->mci_phase = "client EHLO"; 13759285Seric } 13859285Seric else 13959285Seric { 14059285Seric smtpmessage("HELO %s", m, mci, MyHostName); 14161093Seric SmtpPhase = mci->mci_phase = "client HELO"; 14259285Seric } 14353751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 14459285Seric r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 1458005Seric if (r < 0) 14652104Seric goto tempfail1; 1478005Seric else if (REPLYTYPE(r) == 5) 14859285Seric { 14959285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 15059285Seric { 15159285Seric /* try old SMTP instead */ 15259285Seric mci->mci_flags &= ~MCIF_ESMTP; 15359285Seric goto tryhelo; 15459285Seric } 15514913Seric goto unavailable; 15659285Seric } 1577963Seric else if (REPLYTYPE(r) != 2) 15852104Seric goto tempfail1; 1594976Seric 1604976Seric /* 16158957Seric ** Check to see if we actually ended up talking to ourself. 16258957Seric ** This means we didn't know about an alias or MX, or we managed 16358957Seric ** to connect to an echo server. 16458957Seric */ 16558957Seric 16659026Seric p = strchr(&SmtpReplyBuffer[4], ' '); 16758957Seric if (p != NULL) 16861707Seric *p = '\0'; 16967472Seric if (!bitnset(M_NOLOOPCHECK, m->m_flags) && 17067472Seric strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0) 17158957Seric { 17258957Seric syserr("553 %s config error: mail loops back to myself", 17358957Seric MyHostName); 17458957Seric mci->mci_exitstat = EX_CONFIG; 17558957Seric mci->mci_errno = 0; 17658957Seric smtpquit(m, mci, e); 17758957Seric return; 17858957Seric } 17958957Seric 18058957Seric /* 1819315Seric ** If this is expected to be another sendmail, send some internal 1829315Seric ** commands. 1839315Seric */ 1849315Seric 18510688Seric if (bitnset(M_INTERNAL, m->m_flags)) 1869315Seric { 1879315Seric /* tell it to be verbose */ 18853751Seric smtpmessage("VERB", m, mci); 18959285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 1909315Seric if (r < 0) 19152104Seric goto tempfail2; 1929315Seric } 1939315Seric 19465057Seric if (mci->mci_state != MCIS_CLOSED) 19565057Seric { 19665057Seric mci->mci_state = MCIS_OPEN; 19765057Seric return; 19865057Seric } 19953751Seric 20065057Seric /* got a 421 error code during startup */ 20165057Seric 20253751Seric tempfail1: 20353751Seric tempfail2: 20453751Seric mci->mci_exitstat = EX_TEMPFAIL; 20557379Seric if (mci->mci_errno == 0) 20657379Seric mci->mci_errno = errno; 20757379Seric if (mci->mci_state != MCIS_CLOSED) 20857379Seric smtpquit(m, mci, e); 20954967Seric return; 21053751Seric 21153751Seric unavailable: 21253751Seric mci->mci_exitstat = EX_UNAVAILABLE; 21353751Seric mci->mci_errno = errno; 21453751Seric smtpquit(m, mci, e); 21554967Seric return; 21653751Seric } 21759285Seric /* 21860210Seric ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 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 { 23969650Seric if (strstr(line, "ESMTP ") != NULL) 24069650Seric mci->mci_flags |= MCIF_ESMTP; 24169650Seric if (strstr(line, "8BIT OK") != NULL) 24269650Seric mci->mci_flags |= MCIF_8BITOK; 24360210Seric } 24460210Seric /* 24559285Seric ** HELO_OPTIONS -- process the options on a HELO line. 24659285Seric ** 24759285Seric ** Parameters: 24859285Seric ** line -- the response line. 24967893Seric ** firstline -- set if this is the first line of the reply. 25059285Seric ** m -- the mailer. 25159285Seric ** mci -- the mailer connection info. 25259285Seric ** e -- the envelope. 25359285Seric ** 25459285Seric ** Returns: 25559285Seric ** none. 25659285Seric */ 25753751Seric 25859285Seric void 25967893Seric helo_options(line, firstline, m, mci, e) 26059285Seric char *line; 26167893Seric bool firstline; 26259285Seric MAILER *m; 26359285Seric register MCI *mci; 26459285Seric ENVELOPE *e; 26559285Seric { 26659285Seric register char *p; 26759285Seric 26867971Seric if (firstline) 26967893Seric return; 27067893Seric 27168706Seric if (strlen(line) < (SIZE_T) 5) 27259285Seric return; 27359285Seric line += 4; 27459285Seric p = strchr(line, ' '); 27559285Seric if (p != NULL) 27659285Seric *p++ = '\0'; 27759285Seric if (strcasecmp(line, "size") == 0) 27859285Seric { 27959285Seric mci->mci_flags |= MCIF_SIZE; 28059285Seric if (p != NULL) 28159285Seric mci->mci_maxsize = atol(p); 28259285Seric } 28359285Seric else if (strcasecmp(line, "8bitmime") == 0) 28465870Seric { 28559285Seric mci->mci_flags |= MCIF_8BITMIME; 28665870Seric mci->mci_flags &= ~MCIF_7BIT; 28765870Seric } 28859285Seric else if (strcasecmp(line, "expn") == 0) 28959285Seric mci->mci_flags |= MCIF_EXPN; 29068606Seric else if (strcasecmp(line, "x-dsn-03") == 0) 29167880Seric mci->mci_flags |= MCIF_DSN; 29259285Seric } 29359285Seric /* 29459285Seric ** SMTPMAILFROM -- send MAIL command 29559285Seric ** 29659285Seric ** Parameters: 29759285Seric ** m -- the mailer. 29859285Seric ** mci -- the mailer connection structure. 29959285Seric ** e -- the envelope (including the sender to specify). 30059285Seric */ 30159285Seric 30269748Seric int 30353751Seric smtpmailfrom(m, mci, e) 30453751Seric struct mailer *m; 30554967Seric MCI *mci; 30653751Seric ENVELOPE *e; 30753751Seric { 30853751Seric int r; 30965494Seric char *bufp; 31067887Seric char *bodytype; 31168528Seric char buf[MAXNAME + 1]; 31259285Seric char optbuf[MAXLINE]; 31353751Seric 31463753Seric if (tTd(18, 2)) 31557943Seric printf("smtpmailfrom: CurHost=%s\n", CurHostName); 31657943Seric 31759285Seric /* set up appropriate options to include */ 31864254Seric if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0) 31959285Seric sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 32059285Seric else 32159285Seric strcpy(optbuf, ""); 32259285Seric 32367887Seric bodytype = e->e_bodytype; 32467887Seric if (bitset(MCIF_8BITMIME, mci->mci_flags)) 32567417Seric { 32667887Seric if (bodytype == NULL && 32767887Seric bitset(MM_MIME8BIT, MimeMode) && 32867887Seric bitset(EF_HAS8BIT, e->e_flags) && 32967887Seric !bitnset(M_8BITS, m->m_flags)) 33067887Seric bodytype = "8BITMIME"; 33167887Seric if (bodytype != NULL) 33267417Seric { 33367417Seric strcat(optbuf, " BODY="); 33467887Seric strcat(optbuf, bodytype); 33567417Seric } 33667417Seric } 33767995Seric else if (bitnset(M_8BITS, m->m_flags) || 33869104Seric !bitset(EF_HAS8BIT, e->e_flags)) 33967887Seric { 34067887Seric /* just pass it through */ 34167887Seric } 34269480Seric #if MIME8TO7 34367887Seric else if (bitset(MM_CVTMIME, MimeMode) && 34468884Seric (!bitset(MM_PASS8BIT, MimeMode) || 34568884Seric bitset(EF_IS_MIME, e->e_flags))) 34667887Seric { 34767887Seric /* must convert from 8bit MIME format to 7bit encoded */ 34867887Seric mci->mci_flags |= MCIF_CVT8TO7; 34967887Seric } 35069480Seric #endif 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); 35568857Seric mci->mci_status = "5.6.3"; 35667887Seric return EX_DATAERR; 35767887Seric } 35867417Seric 35967963Seric if (bitset(MCIF_DSN, mci->mci_flags)) 36067880Seric { 36167963Seric if (e->e_envid != NULL) 36267963Seric { 36367963Seric strcat(optbuf, " ENVID="); 36467963Seric strcat(optbuf, e->e_envid); 36567963Seric } 36668559Seric 36768559Seric /* RET= parameter */ 36868559Seric if (bitset(EF_RET_PARAM, e->e_flags)) 36968559Seric { 37068559Seric strcat(optbuf, " RET="); 37168559Seric if (bitset(EF_NO_BODY_RETN, e->e_flags)) 37268559Seric strcat(optbuf, "HDRS"); 37368559Seric else 37468559Seric strcat(optbuf, "FULL"); 37568559Seric } 37667880Seric } 37767880Seric 3789315Seric /* 3794865Seric ** Send the MAIL command. 3804865Seric ** Designates the sender. 3814865Seric */ 3824796Seric 38353751Seric mci->mci_state = MCIS_ACTIVE; 38453751Seric 38559540Seric if (bitset(EF_RESPONSE, e->e_flags) && 38659540Seric !bitnset(M_NO_NULL_FROM, m->m_flags)) 38758680Seric (void) strcpy(buf, ""); 38858680Seric else 38968529Seric expand("\201g", buf, sizeof buf, e); 39065494Seric if (buf[0] == '<') 39165494Seric { 39265494Seric /* strip off <angle brackets> (put back on below) */ 39365494Seric bufp = &buf[strlen(buf) - 1]; 39465494Seric if (*bufp == '>') 39565494Seric *bufp = '\0'; 39665494Seric bufp = &buf[1]; 39765494Seric } 39865494Seric else 39965494Seric bufp = buf; 40067472Seric if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) || 40110688Seric !bitnset(M_FROMPATH, m->m_flags)) 4028436Seric { 40365494Seric smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf); 4048436Seric } 4058436Seric else 4068436Seric { 40759285Seric smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 40865494Seric *bufp == '@' ? ',' : ':', bufp, optbuf); 4098436Seric } 41061093Seric SmtpPhase = mci->mci_phase = "client MAIL"; 41153751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 41259285Seric r = reply(m, mci, e, TimeOuts.to_mail, NULL); 41368811Seric if (r < 0 || r == 421) 41453751Seric { 41568811Seric /* communications failure/service shutting down */ 41653751Seric mci->mci_exitstat = EX_TEMPFAIL; 41753751Seric mci->mci_errno = errno; 41853751Seric smtpquit(m, mci, e); 41953751Seric return EX_TEMPFAIL; 42053751Seric } 42168811Seric else if (REPLYTYPE(r) == 4) 42268811Seric { 42368811Seric return EX_TEMPFAIL; 42468811Seric } 4257963Seric else if (r == 250) 42653751Seric { 42753751Seric return EX_OK; 42853751Seric } 42968857Seric else if (r == 501) 43068075Seric { 43168857Seric /* syntax error in arguments */ 43268857Seric mci->mci_status = "5.5.2"; 43368075Seric return EX_DATAERR; 43468075Seric } 43568857Seric else if (r == 553) 43668857Seric { 43768857Seric /* mailbox name not allowed */ 43868857Seric mci->mci_status = "5.1.3"; 43968857Seric return EX_DATAERR; 44068857Seric } 4417963Seric else if (r == 552) 44253751Seric { 44368811Seric /* exceeded storage allocation */ 44468857Seric mci->mci_status = "5.2.2"; 44553751Seric return EX_UNAVAILABLE; 44653751Seric } 44714913Seric 44858008Seric #ifdef LOG 44958020Seric if (LogLevel > 1) 45058008Seric { 45167860Seric syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s", 45267860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 45358008Seric } 45458008Seric #endif 45558008Seric 45614913Seric /* protocol error -- close up */ 45753751Seric smtpquit(m, mci, e); 45853751Seric return EX_PROTOCOL; 4594684Seric } 4604684Seric /* 4614976Seric ** SMTPRCPT -- designate recipient. 4624797Seric ** 4634797Seric ** Parameters: 4644865Seric ** to -- address of recipient. 46510175Seric ** m -- the mailer we are sending to. 46657379Seric ** mci -- the connection info for this transaction. 46757379Seric ** e -- the envelope for this transaction. 4684797Seric ** 4694797Seric ** Returns: 4704865Seric ** exit status corresponding to recipient status. 4714797Seric ** 4724797Seric ** Side Effects: 4734865Seric ** Sends the mail via SMTP. 4744797Seric */ 4754797Seric 47669748Seric int 47753751Seric smtprcpt(to, m, mci, e) 4784865Seric ADDRESS *to; 47910175Seric register MAILER *m; 48054967Seric MCI *mci; 48153751Seric ENVELOPE *e; 4824797Seric { 4834797Seric register int r; 48467880Seric char optbuf[MAXLINE]; 48568857Seric extern char *smtptodsn(); 4864797Seric 48767880Seric strcpy(optbuf, ""); 48867880Seric if (bitset(MCIF_DSN, mci->mci_flags)) 48967880Seric { 49067987Seric /* NOTIFY= parameter */ 49168603Seric if (bitset(QHASNOTIFY, to->q_flags) && 49268603Seric bitset(QPRIMARY, to->q_flags)) 49367880Seric { 49468595Seric bool firstone = TRUE; 49568595Seric 49668595Seric strcat(optbuf, " NOTIFY="); 49768595Seric if (bitset(QPINGONSUCCESS, to->q_flags)) 49868595Seric { 49968595Seric strcat(optbuf, "SUCCESS"); 50068595Seric firstone = FALSE; 50168595Seric } 50268595Seric if (bitset(QPINGONFAILURE, to->q_flags)) 50368595Seric { 50468595Seric if (!firstone) 50568595Seric strcat(optbuf, ","); 50668595Seric strcat(optbuf, "FAILURE"); 50768595Seric firstone = FALSE; 50868595Seric } 50968595Seric if (bitset(QPINGONDELAY, to->q_flags)) 51068595Seric { 51168595Seric if (!firstone) 51268595Seric strcat(optbuf, ","); 51368595Seric strcat(optbuf, "DELAY"); 51468595Seric firstone = FALSE; 51568595Seric } 51668595Seric if (firstone) 51768595Seric strcat(optbuf, "NEVER"); 51867880Seric } 51967963Seric 52067987Seric /* ORCPT= parameter */ 52167987Seric if (to->q_orcpt != NULL) 52267987Seric { 52367987Seric strcat(optbuf, " ORCPT="); 52467987Seric strcat(optbuf, to->q_orcpt); 52567987Seric } 52667880Seric } 5274865Seric 52867880Seric smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf); 52967880Seric 53061093Seric SmtpPhase = mci->mci_phase = "client RCPT"; 53153751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 53259285Seric r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 53368867Seric to->q_rstatus = newstr(SmtpReplyBuffer); 53468857Seric to->q_status = smtptodsn(r); 5358005Seric if (r < 0 || REPLYTYPE(r) == 4) 53668857Seric return EX_TEMPFAIL; 5377963Seric else if (REPLYTYPE(r) == 2) 53868857Seric return EX_OK; 5397964Seric else if (r == 550 || r == 551 || r == 553) 54068857Seric return EX_NOUSER; 5417964Seric else if (r == 552 || r == 554) 54268857Seric return EX_UNAVAILABLE; 54358008Seric 54458008Seric #ifdef LOG 54558020Seric if (LogLevel > 1) 54658008Seric { 54767860Seric syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s", 54867860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 54958008Seric } 55058008Seric #endif 55158008Seric 5527964Seric return (EX_PROTOCOL); 5534797Seric } 5544797Seric /* 55510175Seric ** SMTPDATA -- send the data and clean up the transaction. 5564684Seric ** 5574684Seric ** Parameters: 5584865Seric ** m -- mailer being sent to. 5596980Seric ** e -- the envelope for this message. 5604684Seric ** 5614684Seric ** Returns: 5624976Seric ** exit status corresponding to DATA command. 5634684Seric ** 5644684Seric ** Side Effects: 5654865Seric ** none. 5664684Seric */ 5674684Seric 56863753Seric static jmp_buf CtxDataTimeout; 56968433Seric static void datatimeout(); 57063753Seric 57169748Seric int 57253740Seric smtpdata(m, mci, e) 5734865Seric struct mailer *m; 57454967Seric register MCI *mci; 5756980Seric register ENVELOPE *e; 5764684Seric { 5774684Seric register int r; 57863753Seric register EVENT *ev; 57963753Seric time_t timeout; 5804684Seric 5814797Seric /* 5824797Seric ** Send the data. 58310175Seric ** First send the command and check that it is ok. 58410175Seric ** Then send the data. 58510175Seric ** Follow it up with a dot to terminate. 58610175Seric ** Finally get the results of the transaction. 5874797Seric */ 5884797Seric 58910175Seric /* send the command and check ok to proceed */ 59053751Seric smtpmessage("DATA", m, mci); 59161093Seric SmtpPhase = mci->mci_phase = "client DATA 354"; 59253751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 59359285Seric r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 5948005Seric if (r < 0 || REPLYTYPE(r) == 4) 59557990Seric { 59657990Seric smtpquit(m, mci, e); 5974797Seric return (EX_TEMPFAIL); 59857990Seric } 5997963Seric else if (r == 554) 60057990Seric { 60157990Seric smtprset(m, mci, e); 6027963Seric return (EX_UNAVAILABLE); 60357990Seric } 6047963Seric else if (r != 354) 60557990Seric { 60658008Seric #ifdef LOG 60758020Seric if (LogLevel > 1) 60858008Seric { 60967860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s", 61067860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 61158008Seric } 61258008Seric #endif 61357990Seric smtprset(m, mci, e); 6147964Seric return (EX_PROTOCOL); 61557990Seric } 61610175Seric 61763757Seric /* 61863757Seric ** Set timeout around data writes. Make it at least large 61963757Seric ** enough for DNS timeouts on all recipients plus some fudge 62063757Seric ** factor. The main thing is that it should not be infinite. 62163757Seric */ 62263757Seric 62363753Seric if (setjmp(CtxDataTimeout) != 0) 62463753Seric { 62563753Seric mci->mci_errno = errno; 62663753Seric mci->mci_exitstat = EX_TEMPFAIL; 62763753Seric mci->mci_state = MCIS_ERROR; 62863753Seric syserr("451 timeout writing message to %s", mci->mci_host); 62963753Seric smtpquit(m, mci, e); 63063753Seric return EX_TEMPFAIL; 63163753Seric } 63263753Seric 63363787Seric timeout = e->e_msgsize / 16; 63468740Seric if (timeout < (time_t) 600) 63568740Seric timeout = (time_t) 600; 63668740Seric timeout += e->e_nrcpts * 300; 63763753Seric ev = setevent(timeout, datatimeout, 0); 63863753Seric 63967546Seric /* 64067546Seric ** Output the actual message. 64167546Seric */ 64267546Seric 64368228Seric (*e->e_puthdr)(mci, e->e_header, e); 64468228Seric (*e->e_putbody)(mci, e, NULL); 64510175Seric 64667546Seric /* 64767546Seric ** Cleanup after sending message. 64867546Seric */ 64967546Seric 65063753Seric clrevent(ev); 65163753Seric 65264718Seric if (ferror(mci->mci_out)) 65364718Seric { 65464718Seric /* error during processing -- don't send the dot */ 65564718Seric mci->mci_errno = EIO; 65664718Seric mci->mci_exitstat = EX_IOERR; 65764718Seric mci->mci_state = MCIS_ERROR; 65864718Seric smtpquit(m, mci, e); 65964718Seric return EX_IOERR; 66064718Seric } 66164718Seric 66210175Seric /* terminate the message */ 66353740Seric fprintf(mci->mci_out, ".%s", m->m_eol); 66463753Seric if (TrafficLogFile != NULL) 66563753Seric fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 66658120Seric if (Verbose) 66758151Seric nmessage(">>> ."); 66810175Seric 66910175Seric /* check for the results of the transaction */ 67061093Seric SmtpPhase = mci->mci_phase = "client DATA 250"; 67153751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 67259285Seric r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 67353751Seric if (r < 0) 67457990Seric { 67557990Seric smtpquit(m, mci, e); 6764797Seric return (EX_TEMPFAIL); 67757990Seric } 67853751Seric mci->mci_state = MCIS_OPEN; 67958917Seric e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 68053751Seric if (REPLYTYPE(r) == 4) 68153751Seric return (EX_TEMPFAIL); 6827963Seric else if (r == 250) 6837963Seric return (EX_OK); 6847963Seric else if (r == 552 || r == 554) 6857963Seric return (EX_UNAVAILABLE); 68658008Seric #ifdef LOG 68758020Seric if (LogLevel > 1) 68858008Seric { 68967860Seric syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s", 69067860Seric e->e_id, mci->mci_host, SmtpReplyBuffer); 69158008Seric } 69258008Seric #endif 6937964Seric return (EX_PROTOCOL); 6944684Seric } 69563753Seric 69663753Seric 69768433Seric static void 69863753Seric datatimeout() 69963753Seric { 70063753Seric longjmp(CtxDataTimeout, 1); 70163753Seric } 7024684Seric /* 7034865Seric ** SMTPQUIT -- close the SMTP connection. 7044865Seric ** 7054865Seric ** Parameters: 70615535Seric ** m -- a pointer to the mailer. 7074865Seric ** 7084865Seric ** Returns: 7094865Seric ** none. 7104865Seric ** 7114865Seric ** Side Effects: 7124865Seric ** sends the final protocol and closes the connection. 7134865Seric */ 7144865Seric 71569748Seric void 71653751Seric smtpquit(m, mci, e) 71753751Seric register MAILER *m; 71854967Seric register MCI *mci; 71953751Seric ENVELOPE *e; 7204865Seric { 72164822Seric bool oldSuprErrs = SuprErrs; 7224865Seric 72364822Seric /* 72464822Seric ** Suppress errors here -- we may be processing a different 72564822Seric ** job when we do the quit connection, and we don't want the 72664822Seric ** new job to be penalized for something that isn't it's 72764822Seric ** problem. 72864822Seric */ 72964822Seric 73064822Seric SuprErrs = TRUE; 73164822Seric 73254967Seric /* send the quit message if we haven't gotten I/O error */ 73353751Seric if (mci->mci_state != MCIS_ERROR) 7349391Seric { 73561093Seric SmtpPhase = "client QUIT"; 73653751Seric smtpmessage("QUIT", m, mci); 73759285Seric (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 73864822Seric SuprErrs = oldSuprErrs; 73953740Seric if (mci->mci_state == MCIS_CLOSED) 74064822Seric { 74164822Seric SuprErrs = oldSuprErrs; 74210159Seric return; 74364822Seric } 7449391Seric } 7459391Seric 74652676Seric /* now actually close the connection and pick up the zombie */ 74765194Seric (void) endmailer(mci, e, NULL); 74864822Seric 74964822Seric SuprErrs = oldSuprErrs; 7504865Seric } 7514865Seric /* 75254967Seric ** SMTPRSET -- send a RSET (reset) command 75354967Seric */ 75454967Seric 75569748Seric void 75654967Seric smtprset(m, mci, e) 75754967Seric register MAILER *m; 75854967Seric register MCI *mci; 75954967Seric ENVELOPE *e; 76054967Seric { 76154967Seric int r; 76254967Seric 76361093Seric SmtpPhase = "client RSET"; 76454967Seric smtpmessage("RSET", m, mci); 76559285Seric r = reply(m, mci, e, TimeOuts.to_rset, NULL); 76657734Seric if (r < 0) 76757734Seric mci->mci_state = MCIS_ERROR; 76854967Seric else if (REPLYTYPE(r) == 2) 76957734Seric { 77057734Seric mci->mci_state = MCIS_OPEN; 77157734Seric return; 77257734Seric } 77357734Seric smtpquit(m, mci, e); 77454967Seric } 77554967Seric /* 77658867Seric ** SMTPPROBE -- check the connection state 77754967Seric */ 77854967Seric 77969748Seric int 78058867Seric smtpprobe(mci) 78154967Seric register MCI *mci; 78254967Seric { 78354967Seric int r; 78454967Seric MAILER *m = mci->mci_mailer; 78554967Seric extern ENVELOPE BlankEnvelope; 78654967Seric ENVELOPE *e = &BlankEnvelope; 78754967Seric 78861093Seric SmtpPhase = "client probe"; 78958867Seric smtpmessage("RSET", m, mci); 79059285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 79158061Seric if (r < 0 || REPLYTYPE(r) != 2) 79254967Seric smtpquit(m, mci, e); 79354967Seric return r; 79454967Seric } 79554967Seric /* 7964684Seric ** REPLY -- read arpanet reply 7974684Seric ** 7984684Seric ** Parameters: 79910175Seric ** m -- the mailer we are reading the reply from. 80057379Seric ** mci -- the mailer connection info structure. 80157379Seric ** e -- the current envelope. 80257379Seric ** timeout -- the timeout for reads. 80369107Seric ** pfunc -- processing function called on each line of response. 80469107Seric ** If null, no special processing is done. 8054684Seric ** 8064684Seric ** Returns: 8074684Seric ** reply code it reads. 8084684Seric ** 8094684Seric ** Side Effects: 8104684Seric ** flushes the mail file. 8114684Seric */ 8124684Seric 81369748Seric int 81459285Seric reply(m, mci, e, timeout, pfunc) 81553751Seric MAILER *m; 81654967Seric MCI *mci; 81753751Seric ENVELOPE *e; 81859285Seric time_t timeout; 81959285Seric void (*pfunc)(); 8204684Seric { 82159014Seric register char *bufp; 82259014Seric register int r; 82359285Seric bool firstline = TRUE; 82458957Seric char junkbuf[MAXLINE]; 82558957Seric 82657379Seric if (mci->mci_out != NULL) 82757379Seric (void) fflush(mci->mci_out); 8284684Seric 8297677Seric if (tTd(18, 1)) 8304796Seric printf("reply\n"); 8314796Seric 8327356Seric /* 8337356Seric ** Read the input line, being careful not to hang. 8347356Seric */ 8357356Seric 83659014Seric for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 8374684Seric { 8387356Seric register char *p; 83953751Seric extern time_t curtime(); 8404684Seric 8417685Seric /* actually do the read */ 84253751Seric if (e->e_xfp != NULL) 84353751Seric (void) fflush(e->e_xfp); /* for debugging */ 8447356Seric 84510054Seric /* if we are in the process of closing just give the code */ 84653740Seric if (mci->mci_state == MCIS_CLOSED) 84710054Seric return (SMTPCLOSING); 84810054Seric 84958680Seric if (mci->mci_out != NULL) 85058680Seric fflush(mci->mci_out); 85158680Seric 85210054Seric /* get the line from the other side */ 85361093Seric p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 85453751Seric mci->mci_lastuse = curtime(); 85553751Seric 85610054Seric if (p == NULL) 85710131Seric { 85863753Seric bool oldholderrs; 85910148Seric 86021065Seric /* if the remote end closed early, fake an error */ 86121065Seric if (errno == 0) 86221065Seric # ifdef ECONNRESET 86321065Seric errno = ECONNRESET; 86456795Seric # else /* ECONNRESET */ 86521065Seric errno = EPIPE; 86656795Seric # endif /* ECONNRESET */ 86721065Seric 86857379Seric mci->mci_errno = errno; 86957642Seric mci->mci_exitstat = EX_TEMPFAIL; 87063753Seric oldholderrs = HoldErrs; 87163753Seric HoldErrs = TRUE; 87263753Seric usrerr("451 reply: read error from %s", mci->mci_host); 87363753Seric 87410420Seric /* if debugging, pause so we can see state */ 87510420Seric if (tTd(18, 100)) 87610420Seric pause(); 87754967Seric mci->mci_state = MCIS_ERROR; 87853751Seric smtpquit(m, mci, e); 879*69881Seric #if XDEBUG 88063753Seric { 88163753Seric char wbuf[MAXLINE]; 88264082Seric char *p = wbuf; 88364082Seric if (e->e_to != NULL) 88464082Seric { 88564082Seric sprintf(p, "%s... ", e->e_to); 88664082Seric p += strlen(p); 88764082Seric } 88864082Seric sprintf(p, "reply(%s) during %s", 88964082Seric mci->mci_host, SmtpPhase); 89063753Seric checkfd012(wbuf); 89163753Seric } 89263753Seric #endif 89363753Seric HoldErrs = oldholderrs; 89410054Seric return (-1); 89510131Seric } 89658957Seric fixcrlf(bufp, TRUE); 89710054Seric 89863753Seric /* EHLO failure is not a real error */ 89963753Seric if (e->e_xfp != NULL && (bufp[0] == '4' || 90063753Seric (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 90114900Seric { 90214900Seric /* serious error -- log the previous command */ 90364071Seric if (SmtpNeedIntro) 90464071Seric { 90564071Seric /* inform user who we are chatting with */ 90664071Seric fprintf(CurEnv->e_xfp, 90764071Seric "... while talking to %s:\n", 90864071Seric CurHostName); 90964071Seric SmtpNeedIntro = FALSE; 91064071Seric } 91159014Seric if (SmtpMsgBuffer[0] != '\0') 91259014Seric fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 91359014Seric SmtpMsgBuffer[0] = '\0'; 91414900Seric 91514900Seric /* now log the message as from the other side */ 91658957Seric fprintf(e->e_xfp, "<<< %s\n", bufp); 91714900Seric } 91814900Seric 91914900Seric /* display the input for verbose mode */ 92058120Seric if (Verbose) 92159956Seric nmessage("050 %s", bufp); 9227356Seric 92359285Seric /* process the line */ 92467893Seric if (pfunc != NULL) 92567893Seric (*pfunc)(bufp, firstline, m, mci, e); 92659285Seric 92759285Seric firstline = FALSE; 92859285Seric 9297356Seric /* if continuation is required, we can go on */ 93059014Seric if (bufp[3] == '-') 9314684Seric continue; 9327356Seric 93359014Seric /* ignore improperly formated input */ 93459014Seric if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 93559014Seric continue; 93659014Seric 9377356Seric /* decode the reply code */ 93859014Seric r = atoi(bufp); 9397356Seric 9407356Seric /* extra semantics: 0xx codes are "informational" */ 94159014Seric if (r >= 100) 94259014Seric break; 94359014Seric } 9447356Seric 94559014Seric /* 94659014Seric ** Now look at SmtpReplyBuffer -- only care about the first 94759014Seric ** line of the response from here on out. 94859014Seric */ 94958061Seric 95059014Seric /* save temporary failure messages for posterity */ 95159014Seric if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 95259014Seric (void) strcpy(SmtpError, SmtpReplyBuffer); 9539391Seric 95459014Seric /* reply code 421 is "Service Shutting Down" */ 95559014Seric if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 95659014Seric { 95759014Seric /* send the quit protocol */ 95859014Seric mci->mci_state = MCIS_SSD; 95959014Seric smtpquit(m, mci, e); 9604684Seric } 96159014Seric 96259014Seric return (r); 9634684Seric } 9644796Seric /* 9654865Seric ** SMTPMESSAGE -- send message to server 9664796Seric ** 9674796Seric ** Parameters: 9684796Seric ** f -- format 96910175Seric ** m -- the mailer to control formatting. 9704796Seric ** a, b, c -- parameters 9714796Seric ** 9724796Seric ** Returns: 9734796Seric ** none. 9744796Seric ** 9754796Seric ** Side Effects: 97653740Seric ** writes message to mci->mci_out. 9774796Seric */ 9784796Seric 9794865Seric /*VARARGS1*/ 98069748Seric void 98157642Seric #ifdef __STDC__ 98257642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...) 98357642Seric #else 98457642Seric smtpmessage(f, m, mci, va_alist) 9854796Seric char *f; 98610175Seric MAILER *m; 98754967Seric MCI *mci; 98857642Seric va_dcl 98957642Seric #endif 9904796Seric { 99156852Seric VA_LOCAL_DECL 99256852Seric 99357135Seric VA_START(mci); 99456852Seric (void) vsprintf(SmtpMsgBuffer, f, ap); 99556852Seric VA_END; 99658680Seric 99758120Seric if (tTd(18, 1) || Verbose) 99858151Seric nmessage(">>> %s", SmtpMsgBuffer); 99963753Seric if (TrafficLogFile != NULL) 100063753Seric fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 100153740Seric if (mci->mci_out != NULL) 100258680Seric { 100353740Seric fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 100454967Seric m == NULL ? "\r\n" : m->m_eol); 100558680Seric } 100659149Seric else if (tTd(18, 1)) 100758725Seric { 100859149Seric printf("smtpmessage: NULL mci_out\n"); 100958725Seric } 10104796Seric } 10115182Seric 101256795Seric # endif /* SMTP */ 1013