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*65580Seric static char sccsid[] = "@(#)envelope.c	8.27 (Berkeley) 01/08/94";
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);
8765089Seric 		printf(", flags=0x%x\n", e->e_flags);
8863753Seric 		if (tTd(50, 10))
8963753Seric 		{
9063753Seric 			printf("sendq=");
9163753Seric 			printaddr(e->e_sendqueue, TRUE);
9263753Seric 		}
939536Seric 	}
9457943Seric 
9558680Seric 	/* we must have an id to remove disk files */
9657943Seric 	if (id == NULL)
9758680Seric 		return;
9857943Seric 
999536Seric #ifdef LOG
10065089Seric 	if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags))
10165089Seric 		logsender(e, NULL);
10258020Seric 	if (LogLevel > 84)
10365089Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d",
10457943Seric 				  id, e->e_flags, getpid());
10556795Seric #endif /* LOG */
10665089Seric 	e->e_flags &= ~EF_LOGSENDER;
1079536Seric 
10863753Seric 	/* post statistics */
10963753Seric 	poststats(StatFile);
11063753Seric 
1119536Seric 	/*
1129536Seric 	**  Extract state information from dregs of send list.
1139536Seric 	*/
1149536Seric 
11564743Seric 	e->e_flags &= ~EF_QUEUERUN;
1169536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1179536Seric 	{
1189536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1199536Seric 			queueit = TRUE;
12063839Seric 		if (!bitset(QDONTSEND, q->q_flags) &&
12163839Seric 		    bitset(QBADADDR, q->q_flags))
12263839Seric 		{
12363839Seric 			if (q->q_owner == NULL &&
12463839Seric 			    strcmp(e->e_from.q_paddr, "<>") != 0)
12563839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
12663839Seric 						  &e->e_errorqueue, e);
12763839Seric 		}
1289536Seric 	}
1299536Seric 
1309536Seric 	/*
13163753Seric 	**  See if the message timed out.
13263753Seric 	*/
13363753Seric 
13463753Seric 	if (!queueit)
13563753Seric 		/* nothing to do */ ;
13663753Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
13763753Seric 	{
13863839Seric 		(void) sprintf(buf, "Cannot send message for %s",
13963839Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14063839Seric 		if (e->e_message != NULL)
14163839Seric 			free(e->e_message);
14263839Seric 		e->e_message = newstr(buf);
14363839Seric 		message(buf);
14463839Seric 		e->e_flags |= EF_CLRQUEUE;
14563839Seric 		saveit = TRUE;
14663787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
14763787Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14863787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
14963787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
15063787Seric 		{
15163787Seric 			if (bitset(QQUEUEUP, q->q_flags))
15263787Seric 				q->q_flags |= QBADADDR;
15363787Seric 		}
15463753Seric 	}
15563753Seric 	else if (TimeOuts.to_q_warning > 0 &&
15663753Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning)
15763753Seric 	{
15863753Seric 		if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
15963753Seric 		    e->e_class >= 0 &&
16063753Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0)
16163753Seric 		{
16263753Seric 			(void) sprintf(buf,
16363753Seric 				"warning: cannot send message for %s",
16463753Seric 				pintvl(TimeOuts.to_q_warning, FALSE));
16563753Seric 			if (e->e_message != NULL)
16663753Seric 				free(e->e_message);
16763753Seric 			e->e_message = newstr(buf);
16863753Seric 			message(buf);
16963839Seric 			e->e_flags |= EF_WARNING;
17063839Seric 			saveit = TRUE;
17163753Seric 		}
17263753Seric 		fprintf(e->e_xfp,
17363753Seric 			"Warning: message still undelivered after %s\n",
17463753Seric 			pintvl(TimeOuts.to_q_warning, FALSE));
17563753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
17663753Seric 			pintvl(TimeOuts.to_q_return, FALSE));
17763787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
17863787Seric 		{
17963787Seric 			if (bitset(QQUEUEUP, q->q_flags))
18063787Seric 				q->q_flags |= QREPORT;
18163787Seric 		}
18263753Seric 	}
18363753Seric 
18463753Seric 	/*
1859536Seric 	**  Send back return receipts as requested.
1869536Seric 	*/
1879536Seric 
1889536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1899536Seric 	{
19010844Seric 		auto ADDRESS *rlist = NULL;
1919536Seric 
19264284Seric 		(void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e);
19355012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
19465054Seric 		e->e_flags &= ~EF_SENDRECEIPT;
1959536Seric 	}
1969536Seric 
1979536Seric 	/*
1989536Seric 	**  Arrange to send error messages if there are fatal errors.
1999536Seric 	*/
2009536Seric 
20163839Seric 	if (saveit && e->e_errormode != EM_QUIET)
2029536Seric 		savemail(e);
2039536Seric 
2049536Seric 	/*
20563849Seric 	**  Arrange to send warning messages to postmaster as requested.
20663849Seric 	*/
20763849Seric 
20863849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
20964363Seric 	    !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0)
21063849Seric 	{
21163849Seric 		auto ADDRESS *rlist = NULL;
21263849Seric 
21364284Seric 		(void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e);
21463849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
21563849Seric 	}
21663849Seric 
21763849Seric 	/*
2189536Seric 	**  Instantiate or deinstantiate the queue.
2199536Seric 	*/
2209536Seric 
2219536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2229536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2239536Seric 	{
22464307Seric 		if (tTd(50, 1))
22564307Seric 			printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id);
22623497Seric 		if (e->e_df != NULL)
22723497Seric 			xunlink(e->e_df);
2289536Seric 		xunlink(queuename(e, 'q'));
22963839Seric 
23063839Seric #ifdef LOG
23163839Seric 		if (LogLevel > 10)
23263839Seric 			syslog(LOG_INFO, "%s: done", id);
23363839Seric #endif
2349536Seric 	}
2359536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
23610754Seric 	{
23710754Seric #ifdef QUEUE
23864307Seric 		queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE);
23956795Seric #else /* QUEUE */
24058151Seric 		syserr("554 dropenvelope: queueup");
24156795Seric #endif /* QUEUE */
24210754Seric 	}
2439536Seric 
2449536Seric 	/* now unlock the job */
24510196Seric 	closexscript(e);
2469536Seric 	unlockqueue(e);
2479536Seric 
2489536Seric 	/* make sure that this envelope is marked unused */
24924944Seric 	if (e->e_dfp != NULL)
25058680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
25110196Seric 	e->e_dfp = NULL;
25258680Seric 	e->e_id = e->e_df = NULL;
2539536Seric }
2549536Seric /*
2559536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
2569536Seric **
2579536Seric **	This is normally used by a child process to get a clean
2589536Seric **	envelope without disturbing the parent.
2599536Seric **
2609536Seric **	Parameters:
2619536Seric **		e -- the envelope to clear.
26225611Seric **		fullclear - if set, the current envelope is total
26325611Seric **			garbage and should be ignored; otherwise,
26425611Seric **			release any resources it may indicate.
2659536Seric **
2669536Seric **	Returns:
2679536Seric **		none.
2689536Seric **
2699536Seric **	Side Effects:
2709536Seric **		Closes files associated with the envelope.
2719536Seric **		Marks the envelope as unallocated.
2729536Seric */
2739536Seric 
27460494Seric void
27525611Seric clearenvelope(e, fullclear)
2769536Seric 	register ENVELOPE *e;
27725611Seric 	bool fullclear;
2789536Seric {
27925514Seric 	register HDR *bh;
28025514Seric 	register HDR **nhp;
28125514Seric 	extern ENVELOPE BlankEnvelope;
28225514Seric 
28325611Seric 	if (!fullclear)
28425611Seric 	{
28525611Seric 		/* clear out any file information */
28625611Seric 		if (e->e_xfp != NULL)
28758680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
28825611Seric 		if (e->e_dfp != NULL)
28958680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
29058680Seric 		e->e_xfp = e->e_dfp = NULL;
29125611Seric 	}
2929536Seric 
29324961Seric 	/* now clear out the data */
29424965Seric 	STRUCTCOPY(BlankEnvelope, *e);
29559698Seric 	if (Verbose)
29659698Seric 		e->e_sendmode = SM_DELIVER;
29725514Seric 	bh = BlankEnvelope.e_header;
29825514Seric 	nhp = &e->e_header;
29925514Seric 	while (bh != NULL)
30025514Seric 	{
30125514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
30225514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
30325514Seric 		bh = bh->h_link;
30425514Seric 		nhp = &(*nhp)->h_link;
30525514Seric 	}
3069536Seric }
3079536Seric /*
3089536Seric **  INITSYS -- initialize instantiation of system
3099536Seric **
3109536Seric **	In Daemon mode, this is done in the child.
3119536Seric **
3129536Seric **	Parameters:
3139536Seric **		none.
3149536Seric **
3159536Seric **	Returns:
3169536Seric **		none.
3179536Seric **
3189536Seric **	Side Effects:
3199536Seric **		Initializes the system macros, some global variables,
3209536Seric **		etc.  In particular, the current time in various
3219536Seric **		forms is set.
3229536Seric */
3239536Seric 
32460494Seric void
32555012Seric initsys(e)
32655012Seric 	register ENVELOPE *e;
3279536Seric {
32864768Seric 	char cbuf[5];				/* holds hop count */
32964768Seric 	char pbuf[10];				/* holds pid */
33022963Smiriam #ifdef TTYNAME
33159304Seric 	static char ybuf[60];			/* holds tty id */
3329536Seric 	register char *p;
33356795Seric #endif /* TTYNAME */
3349536Seric 	extern char *ttyname();
33560494Seric 	extern void settime();
3369536Seric 	extern char Version[];
3379536Seric 
3389536Seric 	/*
3399536Seric 	**  Give this envelope a reality.
3409536Seric 	**	I.e., an id, a transcript, and a creation time.
3419536Seric 	*/
3429536Seric 
34355012Seric 	openxscript(e);
34455012Seric 	e->e_ctime = curtime();
3459536Seric 
3469536Seric 	/*
3479536Seric 	**  Set OutChannel to something useful if stdout isn't it.
3489536Seric 	**	This arranges that any extra stuff the mailer produces
3499536Seric 	**	gets sent back to the user on error (because it is
3509536Seric 	**	tucked away in the transcript).
3519536Seric 	*/
3529536Seric 
35364760Seric 	if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) &&
35458737Seric 	    e->e_xfp != NULL)
35555012Seric 		OutChannel = e->e_xfp;
3569536Seric 
3579536Seric 	/*
3589536Seric 	**  Set up some basic system macros.
3599536Seric 	*/
3609536Seric 
3619536Seric 	/* process id */
3629536Seric 	(void) sprintf(pbuf, "%d", getpid());
36364768Seric 	define('p', newstr(pbuf), e);
3649536Seric 
3659536Seric 	/* hop count */
36655012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
36764768Seric 	define('c', newstr(cbuf), e);
3689536Seric 
3699536Seric 	/* time as integer, unix time, arpa time */
37055012Seric 	settime(e);
3719536Seric 
37217472Seric #ifdef TTYNAME
3739536Seric 	/* tty name */
37455012Seric 	if (macvalue('y', e) == NULL)
3759536Seric 	{
3769536Seric 		p = ttyname(2);
3779536Seric 		if (p != NULL)
3789536Seric 		{
37956795Seric 			if (strrchr(p, '/') != NULL)
38056795Seric 				p = strrchr(p, '/') + 1;
3819536Seric 			(void) strcpy(ybuf, p);
38255012Seric 			define('y', ybuf, e);
3839536Seric 		}
3849536Seric 	}
38556795Seric #endif /* TTYNAME */
3869536Seric }
3879536Seric /*
38811932Seric **  SETTIME -- set the current time.
38911932Seric **
39011932Seric **	Parameters:
39111932Seric **		none.
39211932Seric **
39311932Seric **	Returns:
39411932Seric **		none.
39511932Seric **
39611932Seric **	Side Effects:
39711932Seric **		Sets the various time macros -- $a, $b, $d, $t.
39811932Seric */
39911932Seric 
40060494Seric void
40155012Seric settime(e)
40255012Seric 	register ENVELOPE *e;
40311932Seric {
40411932Seric 	register char *p;
40511932Seric 	auto time_t now;
40664768Seric 	char tbuf[20];				/* holds "current" time */
40764768Seric 	char dbuf[30];				/* holds ctime(tbuf) */
40811932Seric 	register struct tm *tm;
40911932Seric 	extern char *arpadate();
41011932Seric 	extern struct tm *gmtime();
41111932Seric 
41211932Seric 	now = curtime();
41311932Seric 	tm = gmtime(&now);
41457014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
41557014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
41664768Seric 	define('t', newstr(tbuf), e);
41711932Seric 	(void) strcpy(dbuf, ctime(&now));
41858131Seric 	p = strchr(dbuf, '\n');
41958131Seric 	if (p != NULL)
42058131Seric 		*p = '\0';
42164768Seric 	define('d', newstr(dbuf), e);
42264086Seric 	p = arpadate(dbuf);
42364086Seric 	p = newstr(p);
42455012Seric 	if (macvalue('a', e) == NULL)
42555012Seric 		define('a', p, e);
42655012Seric 	define('b', p, e);
42711932Seric }
42811932Seric /*
4299536Seric **  OPENXSCRIPT -- Open transcript file
4309536Seric **
4319536Seric **	Creates a transcript file for possible eventual mailing or
4329536Seric **	sending back.
4339536Seric **
4349536Seric **	Parameters:
4359536Seric **		e -- the envelope to create the transcript in/for.
4369536Seric **
4379536Seric **	Returns:
4389536Seric **		none
4399536Seric **
4409536Seric **	Side Effects:
4419536Seric **		Creates the transcript file.
4429536Seric */
4439536Seric 
44458803Seric #ifndef O_APPEND
44558803Seric #define O_APPEND	0
44658803Seric #endif
44758803Seric 
44860494Seric void
4499536Seric openxscript(e)
4509536Seric 	register ENVELOPE *e;
4519536Seric {
4529536Seric 	register char *p;
45340933Srick 	int fd;
4549536Seric 
4559536Seric 	if (e->e_xfp != NULL)
4569536Seric 		return;
4579536Seric 	p = queuename(e, 'x');
45858803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
45940933Srick 	if (fd < 0)
46063753Seric 	{
46163753Seric 		syserr("Can't create transcript file %s", p);
46263753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
46363753Seric 		if (fd < 0)
46463753Seric 			syserr("!Can't open /dev/null");
46563753Seric 	}
46663753Seric 	e->e_xfp = fdopen(fd, "w");
46764724Seric 	if (e->e_xfp == NULL)
46864724Seric 	{
46964724Seric 		syserr("!Can't create transcript stream %s", p);
47064724Seric 	}
47164743Seric 	if (tTd(46, 9))
47264743Seric 	{
47364743Seric 		printf("openxscript(%s):\n  ", p);
47464743Seric 		dumpfd(fileno(e->e_xfp), TRUE, FALSE);
47564743Seric 	}
4769536Seric }
4779536Seric /*
47810196Seric **  CLOSEXSCRIPT -- close the transcript file.
47910196Seric **
48010196Seric **	Parameters:
48110196Seric **		e -- the envelope containing the transcript to close.
48210196Seric **
48310196Seric **	Returns:
48410196Seric **		none.
48510196Seric **
48610196Seric **	Side Effects:
48710196Seric **		none.
48810196Seric */
48910196Seric 
49060494Seric void
49110196Seric closexscript(e)
49210196Seric 	register ENVELOPE *e;
49310196Seric {
49410196Seric 	if (e->e_xfp == NULL)
49510196Seric 		return;
49658680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
49710196Seric 	e->e_xfp = NULL;
49810196Seric }
49910196Seric /*
5009536Seric **  SETSENDER -- set the person who this message is from
5019536Seric **
5029536Seric **	Under certain circumstances allow the user to say who
5039536Seric **	s/he is (using -f or -r).  These are:
5049536Seric **	1.  The user's uid is zero (root).
5059536Seric **	2.  The user's login name is in an approved list (typically
5069536Seric **	    from a network server).
5079536Seric **	3.  The address the user is trying to claim has a
5089536Seric **	    "!" character in it (since #2 doesn't do it for
5099536Seric **	    us if we are dialing out for UUCP).
5109536Seric **	A better check to replace #3 would be if the
5119536Seric **	effective uid is "UUCP" -- this would require me
5129536Seric **	to rewrite getpwent to "grab" uucp as it went by,
5139536Seric **	make getname more nasty, do another passwd file
5149536Seric **	scan, or compile the UID of "UUCP" into the code,
5159536Seric **	all of which are reprehensible.
5169536Seric **
5179536Seric **	Assuming all of these fail, we figure out something
5189536Seric **	ourselves.
5199536Seric **
5209536Seric **	Parameters:
5219536Seric **		from -- the person we would like to believe this message
5229536Seric **			is from, as specified on the command line.
52353182Seric **		e -- the envelope in which we would like the sender set.
52458333Seric **		delimptr -- if non-NULL, set to the location of the
52558333Seric **			trailing delimiter.
52658704Seric **		internal -- set if this address is coming from an internal
52758704Seric **			source such as an owner alias.
5289536Seric **
5299536Seric **	Returns:
53058704Seric **		none.
5319536Seric **
5329536Seric **	Side Effects:
5339536Seric **		sets sendmail's notion of who the from person is.
5349536Seric */
5359536Seric 
53660494Seric void
53758704Seric setsender(from, e, delimptr, internal)
5389536Seric 	char *from;
53953182Seric 	register ENVELOPE *e;
54058333Seric 	char **delimptr;
54158704Seric 	bool internal;
5429536Seric {
5439536Seric 	register char **pvp;
5449536Seric 	char *realname = NULL;
54518665Seric 	register struct passwd *pw;
54658727Seric 	char delimchar;
54764147Seric 	char *bp;
54864147Seric 	char buf[MAXNAME + 2];
54916913Seric 	char pvpbuf[PSBUFSIZE];
55018665Seric 	extern struct passwd *getpwnam();
5519536Seric 	extern char *FullName;
5529536Seric 
5539536Seric 	if (tTd(45, 1))
55414786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5559536Seric 
5569536Seric 	/*
5579536Seric 	**  Figure out the real user executing us.
5589536Seric 	**	Username can return errno != 0 on non-errors.
5599536Seric 	*/
5609536Seric 
561*65580Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP ||
562*65580Seric 	    OpMode == MD_DAEMON)
5639536Seric 		realname = from;
5649536Seric 	if (realname == NULL || realname[0] == '\0')
5659536Seric 		realname = username();
5669536Seric 
56759027Seric 	if (ConfigLevel < 2)
56859027Seric 		SuprErrs = TRUE;
56959027Seric 
57058727Seric 	delimchar = internal ? '\0' : ' ';
57164793Seric 	e->e_from.q_flags = QBADADDR;
57258333Seric 	if (from == NULL ||
57364284Seric 	    parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR,
57464793Seric 		      delimchar, delimptr, e) == NULL ||
57564793Seric 	    bitset(QBADADDR, e->e_from.q_flags) ||
57664793Seric 	    e->e_from.q_mailer == ProgMailer ||
57764793Seric 	    e->e_from.q_mailer == FileMailer ||
57864793Seric 	    e->e_from.q_mailer == InclMailer)
5799536Seric 	{
58021750Seric 		/* log garbage addresses for traceback */
58155173Seric # ifdef LOG
58258020Seric 		if (from != NULL && LogLevel > 2)
58321750Seric 		{
58458951Seric 			char *p;
58558951Seric 			char ebuf[MAXNAME * 2 + 2];
58655173Seric 
58758951Seric 			p = macvalue('_', e);
58858951Seric 			if (p == NULL)
58958951Seric 			{
59058951Seric 				char *host = RealHostName;
59158951Seric 				if (host == NULL)
59258951Seric 					host = MyHostName;
59358951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
59458951Seric 				p = ebuf;
59558951Seric 			}
59655173Seric 			syslog(LOG_NOTICE,
59764793Seric 				"setsender: %s: invalid or unparseable, received from %s",
59865015Seric 				shortenstring(from, 83), p);
59955173Seric 		}
60056795Seric # endif /* LOG */
60157589Seric 		if (from != NULL)
60264793Seric 		{
60364793Seric 			if (!bitset(QBADADDR, e->e_from.q_flags))
60464793Seric 			{
60564793Seric 				/* it was a bogus mailer in the from addr */
60664793Seric 				usrerr("553 Invalid sender address");
60764793Seric 			}
60857589Seric 			SuprErrs = TRUE;
60964793Seric 		}
61057589Seric 		if (from == realname ||
61164284Seric 		    parseaddr(from = newstr(realname), &e->e_from,
61264284Seric 			      RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL)
61324944Seric 		{
61464793Seric 			char nbuf[100];
61564793Seric 
61657589Seric 			SuprErrs = TRUE;
61764808Seric 			expand("\201n", nbuf, &nbuf[sizeof nbuf], e);
61864793Seric 			if (parseaddr(from = newstr(nbuf), &e->e_from,
61964793Seric 				      RF_COPYALL, ' ', NULL, e) == NULL &&
62064793Seric 			    parseaddr(from = "postmaster", &e->e_from,
62164793Seric 			    	      RF_COPYALL, ' ', NULL, e) == NULL)
62258151Seric 				syserr("553 setsender: can't even parse postmaster!");
62324944Seric 		}
6249536Seric 	}
6259536Seric 	else
6269536Seric 		FromFlag = TRUE;
62753182Seric 	e->e_from.q_flags |= QDONTSEND;
62857731Seric 	if (tTd(45, 5))
62957731Seric 	{
63057731Seric 		printf("setsender: QDONTSEND ");
63157731Seric 		printaddr(&e->e_from, FALSE);
63257731Seric 	}
6339536Seric 	SuprErrs = FALSE;
6349536Seric 
63553736Seric 	pvp = NULL;
63653736Seric 	if (e->e_from.q_mailer == LocalMailer)
6379536Seric 	{
63853736Seric # ifdef USERDB
63953736Seric 		register char *p;
64053736Seric 		extern char *udbsender();
64153736Seric # endif
64217472Seric 
64358704Seric 		if (!internal)
64458704Seric 		{
64558704Seric 			/* if the user has given fullname already, don't redefine */
64658704Seric 			if (FullName == NULL)
64758704Seric 				FullName = macvalue('x', e);
64858704Seric 			if (FullName != NULL && FullName[0] == '\0')
64958704Seric 				FullName = NULL;
6509536Seric 
65153736Seric # ifdef USERDB
65264284Seric 			p = udbsender(e->e_from.q_user);
65353736Seric 
65458704Seric 			if (p != NULL)
65558704Seric 			{
65658704Seric 				/*
65758704Seric 				**  We have an alternate address for the sender
65858704Seric 				*/
65953736Seric 
66065066Seric 				pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL);
66158704Seric 			}
66258704Seric # endif /* USERDB */
6639536Seric 		}
66453736Seric 
66553736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
66653736Seric 		{
66753736Seric 			/*
66853736Seric 			**  Process passwd file entry.
66953736Seric 			*/
67053736Seric 
67153736Seric 
67253736Seric 			/* extract home directory */
67353736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
67453736Seric 			define('z', e->e_from.q_home, e);
67553736Seric 
67653736Seric 			/* extract user and group id */
67753736Seric 			e->e_from.q_uid = pw->pw_uid;
67853736Seric 			e->e_from.q_gid = pw->pw_gid;
67965023Seric 			e->e_from.q_flags |= QGOODUID;
68053736Seric 
68153736Seric 			/* extract full name from passwd file */
68253736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
68358704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
68458704Seric 			    !internal)
68553736Seric 			{
68665015Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
68753736Seric 				if (buf[0] != '\0')
68853736Seric 					FullName = newstr(buf);
68953736Seric 			}
69053736Seric 		}
69158704Seric 		if (FullName != NULL && !internal)
69253182Seric 			define('x', FullName, e);
6939536Seric 	}
694*65580Seric 	else if (!internal && OpMode != MD_DAEMON)
69511625Seric 	{
69653182Seric 		if (e->e_from.q_home == NULL)
69753182Seric 			e->e_from.q_home = getenv("HOME");
69863787Seric 		e->e_from.q_uid = RealUid;
69963787Seric 		e->e_from.q_gid = RealGid;
70065023Seric 		e->e_from.q_flags |= QGOODUID;
70111625Seric 	}
70211625Seric 
7039536Seric 	/*
7049536Seric 	**  Rewrite the from person to dispose of possible implicit
7059536Seric 	**	links in the net.
7069536Seric 	*/
7079536Seric 
7089536Seric 	if (pvp == NULL)
70965066Seric 		pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL);
71053736Seric 	if (pvp == NULL)
7119536Seric 	{
71258403Seric 		/* don't need to give error -- prescan did that already */
71336233Skarels # ifdef LOG
71458020Seric 		if (LogLevel > 2)
71536233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
71636233Skarels # endif
7179536Seric 		finis();
7189536Seric 	}
71965071Seric 	(void) rewrite(pvp, 3, 0, e);
72065071Seric 	(void) rewrite(pvp, 1, 0, e);
72165071Seric 	(void) rewrite(pvp, 4, 0, e);
72264147Seric 	bp = buf + 1;
72364147Seric 	cataddr(pvp, NULL, bp, sizeof buf - 2, '\0');
72464147Seric 	if (*bp == '@')
72564147Seric 	{
72664147Seric 		/* heuristic: route-addr: add angle brackets */
72764147Seric 		strcat(bp, ">");
72864147Seric 		*--bp = '<';
72964147Seric 	}
73064147Seric 	e->e_sender = newstr(bp);
73158704Seric 	define('f', e->e_sender, e);
7329536Seric 
7339536Seric 	/* save the domain spec if this mailer wants it */
73458704Seric 	if (!internal && e->e_from.q_mailer != NULL &&
73553182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
7369536Seric 	{
7379536Seric 		extern char **copyplist();
7389536Seric 
7399536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
7409536Seric 			pvp++;
7419536Seric 		if (*pvp != NULL)
74253182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
7439536Seric 	}
7449536Seric }
745