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*68019Seric static char sccsid[] = "@(#)envelope.c	8.49 (Berkeley) 12/01/94";
1133729Sbostic #endif /* not lint */
1222704Sdist 
1358332Seric #include "sendmail.h"
149536Seric #include <pwd.h>
159536Seric 
169536Seric /*
179536Seric **  NEWENVELOPE -- allocate a new envelope
189536Seric **
199536Seric **	Supports inheritance.
209536Seric **
219536Seric **	Parameters:
229536Seric **		e -- the new envelope to fill in.
2358179Seric **		parent -- the envelope to be the parent of e.
249536Seric **
259536Seric **	Returns:
269536Seric **		e.
279536Seric **
289536Seric **	Side Effects:
299536Seric **		none.
309536Seric */
319536Seric 
329536Seric ENVELOPE *
3358179Seric newenvelope(e, parent)
349536Seric 	register ENVELOPE *e;
3558179Seric 	register ENVELOPE *parent;
369536Seric {
379536Seric 	extern putheader(), putbody();
3825611Seric 	extern ENVELOPE BlankEnvelope;
399536Seric 
4058179Seric 	if (e == parent && e->e_parent != NULL)
419536Seric 		parent = e->e_parent;
4225611Seric 	clearenvelope(e, TRUE);
4324944Seric 	if (e == CurEnv)
4424944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4524944Seric 	else
4624944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
479536Seric 	e->e_parent = parent;
489536Seric 	e->e_ctime = curtime();
4956215Seric 	if (parent != NULL)
5056215Seric 		e->e_msgpriority = parent->e_msgsize;
519536Seric 	e->e_puthdr = putheader;
529536Seric 	e->e_putbody = putbody;
539536Seric 	if (CurEnv->e_xfp != NULL)
549536Seric 		(void) fflush(CurEnv->e_xfp);
559536Seric 
569536Seric 	return (e);
579536Seric }
589536Seric /*
599536Seric **  DROPENVELOPE -- deallocate an envelope.
609536Seric **
619536Seric **	Parameters:
629536Seric **		e -- the envelope to deallocate.
639536Seric **
649536Seric **	Returns:
659536Seric **		none.
669536Seric **
679536Seric **	Side Effects:
689536Seric **		housekeeping necessary to dispose of an envelope.
699536Seric **		Unlocks this queue file.
709536Seric */
719536Seric 
7260494Seric void
739536Seric dropenvelope(e)
749536Seric 	register ENVELOPE *e;
759536Seric {
769536Seric 	bool queueit = FALSE;
7767880Seric 	bool failure_return = FALSE;
7867880Seric 	bool success_return = FALSE;
799536Seric 	register ADDRESS *q;
8057943Seric 	char *id = e->e_id;
8167880Seric 	bool return_no, return_yes;
8263753Seric 	char buf[MAXLINE];
839536Seric 
849536Seric 	if (tTd(50, 1))
859536Seric 	{
8658680Seric 		printf("dropenvelope %x: id=", e);
879536Seric 		xputs(e->e_id);
8865089Seric 		printf(", flags=0x%x\n", e->e_flags);
8963753Seric 		if (tTd(50, 10))
9063753Seric 		{
9163753Seric 			printf("sendq=");
9263753Seric 			printaddr(e->e_sendqueue, TRUE);
9363753Seric 		}
949536Seric 	}
9557943Seric 
9658680Seric 	/* we must have an id to remove disk files */
9757943Seric 	if (id == NULL)
9858680Seric 		return;
9957943Seric 
1009536Seric #ifdef LOG
10165089Seric 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
10265089Seric 		logsender(e, NULL);
10358020Seric 	if (LogLevel > 84)
10465089Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
10557943Seric 				  id, e->e_flags, getpid());
10656795Seric #endif /* LOG */
10765089Seric 	e->e_flags &= ~EF_LOGSENDER;
1089536Seric 
10963753Seric 	/* post statistics */
11063753Seric 	poststats(StatFile);
11163753Seric 
1129536Seric 	/*
1139536Seric 	**  Extract state information from dregs of send list.
1149536Seric 	*/
1159536Seric 
11664743Seric 	e->e_flags &= ~EF_QUEUERUN;
11767880Seric 	return_no = return_yes = FALSE;
1189536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1199536Seric 	{
1209536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1219536Seric 			queueit = TRUE;
12267880Seric 
12367880Seric 		/* see if a notification is needed */
12467975Seric 		if (e->e_df != NULL &&
12567975Seric 		    bitset(QBADADDR, q->q_flags) &&
12667880Seric 		    bitset(QPINGONFAILURE, q->q_flags))
12763839Seric 		{
12867880Seric 			failure_return = TRUE;
12967939Seric 			if (q->q_owner == NULL && !emptyaddr(&e->e_from))
13063839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
13167982Seric 						  &e->e_errorqueue, 0, e);
13263839Seric 		}
13367880Seric 		else if (bitset(QSENT, q->q_flags) &&
13467880Seric 		    bitnset(M_LOCALMAILER, q->q_mailer->m_flags) &&
13567880Seric 		    bitset(QPINGONSUCCESS, q->q_flags))
13667880Seric 		{
13767880Seric 			success_return = TRUE;
13867880Seric 		}
13967963Seric 		else if (bitset(QRELAYED, q->q_flags))
14067963Seric 		{
14167963Seric 			success_return = TRUE;
14267963Seric 		}
14367880Seric 		else
14467880Seric 			continue;
14567880Seric 
14667880Seric 		/* common code for error returns and return receipts */
14767880Seric 
14867880Seric 		/* test for returning the body */
14967981Seric 		if (bitset(QHAS_RET_PARAM, q->q_flags))
15067880Seric 		{
15167981Seric 			if (bitset(QRET_HDRS, q->q_flags))
15267981Seric 				return_no = TRUE;
15367981Seric 			else
15467880Seric 				return_yes = TRUE;
15567880Seric 		}
1569536Seric 	}
15767880Seric 	if (return_no && !return_yes)
15867880Seric 		e->e_flags |= EF_NORETURN;
1599536Seric 
1609536Seric 	/*
16163753Seric 	**  See if the message timed out.
16263753Seric 	*/
16363753Seric 
16463753Seric 	if (!queueit)
16563753Seric 		/* nothing to do */ ;
16667730Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass])
16763753Seric 	{
16863839Seric 		(void) sprintf(buf, "Cannot send message for %s",
16967730Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
17063839Seric 		if (e->e_message != NULL)
17163839Seric 			free(e->e_message);
17263839Seric 		e->e_message = newstr(buf);
17363839Seric 		message(buf);
17463839Seric 		e->e_flags |= EF_CLRQUEUE;
17567880Seric 		failure_return = TRUE;
17663787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
17767730Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
17863787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
17963787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
18063787Seric 		{
18163787Seric 			if (bitset(QQUEUEUP, q->q_flags))
18263787Seric 				q->q_flags |= QBADADDR;
18363787Seric 		}
18463753Seric 	}
18567730Seric 	else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 &&
18667730Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass])
18763753Seric 	{
18867963Seric 		bool delay_return = FALSE;
18967963Seric 
19067963Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
19167963Seric 		{
19267963Seric 			if (bitset(QQUEUEUP, q->q_flags) &&
19367963Seric 			    bitset(QPINGONDELAY, q->q_flags))
19467963Seric 			{
19567963Seric 				q->q_flags |= QREPORT;
19667963Seric 				delay_return = TRUE;
19767963Seric 			}
19867963Seric 		}
19967963Seric 		if (delay_return &&
20067963Seric 		    !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
20163753Seric 		    e->e_class >= 0 &&
20267691Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0 &&
20367691Seric 		    strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 &&
20467691Seric 		    (strlen(e->e_from.q_paddr) <= 8 ||
20567691Seric 		     strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0))
20663753Seric 		{
20763753Seric 			(void) sprintf(buf,
20867261Seric 				"Warning: cannot send message for %s",
20967730Seric 				pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
21063753Seric 			if (e->e_message != NULL)
21163753Seric 				free(e->e_message);
21263753Seric 			e->e_message = newstr(buf);
21363753Seric 			message(buf);
21463839Seric 			e->e_flags |= EF_WARNING;
21567880Seric 			failure_return = TRUE;
21663753Seric 		}
21763753Seric 		fprintf(e->e_xfp,
21863753Seric 			"Warning: message still undelivered after %s\n",
21967730Seric 			pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE));
22063753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
22167730Seric 			pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE));
22263753Seric 	}
22363753Seric 
22467880Seric 	if (tTd(50, 2))
22567880Seric 		printf("failure_return=%d success_return=%d queueit=%d\n",
22667880Seric 			failure_return, success_return, queueit);
22767880Seric 
22863753Seric 	/*
229*68019Seric 	**  If we had some fatal error, but no addresses are marked as
230*68019Seric 	**  bad, mark them _all_ as bad.
231*68019Seric 	*/
232*68019Seric 
233*68019Seric 	if (bitset(EF_FATALERRS, e->e_flags) && !failure_return)
234*68019Seric 	{
235*68019Seric 		failure_return = TRUE;
236*68019Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
237*68019Seric 		{
238*68019Seric 			if (!bitset(QDONTSEND, q->q_flags))
239*68019Seric 				q->q_flags |= QBADADDR;
240*68019Seric 		}
241*68019Seric 	}
242*68019Seric 
243*68019Seric 	/*
2449536Seric 	**  Send back return receipts as requested.
2459536Seric 	*/
2469536Seric 
24767880Seric /*
24866783Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)
24966783Seric 	    && !bitset(PRIV_NORECEIPTS, PrivacyFlags))
25067880Seric */
25167880Seric 	if (e->e_receiptto == NULL)
25267880Seric 		e->e_receiptto = e->e_from.q_paddr;
25367981Seric 	if (success_return && !failure_return &&
25468002Seric 	    !bitset(PRIV_NORECEIPTS, PrivacyFlags) &&
25567981Seric 	    strcmp(e->e_receiptto, "<>") != 0)
2569536Seric 	{
25710844Seric 		auto ADDRESS *rlist = NULL;
2589536Seric 
25967880Seric 		e->e_flags |= EF_SENDRECEIPT;
26067982Seric 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e);
26167963Seric 		(void) returntosender("Return receipt", rlist, return_yes, e);
2629536Seric 	}
26367682Seric 	e->e_flags &= ~EF_SENDRECEIPT;
2649536Seric 
2659536Seric 	/*
2669536Seric 	**  Arrange to send error messages if there are fatal errors.
2679536Seric 	*/
2689536Seric 
26967880Seric 	if (failure_return && e->e_errormode != EM_QUIET)
27067981Seric 		savemail(e, return_yes || (!return_no && e->e_class >= 0));
2719536Seric 
2729536Seric 	/*
27363849Seric 	**  Arrange to send warning messages to postmaster as requested.
27463849Seric 	*/
27563849Seric 
27663849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
27764363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
27863849Seric 	{
27963849Seric 		auto ADDRESS *rlist = NULL;
28063849Seric 
28167982Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e);
28263849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
28363849Seric 	}
28463849Seric 
28563849Seric 	/*
2869536Seric 	**  Instantiate or deinstantiate the queue.
2879536Seric 	*/
2889536Seric 
2899536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2909536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2919536Seric 	{
29264307Seric 		if (tTd(50, 1))
29367880Seric 			printf("\n===== Dropping [dq]f%s (queueit=%d, e_flags=%x) =====\n\n",
29467880Seric 				e->e_id, queueit, e->e_flags);
29523497Seric 		if (e->e_df != NULL)
29623497Seric 			xunlink(e->e_df);
2979536Seric 		xunlink(queuename(e, 'q'));
29863839Seric 
29963839Seric #ifdef LOG
30063839Seric 		if (LogLevel > 10)
30163839Seric 			syslog(LOG_INFO, "%s: done", id);
30263839Seric #endif
3039536Seric 	}
3049536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
30510754Seric 	{
30610754Seric #ifdef QUEUE
30764307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
30856795Seric #else /* QUEUE */
30958151Seric 		syserr("554 dropenvelope: queueup");
31056795Seric #endif /* QUEUE */
31110754Seric 	}
3129536Seric 
3139536Seric 	/* now unlock the job */
31410196Seric 	closexscript(e);
3159536Seric 	unlockqueue(e);
3169536Seric 
3179536Seric 	/* make sure that this envelope is marked unused */
31824944Seric 	if (e->e_dfp != NULL)
31958680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
32010196Seric 	e->e_dfp = NULL;
32158680Seric 	e->e_id = e->e_df = NULL;
3229536Seric }
3239536Seric /*
3249536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
3259536Seric **
3269536Seric **	This is normally used by a child process to get a clean
3279536Seric **	envelope without disturbing the parent.
3289536Seric **
3299536Seric **	Parameters:
3309536Seric **		e -- the envelope to clear.
33125611Seric **		fullclear - if set, the current envelope is total
33225611Seric **			garbage and should be ignored; otherwise,
33325611Seric **			release any resources it may indicate.
3349536Seric **
3359536Seric **	Returns:
3369536Seric **		none.
3379536Seric **
3389536Seric **	Side Effects:
3399536Seric **		Closes files associated with the envelope.
3409536Seric **		Marks the envelope as unallocated.
3419536Seric */
3429536Seric 
34360494Seric void
34425611Seric clearenvelope(e, fullclear)
3459536Seric 	register ENVELOPE *e;
34625611Seric 	bool fullclear;
3479536Seric {
34825514Seric 	register HDR *bh;
34925514Seric 	register HDR **nhp;
35025514Seric 	extern ENVELOPE BlankEnvelope;
35125514Seric 
35225611Seric 	if (!fullclear)
35325611Seric 	{
35425611Seric 		/* clear out any file information */
35525611Seric 		if (e->e_xfp != NULL)
35658680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
35725611Seric 		if (e->e_dfp != NULL)
35858680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
35958680Seric 		e->e_xfp = e->e_dfp = NULL;
36025611Seric 	}
3619536Seric 
36224961Seric 	/* now clear out the data */
36324965Seric 	STRUCTCOPY(BlankEnvelope, *e);
36459698Seric 	if (Verbose)
36559698Seric 		e->e_sendmode = SM_DELIVER;
36625514Seric 	bh = BlankEnvelope.e_header;
36725514Seric 	nhp = &e->e_header;
36825514Seric 	while (bh != NULL)
36925514Seric 	{
37025514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
37125514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
37225514Seric 		bh = bh->h_link;
37325514Seric 		nhp = &(*nhp)->h_link;
37425514Seric 	}
3759536Seric }
3769536Seric /*
3779536Seric **  INITSYS -- initialize instantiation of system
3789536Seric **
3799536Seric **	In Daemon mode, this is done in the child.
3809536Seric **
3819536Seric **	Parameters:
3829536Seric **		none.
3839536Seric **
3849536Seric **	Returns:
3859536Seric **		none.
3869536Seric **
3879536Seric **	Side Effects:
3889536Seric **		Initializes the system macros, some global variables,
3899536Seric **		etc.  In particular, the current time in various
3909536Seric **		forms is set.
3919536Seric */
3929536Seric 
39360494Seric void
39455012Seric initsys(e)
39555012Seric 	register ENVELOPE *e;
3969536Seric {
39764768Seric 	char cbuf[5];				/* holds hop count */
39864768Seric 	char pbuf[10];				/* holds pid */
39922963Smiriam #ifdef TTYNAME
40059304Seric 	static char ybuf[60];			/* holds tty id */
4019536Seric 	register char *p;
40256795Seric #endif /* TTYNAME */
4039536Seric 	extern char *ttyname();
40460494Seric 	extern void settime();
4059536Seric 	extern char Version[];
4069536Seric 
4079536Seric 	/*
4089536Seric 	**  Give this envelope a reality.
4099536Seric 	**	I.e., an id, a transcript, and a creation time.
4109536Seric 	*/
4119536Seric 
41255012Seric 	openxscript(e);
41355012Seric 	e->e_ctime = curtime();
4149536Seric 
4159536Seric 	/*
4169536Seric 	**  Set OutChannel to something useful if stdout isn't it.
4179536Seric 	**	This arranges that any extra stuff the mailer produces
4189536Seric 	**	gets sent back to the user on error (because it is
4199536Seric 	**	tucked away in the transcript).
4209536Seric 	*/
4219536Seric 
42264760Seric 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
42358737Seric 	    e->e_xfp != NULL)
42455012Seric 		OutChannel = e->e_xfp;
4259536Seric 
4269536Seric 	/*
4279536Seric 	**  Set up some basic system macros.
4289536Seric 	*/
4299536Seric 
4309536Seric 	/* process id */
4319536Seric 	(void) sprintf(pbuf, "%d", getpid());
43264768Seric 	define('p', newstr(pbuf), e);
4339536Seric 
4349536Seric 	/* hop count */
43555012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
43664768Seric 	define('c', newstr(cbuf), e);
4379536Seric 
4389536Seric 	/* time as integer, unix time, arpa time */
43955012Seric 	settime(e);
4409536Seric 
44117472Seric #ifdef TTYNAME
4429536Seric 	/* tty name */
44355012Seric 	if (macvalue('y', e) == NULL)
4449536Seric 	{
4459536Seric 		p = ttyname(2);
4469536Seric 		if (p != NULL)
4479536Seric 		{
44856795Seric 			if (strrchr(p, '/') != NULL)
44956795Seric 				p = strrchr(p, '/') + 1;
4509536Seric 			(void) strcpy(ybuf, p);
45155012Seric 			define('y', ybuf, e);
4529536Seric 		}
4539536Seric 	}
45456795Seric #endif /* TTYNAME */
4559536Seric }
4569536Seric /*
45711932Seric **  SETTIME -- set the current time.
45811932Seric **
45911932Seric **	Parameters:
46011932Seric **		none.
46111932Seric **
46211932Seric **	Returns:
46311932Seric **		none.
46411932Seric **
46511932Seric **	Side Effects:
46611932Seric **		Sets the various time macros -- $a, $b, $d, $t.
46711932Seric */
46811932Seric 
46960494Seric void
47055012Seric settime(e)
47155012Seric 	register ENVELOPE *e;
47211932Seric {
47311932Seric 	register char *p;
47411932Seric 	auto time_t now;
47564768Seric 	char tbuf[20];				/* holds "current" time */
47664768Seric 	char dbuf[30];				/* holds ctime(tbuf) */
47711932Seric 	register struct tm *tm;
47811932Seric 	extern char *arpadate();
47911932Seric 	extern struct tm *gmtime();
48011932Seric 
48111932Seric 	now = curtime();
48211932Seric 	tm = gmtime(&now);
48357014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
48457014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
48564768Seric 	define('t', newstr(tbuf), e);
48611932Seric 	(void) strcpy(dbuf, ctime(&now));
48758131Seric 	p = strchr(dbuf, '\n');
48858131Seric 	if (p != NULL)
48958131Seric 		*p = '\0';
49064768Seric 	define('d', newstr(dbuf), e);
49164086Seric 	p = arpadate(dbuf);
49264086Seric 	p = newstr(p);
49355012Seric 	if (macvalue('a', e) == NULL)
49455012Seric 		define('a', p, e);
49555012Seric 	define('b', p, e);
49611932Seric }
49711932Seric /*
4989536Seric **  OPENXSCRIPT -- Open transcript file
4999536Seric **
5009536Seric **	Creates a transcript file for possible eventual mailing or
5019536Seric **	sending back.
5029536Seric **
5039536Seric **	Parameters:
5049536Seric **		e -- the envelope to create the transcript in/for.
5059536Seric **
5069536Seric **	Returns:
5079536Seric **		none
5089536Seric **
5099536Seric **	Side Effects:
5109536Seric **		Creates the transcript file.
5119536Seric */
5129536Seric 
51358803Seric #ifndef O_APPEND
51458803Seric #define O_APPEND	0
51558803Seric #endif
51658803Seric 
51760494Seric void
5189536Seric openxscript(e)
5199536Seric 	register ENVELOPE *e;
5209536Seric {
5219536Seric 	register char *p;
52240933Srick 	int fd;
5239536Seric 
5249536Seric 	if (e->e_xfp != NULL)
5259536Seric 		return;
5269536Seric 	p = queuename(e, 'x');
52758803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
52840933Srick 	if (fd < 0)
52963753Seric 	{
53063753Seric 		syserr("Can't create transcript file %s", p);
53163753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
53263753Seric 		if (fd < 0)
53363753Seric 			syserr("!Can't open /dev/null");
53463753Seric 	}
53563753Seric 	e->e_xfp = fdopen(fd, "w");
53664724Seric 	if (e->e_xfp == NULL)
53764724Seric 	{
53864724Seric 		syserr("!Can't create transcript stream %s", p);
53964724Seric 	}
54064743Seric 	if (tTd(46, 9))
54164743Seric 	{
54264743Seric 		printf("openxscript(%s):\n  ", p);
54364743Seric 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
54464743Seric 	}
5459536Seric }
5469536Seric /*
54710196Seric **  CLOSEXSCRIPT -- close the transcript file.
54810196Seric **
54910196Seric **	Parameters:
55010196Seric **		e -- the envelope containing the transcript to close.
55110196Seric **
55210196Seric **	Returns:
55310196Seric **		none.
55410196Seric **
55510196Seric **	Side Effects:
55610196Seric **		none.
55710196Seric */
55810196Seric 
55960494Seric void
56010196Seric closexscript(e)
56110196Seric 	register ENVELOPE *e;
56210196Seric {
56310196Seric 	if (e->e_xfp == NULL)
56410196Seric 		return;
56558680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
56610196Seric 	e->e_xfp = NULL;
56710196Seric }
56810196Seric /*
5699536Seric **  SETSENDER -- set the person who this message is from
5709536Seric **
5719536Seric **	Under certain circumstances allow the user to say who
5729536Seric **	s/he is (using -f or -r).  These are:
5739536Seric **	1.  The user's uid is zero (root).
5749536Seric **	2.  The user's login name is in an approved list (typically
5759536Seric **	    from a network server).
5769536Seric **	3.  The address the user is trying to claim has a
5779536Seric **	    "!" character in it (since #2 doesn't do it for
5789536Seric **	    us if we are dialing out for UUCP).
5799536Seric **	A better check to replace #3 would be if the
5809536Seric **	effective uid is "UUCP" -- this would require me
5819536Seric **	to rewrite getpwent to "grab" uucp as it went by,
5829536Seric **	make getname more nasty, do another passwd file
5839536Seric **	scan, or compile the UID of "UUCP" into the code,
5849536Seric **	all of which are reprehensible.
5859536Seric **
5869536Seric **	Assuming all of these fail, we figure out something
5879536Seric **	ourselves.
5889536Seric **
5899536Seric **	Parameters:
5909536Seric **		from -- the person we would like to believe this message
5919536Seric **			is from, as specified on the command line.
59253182Seric **		e -- the envelope in which we would like the sender set.
59358333Seric **		delimptr -- if non-NULL, set to the location of the
59458333Seric **			trailing delimiter.
59558704Seric **		internal -- set if this address is coming from an internal
59658704Seric **			source such as an owner alias.
5979536Seric **
5989536Seric **	Returns:
59958704Seric **		none.
6009536Seric **
6019536Seric **	Side Effects:
6029536Seric **		sets sendmail's notion of who the from person is.
6039536Seric */
6049536Seric 
60560494Seric void
60658704Seric setsender(from, e, delimptr, internal)
6079536Seric 	char *from;
60853182Seric 	register ENVELOPE *e;
60958333Seric 	char **delimptr;
61058704Seric 	bool internal;
6119536Seric {
6129536Seric 	register char **pvp;
6139536Seric 	char *realname = NULL;
61418665Seric 	register struct passwd *pw;
61558727Seric 	char delimchar;
61664147Seric 	char *bp;
61764147Seric 	char buf[MAXNAME + 2];
61816913Seric 	char pvpbuf[PSBUFSIZE];
61918665Seric 	extern struct passwd *getpwnam();
6209536Seric 	extern char *FullName;
6219536Seric 
6229536Seric 	if (tTd(45, 1))
62314786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
6249536Seric 
6259536Seric 	/*
6269536Seric 	**  Figure out the real user executing us.
6279536Seric 	**	Username can return errno != 0 on non-errors.
6289536Seric 	*/
6299536Seric 
63065580Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
63165983Seric 	    OpMode == MD_ARPAFTP || OpMode == MD_DAEMON)
6329536Seric 		realname = from;
6339536Seric 	if (realname == NULL || realname[0] == '\0')
6349536Seric 		realname = username();
6359536Seric 
63659027Seric 	if (ConfigLevel < 2)
63759027Seric 		SuprErrs = TRUE;
63859027Seric 
63958727Seric 	delimchar = internal ? '\0' : ' ';
64064793Seric 	e->e_from.q_flags = QBADADDR;
64158333Seric 	if (from == NULL ||
64264284Seric 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
64364793Seric 		      delimchar, delimptr, e) == NULL ||
64464793Seric 	    bitset(QBADADDR, e->e_from.q_flags) ||
64564793Seric 	    e->e_from.q_mailer == ProgMailer ||
64664793Seric 	    e->e_from.q_mailer == FileMailer ||
64764793Seric 	    e->e_from.q_mailer == InclMailer)
6489536Seric 	{
64921750Seric 		/* log garbage addresses for traceback */
65055173Seric # ifdef LOG
65158020Seric 		if (from != NULL && LogLevel > 2)
65221750Seric 		{
65358951Seric 			char *p;
65458951Seric 			char ebuf[MAXNAME * 2 + 2];
65555173Seric 
65658951Seric 			p = macvalue('_', e);
65758951Seric 			if (p == NULL)
65858951Seric 			{
65958951Seric 				char *host = RealHostName;
66058951Seric 				if (host == NULL)
66158951Seric 					host = MyHostName;
66258951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
66358951Seric 				p = ebuf;
66458951Seric 			}
66555173Seric 			syslog(LOG_NOTICE,
66664793Seric 				"setsender: %s: invalid or unparseable, received from %s",
66765015Seric 				shortenstring(from, 83), p);
66855173Seric 		}
66956795Seric # endif /* LOG */
67057589Seric 		if (from != NULL)
67164793Seric 		{
67264793Seric 			if (!bitset(QBADADDR, e->e_from.q_flags))
67364793Seric 			{
67464793Seric 				/* it was a bogus mailer in the from addr */
67564793Seric 				usrerr("553 Invalid sender address");
67664793Seric 			}
67757589Seric 			SuprErrs = TRUE;
67864793Seric 		}
67957589Seric 		if (from == realname ||
68064284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
68164284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
68224944Seric 		{
68364793Seric 			char nbuf[100];
68464793Seric 
68557589Seric 			SuprErrs = TRUE;
68664808Seric 			expand("\201n", nbuf, &nbuf[sizeof nbuf], e);
68764793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
68864793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
68964793Seric 			    parseaddr(from = "postmaster", &e->e_from,
69064793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
69158151Seric 				syserr("553 setsender: can't even parse postmaster!");
69224944Seric 		}
6939536Seric 	}
6949536Seric 	else
6959536Seric 		FromFlag = TRUE;
69653182Seric 	e->e_from.q_flags |= QDONTSEND;
69757731Seric 	if (tTd(45, 5))
69857731Seric 	{
69957731Seric 		printf("setsender: QDONTSEND ");
70057731Seric 		printaddr(&e->e_from, FALSE);
70157731Seric 	}
7029536Seric 	SuprErrs = FALSE;
7039536Seric 
70467765Seric # ifdef USERDB
70567472Seric 	if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags))
7069536Seric 	{
70753736Seric 		register char *p;
70853736Seric 		extern char *udbsender();
70917472Seric 
71067765Seric 		p = udbsender(e->e_from.q_user);
71167765Seric 		if (p != NULL)
71267765Seric 			from = p;
71367765Seric 	}
71467765Seric # endif /* USERDB */
71567765Seric 
71667765Seric 	if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags))
71767765Seric 	{
71858704Seric 		if (!internal)
71958704Seric 		{
72067765Seric 			/* if the user already given fullname don't redefine */
72158704Seric 			if (FullName == NULL)
72258704Seric 				FullName = macvalue('x', e);
72358704Seric 			if (FullName != NULL && FullName[0] == '\0')
72458704Seric 				FullName = NULL;
7259536Seric 		}
72653736Seric 
72753736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
72853736Seric 		{
72953736Seric 			/*
73053736Seric 			**  Process passwd file entry.
73153736Seric 			*/
73253736Seric 
73353736Seric 			/* extract home directory */
73465822Seric 			if (strcmp(pw->pw_dir, "/") == 0)
73565822Seric 				e->e_from.q_home = newstr("");
73665822Seric 			else
73765822Seric 				e->e_from.q_home = newstr(pw->pw_dir);
73853736Seric 			define('z', e->e_from.q_home, e);
73953736Seric 
74053736Seric 			/* extract user and group id */
74153736Seric 			e->e_from.q_uid = pw->pw_uid;
74253736Seric 			e->e_from.q_gid = pw->pw_gid;
74365023Seric 			e->e_from.q_flags |= QGOODUID;
74453736Seric 
74553736Seric 			/* extract full name from passwd file */
74653736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
74758704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
74858704Seric 			    !internal)
74953736Seric 			{
75065015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
75153736Seric 				if (buf[0] != '\0')
75253736Seric 					FullName = newstr(buf);
75353736Seric 			}
75453736Seric 		}
75558704Seric 		if (FullName != NULL && !internal)
75653182Seric 			define('x', FullName, e);
7579536Seric 	}
75865580Seric 	else if (!internal && OpMode != MD_DAEMON)
75911625Seric 	{
76053182Seric 		if (e->e_from.q_home == NULL)
76165822Seric 		{
76253182Seric 			e->e_from.q_home = getenv("HOME");
76366049Seric 			if (e->e_from.q_home != NULL &&
76466049Seric 			    strcmp(e->e_from.q_home, "/") == 0)
76565822Seric 				e->e_from.q_home++;
76665822Seric 		}
76763787Seric 		e->e_from.q_uid = RealUid;
76863787Seric 		e->e_from.q_gid = RealGid;
76965023Seric 		e->e_from.q_flags |= QGOODUID;
77011625Seric 	}
77111625Seric 
7729536Seric 	/*
7739536Seric 	**  Rewrite the from person to dispose of possible implicit
7749536Seric 	**	links in the net.
7759536Seric 	*/
7769536Seric 
77767765Seric 	pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
7789536Seric 	if (pvp == NULL)
7799536Seric 	{
78058403Seric 		/* don't need to give error -- prescan did that already */
78136233Skarels # ifdef LOG
78258020Seric 		if (LogLevel > 2)
78336233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
78436233Skarels # endif
7859536Seric 		finis();
7869536Seric 	}
78767765Seric /*
78865071Seric 	(void) rewrite(pvp, 3, 0, e);
78965071Seric 	(void) rewrite(pvp, 1, 0, e);
79065071Seric 	(void) rewrite(pvp, 4, 0, e);
79167765Seric */
79264147Seric 	bp = buf + 1;
79364147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
79467619Seric 	if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags))
79564147Seric 	{
79664147Seric 		/* heuristic: route-addr: add angle brackets */
79764147Seric 		strcat(bp, ">");
79864147Seric 		*--bp = '<';
79964147Seric 	}
80064147Seric 	e->e_sender = newstr(bp);
80158704Seric 	define('f', e->e_sender, e);
8029536Seric 
8039536Seric 	/* save the domain spec if this mailer wants it */
80465584Seric 	if (e->e_from.q_mailer != NULL &&
80553182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
8069536Seric 	{
8079536Seric 		extern char **copyplist();
8089536Seric 
8099536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
8109536Seric 			pvp++;
8119536Seric 		if (*pvp != NULL)
81253182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
8139536Seric 	}
8149536Seric }
815