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*63787Seric static char sccsid[] = "@(#)envelope.c	8.3 (Berkeley) 07/13/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;
789536Seric 	register ADDRESS *q;
7957943Seric 	char *id = e->e_id;
8063753Seric 	char buf[MAXLINE];
819536Seric 
829536Seric 	if (tTd(50, 1))
839536Seric 	{
8458680Seric 		printf("dropenvelope %x: id=", e);
859536Seric 		xputs(e->e_id);
8658680Seric 		printf(", flags=%o\n", e->e_flags);
8763753Seric 		if (tTd(50, 10))
8863753Seric 		{
8963753Seric 			printf("sendq=");
9063753Seric 			printaddr(e->e_sendqueue, TRUE);
9163753Seric 		}
929536Seric 	}
9357943Seric 
9458680Seric 	/* we must have an id to remove disk files */
9557943Seric 	if (id == NULL)
9658680Seric 		return;
9757943Seric 
989536Seric #ifdef LOG
9958020Seric 	if (LogLevel > 84)
1009536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
10157943Seric 				  id, e->e_flags, getpid());
10256795Seric #endif /* LOG */
1039536Seric 
10463753Seric 	/* post statistics */
10563753Seric 	poststats(StatFile);
10663753Seric 
1079536Seric 	/*
1089536Seric 	**  Extract state information from dregs of send list.
1099536Seric 	*/
1109536Seric 
1119536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1129536Seric 	{
1139536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1149536Seric 			queueit = TRUE;
1159536Seric 	}
1169536Seric 
1179536Seric 	/*
11863753Seric 	**  See if the message timed out.
11963753Seric 	*/
12063753Seric 
12163753Seric 	if (!queueit)
12263753Seric 		/* nothing to do */ ;
12363753Seric 	else if (curtime() > e->e_ctime + TimeOuts.to_q_return)
12463753Seric 	{
12563753Seric 		if (!bitset(EF_TIMEOUT, e->e_flags))
12663753Seric 		{
12763753Seric 			(void) sprintf(buf, "Cannot send message for %s",
12863753Seric 				pintvl(TimeOuts.to_q_return, FALSE));
12963753Seric 			if (e->e_message != NULL)
13063753Seric 				free(e->e_message);
13163753Seric 			e->e_message = newstr(buf);
13263753Seric 			message(buf);
13363753Seric 		}
13463753Seric 		e->e_flags |= EF_TIMEOUT|EF_CLRQUEUE;
135*63787Seric 		fprintf(e->e_xfp, "Message could not be delivered for %s\n",
136*63787Seric 			pintvl(TimeOuts.to_q_return, FALSE));
137*63787Seric 		fprintf(e->e_xfp, "Message will be deleted from queue\n");
138*63787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
139*63787Seric 		{
140*63787Seric 			if (bitset(QQUEUEUP, q->q_flags))
141*63787Seric 				q->q_flags |= QBADADDR;
142*63787Seric 		}
14363753Seric 	}
14463753Seric 	else if (TimeOuts.to_q_warning > 0 &&
14563753Seric 	    curtime() > e->e_ctime + TimeOuts.to_q_warning)
14663753Seric 	{
14763753Seric 		if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) &&
14863753Seric 		    e->e_class >= 0 &&
14963753Seric 		    strcmp(e->e_from.q_paddr, "<>") != 0)
15063753Seric 		{
15163753Seric 			(void) sprintf(buf,
15263753Seric 				"warning: cannot send message for %s",
15363753Seric 				pintvl(TimeOuts.to_q_warning, FALSE));
15463753Seric 			if (e->e_message != NULL)
15563753Seric 				free(e->e_message);
15663753Seric 			e->e_message = newstr(buf);
15763753Seric 			message(buf);
15863753Seric 			e->e_flags |= EF_WARNING|EF_TIMEOUT;
15963753Seric 		}
16063753Seric 		fprintf(e->e_xfp,
16163753Seric 			"Warning: message still undelivered after %s\n",
16263753Seric 			pintvl(TimeOuts.to_q_warning, FALSE));
16363753Seric 		fprintf(e->e_xfp, "Will keep trying until message is %s old\n",
16463753Seric 			pintvl(TimeOuts.to_q_return, FALSE));
165*63787Seric 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
166*63787Seric 		{
167*63787Seric 			if (bitset(QQUEUEUP, q->q_flags))
168*63787Seric 				q->q_flags |= QREPORT;
169*63787Seric 		}
17063753Seric 	}
17163753Seric 
17263753Seric 	/*
1739536Seric 	**  Send back return receipts as requested.
1749536Seric 	*/
1759536Seric 
1769536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1779536Seric 	{
17810844Seric 		auto ADDRESS *rlist = NULL;
1799536Seric 
18058082Seric 		(void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e);
18155012Seric 		(void) returntosender("Return receipt", rlist, FALSE, e);
1829536Seric 	}
1839536Seric 
1849536Seric 	/*
1859536Seric 	**  Arrange to send error messages if there are fatal errors.
1869536Seric 	*/
1879536Seric 
18858734Seric 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) &&
18958734Seric 	    e->e_errormode != EM_QUIET)
1909536Seric 		savemail(e);
1919536Seric 
1929536Seric 	/*
1939536Seric 	**  Instantiate or deinstantiate the queue.
1949536Seric 	*/
1959536Seric 
1969536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
1979536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
1989536Seric 	{
19963753Seric 		if (tTd(50, 2))
20063753Seric 			printf("Dropping envelope\n");
20123497Seric 		if (e->e_df != NULL)
20223497Seric 			xunlink(e->e_df);
2039536Seric 		xunlink(queuename(e, 'q'));
2049536Seric 	}
2059536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
20610754Seric 	{
20710754Seric #ifdef QUEUE
20851920Seric 		queueup(e, FALSE, FALSE);
20956795Seric #else /* QUEUE */
21058151Seric 		syserr("554 dropenvelope: queueup");
21156795Seric #endif /* QUEUE */
21210754Seric 	}
2139536Seric 
2149536Seric 	/* now unlock the job */
21510196Seric 	closexscript(e);
2169536Seric 	unlockqueue(e);
2179536Seric 
2189536Seric 	/* make sure that this envelope is marked unused */
21924944Seric 	if (e->e_dfp != NULL)
22058680Seric 		(void) xfclose(e->e_dfp, "dropenvelope", e->e_df);
22110196Seric 	e->e_dfp = NULL;
22258680Seric 	e->e_id = e->e_df = NULL;
22357589Seric 
22457589Seric #ifdef LOG
22558020Seric 	if (LogLevel > 74)
22657943Seric 		syslog(LOG_INFO, "%s: done", id);
22757589Seric #endif /* LOG */
2289536Seric }
2299536Seric /*
2309536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
2319536Seric **
2329536Seric **	This is normally used by a child process to get a clean
2339536Seric **	envelope without disturbing the parent.
2349536Seric **
2359536Seric **	Parameters:
2369536Seric **		e -- the envelope to clear.
23725611Seric **		fullclear - if set, the current envelope is total
23825611Seric **			garbage and should be ignored; otherwise,
23925611Seric **			release any resources it may indicate.
2409536Seric **
2419536Seric **	Returns:
2429536Seric **		none.
2439536Seric **
2449536Seric **	Side Effects:
2459536Seric **		Closes files associated with the envelope.
2469536Seric **		Marks the envelope as unallocated.
2479536Seric */
2489536Seric 
24960494Seric void
25025611Seric clearenvelope(e, fullclear)
2519536Seric 	register ENVELOPE *e;
25225611Seric 	bool fullclear;
2539536Seric {
25425514Seric 	register HDR *bh;
25525514Seric 	register HDR **nhp;
25625514Seric 	extern ENVELOPE BlankEnvelope;
25725514Seric 
25825611Seric 	if (!fullclear)
25925611Seric 	{
26025611Seric 		/* clear out any file information */
26125611Seric 		if (e->e_xfp != NULL)
26258680Seric 			(void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id);
26325611Seric 		if (e->e_dfp != NULL)
26458680Seric 			(void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df);
26558680Seric 		e->e_xfp = e->e_dfp = NULL;
26625611Seric 	}
2679536Seric 
26824961Seric 	/* now clear out the data */
26924965Seric 	STRUCTCOPY(BlankEnvelope, *e);
27059698Seric 	if (Verbose)
27159698Seric 		e->e_sendmode = SM_DELIVER;
27225514Seric 	bh = BlankEnvelope.e_header;
27325514Seric 	nhp = &e->e_header;
27425514Seric 	while (bh != NULL)
27525514Seric 	{
27625514Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
27725514Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
27825514Seric 		bh = bh->h_link;
27925514Seric 		nhp = &(*nhp)->h_link;
28025514Seric 	}
2819536Seric }
2829536Seric /*
2839536Seric **  INITSYS -- initialize instantiation of system
2849536Seric **
2859536Seric **	In Daemon mode, this is done in the child.
2869536Seric **
2879536Seric **	Parameters:
2889536Seric **		none.
2899536Seric **
2909536Seric **	Returns:
2919536Seric **		none.
2929536Seric **
2939536Seric **	Side Effects:
2949536Seric **		Initializes the system macros, some global variables,
2959536Seric **		etc.  In particular, the current time in various
2969536Seric **		forms is set.
2979536Seric */
2989536Seric 
29960494Seric void
30055012Seric initsys(e)
30155012Seric 	register ENVELOPE *e;
3029536Seric {
3039536Seric 	static char cbuf[5];			/* holds hop count */
3049536Seric 	static char pbuf[10];			/* holds pid */
30522963Smiriam #ifdef TTYNAME
30659304Seric 	static char ybuf[60];			/* holds tty id */
3079536Seric 	register char *p;
30856795Seric #endif /* TTYNAME */
3099536Seric 	extern char *ttyname();
31060494Seric 	extern void settime();
3119536Seric 	extern char Version[];
3129536Seric 
3139536Seric 	/*
3149536Seric 	**  Give this envelope a reality.
3159536Seric 	**	I.e., an id, a transcript, and a creation time.
3169536Seric 	*/
3179536Seric 
31855012Seric 	openxscript(e);
31955012Seric 	e->e_ctime = curtime();
3209536Seric 
3219536Seric 	/*
3229536Seric 	**  Set OutChannel to something useful if stdout isn't it.
3239536Seric 	**	This arranges that any extra stuff the mailer produces
3249536Seric 	**	gets sent back to the user on error (because it is
3259536Seric 	**	tucked away in the transcript).
3269536Seric 	*/
3279536Seric 
32858737Seric 	if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) &&
32958737Seric 	    e->e_xfp != NULL)
33055012Seric 		OutChannel = e->e_xfp;
3319536Seric 
3329536Seric 	/*
3339536Seric 	**  Set up some basic system macros.
3349536Seric 	*/
3359536Seric 
3369536Seric 	/* process id */
3379536Seric 	(void) sprintf(pbuf, "%d", getpid());
33855012Seric 	define('p', pbuf, e);
3399536Seric 
3409536Seric 	/* hop count */
34155012Seric 	(void) sprintf(cbuf, "%d", e->e_hopcount);
34255012Seric 	define('c', cbuf, e);
3439536Seric 
3449536Seric 	/* time as integer, unix time, arpa time */
34555012Seric 	settime(e);
3469536Seric 
34717472Seric #ifdef TTYNAME
3489536Seric 	/* tty name */
34955012Seric 	if (macvalue('y', e) == NULL)
3509536Seric 	{
3519536Seric 		p = ttyname(2);
3529536Seric 		if (p != NULL)
3539536Seric 		{
35456795Seric 			if (strrchr(p, '/') != NULL)
35556795Seric 				p = strrchr(p, '/') + 1;
3569536Seric 			(void) strcpy(ybuf, p);
35755012Seric 			define('y', ybuf, e);
3589536Seric 		}
3599536Seric 	}
36056795Seric #endif /* TTYNAME */
3619536Seric }
3629536Seric /*
36311932Seric **  SETTIME -- set the current time.
36411932Seric **
36511932Seric **	Parameters:
36611932Seric **		none.
36711932Seric **
36811932Seric **	Returns:
36911932Seric **		none.
37011932Seric **
37111932Seric **	Side Effects:
37211932Seric **		Sets the various time macros -- $a, $b, $d, $t.
37311932Seric */
37411932Seric 
37560494Seric void
37655012Seric settime(e)
37755012Seric 	register ENVELOPE *e;
37811932Seric {
37911932Seric 	register char *p;
38011932Seric 	auto time_t now;
38111932Seric 	static char tbuf[20];			/* holds "current" time */
38211932Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
38311932Seric 	register struct tm *tm;
38411932Seric 	extern char *arpadate();
38511932Seric 	extern struct tm *gmtime();
38611932Seric 
38711932Seric 	now = curtime();
38811932Seric 	tm = gmtime(&now);
38957014Seric 	(void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900,
39057014Seric 			tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min);
39155012Seric 	define('t', tbuf, e);
39211932Seric 	(void) strcpy(dbuf, ctime(&now));
39358131Seric 	p = strchr(dbuf, '\n');
39458131Seric 	if (p != NULL)
39558131Seric 		*p = '\0';
39658131Seric 	define('d', dbuf, e);
39711932Seric 	p = newstr(arpadate(dbuf));
39855012Seric 	if (macvalue('a', e) == NULL)
39955012Seric 		define('a', p, e);
40055012Seric 	define('b', p, e);
40111932Seric }
40211932Seric /*
4039536Seric **  OPENXSCRIPT -- Open transcript file
4049536Seric **
4059536Seric **	Creates a transcript file for possible eventual mailing or
4069536Seric **	sending back.
4079536Seric **
4089536Seric **	Parameters:
4099536Seric **		e -- the envelope to create the transcript in/for.
4109536Seric **
4119536Seric **	Returns:
4129536Seric **		none
4139536Seric **
4149536Seric **	Side Effects:
4159536Seric **		Creates the transcript file.
4169536Seric */
4179536Seric 
41858803Seric #ifndef O_APPEND
41958803Seric #define O_APPEND	0
42058803Seric #endif
42158803Seric 
42260494Seric void
4239536Seric openxscript(e)
4249536Seric 	register ENVELOPE *e;
4259536Seric {
4269536Seric 	register char *p;
42740933Srick 	int fd;
4289536Seric 
4299536Seric 	if (e->e_xfp != NULL)
4309536Seric 		return;
4319536Seric 	p = queuename(e, 'x');
43258803Seric 	fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644);
43340933Srick 	if (fd < 0)
43463753Seric 	{
43563753Seric 		syserr("Can't create transcript file %s", p);
43663753Seric 		fd = open("/dev/null", O_WRONLY, 0644);
43763753Seric 		if (fd < 0)
43863753Seric 			syserr("!Can't open /dev/null");
43963753Seric 	}
44063753Seric 	e->e_xfp = fdopen(fd, "w");
4419536Seric }
4429536Seric /*
44310196Seric **  CLOSEXSCRIPT -- close the transcript file.
44410196Seric **
44510196Seric **	Parameters:
44610196Seric **		e -- the envelope containing the transcript to close.
44710196Seric **
44810196Seric **	Returns:
44910196Seric **		none.
45010196Seric **
45110196Seric **	Side Effects:
45210196Seric **		none.
45310196Seric */
45410196Seric 
45560494Seric void
45610196Seric closexscript(e)
45710196Seric 	register ENVELOPE *e;
45810196Seric {
45910196Seric 	if (e->e_xfp == NULL)
46010196Seric 		return;
46158680Seric 	(void) xfclose(e->e_xfp, "closexscript", e->e_id);
46210196Seric 	e->e_xfp = NULL;
46310196Seric }
46410196Seric /*
4659536Seric **  SETSENDER -- set the person who this message is from
4669536Seric **
4679536Seric **	Under certain circumstances allow the user to say who
4689536Seric **	s/he is (using -f or -r).  These are:
4699536Seric **	1.  The user's uid is zero (root).
4709536Seric **	2.  The user's login name is in an approved list (typically
4719536Seric **	    from a network server).
4729536Seric **	3.  The address the user is trying to claim has a
4739536Seric **	    "!" character in it (since #2 doesn't do it for
4749536Seric **	    us if we are dialing out for UUCP).
4759536Seric **	A better check to replace #3 would be if the
4769536Seric **	effective uid is "UUCP" -- this would require me
4779536Seric **	to rewrite getpwent to "grab" uucp as it went by,
4789536Seric **	make getname more nasty, do another passwd file
4799536Seric **	scan, or compile the UID of "UUCP" into the code,
4809536Seric **	all of which are reprehensible.
4819536Seric **
4829536Seric **	Assuming all of these fail, we figure out something
4839536Seric **	ourselves.
4849536Seric **
4859536Seric **	Parameters:
4869536Seric **		from -- the person we would like to believe this message
4879536Seric **			is from, as specified on the command line.
48853182Seric **		e -- the envelope in which we would like the sender set.
48958333Seric **		delimptr -- if non-NULL, set to the location of the
49058333Seric **			trailing delimiter.
49158704Seric **		internal -- set if this address is coming from an internal
49258704Seric **			source such as an owner alias.
4939536Seric **
4949536Seric **	Returns:
49558704Seric **		none.
4969536Seric **
4979536Seric **	Side Effects:
4989536Seric **		sets sendmail's notion of who the from person is.
4999536Seric */
5009536Seric 
50160494Seric void
50258704Seric setsender(from, e, delimptr, internal)
5039536Seric 	char *from;
50453182Seric 	register ENVELOPE *e;
50558333Seric 	char **delimptr;
50658704Seric 	bool internal;
5079536Seric {
5089536Seric 	register char **pvp;
5099536Seric 	char *realname = NULL;
51018665Seric 	register struct passwd *pw;
51158727Seric 	char delimchar;
5129536Seric 	char buf[MAXNAME];
51316913Seric 	char pvpbuf[PSBUFSIZE];
51418665Seric 	extern struct passwd *getpwnam();
5159536Seric 	extern char *FullName;
5169536Seric 
5179536Seric 	if (tTd(45, 1))
51814786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
5199536Seric 
5209536Seric 	/*
5219536Seric 	**  Figure out the real user executing us.
5229536Seric 	**	Username can return errno != 0 on non-errors.
5239536Seric 	*/
5249536Seric 
52558737Seric 	if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP)
5269536Seric 		realname = from;
5279536Seric 	if (realname == NULL || realname[0] == '\0')
5289536Seric 		realname = username();
5299536Seric 
53059027Seric 	if (ConfigLevel < 2)
53159027Seric 		SuprErrs = TRUE;
53259027Seric 
53358727Seric 	delimchar = internal ? '\0' : ' ';
53458333Seric 	if (from == NULL ||
53558727Seric 	    parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL)
5369536Seric 	{
53721750Seric 		/* log garbage addresses for traceback */
53855173Seric # ifdef LOG
53958020Seric 		if (from != NULL && LogLevel > 2)
54021750Seric 		{
54158951Seric 			char *p;
54258951Seric 			char ebuf[MAXNAME * 2 + 2];
54355173Seric 
54458951Seric 			p = macvalue('_', e);
54558951Seric 			if (p == NULL)
54658951Seric 			{
54758951Seric 				char *host = RealHostName;
54858951Seric 				if (host == NULL)
54958951Seric 					host = MyHostName;
55058951Seric 				(void) sprintf(ebuf, "%s@%s", realname, host);
55158951Seric 				p = ebuf;
55258951Seric 			}
55355173Seric 			syslog(LOG_NOTICE,
55458951Seric 				"from=%s unparseable, received from %s",
55558951Seric 				from, p);
55655173Seric 		}
55756795Seric # endif /* LOG */
55857589Seric 		if (from != NULL)
55957589Seric 			SuprErrs = TRUE;
56057589Seric 		if (from == realname ||
56158333Seric 		    parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL)
56224944Seric 		{
56357589Seric 			SuprErrs = TRUE;
56458333Seric 			if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL)
56558151Seric 				syserr("553 setsender: can't even parse postmaster!");
56624944Seric 		}
5679536Seric 	}
5689536Seric 	else
5699536Seric 		FromFlag = TRUE;
57053182Seric 	e->e_from.q_flags |= QDONTSEND;
57157731Seric 	if (tTd(45, 5))
57257731Seric 	{
57357731Seric 		printf("setsender: QDONTSEND ");
57457731Seric 		printaddr(&e->e_from, FALSE);
57557731Seric 	}
5769536Seric 	SuprErrs = FALSE;
5779536Seric 
57853736Seric 	pvp = NULL;
57953736Seric 	if (e->e_from.q_mailer == LocalMailer)
5809536Seric 	{
58153736Seric # ifdef USERDB
58253736Seric 		register char *p;
58353736Seric 		extern char *udbsender();
58453736Seric # endif
58517472Seric 
58658704Seric 		if (!internal)
58758704Seric 		{
58858704Seric 			/* if the user has given fullname already, don't redefine */
58958704Seric 			if (FullName == NULL)
59058704Seric 				FullName = macvalue('x', e);
59158704Seric 			if (FullName != NULL && FullName[0] == '\0')
59258704Seric 				FullName = NULL;
5939536Seric 
59453736Seric # ifdef USERDB
59558704Seric 			p = udbsender(from);
59653736Seric 
59758704Seric 			if (p != NULL)
59858704Seric 			{
59958704Seric 				/*
60058704Seric 				**  We have an alternate address for the sender
60158704Seric 				*/
60253736Seric 
60358704Seric 				pvp = prescan(p, '\0', pvpbuf, NULL);
60458704Seric 			}
60558704Seric # endif /* USERDB */
6069536Seric 		}
60753736Seric 
60853736Seric 		if ((pw = getpwnam(e->e_from.q_user)) != NULL)
60953736Seric 		{
61053736Seric 			/*
61153736Seric 			**  Process passwd file entry.
61253736Seric 			*/
61353736Seric 
61453736Seric 
61553736Seric 			/* extract home directory */
61653736Seric 			e->e_from.q_home = newstr(pw->pw_dir);
61753736Seric 			define('z', e->e_from.q_home, e);
61853736Seric 
61953736Seric 			/* extract user and group id */
62053736Seric 			e->e_from.q_uid = pw->pw_uid;
62153736Seric 			e->e_from.q_gid = pw->pw_gid;
62253736Seric 
62353736Seric 			/* extract full name from passwd file */
62453736Seric 			if (FullName == NULL && pw->pw_gecos != NULL &&
62558704Seric 			    strcmp(pw->pw_name, e->e_from.q_user) == 0 &&
62658704Seric 			    !internal)
62753736Seric 			{
62853736Seric 				buildfname(pw->pw_gecos, e->e_from.q_user, buf);
62953736Seric 				if (buf[0] != '\0')
63053736Seric 					FullName = newstr(buf);
63153736Seric 			}
63253736Seric 		}
63358704Seric 		if (FullName != NULL && !internal)
63453182Seric 			define('x', FullName, e);
6359536Seric 	}
63658704Seric 	else if (!internal)
63711625Seric 	{
63853182Seric 		if (e->e_from.q_home == NULL)
63953182Seric 			e->e_from.q_home = getenv("HOME");
640*63787Seric 		e->e_from.q_uid = RealUid;
641*63787Seric 		e->e_from.q_gid = RealGid;
64211625Seric 	}
64311625Seric 
6449536Seric 	/*
6459536Seric 	**  Rewrite the from person to dispose of possible implicit
6469536Seric 	**	links in the net.
6479536Seric 	*/
6489536Seric 
6499536Seric 	if (pvp == NULL)
65058333Seric 		pvp = prescan(from, '\0', pvpbuf, NULL);
65153736Seric 	if (pvp == NULL)
6529536Seric 	{
65358403Seric 		/* don't need to give error -- prescan did that already */
65436233Skarels # ifdef LOG
65558020Seric 		if (LogLevel > 2)
65636233Skarels 			syslog(LOG_NOTICE, "cannot prescan from (%s)", from);
65736233Skarels # endif
6589536Seric 		finis();
6599536Seric 	}
66059084Seric 	(void) rewrite(pvp, 3, e);
66159084Seric 	(void) rewrite(pvp, 1, e);
66259084Seric 	(void) rewrite(pvp, 4, e);
66358814Seric 	cataddr(pvp, NULL, buf, sizeof buf, '\0');
66458704Seric 	e->e_sender = newstr(buf);
66558704Seric 	define('f', e->e_sender, e);
6669536Seric 
6679536Seric 	/* save the domain spec if this mailer wants it */
66858704Seric 	if (!internal && e->e_from.q_mailer != NULL &&
66953182Seric 	    bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags))
6709536Seric 	{
6719536Seric 		extern char **copyplist();
6729536Seric 
6739536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
6749536Seric 			pvp++;
6759536Seric 		if (*pvp != NULL)
67653182Seric 			e->e_fromdomain = copyplist(pvp, TRUE);
6779536Seric 	}
6789536Seric }
679