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*63849Seric static char sccsid[] = "@(#)envelope.c	8.5 (Berkeley) 07/17/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 
9558680Seric 	/* we must have an id to remove disk files */
9657943Seric 	if (id == NULL)
9758680Seric 		return;
9857943Seric 
999536Seric #ifdef LOG
10058020Seric 	if (LogLevel > 84)
1019536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
10257943Seric 				  id, e->e_flags, getpid());
10356795Seric #endif /* LOG */
1049536Seric 
10563753Seric 	/* post statistics */
10663753Seric 	poststats(StatFile);
10763753Seric 
1089536Seric 	/*
1099536Seric 	**  Extract state information from dregs of send list.
1109536Seric 	*/
1119536Seric 
1129536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1139536Seric 	{
1149536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1159536Seric 			queueit = TRUE;
11663839Seric 		if (!bitset(QDONTSEND, q->q_flags) &&
11763839Seric 		    bitset(QBADADDR, q->q_flags))
11863839Seric 		{
11963839Seric 			if (q->q_owner == NULL &&
12063839Seric 			    strcmp(e->e_from.q_paddr, "<>") != 0)
12163839Seric 				(void) sendtolist(e->e_from.q_paddr, NULL,
12263839Seric 						  &e->e_errorqueue, e);
12363839Seric 		}
1249536Seric 	}
1259536Seric 
1269536Seric 	/*
12763753Seric 	**  See if the message timed out.
12863753Seric 	*/
12963753Seric 
13063753Seric 	if (!queueit)
13163753Seric 		/* nothing to do */ ;
13263753Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
13363753Seric 	{
13463839Seric 		(void) sprintf(buf, "Cannot send message for %s",
13563839Seric 			pintvl(TimeOuts.to_q_return, FALSE));
13663839Seric 		if (e->e_message != NULL)
13763839Seric 			free(e->e_message);
13863839Seric 		e->e_message = newstr(buf);
13963839Seric 		message(buf);
14063839Seric 		e->e_flags |= EF_CLRQUEUE;
14163839Seric 		saveit = TRUE;
14263787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
14363787Seric 			pintvl(TimeOuts.to_q_return, FALSE));
14463787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
14563787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14663787Seric 		{
14763787Seric 			if (bitset(QQUEUEUP, q->q_flags))
14863787Seric 				q->q_flags |= QBADADDR;
14963787Seric 		}
15063753Seric 	}
15163753Seric 	else if (TimeOuts.to_q_warning > 0 &&
15263753Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning)
15363753Seric 	{
15463753Seric 		if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
15563753Seric 		    e->e_class >= 0 &&
15663753Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0)
15763753Seric 		{
15863753Seric 			(void) sprintf(buf,
15963753Seric 				"warning: cannot send message for %s",
16063753Seric 				pintvl(TimeOuts.to_q_warning, FALSE));
16163753Seric 			if (e->e_message != NULL)
16263753Seric 				free(e->e_message);
16363753Seric 			e->e_message = newstr(buf);
16463753Seric 			message(buf);
16563839Seric 			e->e_flags |= EF_WARNING;
16663839Seric 			saveit = TRUE;
16763753Seric 		}
16863753Seric 		fprintf(e->e_xfp,
16963753Seric 			"Warning: message still undelivered after %s\n",
17063753Seric 			pintvl(TimeOuts.to_q_warning, FALSE));
17163753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
17263753Seric 			pintvl(TimeOuts.to_q_return, FALSE));
17363787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
17463787Seric 		{
17563787Seric 			if (bitset(QQUEUEUP, q->q_flags))
17663787Seric 				q->q_flags |= QREPORT;
17763787Seric 		}
17863753Seric 	}
17963753Seric 
18063753Seric 	/*
1819536Seric 	**  Send back return receipts as requested.
1829536Seric 	*/
1839536Seric 
1849536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1859536Seric 	{
18610844Seric 		auto ADDRESS *rlist = NULL;
1879536Seric 
18858082Seric 		(void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e);
18955012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
1909536Seric 	}
1919536Seric 
1929536Seric 	/*
1939536Seric 	**  Arrange to send error messages if there are fatal errors.
1949536Seric 	*/
1959536Seric 
19663839Seric 	if (saveit && e->e_errormode != EM_QUIET)
1979536Seric 		savemail(e);
1989536Seric 
1999536Seric 	/*
200*63849Seric 	**  Arrange to send warning messages to postmaster as requested.
201*63849Seric 	*/
202*63849Seric 
203*63849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
204*63849Seric 	    !bitset(EF_RESPONSE, e->e_flags))
205*63849Seric 	{
206*63849Seric 		auto ADDRESS *rlist = NULL;
207*63849Seric 
208*63849Seric 		(void) sendtolist(PostMasterCopy, (ADDRESS *) NULL, &rlist, e);
209*63849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
210*63849Seric 	}
211*63849Seric 
212*63849Seric 	/*
2139536Seric 	**  Instantiate or deinstantiate the queue.
2149536Seric 	*/
2159536Seric 
2169536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
2179536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
2189536Seric 	{
21963753Seric 		if (tTd(50, 2))
22063753Seric 			printf("Dropping envelope\n");
22123497Seric 		if (e->e_df != NULL)
22223497Seric 			xunlink(e->e_df);
2239536Seric 		xunlink(queuename(e, 'q'));
22463839Seric 
22563839Seric #ifdef LOG
22663839Seric 		if (LogLevel > 10)
22763839Seric 			syslog(LOG_INFO, "%s: done", id);
22863839Seric #endif
2299536Seric 	}
2309536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
23110754Seric 	{
23210754Seric #ifdef QUEUE
23351920Seric 		queueup(e, FALSE, FALSE);
23456795Seric #else /* QUEUE */
23558151Seric 		syserr("554 dropenvelope: queueup");
23656795Seric #endif /* QUEUE */
23710754Seric 	}
2389536Seric 
2399536Seric 	/* now unlock the job */
24010196Seric 	closexscript(e);
2419536Seric 	unlockqueue(e);
2429536Seric 
2439536Seric 	/* make sure that this envelope is marked unused */
24424944Seric 	if (e->e_dfp != NULL)
24558680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
24610196Seric 	e->e_dfp = NULL;
24758680Seric 	e->e_id = e->e_df = NULL;
2489536Seric }
2499536Seric /*
2509536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
2519536Seric **
2529536Seric **	This is normally used by a child process to get a clean
2539536Seric **	envelope without disturbing the parent.
2549536Seric **
2559536Seric **	Parameters:
2569536Seric **		e -- the envelope to clear.
25725611Seric **		fullclear - if set, the current envelope is total
25825611Seric **			garbage and should be ignored; otherwise,
25925611Seric **			release any resources it may indicate.
2609536Seric **
2619536Seric **	Returns:
2629536Seric **		none.
2639536Seric **
2649536Seric **	Side Effects:
2659536Seric **		Closes files associated with the envelope.
2669536Seric **		Marks the envelope as unallocated.
2679536Seric */
2689536Seric 
26960494Seric void
27025611Seric clearenvelope(e, fullclear)
2719536Seric 	register ENVELOPE *e;
27225611Seric 	bool fullclear;
2739536Seric {
27425514Seric 	register HDR *bh;
27525514Seric 	register HDR **nhp;
27625514Seric 	extern ENVELOPE BlankEnvelope;
27725514Seric 
27825611Seric 	if (!fullclear)
27925611Seric 	{
28025611Seric 		/* clear out any file information */
28125611Seric 		if (e->e_xfp != NULL)
28258680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
28325611Seric 		if (e->e_dfp != NULL)
28458680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
28558680Seric 		e->e_xfp = e->e_dfp = NULL;
28625611Seric 	}
2879536Seric 
28824961Seric 	/* now clear out the data */
28924965Seric 	STRUCTCOPY(BlankEnvelope, *e);
29059698Seric 	if (Verbose)
29159698Seric 		e->e_sendmode = SM_DELIVER;
29225514Seric 	bh = BlankEnvelope.e_header;
29325514Seric 	nhp = &e->e_header;
29425514Seric 	while (bh != NULL)
29525514Seric 	{
29625514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
29725514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
29825514Seric 		bh = bh->h_link;
29925514Seric 		nhp = &(*nhp)->h_link;
30025514Seric 	}
3019536Seric }
3029536Seric /*
3039536Seric **  INITSYS -- initialize instantiation of system
3049536Seric **
3059536Seric **	In Daemon mode, this is done in the child.
3069536Seric **
3079536Seric **	Parameters:
3089536Seric **		none.
3099536Seric **
3109536Seric **	Returns:
3119536Seric **		none.
3129536Seric **
3139536Seric **	Side Effects:
3149536Seric **		Initializes the system macros, some global variables,
3159536Seric **		etc.  In particular, the current time in various
3169536Seric **		forms is set.
3179536Seric */
3189536Seric 
31960494Seric void
32055012Seric initsys(e)
32155012Seric 	register ENVELOPE *e;
3229536Seric {
3239536Seric 	static char cbuf[5];			/* holds hop count */
3249536Seric 	static char pbuf[10];			/* holds pid */
32522963Smiriam #ifdef TTYNAME
32659304Seric 	static char ybuf[60];			/* holds tty id */
3279536Seric 	register char *p;
32856795Seric #endif /* TTYNAME */
3299536Seric 	extern char *ttyname();
33060494Seric 	extern void settime();
3319536Seric 	extern char Version[];
3329536Seric 
3339536Seric 	/*
3349536Seric 	**  Give this envelope a reality.
3359536Seric 	**	I.e., an id, a transcript, and a creation time.
3369536Seric 	*/
3379536Seric 
33855012Seric 	openxscript(e);
33955012Seric 	e->e_ctime = curtime();
3409536Seric 
3419536Seric 	/*
3429536Seric 	**  Set OutChannel to something useful if stdout isn't it.
3439536Seric 	**	This arranges that any extra stuff the mailer produces
3449536Seric 	**	gets sent back to the user on error (because it is
3459536Seric 	**	tucked away in the transcript).
3469536Seric 	*/
3479536Seric 
34858737Seric 	if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) &&
34958737Seric 	    e->e_xfp != NULL)
35055012Seric 		OutChannel = e->e_xfp;
3519536Seric 
3529536Seric 	/*
3539536Seric 	**  Set up some basic system macros.
3549536Seric 	*/
3559536Seric 
3569536Seric 	/* process id */
3579536Seric 	(void) sprintf(pbuf, "%d", getpid());
35855012Seric 	define('p', pbuf, e);
3599536Seric 
3609536Seric 	/* hop count */
36155012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
36255012Seric 	define('c', cbuf, e);
3639536Seric 
3649536Seric 	/* time as integer, unix time, arpa time */
36555012Seric 	settime(e);
3669536Seric 
36717472Seric #ifdef TTYNAME
3689536Seric 	/* tty name */
36955012Seric 	if (macvalue('y', e) == NULL)
3709536Seric 	{
3719536Seric 		p = ttyname(2);
3729536Seric 		if (p != NULL)
3739536Seric 		{
37456795Seric 			if (strrchr(p, '/') != NULL)
37556795Seric 				p = strrchr(p, '/') + 1;
3769536Seric 			(void) strcpy(ybuf, p);
37755012Seric 			define('y', ybuf, e);
3789536Seric 		}
3799536Seric 	}
38056795Seric #endif /* TTYNAME */
3819536Seric }
3829536Seric /*
38311932Seric **  SETTIME -- set the current time.
38411932Seric **
38511932Seric **	Parameters:
38611932Seric **		none.
38711932Seric **
38811932Seric **	Returns:
38911932Seric **		none.
39011932Seric **
39111932Seric **	Side Effects:
39211932Seric **		Sets the various time macros -- $a, $b, $d, $t.
39311932Seric */
39411932Seric 
39560494Seric void
39655012Seric settime(e)
39755012Seric 	register ENVELOPE *e;
39811932Seric {
39911932Seric 	register char *p;
40011932Seric 	auto time_t now;
40111932Seric 	static char tbuf[20];			/* holds "current" time */
40211932Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
40311932Seric 	register struct tm *tm;
40411932Seric 	extern char *arpadate();
40511932Seric 	extern struct tm *gmtime();
40611932Seric 
40711932Seric 	now = curtime();
40811932Seric 	tm = gmtime(&now);
40957014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
41057014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
41155012Seric 	define('t', tbuf, e);
41211932Seric 	(void) strcpy(dbuf, ctime(&now));
41358131Seric 	p = strchr(dbuf, '\n');
41458131Seric 	if (p != NULL)
41558131Seric 		*p = '\0';
41658131Seric 	define('d', dbuf, e);
41711932Seric 	p = newstr(arpadate(dbuf));
41855012Seric 	if (macvalue('a', e) == NULL)
41955012Seric 		define('a', p, e);
42055012Seric 	define('b', p, e);
42111932Seric }
42211932Seric /*
4239536Seric **  OPENXSCRIPT -- Open transcript file
4249536Seric **
4259536Seric **	Creates a transcript file for possible eventual mailing or
4269536Seric **	sending back.
4279536Seric **
4289536Seric **	Parameters:
4299536Seric **		e -- the envelope to create the transcript in/for.
4309536Seric **
4319536Seric **	Returns:
4329536Seric **		none
4339536Seric **
4349536Seric **	Side Effects:
4359536Seric **		Creates the transcript file.
4369536Seric */
4379536Seric 
43858803Seric #ifndef O_APPEND
43958803Seric #define O_APPEND	0
44058803Seric #endif
44158803Seric 
44260494Seric void
4439536Seric openxscript(e)
4449536Seric 	register ENVELOPE *e;
4459536Seric {
4469536Seric 	register char *p;
44740933Srick 	int fd;
4489536Seric 
4499536Seric 	if (e->e_xfp != NULL)
4509536Seric 		return;
4519536Seric 	p = queuename(e, 'x');
45258803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
45340933Srick 	if (fd < 0)
45463753Seric 	{
45563753Seric 		syserr("Can't create transcript file %s", p);
45663753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
45763753Seric 		if (fd < 0)
45863753Seric 			syserr("!Can't open /dev/null");
45963753Seric 	}
46063753Seric 	e->e_xfp = fdopen(fd, "w");
4619536Seric }
4629536Seric /*
46310196Seric **  CLOSEXSCRIPT -- close the transcript file.
46410196Seric **
46510196Seric **	Parameters:
46610196Seric **		e -- the envelope containing the transcript to close.
46710196Seric **
46810196Seric **	Returns:
46910196Seric **		none.
47010196Seric **
47110196Seric **	Side Effects:
47210196Seric **		none.
47310196Seric */
47410196Seric 
47560494Seric void
47610196Seric closexscript(e)
47710196Seric 	register ENVELOPE *e;
47810196Seric {
47910196Seric 	if (e->e_xfp == NULL)
48010196Seric 		return;
48158680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
48210196Seric 	e->e_xfp = NULL;
48310196Seric }
48410196Seric /*
4859536Seric **  SETSENDER -- set the person who this message is from
4869536Seric **
4879536Seric **	Under certain circumstances allow the user to say who
4889536Seric **	s/he is (using -f or -r).  These are:
4899536Seric **	1.  The user's uid is zero (root).
4909536Seric **	2.  The user's login name is in an approved list (typically
4919536Seric **	    from a network server).
4929536Seric **	3.  The address the user is trying to claim has a
4939536Seric **	    "!" character in it (since #2 doesn't do it for
4949536Seric **	    us if we are dialing out for UUCP).
4959536Seric **	A better check to replace #3 would be if the
4969536Seric **	effective uid is "UUCP" -- this would require me
4979536Seric **	to rewrite getpwent to "grab" uucp as it went by,
4989536Seric **	make getname more nasty, do another passwd file
4999536Seric **	scan, or compile the UID of "UUCP" into the code,
5009536Seric **	all of which are reprehensible.
5019536Seric **
5029536Seric **	Assuming all of these fail, we figure out something
5039536Seric **	ourselves.
5049536Seric **
5059536Seric **	Parameters:
5069536Seric **		from -- the person we would like to believe this message
5079536Seric **			is from, as specified on the command line.
50853182Seric **		e -- the envelope in which we would like the sender set.
50958333Seric **		delimptr -- if non-NULL, set to the location of the
51058333Seric **			trailing delimiter.
51158704Seric **		internal -- set if this address is coming from an internal
51258704Seric **			source such as an owner alias.
5139536Seric **
5149536Seric **	Returns:
51558704Seric **		none.
5169536Seric **
5179536Seric **	Side Effects:
5189536Seric **		sets sendmail's notion of who the from person is.
5199536Seric */
5209536Seric 
52160494Seric void
52258704Seric setsender(from, e, delimptr, internal)
5239536Seric 	char *from;
52453182Seric 	register ENVELOPE *e;
52558333Seric 	char **delimptr;
52658704Seric 	bool internal;
5279536Seric {
5289536Seric 	register char **pvp;
5299536Seric 	char *realname = NULL;
53018665Seric 	register struct passwd *pw;
53158727Seric 	char delimchar;
5329536Seric 	char buf[MAXNAME];
53316913Seric 	char pvpbuf[PSBUFSIZE];
53418665Seric 	extern struct passwd *getpwnam();
5359536Seric 	extern char *FullName;
5369536Seric 
5379536Seric 	if (tTd(45, 1))
53814786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5399536Seric 
5409536Seric 	/*
5419536Seric 	**  Figure out the real user executing us.
5429536Seric 	**	Username can return errno != 0 on non-errors.
5439536Seric 	*/
5449536Seric 
54558737Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
5469536Seric 		realname = from;
5479536Seric 	if (realname == NULL || realname[0] == '\0')
5489536Seric 		realname = username();
5499536Seric 
55059027Seric 	if (ConfigLevel < 2)
55159027Seric 		SuprErrs = TRUE;
55259027Seric 
55358727Seric 	delimchar = internal ? '\0' : ' ';
55458333Seric 	if (from == NULL ||
55558727Seric 	    parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
5569536Seric 	{
55721750Seric 		/* log garbage addresses for traceback */
55855173Seric # ifdef LOG
55958020Seric 		if (from != NULL && LogLevel > 2)
56021750Seric 		{
56158951Seric 			char *p;
56258951Seric 			char ebuf[MAXNAME * 2 + 2];
56355173Seric 
56458951Seric 			p = macvalue('_', e);
56558951Seric 			if (p == NULL)
56658951Seric 			{
56758951Seric 				char *host = RealHostName;
56858951Seric 				if (host == NULL)
56958951Seric 					host = MyHostName;
57058951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
57158951Seric 				p = ebuf;
57258951Seric 			}
57355173Seric 			syslog(LOG_NOTICE,
57458951Seric 				"from=%s unparseable, received from %s",
57558951Seric 				from, p);
57655173Seric 		}
57756795Seric # endif /* LOG */
57857589Seric 		if (from != NULL)
57957589Seric 			SuprErrs = TRUE;
58057589Seric 		if (from == realname ||
58158333Seric 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
58224944Seric 		{
58357589Seric 			SuprErrs = TRUE;
58458333Seric 			if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
58558151Seric 				syserr("553 setsender: can't even parse postmaster!");
58624944Seric 		}
5879536Seric 	}
5889536Seric 	else
5899536Seric 		FromFlag = TRUE;
59053182Seric 	e->e_from.q_flags |= QDONTSEND;
59157731Seric 	if (tTd(45, 5))
59257731Seric 	{
59357731Seric 		printf("setsender: QDONTSEND ");
59457731Seric 		printaddr(&e->e_from, FALSE);
59557731Seric 	}
5969536Seric 	SuprErrs = FALSE;
5979536Seric 
59853736Seric 	pvp = NULL;
59953736Seric 	if (e->e_from.q_mailer == LocalMailer)
6009536Seric 	{
60153736Seric # ifdef USERDB
60253736Seric 		register char *p;
60353736Seric 		extern char *udbsender();
60453736Seric # endif
60517472Seric 
60658704Seric 		if (!internal)
60758704Seric 		{
60858704Seric 			/* if the user has given fullname already, don't redefine */
60958704Seric 			if (FullName == NULL)
61058704Seric 				FullName = macvalue('x', e);
61158704Seric 			if (FullName != NULL && FullName[0] == '\0')
61258704Seric 				FullName = NULL;
6139536Seric 
61453736Seric # ifdef USERDB
61558704Seric 			p = udbsender(from);
61653736Seric 
61758704Seric 			if (p != NULL)
61858704Seric 			{
61958704Seric 				/*
62058704Seric 				**  We have an alternate address for the sender
62158704Seric 				*/
62253736Seric 
62358704Seric 				pvp = prescan(p, '\0', pvpbuf, NULL);
62458704Seric 			}
62558704Seric # endif /* USERDB */
6269536Seric 		}
62753736Seric 
62853736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
62953736Seric 		{
63053736Seric 			/*
63153736Seric 			**  Process passwd file entry.
63253736Seric 			*/
63353736Seric 
63453736Seric 
63553736Seric 			/* extract home directory */
63653736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
63753736Seric 			define('z', e->e_from.q_home, e);
63853736Seric 
63953736Seric 			/* extract user and group id */
64053736Seric 			e->e_from.q_uid = pw->pw_uid;
64153736Seric 			e->e_from.q_gid = pw->pw_gid;
64253736Seric 
64353736Seric 			/* extract full name from passwd file */
64453736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
64558704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
64658704Seric 			    !internal)
64753736Seric 			{
64853736Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
64953736Seric 				if (buf[0] != '\0')
65053736Seric 					FullName = newstr(buf);
65153736Seric 			}
65253736Seric 		}
65358704Seric 		if (FullName != NULL && !internal)
65453182Seric 			define('x', FullName, e);
6559536Seric 	}
65658704Seric 	else if (!internal)
65711625Seric 	{
65853182Seric 		if (e->e_from.q_home == NULL)
65953182Seric 			e->e_from.q_home = getenv("HOME");
66063787Seric 		e->e_from.q_uid = RealUid;
66163787Seric 		e->e_from.q_gid = RealGid;
66211625Seric 	}
66311625Seric 
6649536Seric 	/*
6659536Seric 	**  Rewrite the from person to dispose of possible implicit
6669536Seric 	**	links in the net.
6679536Seric 	*/
6689536Seric 
6699536Seric 	if (pvp == NULL)
67058333Seric 		pvp = prescan(from, '\0', pvpbuf, NULL);
67153736Seric 	if (pvp == NULL)
6729536Seric 	{
67358403Seric 		/* don't need to give error -- prescan did that already */
67436233Skarels # ifdef LOG
67558020Seric 		if (LogLevel > 2)
67636233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
67736233Skarels # endif
6789536Seric 		finis();
6799536Seric 	}
68059084Seric 	(void) rewrite(pvp, 3, e);
68159084Seric 	(void) rewrite(pvp, 1, e);
68259084Seric 	(void) rewrite(pvp, 4, e);
68358814Seric 	cataddr(pvp, NULL, buf, sizeof buf, '\0');
68458704Seric 	e->e_sender = newstr(buf);
68558704Seric 	define('f', e->e_sender, e);
6869536Seric 
6879536Seric 	/* save the domain spec if this mailer wants it */
68858704Seric 	if (!internal && e->e_from.q_mailer != NULL &&
68953182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
6909536Seric 	{
6919536Seric 		extern char **copyplist();
6929536Seric 
6939536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
6949536Seric 			pvp++;
6959536Seric 		if (*pvp != NULL)
69653182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
6979536Seric 	}
6989536Seric }
699