122716Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333731Sbostic * Copyright (c) 1988 Regents of the University of California. 433731Sbostic * All rights reserved. 533731Sbostic * 642831Sbostic * %sccs.include.redist.c% 733731Sbostic */ 822716Sdist 933731Sbostic # include "sendmail.h" 1022716Sdist 1133731Sbostic #ifndef lint 1233731Sbostic #ifdef SMTP 13*61093Seric static char sccsid[] = "@(#)usersmtp.c 6.33 (Berkeley) 06/03/93 (with SMTP)"; 1433731Sbostic #else 15*61093Seric static char sccsid[] = "@(#)usersmtp.c 6.33 (Berkeley) 06/03/93 (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 */ 3858671Seric 3958671Seric #ifdef __STDC__ 4058671Seric extern smtpmessage(char *f, MAILER *m, MCI *mci, ...); 4158671Seric #endif 429391Seric /* 434865Seric ** SMTPINIT -- initialize SMTP. 444684Seric ** 454865Seric ** Opens the connection and sends the initial protocol. 464684Seric ** 474684Seric ** Parameters: 484865Seric ** m -- mailer to create connection to. 494865Seric ** pvp -- pointer to parameter vector to pass to 504865Seric ** the mailer. 514684Seric ** 524684Seric ** Returns: 5354967Seric ** none. 544684Seric ** 554684Seric ** Side Effects: 564865Seric ** creates connection and sends initial protocol. 574684Seric */ 584684Seric 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 6957379Seric if (tTd(17, 1)) 7057379Seric { 7157379Seric printf("smtpinit "); 7257379Seric mci_dump(mci); 7357379Seric } 7457379Seric 754865Seric /* 764865Seric ** Open the connection to the mailer. 774865Seric */ 784684Seric 7921065Seric SmtpError[0] = '\0'; 8057379Seric CurHostName = mci->mci_host; /* XXX UGLY XXX */ 8154967Seric switch (mci->mci_state) 826051Seric { 8354967Seric case MCIS_ACTIVE: 8454967Seric /* need to clear old information */ 8554967Seric smtprset(m, mci, e); 8657734Seric /* fall through */ 8715139Seric 8854967Seric case MCIS_OPEN: 8954967Seric return; 9054967Seric 9154967Seric case MCIS_ERROR: 9254967Seric case MCIS_SSD: 9354967Seric /* shouldn't happen */ 9454967Seric smtpquit(m, mci, e); 9557734Seric /* fall through */ 9654967Seric 9754967Seric case MCIS_CLOSED: 9858151Seric syserr("451 smtpinit: state CLOSED"); 9954967Seric return; 10054967Seric 10154967Seric case MCIS_OPENING: 10254967Seric break; 1036051Seric } 1044796Seric 10554967Seric mci->mci_state = MCIS_OPENING; 10654967Seric 1074865Seric /* 1084865Seric ** Get the greeting message. 10914913Seric ** This should appear spontaneously. Give it five minutes to 11014886Seric ** happen. 1114865Seric */ 1124797Seric 113*61093Seric SmtpPhase = mci->mci_phase = "client greeting"; 11453751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 11560210Seric r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check); 1168005Seric if (r < 0 || REPLYTYPE(r) != 2) 11752104Seric goto tempfail1; 1184684Seric 1194865Seric /* 1204976Seric ** Send the HELO command. 1217963Seric ** My mother taught me to always introduce myself. 1224976Seric */ 1234976Seric 12459285Seric if (bitnset(M_ESMTP, m->m_flags)) 12559285Seric mci->mci_flags |= MCIF_ESMTP; 12659285Seric 12759285Seric tryhelo: 12859285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 12959285Seric { 13059285Seric smtpmessage("EHLO %s", m, mci, MyHostName); 131*61093Seric SmtpPhase = mci->mci_phase = "client EHLO"; 13259285Seric } 13359285Seric else 13459285Seric { 13559285Seric smtpmessage("HELO %s", m, mci, MyHostName); 136*61093Seric SmtpPhase = mci->mci_phase = "client HELO"; 13759285Seric } 13853751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 13959285Seric r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 1408005Seric if (r < 0) 14152104Seric goto tempfail1; 1428005Seric else if (REPLYTYPE(r) == 5) 14359285Seric { 14459285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 14559285Seric { 14659285Seric /* try old SMTP instead */ 14759285Seric mci->mci_flags &= ~MCIF_ESMTP; 14859285Seric goto tryhelo; 14959285Seric } 15014913Seric goto unavailable; 15159285Seric } 1527963Seric else if (REPLYTYPE(r) != 2) 15352104Seric goto tempfail1; 1544976Seric 1554976Seric /* 15658957Seric ** Check to see if we actually ended up talking to ourself. 15758957Seric ** This means we didn't know about an alias or MX, or we managed 15858957Seric ** to connect to an echo server. 15958957Seric */ 16058957Seric 16159026Seric p = strchr(&SmtpReplyBuffer[4], ' '); 16258957Seric if (p != NULL) 16358957Seric *p == '\0'; 16459026Seric if (strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0) 16558957Seric { 16658957Seric syserr("553 %s config error: mail loops back to myself", 16758957Seric MyHostName); 16858957Seric mci->mci_exitstat = EX_CONFIG; 16958957Seric mci->mci_errno = 0; 17058957Seric smtpquit(m, mci, e); 17158957Seric return; 17258957Seric } 17358957Seric 17458957Seric /* 1759315Seric ** If this is expected to be another sendmail, send some internal 1769315Seric ** commands. 1779315Seric */ 1789315Seric 17910688Seric if (bitnset(M_INTERNAL, m->m_flags)) 1809315Seric { 1819315Seric /* tell it to be verbose */ 18253751Seric smtpmessage("VERB", m, mci); 18359285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 1849315Seric if (r < 0) 18552104Seric goto tempfail2; 1869315Seric } 1879315Seric 18853751Seric mci->mci_state = MCIS_OPEN; 18954967Seric return; 19053751Seric 19153751Seric tempfail1: 19253751Seric tempfail2: 19353751Seric mci->mci_exitstat = EX_TEMPFAIL; 19457379Seric if (mci->mci_errno == 0) 19557379Seric mci->mci_errno = errno; 19657379Seric if (mci->mci_state != MCIS_CLOSED) 19757379Seric smtpquit(m, mci, e); 19854967Seric return; 19953751Seric 20053751Seric unavailable: 20153751Seric mci->mci_exitstat = EX_UNAVAILABLE; 20253751Seric mci->mci_errno = errno; 20353751Seric smtpquit(m, mci, e); 20454967Seric return; 20553751Seric } 20659285Seric /* 20760210Seric ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 20860210Seric ** 20960210Seric ** 21060210Seric ** Parameters: 21160210Seric ** line -- the response line. 21260210Seric ** m -- the mailer. 21360210Seric ** mci -- the mailer connection info. 21460210Seric ** e -- the envelope. 21560210Seric ** 21660210Seric ** Returns: 21760210Seric ** none. 21860210Seric */ 21960210Seric 22060210Seric void 22160210Seric esmtp_check(line, m, mci, e) 22260210Seric char *line; 22360210Seric MAILER *m; 22460210Seric register MCI *mci; 22560210Seric ENVELOPE *e; 22660210Seric { 22760210Seric if (strlen(line) < 5) 22860210Seric return; 22960210Seric line += 4; 23060210Seric if (strncmp(line, "ESMTP ", 6) == 0) 23160210Seric mci->mci_flags |= MCIF_ESMTP; 23260210Seric } 23360210Seric /* 23459285Seric ** HELO_OPTIONS -- process the options on a HELO line. 23559285Seric ** 23659285Seric ** Parameters: 23759285Seric ** line -- the response line. 23859285Seric ** m -- the mailer. 23959285Seric ** mci -- the mailer connection info. 24059285Seric ** e -- the envelope. 24159285Seric ** 24259285Seric ** Returns: 24359285Seric ** none. 24459285Seric */ 24553751Seric 24659285Seric void 24759285Seric helo_options(line, m, mci, e) 24859285Seric char *line; 24959285Seric MAILER *m; 25059285Seric register MCI *mci; 25159285Seric ENVELOPE *e; 25259285Seric { 25359285Seric register char *p; 25459285Seric 25559285Seric if (strlen(line) < 5) 25659285Seric return; 25759285Seric line += 4; 25859285Seric p = strchr(line, ' '); 25959285Seric if (p != NULL) 26059285Seric *p++ = '\0'; 26159285Seric if (strcasecmp(line, "size") == 0) 26259285Seric { 26359285Seric mci->mci_flags |= MCIF_SIZE; 26459285Seric if (p != NULL) 26559285Seric mci->mci_maxsize = atol(p); 26659285Seric } 26759285Seric else if (strcasecmp(line, "8bitmime") == 0) 26859285Seric mci->mci_flags |= MCIF_8BITMIME; 26959285Seric else if (strcasecmp(line, "expn") == 0) 27059285Seric mci->mci_flags |= MCIF_EXPN; 27159285Seric } 27259285Seric /* 27359285Seric ** SMTPMAILFROM -- send MAIL command 27459285Seric ** 27559285Seric ** Parameters: 27659285Seric ** m -- the mailer. 27759285Seric ** mci -- the mailer connection structure. 27859285Seric ** e -- the envelope (including the sender to specify). 27959285Seric */ 28059285Seric 28153751Seric smtpmailfrom(m, mci, e) 28253751Seric struct mailer *m; 28354967Seric MCI *mci; 28453751Seric ENVELOPE *e; 28553751Seric { 28653751Seric int r; 28753751Seric char buf[MAXNAME]; 28859285Seric char optbuf[MAXLINE]; 28953751Seric 29057943Seric if (tTd(17, 2)) 29157943Seric printf("smtpmailfrom: CurHost=%s\n", CurHostName); 29257943Seric 29359285Seric /* set up appropriate options to include */ 29459285Seric if (bitset(MCIF_SIZE, mci->mci_flags)) 29559285Seric sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 29659285Seric else 29759285Seric strcpy(optbuf, ""); 29859285Seric 2999315Seric /* 3004865Seric ** Send the MAIL command. 3014865Seric ** Designates the sender. 3024865Seric */ 3034796Seric 30453751Seric mci->mci_state = MCIS_ACTIVE; 30553751Seric 30659540Seric if (bitset(EF_RESPONSE, e->e_flags) && 30759540Seric !bitnset(M_NO_NULL_FROM, m->m_flags)) 30858680Seric (void) strcpy(buf, ""); 30958680Seric else 31058680Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 31153751Seric if (e->e_from.q_mailer == LocalMailer || 31210688Seric !bitnset(M_FROMPATH, m->m_flags)) 3138436Seric { 31459285Seric smtpmessage("MAIL From:<%s>%s", m, mci, buf, optbuf); 3158436Seric } 3168436Seric else 3178436Seric { 31859285Seric smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 31959285Seric buf[0] == '@' ? ',' : ':', buf, optbuf); 3208436Seric } 321*61093Seric SmtpPhase = mci->mci_phase = "client MAIL"; 32253751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 32359285Seric r = reply(m, mci, e, TimeOuts.to_mail, NULL); 3248005Seric if (r < 0 || REPLYTYPE(r) == 4) 32553751Seric { 32653751Seric mci->mci_exitstat = EX_TEMPFAIL; 32753751Seric mci->mci_errno = errno; 32853751Seric smtpquit(m, mci, e); 32953751Seric return EX_TEMPFAIL; 33053751Seric } 3317963Seric else if (r == 250) 33253751Seric { 33353751Seric mci->mci_exitstat = EX_OK; 33453751Seric return EX_OK; 33553751Seric } 3367963Seric else if (r == 552) 33753751Seric { 33853751Seric /* signal service unavailable */ 33953751Seric mci->mci_exitstat = EX_UNAVAILABLE; 34053751Seric smtpquit(m, mci, e); 34153751Seric return EX_UNAVAILABLE; 34253751Seric } 34314913Seric 34458008Seric #ifdef LOG 34558020Seric if (LogLevel > 1) 34658008Seric { 34758008Seric syslog(LOG_CRIT, "%s: SMTP MAIL protocol error: %s", 34858008Seric e->e_id, SmtpReplyBuffer); 34958008Seric } 35058008Seric #endif 35158008Seric 35214913Seric /* protocol error -- close up */ 35353751Seric smtpquit(m, mci, e); 35453751Seric mci->mci_exitstat = EX_PROTOCOL; 35553751Seric return EX_PROTOCOL; 3564684Seric } 3574684Seric /* 3584976Seric ** SMTPRCPT -- designate recipient. 3594797Seric ** 3604797Seric ** Parameters: 3614865Seric ** to -- address of recipient. 36210175Seric ** m -- the mailer we are sending to. 36357379Seric ** mci -- the connection info for this transaction. 36457379Seric ** e -- the envelope for this transaction. 3654797Seric ** 3664797Seric ** Returns: 3674865Seric ** exit status corresponding to recipient status. 3684797Seric ** 3694797Seric ** Side Effects: 3704865Seric ** Sends the mail via SMTP. 3714797Seric */ 3724797Seric 37353751Seric smtprcpt(to, m, mci, e) 3744865Seric ADDRESS *to; 37510175Seric register MAILER *m; 37654967Seric MCI *mci; 37753751Seric ENVELOPE *e; 3784797Seric { 3794797Seric register int r; 3804797Seric 38153751Seric smtpmessage("RCPT To:<%s>", m, mci, to->q_user); 3824865Seric 383*61093Seric SmtpPhase = mci->mci_phase = "client RCPT"; 38453751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 38559285Seric r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 3868005Seric if (r < 0 || REPLYTYPE(r) == 4) 3874865Seric return (EX_TEMPFAIL); 3887963Seric else if (REPLYTYPE(r) == 2) 3897963Seric return (EX_OK); 3907964Seric else if (r == 550 || r == 551 || r == 553) 3917964Seric return (EX_NOUSER); 3927964Seric else if (r == 552 || r == 554) 3937964Seric return (EX_UNAVAILABLE); 39458008Seric 39558008Seric #ifdef LOG 39658020Seric if (LogLevel > 1) 39758008Seric { 39858008Seric syslog(LOG_CRIT, "%s: SMTP RCPT protocol error: %s", 39958008Seric e->e_id, SmtpReplyBuffer); 40058008Seric } 40158008Seric #endif 40258008Seric 4037964Seric return (EX_PROTOCOL); 4044797Seric } 4054797Seric /* 40610175Seric ** SMTPDATA -- send the data and clean up the transaction. 4074684Seric ** 4084684Seric ** Parameters: 4094865Seric ** m -- mailer being sent to. 4106980Seric ** e -- the envelope for this message. 4114684Seric ** 4124684Seric ** Returns: 4134976Seric ** exit status corresponding to DATA command. 4144684Seric ** 4154684Seric ** Side Effects: 4164865Seric ** none. 4174684Seric */ 4184684Seric 41953740Seric smtpdata(m, mci, e) 4204865Seric struct mailer *m; 42154967Seric register MCI *mci; 4226980Seric register ENVELOPE *e; 4234684Seric { 4244684Seric register int r; 4254684Seric 4264797Seric /* 4274797Seric ** Send the data. 42810175Seric ** First send the command and check that it is ok. 42910175Seric ** Then send the data. 43010175Seric ** Follow it up with a dot to terminate. 43110175Seric ** Finally get the results of the transaction. 4324797Seric */ 4334797Seric 43410175Seric /* send the command and check ok to proceed */ 43553751Seric smtpmessage("DATA", m, mci); 436*61093Seric SmtpPhase = mci->mci_phase = "client DATA 354"; 43753751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 43859285Seric r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 4398005Seric if (r < 0 || REPLYTYPE(r) == 4) 44057990Seric { 44157990Seric smtpquit(m, mci, e); 4424797Seric return (EX_TEMPFAIL); 44357990Seric } 4447963Seric else if (r == 554) 44557990Seric { 44657990Seric smtprset(m, mci, e); 4477963Seric return (EX_UNAVAILABLE); 44857990Seric } 4497963Seric else if (r != 354) 45057990Seric { 45158008Seric #ifdef LOG 45258020Seric if (LogLevel > 1) 45358008Seric { 45458008Seric syslog(LOG_CRIT, "%s: SMTP DATA-1 protocol error: %s", 45558008Seric e->e_id, SmtpReplyBuffer); 45658008Seric } 45758008Seric #endif 45857990Seric smtprset(m, mci, e); 4597964Seric return (EX_PROTOCOL); 46057990Seric } 46110175Seric 46210175Seric /* now output the actual message */ 46353751Seric (*e->e_puthdr)(mci->mci_out, m, e); 46453740Seric putline("\n", mci->mci_out, m); 46559730Seric (*e->e_putbody)(mci->mci_out, m, e, NULL); 46610175Seric 46710175Seric /* terminate the message */ 46853740Seric fprintf(mci->mci_out, ".%s", m->m_eol); 46958120Seric if (Verbose) 47058151Seric nmessage(">>> ."); 47110175Seric 47210175Seric /* check for the results of the transaction */ 473*61093Seric SmtpPhase = mci->mci_phase = "client DATA 250"; 47453751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 47559285Seric r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 47653751Seric if (r < 0) 47757990Seric { 47857990Seric smtpquit(m, mci, e); 4794797Seric return (EX_TEMPFAIL); 48057990Seric } 48153751Seric mci->mci_state = MCIS_OPEN; 48258917Seric e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 48353751Seric if (REPLYTYPE(r) == 4) 48453751Seric return (EX_TEMPFAIL); 4857963Seric else if (r == 250) 4867963Seric return (EX_OK); 4877963Seric else if (r == 552 || r == 554) 4887963Seric return (EX_UNAVAILABLE); 48958008Seric #ifdef LOG 49058020Seric if (LogLevel > 1) 49158008Seric { 49258008Seric syslog(LOG_CRIT, "%s: SMTP DATA-2 protocol error: %s", 49358008Seric e->e_id, SmtpReplyBuffer); 49458008Seric } 49558008Seric #endif 4967964Seric return (EX_PROTOCOL); 4974684Seric } 4984684Seric /* 4994865Seric ** SMTPQUIT -- close the SMTP connection. 5004865Seric ** 5014865Seric ** Parameters: 50215535Seric ** m -- a pointer to the mailer. 5034865Seric ** 5044865Seric ** Returns: 5054865Seric ** none. 5064865Seric ** 5074865Seric ** Side Effects: 5084865Seric ** sends the final protocol and closes the connection. 5094865Seric */ 5104865Seric 51153751Seric smtpquit(m, mci, e) 51253751Seric register MAILER *m; 51354967Seric register MCI *mci; 51453751Seric ENVELOPE *e; 5154865Seric { 5169391Seric int i; 5174865Seric 51854967Seric /* send the quit message if we haven't gotten I/O error */ 51953751Seric if (mci->mci_state != MCIS_ERROR) 5209391Seric { 521*61093Seric SmtpPhase = "client QUIT"; 52253751Seric smtpmessage("QUIT", m, mci); 52359285Seric (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 52453740Seric if (mci->mci_state == MCIS_CLOSED) 52510159Seric return; 5269391Seric } 5279391Seric 52852676Seric /* now actually close the connection and pick up the zombie */ 52958846Seric i = endmailer(mci, e, m->m_argv); 5309391Seric if (i != EX_OK) 53158151Seric syserr("451 smtpquit %s: stat %d", m->m_argv[0], i); 5324865Seric } 5334865Seric /* 53454967Seric ** SMTPRSET -- send a RSET (reset) command 53554967Seric */ 53654967Seric 53754967Seric smtprset(m, mci, e) 53854967Seric register MAILER *m; 53954967Seric register MCI *mci; 54054967Seric ENVELOPE *e; 54154967Seric { 54254967Seric int r; 54354967Seric 544*61093Seric SmtpPhase = "client RSET"; 54554967Seric smtpmessage("RSET", m, mci); 54659285Seric r = reply(m, mci, e, TimeOuts.to_rset, NULL); 54757734Seric if (r < 0) 54857734Seric mci->mci_state = MCIS_ERROR; 54954967Seric else if (REPLYTYPE(r) == 2) 55057734Seric { 55157734Seric mci->mci_state = MCIS_OPEN; 55257734Seric return; 55357734Seric } 55457734Seric smtpquit(m, mci, e); 55554967Seric } 55654967Seric /* 55758867Seric ** SMTPPROBE -- check the connection state 55854967Seric */ 55954967Seric 56058867Seric smtpprobe(mci) 56154967Seric register MCI *mci; 56254967Seric { 56354967Seric int r; 56454967Seric MAILER *m = mci->mci_mailer; 56554967Seric extern ENVELOPE BlankEnvelope; 56654967Seric ENVELOPE *e = &BlankEnvelope; 56754967Seric 568*61093Seric SmtpPhase = "client probe"; 56958867Seric smtpmessage("RSET", m, mci); 57059285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 57158061Seric if (r < 0 || REPLYTYPE(r) != 2) 57254967Seric smtpquit(m, mci, e); 57354967Seric return r; 57454967Seric } 57554967Seric /* 5764684Seric ** REPLY -- read arpanet reply 5774684Seric ** 5784684Seric ** Parameters: 57910175Seric ** m -- the mailer we are reading the reply from. 58057379Seric ** mci -- the mailer connection info structure. 58157379Seric ** e -- the current envelope. 58257379Seric ** timeout -- the timeout for reads. 58359285Seric ** pfunc -- processing function for second and subsequent 58459285Seric ** lines of response -- if null, no special 58559285Seric ** processing is done. 5864684Seric ** 5874684Seric ** Returns: 5884684Seric ** reply code it reads. 5894684Seric ** 5904684Seric ** Side Effects: 5914684Seric ** flushes the mail file. 5924684Seric */ 5934684Seric 59459285Seric reply(m, mci, e, timeout, pfunc) 59553751Seric MAILER *m; 59654967Seric MCI *mci; 59753751Seric ENVELOPE *e; 59859285Seric time_t timeout; 59959285Seric void (*pfunc)(); 6004684Seric { 60159014Seric register char *bufp; 60259014Seric register int r; 60359285Seric bool firstline = TRUE; 60458957Seric char junkbuf[MAXLINE]; 60558957Seric 60657379Seric if (mci->mci_out != NULL) 60757379Seric (void) fflush(mci->mci_out); 6084684Seric 6097677Seric if (tTd(18, 1)) 6104796Seric printf("reply\n"); 6114796Seric 6127356Seric /* 6137356Seric ** Read the input line, being careful not to hang. 6147356Seric */ 6157356Seric 61659014Seric for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 6174684Seric { 6187356Seric register char *p; 61953751Seric extern time_t curtime(); 6204684Seric 6217685Seric /* actually do the read */ 62253751Seric if (e->e_xfp != NULL) 62353751Seric (void) fflush(e->e_xfp); /* for debugging */ 6247356Seric 62510054Seric /* if we are in the process of closing just give the code */ 62653740Seric if (mci->mci_state == MCIS_CLOSED) 62710054Seric return (SMTPCLOSING); 62810054Seric 62958680Seric if (mci->mci_out != NULL) 63058680Seric fflush(mci->mci_out); 63158680Seric 63210054Seric /* get the line from the other side */ 633*61093Seric p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 63453751Seric mci->mci_lastuse = curtime(); 63553751Seric 63610054Seric if (p == NULL) 63710131Seric { 63810148Seric extern char MsgBuf[]; /* err.c */ 63910148Seric 64021065Seric /* if the remote end closed early, fake an error */ 64121065Seric if (errno == 0) 64221065Seric # ifdef ECONNRESET 64321065Seric errno = ECONNRESET; 64456795Seric # else /* ECONNRESET */ 64521065Seric errno = EPIPE; 64656795Seric # endif /* ECONNRESET */ 64721065Seric 64857379Seric mci->mci_errno = errno; 64957642Seric mci->mci_exitstat = EX_TEMPFAIL; 65058151Seric message("451 %s: reply: read error from %s", 65157642Seric e->e_id == NULL ? "NOQUEUE" : e->e_id, 65257203Seric mci->mci_host); 65310420Seric /* if debugging, pause so we can see state */ 65410420Seric if (tTd(18, 100)) 65510420Seric pause(); 65610148Seric # ifdef LOG 65758020Seric if (LogLevel > 1) 65857203Seric syslog(LOG_INFO, "%s", &MsgBuf[4]); 65956795Seric # endif /* LOG */ 66054967Seric mci->mci_state = MCIS_ERROR; 66153751Seric smtpquit(m, mci, e); 66210054Seric return (-1); 66310131Seric } 66458957Seric fixcrlf(bufp, TRUE); 66510054Seric 66659014Seric if (e->e_xfp != NULL && strchr("45", bufp[0]) != NULL) 66714900Seric { 66814900Seric /* serious error -- log the previous command */ 66959014Seric if (SmtpMsgBuffer[0] != '\0') 67059014Seric fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 67159014Seric SmtpMsgBuffer[0] = '\0'; 67214900Seric 67314900Seric /* now log the message as from the other side */ 67458957Seric fprintf(e->e_xfp, "<<< %s\n", bufp); 67514900Seric } 67614900Seric 67714900Seric /* display the input for verbose mode */ 67858120Seric if (Verbose) 67959956Seric nmessage("050 %s", bufp); 6807356Seric 68159285Seric /* process the line */ 68259285Seric if (pfunc != NULL && !firstline) 68359285Seric (*pfunc)(bufp, m, mci, e); 68459285Seric 68559285Seric firstline = FALSE; 68659285Seric 6877356Seric /* if continuation is required, we can go on */ 68859014Seric if (bufp[3] == '-') 6894684Seric continue; 6907356Seric 69159014Seric /* ignore improperly formated input */ 69259014Seric if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 69359014Seric continue; 69459014Seric 6957356Seric /* decode the reply code */ 69659014Seric r = atoi(bufp); 6977356Seric 6987356Seric /* extra semantics: 0xx codes are "informational" */ 69959014Seric if (r >= 100) 70059014Seric break; 70159014Seric } 7027356Seric 70359014Seric /* 70459014Seric ** Now look at SmtpReplyBuffer -- only care about the first 70559014Seric ** line of the response from here on out. 70659014Seric */ 70758061Seric 70859014Seric /* save temporary failure messages for posterity */ 70959014Seric if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 71059014Seric (void) strcpy(SmtpError, SmtpReplyBuffer); 7119391Seric 71259014Seric /* reply code 421 is "Service Shutting Down" */ 71359014Seric if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 71459014Seric { 71559014Seric /* send the quit protocol */ 71659014Seric mci->mci_state = MCIS_SSD; 71759014Seric smtpquit(m, mci, e); 7184684Seric } 71959014Seric 72059014Seric return (r); 7214684Seric } 7224796Seric /* 7234865Seric ** SMTPMESSAGE -- send message to server 7244796Seric ** 7254796Seric ** Parameters: 7264796Seric ** f -- format 72710175Seric ** m -- the mailer to control formatting. 7284796Seric ** a, b, c -- parameters 7294796Seric ** 7304796Seric ** Returns: 7314796Seric ** none. 7324796Seric ** 7334796Seric ** Side Effects: 73453740Seric ** writes message to mci->mci_out. 7354796Seric */ 7364796Seric 7374865Seric /*VARARGS1*/ 73857642Seric #ifdef __STDC__ 73957642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...) 74057642Seric #else 74157642Seric smtpmessage(f, m, mci, va_alist) 7424796Seric char *f; 74310175Seric MAILER *m; 74454967Seric MCI *mci; 74557642Seric va_dcl 74657642Seric #endif 7474796Seric { 74856852Seric VA_LOCAL_DECL 74956852Seric 75057135Seric VA_START(mci); 75156852Seric (void) vsprintf(SmtpMsgBuffer, f, ap); 75256852Seric VA_END; 75358680Seric 75458120Seric if (tTd(18, 1) || Verbose) 75558151Seric nmessage(">>> %s", SmtpMsgBuffer); 75653740Seric if (mci->mci_out != NULL) 75758680Seric { 75853740Seric fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 75954967Seric m == NULL ? "\r\n" : m->m_eol); 76058680Seric } 76159149Seric else if (tTd(18, 1)) 76258725Seric { 76359149Seric printf("smtpmessage: NULL mci_out\n"); 76458725Seric } 7654796Seric } 7665182Seric 76756795Seric # endif /* SMTP */ 768