122704Sdist /*
234921Sbostic  * Copyright (c) 1983 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*68711Seric static char sccsid[] = "@(#)envelope.c	8.59 (Berkeley) 04/02/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 *
3258179Seric newenvelope(e, parent)
339536Seric 	register ENVELOPE *e;
3458179Seric 	register ENVELOPE *parent;
359536Seric {
369536Seric 	extern putheader(), putbody();
3725611Seric 	extern ENVELOPE BlankEnvelope;
389536Seric 
3958179Seric 	if (e == parent && e->e_parent != NULL)
409536Seric 		parent = e->e_parent;
4125611Seric 	clearenvelope(e, TRUE);
4224944Seric 	if (e == CurEnv)
4324944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4424944Seric 	else
4524944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
469536Seric 	e->e_parent = parent;
479536Seric 	e->e_ctime = curtime();
4856215Seric 	if (parent != NULL)
4956215Seric 		e->e_msgpriority = parent->e_msgsize;
509536Seric 	e->e_puthdr = putheader;
519536Seric 	e->e_putbody = putbody;
529536Seric 	if (CurEnv->e_xfp != NULL)
539536Seric 		(void) fflush(CurEnv->e_xfp);
549536Seric 
559536Seric 	return (e);
569536Seric }
579536Seric /*
589536Seric **  DROPENVELOPE -- deallocate an envelope.
599536Seric **
609536Seric **	Parameters:
619536Seric **		e -- the envelope to deallocate.
629536Seric **
639536Seric **	Returns:
649536Seric **		none.
659536Seric **
669536Seric **	Side Effects:
679536Seric **		housekeeping necessary to dispose of an envelope.
689536Seric **		Unlocks this queue file.
699536Seric */
709536Seric 
7160494Seric void
729536Seric dropenvelope(e)
739536Seric 	register ENVELOPE *e;
749536Seric {
759536Seric 	bool queueit = FALSE;
7668498Seric 	bool failure_return = FALSE;
7768498Seric 	bool success_return = FALSE;
789536Seric 	register ADDRESS *q;
7957943Seric 	char *id = e->e_id;
8063753Seric 	char buf[MAXLINE];
819536Seric 
829536Seric 	if (tTd(50, 1))
839536Seric 	{
8468567Seric 		extern void printenvflags();
8568567Seric 
8658680Seric 		printf("dropenvelope %x: id=", e);
879536Seric 		xputs(e->e_id);
8868567Seric 		printf(", flags=");
8968567Seric 		printenvflags(e);
9063753Seric 		if (tTd(50, 10))
9163753Seric 		{
9263753Seric 			printf("sendq=");
9363753Seric 			printaddr(e->e_sendqueue, TRUE);
9463753Seric 		}
959536Seric 	}
9657943Seric 
9758680Seric 	/* we must have an id to remove disk files */
9857943Seric 	if (id == NULL)
9958680Seric 		return;
10057943Seric 
1019536Seric #ifdef LOG
10265089Seric 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
10365089Seric 		logsender(e, NULL);
10458020Seric 	if (LogLevel > 84)
10565089Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
10657943Seric 				  id, e->e_flags, getpid());
10756795Seric #endif /* LOG */
10865089Seric 	e->e_flags &= ~EF_LOGSENDER;
1099536Seric 
11063753Seric 	/* post statistics */
11163753Seric 	poststats(StatFile);
11263753Seric 
1139536Seric 	/*
1149536Seric 	**  Extract state information from dregs of send list.
1159536Seric 	*/
1169536Seric 
11764743Seric 	e->e_flags &= ~EF_QUEUERUN;
1189536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1199536Seric 	{
1209536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1219536Seric 			queueit = TRUE;
12268498Seric 
12368498Seric 		/* see if a notification is needed */
12468564Seric 		if (bitset(QBADADDR, q->q_flags) &&
12568498Seric 		    bitset(QPINGONFAILURE, q->q_flags))
12663839Seric 		{
12768498Seric 			failure_return = TRUE;
12868498Seric 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
12963839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
13068498Seric 						  &e->e_errorqueue, 0, e);
13163839Seric 		}
13268603Seric 		else if (bitset(QPINGONSUCCESS, q->q_flags) &&
13368603Seric 			 ((bitset(QSENT, q->q_flags) &&
13468603Seric 			   bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) ||
13568603Seric 			  bitset(QRELAYED|QEXPLODED, q->q_flags)))
13668498Seric 		{
13768498Seric 			success_return = TRUE;
13868498Seric 		}
1399536Seric 	}
1409536Seric 
14168559Seric 	if (e->e_class < 0)
14268559Seric 		e->e_flags |= EF_NO_BODY_RETN;
14368559Seric 
1449536Seric 	/*
14563753Seric 	**  See if the message timed out.
14663753Seric 	*/
14763753Seric 
14863753Seric 	if (!queueit)
14963753Seric 		/* nothing to do */ ;
15068498Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
15163753Seric 	{
15263839Seric 		(void) sprintf(buf, "Cannot send message for %s",
15368498Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
15463839Seric 		if (e->e_message != NULL)
15563839Seric 			free(e->e_message);
15663839Seric 		e->e_message = newstr(buf);
15763839Seric 		message(buf);
15863839Seric 		e->e_flags |= EF_CLRQUEUE;
15968498Seric 		failure_return = TRUE;
16063787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
16168498Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
16263787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
16363787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16463787Seric 		{
16563787Seric 			if (bitset(QQUEUEUP, q->q_flags))
16663787Seric 				q->q_flags |= QBADADDR;
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 			{
17968498Seric 				q->q_flags |= QREPORT;
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 
26063849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
26164363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
26263849Seric 	{
26363849Seric 		auto ADDRESS *rlist = NULL;
26463849Seric 
26568498Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e);
26663849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
26763849Seric 	}
26863849Seric 
26963849Seric 	/*
2709536Seric 	**  Instantiate or deinstantiate the queue.
2719536Seric 	*/
2729536Seric 
2739536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2749536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2759536Seric 	{
27664307Seric 		if (tTd(50, 1))
27768567Seric 		{
27868567Seric 			extern void printenvflags();
27968567Seric 
28068567Seric 			printf("\n===== Dropping [dq]f%s... queueit=%d, e_flags=",
28168567Seric 				e->e_id, queueit);
28268567Seric 			printenvflags(e);
28368567Seric 		}
28468564Seric 		xunlink(queuename(e, 'd'));
2859536Seric 		xunlink(queuename(e, 'q'));
28663839Seric 
28763839Seric #ifdef LOG
28863839Seric 		if (LogLevel > 10)
28963839Seric 			syslog(LOG_INFO, "%s: done", id);
29063839Seric #endif
2919536Seric 	}
2929536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
29310754Seric 	{
29410754Seric #ifdef QUEUE
29564307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
29656795Seric #else /* QUEUE */
29758151Seric 		syserr("554 dropenvelope: queueup");
29856795Seric #endif /* QUEUE */
29910754Seric 	}
3009536Seric 
3019536Seric 	/* now unlock the job */
30210196Seric 	closexscript(e);
3039536Seric 	unlockqueue(e);
3049536Seric 
3059536Seric 	/* make sure that this envelope is marked unused */
30624944Seric 	if (e->e_dfp != NULL)
30768564Seric 		(void) xfclose(e->e_dfp, "dropenvelope df", e->e_id);
30810196Seric 	e->e_dfp = NULL;
30968564Seric 	e->e_id = NULL;
31068564Seric 	e->e_flags &= ~EF_HAS_DF;
3119536Seric }
3129536Seric /*
3139536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
3149536Seric **
3159536Seric **	This is normally used by a child process to get a clean
3169536Seric **	envelope without disturbing the parent.
3179536Seric **
3189536Seric **	Parameters:
3199536Seric **		e -- the envelope to clear.
32025611Seric **		fullclear - if set, the current envelope is total
32125611Seric **			garbage and should be ignored; otherwise,
32225611Seric **			release any resources it may indicate.
3239536Seric **
3249536Seric **	Returns:
3259536Seric **		none.
3269536Seric **
3279536Seric **	Side Effects:
3289536Seric **		Closes files associated with the envelope.
3299536Seric **		Marks the envelope as unallocated.
3309536Seric */
3319536Seric 
33260494Seric void
33325611Seric clearenvelope(e, fullclear)
3349536Seric 	register ENVELOPE *e;
33525611Seric 	bool fullclear;
3369536Seric {
33725514Seric 	register HDR *bh;
33825514Seric 	register HDR **nhp;
33925514Seric 	extern ENVELOPE BlankEnvelope;
34025514Seric 
34125611Seric 	if (!fullclear)
34225611Seric 	{
34325611Seric 		/* clear out any file information */
34425611Seric 		if (e->e_xfp != NULL)
34558680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
34625611Seric 		if (e->e_dfp != NULL)
34768564Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_id);
34858680Seric 		e->e_xfp = e->e_dfp = NULL;
34925611Seric 	}
3509536Seric 
35124961Seric 	/* now clear out the data */
35224965Seric 	STRUCTCOPY(BlankEnvelope, *e);
35359698Seric 	if (Verbose)
35459698Seric 		e->e_sendmode = SM_DELIVER;
35525514Seric 	bh = BlankEnvelope.e_header;
35625514Seric 	nhp = &e->e_header;
35725514Seric 	while (bh != NULL)
35825514Seric 	{
35925514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
36025514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
36125514Seric 		bh = bh->h_link;
36225514Seric 		nhp = &(*nhp)->h_link;
36325514Seric 	}
3649536Seric }
3659536Seric /*
3669536Seric **  INITSYS -- initialize instantiation of system
3679536Seric **
3689536Seric **	In Daemon mode, this is done in the child.
3699536Seric **
3709536Seric **	Parameters:
3719536Seric **		none.
3729536Seric **
3739536Seric **	Returns:
3749536Seric **		none.
3759536Seric **
3769536Seric **	Side Effects:
3779536Seric **		Initializes the system macros, some global variables,
3789536Seric **		etc.  In particular, the current time in various
3799536Seric **		forms is set.
3809536Seric */
3819536Seric 
38260494Seric void
38355012Seric initsys(e)
38455012Seric 	register ENVELOPE *e;
3859536Seric {
38664768Seric 	char cbuf[5];				/* holds hop count */
38764768Seric 	char pbuf[10];				/* holds pid */
38822963Smiriam #ifdef TTYNAME
38959304Seric 	static char ybuf[60];			/* holds tty id */
3909536Seric 	register char *p;
39156795Seric #endif /* TTYNAME */
3929536Seric 	extern char *ttyname();
39360494Seric 	extern void settime();
3949536Seric 	extern char Version[];
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
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
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
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
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 */
66664793Seric 				usrerr("553 Invalid sender address");
66764793Seric 			}
66857589Seric 			SuprErrs = TRUE;
66964793Seric 		}
67057589Seric 		if (from == realname ||
67164284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
67264284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
67324944Seric 		{
67464793Seric 			char nbuf[100];
67564793Seric 
67657589Seric 			SuprErrs = TRUE;
67768529Seric 			expand("\201n", nbuf, sizeof nbuf, e);
67864793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
67964793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
68064793Seric 			    parseaddr(from = "postmaster", &e->e_from,
68164793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
68258151Seric 				syserr("553 setsender: can't even parse postmaster!");
68324944Seric 		}
6849536Seric 	}
6859536Seric 	else
6869536Seric 		FromFlag = TRUE;
68753182Seric 	e->e_from.q_flags |= QDONTSEND;
68857731Seric 	if (tTd(45, 5))
68957731Seric 	{
69057731Seric 		printf("setsender: QDONTSEND ");
69157731Seric 		printaddr(&e->e_from, FALSE);
69257731Seric 	}
6939536Seric 	SuprErrs = FALSE;
6949536Seric 
69568498Seric # ifdef USERDB
69668498Seric 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
69768460Seric 	{
69853736Seric 		register char *p;
69953736Seric 		extern char *udbsender();
70017472Seric 
70168498Seric 		p = udbsender(e->e_from.q_user);
70268498Seric 		if (p != NULL)
70368498Seric 			from = p;
70468498Seric 	}
70568498Seric # endif /* USERDB */
70668498Seric 
70768498Seric 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
70868498Seric 	{
70958704Seric 		if (!internal)
71058704Seric 		{
71168498Seric 			/* if the user already given fullname don't redefine */
71258704Seric 			if (FullName == NULL)
71358704Seric 				FullName = macvalue('x', e);
71458704Seric 			if (FullName != NULL && FullName[0] == '\0')
71558704Seric 				FullName = NULL;
7169536Seric 		}
71753736Seric 
71868693Seric 		if ((pw = sm_getpwnam(e->e_from.q_user)) != NULL)
71953736Seric 		{
72053736Seric 			/*
72153736Seric 			**  Process passwd file entry.
72253736Seric 			*/
72353736Seric 
72453736Seric 			/* extract home directory */
72565822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
72665822Seric 				e->e_from.q_home = newstr("");
72765822Seric 			else
72865822Seric 				e->e_from.q_home = newstr(pw->pw_dir);
72953736Seric 			define('z', e->e_from.q_home, e);
73053736Seric 
73153736Seric 			/* extract user and group id */
73253736Seric 			e->e_from.q_uid = pw->pw_uid;
73353736Seric 			e->e_from.q_gid = pw->pw_gid;
73465023Seric 			e->e_from.q_flags |= QGOODUID;
73553736Seric 
73653736Seric 			/* extract full name from passwd file */
73753736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
73858704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
73958704Seric 			    !internal)
74053736Seric 			{
74165015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
74253736Seric 				if (buf[0] != '\0')
74353736Seric 					FullName = newstr(buf);
74453736Seric 			}
74553736Seric 		}
74658704Seric 		if (FullName != NULL && !internal)
74753182Seric 			define('x', FullName, e);
7489536Seric 	}
74965580Seric 	else if (!internal && OpMode != MD_DAEMON)
75011625Seric 	{
75153182Seric 		if (e->e_from.q_home == NULL)
75265822Seric 		{
75353182Seric 			e->e_from.q_home = getenv("HOME");
75466049Seric 			if (e->e_from.q_home != NULL &&
75566049Seric 			    strcmp(e->e_from.q_home, "/") == 0)
75665822Seric 				e->e_from.q_home++;
75765822Seric 		}
75863787Seric 		e->e_from.q_uid = RealUid;
75963787Seric 		e->e_from.q_gid = RealGid;
76065023Seric 		e->e_from.q_flags |= QGOODUID;
76111625Seric 	}
76211625Seric 
7639536Seric 	/*
7649536Seric 	**  Rewrite the from person to dispose of possible implicit
7659536Seric 	**	links in the net.
7669536Seric 	*/
7679536Seric 
768*68711Seric 	pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, NULL);
7699536Seric 	if (pvp == NULL)
7709536Seric 	{
77158403Seric 		/* don't need to give error -- prescan did that already */
77236233Skarels # ifdef LOG
77358020Seric 		if (LogLevel > 2)
77436233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
77536233Skarels # endif
7769536Seric 		finis();
7779536Seric 	}
77868498Seric /*
77965071Seric 	(void) rewrite(pvp, 3, 0, e);
78065071Seric 	(void) rewrite(pvp, 1, 0, e);
78165071Seric 	(void) rewrite(pvp, 4, 0, e);
78268498Seric */
78364147Seric 	bp = buf + 1;
78464147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
78568498Seric 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
78664147Seric 	{
78764147Seric 		/* heuristic: route-addr: add angle brackets */
78864147Seric 		strcat(bp, ">");
78964147Seric 		*--bp = '<';
79064147Seric 	}
79164147Seric 	e->e_sender = newstr(bp);
79258704Seric 	define('f', e->e_sender, e);
7939536Seric 
7949536Seric 	/* save the domain spec if this mailer wants it */
79565584Seric 	if (e->e_from.q_mailer != NULL &&
79653182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
7979536Seric 	{
7989536Seric 		extern char **copyplist();
7999536Seric 
8009536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
8019536Seric 			pvp++;
8029536Seric 		if (*pvp != NULL)
80353182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
8049536Seric 	}
8059536Seric }
80668567Seric /*
80768567Seric **  PRINTENVFLAGS -- print envelope flags for debugging
80868567Seric **
80968567Seric **	Parameters:
81068567Seric **		e -- the envelope with the flags to be printed.
81168567Seric **
81268567Seric **	Returns:
81368567Seric **		none.
81468567Seric */
81568567Seric 
81668567Seric struct eflags
81768567Seric {
81868567Seric 	char	*ef_name;
81968567Seric 	u_long	ef_bit;
82068567Seric };
82168567Seric 
82268567Seric struct eflags	EnvelopeFlags[] =
82368567Seric {
82468567Seric 	"OLDSTYLE",	EF_OLDSTYLE,
82568567Seric 	"INQUEUE",	EF_INQUEUE,
82668567Seric 	"NO_BODY_RETN",	EF_NO_BODY_RETN,
82768567Seric 	"CLRQUEUE",	EF_CLRQUEUE,
82868567Seric 	"SENDRECEIPT",	EF_SENDRECEIPT,
82968567Seric 	"FATALERRS",	EF_FATALERRS,
83068567Seric 	"KEEPQUEUE",	EF_KEEPQUEUE,
83168567Seric 	"RESPONSE",	EF_RESPONSE,
83268567Seric 	"RESENT",	EF_RESENT,
83368567Seric 	"VRFYONLY",	EF_VRFYONLY,
83468567Seric 	"WARNING",	EF_WARNING,
83568567Seric 	"QUEUERUN",	EF_QUEUERUN,
83668567Seric 	"GLOBALERRS",	EF_GLOBALERRS,
83768567Seric 	"PM_NOTIFY",	EF_PM_NOTIFY,
83868567Seric 	"METOO",	EF_METOO,
83968567Seric 	"LOGSENDER",	EF_LOGSENDER,
84068567Seric 	"NORECEIPT",	EF_NORECEIPT,
84168567Seric 	"HAS8BIT",	EF_HAS8BIT,
84268567Seric 	"NL_NOT_EOL",	EF_NL_NOT_EOL,
84368567Seric 	"CRLF_NOT_EOL",	EF_CRLF_NOT_EOL,
84468567Seric 	"RET_PARAM",	EF_RET_PARAM,
84568567Seric 	"HAS_DF",	EF_HAS_DF,
84668567Seric 	NULL
84768567Seric };
84868567Seric 
84968567Seric void
85068567Seric printenvflags(e)
85168567Seric 	register ENVELOPE *e;
85268567Seric {
85368567Seric 	register struct eflags *ef;
85468567Seric 	bool first = TRUE;
85568567Seric 
85668567Seric 	printf("%lx", e->e_flags);
85768567Seric 	for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++)
85868567Seric 	{
85968567Seric 		if (!bitset(ef->ef_bit, e->e_flags))
86068567Seric 			continue;
86168567Seric 		if (first)
86268567Seric 			printf("<%s", ef->ef_name);
86368567Seric 		else
86468567Seric 			printf(",%s", ef->ef_name);
86568567Seric 		first = FALSE;
86668567Seric 	}
86768567Seric 	if (!first)
86868567Seric 		printf(">\n");
86968567Seric }
870