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*64086Seric static char sccsid[] = "@(#)envelope.c	8.6 (Berkeley) 07/29/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 	/*
20063849Seric 	**  Arrange to send warning messages to postmaster as requested.
20163849Seric 	*/
20263849Seric 
20363849Seric 	if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL &&
20463849Seric 	    !bitset(EF_RESPONSE, e->e_flags))
20563849Seric 	{
20663849Seric 		auto ADDRESS *rlist = NULL;
20763849Seric 
20863849Seric 		(void) sendtolist(PostMasterCopy, (ADDRESS *) NULL, &rlist, e);
20963849Seric 		(void) returntosender(e->e_message, rlist, FALSE, e);
21063849Seric 	}
21163849Seric 
21263849Seric 	/*
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);
417*64086Seric 	p = arpadate(dbuf);
418*64086Seric 	p = newstr(p);
41955012Seric 	if (macvalue('a', e) == NULL)
42055012Seric 		define('a', p, e);
42155012Seric 	define('b', p, e);
42211932Seric }
42311932Seric /*
4249536Seric **  OPENXSCRIPT -- Open transcript file
4259536Seric **
4269536Seric **	Creates a transcript file for possible eventual mailing or
4279536Seric **	sending back.
4289536Seric **
4299536Seric **	Parameters:
4309536Seric **		e -- the envelope to create the transcript in/for.
4319536Seric **
4329536Seric **	Returns:
4339536Seric **		none
4349536Seric **
4359536Seric **	Side Effects:
4369536Seric **		Creates the transcript file.
4379536Seric */
4389536Seric 
43958803Seric #ifndef O_APPEND
44058803Seric #define O_APPEND	0
44158803Seric #endif
44258803Seric 
44360494Seric void
4449536Seric openxscript(e)
4459536Seric 	register ENVELOPE *e;
4469536Seric {
4479536Seric 	register char *p;
44840933Srick 	int fd;
4499536Seric 
4509536Seric 	if (e->e_xfp != NULL)
4519536Seric 		return;
4529536Seric 	p = queuename(e, 'x');
45358803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
45440933Srick 	if (fd < 0)
45563753Seric 	{
45663753Seric 		syserr("Can't create transcript file %s", p);
45763753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
45863753Seric 		if (fd < 0)
45963753Seric 			syserr("!Can't open /dev/null");
46063753Seric 	}
46163753Seric 	e->e_xfp = fdopen(fd, "w");
4629536Seric }
4639536Seric /*
46410196Seric **  CLOSEXSCRIPT -- close the transcript file.
46510196Seric **
46610196Seric **	Parameters:
46710196Seric **		e -- the envelope containing the transcript to close.
46810196Seric **
46910196Seric **	Returns:
47010196Seric **		none.
47110196Seric **
47210196Seric **	Side Effects:
47310196Seric **		none.
47410196Seric */
47510196Seric 
47660494Seric void
47710196Seric closexscript(e)
47810196Seric 	register ENVELOPE *e;
47910196Seric {
48010196Seric 	if (e->e_xfp == NULL)
48110196Seric 		return;
48258680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
48310196Seric 	e->e_xfp = NULL;
48410196Seric }
48510196Seric /*
4869536Seric **  SETSENDER -- set the person who this message is from
4879536Seric **
4889536Seric **	Under certain circumstances allow the user to say who
4899536Seric **	s/he is (using -f or -r).  These are:
4909536Seric **	1.  The user's uid is zero (root).
4919536Seric **	2.  The user's login name is in an approved list (typically
4929536Seric **	    from a network server).
4939536Seric **	3.  The address the user is trying to claim has a
4949536Seric **	    "!" character in it (since #2 doesn't do it for
4959536Seric **	    us if we are dialing out for UUCP).
4969536Seric **	A better check to replace #3 would be if the
4979536Seric **	effective uid is "UUCP" -- this would require me
4989536Seric **	to rewrite getpwent to "grab" uucp as it went by,
4999536Seric **	make getname more nasty, do another passwd file
5009536Seric **	scan, or compile the UID of "UUCP" into the code,
5019536Seric **	all of which are reprehensible.
5029536Seric **
5039536Seric **	Assuming all of these fail, we figure out something
5049536Seric **	ourselves.
5059536Seric **
5069536Seric **	Parameters:
5079536Seric **		from -- the person we would like to believe this message
5089536Seric **			is from, as specified on the command line.
50953182Seric **		e -- the envelope in which we would like the sender set.
51058333Seric **		delimptr -- if non-NULL, set to the location of the
51158333Seric **			trailing delimiter.
51258704Seric **		internal -- set if this address is coming from an internal
51358704Seric **			source such as an owner alias.
5149536Seric **
5159536Seric **	Returns:
51658704Seric **		none.
5179536Seric **
5189536Seric **	Side Effects:
5199536Seric **		sets sendmail's notion of who the from person is.
5209536Seric */
5219536Seric 
52260494Seric void
52358704Seric setsender(from, e, delimptr, internal)
5249536Seric 	char *from;
52553182Seric 	register ENVELOPE *e;
52658333Seric 	char **delimptr;
52758704Seric 	bool internal;
5289536Seric {
5299536Seric 	register char **pvp;
5309536Seric 	char *realname = NULL;
53118665Seric 	register struct passwd *pw;
53258727Seric 	char delimchar;
5339536Seric 	char buf[MAXNAME];
53416913Seric 	char pvpbuf[PSBUFSIZE];
53518665Seric 	extern struct passwd *getpwnam();
5369536Seric 	extern char *FullName;
5379536Seric 
5389536Seric 	if (tTd(45, 1))
53914786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5409536Seric 
5419536Seric 	/*
5429536Seric 	**  Figure out the real user executing us.
5439536Seric 	**	Username can return errno != 0 on non-errors.
5449536Seric 	*/
5459536Seric 
54658737Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
5479536Seric 		realname = from;
5489536Seric 	if (realname == NULL || realname[0] == '\0')
5499536Seric 		realname = username();
5509536Seric 
55159027Seric 	if (ConfigLevel < 2)
55259027Seric 		SuprErrs = TRUE;
55359027Seric 
55458727Seric 	delimchar = internal ? '\0' : ' ';
55558333Seric 	if (from == NULL ||
55658727Seric 	    parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
5579536Seric 	{
55821750Seric 		/* log garbage addresses for traceback */
55955173Seric # ifdef LOG
56058020Seric 		if (from != NULL && LogLevel > 2)
56121750Seric 		{
56258951Seric 			char *p;
56358951Seric 			char ebuf[MAXNAME * 2 + 2];
56455173Seric 
56558951Seric 			p = macvalue('_', e);
56658951Seric 			if (p == NULL)
56758951Seric 			{
56858951Seric 				char *host = RealHostName;
56958951Seric 				if (host == NULL)
57058951Seric 					host = MyHostName;
57158951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
57258951Seric 				p = ebuf;
57358951Seric 			}
57455173Seric 			syslog(LOG_NOTICE,
57558951Seric 				"from=%s unparseable, received from %s",
57658951Seric 				from, p);
57755173Seric 		}
57856795Seric # endif /* LOG */
57957589Seric 		if (from != NULL)
58057589Seric 			SuprErrs = TRUE;
58157589Seric 		if (from == realname ||
58258333Seric 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
58324944Seric 		{
58457589Seric 			SuprErrs = TRUE;
58558333Seric 			if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
58658151Seric 				syserr("553 setsender: can't even parse postmaster!");
58724944Seric 		}
5889536Seric 	}
5899536Seric 	else
5909536Seric 		FromFlag = TRUE;
59153182Seric 	e->e_from.q_flags |= QDONTSEND;
59257731Seric 	if (tTd(45, 5))
59357731Seric 	{
59457731Seric 		printf("setsender: QDONTSEND ");
59557731Seric 		printaddr(&e->e_from, FALSE);
59657731Seric 	}
5979536Seric 	SuprErrs = FALSE;
5989536Seric 
59953736Seric 	pvp = NULL;
60053736Seric 	if (e->e_from.q_mailer == LocalMailer)
6019536Seric 	{
60253736Seric # ifdef USERDB
60353736Seric 		register char *p;
60453736Seric 		extern char *udbsender();
60553736Seric # endif
60617472Seric 
60758704Seric 		if (!internal)
60858704Seric 		{
60958704Seric 			/* if the user has given fullname already, don't redefine */
61058704Seric 			if (FullName == NULL)
61158704Seric 				FullName = macvalue('x', e);
61258704Seric 			if (FullName != NULL && FullName[0] == '\0')
61358704Seric 				FullName = NULL;
6149536Seric 
61553736Seric # ifdef USERDB
61658704Seric 			p = udbsender(from);
61753736Seric 
61858704Seric 			if (p != NULL)
61958704Seric 			{
62058704Seric 				/*
62158704Seric 				**  We have an alternate address for the sender
62258704Seric 				*/
62353736Seric 
62458704Seric 				pvp = prescan(p, '\0', pvpbuf, NULL);
62558704Seric 			}
62658704Seric # endif /* USERDB */
6279536Seric 		}
62853736Seric 
62953736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
63053736Seric 		{
63153736Seric 			/*
63253736Seric 			**  Process passwd file entry.
63353736Seric 			*/
63453736Seric 
63553736Seric 
63653736Seric 			/* extract home directory */
63753736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
63853736Seric 			define('z', e->e_from.q_home, e);
63953736Seric 
64053736Seric 			/* extract user and group id */
64153736Seric 			e->e_from.q_uid = pw->pw_uid;
64253736Seric 			e->e_from.q_gid = pw->pw_gid;
64353736Seric 
64453736Seric 			/* extract full name from passwd file */
64553736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
64658704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
64758704Seric 			    !internal)
64853736Seric 			{
64953736Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
65053736Seric 				if (buf[0] != '\0')
65153736Seric 					FullName = newstr(buf);
65253736Seric 			}
65353736Seric 		}
65458704Seric 		if (FullName != NULL && !internal)
65553182Seric 			define('x', FullName, e);
6569536Seric 	}
65758704Seric 	else if (!internal)
65811625Seric 	{
65953182Seric 		if (e->e_from.q_home == NULL)
66053182Seric 			e->e_from.q_home = getenv("HOME");
66163787Seric 		e->e_from.q_uid = RealUid;
66263787Seric 		e->e_from.q_gid = RealGid;
66311625Seric 	}
66411625Seric 
6659536Seric 	/*
6669536Seric 	**  Rewrite the from person to dispose of possible implicit
6679536Seric 	**	links in the net.
6689536Seric 	*/
6699536Seric 
6709536Seric 	if (pvp == NULL)
67158333Seric 		pvp = prescan(from, '\0', pvpbuf, NULL);
67253736Seric 	if (pvp == NULL)
6739536Seric 	{
67458403Seric 		/* don't need to give error -- prescan did that already */
67536233Skarels # ifdef LOG
67658020Seric 		if (LogLevel > 2)
67736233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
67836233Skarels # endif
6799536Seric 		finis();
6809536Seric 	}
68159084Seric 	(void) rewrite(pvp, 3, e);
68259084Seric 	(void) rewrite(pvp, 1, e);
68359084Seric 	(void) rewrite(pvp, 4, e);
68458814Seric 	cataddr(pvp, NULL, buf, sizeof buf, '\0');
68558704Seric 	e->e_sender = newstr(buf);
68658704Seric 	define('f', e->e_sender, e);
6879536Seric 
6889536Seric 	/* save the domain spec if this mailer wants it */
68958704Seric 	if (!internal && e->e_from.q_mailer != NULL &&
69053182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
6919536Seric 	{
6929536Seric 		extern char **copyplist();
6939536Seric 
6949536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
6959536Seric 			pvp++;
6969536Seric 		if (*pvp != NULL)
69753182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
6989536Seric 	}
6999536Seric }
700