122704Sdist /*
268839Seric  * Copyright (c) 1983, 1995 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822704Sdist 
922704Sdist #ifndef lint
10*69837Seric static char sccsid[] = "@(#)envelope.c	8.67 (Berkeley) 06/10/95";
1133729Sbostic #endif /* not lint */
1222704Sdist 
1358332Seric #include "sendmail.h"
149536Seric 
159536Seric /*
169536Seric **  NEWENVELOPE -- allocate a new envelope
179536Seric **
189536Seric **	Supports inheritance.
199536Seric **
209536Seric **	Parameters:
219536Seric **		e -- the new envelope to fill in.
2258179Seric **		parent -- the envelope to be the parent of e.
239536Seric **
249536Seric **	Returns:
259536Seric **		e.
269536Seric **
279536Seric **	Side Effects:
289536Seric **		none.
299536Seric */
309536Seric 
319536Seric ENVELOPE *
newenvelope(e,parent)3258179Seric newenvelope(e, parent)
339536Seric 	register ENVELOPE *e;
3458179Seric 	register ENVELOPE *parent;
359536Seric {
3658179Seric 	if (e == parent && e->e_parent != NULL)
379536Seric 		parent = e->e_parent;
3825611Seric 	clearenvelope(e, TRUE);
3924944Seric 	if (e == CurEnv)
4024944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4124944Seric 	else
4224944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
439536Seric 	e->e_parent = parent;
449536Seric 	e->e_ctime = curtime();
4556215Seric 	if (parent != NULL)
4656215Seric 		e->e_msgpriority = parent->e_msgsize;
479536Seric 	e->e_puthdr = putheader;
489536Seric 	e->e_putbody = putbody;
499536Seric 	if (CurEnv->e_xfp != NULL)
509536Seric 		(void) fflush(CurEnv->e_xfp);
519536Seric 
529536Seric 	return (e);
539536Seric }
549536Seric /*
559536Seric **  DROPENVELOPE -- deallocate an envelope.
569536Seric **
579536Seric **	Parameters:
589536Seric **		e -- the envelope to deallocate.
599536Seric **
609536Seric **	Returns:
619536Seric **		none.
629536Seric **
639536Seric **	Side Effects:
649536Seric **		housekeeping necessary to dispose of an envelope.
659536Seric **		Unlocks this queue file.
669536Seric */
679536Seric 
6860494Seric void
dropenvelope(e)699536Seric dropenvelope(e)
709536Seric 	register ENVELOPE *e;
719536Seric {
729536Seric 	bool queueit = FALSE;
7368498Seric 	bool failure_return = FALSE;
7468498Seric 	bool success_return = FALSE;
759536Seric 	register ADDRESS *q;
7657943Seric 	char *id = e->e_id;
7763753Seric 	char buf[MAXLINE];
789536Seric 
799536Seric 	if (tTd(50, 1))
809536Seric 	{
8168567Seric 		extern void printenvflags();
8268567Seric 
8358680Seric 		printf("dropenvelope %x: id=", e);
849536Seric 		xputs(e->e_id);
8568567Seric 		printf(", flags=");
8668567Seric 		printenvflags(e);
8763753Seric 		if (tTd(50, 10))
8863753Seric 		{
8963753Seric 			printf("sendq=");
9063753Seric 			printaddr(e->e_sendqueue, TRUE);
9163753Seric 		}
929536Seric 	}
9357943Seric 
9458680Seric 	/* we must have an id to remove disk files */
9557943Seric 	if (id == NULL)
9658680Seric 		return;
9757943Seric 
989536Seric #ifdef LOG
9965089Seric 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
10065089Seric 		logsender(e, NULL);
10158020Seric 	if (LogLevel > 84)
10265089Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
10357943Seric 				  id, e->e_flags, getpid());
10456795Seric #endif /* LOG */
10565089Seric 	e->e_flags &= ~EF_LOGSENDER;
1069536Seric 
10763753Seric 	/* post statistics */
10863753Seric 	poststats(StatFile);
10963753Seric 
1109536Seric 	/*
1119536Seric 	**  Extract state information from dregs of send list.
1129536Seric 	*/
1139536Seric 
11464743Seric 	e->e_flags &= ~EF_QUEUERUN;
1159536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1169536Seric 	{
1179536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1189536Seric 			queueit = TRUE;
11968498Seric 
12068498Seric 		/* see if a notification is needed */
12168564Seric 		if (bitset(QBADADDR, q->q_flags) &&
12268498Seric 		    bitset(QPINGONFAILURE, q->q_flags))
12363839Seric 		{
12468498Seric 			failure_return = TRUE;
12568498Seric 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
12663839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
12768498Seric 						  &e->e_errorqueue, 0, e);
12863839Seric 		}
12968603Seric 		else if (bitset(QPINGONSUCCESS, q->q_flags) &&
13068603Seric 			 ((bitset(QSENT, q->q_flags) &&
13168603Seric 			   bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
13268867Seric 			  bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags)))
13368498Seric 		{
13468498Seric 			success_return = TRUE;
13568498Seric 		}
1369536Seric 	}
1379536Seric 
13868559Seric 	if (e->e_class < 0)
13968559Seric 		e->e_flags |= EF_NO_BODY_RETN;
14068559Seric 
1419536Seric 	/*
14263753Seric 	**  See if the message timed out.
14363753Seric 	*/
14463753Seric 
14563753Seric 	if (!queueit)
14663753Seric 		/* nothing to do */ ;
14768498Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
14863753Seric 	{
14963839Seric 		(void) sprintf(buf, "Cannot send message for %s",
15068498Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
15163839Seric 		if (e->e_message != NULL)
15263839Seric 			free(e->e_message);
15363839Seric 		e->e_message = newstr(buf);
15463839Seric 		message(buf);
15563839Seric 		e->e_flags |= EF_CLRQUEUE;
15668498Seric 		failure_return = TRUE;
15763787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
15868498Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
15963787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
16063787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16163787Seric 		{
16263787Seric 			if (bitset(QQUEUEUP, q->q_flags))
16368859Seric 			{
16463787Seric 				q->q_flags |= QBADADDR;
16568859Seric 				q->q_status = "4.4.7";
16668859Seric 			}
16763787Seric 		}
16863753Seric 	}
16968498Seric 	else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
17068498Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass])
17163753Seric 	{
17268498Seric 		bool delay_return = FALSE;
17368498Seric 
17468498Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
17568498Seric 		{
17668498Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
17768498Seric 			    bitset(QPINGONDELAY, q->q_flags))
17868498Seric 			{
17968867Seric 				q->q_flags |= QDELAYED;
18068498Seric 				delay_return = TRUE;
18168498Seric 			}
18268498Seric 		}
18368498Seric 		if (delay_return &&
18468498Seric 		    !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
18563753Seric 		    e->e_class >= 0 &&
18668498Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0 &&
18768498Seric 		    strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
18868706Seric 		    (strlen(e->e_from.q_paddr) <= (SIZE_T) 8 ||
18968498Seric 		     strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
19063753Seric 		{
19163753Seric 			(void) sprintf(buf,
19268498Seric 				"Warning: cannot send message for %s",
19368498Seric 				pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
19463753Seric 			if (e->e_message != NULL)
19563753Seric 				free(e->e_message);
19663753Seric 			e->e_message = newstr(buf);
19763753Seric 			message(buf);
19863839Seric 			e->e_flags |= EF_WARNING;
19968498Seric 			failure_return = TRUE;
20063753Seric 		}
20163753Seric 		fprintf(e->e_xfp,
20263753Seric 			"Warning: message still undelivered after %s\n",
20368498Seric 			pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
20463753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
20568498Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
20668498Seric 	}
20768498Seric 
20868498Seric 	if (tTd(50, 2))
20968498Seric 		printf("failure_return=%d success_return=%d queueit=%d\n",
21068498Seric 			failure_return, success_return, queueit);
21168498Seric 
21268498Seric 	/*
21368498Seric 	**  If we had some fatal error, but no addresses are marked as
21468498Seric 	**  bad, mark them _all_ as bad.
21568498Seric 	*/
21668498Seric 
21768498Seric 	if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
21868498Seric 	{
21968498Seric 		failure_return = TRUE;
22068019Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
22168019Seric 		{
22268498Seric 			if (!bitset(QDONTSEND, q->q_flags))
22368498Seric 				q->q_flags |= QBADADDR;
22468019Seric 		}
22568019Seric 	}
22668019Seric 
22768019Seric 	/*
2289536Seric 	**  Send back return receipts as requested.
2299536Seric 	*/
2309536Seric 
23168498Seric /*
23266783Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
23366783Seric 	    && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
23468498Seric */
23568498Seric 	if (e->e_receiptto == NULL)
23668498Seric 		e->e_receiptto = e->e_from.q_paddr;
23768498Seric 	if (success_return && !failure_return &&
23868498Seric 	    !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
23968498Seric 	    strcmp(e->e_receiptto, "<>") != 0)
2409536Seric 	{
24110844Seric 		auto ADDRESS *rlist = NULL;
2429536Seric 
24368498Seric 		e->e_flags |= EF_SENDRECEIPT;
24468498Seric 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e);
24568559Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
2469536Seric 	}
24768498Seric 	e->e_flags &= ~EF_SENDRECEIPT;
2489536Seric 
2499536Seric 	/*
2509536Seric 	**  Arrange to send error messages if there are fatal errors.
2519536Seric 	*/
2529536Seric 
25368498Seric 	if (failure_return && e->e_errormode != EM_QUIET)
25468559Seric 		savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags));
2559536Seric 
2569536Seric 	/*
25763849Seric 	**  Arrange to send warning messages to postmaster as requested.
25863849Seric 	*/
25963849Seric 
26068802Seric 	if ((failure_return || bitset(EF_PM_NOTIFY, e->e_flags)) &&
26168802Seric 	    PostMasterCopy != NULL &&
26264363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
26363849Seric 	{
26463849Seric 		auto ADDRESS *rlist = NULL;
26563849Seric 
26668498Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e);
26763849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
26863849Seric 	}
26963849Seric 
27063849Seric 	/*
2719536Seric 	**  Instantiate or deinstantiate the queue.
2729536Seric 	*/
2739536Seric 
2749536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2759536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2769536Seric 	{
27764307Seric 		if (tTd(50, 1))
27868567Seric 		{
27968567Seric 			extern void printenvflags();
28068567Seric 
28168567Seric 			printf("\n===== Dropping [dq]f%s... queueit=%d, e_flags=",
28268567Seric 				e->e_id, queueit);
28368567Seric 			printenvflags(e);
28468567Seric 		}
28568564Seric 		xunlink(queuename(e, 'd'));
2869536Seric 		xunlink(queuename(e, 'q'));
28763839Seric 
28863839Seric #ifdef LOG
28963839Seric 		if (LogLevel > 10)
29063839Seric 			syslog(LOG_INFO, "%s: done", id);
29163839Seric #endif
2929536Seric 	}
2939536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
29410754Seric 	{
29510754Seric #ifdef QUEUE
29664307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
29756795Seric #else /* QUEUE */
29858151Seric 		syserr("554 dropenvelope: queueup");
29956795Seric #endif /* QUEUE */
30010754Seric 	}
3019536Seric 
3029536Seric 	/* now unlock the job */
30310196Seric 	closexscript(e);
3049536Seric 	unlockqueue(e);
3059536Seric 
3069536Seric 	/* make sure that this envelope is marked unused */
30724944Seric 	if (e->e_dfp != NULL)
30868564Seric 		(void) xfclose(e->e_dfp, "dropenvelope df", e->e_id);
30910196Seric 	e->e_dfp = NULL;
31068564Seric 	e->e_id = NULL;
31168564Seric 	e->e_flags &= ~EF_HAS_DF;
3129536Seric }
3139536Seric /*
3149536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
3159536Seric **
3169536Seric **	This is normally used by a child process to get a clean
3179536Seric **	envelope without disturbing the parent.
3189536Seric **
3199536Seric **	Parameters:
3209536Seric **		e -- the envelope to clear.
32125611Seric **		fullclear - if set, the current envelope is total
32225611Seric **			garbage and should be ignored; otherwise,
32325611Seric **			release any resources it may indicate.
3249536Seric **
3259536Seric **	Returns:
3269536Seric **		none.
3279536Seric **
3289536Seric **	Side Effects:
3299536Seric **		Closes files associated with the envelope.
3309536Seric **		Marks the envelope as unallocated.
3319536Seric */
3329536Seric 
33360494Seric void
clearenvelope(e,fullclear)33425611Seric clearenvelope(e, fullclear)
3359536Seric 	register ENVELOPE *e;
33625611Seric 	bool fullclear;
3379536Seric {
33825514Seric 	register HDR *bh;
33925514Seric 	register HDR **nhp;
34025514Seric 	extern ENVELOPE BlankEnvelope;
34125514Seric 
34225611Seric 	if (!fullclear)
34325611Seric 	{
34425611Seric 		/* clear out any file information */
34525611Seric 		if (e->e_xfp != NULL)
34658680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
34725611Seric 		if (e->e_dfp != NULL)
34868564Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_id);
34958680Seric 		e->e_xfp = e->e_dfp = NULL;
35025611Seric 	}
3519536Seric 
35224961Seric 	/* now clear out the data */
35324965Seric 	STRUCTCOPY(BlankEnvelope, *e);
35459698Seric 	if (Verbose)
35559698Seric 		e->e_sendmode = SM_DELIVER;
35625514Seric 	bh = BlankEnvelope.e_header;
35725514Seric 	nhp = &e->e_header;
35825514Seric 	while (bh != NULL)
35925514Seric 	{
36025514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
36125514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
36225514Seric 		bh = bh->h_link;
36325514Seric 		nhp = &(*nhp)->h_link;
36425514Seric 	}
3659536Seric }
3669536Seric /*
3679536Seric **  INITSYS -- initialize instantiation of system
3689536Seric **
3699536Seric **	In Daemon mode, this is done in the child.
3709536Seric **
3719536Seric **	Parameters:
3729536Seric **		none.
3739536Seric **
3749536Seric **	Returns:
3759536Seric **		none.
3769536Seric **
3779536Seric **	Side Effects:
3789536Seric **		Initializes the system macros, some global variables,
3799536Seric **		etc.  In particular, the current time in various
3809536Seric **		forms is set.
3819536Seric */
3829536Seric 
38360494Seric void
initsys(e)38455012Seric initsys(e)
38555012Seric 	register ENVELOPE *e;
3869536Seric {
38764768Seric 	char cbuf[5];				/* holds hop count */
38864768Seric 	char pbuf[10];				/* holds pid */
38922963Smiriam #ifdef TTYNAME
39059304Seric 	static char ybuf[60];			/* holds tty id */
3919536Seric 	register char *p;
39256795Seric #endif /* TTYNAME */
3939536Seric 	extern char *ttyname();
39460494Seric 	extern void settime();
3959536Seric 
3969536Seric 	/*
3979536Seric 	**  Give this envelope a reality.
3989536Seric 	**	I.e., an id, a transcript, and a creation time.
3999536Seric 	*/
4009536Seric 
40155012Seric 	openxscript(e);
40255012Seric 	e->e_ctime = curtime();
4039536Seric 
4049536Seric 	/*
4059536Seric 	**  Set OutChannel to something useful if stdout isn't it.
4069536Seric 	**	This arranges that any extra stuff the mailer produces
4079536Seric 	**	gets sent back to the user on error (because it is
4089536Seric 	**	tucked away in the transcript).
4099536Seric 	*/
4109536Seric 
41164760Seric 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
41258737Seric 	    e->e_xfp != NULL)
41355012Seric 		OutChannel = e->e_xfp;
4149536Seric 
4159536Seric 	/*
4169536Seric 	**  Set up some basic system macros.
4179536Seric 	*/
4189536Seric 
4199536Seric 	/* process id */
4209536Seric 	(void) sprintf(pbuf, "%d", getpid());
42164768Seric 	define('p', newstr(pbuf), e);
4229536Seric 
4239536Seric 	/* hop count */
42455012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
42564768Seric 	define('c', newstr(cbuf), e);
4269536Seric 
4279536Seric 	/* time as integer, unix time, arpa time */
42855012Seric 	settime(e);
4299536Seric 
43017472Seric #ifdef TTYNAME
4319536Seric 	/* tty name */
43255012Seric 	if (macvalue('y', e) == NULL)
4339536Seric 	{
4349536Seric 		p = ttyname(2);
4359536Seric 		if (p != NULL)
4369536Seric 		{
43756795Seric 			if (strrchr(p, '/') != NULL)
43856795Seric 				p = strrchr(p, '/') + 1;
4399536Seric 			(void) strcpy(ybuf, p);
44055012Seric 			define('y', ybuf, e);
4419536Seric 		}
4429536Seric 	}
44356795Seric #endif /* TTYNAME */
4449536Seric }
4459536Seric /*
44611932Seric **  SETTIME -- set the current time.
44711932Seric **
44811932Seric **	Parameters:
44911932Seric **		none.
45011932Seric **
45111932Seric **	Returns:
45211932Seric **		none.
45311932Seric **
45411932Seric **	Side Effects:
45511932Seric **		Sets the various time macros -- $a, $b, $d, $t.
45611932Seric */
45711932Seric 
45860494Seric void
settime(e)45955012Seric settime(e)
46055012Seric 	register ENVELOPE *e;
46111932Seric {
46211932Seric 	register char *p;
46311932Seric 	auto time_t now;
46464768Seric 	char tbuf[20];				/* holds "current" time */
46564768Seric 	char dbuf[30];				/* holds ctime(tbuf) */
46611932Seric 	register struct tm *tm;
46711932Seric 	extern char *arpadate();
46811932Seric 	extern struct tm *gmtime();
46911932Seric 
47011932Seric 	now = curtime();
47111932Seric 	tm = gmtime(&now);
47257014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
47357014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
47464768Seric 	define('t', newstr(tbuf), e);
47511932Seric 	(void) strcpy(dbuf, ctime(&now));
47658131Seric 	p = strchr(dbuf, '\n');
47758131Seric 	if (p != NULL)
47858131Seric 		*p = '\0';
47964768Seric 	define('d', newstr(dbuf), e);
48064086Seric 	p = arpadate(dbuf);
48164086Seric 	p = newstr(p);
48255012Seric 	if (macvalue('a', e) == NULL)
48355012Seric 		define('a', p, e);
48455012Seric 	define('b', p, e);
48511932Seric }
48611932Seric /*
4879536Seric **  OPENXSCRIPT -- Open transcript file
4889536Seric **
4899536Seric **	Creates a transcript file for possible eventual mailing or
4909536Seric **	sending back.
4919536Seric **
4929536Seric **	Parameters:
4939536Seric **		e -- the envelope to create the transcript in/for.
4949536Seric **
4959536Seric **	Returns:
4969536Seric **		none
4979536Seric **
4989536Seric **	Side Effects:
4999536Seric **		Creates the transcript file.
5009536Seric */
5019536Seric 
50258803Seric #ifndef O_APPEND
50358803Seric #define O_APPEND	0
50458803Seric #endif
50558803Seric 
50660494Seric void
openxscript(e)5079536Seric openxscript(e)
5089536Seric 	register ENVELOPE *e;
5099536Seric {
5109536Seric 	register char *p;
51140933Srick 	int fd;
5129536Seric 
5139536Seric 	if (e->e_xfp != NULL)
5149536Seric 		return;
5159536Seric 	p = queuename(e, 'x');
51658803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
51740933Srick 	if (fd < 0)
51863753Seric 	{
51963753Seric 		syserr("Can't create transcript file %s", p);
52063753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
52163753Seric 		if (fd < 0)
52263753Seric 			syserr("!Can't open /dev/null");
52363753Seric 	}
52468352Seric 	e->e_xfp = fdopen(fd, "a");
52564724Seric 	if (e->e_xfp == NULL)
52664724Seric 		syserr("!Can't create transcript stream %s", p);
52768498Seric #ifdef HASSETVBUF
52868498Seric 	setvbuf(e->e_xfp, NULL, _IOLBF, 0);
52968498Seric #else
53068498Seric 	setlinebuf(e->e_xfp);
53168498Seric #endif
53264743Seric 	if (tTd(46, 9))
53364743Seric 	{
53464743Seric 		printf("openxscript(%s):\n  ", p);
53564743Seric 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
53664743Seric 	}
5379536Seric }
5389536Seric /*
53910196Seric **  CLOSEXSCRIPT -- close the transcript file.
54010196Seric **
54110196Seric **	Parameters:
54210196Seric **		e -- the envelope containing the transcript to close.
54310196Seric **
54410196Seric **	Returns:
54510196Seric **		none.
54610196Seric **
54710196Seric **	Side Effects:
54810196Seric **		none.
54910196Seric */
55010196Seric 
55160494Seric void
closexscript(e)55210196Seric closexscript(e)
55310196Seric 	register ENVELOPE *e;
55410196Seric {
55510196Seric 	if (e->e_xfp == NULL)
55610196Seric 		return;
55758680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
55810196Seric 	e->e_xfp = NULL;
55910196Seric }
56010196Seric /*
5619536Seric **  SETSENDER -- set the person who this message is from
5629536Seric **
5639536Seric **	Under certain circumstances allow the user to say who
5649536Seric **	s/he is (using -f or -r).  These are:
5659536Seric **	1.  The user's uid is zero (root).
5669536Seric **	2.  The user's login name is in an approved list (typically
5679536Seric **	    from a network server).
5689536Seric **	3.  The address the user is trying to claim has a
5699536Seric **	    "!" character in it (since #2 doesn't do it for
5709536Seric **	    us if we are dialing out for UUCP).
5719536Seric **	A better check to replace #3 would be if the
5729536Seric **	effective uid is "UUCP" -- this would require me
5739536Seric **	to rewrite getpwent to "grab" uucp as it went by,
5749536Seric **	make getname more nasty, do another passwd file
5759536Seric **	scan, or compile the UID of "UUCP" into the code,
5769536Seric **	all of which are reprehensible.
5779536Seric **
5789536Seric **	Assuming all of these fail, we figure out something
5799536Seric **	ourselves.
5809536Seric **
5819536Seric **	Parameters:
5829536Seric **		from -- the person we would like to believe this message
5839536Seric **			is from, as specified on the command line.
58453182Seric **		e -- the envelope in which we would like the sender set.
58558333Seric **		delimptr -- if non-NULL, set to the location of the
58658333Seric **			trailing delimiter.
58758704Seric **		internal -- set if this address is coming from an internal
58858704Seric **			source such as an owner alias.
5899536Seric **
5909536Seric **	Returns:
59158704Seric **		none.
5929536Seric **
5939536Seric **	Side Effects:
5949536Seric **		sets sendmail's notion of who the from person is.
5959536Seric */
5969536Seric 
59760494Seric void
setsender(from,e,delimptr,internal)59858704Seric setsender(from, e, delimptr, internal)
5999536Seric 	char *from;
60053182Seric 	register ENVELOPE *e;
60158333Seric 	char **delimptr;
60258704Seric 	bool internal;
6039536Seric {
6049536Seric 	register char **pvp;
6059536Seric 	char *realname = NULL;
60618665Seric 	register struct passwd *pw;
60758727Seric 	char delimchar;
60864147Seric 	char *bp;
60964147Seric 	char buf[MAXNAME + 2];
61016913Seric 	char pvpbuf[PSBUFSIZE];
6119536Seric 	extern char *FullName;
6129536Seric 
6139536Seric 	if (tTd(45, 1))
61414786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
6159536Seric 
6169536Seric 	/*
6179536Seric 	**  Figure out the real user executing us.
6189536Seric 	**	Username can return errno != 0 on non-errors.
6199536Seric 	*/
6209536Seric 
62165580Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
62265983Seric 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
6239536Seric 		realname = from;
6249536Seric 	if (realname == NULL || realname[0] == '\0')
6259536Seric 		realname = username();
6269536Seric 
62759027Seric 	if (ConfigLevel < 2)
62859027Seric 		SuprErrs = TRUE;
62959027Seric 
63058727Seric 	delimchar = internal ? '\0' : ' ';
63164793Seric 	e->e_from.q_flags = QBADADDR;
63258333Seric 	if (from == NULL ||
63364284Seric 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
63464793Seric 		      delimchar, delimptr, e) == NULL ||
63564793Seric 	    bitset(QBADADDR, e->e_from.q_flags) ||
63664793Seric 	    e->e_from.q_mailer == ProgMailer ||
63764793Seric 	    e->e_from.q_mailer == FileMailer ||
63864793Seric 	    e->e_from.q_mailer == InclMailer)
6399536Seric 	{
64021750Seric 		/* log garbage addresses for traceback */
64155173Seric # ifdef LOG
64258020Seric 		if (from != NULL && LogLevel > 2)
64321750Seric 		{
64458951Seric 			char *p;
64558951Seric 			char ebuf[MAXNAME * 2 + 2];
64655173Seric 
64758951Seric 			p = macvalue('_', e);
64858951Seric 			if (p == NULL)
64958951Seric 			{
65058951Seric 				char *host = RealHostName;
65158951Seric 				if (host == NULL)
65258951Seric 					host = MyHostName;
65358951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
65458951Seric 				p = ebuf;
65558951Seric 			}
65655173Seric 			syslog(LOG_NOTICE,
65764793Seric 				"setsender: %s: invalid or unparseable, received from %s",
65865015Seric 				shortenstring(from, 83), p);
65955173Seric 		}
66056795Seric # endif /* LOG */
66157589Seric 		if (from != NULL)
66264793Seric 		{
66364793Seric 			if (!bitset(QBADADDR, e->e_from.q_flags))
66464793Seric 			{
66564793Seric 				/* it was a bogus mailer in the from addr */
66668859Seric 				e->e_status = "5.1.7";
66764793Seric 				usrerr("553 Invalid sender address");
66864793Seric 			}
66957589Seric 			SuprErrs = TRUE;
67064793Seric 		}
67157589Seric 		if (from == realname ||
67264284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
67364284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
67424944Seric 		{
67564793Seric 			char nbuf[100];
67664793Seric 
67757589Seric 			SuprErrs = TRUE;
67868529Seric 			expand("\201n", nbuf, sizeof nbuf, e);
67964793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
68064793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
68164793Seric 			    parseaddr(from = "postmaster", &e->e_from,
68264793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
68358151Seric 				syserr("553 setsender: can't even parse postmaster!");
68424944Seric 		}
6859536Seric 	}
6869536Seric 	else
6879536Seric 		FromFlag = TRUE;
68853182Seric 	e->e_from.q_flags |= QDONTSEND;
68957731Seric 	if (tTd(45, 5))
69057731Seric 	{
69157731Seric 		printf("setsender: QDONTSEND ");
69257731Seric 		printaddr(&e->e_from, FALSE);
69357731Seric 	}
6949536Seric 	SuprErrs = FALSE;
6959536Seric 
69669713Seric # if USERDB
69768498Seric 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
69868460Seric 	{
69953736Seric 		register char *p;
70053736Seric 		extern char *udbsender();
70117472Seric 
70268498Seric 		p = udbsender(e->e_from.q_user);
70368498Seric 		if (p != NULL)
70468498Seric 			from = p;
70568498Seric 	}
70668498Seric # endif /* USERDB */
70768498Seric 
70868498Seric 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
70968498Seric 	{
71058704Seric 		if (!internal)
71158704Seric 		{
71268498Seric 			/* if the user already given fullname don't redefine */
71358704Seric 			if (FullName == NULL)
71458704Seric 				FullName = macvalue('x', e);
71558704Seric 			if (FullName != NULL && FullName[0] == '\0')
71658704Seric 				FullName = NULL;
7179536Seric 		}
71853736Seric 
71968693Seric 		if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
72053736Seric 		{
72153736Seric 			/*
72253736Seric 			**  Process passwd file entry.
72353736Seric 			*/
72453736Seric 
72553736Seric 			/* extract home directory */
72665822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
72765822Seric 				e->e_from.q_home = newstr("");
72865822Seric 			else
72965822Seric 				e->e_from.q_home = newstr(pw->pw_dir);
73053736Seric 			define('z', e->e_from.q_home, e);
73153736Seric 
73253736Seric 			/* extract user and group id */
73353736Seric 			e->e_from.q_uid = pw->pw_uid;
73453736Seric 			e->e_from.q_gid = pw->pw_gid;
73565023Seric 			e->e_from.q_flags |= QGOODUID;
73653736Seric 
73753736Seric 			/* extract full name from passwd file */
73853736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
73958704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
74058704Seric 			    !internal)
74153736Seric 			{
74265015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
74353736Seric 				if (buf[0] != '\0')
74453736Seric 					FullName = newstr(buf);
74553736Seric 			}
74653736Seric 		}
74758704Seric 		if (FullName != NULL && !internal)
74853182Seric 			define('x', FullName, e);
7499536Seric 	}
75065580Seric 	else if (!internal && OpMode != MD_DAEMON)
75111625Seric 	{
75253182Seric 		if (e->e_from.q_home == NULL)
75365822Seric 		{
75453182Seric 			e->e_from.q_home = getenv("HOME");
75566049Seric 			if (e->e_from.q_home != NULL &&
75666049Seric 			    strcmp(e->e_from.q_home, "/") == 0)
75765822Seric 				e->e_from.q_home++;
75865822Seric 		}
75963787Seric 		e->e_from.q_uid = RealUid;
76063787Seric 		e->e_from.q_gid = RealGid;
76165023Seric 		e->e_from.q_flags |= QGOODUID;
76211625Seric 	}
76311625Seric 
7649536Seric 	/*
7659536Seric 	**  Rewrite the from person to dispose of possible implicit
7669536Seric 	**	links in the net.
7679536Seric 	*/
7689536Seric 
76968711Seric 	pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, NULL);
7709536Seric 	if (pvp == NULL)
7719536Seric 	{
77258403Seric 		/* don't need to give error -- prescan did that already */
77336233Skarels # ifdef LOG
77458020Seric 		if (LogLevel > 2)
77536233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
77636233Skarels # endif
7779536Seric 		finis();
7789536Seric 	}
77965071Seric 	(void) rewrite(pvp, 3, 0, e);
78065071Seric 	(void) rewrite(pvp, 1, 0, e);
78165071Seric 	(void) rewrite(pvp, 4, 0, e);
78264147Seric 	bp = buf + 1;
78364147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
78468498Seric 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
78564147Seric 	{
78664147Seric 		/* heuristic: route-addr: add angle brackets */
78764147Seric 		strcat(bp, ">");
78864147Seric 		*--bp = '<';
78964147Seric 	}
79064147Seric 	e->e_sender = newstr(bp);
79158704Seric 	define('f', e->e_sender, e);
7929536Seric 
7939536Seric 	/* save the domain spec if this mailer wants it */
79465584Seric 	if (e->e_from.q_mailer != NULL &&
79553182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
7969536Seric 	{
79768736Seric 		char **lastat;
7989536Seric 		extern char **copyplist();
7999536Seric 
80068736Seric 		/* get rid of any pesky angle brackets */
80168736Seric 		(void) rewrite(pvp, 3, 0, e);
80268736Seric 		(void) rewrite(pvp, 1, 0, e);
80368736Seric 		(void) rewrite(pvp, 4, 0, e);
80468736Seric 
80568736Seric 		/* strip off to the last "@" sign */
80668736Seric 		for (lastat = NULL; *pvp != NULL; pvp++)
80768736Seric 			if (strcmp(*pvp, "@") == 0)
80868736Seric 				lastat = pvp;
80968736Seric 		if (lastat != NULL)
81068736Seric 		{
81168736Seric 			e->e_fromdomain = copyplist(lastat, TRUE);
81268736Seric 			if (tTd(45, 3))
81368736Seric 			{
81468736Seric 				printf("Saving from domain: ");
81568736Seric 				printav(e->e_fromdomain);
81668736Seric 			}
81768736Seric 		}
8189536Seric 	}
8199536Seric }
82068567Seric /*
82168567Seric **  PRINTENVFLAGS -- print envelope flags for debugging
82268567Seric **
82368567Seric **	Parameters:
82468567Seric **		e -- the envelope with the flags to be printed.
82568567Seric **
82668567Seric **	Returns:
82768567Seric **		none.
82868567Seric */
82968567Seric 
83068567Seric struct eflags
83168567Seric {
83268567Seric 	char	*ef_name;
83368567Seric 	u_long	ef_bit;
83468567Seric };
83568567Seric 
83668567Seric struct eflags	EnvelopeFlags[] =
83768567Seric {
83868567Seric 	"OLDSTYLE",	EF_OLDSTYLE,
83968567Seric 	"INQUEUE",	EF_INQUEUE,
84068567Seric 	"NO_BODY_RETN",	EF_NO_BODY_RETN,
84168567Seric 	"CLRQUEUE",	EF_CLRQUEUE,
84268567Seric 	"SENDRECEIPT",	EF_SENDRECEIPT,
84368567Seric 	"FATALERRS",	EF_FATALERRS,
84468567Seric 	"KEEPQUEUE",	EF_KEEPQUEUE,
84568567Seric 	"RESPONSE",	EF_RESPONSE,
84668567Seric 	"RESENT",	EF_RESENT,
84768567Seric 	"VRFYONLY",	EF_VRFYONLY,
84868567Seric 	"WARNING",	EF_WARNING,
84968567Seric 	"QUEUERUN",	EF_QUEUERUN,
85068567Seric 	"GLOBALERRS",	EF_GLOBALERRS,
85168567Seric 	"PM_NOTIFY",	EF_PM_NOTIFY,
85268567Seric 	"METOO",	EF_METOO,
85368567Seric 	"LOGSENDER",	EF_LOGSENDER,
85468567Seric 	"NORECEIPT",	EF_NORECEIPT,
85568567Seric 	"HAS8BIT",	EF_HAS8BIT,
85668567Seric 	"NL_NOT_EOL",	EF_NL_NOT_EOL,
85768567Seric 	"CRLF_NOT_EOL",	EF_CRLF_NOT_EOL,
85868567Seric 	"RET_PARAM",	EF_RET_PARAM,
85968567Seric 	"HAS_DF",	EF_HAS_DF,
860*69837Seric 	"IS_MIME",	EF_IS_MIME,
86168567Seric 	NULL
86268567Seric };
86368567Seric 
86468567Seric void
printenvflags(e)86568567Seric printenvflags(e)
86668567Seric 	register ENVELOPE *e;
86768567Seric {
86868567Seric 	register struct eflags *ef;
86968567Seric 	bool first = TRUE;
87068567Seric 
87168567Seric 	printf("%lx", e->e_flags);
87268567Seric 	for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
87368567Seric 	{
87468567Seric 		if (!bitset(ef->ef_bit, e->e_flags))
87568567Seric 			continue;
87668567Seric 		if (first)
87768567Seric 			printf("<%s", ef->ef_name);
87868567Seric 		else
87968567Seric 			printf(",%s", ef->ef_name);
88068567Seric 		first = FALSE;
88168567Seric 	}
88268567Seric 	if (!first)
88368567Seric 		printf(">\n");
88468567Seric }
885