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*68867Seric static char sccsid[] = "@(#)usersmtp.c	8.48 (Berkeley) 04/23/95 (with SMTP)";
1433731Sbostic #else
15*68867Seric static char sccsid[] = "@(#)usersmtp.c	8.48 (Berkeley) 04/23/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 
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 */
8268100Seric 	if (CurHostName == NULL)
8368100Seric 		CurHostName = MyHostName;
8464071Seric 	SmtpNeedIntro = TRUE;
8554967Seric 	switch (mci->mci_state)
866051Seric 	{
8754967Seric 	  case MCIS_ACTIVE:
8854967Seric 		/* need to clear old information */
8954967Seric 		smtprset(m, mci, e);
9057734Seric 		/* fall through */
9115139Seric 
9254967Seric 	  case MCIS_OPEN:
9354967Seric 		return;
9454967Seric 
9554967Seric 	  case MCIS_ERROR:
9654967Seric 	  case MCIS_SSD:
9754967Seric 		/* shouldn't happen */
9854967Seric 		smtpquit(m, mci, e);
9957734Seric 		/* fall through */
10054967Seric 
10154967Seric 	  case MCIS_CLOSED:
10258151Seric 		syserr("451 smtpinit: state CLOSED");
10354967Seric 		return;
10454967Seric 
10554967Seric 	  case MCIS_OPENING:
10654967Seric 		break;
1076051Seric 	}
1084796Seric 
10954967Seric 	mci->mci_state = MCIS_OPENING;
11054967Seric 
1114865Seric 	/*
1124865Seric 	**  Get the greeting message.
11314913Seric 	**	This should appear spontaneously.  Give it five minutes to
11414886Seric 	**	happen.
1154865Seric 	*/
1164797Seric 
11761093Seric 	SmtpPhase = mci->mci_phase = "client greeting";
11853751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
11960210Seric 	r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check);
12064750Seric 	if (r < 0 || REPLYTYPE(r) == 4)
12152104Seric 		goto tempfail1;
12264750Seric 	if (REPLYTYPE(r) != 2)
12364750Seric 		goto unavailable;
1244684Seric 
1254865Seric 	/*
1264976Seric 	**  Send the HELO command.
1277963Seric 	**	My mother taught me to always introduce myself.
1284976Seric 	*/
1294976Seric 
13059285Seric 	if (bitnset(M_ESMTP, m->m_flags))
13159285Seric 		mci->mci_flags |= MCIF_ESMTP;
13259285Seric 
13359285Seric tryhelo:
13459285Seric 	if (bitset(MCIF_ESMTP, mci->mci_flags))
13559285Seric 	{
13659285Seric 		smtpmessage("EHLO %s", m, mci, MyHostName);
13761093Seric 		SmtpPhase = mci->mci_phase = "client EHLO";
13859285Seric 	}
13959285Seric 	else
14059285Seric 	{
14159285Seric 		smtpmessage("HELO %s", m, mci, MyHostName);
14261093Seric 		SmtpPhase = mci->mci_phase = "client HELO";
14359285Seric 	}
14453751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
14559285Seric 	r = reply(m, mci, e, TimeOuts.to_helo, helo_options);
1468005Seric 	if (r < 0)
14752104Seric 		goto tempfail1;
1488005Seric 	else if (REPLYTYPE(r) == 5)
14959285Seric 	{
15059285Seric 		if (bitset(MCIF_ESMTP, mci->mci_flags))
15159285Seric 		{
15259285Seric 			/* try old SMTP instead */
15359285Seric 			mci->mci_flags &= ~MCIF_ESMTP;
15459285Seric 			goto tryhelo;
15559285Seric 		}
15614913Seric 		goto unavailable;
15759285Seric 	}
1587963Seric 	else if (REPLYTYPE(r) != 2)
15952104Seric 		goto tempfail1;
1604976Seric 
1614976Seric 	/*
16258957Seric 	**  Check to see if we actually ended up talking to ourself.
16358957Seric 	**  This means we didn't know about an alias or MX, or we managed
16458957Seric 	**  to connect to an echo server.
16558957Seric 	*/
16658957Seric 
16759026Seric 	p = strchr(&SmtpReplyBuffer[4], ' ');
16858957Seric 	if (p != NULL)
16961707Seric 		*p = '\0';
17067472Seric 	if (!bitnset(M_NOLOOPCHECK, m->m_flags) &&
17167472Seric 	    strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0)
17258957Seric 	{
17358957Seric 		syserr("553 %s config error: mail loops back to myself",
17458957Seric 			MyHostName);
17558957Seric 		mci->mci_exitstat = EX_CONFIG;
17658957Seric 		mci->mci_errno = 0;
17758957Seric 		smtpquit(m, mci, e);
17858957Seric 		return;
17958957Seric 	}
18058957Seric 
18158957Seric 	/*
1829315Seric 	**  If this is expected to be another sendmail, send some internal
1839315Seric 	**  commands.
1849315Seric 	*/
1859315Seric 
18610688Seric 	if (bitnset(M_INTERNAL, m->m_flags))
1879315Seric 	{
1889315Seric 		/* tell it to be verbose */
18953751Seric 		smtpmessage("VERB", m, mci);
19059285Seric 		r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
1919315Seric 		if (r < 0)
19252104Seric 			goto tempfail2;
1939315Seric 	}
1949315Seric 
19565057Seric 	if (mci->mci_state != MCIS_CLOSED)
19665057Seric 	{
19765057Seric 		mci->mci_state = MCIS_OPEN;
19865057Seric 		return;
19965057Seric 	}
20053751Seric 
20165057Seric 	/* got a 421 error code during startup */
20265057Seric 
20353751Seric   tempfail1:
20453751Seric   tempfail2:
20553751Seric 	mci->mci_exitstat = EX_TEMPFAIL;
20657379Seric 	if (mci->mci_errno == 0)
20757379Seric 		mci->mci_errno = errno;
20857379Seric 	if (mci->mci_state != MCIS_CLOSED)
20957379Seric 		smtpquit(m, mci, e);
21054967Seric 	return;
21153751Seric 
21253751Seric   unavailable:
21353751Seric 	mci->mci_exitstat = EX_UNAVAILABLE;
21453751Seric 	mci->mci_errno = errno;
21553751Seric 	smtpquit(m, mci, e);
21654967Seric 	return;
21753751Seric }
21859285Seric /*
21960210Seric **  ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol
22060210Seric **
22160210Seric **	Parameters:
22260210Seric **		line -- the response line.
22367893Seric **		firstline -- set if this is the first line of the reply.
22460210Seric **		m -- the mailer.
22560210Seric **		mci -- the mailer connection info.
22660210Seric **		e -- the envelope.
22760210Seric **
22860210Seric **	Returns:
22960210Seric **		none.
23060210Seric */
23160210Seric 
23260210Seric void
23367893Seric esmtp_check(line, firstline, m, mci, e)
23460210Seric 	char *line;
23567893Seric 	bool firstline;
23660210Seric 	MAILER *m;
23760210Seric 	register MCI *mci;
23860210Seric 	ENVELOPE *e;
23960210Seric {
24068559Seric 	register char *l;
24168559Seric 
24268559Seric 	for (l = line; (l = strchr(++l, 'E')) != NULL; )
24367893Seric 	{
24468559Seric 		if (strncmp(l, "ESMTP ", 6) == 0)
24567893Seric 		{
24667893Seric 			mci->mci_flags |= MCIF_ESMTP;
24767893Seric 			break;
24867893Seric 		}
24967893Seric 	}
25068559Seric 	for (l = line; (l = strchr(++l, '8')) != NULL; )
25168559Seric 	{
25268559Seric 		if (strncmp(l, "8BIT OK", 7) == 0)
25368559Seric 		{
25468559Seric 			mci->mci_flags |= MCIF_8BITOK;
25568559Seric 			break;
25668559Seric 		}
25768559Seric 	}
25860210Seric }
25960210Seric /*
26059285Seric **  HELO_OPTIONS -- process the options on a HELO line.
26159285Seric **
26259285Seric **	Parameters:
26359285Seric **		line -- the response line.
26467893Seric **		firstline -- set if this is the first line of the reply.
26559285Seric **		m -- the mailer.
26659285Seric **		mci -- the mailer connection info.
26759285Seric **		e -- the envelope.
26859285Seric **
26959285Seric **	Returns:
27059285Seric **		none.
27159285Seric */
27253751Seric 
27359285Seric void
27467893Seric helo_options(line, firstline, m, mci, e)
27559285Seric 	char *line;
27667893Seric 	bool firstline;
27759285Seric 	MAILER *m;
27859285Seric 	register MCI *mci;
27959285Seric 	ENVELOPE *e;
28059285Seric {
28159285Seric 	register char *p;
28259285Seric 
28367971Seric 	if (firstline)
28467893Seric 		return;
28567893Seric 
28668706Seric 	if (strlen(line) < (SIZE_T) 5)
28759285Seric 		return;
28859285Seric 	line += 4;
28959285Seric 	p = strchr(line, ' ');
29059285Seric 	if (p != NULL)
29159285Seric 		*p++ = '\0';
29259285Seric 	if (strcasecmp(line, "size") == 0)
29359285Seric 	{
29459285Seric 		mci->mci_flags |= MCIF_SIZE;
29559285Seric 		if (p != NULL)
29659285Seric 			mci->mci_maxsize = atol(p);
29759285Seric 	}
29859285Seric 	else if (strcasecmp(line, "8bitmime") == 0)
29965870Seric 	{
30059285Seric 		mci->mci_flags |= MCIF_8BITMIME;
30165870Seric 		mci->mci_flags &= ~MCIF_7BIT;
30265870Seric 	}
30359285Seric 	else if (strcasecmp(line, "expn") == 0)
30459285Seric 		mci->mci_flags |= MCIF_EXPN;
30568606Seric 	else if (strcasecmp(line, "x-dsn-03") == 0)
30667880Seric 		mci->mci_flags |= MCIF_DSN;
30759285Seric }
30859285Seric /*
30959285Seric **  SMTPMAILFROM -- send MAIL command
31059285Seric **
31159285Seric **	Parameters:
31259285Seric **		m -- the mailer.
31359285Seric **		mci -- the mailer connection structure.
31459285Seric **		e -- the envelope (including the sender to specify).
31559285Seric */
31659285Seric 
31753751Seric smtpmailfrom(m, mci, e)
31853751Seric 	struct mailer *m;
31954967Seric 	MCI *mci;
32053751Seric 	ENVELOPE *e;
32153751Seric {
32253751Seric 	int r;
32365494Seric 	char *bufp;
32467887Seric 	char *bodytype;
32568528Seric 	char buf[MAXNAME + 1];
32659285Seric 	char optbuf[MAXLINE];
32753751Seric 
32863753Seric 	if (tTd(18, 2))
32957943Seric 		printf("smtpmailfrom: CurHost=%s\n", CurHostName);
33057943Seric 
33159285Seric 	/* set up appropriate options to include */
33264254Seric 	if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0)
33359285Seric 		sprintf(optbuf, " SIZE=%ld", e->e_msgsize);
33459285Seric 	else
33559285Seric 		strcpy(optbuf, "");
33659285Seric 
33767887Seric 	bodytype = e->e_bodytype;
33867887Seric 	if (bitset(MCIF_8BITMIME, mci->mci_flags))
33967417Seric 	{
34067887Seric 		if (bodytype == NULL &&
34167887Seric 		    bitset(MM_MIME8BIT, MimeMode) &&
34267887Seric 		    bitset(EF_HAS8BIT, e->e_flags) &&
34367887Seric 		    !bitnset(M_8BITS, m->m_flags))
34467887Seric 			bodytype = "8BITMIME";
34567887Seric 		if (bodytype != NULL)
34667417Seric 		{
34767417Seric 			strcat(optbuf, " BODY=");
34867887Seric 			strcat(optbuf, bodytype);
34967417Seric 		}
35067417Seric 	}
35167995Seric 	else if (bitnset(M_8BITS, m->m_flags) ||
35267995Seric 		 !bitset(EF_HAS8BIT, e->e_flags) ||
35367995Seric 		 (e->e_bodytype != NULL &&
35467995Seric 		  strcasecmp(e->e_bodytype, "7bit") == 0))
35567887Seric 	{
35667887Seric 		/* just pass it through */
35767887Seric 	}
35867887Seric 	else if (bitset(MM_CVTMIME, MimeMode) &&
35967995Seric 		 (e->e_bodytype != NULL || !bitset(MM_PASS8BIT, MimeMode)))
36067887Seric 	{
36167887Seric 		/* must convert from 8bit MIME format to 7bit encoded */
36267887Seric 		mci->mci_flags |= MCIF_CVT8TO7;
36367887Seric 	}
36467887Seric 	else if (!bitset(MM_PASS8BIT, MimeMode))
36567887Seric 	{
36667887Seric 		/* cannot just send a 8-bit version */
36767887Seric 		usrerr("%s does not support 8BITMIME", mci->mci_host);
36868857Seric 		mci->mci_status = "5.6.3";
36967887Seric 		return EX_DATAERR;
37067887Seric 	}
37167417Seric 
37267963Seric 	if (bitset(MCIF_DSN, mci->mci_flags))
37367880Seric 	{
37467963Seric 		if (e->e_envid != NULL)
37567963Seric 		{
37667963Seric 			strcat(optbuf, " ENVID=");
37767963Seric 			strcat(optbuf, e->e_envid);
37867963Seric 		}
37968559Seric 
38068559Seric 		/* RET= parameter */
38168559Seric 		if (bitset(EF_RET_PARAM, e->e_flags))
38268559Seric 		{
38368559Seric 			strcat(optbuf, " RET=");
38468559Seric 			if (bitset(EF_NO_BODY_RETN, e->e_flags))
38568559Seric 				strcat(optbuf, "HDRS");
38668559Seric 			else
38768559Seric 				strcat(optbuf, "FULL");
38868559Seric 		}
38967880Seric 	}
39067880Seric 
3919315Seric 	/*
3924865Seric 	**  Send the MAIL command.
3934865Seric 	**	Designates the sender.
3944865Seric 	*/
3954796Seric 
39653751Seric 	mci->mci_state = MCIS_ACTIVE;
39753751Seric 
39859540Seric 	if (bitset(EF_RESPONSE, e->e_flags) &&
39959540Seric 	    !bitnset(M_NO_NULL_FROM, m->m_flags))
40058680Seric 		(void) strcpy(buf, "");
40158680Seric 	else
40268529Seric 		expand("\201g", buf, sizeof buf, e);
40365494Seric 	if (buf[0] == '<')
40465494Seric 	{
40565494Seric 		/* strip off <angle brackets> (put back on below) */
40665494Seric 		bufp = &buf[strlen(buf) - 1];
40765494Seric 		if (*bufp == '>')
40865494Seric 			*bufp = '\0';
40965494Seric 		bufp = &buf[1];
41065494Seric 	}
41165494Seric 	else
41265494Seric 		bufp = buf;
41367472Seric 	if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) ||
41410688Seric 	    !bitnset(M_FROMPATH, m->m_flags))
4158436Seric 	{
41665494Seric 		smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf);
4178436Seric 	}
4188436Seric 	else
4198436Seric 	{
42059285Seric 		smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
42165494Seric 			*bufp == '@' ? ',' : ':', bufp, optbuf);
4228436Seric 	}
42361093Seric 	SmtpPhase = mci->mci_phase = "client MAIL";
42453751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
42559285Seric 	r = reply(m, mci, e, TimeOuts.to_mail, NULL);
42668811Seric 	if (r < 0 || r == 421)
42753751Seric 	{
42868811Seric 		/* communications failure/service shutting down */
42953751Seric 		mci->mci_exitstat = EX_TEMPFAIL;
43053751Seric 		mci->mci_errno = errno;
43153751Seric 		smtpquit(m, mci, e);
43253751Seric 		return EX_TEMPFAIL;
43353751Seric 	}
43468811Seric 	else if (REPLYTYPE(r) == 4)
43568811Seric 	{
43668811Seric 		return EX_TEMPFAIL;
43768811Seric 	}
4387963Seric 	else if (r == 250)
43953751Seric 	{
44053751Seric 		return EX_OK;
44153751Seric 	}
44268857Seric 	else if (r == 501)
44368075Seric 	{
44468857Seric 		/* syntax error in arguments */
44568857Seric 		mci->mci_status = "5.5.2";
44668075Seric 		return EX_DATAERR;
44768075Seric 	}
44868857Seric 	else if (r == 553)
44968857Seric 	{
45068857Seric 		/* mailbox name not allowed */
45168857Seric 		mci->mci_status = "5.1.3";
45268857Seric 		return EX_DATAERR;
45368857Seric 	}
4547963Seric 	else if (r == 552)
45553751Seric 	{
45668811Seric 		/* exceeded storage allocation */
45768857Seric 		mci->mci_status = "5.2.2";
45853751Seric 		return EX_UNAVAILABLE;
45953751Seric 	}
46014913Seric 
46158008Seric #ifdef LOG
46258020Seric 	if (LogLevel > 1)
46358008Seric 	{
46467860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s",
46567860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
46658008Seric 	}
46758008Seric #endif
46858008Seric 
46914913Seric 	/* protocol error -- close up */
47053751Seric 	smtpquit(m, mci, e);
47153751Seric 	return EX_PROTOCOL;
4724684Seric }
4734684Seric /*
4744976Seric **  SMTPRCPT -- designate recipient.
4754797Seric **
4764797Seric **	Parameters:
4774865Seric **		to -- address of recipient.
47810175Seric **		m -- the mailer we are sending to.
47957379Seric **		mci -- the connection info for this transaction.
48057379Seric **		e -- the envelope for this transaction.
4814797Seric **
4824797Seric **	Returns:
4834865Seric **		exit status corresponding to recipient status.
4844797Seric **
4854797Seric **	Side Effects:
4864865Seric **		Sends the mail via SMTP.
4874797Seric */
4884797Seric 
48953751Seric smtprcpt(to, m, mci, e)
4904865Seric 	ADDRESS *to;
49110175Seric 	register MAILER *m;
49254967Seric 	MCI *mci;
49353751Seric 	ENVELOPE *e;
4944797Seric {
4954797Seric 	register int r;
49667880Seric 	char optbuf[MAXLINE];
49768857Seric 	extern char *smtptodsn();
4984797Seric 
49967880Seric 	strcpy(optbuf, "");
50067880Seric 	if (bitset(MCIF_DSN, mci->mci_flags))
50167880Seric 	{
50267987Seric 		/* NOTIFY= parameter */
50368603Seric 		if (bitset(QHASNOTIFY, to->q_flags) &&
50468603Seric 		    bitset(QPRIMARY, to->q_flags))
50567880Seric 		{
50668595Seric 			bool firstone = TRUE;
50768595Seric 
50868595Seric 			strcat(optbuf, " NOTIFY=");
50968595Seric 			if (bitset(QPINGONSUCCESS, to->q_flags))
51068595Seric 			{
51168595Seric 				strcat(optbuf, "SUCCESS");
51268595Seric 				firstone = FALSE;
51368595Seric 			}
51468595Seric 			if (bitset(QPINGONFAILURE, to->q_flags))
51568595Seric 			{
51668595Seric 				if (!firstone)
51768595Seric 					strcat(optbuf, ",");
51868595Seric 				strcat(optbuf, "FAILURE");
51968595Seric 				firstone = FALSE;
52068595Seric 			}
52168595Seric 			if (bitset(QPINGONDELAY, to->q_flags))
52268595Seric 			{
52368595Seric 				if (!firstone)
52468595Seric 					strcat(optbuf, ",");
52568595Seric 				strcat(optbuf, "DELAY");
52668595Seric 				firstone = FALSE;
52768595Seric 			}
52868595Seric 			if (firstone)
52968595Seric 				strcat(optbuf, "NEVER");
53067880Seric 		}
53167963Seric 
53267987Seric 		/* ORCPT= parameter */
53367987Seric 		if (to->q_orcpt != NULL)
53467987Seric 		{
53567987Seric 			strcat(optbuf, " ORCPT=");
53667987Seric 			strcat(optbuf, to->q_orcpt);
53767987Seric 		}
53867880Seric 	}
5394865Seric 
54067880Seric 	smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
54167880Seric 
54261093Seric 	SmtpPhase = mci->mci_phase = "client RCPT";
54353751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
54459285Seric 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
545*68867Seric 	to->q_rstatus = newstr(SmtpReplyBuffer);
54668857Seric 	to->q_status = smtptodsn(r);
5478005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
54868857Seric 		return EX_TEMPFAIL;
5497963Seric 	else if (REPLYTYPE(r) == 2)
55068857Seric 		return EX_OK;
5517964Seric 	else if (r == 550 || r == 551 || r == 553)
55268857Seric 		return EX_NOUSER;
5537964Seric 	else if (r == 552 || r == 554)
55468857Seric 		return EX_UNAVAILABLE;
55558008Seric 
55658008Seric #ifdef LOG
55758020Seric 	if (LogLevel > 1)
55858008Seric 	{
55967860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s",
56067860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
56158008Seric 	}
56258008Seric #endif
56358008Seric 
5647964Seric 	return (EX_PROTOCOL);
5654797Seric }
5664797Seric /*
56710175Seric **  SMTPDATA -- send the data and clean up the transaction.
5684684Seric **
5694684Seric **	Parameters:
5704865Seric **		m -- mailer being sent to.
5716980Seric **		e -- the envelope for this message.
5724684Seric **
5734684Seric **	Returns:
5744976Seric **		exit status corresponding to DATA command.
5754684Seric **
5764684Seric **	Side Effects:
5774865Seric **		none.
5784684Seric */
5794684Seric 
58063753Seric static jmp_buf	CtxDataTimeout;
58168433Seric static void	datatimeout();
58263753Seric 
58353740Seric smtpdata(m, mci, e)
5844865Seric 	struct mailer *m;
58554967Seric 	register MCI *mci;
5866980Seric 	register ENVELOPE *e;
5874684Seric {
5884684Seric 	register int r;
58963753Seric 	register EVENT *ev;
59063753Seric 	time_t timeout;
5914684Seric 
5924797Seric 	/*
5934797Seric 	**  Send the data.
59410175Seric 	**	First send the command and check that it is ok.
59510175Seric 	**	Then send the data.
59610175Seric 	**	Follow it up with a dot to terminate.
59710175Seric 	**	Finally get the results of the transaction.
5984797Seric 	*/
5994797Seric 
60010175Seric 	/* send the command and check ok to proceed */
60153751Seric 	smtpmessage("DATA", m, mci);
60261093Seric 	SmtpPhase = mci->mci_phase = "client DATA 354";
60353751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
60459285Seric 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
6058005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
60657990Seric 	{
60757990Seric 		smtpquit(m, mci, e);
6084797Seric 		return (EX_TEMPFAIL);
60957990Seric 	}
6107963Seric 	else if (r == 554)
61157990Seric 	{
61257990Seric 		smtprset(m, mci, e);
6137963Seric 		return (EX_UNAVAILABLE);
61457990Seric 	}
6157963Seric 	else if (r != 354)
61657990Seric 	{
61758008Seric #ifdef LOG
61858020Seric 		if (LogLevel > 1)
61958008Seric 		{
62067860Seric 			syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s",
62167860Seric 				e->e_id, mci->mci_host, SmtpReplyBuffer);
62258008Seric 		}
62358008Seric #endif
62457990Seric 		smtprset(m, mci, e);
6257964Seric 		return (EX_PROTOCOL);
62657990Seric 	}
62710175Seric 
62863757Seric 	/*
62963757Seric 	**  Set timeout around data writes.  Make it at least large
63063757Seric 	**  enough for DNS timeouts on all recipients plus some fudge
63163757Seric 	**  factor.  The main thing is that it should not be infinite.
63263757Seric 	*/
63363757Seric 
63463753Seric 	if (setjmp(CtxDataTimeout) != 0)
63563753Seric 	{
63663753Seric 		mci->mci_errno = errno;
63763753Seric 		mci->mci_exitstat = EX_TEMPFAIL;
63863753Seric 		mci->mci_state = MCIS_ERROR;
63963753Seric 		syserr("451 timeout writing message to %s", mci->mci_host);
64063753Seric 		smtpquit(m, mci, e);
64163753Seric 		return EX_TEMPFAIL;
64263753Seric 	}
64363753Seric 
64463787Seric 	timeout = e->e_msgsize / 16;
64568740Seric 	if (timeout < (time_t) 600)
64668740Seric 		timeout = (time_t) 600;
64768740Seric 	timeout += e->e_nrcpts * 300;
64863753Seric 	ev = setevent(timeout, datatimeout, 0);
64963753Seric 
65067546Seric 	/*
65167546Seric 	**  Output the actual message.
65267546Seric 	*/
65367546Seric 
65468228Seric 	(*e->e_puthdr)(mci, e->e_header, e);
65568228Seric 	(*e->e_putbody)(mci, e, NULL);
65610175Seric 
65767546Seric 	/*
65867546Seric 	**  Cleanup after sending message.
65967546Seric 	*/
66067546Seric 
66163753Seric 	clrevent(ev);
66263753Seric 
66364718Seric 	if (ferror(mci->mci_out))
66464718Seric 	{
66564718Seric 		/* error during processing -- don't send the dot */
66664718Seric 		mci->mci_errno = EIO;
66764718Seric 		mci->mci_exitstat = EX_IOERR;
66864718Seric 		mci->mci_state = MCIS_ERROR;
66964718Seric 		smtpquit(m, mci, e);
67064718Seric 		return EX_IOERR;
67164718Seric 	}
67264718Seric 
67310175Seric 	/* terminate the message */
67453740Seric 	fprintf(mci->mci_out, ".%s", m->m_eol);
67563753Seric 	if (TrafficLogFile != NULL)
67663753Seric 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
67758120Seric 	if (Verbose)
67858151Seric 		nmessage(">>> .");
67910175Seric 
68010175Seric 	/* check for the results of the transaction */
68161093Seric 	SmtpPhase = mci->mci_phase = "client DATA 250";
68253751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
68359285Seric 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
68453751Seric 	if (r < 0)
68557990Seric 	{
68657990Seric 		smtpquit(m, mci, e);
6874797Seric 		return (EX_TEMPFAIL);
68857990Seric 	}
68953751Seric 	mci->mci_state = MCIS_OPEN;
69058917Seric 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
69153751Seric 	if (REPLYTYPE(r) == 4)
69253751Seric 		return (EX_TEMPFAIL);
6937963Seric 	else if (r == 250)
6947963Seric 		return (EX_OK);
6957963Seric 	else if (r == 552 || r == 554)
6967963Seric 		return (EX_UNAVAILABLE);
69758008Seric #ifdef LOG
69858020Seric 	if (LogLevel > 1)
69958008Seric 	{
70067860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s",
70167860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
70258008Seric 	}
70358008Seric #endif
7047964Seric 	return (EX_PROTOCOL);
7054684Seric }
70663753Seric 
70763753Seric 
70868433Seric static void
70963753Seric datatimeout()
71063753Seric {
71163753Seric 	longjmp(CtxDataTimeout, 1);
71263753Seric }
7134684Seric /*
7144865Seric **  SMTPQUIT -- close the SMTP connection.
7154865Seric **
7164865Seric **	Parameters:
71715535Seric **		m -- a pointer to the mailer.
7184865Seric **
7194865Seric **	Returns:
7204865Seric **		none.
7214865Seric **
7224865Seric **	Side Effects:
7234865Seric **		sends the final protocol and closes the connection.
7244865Seric */
7254865Seric 
72653751Seric smtpquit(m, mci, e)
72753751Seric 	register MAILER *m;
72854967Seric 	register MCI *mci;
72953751Seric 	ENVELOPE *e;
7304865Seric {
73164822Seric 	bool oldSuprErrs = SuprErrs;
7324865Seric 
73364822Seric 	/*
73464822Seric 	**	Suppress errors here -- we may be processing a different
73564822Seric 	**	job when we do the quit connection, and we don't want the
73664822Seric 	**	new job to be penalized for something that isn't it's
73764822Seric 	**	problem.
73864822Seric 	*/
73964822Seric 
74064822Seric 	SuprErrs = TRUE;
74164822Seric 
74254967Seric 	/* send the quit message if we haven't gotten I/O error */
74353751Seric 	if (mci->mci_state != MCIS_ERROR)
7449391Seric 	{
74561093Seric 		SmtpPhase = "client QUIT";
74653751Seric 		smtpmessage("QUIT", m, mci);
74759285Seric 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
74864822Seric 		SuprErrs = oldSuprErrs;
74953740Seric 		if (mci->mci_state == MCIS_CLOSED)
75064822Seric 		{
75164822Seric 			SuprErrs = oldSuprErrs;
75210159Seric 			return;
75364822Seric 		}
7549391Seric 	}
7559391Seric 
75652676Seric 	/* now actually close the connection and pick up the zombie */
75765194Seric 	(void) endmailer(mci, e, NULL);
75864822Seric 
75964822Seric 	SuprErrs = oldSuprErrs;
7604865Seric }
7614865Seric /*
76254967Seric **  SMTPRSET -- send a RSET (reset) command
76354967Seric */
76454967Seric 
76554967Seric smtprset(m, mci, e)
76654967Seric 	register MAILER *m;
76754967Seric 	register MCI *mci;
76854967Seric 	ENVELOPE *e;
76954967Seric {
77054967Seric 	int r;
77154967Seric 
77261093Seric 	SmtpPhase = "client RSET";
77354967Seric 	smtpmessage("RSET", m, mci);
77459285Seric 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
77557734Seric 	if (r < 0)
77657734Seric 		mci->mci_state = MCIS_ERROR;
77754967Seric 	else if (REPLYTYPE(r) == 2)
77857734Seric 	{
77957734Seric 		mci->mci_state = MCIS_OPEN;
78057734Seric 		return;
78157734Seric 	}
78257734Seric 	smtpquit(m, mci, e);
78354967Seric }
78454967Seric /*
78558867Seric **  SMTPPROBE -- check the connection state
78654967Seric */
78754967Seric 
78858867Seric smtpprobe(mci)
78954967Seric 	register MCI *mci;
79054967Seric {
79154967Seric 	int r;
79254967Seric 	MAILER *m = mci->mci_mailer;
79354967Seric 	extern ENVELOPE BlankEnvelope;
79454967Seric 	ENVELOPE *e = &BlankEnvelope;
79554967Seric 
79661093Seric 	SmtpPhase = "client probe";
79758867Seric 	smtpmessage("RSET", m, mci);
79859285Seric 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
79958061Seric 	if (r < 0 || REPLYTYPE(r) != 2)
80054967Seric 		smtpquit(m, mci, e);
80154967Seric 	return r;
80254967Seric }
80354967Seric /*
8044684Seric **  REPLY -- read arpanet reply
8054684Seric **
8064684Seric **	Parameters:
80710175Seric **		m -- the mailer we are reading the reply from.
80857379Seric **		mci -- the mailer connection info structure.
80957379Seric **		e -- the current envelope.
81057379Seric **		timeout -- the timeout for reads.
81159285Seric **		pfunc -- processing function for second and subsequent
81259285Seric **			lines of response -- if null, no special
81359285Seric **			processing is done.
8144684Seric **
8154684Seric **	Returns:
8164684Seric **		reply code it reads.
8174684Seric **
8184684Seric **	Side Effects:
8194684Seric **		flushes the mail file.
8204684Seric */
8214684Seric 
82259285Seric reply(m, mci, e, timeout, pfunc)
82353751Seric 	MAILER *m;
82454967Seric 	MCI *mci;
82553751Seric 	ENVELOPE *e;
82659285Seric 	time_t timeout;
82759285Seric 	void (*pfunc)();
8284684Seric {
82959014Seric 	register char *bufp;
83059014Seric 	register int r;
83159285Seric 	bool firstline = TRUE;
83258957Seric 	char junkbuf[MAXLINE];
83358957Seric 
83457379Seric 	if (mci->mci_out != NULL)
83557379Seric 		(void) fflush(mci->mci_out);
8364684Seric 
8377677Seric 	if (tTd(18, 1))
8384796Seric 		printf("reply\n");
8394796Seric 
8407356Seric 	/*
8417356Seric 	**  Read the input line, being careful not to hang.
8427356Seric 	*/
8437356Seric 
84459014Seric 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
8454684Seric 	{
8467356Seric 		register char *p;
84753751Seric 		extern time_t curtime();
8484684Seric 
8497685Seric 		/* actually do the read */
85053751Seric 		if (e->e_xfp != NULL)
85153751Seric 			(void) fflush(e->e_xfp);	/* for debugging */
8527356Seric 
85310054Seric 		/* if we are in the process of closing just give the code */
85453740Seric 		if (mci->mci_state == MCIS_CLOSED)
85510054Seric 			return (SMTPCLOSING);
85610054Seric 
85758680Seric 		if (mci->mci_out != NULL)
85858680Seric 			fflush(mci->mci_out);
85958680Seric 
86010054Seric 		/* get the line from the other side */
86161093Seric 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
86253751Seric 		mci->mci_lastuse = curtime();
86353751Seric 
86410054Seric 		if (p == NULL)
86510131Seric 		{
86663753Seric 			bool oldholderrs;
86710148Seric 			extern char MsgBuf[];		/* err.c */
86810148Seric 
86921065Seric 			/* if the remote end closed early, fake an error */
87021065Seric 			if (errno == 0)
87121065Seric # ifdef ECONNRESET
87221065Seric 				errno = ECONNRESET;
87356795Seric # else /* ECONNRESET */
87421065Seric 				errno = EPIPE;
87556795Seric # endif /* ECONNRESET */
87621065Seric 
87757379Seric 			mci->mci_errno = errno;
87857642Seric 			mci->mci_exitstat = EX_TEMPFAIL;
87963753Seric 			oldholderrs = HoldErrs;
88063753Seric 			HoldErrs = TRUE;
88163753Seric 			usrerr("451 reply: read error from %s", mci->mci_host);
88263753Seric 
88310420Seric 			/* if debugging, pause so we can see state */
88410420Seric 			if (tTd(18, 100))
88510420Seric 				pause();
88654967Seric 			mci->mci_state = MCIS_ERROR;
88753751Seric 			smtpquit(m, mci, e);
88863753Seric #ifdef XDEBUG
88963753Seric 			{
89063753Seric 				char wbuf[MAXLINE];
89164082Seric 				char *p = wbuf;
89264082Seric 				if (e->e_to != NULL)
89364082Seric 				{
89464082Seric 					sprintf(p, "%s... ", e->e_to);
89564082Seric 					p += strlen(p);
89664082Seric 				}
89764082Seric 				sprintf(p, "reply(%s) during %s",
89864082Seric 					mci->mci_host, SmtpPhase);
89963753Seric 				checkfd012(wbuf);
90063753Seric 			}
90163753Seric #endif
90263753Seric 			HoldErrs = oldholderrs;
90310054Seric 			return (-1);
90410131Seric 		}
90558957Seric 		fixcrlf(bufp, TRUE);
90610054Seric 
90763753Seric 		/* EHLO failure is not a real error */
90863753Seric 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
90963753Seric 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
91014900Seric 		{
91114900Seric 			/* serious error -- log the previous command */
91264071Seric 			if (SmtpNeedIntro)
91364071Seric 			{
91464071Seric 				/* inform user who we are chatting with */
91564071Seric 				fprintf(CurEnv->e_xfp,
91664071Seric 					"... while talking to %s:\n",
91764071Seric 					CurHostName);
91864071Seric 				SmtpNeedIntro = FALSE;
91964071Seric 			}
92059014Seric 			if (SmtpMsgBuffer[0] != '\0')
92159014Seric 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
92259014Seric 			SmtpMsgBuffer[0] = '\0';
92314900Seric 
92414900Seric 			/* now log the message as from the other side */
92558957Seric 			fprintf(e->e_xfp, "<<< %s\n", bufp);
92614900Seric 		}
92714900Seric 
92814900Seric 		/* display the input for verbose mode */
92958120Seric 		if (Verbose)
93059956Seric 			nmessage("050 %s", bufp);
9317356Seric 
93259285Seric 		/* process the line */
93367893Seric 		if (pfunc != NULL)
93467893Seric 			(*pfunc)(bufp, firstline, m, mci, e);
93559285Seric 
93659285Seric 		firstline = FALSE;
93759285Seric 
9387356Seric 		/* if continuation is required, we can go on */
93959014Seric 		if (bufp[3] == '-')
9404684Seric 			continue;
9417356Seric 
94259014Seric 		/* ignore improperly formated input */
94359014Seric 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
94459014Seric 			continue;
94559014Seric 
9467356Seric 		/* decode the reply code */
94759014Seric 		r = atoi(bufp);
9487356Seric 
9497356Seric 		/* extra semantics: 0xx codes are "informational" */
95059014Seric 		if (r >= 100)
95159014Seric 			break;
95259014Seric 	}
9537356Seric 
95459014Seric 	/*
95559014Seric 	**  Now look at SmtpReplyBuffer -- only care about the first
95659014Seric 	**  line of the response from here on out.
95759014Seric 	*/
95858061Seric 
95959014Seric 	/* save temporary failure messages for posterity */
96059014Seric 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
96159014Seric 		(void) strcpy(SmtpError, SmtpReplyBuffer);
9629391Seric 
96359014Seric 	/* reply code 421 is "Service Shutting Down" */
96459014Seric 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
96559014Seric 	{
96659014Seric 		/* send the quit protocol */
96759014Seric 		mci->mci_state = MCIS_SSD;
96859014Seric 		smtpquit(m, mci, e);
9694684Seric 	}
97059014Seric 
97159014Seric 	return (r);
9724684Seric }
9734796Seric /*
9744865Seric **  SMTPMESSAGE -- send message to server
9754796Seric **
9764796Seric **	Parameters:
9774796Seric **		f -- format
97810175Seric **		m -- the mailer to control formatting.
9794796Seric **		a, b, c -- parameters
9804796Seric **
9814796Seric **	Returns:
9824796Seric **		none.
9834796Seric **
9844796Seric **	Side Effects:
98553740Seric **		writes message to mci->mci_out.
9864796Seric */
9874796Seric 
9884865Seric /*VARARGS1*/
98957642Seric #ifdef __STDC__
99057642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...)
99157642Seric #else
99257642Seric smtpmessage(f, m, mci, va_alist)
9934796Seric 	char *f;
99410175Seric 	MAILER *m;
99554967Seric 	MCI *mci;
99657642Seric 	va_dcl
99757642Seric #endif
9984796Seric {
99956852Seric 	VA_LOCAL_DECL
100056852Seric 
100157135Seric 	VA_START(mci);
100256852Seric 	(void) vsprintf(SmtpMsgBuffer, f, ap);
100356852Seric 	VA_END;
100458680Seric 
100558120Seric 	if (tTd(18, 1) || Verbose)
100658151Seric 		nmessage(">>> %s", SmtpMsgBuffer);
100763753Seric 	if (TrafficLogFile != NULL)
100863753Seric 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
100953740Seric 	if (mci->mci_out != NULL)
101058680Seric 	{
101153740Seric 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
101254967Seric 			m == NULL ? "\r\n" : m->m_eol);
101358680Seric 	}
101459149Seric 	else if (tTd(18, 1))
101558725Seric 	{
101659149Seric 		printf("smtpmessage: NULL mci_out\n");
101758725Seric 	}
10184796Seric }
10195182Seric 
102056795Seric # endif /* SMTP */
1021