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*68595Seric static char sccsid[] = "@(#)usersmtp.c	8.40 (Berkeley) 03/25/95 (with SMTP)";
1433731Sbostic #else
15*68595Seric static char sccsid[] = "@(#)usersmtp.c	8.40 (Berkeley) 03/25/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 
28659285Seric 	if (strlen(line) < 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;
30568559Seric 	else if (strcasecmp(line, "x-dsn-3") == 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);
36867887Seric 		return EX_DATAERR;
36967887Seric 	}
37067417Seric 
37167963Seric 	if (bitset(MCIF_DSN, mci->mci_flags))
37267880Seric 	{
37367963Seric 		if (e->e_envid != NULL)
37467963Seric 		{
37567963Seric 			strcat(optbuf, " ENVID=");
37667963Seric 			strcat(optbuf, e->e_envid);
37767963Seric 		}
37868559Seric 
37968559Seric 		/* RET= parameter */
38068559Seric 		if (bitset(EF_RET_PARAM, e->e_flags))
38168559Seric 		{
38268559Seric 			strcat(optbuf, " RET=");
38368559Seric 			if (bitset(EF_NO_BODY_RETN, e->e_flags))
38468559Seric 				strcat(optbuf, "HDRS");
38568559Seric 			else
38668559Seric 				strcat(optbuf, "FULL");
38768559Seric 		}
38867880Seric 	}
38967880Seric 
3909315Seric 	/*
3914865Seric 	**  Send the MAIL command.
3924865Seric 	**	Designates the sender.
3934865Seric 	*/
3944796Seric 
39553751Seric 	mci->mci_state = MCIS_ACTIVE;
39653751Seric 
39759540Seric 	if (bitset(EF_RESPONSE, e->e_flags) &&
39859540Seric 	    !bitnset(M_NO_NULL_FROM, m->m_flags))
39958680Seric 		(void) strcpy(buf, "");
40058680Seric 	else
40168529Seric 		expand("\201g", buf, sizeof buf, e);
40265494Seric 	if (buf[0] == '<')
40365494Seric 	{
40465494Seric 		/* strip off <angle brackets> (put back on below) */
40565494Seric 		bufp = &buf[strlen(buf) - 1];
40665494Seric 		if (*bufp == '>')
40765494Seric 			*bufp = '\0';
40865494Seric 		bufp = &buf[1];
40965494Seric 	}
41065494Seric 	else
41165494Seric 		bufp = buf;
41267472Seric 	if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) ||
41310688Seric 	    !bitnset(M_FROMPATH, m->m_flags))
4148436Seric 	{
41565494Seric 		smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf);
4168436Seric 	}
4178436Seric 	else
4188436Seric 	{
41959285Seric 		smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName,
42065494Seric 			*bufp == '@' ? ',' : ':', bufp, optbuf);
4218436Seric 	}
42261093Seric 	SmtpPhase = mci->mci_phase = "client MAIL";
42353751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
42459285Seric 	r = reply(m, mci, e, TimeOuts.to_mail, NULL);
4258005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
42653751Seric 	{
42753751Seric 		mci->mci_exitstat = EX_TEMPFAIL;
42853751Seric 		mci->mci_errno = errno;
42953751Seric 		smtpquit(m, mci, e);
43053751Seric 		return EX_TEMPFAIL;
43153751Seric 	}
4327963Seric 	else if (r == 250)
43353751Seric 	{
43453751Seric 		return EX_OK;
43553751Seric 	}
43668075Seric 	else if (r == 501 || r == 553)
43768075Seric 	{
43868075Seric 		/* syntax error in arguments */
43968075Seric 		smtpquit(m, mci, e);
44068075Seric 		return EX_DATAERR;
44168075Seric 	}
4427963Seric 	else if (r == 552)
44353751Seric 	{
44453751Seric 		/* signal service unavailable */
44553751Seric 		smtpquit(m, mci, e);
44653751Seric 		return EX_UNAVAILABLE;
44753751Seric 	}
44814913Seric 
44958008Seric #ifdef LOG
45058020Seric 	if (LogLevel > 1)
45158008Seric 	{
45267860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s",
45367860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
45458008Seric 	}
45558008Seric #endif
45658008Seric 
45714913Seric 	/* protocol error -- close up */
45853751Seric 	smtpquit(m, mci, e);
45953751Seric 	return EX_PROTOCOL;
4604684Seric }
4614684Seric /*
4624976Seric **  SMTPRCPT -- designate recipient.
4634797Seric **
4644797Seric **	Parameters:
4654865Seric **		to -- address of recipient.
46610175Seric **		m -- the mailer we are sending to.
46757379Seric **		mci -- the connection info for this transaction.
46857379Seric **		e -- the envelope for this transaction.
4694797Seric **
4704797Seric **	Returns:
4714865Seric **		exit status corresponding to recipient status.
4724797Seric **
4734797Seric **	Side Effects:
4744865Seric **		Sends the mail via SMTP.
4754797Seric */
4764797Seric 
47753751Seric smtprcpt(to, m, mci, e)
4784865Seric 	ADDRESS *to;
47910175Seric 	register MAILER *m;
48054967Seric 	MCI *mci;
48153751Seric 	ENVELOPE *e;
4824797Seric {
4834797Seric 	register int r;
48467880Seric 	char optbuf[MAXLINE];
4854797Seric 
48667880Seric 	strcpy(optbuf, "");
48767880Seric 	if (bitset(MCIF_DSN, mci->mci_flags))
48867880Seric 	{
48967987Seric 		/* NOTIFY= parameter */
490*68595Seric 		if (bitset(QHASNOTIFY, to->q_flags))
49167880Seric 		{
492*68595Seric 			bool firstone = TRUE;
493*68595Seric 
494*68595Seric 			strcat(optbuf, " NOTIFY=");
495*68595Seric 			if (bitset(QPINGONSUCCESS, to->q_flags))
496*68595Seric 			{
497*68595Seric 				strcat(optbuf, "SUCCESS");
498*68595Seric 				firstone = FALSE;
499*68595Seric 			}
500*68595Seric 			if (bitset(QPINGONFAILURE, to->q_flags))
501*68595Seric 			{
502*68595Seric 				if (!firstone)
503*68595Seric 					strcat(optbuf, ",");
504*68595Seric 				strcat(optbuf, "FAILURE");
505*68595Seric 				firstone = FALSE;
506*68595Seric 			}
507*68595Seric 			if (bitset(QPINGONDELAY, to->q_flags))
508*68595Seric 			{
509*68595Seric 				if (!firstone)
510*68595Seric 					strcat(optbuf, ",");
511*68595Seric 				strcat(optbuf, "DELAY");
512*68595Seric 				firstone = FALSE;
513*68595Seric 			}
514*68595Seric 			if (firstone)
515*68595Seric 				strcat(optbuf, "NEVER");
51667880Seric 		}
51767963Seric 
51867987Seric 		/* ORCPT= parameter */
51967987Seric 		if (to->q_orcpt != NULL)
52067987Seric 		{
52167987Seric 			strcat(optbuf, " ORCPT=");
52267987Seric 			strcat(optbuf, to->q_orcpt);
52367987Seric 		}
52467880Seric 	}
5254865Seric 
52667880Seric 	smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf);
52767880Seric 
52861093Seric 	SmtpPhase = mci->mci_phase = "client RCPT";
52953751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
53059285Seric 	r = reply(m, mci, e, TimeOuts.to_rcpt, NULL);
53167880Seric 	setstatus(to, SmtpReplyBuffer);
5328005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
5334865Seric 		return (EX_TEMPFAIL);
5347963Seric 	else if (REPLYTYPE(r) == 2)
5357963Seric 		return (EX_OK);
5367964Seric 	else if (r == 550 || r == 551 || r == 553)
5377964Seric 		return (EX_NOUSER);
5387964Seric 	else if (r == 552 || r == 554)
5397964Seric 		return (EX_UNAVAILABLE);
54058008Seric 
54158008Seric #ifdef LOG
54258020Seric 	if (LogLevel > 1)
54358008Seric 	{
54467860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s",
54567860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
54658008Seric 	}
54758008Seric #endif
54858008Seric 
5497964Seric 	return (EX_PROTOCOL);
5504797Seric }
5514797Seric /*
55210175Seric **  SMTPDATA -- send the data and clean up the transaction.
5534684Seric **
5544684Seric **	Parameters:
5554865Seric **		m -- mailer being sent to.
5566980Seric **		e -- the envelope for this message.
5574684Seric **
5584684Seric **	Returns:
5594976Seric **		exit status corresponding to DATA command.
5604684Seric **
5614684Seric **	Side Effects:
5624865Seric **		none.
5634684Seric */
5644684Seric 
56563753Seric static jmp_buf	CtxDataTimeout;
56668433Seric static void	datatimeout();
56763753Seric 
56853740Seric smtpdata(m, mci, e)
5694865Seric 	struct mailer *m;
57054967Seric 	register MCI *mci;
5716980Seric 	register ENVELOPE *e;
5724684Seric {
5734684Seric 	register int r;
57463753Seric 	register EVENT *ev;
57563753Seric 	time_t timeout;
5764684Seric 
5774797Seric 	/*
5784797Seric 	**  Send the data.
57910175Seric 	**	First send the command and check that it is ok.
58010175Seric 	**	Then send the data.
58110175Seric 	**	Follow it up with a dot to terminate.
58210175Seric 	**	Finally get the results of the transaction.
5834797Seric 	*/
5844797Seric 
58510175Seric 	/* send the command and check ok to proceed */
58653751Seric 	smtpmessage("DATA", m, mci);
58761093Seric 	SmtpPhase = mci->mci_phase = "client DATA 354";
58853751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
58959285Seric 	r = reply(m, mci, e, TimeOuts.to_datainit, NULL);
5908005Seric 	if (r < 0 || REPLYTYPE(r) == 4)
59157990Seric 	{
59257990Seric 		smtpquit(m, mci, e);
5934797Seric 		return (EX_TEMPFAIL);
59457990Seric 	}
5957963Seric 	else if (r == 554)
59657990Seric 	{
59757990Seric 		smtprset(m, mci, e);
5987963Seric 		return (EX_UNAVAILABLE);
59957990Seric 	}
6007963Seric 	else if (r != 354)
60157990Seric 	{
60258008Seric #ifdef LOG
60358020Seric 		if (LogLevel > 1)
60458008Seric 		{
60567860Seric 			syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s",
60667860Seric 				e->e_id, mci->mci_host, SmtpReplyBuffer);
60758008Seric 		}
60858008Seric #endif
60957990Seric 		smtprset(m, mci, e);
6107964Seric 		return (EX_PROTOCOL);
61157990Seric 	}
61210175Seric 
61363757Seric 	/*
61463757Seric 	**  Set timeout around data writes.  Make it at least large
61563757Seric 	**  enough for DNS timeouts on all recipients plus some fudge
61663757Seric 	**  factor.  The main thing is that it should not be infinite.
61763757Seric 	*/
61863757Seric 
61963753Seric 	if (setjmp(CtxDataTimeout) != 0)
62063753Seric 	{
62163753Seric 		mci->mci_errno = errno;
62263753Seric 		mci->mci_exitstat = EX_TEMPFAIL;
62363753Seric 		mci->mci_state = MCIS_ERROR;
62463753Seric 		syserr("451 timeout writing message to %s", mci->mci_host);
62563753Seric 		smtpquit(m, mci, e);
62663753Seric 		return EX_TEMPFAIL;
62763753Seric 	}
62863753Seric 
62963787Seric 	timeout = e->e_msgsize / 16;
63063787Seric 	if (timeout < (time_t) 60)
63163787Seric 		timeout = (time_t) 60;
63263787Seric 	timeout += e->e_nrcpts * 90;
63363753Seric 	ev = setevent(timeout, datatimeout, 0);
63463753Seric 
63567546Seric 	/*
63667546Seric 	**  Output the actual message.
63767546Seric 	*/
63867546Seric 
63968228Seric 	(*e->e_puthdr)(mci, e->e_header, e);
64068228Seric 	(*e->e_putbody)(mci, e, NULL);
64110175Seric 
64267546Seric 	/*
64367546Seric 	**  Cleanup after sending message.
64467546Seric 	*/
64567546Seric 
64663753Seric 	clrevent(ev);
64763753Seric 
64864718Seric 	if (ferror(mci->mci_out))
64964718Seric 	{
65064718Seric 		/* error during processing -- don't send the dot */
65164718Seric 		mci->mci_errno = EIO;
65264718Seric 		mci->mci_exitstat = EX_IOERR;
65364718Seric 		mci->mci_state = MCIS_ERROR;
65464718Seric 		smtpquit(m, mci, e);
65564718Seric 		return EX_IOERR;
65664718Seric 	}
65764718Seric 
65810175Seric 	/* terminate the message */
65953740Seric 	fprintf(mci->mci_out, ".%s", m->m_eol);
66063753Seric 	if (TrafficLogFile != NULL)
66163753Seric 		fprintf(TrafficLogFile, "%05d >>> .\n", getpid());
66258120Seric 	if (Verbose)
66358151Seric 		nmessage(">>> .");
66410175Seric 
66510175Seric 	/* check for the results of the transaction */
66661093Seric 	SmtpPhase = mci->mci_phase = "client DATA 250";
66753751Seric 	setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase);
66859285Seric 	r = reply(m, mci, e, TimeOuts.to_datafinal, NULL);
66953751Seric 	if (r < 0)
67057990Seric 	{
67157990Seric 		smtpquit(m, mci, e);
6724797Seric 		return (EX_TEMPFAIL);
67357990Seric 	}
67453751Seric 	mci->mci_state = MCIS_OPEN;
67558917Seric 	e->e_statmsg = newstr(&SmtpReplyBuffer[4]);
67653751Seric 	if (REPLYTYPE(r) == 4)
67753751Seric 		return (EX_TEMPFAIL);
6787963Seric 	else if (r == 250)
6797963Seric 		return (EX_OK);
6807963Seric 	else if (r == 552 || r == 554)
6817963Seric 		return (EX_UNAVAILABLE);
68258008Seric #ifdef LOG
68358020Seric 	if (LogLevel > 1)
68458008Seric 	{
68567860Seric 		syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s",
68667860Seric 			e->e_id, mci->mci_host, SmtpReplyBuffer);
68758008Seric 	}
68858008Seric #endif
6897964Seric 	return (EX_PROTOCOL);
6904684Seric }
69163753Seric 
69263753Seric 
69368433Seric static void
69463753Seric datatimeout()
69563753Seric {
69663753Seric 	longjmp(CtxDataTimeout, 1);
69763753Seric }
6984684Seric /*
6994865Seric **  SMTPQUIT -- close the SMTP connection.
7004865Seric **
7014865Seric **	Parameters:
70215535Seric **		m -- a pointer to the mailer.
7034865Seric **
7044865Seric **	Returns:
7054865Seric **		none.
7064865Seric **
7074865Seric **	Side Effects:
7084865Seric **		sends the final protocol and closes the connection.
7094865Seric */
7104865Seric 
71153751Seric smtpquit(m, mci, e)
71253751Seric 	register MAILER *m;
71354967Seric 	register MCI *mci;
71453751Seric 	ENVELOPE *e;
7154865Seric {
71664822Seric 	bool oldSuprErrs = SuprErrs;
7174865Seric 
71864822Seric 	/*
71964822Seric 	**	Suppress errors here -- we may be processing a different
72064822Seric 	**	job when we do the quit connection, and we don't want the
72164822Seric 	**	new job to be penalized for something that isn't it's
72264822Seric 	**	problem.
72364822Seric 	*/
72464822Seric 
72564822Seric 	SuprErrs = TRUE;
72664822Seric 
72754967Seric 	/* send the quit message if we haven't gotten I/O error */
72853751Seric 	if (mci->mci_state != MCIS_ERROR)
7299391Seric 	{
73061093Seric 		SmtpPhase = "client QUIT";
73153751Seric 		smtpmessage("QUIT", m, mci);
73259285Seric 		(void) reply(m, mci, e, TimeOuts.to_quit, NULL);
73364822Seric 		SuprErrs = oldSuprErrs;
73453740Seric 		if (mci->mci_state == MCIS_CLOSED)
73564822Seric 		{
73664822Seric 			SuprErrs = oldSuprErrs;
73710159Seric 			return;
73864822Seric 		}
7399391Seric 	}
7409391Seric 
74152676Seric 	/* now actually close the connection and pick up the zombie */
74265194Seric 	(void) endmailer(mci, e, NULL);
74364822Seric 
74464822Seric 	SuprErrs = oldSuprErrs;
7454865Seric }
7464865Seric /*
74754967Seric **  SMTPRSET -- send a RSET (reset) command
74854967Seric */
74954967Seric 
75054967Seric smtprset(m, mci, e)
75154967Seric 	register MAILER *m;
75254967Seric 	register MCI *mci;
75354967Seric 	ENVELOPE *e;
75454967Seric {
75554967Seric 	int r;
75654967Seric 
75761093Seric 	SmtpPhase = "client RSET";
75854967Seric 	smtpmessage("RSET", m, mci);
75959285Seric 	r = reply(m, mci, e, TimeOuts.to_rset, NULL);
76057734Seric 	if (r < 0)
76157734Seric 		mci->mci_state = MCIS_ERROR;
76254967Seric 	else if (REPLYTYPE(r) == 2)
76357734Seric 	{
76457734Seric 		mci->mci_state = MCIS_OPEN;
76557734Seric 		return;
76657734Seric 	}
76757734Seric 	smtpquit(m, mci, e);
76854967Seric }
76954967Seric /*
77058867Seric **  SMTPPROBE -- check the connection state
77154967Seric */
77254967Seric 
77358867Seric smtpprobe(mci)
77454967Seric 	register MCI *mci;
77554967Seric {
77654967Seric 	int r;
77754967Seric 	MAILER *m = mci->mci_mailer;
77854967Seric 	extern ENVELOPE BlankEnvelope;
77954967Seric 	ENVELOPE *e = &BlankEnvelope;
78054967Seric 
78161093Seric 	SmtpPhase = "client probe";
78258867Seric 	smtpmessage("RSET", m, mci);
78359285Seric 	r = reply(m, mci, e, TimeOuts.to_miscshort, NULL);
78458061Seric 	if (r < 0 || REPLYTYPE(r) != 2)
78554967Seric 		smtpquit(m, mci, e);
78654967Seric 	return r;
78754967Seric }
78854967Seric /*
7894684Seric **  REPLY -- read arpanet reply
7904684Seric **
7914684Seric **	Parameters:
79210175Seric **		m -- the mailer we are reading the reply from.
79357379Seric **		mci -- the mailer connection info structure.
79457379Seric **		e -- the current envelope.
79557379Seric **		timeout -- the timeout for reads.
79659285Seric **		pfunc -- processing function for second and subsequent
79759285Seric **			lines of response -- if null, no special
79859285Seric **			processing is done.
7994684Seric **
8004684Seric **	Returns:
8014684Seric **		reply code it reads.
8024684Seric **
8034684Seric **	Side Effects:
8044684Seric **		flushes the mail file.
8054684Seric */
8064684Seric 
80759285Seric reply(m, mci, e, timeout, pfunc)
80853751Seric 	MAILER *m;
80954967Seric 	MCI *mci;
81053751Seric 	ENVELOPE *e;
81159285Seric 	time_t timeout;
81259285Seric 	void (*pfunc)();
8134684Seric {
81459014Seric 	register char *bufp;
81559014Seric 	register int r;
81659285Seric 	bool firstline = TRUE;
81758957Seric 	char junkbuf[MAXLINE];
81858957Seric 
81957379Seric 	if (mci->mci_out != NULL)
82057379Seric 		(void) fflush(mci->mci_out);
8214684Seric 
8227677Seric 	if (tTd(18, 1))
8234796Seric 		printf("reply\n");
8244796Seric 
8257356Seric 	/*
8267356Seric 	**  Read the input line, being careful not to hang.
8277356Seric 	*/
8287356Seric 
82959014Seric 	for (bufp = SmtpReplyBuffer;; bufp = junkbuf)
8304684Seric 	{
8317356Seric 		register char *p;
83253751Seric 		extern time_t curtime();
8334684Seric 
8347685Seric 		/* actually do the read */
83553751Seric 		if (e->e_xfp != NULL)
83653751Seric 			(void) fflush(e->e_xfp);	/* for debugging */
8377356Seric 
83810054Seric 		/* if we are in the process of closing just give the code */
83953740Seric 		if (mci->mci_state == MCIS_CLOSED)
84010054Seric 			return (SMTPCLOSING);
84110054Seric 
84258680Seric 		if (mci->mci_out != NULL)
84358680Seric 			fflush(mci->mci_out);
84458680Seric 
84510054Seric 		/* get the line from the other side */
84661093Seric 		p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase);
84753751Seric 		mci->mci_lastuse = curtime();
84853751Seric 
84910054Seric 		if (p == NULL)
85010131Seric 		{
85163753Seric 			bool oldholderrs;
85210148Seric 			extern char MsgBuf[];		/* err.c */
85310148Seric 
85421065Seric 			/* if the remote end closed early, fake an error */
85521065Seric 			if (errno == 0)
85621065Seric # ifdef ECONNRESET
85721065Seric 				errno = ECONNRESET;
85856795Seric # else /* ECONNRESET */
85921065Seric 				errno = EPIPE;
86056795Seric # endif /* ECONNRESET */
86121065Seric 
86257379Seric 			mci->mci_errno = errno;
86357642Seric 			mci->mci_exitstat = EX_TEMPFAIL;
86463753Seric 			oldholderrs = HoldErrs;
86563753Seric 			HoldErrs = TRUE;
86663753Seric 			usrerr("451 reply: read error from %s", mci->mci_host);
86763753Seric 
86810420Seric 			/* if debugging, pause so we can see state */
86910420Seric 			if (tTd(18, 100))
87010420Seric 				pause();
87154967Seric 			mci->mci_state = MCIS_ERROR;
87253751Seric 			smtpquit(m, mci, e);
87363753Seric #ifdef XDEBUG
87463753Seric 			{
87563753Seric 				char wbuf[MAXLINE];
87664082Seric 				char *p = wbuf;
87764082Seric 				if (e->e_to != NULL)
87864082Seric 				{
87964082Seric 					sprintf(p, "%s... ", e->e_to);
88064082Seric 					p += strlen(p);
88164082Seric 				}
88264082Seric 				sprintf(p, "reply(%s) during %s",
88364082Seric 					mci->mci_host, SmtpPhase);
88463753Seric 				checkfd012(wbuf);
88563753Seric 			}
88663753Seric #endif
88763753Seric 			HoldErrs = oldholderrs;
88810054Seric 			return (-1);
88910131Seric 		}
89058957Seric 		fixcrlf(bufp, TRUE);
89110054Seric 
89263753Seric 		/* EHLO failure is not a real error */
89363753Seric 		if (e->e_xfp != NULL && (bufp[0] == '4' ||
89463753Seric 		    (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0)))
89514900Seric 		{
89614900Seric 			/* serious error -- log the previous command */
89764071Seric 			if (SmtpNeedIntro)
89864071Seric 			{
89964071Seric 				/* inform user who we are chatting with */
90064071Seric 				fprintf(CurEnv->e_xfp,
90164071Seric 					"... while talking to %s:\n",
90264071Seric 					CurHostName);
90364071Seric 				SmtpNeedIntro = FALSE;
90464071Seric 			}
90559014Seric 			if (SmtpMsgBuffer[0] != '\0')
90659014Seric 				fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer);
90759014Seric 			SmtpMsgBuffer[0] = '\0';
90814900Seric 
90914900Seric 			/* now log the message as from the other side */
91058957Seric 			fprintf(e->e_xfp, "<<< %s\n", bufp);
91114900Seric 		}
91214900Seric 
91314900Seric 		/* display the input for verbose mode */
91458120Seric 		if (Verbose)
91559956Seric 			nmessage("050 %s", bufp);
9167356Seric 
91759285Seric 		/* process the line */
91867893Seric 		if (pfunc != NULL)
91967893Seric 			(*pfunc)(bufp, firstline, m, mci, e);
92059285Seric 
92159285Seric 		firstline = FALSE;
92259285Seric 
9237356Seric 		/* if continuation is required, we can go on */
92459014Seric 		if (bufp[3] == '-')
9254684Seric 			continue;
9267356Seric 
92759014Seric 		/* ignore improperly formated input */
92859014Seric 		if (!(isascii(bufp[0]) && isdigit(bufp[0])))
92959014Seric 			continue;
93059014Seric 
9317356Seric 		/* decode the reply code */
93259014Seric 		r = atoi(bufp);
9337356Seric 
9347356Seric 		/* extra semantics: 0xx codes are "informational" */
93559014Seric 		if (r >= 100)
93659014Seric 			break;
93759014Seric 	}
9387356Seric 
93959014Seric 	/*
94059014Seric 	**  Now look at SmtpReplyBuffer -- only care about the first
94159014Seric 	**  line of the response from here on out.
94259014Seric 	*/
94358061Seric 
94459014Seric 	/* save temporary failure messages for posterity */
94559014Seric 	if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0')
94659014Seric 		(void) strcpy(SmtpError, SmtpReplyBuffer);
9479391Seric 
94859014Seric 	/* reply code 421 is "Service Shutting Down" */
94959014Seric 	if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD)
95059014Seric 	{
95159014Seric 		/* send the quit protocol */
95259014Seric 		mci->mci_state = MCIS_SSD;
95359014Seric 		smtpquit(m, mci, e);
9544684Seric 	}
95559014Seric 
95659014Seric 	return (r);
9574684Seric }
9584796Seric /*
9594865Seric **  SMTPMESSAGE -- send message to server
9604796Seric **
9614796Seric **	Parameters:
9624796Seric **		f -- format
96310175Seric **		m -- the mailer to control formatting.
9644796Seric **		a, b, c -- parameters
9654796Seric **
9664796Seric **	Returns:
9674796Seric **		none.
9684796Seric **
9694796Seric **	Side Effects:
97053740Seric **		writes message to mci->mci_out.
9714796Seric */
9724796Seric 
9734865Seric /*VARARGS1*/
97457642Seric #ifdef __STDC__
97557642Seric smtpmessage(char *f, MAILER *m, MCI *mci, ...)
97657642Seric #else
97757642Seric smtpmessage(f, m, mci, va_alist)
9784796Seric 	char *f;
97910175Seric 	MAILER *m;
98054967Seric 	MCI *mci;
98157642Seric 	va_dcl
98257642Seric #endif
9834796Seric {
98456852Seric 	VA_LOCAL_DECL
98556852Seric 
98657135Seric 	VA_START(mci);
98756852Seric 	(void) vsprintf(SmtpMsgBuffer, f, ap);
98856852Seric 	VA_END;
98958680Seric 
99058120Seric 	if (tTd(18, 1) || Verbose)
99158151Seric 		nmessage(">>> %s", SmtpMsgBuffer);
99263753Seric 	if (TrafficLogFile != NULL)
99363753Seric 		fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer);
99453740Seric 	if (mci->mci_out != NULL)
99558680Seric 	{
99653740Seric 		fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer,
99754967Seric 			m == NULL ? "\r\n" : m->m_eol);
99858680Seric 	}
99959149Seric 	else if (tTd(18, 1))
100058725Seric 	{
100159149Seric 		printf("smtpmessage: NULL mci_out\n");
100258725Seric 	}
10034796Seric }
10045182Seric 
100556795Seric # endif /* SMTP */
1006