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*65066Seric static char sccsid[] = "@(#)envelope.c	8.23 (Berkeley) 12/10/93";
1133729Sbostic #endif /* not lint */
1222704Sdist 
1358332Seric #include "sendmail.h"
1436928Sbostic #include <sys/time.h>
159536Seric #include <pwd.h>
169536Seric 
179536Seric /*
189536Seric **  NEWENVELOPE -- allocate a new envelope
199536Seric **
209536Seric **	Supports inheritance.
219536Seric **
229536Seric **	Parameters:
239536Seric **		e -- the new envelope to fill in.
2458179Seric **		parent -- the envelope to be the parent of e.
259536Seric **
269536Seric **	Returns:
279536Seric **		e.
289536Seric **
299536Seric **	Side Effects:
309536Seric **		none.
319536Seric */
329536Seric 
339536Seric ENVELOPE *
3458179Seric newenvelope(e, parent)
359536Seric 	register ENVELOPE *e;
3658179Seric 	register ENVELOPE *parent;
379536Seric {
389536Seric 	extern putheader(), putbody();
3925611Seric 	extern ENVELOPE BlankEnvelope;
409536Seric 
4158179Seric 	if (e == parent && e->e_parent != NULL)
429536Seric 		parent = e->e_parent;
4325611Seric 	clearenvelope(e, TRUE);
4424944Seric 	if (e == CurEnv)
4524944Seric 		bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from);
4624944Seric 	else
4724944Seric 		bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
489536Seric 	e->e_parent = parent;
499536Seric 	e->e_ctime = curtime();
5056215Seric 	if (parent != NULL)
5156215Seric 		e->e_msgpriority = parent->e_msgsize;
529536Seric 	e->e_puthdr = putheader;
539536Seric 	e->e_putbody = putbody;
549536Seric 	if (CurEnv->e_xfp != NULL)
559536Seric 		(void) fflush(CurEnv->e_xfp);
569536Seric 
579536Seric 	return (e);
589536Seric }
599536Seric /*
609536Seric **  DROPENVELOPE -- deallocate an envelope.
619536Seric **
629536Seric **	Parameters:
639536Seric **		e -- the envelope to deallocate.
649536Seric **
659536Seric **	Returns:
669536Seric **		none.
679536Seric **
689536Seric **	Side Effects:
699536Seric **		housekeeping necessary to dispose of an envelope.
709536Seric **		Unlocks this queue file.
719536Seric */
729536Seric 
7360494Seric void
749536Seric dropenvelope(e)
759536Seric 	register ENVELOPE *e;
769536Seric {
779536Seric 	bool queueit = FALSE;
7863839Seric 	bool saveit = bitset(EF_FATALERRS, e->e_flags);
799536Seric 	register ADDRESS *q;
8057943Seric 	char *id = e->e_id;
8163753Seric 	char buf[MAXLINE];
829536Seric 
839536Seric 	if (tTd(50, 1))
849536Seric 	{
8558680Seric 		printf("dropenvelope %x: id=", e);
869536Seric 		xputs(e->e_id);
8758680Seric 		printf(", flags=%o\n", e->e_flags);
8863753Seric 		if (tTd(50, 10))
8963753Seric 		{
9063753Seric 			printf("sendq=");
9163753Seric 			printaddr(e->e_sendqueue, TRUE);
9263753Seric 		}
939536Seric 	}
9457943Seric 
9564925Seric #ifdef XDEBUG
9664925Seric 	checkfd012("dropenvelope 1");
9764925Seric #endif
9864925Seric 
9958680Seric 	/* we must have an id to remove disk files */
10057943Seric 	if (id == NULL)
10158680Seric 		return;
10257943Seric 
1039536Seric #ifdef LOG
10458020Seric 	if (LogLevel > 84)
1059536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
10657943Seric 				  id, e->e_flags, getpid());
10756795Seric #endif /* LOG */
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;
1179536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1189536Seric 	{
1199536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1209536Seric 			queueit = TRUE;
12163839Seric 		if (!bitset(QDONTSEND, q->q_flags) &&
12263839Seric 		    bitset(QBADADDR, q->q_flags))
12363839Seric 		{
12463839Seric 			if (q->q_owner == NULL &&
12563839Seric 			    strcmp(e->e_from.q_paddr, "<>") != 0)
12663839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
12763839Seric 						  &e->e_errorqueue, e);
12863839Seric 		}
1299536Seric 	}
1309536Seric 
1319536Seric 	/*
13263753Seric 	**  See if the message timed out.
13363753Seric 	*/
13463753Seric 
13563753Seric 	if (!queueit)
13663753Seric 		/* nothing to do */ ;
13763753Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
13863753Seric 	{
13963839Seric 		(void) sprintf(buf, "Cannot send message for %s",
14063839Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14163839Seric 		if (e->e_message != NULL)
14263839Seric 			free(e->e_message);
14363839Seric 		e->e_message = newstr(buf);
14463839Seric 		message(buf);
14563839Seric 		e->e_flags |= EF_CLRQUEUE;
14663839Seric 		saveit = TRUE;
14763787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
14863787Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14963787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
15063787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15163787Seric 		{
15263787Seric 			if (bitset(QQUEUEUP, q->q_flags))
15363787Seric 				q->q_flags |= QBADADDR;
15463787Seric 		}
15563753Seric 	}
15663753Seric 	else if (TimeOuts.to_q_warning > 0 &&
15763753Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning)
15863753Seric 	{
15963753Seric 		if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
16063753Seric 		    e->e_class >= 0 &&
16163753Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0)
16263753Seric 		{
16363753Seric 			(void) sprintf(buf,
16463753Seric 				"warning: cannot send message for %s",
16563753Seric 				pintvl(TimeOuts.to_q_warning, FALSE));
16663753Seric 			if (e->e_message != NULL)
16763753Seric 				free(e->e_message);
16863753Seric 			e->e_message = newstr(buf);
16963753Seric 			message(buf);
17063839Seric 			e->e_flags |= EF_WARNING;
17163839Seric 			saveit = TRUE;
17263753Seric 		}
17363753Seric 		fprintf(e->e_xfp,
17463753Seric 			"Warning: message still undelivered after %s\n",
17563753Seric 			pintvl(TimeOuts.to_q_warning, FALSE));
17663753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
17763753Seric 			pintvl(TimeOuts.to_q_return, FALSE));
17863787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
17963787Seric 		{
18063787Seric 			if (bitset(QQUEUEUP, q->q_flags))
18163787Seric 				q->q_flags |= QREPORT;
18263787Seric 		}
18363753Seric 	}
18463753Seric 
18563753Seric 	/*
1869536Seric 	**  Send back return receipts as requested.
1879536Seric 	*/
1889536Seric 
1899536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1909536Seric 	{
19110844Seric 		auto ADDRESS *rlist = NULL;
1929536Seric 
19364284Seric 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e);
19455012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
19565054Seric 		e->e_flags &= ~EF_SENDRECEIPT;
1969536Seric 	}
1979536Seric 
1989536Seric 	/*
1999536Seric 	**  Arrange to send error messages if there are fatal errors.
2009536Seric 	*/
2019536Seric 
20263839Seric 	if (saveit && e->e_errormode != EM_QUIET)
2039536Seric 		savemail(e);
2049536Seric 
20564925Seric #ifdef XDEBUG
20664925Seric 	checkfd012("dropenvelope 2");
20764925Seric #endif
20864925Seric 
2099536Seric 	/*
21063849Seric 	**  Arrange to send warning messages to postmaster as requested.
21163849Seric 	*/
21263849Seric 
21363849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
21464363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
21563849Seric 	{
21663849Seric 		auto ADDRESS *rlist = NULL;
21763849Seric 
21864284Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e);
21963849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
22063849Seric 	}
22163849Seric 
22263849Seric 	/*
2239536Seric 	**  Instantiate or deinstantiate the queue.
2249536Seric 	*/
2259536Seric 
2269536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2279536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2289536Seric 	{
22964307Seric 		if (tTd(50, 1))
23064307Seric 			printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id);
23123497Seric 		if (e->e_df != NULL)
23223497Seric 			xunlink(e->e_df);
2339536Seric 		xunlink(queuename(e, 'q'));
23463839Seric 
23563839Seric #ifdef LOG
23663839Seric 		if (LogLevel > 10)
23763839Seric 			syslog(LOG_INFO, "%s: done", id);
23863839Seric #endif
2399536Seric 	}
2409536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
24110754Seric 	{
24210754Seric #ifdef QUEUE
24364307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
24456795Seric #else /* QUEUE */
24558151Seric 		syserr("554 dropenvelope: queueup");
24656795Seric #endif /* QUEUE */
24710754Seric 	}
2489536Seric 
24964925Seric #ifdef XDEBUG
25064925Seric 	checkfd012("dropenvelope 3");
25164925Seric #endif
25264925Seric 
2539536Seric 	/* now unlock the job */
25410196Seric 	closexscript(e);
2559536Seric 	unlockqueue(e);
2569536Seric 
2579536Seric 	/* make sure that this envelope is marked unused */
25824944Seric 	if (e->e_dfp != NULL)
25958680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
26010196Seric 	e->e_dfp = NULL;
26158680Seric 	e->e_id = e->e_df = NULL;
26264401Seric #ifdef XDEBUG
26364925Seric 	checkfd012("dropenvelope 4");
26464401Seric #endif
2659536Seric }
2669536Seric /*
2679536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
2689536Seric **
2699536Seric **	This is normally used by a child process to get a clean
2709536Seric **	envelope without disturbing the parent.
2719536Seric **
2729536Seric **	Parameters:
2739536Seric **		e -- the envelope to clear.
27425611Seric **		fullclear - if set, the current envelope is total
27525611Seric **			garbage and should be ignored; otherwise,
27625611Seric **			release any resources it may indicate.
2779536Seric **
2789536Seric **	Returns:
2799536Seric **		none.
2809536Seric **
2819536Seric **	Side Effects:
2829536Seric **		Closes files associated with the envelope.
2839536Seric **		Marks the envelope as unallocated.
2849536Seric */
2859536Seric 
28660494Seric void
28725611Seric clearenvelope(e, fullclear)
2889536Seric 	register ENVELOPE *e;
28925611Seric 	bool fullclear;
2909536Seric {
29125514Seric 	register HDR *bh;
29225514Seric 	register HDR **nhp;
29325514Seric 	extern ENVELOPE BlankEnvelope;
29425514Seric 
29525611Seric 	if (!fullclear)
29625611Seric 	{
29725611Seric 		/* clear out any file information */
29825611Seric 		if (e->e_xfp != NULL)
29958680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
30025611Seric 		if (e->e_dfp != NULL)
30158680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
30258680Seric 		e->e_xfp = e->e_dfp = NULL;
30325611Seric 	}
3049536Seric 
30524961Seric 	/* now clear out the data */
30624965Seric 	STRUCTCOPY(BlankEnvelope, *e);
30759698Seric 	if (Verbose)
30859698Seric 		e->e_sendmode = SM_DELIVER;
30925514Seric 	bh = BlankEnvelope.e_header;
31025514Seric 	nhp = &e->e_header;
31125514Seric 	while (bh != NULL)
31225514Seric 	{
31325514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
31425514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
31525514Seric 		bh = bh->h_link;
31625514Seric 		nhp = &(*nhp)->h_link;
31725514Seric 	}
3189536Seric }
3199536Seric /*
3209536Seric **  INITSYS -- initialize instantiation of system
3219536Seric **
3229536Seric **	In Daemon mode, this is done in the child.
3239536Seric **
3249536Seric **	Parameters:
3259536Seric **		none.
3269536Seric **
3279536Seric **	Returns:
3289536Seric **		none.
3299536Seric **
3309536Seric **	Side Effects:
3319536Seric **		Initializes the system macros, some global variables,
3329536Seric **		etc.  In particular, the current time in various
3339536Seric **		forms is set.
3349536Seric */
3359536Seric 
33660494Seric void
33755012Seric initsys(e)
33855012Seric 	register ENVELOPE *e;
3399536Seric {
34064768Seric 	char cbuf[5];				/* holds hop count */
34164768Seric 	char pbuf[10];				/* holds pid */
34222963Smiriam #ifdef TTYNAME
34359304Seric 	static char ybuf[60];			/* holds tty id */
3449536Seric 	register char *p;
34556795Seric #endif /* TTYNAME */
3469536Seric 	extern char *ttyname();
34760494Seric 	extern void settime();
3489536Seric 	extern char Version[];
3499536Seric 
3509536Seric 	/*
3519536Seric 	**  Give this envelope a reality.
3529536Seric 	**	I.e., an id, a transcript, and a creation time.
3539536Seric 	*/
3549536Seric 
35555012Seric 	openxscript(e);
35655012Seric 	e->e_ctime = curtime();
3579536Seric 
3589536Seric 	/*
3599536Seric 	**  Set OutChannel to something useful if stdout isn't it.
3609536Seric 	**	This arranges that any extra stuff the mailer produces
3619536Seric 	**	gets sent back to the user on error (because it is
3629536Seric 	**	tucked away in the transcript).
3639536Seric 	*/
3649536Seric 
36564760Seric 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
36658737Seric 	    e->e_xfp != NULL)
36755012Seric 		OutChannel = e->e_xfp;
3689536Seric 
3699536Seric 	/*
3709536Seric 	**  Set up some basic system macros.
3719536Seric 	*/
3729536Seric 
3739536Seric 	/* process id */
3749536Seric 	(void) sprintf(pbuf, "%d", getpid());
37564768Seric 	define('p', newstr(pbuf), e);
3769536Seric 
3779536Seric 	/* hop count */
37855012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
37964768Seric 	define('c', newstr(cbuf), e);
3809536Seric 
3819536Seric 	/* time as integer, unix time, arpa time */
38255012Seric 	settime(e);
3839536Seric 
38417472Seric #ifdef TTYNAME
3859536Seric 	/* tty name */
38655012Seric 	if (macvalue('y', e) == NULL)
3879536Seric 	{
3889536Seric 		p = ttyname(2);
3899536Seric 		if (p != NULL)
3909536Seric 		{
39156795Seric 			if (strrchr(p, '/') != NULL)
39256795Seric 				p = strrchr(p, '/') + 1;
3939536Seric 			(void) strcpy(ybuf, p);
39455012Seric 			define('y', ybuf, e);
3959536Seric 		}
3969536Seric 	}
39756795Seric #endif /* TTYNAME */
3989536Seric }
3999536Seric /*
40011932Seric **  SETTIME -- set the current time.
40111932Seric **
40211932Seric **	Parameters:
40311932Seric **		none.
40411932Seric **
40511932Seric **	Returns:
40611932Seric **		none.
40711932Seric **
40811932Seric **	Side Effects:
40911932Seric **		Sets the various time macros -- $a, $b, $d, $t.
41011932Seric */
41111932Seric 
41260494Seric void
41355012Seric settime(e)
41455012Seric 	register ENVELOPE *e;
41511932Seric {
41611932Seric 	register char *p;
41711932Seric 	auto time_t now;
41864768Seric 	char tbuf[20];				/* holds "current" time */
41964768Seric 	char dbuf[30];				/* holds ctime(tbuf) */
42011932Seric 	register struct tm *tm;
42111932Seric 	extern char *arpadate();
42211932Seric 	extern struct tm *gmtime();
42311932Seric 
42411932Seric 	now = curtime();
42511932Seric 	tm = gmtime(&now);
42657014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
42757014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
42864768Seric 	define('t', newstr(tbuf), e);
42911932Seric 	(void) strcpy(dbuf, ctime(&now));
43058131Seric 	p = strchr(dbuf, '\n');
43158131Seric 	if (p != NULL)
43258131Seric 		*p = '\0';
43364768Seric 	define('d', newstr(dbuf), e);
43464086Seric 	p = arpadate(dbuf);
43564086Seric 	p = newstr(p);
43655012Seric 	if (macvalue('a', e) == NULL)
43755012Seric 		define('a', p, e);
43855012Seric 	define('b', p, e);
43911932Seric }
44011932Seric /*
4419536Seric **  OPENXSCRIPT -- Open transcript file
4429536Seric **
4439536Seric **	Creates a transcript file for possible eventual mailing or
4449536Seric **	sending back.
4459536Seric **
4469536Seric **	Parameters:
4479536Seric **		e -- the envelope to create the transcript in/for.
4489536Seric **
4499536Seric **	Returns:
4509536Seric **		none
4519536Seric **
4529536Seric **	Side Effects:
4539536Seric **		Creates the transcript file.
4549536Seric */
4559536Seric 
45658803Seric #ifndef O_APPEND
45758803Seric #define O_APPEND	0
45858803Seric #endif
45958803Seric 
46060494Seric void
4619536Seric openxscript(e)
4629536Seric 	register ENVELOPE *e;
4639536Seric {
4649536Seric 	register char *p;
46540933Srick 	int fd;
4669536Seric 
4679536Seric 	if (e->e_xfp != NULL)
4689536Seric 		return;
4699536Seric 	p = queuename(e, 'x');
47058803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
47140933Srick 	if (fd < 0)
47263753Seric 	{
47363753Seric 		syserr("Can't create transcript file %s", p);
47463753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
47563753Seric 		if (fd < 0)
47663753Seric 			syserr("!Can't open /dev/null");
47763753Seric 	}
47863753Seric 	e->e_xfp = fdopen(fd, "w");
47964724Seric 	if (e->e_xfp == NULL)
48064724Seric 	{
48164724Seric 		syserr("!Can't create transcript stream %s", p);
48264724Seric 	}
48364743Seric 	if (tTd(46, 9))
48464743Seric 	{
48564743Seric 		printf("openxscript(%s):\n  ", p);
48664743Seric 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
48764743Seric 	}
4889536Seric }
4899536Seric /*
49010196Seric **  CLOSEXSCRIPT -- close the transcript file.
49110196Seric **
49210196Seric **	Parameters:
49310196Seric **		e -- the envelope containing the transcript to close.
49410196Seric **
49510196Seric **	Returns:
49610196Seric **		none.
49710196Seric **
49810196Seric **	Side Effects:
49910196Seric **		none.
50010196Seric */
50110196Seric 
50260494Seric void
50310196Seric closexscript(e)
50410196Seric 	register ENVELOPE *e;
50510196Seric {
50610196Seric 	if (e->e_xfp == NULL)
50710196Seric 		return;
50858680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
50910196Seric 	e->e_xfp = NULL;
51010196Seric }
51110196Seric /*
5129536Seric **  SETSENDER -- set the person who this message is from
5139536Seric **
5149536Seric **	Under certain circumstances allow the user to say who
5159536Seric **	s/he is (using -f or -r).  These are:
5169536Seric **	1.  The user's uid is zero (root).
5179536Seric **	2.  The user's login name is in an approved list (typically
5189536Seric **	    from a network server).
5199536Seric **	3.  The address the user is trying to claim has a
5209536Seric **	    "!" character in it (since #2 doesn't do it for
5219536Seric **	    us if we are dialing out for UUCP).
5229536Seric **	A better check to replace #3 would be if the
5239536Seric **	effective uid is "UUCP" -- this would require me
5249536Seric **	to rewrite getpwent to "grab" uucp as it went by,
5259536Seric **	make getname more nasty, do another passwd file
5269536Seric **	scan, or compile the UID of "UUCP" into the code,
5279536Seric **	all of which are reprehensible.
5289536Seric **
5299536Seric **	Assuming all of these fail, we figure out something
5309536Seric **	ourselves.
5319536Seric **
5329536Seric **	Parameters:
5339536Seric **		from -- the person we would like to believe this message
5349536Seric **			is from, as specified on the command line.
53553182Seric **		e -- the envelope in which we would like the sender set.
53658333Seric **		delimptr -- if non-NULL, set to the location of the
53758333Seric **			trailing delimiter.
53858704Seric **		internal -- set if this address is coming from an internal
53958704Seric **			source such as an owner alias.
5409536Seric **
5419536Seric **	Returns:
54258704Seric **		none.
5439536Seric **
5449536Seric **	Side Effects:
5459536Seric **		sets sendmail's notion of who the from person is.
5469536Seric */
5479536Seric 
54860494Seric void
54958704Seric setsender(from, e, delimptr, internal)
5509536Seric 	char *from;
55153182Seric 	register ENVELOPE *e;
55258333Seric 	char **delimptr;
55358704Seric 	bool internal;
5549536Seric {
5559536Seric 	register char **pvp;
5569536Seric 	char *realname = NULL;
55718665Seric 	register struct passwd *pw;
55858727Seric 	char delimchar;
55964147Seric 	char *bp;
56064147Seric 	char buf[MAXNAME + 2];
56116913Seric 	char pvpbuf[PSBUFSIZE];
56218665Seric 	extern struct passwd *getpwnam();
5639536Seric 	extern char *FullName;
5649536Seric 
5659536Seric 	if (tTd(45, 1))
56614786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5679536Seric 
5689536Seric 	/*
5699536Seric 	**  Figure out the real user executing us.
5709536Seric 	**	Username can return errno != 0 on non-errors.
5719536Seric 	*/
5729536Seric 
57358737Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
5749536Seric 		realname = from;
5759536Seric 	if (realname == NULL || realname[0] == '\0')
5769536Seric 		realname = username();
5779536Seric 
57859027Seric 	if (ConfigLevel < 2)
57959027Seric 		SuprErrs = TRUE;
58059027Seric 
58158727Seric 	delimchar = internal ? '\0' : ' ';
58264793Seric 	e->e_from.q_flags = QBADADDR;
58358333Seric 	if (from == NULL ||
58464284Seric 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
58564793Seric 		      delimchar, delimptr, e) == NULL ||
58664793Seric 	    bitset(QBADADDR, e->e_from.q_flags) ||
58764793Seric 	    e->e_from.q_mailer == ProgMailer ||
58864793Seric 	    e->e_from.q_mailer == FileMailer ||
58964793Seric 	    e->e_from.q_mailer == InclMailer)
5909536Seric 	{
59121750Seric 		/* log garbage addresses for traceback */
59255173Seric # ifdef LOG
59358020Seric 		if (from != NULL && LogLevel > 2)
59421750Seric 		{
59558951Seric 			char *p;
59658951Seric 			char ebuf[MAXNAME * 2 + 2];
59755173Seric 
59858951Seric 			p = macvalue('_', e);
59958951Seric 			if (p == NULL)
60058951Seric 			{
60158951Seric 				char *host = RealHostName;
60258951Seric 				if (host == NULL)
60358951Seric 					host = MyHostName;
60458951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
60558951Seric 				p = ebuf;
60658951Seric 			}
60755173Seric 			syslog(LOG_NOTICE,
60864793Seric 				"setsender: %s: invalid or unparseable, received from %s",
60965015Seric 				shortenstring(from, 83), p);
61055173Seric 		}
61156795Seric # endif /* LOG */
61257589Seric 		if (from != NULL)
61364793Seric 		{
61464793Seric 			if (!bitset(QBADADDR, e->e_from.q_flags))
61564793Seric 			{
61664793Seric 				/* it was a bogus mailer in the from addr */
61764793Seric 				usrerr("553 Invalid sender address");
61864793Seric 			}
61957589Seric 			SuprErrs = TRUE;
62064793Seric 		}
62157589Seric 		if (from == realname ||
62264284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
62364284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
62424944Seric 		{
62564793Seric 			char nbuf[100];
62664793Seric 
62757589Seric 			SuprErrs = TRUE;
62864808Seric 			expand("\201n", nbuf, &nbuf[sizeof nbuf], e);
62964793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
63064793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
63164793Seric 			    parseaddr(from = "postmaster", &e->e_from,
63264793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
63358151Seric 				syserr("553 setsender: can't even parse postmaster!");
63424944Seric 		}
6359536Seric 	}
6369536Seric 	else
6379536Seric 		FromFlag = TRUE;
63853182Seric 	e->e_from.q_flags |= QDONTSEND;
63957731Seric 	if (tTd(45, 5))
64057731Seric 	{
64157731Seric 		printf("setsender: QDONTSEND ");
64257731Seric 		printaddr(&e->e_from, FALSE);
64357731Seric 	}
6449536Seric 	SuprErrs = FALSE;
6459536Seric 
64653736Seric 	pvp = NULL;
64753736Seric 	if (e->e_from.q_mailer == LocalMailer)
6489536Seric 	{
64953736Seric # ifdef USERDB
65053736Seric 		register char *p;
65153736Seric 		extern char *udbsender();
65253736Seric # endif
65317472Seric 
65458704Seric 		if (!internal)
65558704Seric 		{
65658704Seric 			/* if the user has given fullname already, don't redefine */
65758704Seric 			if (FullName == NULL)
65858704Seric 				FullName = macvalue('x', e);
65958704Seric 			if (FullName != NULL && FullName[0] == '\0')
66058704Seric 				FullName = NULL;
6619536Seric 
66253736Seric # ifdef USERDB
66364284Seric 			p = udbsender(e->e_from.q_user);
66453736Seric 
66558704Seric 			if (p != NULL)
66658704Seric 			{
66758704Seric 				/*
66858704Seric 				**  We have an alternate address for the sender
66958704Seric 				*/
67053736Seric 
671*65066Seric 				pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL);
67258704Seric 			}
67358704Seric # endif /* USERDB */
6749536Seric 		}
67553736Seric 
67653736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
67753736Seric 		{
67853736Seric 			/*
67953736Seric 			**  Process passwd file entry.
68053736Seric 			*/
68153736Seric 
68253736Seric 
68353736Seric 			/* extract home directory */
68453736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
68553736Seric 			define('z', e->e_from.q_home, e);
68653736Seric 
68753736Seric 			/* extract user and group id */
68853736Seric 			e->e_from.q_uid = pw->pw_uid;
68953736Seric 			e->e_from.q_gid = pw->pw_gid;
69065023Seric 			e->e_from.q_flags |= QGOODUID;
69153736Seric 
69253736Seric 			/* extract full name from passwd file */
69353736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
69458704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
69558704Seric 			    !internal)
69653736Seric 			{
69765015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
69853736Seric 				if (buf[0] != '\0')
69953736Seric 					FullName = newstr(buf);
70053736Seric 			}
70153736Seric 		}
70258704Seric 		if (FullName != NULL && !internal)
70353182Seric 			define('x', FullName, e);
7049536Seric 	}
70558704Seric 	else if (!internal)
70611625Seric 	{
70753182Seric 		if (e->e_from.q_home == NULL)
70853182Seric 			e->e_from.q_home = getenv("HOME");
70963787Seric 		e->e_from.q_uid = RealUid;
71063787Seric 		e->e_from.q_gid = RealGid;
71165023Seric 		e->e_from.q_flags |= QGOODUID;
71211625Seric 	}
71311625Seric 
7149536Seric 	/*
7159536Seric 	**  Rewrite the from person to dispose of possible implicit
7169536Seric 	**	links in the net.
7179536Seric 	*/
7189536Seric 
7199536Seric 	if (pvp == NULL)
720*65066Seric 		pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
72153736Seric 	if (pvp == NULL)
7229536Seric 	{
72358403Seric 		/* don't need to give error -- prescan did that already */
72436233Skarels # ifdef LOG
72558020Seric 		if (LogLevel > 2)
72636233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
72736233Skarels # endif
7289536Seric 		finis();
7299536Seric 	}
73059084Seric 	(void) rewrite(pvp, 3, e);
73159084Seric 	(void) rewrite(pvp, 1, e);
73259084Seric 	(void) rewrite(pvp, 4, e);
73364147Seric 	bp = buf + 1;
73464147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
73564147Seric 	if (*bp == '@')
73664147Seric 	{
73764147Seric 		/* heuristic: route-addr: add angle brackets */
73864147Seric 		strcat(bp, ">");
73964147Seric 		*--bp = '<';
74064147Seric 	}
74164147Seric 	e->e_sender = newstr(bp);
74258704Seric 	define('f', e->e_sender, e);
7439536Seric 
7449536Seric 	/* save the domain spec if this mailer wants it */
74558704Seric 	if (!internal && e->e_from.q_mailer != NULL &&
74653182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
7479536Seric 	{
7489536Seric 		extern char **copyplist();
7499536Seric 
7509536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
7519536Seric 			pvp++;
7529536Seric 		if (*pvp != NULL)
75353182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
7549536Seric 	}
7559536Seric }
756