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*64651Seric static char sccsid[] = "@(#)usersmtp.c 8.10 (Berkeley) 09/25/93 (with SMTP)"; 1433731Sbostic #else 15*64651Seric static char sccsid[] = "@(#)usersmtp.c 8.10 (Berkeley) 09/25/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 */ 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 "); 7357379Seric mci_dump(mci); 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); 1188005Seric if (r < 0 || REPLYTYPE(r) != 2) 11952104Seric goto tempfail1; 1204684Seric 1214865Seric /* 1224976Seric ** Send the HELO command. 1237963Seric ** My mother taught me to always introduce myself. 1244976Seric */ 1254976Seric 12659285Seric if (bitnset(M_ESMTP, m->m_flags)) 12759285Seric mci->mci_flags |= MCIF_ESMTP; 12859285Seric 12959285Seric tryhelo: 13059285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 13159285Seric { 13259285Seric smtpmessage("EHLO %s", m, mci, MyHostName); 13361093Seric SmtpPhase = mci->mci_phase = "client EHLO"; 13459285Seric } 13559285Seric else 13659285Seric { 13759285Seric smtpmessage("HELO %s", m, mci, MyHostName); 13861093Seric SmtpPhase = mci->mci_phase = "client HELO"; 13959285Seric } 14053751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 14159285Seric r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 1428005Seric if (r < 0) 14352104Seric goto tempfail1; 1448005Seric else if (REPLYTYPE(r) == 5) 14559285Seric { 14659285Seric if (bitset(MCIF_ESMTP, mci->mci_flags)) 14759285Seric { 14859285Seric /* try old SMTP instead */ 14959285Seric mci->mci_flags &= ~MCIF_ESMTP; 15059285Seric goto tryhelo; 15159285Seric } 15214913Seric goto unavailable; 15359285Seric } 1547963Seric else if (REPLYTYPE(r) != 2) 15552104Seric goto tempfail1; 1564976Seric 1574976Seric /* 15858957Seric ** Check to see if we actually ended up talking to ourself. 15958957Seric ** This means we didn't know about an alias or MX, or we managed 16058957Seric ** to connect to an echo server. 161*64651Seric ** 162*64651Seric ** If this code remains at all, "CheckLoopBack" should be 163*64651Seric ** a mailer flag. This is a MAYBENEXTRELEASE feature. 16458957Seric */ 16558957Seric 16659026Seric p = strchr(&SmtpReplyBuffer[4], ' '); 16758957Seric if (p != NULL) 16861707Seric *p = '\0'; 16964644Seric if (CheckLoopBack && 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 19353751Seric mci->mci_state = MCIS_OPEN; 19454967Seric return; 19553751Seric 19653751Seric tempfail1: 19753751Seric tempfail2: 19853751Seric mci->mci_exitstat = EX_TEMPFAIL; 19957379Seric if (mci->mci_errno == 0) 20057379Seric mci->mci_errno = errno; 20157379Seric if (mci->mci_state != MCIS_CLOSED) 20257379Seric smtpquit(m, mci, e); 20354967Seric return; 20453751Seric 20553751Seric unavailable: 20653751Seric mci->mci_exitstat = EX_UNAVAILABLE; 20753751Seric mci->mci_errno = errno; 20853751Seric smtpquit(m, mci, e); 20954967Seric return; 21053751Seric } 21159285Seric /* 21260210Seric ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 21360210Seric ** 21460210Seric ** 21560210Seric ** Parameters: 21660210Seric ** line -- the response line. 21760210Seric ** m -- the mailer. 21860210Seric ** mci -- the mailer connection info. 21960210Seric ** e -- the envelope. 22060210Seric ** 22160210Seric ** Returns: 22260210Seric ** none. 22360210Seric */ 22460210Seric 22560210Seric void 22660210Seric esmtp_check(line, m, mci, e) 22760210Seric char *line; 22860210Seric MAILER *m; 22960210Seric register MCI *mci; 23060210Seric ENVELOPE *e; 23160210Seric { 23260210Seric if (strlen(line) < 5) 23360210Seric return; 23460210Seric line += 4; 23560210Seric if (strncmp(line, "ESMTP ", 6) == 0) 23660210Seric mci->mci_flags |= MCIF_ESMTP; 23760210Seric } 23860210Seric /* 23959285Seric ** HELO_OPTIONS -- process the options on a HELO line. 24059285Seric ** 24159285Seric ** Parameters: 24259285Seric ** line -- the response line. 24359285Seric ** m -- the mailer. 24459285Seric ** mci -- the mailer connection info. 24559285Seric ** e -- the envelope. 24659285Seric ** 24759285Seric ** Returns: 24859285Seric ** none. 24959285Seric */ 25053751Seric 25159285Seric void 25259285Seric helo_options(line, m, mci, e) 25359285Seric char *line; 25459285Seric MAILER *m; 25559285Seric register MCI *mci; 25659285Seric ENVELOPE *e; 25759285Seric { 25859285Seric register char *p; 25959285Seric 26059285Seric if (strlen(line) < 5) 26159285Seric return; 26259285Seric line += 4; 26359285Seric p = strchr(line, ' '); 26459285Seric if (p != NULL) 26559285Seric *p++ = '\0'; 26659285Seric if (strcasecmp(line, "size") == 0) 26759285Seric { 26859285Seric mci->mci_flags |= MCIF_SIZE; 26959285Seric if (p != NULL) 27059285Seric mci->mci_maxsize = atol(p); 27159285Seric } 27259285Seric else if (strcasecmp(line, "8bitmime") == 0) 27359285Seric mci->mci_flags |= MCIF_8BITMIME; 27459285Seric else if (strcasecmp(line, "expn") == 0) 27559285Seric mci->mci_flags |= MCIF_EXPN; 27659285Seric } 27759285Seric /* 27859285Seric ** SMTPMAILFROM -- send MAIL command 27959285Seric ** 28059285Seric ** Parameters: 28159285Seric ** m -- the mailer. 28259285Seric ** mci -- the mailer connection structure. 28359285Seric ** e -- the envelope (including the sender to specify). 28459285Seric */ 28559285Seric 28653751Seric smtpmailfrom(m, mci, e) 28753751Seric struct mailer *m; 28854967Seric MCI *mci; 28953751Seric ENVELOPE *e; 29053751Seric { 29153751Seric int r; 29253751Seric char buf[MAXNAME]; 29359285Seric char optbuf[MAXLINE]; 29453751Seric 29563753Seric if (tTd(18, 2)) 29657943Seric printf("smtpmailfrom: CurHost=%s\n", CurHostName); 29757943Seric 29859285Seric /* set up appropriate options to include */ 29964254Seric if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0) 30059285Seric sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 30159285Seric else 30259285Seric strcpy(optbuf, ""); 30359285Seric 3049315Seric /* 3054865Seric ** Send the MAIL command. 3064865Seric ** Designates the sender. 3074865Seric */ 3084796Seric 30953751Seric mci->mci_state = MCIS_ACTIVE; 31053751Seric 31159540Seric if (bitset(EF_RESPONSE, e->e_flags) && 31259540Seric !bitnset(M_NO_NULL_FROM, m->m_flags)) 31358680Seric (void) strcpy(buf, ""); 31458680Seric else 31558680Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 31653751Seric if (e->e_from.q_mailer == LocalMailer || 31710688Seric !bitnset(M_FROMPATH, m->m_flags)) 3188436Seric { 31959285Seric smtpmessage("MAIL From:<%s>%s", m, mci, buf, optbuf); 3208436Seric } 3218436Seric else 3228436Seric { 32359285Seric smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 32459285Seric buf[0] == '@' ? ',' : ':', buf, optbuf); 3258436Seric } 32661093Seric SmtpPhase = mci->mci_phase = "client MAIL"; 32753751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 32859285Seric r = reply(m, mci, e, TimeOuts.to_mail, NULL); 3298005Seric if (r < 0 || REPLYTYPE(r) == 4) 33053751Seric { 33153751Seric mci->mci_exitstat = EX_TEMPFAIL; 33253751Seric mci->mci_errno = errno; 33353751Seric smtpquit(m, mci, e); 33453751Seric return EX_TEMPFAIL; 33553751Seric } 3367963Seric else if (r == 250) 33753751Seric { 33853751Seric mci->mci_exitstat = EX_OK; 33953751Seric return EX_OK; 34053751Seric } 3417963Seric else if (r == 552) 34253751Seric { 34353751Seric /* signal service unavailable */ 34453751Seric mci->mci_exitstat = EX_UNAVAILABLE; 34553751Seric smtpquit(m, mci, e); 34653751Seric return EX_UNAVAILABLE; 34753751Seric } 34814913Seric 34958008Seric #ifdef LOG 35058020Seric if (LogLevel > 1) 35158008Seric { 35258008Seric syslog(LOG_CRIT, "%s: SMTP MAIL protocol error: %s", 35358008Seric e->e_id, SmtpReplyBuffer); 35458008Seric } 35558008Seric #endif 35658008Seric 35714913Seric /* protocol error -- close up */ 35853751Seric smtpquit(m, mci, e); 35953751Seric mci->mci_exitstat = EX_PROTOCOL; 36053751Seric return EX_PROTOCOL; 3614684Seric } 3624684Seric /* 3634976Seric ** SMTPRCPT -- designate recipient. 3644797Seric ** 3654797Seric ** Parameters: 3664865Seric ** to -- address of recipient. 36710175Seric ** m -- the mailer we are sending to. 36857379Seric ** mci -- the connection info for this transaction. 36957379Seric ** e -- the envelope for this transaction. 3704797Seric ** 3714797Seric ** Returns: 3724865Seric ** exit status corresponding to recipient status. 3734797Seric ** 3744797Seric ** Side Effects: 3754865Seric ** Sends the mail via SMTP. 3764797Seric */ 3774797Seric 37853751Seric smtprcpt(to, m, mci, e) 3794865Seric ADDRESS *to; 38010175Seric register MAILER *m; 38154967Seric MCI *mci; 38253751Seric ENVELOPE *e; 3834797Seric { 3844797Seric register int r; 3854797Seric 38653751Seric smtpmessage("RCPT To:<%s>", m, mci, to->q_user); 3874865Seric 38861093Seric SmtpPhase = mci->mci_phase = "client RCPT"; 38953751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 39059285Seric r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 3918005Seric if (r < 0 || REPLYTYPE(r) == 4) 3924865Seric return (EX_TEMPFAIL); 3937963Seric else if (REPLYTYPE(r) == 2) 3947963Seric return (EX_OK); 3957964Seric else if (r == 550 || r == 551 || r == 553) 3967964Seric return (EX_NOUSER); 3977964Seric else if (r == 552 || r == 554) 3987964Seric return (EX_UNAVAILABLE); 39958008Seric 40058008Seric #ifdef LOG 40158020Seric if (LogLevel > 1) 40258008Seric { 40358008Seric syslog(LOG_CRIT, "%s: SMTP RCPT protocol error: %s", 40458008Seric e->e_id, SmtpReplyBuffer); 40558008Seric } 40658008Seric #endif 40758008Seric 4087964Seric return (EX_PROTOCOL); 4094797Seric } 4104797Seric /* 41110175Seric ** SMTPDATA -- send the data and clean up the transaction. 4124684Seric ** 4134684Seric ** Parameters: 4144865Seric ** m -- mailer being sent to. 4156980Seric ** e -- the envelope for this message. 4164684Seric ** 4174684Seric ** Returns: 4184976Seric ** exit status corresponding to DATA command. 4194684Seric ** 4204684Seric ** Side Effects: 4214865Seric ** none. 4224684Seric */ 4234684Seric 42463753Seric static jmp_buf CtxDataTimeout; 42563937Seric static int datatimeout(); 42663753Seric 42753740Seric smtpdata(m, mci, e) 4284865Seric struct mailer *m; 42954967Seric register MCI *mci; 4306980Seric register ENVELOPE *e; 4314684Seric { 4324684Seric register int r; 43363753Seric register EVENT *ev; 43463753Seric time_t timeout; 4354684Seric 4364797Seric /* 4374797Seric ** Send the data. 43810175Seric ** First send the command and check that it is ok. 43910175Seric ** Then send the data. 44010175Seric ** Follow it up with a dot to terminate. 44110175Seric ** Finally get the results of the transaction. 4424797Seric */ 4434797Seric 44410175Seric /* send the command and check ok to proceed */ 44553751Seric smtpmessage("DATA", m, mci); 44661093Seric SmtpPhase = mci->mci_phase = "client DATA 354"; 44753751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 44859285Seric r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 4498005Seric if (r < 0 || REPLYTYPE(r) == 4) 45057990Seric { 45157990Seric smtpquit(m, mci, e); 4524797Seric return (EX_TEMPFAIL); 45357990Seric } 4547963Seric else if (r == 554) 45557990Seric { 45657990Seric smtprset(m, mci, e); 4577963Seric return (EX_UNAVAILABLE); 45857990Seric } 4597963Seric else if (r != 354) 46057990Seric { 46158008Seric #ifdef LOG 46258020Seric if (LogLevel > 1) 46358008Seric { 46458008Seric syslog(LOG_CRIT, "%s: SMTP DATA-1 protocol error: %s", 46558008Seric e->e_id, SmtpReplyBuffer); 46658008Seric } 46758008Seric #endif 46857990Seric smtprset(m, mci, e); 4697964Seric return (EX_PROTOCOL); 47057990Seric } 47110175Seric 47263757Seric /* 47363757Seric ** Set timeout around data writes. Make it at least large 47463757Seric ** enough for DNS timeouts on all recipients plus some fudge 47563757Seric ** factor. The main thing is that it should not be infinite. 47663757Seric */ 47763757Seric 47863753Seric if (setjmp(CtxDataTimeout) != 0) 47963753Seric { 48063753Seric mci->mci_errno = errno; 48163753Seric mci->mci_exitstat = EX_TEMPFAIL; 48263753Seric mci->mci_state = MCIS_ERROR; 48363753Seric syserr("451 timeout writing message to %s", mci->mci_host); 48463753Seric smtpquit(m, mci, e); 48563753Seric return EX_TEMPFAIL; 48663753Seric } 48763753Seric 48863787Seric timeout = e->e_msgsize / 16; 48963787Seric if (timeout < (time_t) 60) 49063787Seric timeout = (time_t) 60; 49163787Seric timeout += e->e_nrcpts * 90; 49263753Seric ev = setevent(timeout, datatimeout, 0); 49363753Seric 49410175Seric /* now output the actual message */ 49553751Seric (*e->e_puthdr)(mci->mci_out, m, e); 49653740Seric putline("\n", mci->mci_out, m); 49759730Seric (*e->e_putbody)(mci->mci_out, m, e, NULL); 49810175Seric 49963753Seric clrevent(ev); 50063753Seric 50110175Seric /* terminate the message */ 50253740Seric fprintf(mci->mci_out, ".%s", m->m_eol); 50363753Seric if (TrafficLogFile != NULL) 50463753Seric fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 50558120Seric if (Verbose) 50658151Seric nmessage(">>> ."); 50710175Seric 50810175Seric /* check for the results of the transaction */ 50961093Seric SmtpPhase = mci->mci_phase = "client DATA 250"; 51053751Seric setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 51159285Seric r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 51253751Seric if (r < 0) 51357990Seric { 51457990Seric smtpquit(m, mci, e); 5154797Seric return (EX_TEMPFAIL); 51657990Seric } 51753751Seric mci->mci_state = MCIS_OPEN; 51858917Seric e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 51953751Seric if (REPLYTYPE(r) == 4) 52053751Seric return (EX_TEMPFAIL); 5217963Seric else if (r == 250) 5227963Seric return (EX_OK); 5237963Seric else if (r == 552 || r == 554) 5247963Seric return (EX_UNAVAILABLE); 52558008Seric #ifdef LOG 52658020Seric if (LogLevel > 1) 52758008Seric { 52858008Seric syslog(LOG_CRIT, "%s: SMTP DATA-2 protocol error: %s", 52958008Seric e->e_id, SmtpReplyBuffer); 53058008Seric } 53158008Seric #endif 5327964Seric return (EX_PROTOCOL); 5334684Seric } 53463753Seric 53563753Seric 53663753Seric static int 53763753Seric datatimeout() 53863753Seric { 53963753Seric longjmp(CtxDataTimeout, 1); 54063753Seric } 5414684Seric /* 5424865Seric ** SMTPQUIT -- close the SMTP connection. 5434865Seric ** 5444865Seric ** Parameters: 54515535Seric ** m -- a pointer to the mailer. 5464865Seric ** 5474865Seric ** Returns: 5484865Seric ** none. 5494865Seric ** 5504865Seric ** Side Effects: 5514865Seric ** sends the final protocol and closes the connection. 5524865Seric */ 5534865Seric 55453751Seric smtpquit(m, mci, e) 55553751Seric register MAILER *m; 55654967Seric register MCI *mci; 55753751Seric ENVELOPE *e; 5584865Seric { 5599391Seric int i; 5604865Seric 56154967Seric /* send the quit message if we haven't gotten I/O error */ 56253751Seric if (mci->mci_state != MCIS_ERROR) 5639391Seric { 56461093Seric SmtpPhase = "client QUIT"; 56553751Seric smtpmessage("QUIT", m, mci); 56659285Seric (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 56753740Seric if (mci->mci_state == MCIS_CLOSED) 56810159Seric return; 5699391Seric } 5709391Seric 57152676Seric /* now actually close the connection and pick up the zombie */ 57258846Seric i = endmailer(mci, e, m->m_argv); 5739391Seric if (i != EX_OK) 57458151Seric syserr("451 smtpquit %s: stat %d", m->m_argv[0], i); 5754865Seric } 5764865Seric /* 57754967Seric ** SMTPRSET -- send a RSET (reset) command 57854967Seric */ 57954967Seric 58054967Seric smtprset(m, mci, e) 58154967Seric register MAILER *m; 58254967Seric register MCI *mci; 58354967Seric ENVELOPE *e; 58454967Seric { 58554967Seric int r; 58654967Seric 58761093Seric SmtpPhase = "client RSET"; 58854967Seric smtpmessage("RSET", m, mci); 58959285Seric r = reply(m, mci, e, TimeOuts.to_rset, NULL); 59057734Seric if (r < 0) 59157734Seric mci->mci_state = MCIS_ERROR; 59254967Seric else if (REPLYTYPE(r) == 2) 59357734Seric { 59457734Seric mci->mci_state = MCIS_OPEN; 59557734Seric return; 59657734Seric } 59757734Seric smtpquit(m, mci, e); 59854967Seric } 59954967Seric /* 60058867Seric ** SMTPPROBE -- check the connection state 60154967Seric */ 60254967Seric 60358867Seric smtpprobe(mci) 60454967Seric register MCI *mci; 60554967Seric { 60654967Seric int r; 60754967Seric MAILER *m = mci->mci_mailer; 60854967Seric extern ENVELOPE BlankEnvelope; 60954967Seric ENVELOPE *e = &BlankEnvelope; 61054967Seric 61161093Seric SmtpPhase = "client probe"; 61258867Seric smtpmessage("RSET", m, mci); 61359285Seric r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 61458061Seric if (r < 0 || REPLYTYPE(r) != 2) 61554967Seric smtpquit(m, mci, e); 61654967Seric return r; 61754967Seric } 61854967Seric /* 6194684Seric ** REPLY -- read arpanet reply 6204684Seric ** 6214684Seric ** Parameters: 62210175Seric ** m -- the mailer we are reading the reply from. 62357379Seric ** mci -- the mailer connection info structure. 62457379Seric ** e -- the current envelope. 62557379Seric ** timeout -- the timeout for reads. 62659285Seric ** pfunc -- processing function for second and subsequent 62759285Seric ** lines of response -- if null, no special 62859285Seric ** processing is done. 6294684Seric ** 6304684Seric ** Returns: 6314684Seric ** reply code it reads. 6324684Seric ** 6334684Seric ** Side Effects: 6344684Seric ** flushes the mail file. 6354684Seric */ 6364684Seric 63759285Seric reply(m, mci, e, timeout, pfunc) 63853751Seric MAILER *m; 63954967Seric MCI *mci; 64053751Seric ENVELOPE *e; 64159285Seric time_t timeout; 64259285Seric void (*pfunc)(); 6434684Seric { 64459014Seric register char *bufp; 64559014Seric register int r; 64659285Seric bool firstline = TRUE; 64758957Seric char junkbuf[MAXLINE]; 64858957Seric 64957379Seric if (mci->mci_out != NULL) 65057379Seric (void) fflush(mci->mci_out); 6514684Seric 6527677Seric if (tTd(18, 1)) 6534796Seric printf("reply\n"); 6544796Seric 6557356Seric /* 6567356Seric ** Read the input line, being careful not to hang. 6577356Seric */ 6587356Seric 65959014Seric for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 6604684Seric { 6617356Seric register char *p; 66253751Seric extern time_t curtime(); 6634684Seric 6647685Seric /* actually do the read */ 66553751Seric if (e->e_xfp != NULL) 66653751Seric (void) fflush(e->e_xfp); /* for debugging */ 6677356Seric 66810054Seric /* if we are in the process of closing just give the code */ 66953740Seric if (mci->mci_state == MCIS_CLOSED) 67010054Seric return (SMTPCLOSING); 67110054Seric 67258680Seric if (mci->mci_out != NULL) 67358680Seric fflush(mci->mci_out); 67458680Seric 67510054Seric /* get the line from the other side */ 67661093Seric p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 67753751Seric mci->mci_lastuse = curtime(); 67853751Seric 67910054Seric if (p == NULL) 68010131Seric { 68163753Seric bool oldholderrs; 68210148Seric extern char MsgBuf[]; /* err.c */ 68310148Seric 68421065Seric /* if the remote end closed early, fake an error */ 68521065Seric if (errno == 0) 68621065Seric # ifdef ECONNRESET 68721065Seric errno = ECONNRESET; 68856795Seric # else /* ECONNRESET */ 68921065Seric errno = EPIPE; 69056795Seric # endif /* ECONNRESET */ 69121065Seric 69257379Seric mci->mci_errno = errno; 69357642Seric mci->mci_exitstat = EX_TEMPFAIL; 69463753Seric oldholderrs = HoldErrs; 69563753Seric HoldErrs = TRUE; 69663753Seric usrerr("451 reply: read error from %s", mci->mci_host); 69763753Seric 69810420Seric /* if debugging, pause so we can see state */ 69910420Seric if (tTd(18, 100)) 70010420Seric pause(); 70154967Seric mci->mci_state = MCIS_ERROR; 70253751Seric smtpquit(m, mci, e); 70363753Seric #ifdef XDEBUG 70463753Seric { 70563753Seric char wbuf[MAXLINE]; 70664082Seric char *p = wbuf; 70764082Seric if (e->e_to != NULL) 70864082Seric { 70964082Seric sprintf(p, "%s... ", e->e_to); 71064082Seric p += strlen(p); 71164082Seric } 71264082Seric sprintf(p, "reply(%s) during %s", 71364082Seric mci->mci_host, SmtpPhase); 71463753Seric checkfd012(wbuf); 71563753Seric } 71663753Seric #endif 71763753Seric HoldErrs = oldholderrs; 71810054Seric return (-1); 71910131Seric } 72058957Seric fixcrlf(bufp, TRUE); 72110054Seric 72263753Seric /* EHLO failure is not a real error */ 72363753Seric if (e->e_xfp != NULL && (bufp[0] == '4' || 72463753Seric (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 72514900Seric { 72614900Seric /* serious error -- log the previous command */ 72764071Seric if (SmtpNeedIntro) 72864071Seric { 72964071Seric /* inform user who we are chatting with */ 73064071Seric fprintf(CurEnv->e_xfp, 73164071Seric "... while talking to %s:\n", 73264071Seric CurHostName); 73364071Seric SmtpNeedIntro = FALSE; 73464071Seric } 73559014Seric if (SmtpMsgBuffer[0] != '\0') 73659014Seric fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 73759014Seric SmtpMsgBuffer[0] = '\0'; 73814900Seric 73914900Seric /* now log the message as from the other side */ 74058957Seric fprintf(e->e_xfp, "<<< %s\n", bufp); 74114900Seric } 74214900Seric 74314900Seric /* display the input for verbose mode */ 74458120Seric if (Verbose) 74559956Seric nmessage("050 %s", bufp); 7467356Seric 74759285Seric /* process the line */ 74859285Seric if (pfunc != NULL && !firstline) 74959285Seric (*pfunc)(bufp, m, mci, e); 75059285Seric 75159285Seric firstline = FALSE; 75259285Seric 7537356Seric /* if continuation is required, we can go on */ 75459014Seric if (bufp[3] == '-') 7554684Seric continue; 7567356Seric 75759014Seric /* ignore improperly formated input */ 75859014Seric if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 75959014Seric continue; 76059014Seric 7617356Seric /* decode the reply code */ 76259014Seric r = atoi(bufp); 7637356Seric 7647356Seric /* extra semantics: 0xx codes are "informational" */ 76559014Seric if (r >= 100) 76659014Seric break; 76759014Seric } 7687356Seric 76959014Seric /* 77059014Seric ** Now look at SmtpReplyBuffer -- only care about the first 77159014Seric ** line of the response from here on out. 77259014Seric */ 77358061Seric 77459014Seric /* save temporary failure messages for posterity */ 77559014Seric if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 77659014Seric (void) strcpy(SmtpError, SmtpReplyBuffer); 7779391Seric 77859014Seric /* reply code 421 is "Service Shutting Down" */ 77959014Seric if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 78059014Seric { 78159014Seric /* send the quit protocol */ 78259014Seric mci->mci_state = MCIS_SSD; 78359014Seric smtpquit(m, mci, e); 7844684Seric } 78559014Seric 78659014Seric return (r); 7874684Seric } 7884796Seric /* 7894865Seric ** SMTPMESSAGE -- send message to server 7904796Seric ** 7914796Seric ** Parameters: 7924796Seric ** f -- format 79310175Seric ** m -- the mailer to control formatting. 7944796Seric ** a, b, c -- parameters 7954796Seric ** 7964796Seric ** Returns: 7974796Seric ** none. 7984796Seric ** 7994796Seric ** Side Effects: 80053740Seric ** writes message to mci->mci_out. 8014796Seric */ 8024796Seric 8034865Seric /*VARARGS1*/ 80457642Seric #ifdef __STDC__ 80557642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...) 80657642Seric #else 80757642Seric smtpmessage(f, m, mci, va_alist) 8084796Seric char *f; 80910175Seric MAILER *m; 81054967Seric MCI *mci; 81157642Seric va_dcl 81257642Seric #endif 8134796Seric { 81456852Seric VA_LOCAL_DECL 81556852Seric 81657135Seric VA_START(mci); 81756852Seric (void) vsprintf(SmtpMsgBuffer, f, ap); 81856852Seric VA_END; 81958680Seric 82058120Seric if (tTd(18, 1) || Verbose) 82158151Seric nmessage(">>> %s", SmtpMsgBuffer); 82263753Seric if (TrafficLogFile != NULL) 82363753Seric fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 82453740Seric if (mci->mci_out != NULL) 82558680Seric { 82653740Seric fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 82754967Seric m == NULL ? "\r\n" : m->m_eol); 82858680Seric } 82959149Seric else if (tTd(18, 1)) 83058725Seric { 83159149Seric printf("smtpmessage: NULL mci_out\n"); 83258725Seric } 8334796Seric } 8345182Seric 83556795Seric # endif /* SMTP */ 836