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*67936Seric static char sccsid[] = "@(#)usersmtp.c	8.27 (Berkeley) 11/19/94 (with SMTP)";
1433731Sbostic #else
15*67936Seric static char sccsid[] = "@(#)usersmtp.c	8.27 (Berkeley) 11/19/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 
27267893Seric 	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;
29467887Seric 	else if (strcasecmp(line, "x-dsn-0") == 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 
35867880Seric 	if (e->e_envid != NULL && bitset(MCIF_DSN, mci->mci_flags))
35967880Seric 	{
36067880Seric 		strcat(optbuf, " ENVID=");
36167880Seric 		strcat(optbuf, e->e_envid);
36267880Seric 	}
36367880Seric 
3649315Seric 	/*
3654865Seric 	**  Send the MAIL command.
3664865Seric 	**	Designates the sender.
3674865Seric 	*/
3684796Seric 
36953751Seric 	mci->mci_state = MCIS_ACTIVE;
37053751Seric 
37159540Seric 	if (bitset(EF_RESPONSE, e->e_flags) &&
37259540Seric 	    !bitnset(M_NO_NULL_FROM, m->m_flags))
37358680Seric 		(void) strcpy(buf, "");
37458680Seric 	else
37558680Seric 		expand("\201g", buf, &buf[sizeof buf - 1], e);
37665494Seric 	if (buf[0] == '<')
37765494Seric 	{
37865494Seric 		/* strip off <angle brackets> (put back on below) */
37965494Seric 		bufp = &buf[strlen(buf) - 1];
38065494Seric 		if (*bufp == '>')
38165494Seric 			*bufp = '\0';
38265494Seric 		bufp = &buf[1];
38365494Seric 	}
38465494Seric 	else
38565494Seric 		bufp = buf;
38667472Seric 	if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) ||
38710688Seric 	    !bitnset(M_FROMPATH, m->m_flags))
3888436Seric 	{
38965494Seric 		smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf);
3908436Seric 	}
3918436Seric 	else
3928436Seric 	{
39359285Seric 		smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
39465494Seric 			*bufp == '@' ? ',' : ':', bufp, optbuf);
3958436Seric 	}
39661093Seric 	SmtpPhase = mci->mci_phase = "client MAIL";
39753751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
39859285Seric 	r = reply(m, mci, e, TimeOuts.to_mail, NULL);
3998005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
40053751Seric 	{
40153751Seric 		mci->mci_exitstat = EX_TEMPFAIL;
40253751Seric 		mci->mci_errno = errno;
40353751Seric 		smtpquit(m, mci, e);
40453751Seric 		return EX_TEMPFAIL;
40553751Seric 	}
4067963Seric 	else if (r == 250)
40753751Seric 	{
40853751Seric 		mci->mci_exitstat = EX_OK;
40953751Seric 		return EX_OK;
41053751Seric 	}
4117963Seric 	else if (r == 552)
41253751Seric 	{
41353751Seric 		/* signal service unavailable */
41453751Seric 		mci->mci_exitstat = EX_UNAVAILABLE;
41553751Seric 		smtpquit(m, mci, e);
41653751Seric 		return EX_UNAVAILABLE;
41753751Seric 	}
41814913Seric 
41958008Seric #ifdef LOG
42058020Seric 	if (LogLevel > 1)
42158008Seric 	{
42267860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s",
42367860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
42458008Seric 	}
42558008Seric #endif
42658008Seric 
42714913Seric 	/* protocol error -- close up */
42853751Seric 	smtpquit(m, mci, e);
42953751Seric 	mci->mci_exitstat = EX_PROTOCOL;
43053751Seric 	return EX_PROTOCOL;
4314684Seric }
4324684Seric /*
4334976Seric **  SMTPRCPT -- designate recipient.
4344797Seric **
4354797Seric **	Parameters:
4364865Seric **		to -- address of recipient.
43710175Seric **		m -- the mailer we are sending to.
43857379Seric **		mci -- the connection info for this transaction.
43957379Seric **		e -- the envelope for this transaction.
4404797Seric **
4414797Seric **	Returns:
4424865Seric **		exit status corresponding to recipient status.
4434797Seric **
4444797Seric **	Side Effects:
4454865Seric **		Sends the mail via SMTP.
4464797Seric */
4474797Seric 
44853751Seric smtprcpt(to, m, mci, e)
4494865Seric 	ADDRESS *to;
45010175Seric 	register MAILER *m;
45154967Seric 	MCI *mci;
45253751Seric 	ENVELOPE *e;
4534797Seric {
4544797Seric 	register int r;
45567880Seric 	char optbuf[MAXLINE];
4564797Seric 
45767880Seric 	strcpy(optbuf, "");
45867880Seric 	if (bitset(MCIF_DSN, mci->mci_flags))
45967880Seric 	{
46067880Seric 		strcat(optbuf, " NOTIFY=");
46167880Seric 		if (bitset(QPINGONFAILURE, to->q_flags))
46267880Seric 		{
46367880Seric 			if (bitset(QPINGONSUCCESS, to->q_flags))
46467880Seric 				strcat(optbuf, "ALWAYS");
46567880Seric 			else
46667880Seric 				strcat(optbuf, "FAILURE");
46767880Seric 		}
46867880Seric 		else
46967880Seric 		{
47067880Seric 			if (bitset(QPINGONSUCCESS, to->q_flags))
47167880Seric 				strcat(optbuf, "SUCCESS");
47267880Seric 			else
47367880Seric 				strcat(optbuf, "NEVER");
47467880Seric 		}
47567880Seric 		if (bitset(QHASRETPARAM, to->q_flags))
47667880Seric 		{
47767880Seric 			strcat(optbuf, " RET=");
47867880Seric 			if (bitset(QNOBODYRETURN, to->q_flags))
47967880Seric 				strcat(optbuf, "NO");
48067880Seric 			else
48167880Seric 				strcat(optbuf, "YES");
48267880Seric 		}
48367880Seric 	}
48467880Seric 	else if (bitset(QPINGONSUCCESS, to->q_flags))
48567880Seric 	{
48667880Seric 		to->q_flags |= QRELAYED;
48767880Seric 		fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n",
48867880Seric 			to->q_paddr);
48967880Seric 	}
4904865Seric 
49167880Seric 	smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
49267880Seric 
49361093Seric 	SmtpPhase = mci->mci_phase = "client RCPT";
49453751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
49559285Seric 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
49667880Seric 	setstatus(to, SmtpReplyBuffer);
4978005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
4984865Seric 		return (EX_TEMPFAIL);
4997963Seric 	else if (REPLYTYPE(r) == 2)
5007963Seric 		return (EX_OK);
5017964Seric 	else if (r == 550 || r == 551 || r == 553)
5027964Seric 		return (EX_NOUSER);
5037964Seric 	else if (r == 552 || r == 554)
5047964Seric 		return (EX_UNAVAILABLE);
50558008Seric 
50658008Seric #ifdef LOG
50758020Seric 	if (LogLevel > 1)
50858008Seric 	{
50967860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s",
51067860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
51158008Seric 	}
51258008Seric #endif
51358008Seric 
5147964Seric 	return (EX_PROTOCOL);
5154797Seric }
5164797Seric /*
51710175Seric **  SMTPDATA -- send the data and clean up the transaction.
5184684Seric **
5194684Seric **	Parameters:
5204865Seric **		m -- mailer being sent to.
5216980Seric **		e -- the envelope for this message.
5224684Seric **
5234684Seric **	Returns:
5244976Seric **		exit status corresponding to DATA command.
5254684Seric **
5264684Seric **	Side Effects:
5274865Seric **		none.
5284684Seric */
5294684Seric 
53063753Seric static jmp_buf	CtxDataTimeout;
53163937Seric static int	datatimeout();
53263753Seric 
53353740Seric smtpdata(m, mci, e)
5344865Seric 	struct mailer *m;
53554967Seric 	register MCI *mci;
5366980Seric 	register ENVELOPE *e;
5374684Seric {
5384684Seric 	register int r;
53963753Seric 	register EVENT *ev;
54063753Seric 	time_t timeout;
5414684Seric 
5424797Seric 	/*
5434797Seric 	**  Send the data.
54410175Seric 	**	First send the command and check that it is ok.
54510175Seric 	**	Then send the data.
54610175Seric 	**	Follow it up with a dot to terminate.
54710175Seric 	**	Finally get the results of the transaction.
5484797Seric 	*/
5494797Seric 
55010175Seric 	/* send the command and check ok to proceed */
55153751Seric 	smtpmessage("DATA", m, mci);
55261093Seric 	SmtpPhase = mci->mci_phase = "client DATA 354";
55353751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
55459285Seric 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
5558005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
55657990Seric 	{
55757990Seric 		smtpquit(m, mci, e);
5584797Seric 		return (EX_TEMPFAIL);
55957990Seric 	}
5607963Seric 	else if (r == 554)
56157990Seric 	{
56257990Seric 		smtprset(m, mci, e);
5637963Seric 		return (EX_UNAVAILABLE);
56457990Seric 	}
5657963Seric 	else if (r != 354)
56657990Seric 	{
56758008Seric #ifdef LOG
56858020Seric 		if (LogLevel > 1)
56958008Seric 		{
57067860Seric 			syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s",
57167860Seric 				e->e_id, mci->mci_host, SmtpReplyBuffer);
57258008Seric 		}
57358008Seric #endif
57457990Seric 		smtprset(m, mci, e);
5757964Seric 		return (EX_PROTOCOL);
57657990Seric 	}
57710175Seric 
57863757Seric 	/*
57963757Seric 	**  Set timeout around data writes.  Make it at least large
58063757Seric 	**  enough for DNS timeouts on all recipients plus some fudge
58163757Seric 	**  factor.  The main thing is that it should not be infinite.
58263757Seric 	*/
58363757Seric 
58463753Seric 	if (setjmp(CtxDataTimeout) != 0)
58563753Seric 	{
58663753Seric 		mci->mci_errno = errno;
58763753Seric 		mci->mci_exitstat = EX_TEMPFAIL;
58863753Seric 		mci->mci_state = MCIS_ERROR;
58963753Seric 		syserr("451 timeout writing message to %s", mci->mci_host);
59063753Seric 		smtpquit(m, mci, e);
59163753Seric 		return EX_TEMPFAIL;
59263753Seric 	}
59363753Seric 
59463787Seric 	timeout = e->e_msgsize / 16;
59563787Seric 	if (timeout < (time_t) 60)
59663787Seric 		timeout = (time_t) 60;
59763787Seric 	timeout += e->e_nrcpts * 90;
59863753Seric 	ev = setevent(timeout, datatimeout, 0);
59963753Seric 
60067546Seric 	/*
60167546Seric 	**  Output the actual message.
60267546Seric 	*/
60367546Seric 
604*67936Seric 	(*e->e_puthdr)(mci, e->e_header, e, 0);
605*67936Seric 	(*e->e_putbody)(mci, e, NULL, 0);
60610175Seric 
60767546Seric 	/*
60867546Seric 	**  Cleanup after sending message.
60967546Seric 	*/
61067546Seric 
61163753Seric 	clrevent(ev);
61263753Seric 
61364718Seric 	if (ferror(mci->mci_out))
61464718Seric 	{
61564718Seric 		/* error during processing -- don't send the dot */
61664718Seric 		mci->mci_errno = EIO;
61764718Seric 		mci->mci_exitstat = EX_IOERR;
61864718Seric 		mci->mci_state = MCIS_ERROR;
61964718Seric 		smtpquit(m, mci, e);
62064718Seric 		return EX_IOERR;
62164718Seric 	}
62264718Seric 
62310175Seric 	/* terminate the message */
62453740Seric 	fprintf(mci->mci_out, ".%s", m->m_eol);
62563753Seric 	if (TrafficLogFile != NULL)
62663753Seric 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
62758120Seric 	if (Verbose)
62858151Seric 		nmessage(">>> .");
62910175Seric 
63010175Seric 	/* check for the results of the transaction */
63161093Seric 	SmtpPhase = mci->mci_phase = "client DATA 250";
63253751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
63359285Seric 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
63453751Seric 	if (r < 0)
63557990Seric 	{
63657990Seric 		smtpquit(m, mci, e);
6374797Seric 		return (EX_TEMPFAIL);
63857990Seric 	}
63953751Seric 	mci->mci_state = MCIS_OPEN;
64058917Seric 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
64153751Seric 	if (REPLYTYPE(r) == 4)
64253751Seric 		return (EX_TEMPFAIL);
6437963Seric 	else if (r == 250)
6447963Seric 		return (EX_OK);
6457963Seric 	else if (r == 552 || r == 554)
6467963Seric 		return (EX_UNAVAILABLE);
64758008Seric #ifdef LOG
64858020Seric 	if (LogLevel > 1)
64958008Seric 	{
65067860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s",
65167860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
65258008Seric 	}
65358008Seric #endif
6547964Seric 	return (EX_PROTOCOL);
6554684Seric }
65663753Seric 
65763753Seric 
65863753Seric static int
65963753Seric datatimeout()
66063753Seric {
66163753Seric 	longjmp(CtxDataTimeout, 1);
66263753Seric }
6634684Seric /*
6644865Seric **  SMTPQUIT -- close the SMTP connection.
6654865Seric **
6664865Seric **	Parameters:
66715535Seric **		m -- a pointer to the mailer.
6684865Seric **
6694865Seric **	Returns:
6704865Seric **		none.
6714865Seric **
6724865Seric **	Side Effects:
6734865Seric **		sends the final protocol and closes the connection.
6744865Seric */
6754865Seric 
67653751Seric smtpquit(m, mci, e)
67753751Seric 	register MAILER *m;
67854967Seric 	register MCI *mci;
67953751Seric 	ENVELOPE *e;
6804865Seric {
68164822Seric 	bool oldSuprErrs = SuprErrs;
6824865Seric 
68364822Seric 	/*
68464822Seric 	**	Suppress errors here -- we may be processing a different
68564822Seric 	**	job when we do the quit connection, and we don't want the
68664822Seric 	**	new job to be penalized for something that isn't it's
68764822Seric 	**	problem.
68864822Seric 	*/
68964822Seric 
69064822Seric 	SuprErrs = TRUE;
69164822Seric 
69254967Seric 	/* send the quit message if we haven't gotten I/O error */
69353751Seric 	if (mci->mci_state != MCIS_ERROR)
6949391Seric 	{
69561093Seric 		SmtpPhase = "client QUIT";
69653751Seric 		smtpmessage("QUIT", m, mci);
69759285Seric 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
69864822Seric 		SuprErrs = oldSuprErrs;
69953740Seric 		if (mci->mci_state == MCIS_CLOSED)
70064822Seric 		{
70164822Seric 			SuprErrs = oldSuprErrs;
70210159Seric 			return;
70364822Seric 		}
7049391Seric 	}
7059391Seric 
70652676Seric 	/* now actually close the connection and pick up the zombie */
70765194Seric 	(void) endmailer(mci, e, NULL);
70864822Seric 
70964822Seric 	SuprErrs = oldSuprErrs;
7104865Seric }
7114865Seric /*
71254967Seric **  SMTPRSET -- send a RSET (reset) command
71354967Seric */
71454967Seric 
71554967Seric smtprset(m, mci, e)
71654967Seric 	register MAILER *m;
71754967Seric 	register MCI *mci;
71854967Seric 	ENVELOPE *e;
71954967Seric {
72054967Seric 	int r;
72154967Seric 
72261093Seric 	SmtpPhase = "client RSET";
72354967Seric 	smtpmessage("RSET", m, mci);
72459285Seric 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
72557734Seric 	if (r < 0)
72657734Seric 		mci->mci_state = MCIS_ERROR;
72754967Seric 	else if (REPLYTYPE(r) == 2)
72857734Seric 	{
72957734Seric 		mci->mci_state = MCIS_OPEN;
73057734Seric 		return;
73157734Seric 	}
73257734Seric 	smtpquit(m, mci, e);
73354967Seric }
73454967Seric /*
73558867Seric **  SMTPPROBE -- check the connection state
73654967Seric */
73754967Seric 
73858867Seric smtpprobe(mci)
73954967Seric 	register MCI *mci;
74054967Seric {
74154967Seric 	int r;
74254967Seric 	MAILER *m = mci->mci_mailer;
74354967Seric 	extern ENVELOPE BlankEnvelope;
74454967Seric 	ENVELOPE *e = &BlankEnvelope;
74554967Seric 
74661093Seric 	SmtpPhase = "client probe";
74758867Seric 	smtpmessage("RSET", m, mci);
74859285Seric 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
74958061Seric 	if (r < 0 || REPLYTYPE(r) != 2)
75054967Seric 		smtpquit(m, mci, e);
75154967Seric 	return r;
75254967Seric }
75354967Seric /*
7544684Seric **  REPLY -- read arpanet reply
7554684Seric **
7564684Seric **	Parameters:
75710175Seric **		m -- the mailer we are reading the reply from.
75857379Seric **		mci -- the mailer connection info structure.
75957379Seric **		e -- the current envelope.
76057379Seric **		timeout -- the timeout for reads.
76159285Seric **		pfunc -- processing function for second and subsequent
76259285Seric **			lines of response -- if null, no special
76359285Seric **			processing is done.
7644684Seric **
7654684Seric **	Returns:
7664684Seric **		reply code it reads.
7674684Seric **
7684684Seric **	Side Effects:
7694684Seric **		flushes the mail file.
7704684Seric */
7714684Seric 
77259285Seric reply(m, mci, e, timeout, pfunc)
77353751Seric 	MAILER *m;
77454967Seric 	MCI *mci;
77553751Seric 	ENVELOPE *e;
77659285Seric 	time_t timeout;
77759285Seric 	void (*pfunc)();
7784684Seric {
77959014Seric 	register char *bufp;
78059014Seric 	register int r;
78159285Seric 	bool firstline = TRUE;
78258957Seric 	char junkbuf[MAXLINE];
78358957Seric 
78457379Seric 	if (mci->mci_out != NULL)
78557379Seric 		(void) fflush(mci->mci_out);
7864684Seric 
7877677Seric 	if (tTd(18, 1))
7884796Seric 		printf("reply\n");
7894796Seric 
7907356Seric 	/*
7917356Seric 	**  Read the input line, being careful not to hang.
7927356Seric 	*/
7937356Seric 
79459014Seric 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
7954684Seric 	{
7967356Seric 		register char *p;
79753751Seric 		extern time_t curtime();
7984684Seric 
7997685Seric 		/* actually do the read */
80053751Seric 		if (e->e_xfp != NULL)
80153751Seric 			(void) fflush(e->e_xfp);	/* for debugging */
8027356Seric 
80310054Seric 		/* if we are in the process of closing just give the code */
80453740Seric 		if (mci->mci_state == MCIS_CLOSED)
80510054Seric 			return (SMTPCLOSING);
80610054Seric 
80758680Seric 		if (mci->mci_out != NULL)
80858680Seric 			fflush(mci->mci_out);
80958680Seric 
81010054Seric 		/* get the line from the other side */
81161093Seric 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
81253751Seric 		mci->mci_lastuse = curtime();
81353751Seric 
81410054Seric 		if (p == NULL)
81510131Seric 		{
81663753Seric 			bool oldholderrs;
81710148Seric 			extern char MsgBuf[];		/* err.c */
81810148Seric 
81921065Seric 			/* if the remote end closed early, fake an error */
82021065Seric 			if (errno == 0)
82121065Seric # ifdef ECONNRESET
82221065Seric 				errno = ECONNRESET;
82356795Seric # else /* ECONNRESET */
82421065Seric 				errno = EPIPE;
82556795Seric # endif /* ECONNRESET */
82621065Seric 
82757379Seric 			mci->mci_errno = errno;
82857642Seric 			mci->mci_exitstat = EX_TEMPFAIL;
82963753Seric 			oldholderrs = HoldErrs;
83063753Seric 			HoldErrs = TRUE;
83163753Seric 			usrerr("451 reply: read error from %s", mci->mci_host);
83263753Seric 
83310420Seric 			/* if debugging, pause so we can see state */
83410420Seric 			if (tTd(18, 100))
83510420Seric 				pause();
83654967Seric 			mci->mci_state = MCIS_ERROR;
83753751Seric 			smtpquit(m, mci, e);
83863753Seric #ifdef XDEBUG
83963753Seric 			{
84063753Seric 				char wbuf[MAXLINE];
84164082Seric 				char *p = wbuf;
84264082Seric 				if (e->e_to != NULL)
84364082Seric 				{
84464082Seric 					sprintf(p, "%s... ", e->e_to);
84564082Seric 					p += strlen(p);
84664082Seric 				}
84764082Seric 				sprintf(p, "reply(%s) during %s",
84864082Seric 					mci->mci_host, SmtpPhase);
84963753Seric 				checkfd012(wbuf);
85063753Seric 			}
85163753Seric #endif
85263753Seric 			HoldErrs = oldholderrs;
85310054Seric 			return (-1);
85410131Seric 		}
85558957Seric 		fixcrlf(bufp, TRUE);
85610054Seric 
85763753Seric 		/* EHLO failure is not a real error */
85863753Seric 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
85963753Seric 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
86014900Seric 		{
86114900Seric 			/* serious error -- log the previous command */
86264071Seric 			if (SmtpNeedIntro)
86364071Seric 			{
86464071Seric 				/* inform user who we are chatting with */
86564071Seric 				fprintf(CurEnv->e_xfp,
86664071Seric 					"... while talking to %s:\n",
86764071Seric 					CurHostName);
86864071Seric 				SmtpNeedIntro = FALSE;
86964071Seric 			}
87059014Seric 			if (SmtpMsgBuffer[0] != '\0')
87159014Seric 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
87259014Seric 			SmtpMsgBuffer[0] = '\0';
87314900Seric 
87414900Seric 			/* now log the message as from the other side */
87558957Seric 			fprintf(e->e_xfp, "<<< %s\n", bufp);
87614900Seric 		}
87714900Seric 
87814900Seric 		/* display the input for verbose mode */
87958120Seric 		if (Verbose)
88059956Seric 			nmessage("050 %s", bufp);
8817356Seric 
88259285Seric 		/* process the line */
88367893Seric 		if (pfunc != NULL)
88467893Seric 			(*pfunc)(bufp, firstline, m, mci, e);
88559285Seric 
88659285Seric 		firstline = FALSE;
88759285Seric 
8887356Seric 		/* if continuation is required, we can go on */
88959014Seric 		if (bufp[3] == '-')
8904684Seric 			continue;
8917356Seric 
89259014Seric 		/* ignore improperly formated input */
89359014Seric 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
89459014Seric 			continue;
89559014Seric 
8967356Seric 		/* decode the reply code */
89759014Seric 		r = atoi(bufp);
8987356Seric 
8997356Seric 		/* extra semantics: 0xx codes are "informational" */
90059014Seric 		if (r >= 100)
90159014Seric 			break;
90259014Seric 	}
9037356Seric 
90459014Seric 	/*
90559014Seric 	**  Now look at SmtpReplyBuffer -- only care about the first
90659014Seric 	**  line of the response from here on out.
90759014Seric 	*/
90858061Seric 
90959014Seric 	/* save temporary failure messages for posterity */
91059014Seric 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
91159014Seric 		(void) strcpy(SmtpError, SmtpReplyBuffer);
9129391Seric 
91359014Seric 	/* reply code 421 is "Service Shutting Down" */
91459014Seric 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
91559014Seric 	{
91659014Seric 		/* send the quit protocol */
91759014Seric 		mci->mci_state = MCIS_SSD;
91859014Seric 		smtpquit(m, mci, e);
9194684Seric 	}
92059014Seric 
92159014Seric 	return (r);
9224684Seric }
9234796Seric /*
9244865Seric **  SMTPMESSAGE -- send message to server
9254796Seric **
9264796Seric **	Parameters:
9274796Seric **		f -- format
92810175Seric **		m -- the mailer to control formatting.
9294796Seric **		a, b, c -- parameters
9304796Seric **
9314796Seric **	Returns:
9324796Seric **		none.
9334796Seric **
9344796Seric **	Side Effects:
93553740Seric **		writes message to mci->mci_out.
9364796Seric */
9374796Seric 
9384865Seric /*VARARGS1*/
93957642Seric #ifdef __STDC__
94057642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...)
94157642Seric #else
94257642Seric smtpmessage(f, m, mci, va_alist)
9434796Seric 	char *f;
94410175Seric 	MAILER *m;
94554967Seric 	MCI *mci;
94657642Seric 	va_dcl
94757642Seric #endif
9484796Seric {
94956852Seric 	VA_LOCAL_DECL
95056852Seric 
95157135Seric 	VA_START(mci);
95256852Seric 	(void) vsprintf(SmtpMsgBuffer, f, ap);
95356852Seric 	VA_END;
95458680Seric 
95558120Seric 	if (tTd(18, 1) || Verbose)
95658151Seric 		nmessage(">>> %s", SmtpMsgBuffer);
95763753Seric 	if (TrafficLogFile != NULL)
95863753Seric 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
95953740Seric 	if (mci->mci_out != NULL)
96058680Seric 	{
96153740Seric 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
96254967Seric 			m == NULL ? "\r\n" : m->m_eol);
96358680Seric 	}
96459149Seric 	else if (tTd(18, 1))
96558725Seric 	{
96659149Seric 		printf("smtpmessage: NULL mci_out\n");
96758725Seric 	}
9684796Seric }
9695182Seric 
97056795Seric # endif /* SMTP */
971