19536Seric #include <pwd.h>
29536Seric #include <time.h>
39536Seric #include "sendmail.h"
49536Seric #include <sys/stat.h>
59536Seric 
6*11447Seric SCCSID(@(#)envelope.c	3.12		03/08/83);
79536Seric 
89536Seric /*
99536Seric **  NEWENVELOPE -- allocate a new envelope
109536Seric **
119536Seric **	Supports inheritance.
129536Seric **
139536Seric **	Parameters:
149536Seric **		e -- the new envelope to fill in.
159536Seric **
169536Seric **	Returns:
179536Seric **		e.
189536Seric **
199536Seric **	Side Effects:
209536Seric **		none.
219536Seric */
229536Seric 
239536Seric ENVELOPE *
249536Seric newenvelope(e)
259536Seric 	register ENVELOPE *e;
269536Seric {
279536Seric 	register HDR *bh;
289536Seric 	register HDR **nhp;
299536Seric 	register ENVELOPE *parent;
309536Seric 	extern putheader(), putbody();
319536Seric 	extern ENVELOPE BlankEnvelope;
329536Seric 
339536Seric 	parent = CurEnv;
349536Seric 	if (e == CurEnv)
359536Seric 		parent = e->e_parent;
369536Seric 	clear((char *) e, sizeof *e);
379536Seric 	bmove((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
389536Seric 	e->e_parent = parent;
399536Seric 	e->e_ctime = curtime();
409536Seric 	e->e_puthdr = putheader;
419536Seric 	e->e_putbody = putbody;
429536Seric 	bh = BlankEnvelope.e_header;
439536Seric 	nhp = &e->e_header;
449536Seric 	while (bh != NULL)
459536Seric 	{
469536Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
479536Seric 		bmove((char *) bh, (char *) *nhp, sizeof *bh);
489536Seric 		bh = bh->h_link;
499536Seric 		nhp = &(*nhp)->h_link;
509536Seric 	}
519536Seric 	if (CurEnv->e_xfp != NULL)
529536Seric 		(void) fflush(CurEnv->e_xfp);
539536Seric 
549536Seric 	return (e);
559536Seric }
569536Seric /*
579536Seric **  DROPENVELOPE -- deallocate an envelope.
589536Seric **
599536Seric **	Parameters:
609536Seric **		e -- the envelope to deallocate.
619536Seric **
629536Seric **	Returns:
639536Seric **		none.
649536Seric **
659536Seric **	Side Effects:
669536Seric **		housekeeping necessary to dispose of an envelope.
679536Seric **		Unlocks this queue file.
689536Seric */
699536Seric 
709536Seric dropenvelope(e)
719536Seric 	register ENVELOPE *e;
729536Seric {
739536Seric 	bool queueit = FALSE;
749536Seric 	register ADDRESS *q;
759536Seric 
769536Seric #ifdef DEBUG
779536Seric 	if (tTd(50, 1))
789536Seric 	{
799536Seric 		printf("dropenvelope %x id=", e);
809536Seric 		xputs(e->e_id);
819536Seric 		printf(" flags=%o\n", e->e_flags);
829536Seric 	}
839536Seric #endif DEBUG
849536Seric #ifdef LOG
859536Seric 	if (LogLevel > 10)
869536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
879536Seric 				  e->e_id == NULL ? "(none)" : e->e_id,
889536Seric 				  e->e_flags, getpid());
899536Seric #endif LOG
909536Seric 
919536Seric 	/* we must have an id to remove disk files */
929536Seric 	if (e->e_id == NULL)
939536Seric 		return;
949536Seric 
959536Seric 	/*
969536Seric 	**  Extract state information from dregs of send list.
979536Seric 	*/
989536Seric 
999536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1009536Seric 	{
1019536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1029536Seric 			queueit = TRUE;
1039536Seric 	}
1049536Seric 
1059536Seric 	/*
1069536Seric 	**  Send back return receipts as requested.
1079536Seric 	*/
1089536Seric 
1099536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1109536Seric 	{
11110844Seric 		auto ADDRESS *rlist = NULL;
1129536Seric 
1139621Seric 		sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist);
1149536Seric 		(void) returntosender("Return receipt", rlist, FALSE);
1159536Seric 	}
1169536Seric 
1179536Seric 	/*
1189536Seric 	**  Arrange to send error messages if there are fatal errors.
1199536Seric 	*/
1209536Seric 
12110754Seric 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET)
1229536Seric 		savemail(e);
1239536Seric 
1249536Seric 	/*
1259536Seric 	**  Instantiate or deinstantiate the queue.
1269536Seric 	*/
1279536Seric 
1289536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
1299536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
1309536Seric 	{
1319536Seric 		if (e->e_dfp != NULL)
1329536Seric 			(void) fclose(e->e_dfp);
1339536Seric 		if (e->e_df != NULL)
1349536Seric 			xunlink(e->e_df);
1359536Seric 		xunlink(queuename(e, 'q'));
1369536Seric 	}
1379536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
13810754Seric 	{
13910754Seric #ifdef QUEUE
1409536Seric 		queueup(e, FALSE, FALSE);
14110754Seric #else QUEUE
14210754Seric 		syserr("dropenvelope: queueup");
14310754Seric #endif QUEUE
14410754Seric 	}
1459536Seric 
1469536Seric 	/* now unlock the job */
14710196Seric 	closexscript(e);
1489536Seric 	unlockqueue(e);
1499536Seric 
1509536Seric 	/* make sure that this envelope is marked unused */
1519536Seric 	e->e_id = e->e_df = NULL;
15210196Seric 	e->e_dfp = NULL;
1539536Seric }
1549536Seric /*
1559536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
1569536Seric **
1579536Seric **	This is normally used by a child process to get a clean
1589536Seric **	envelope without disturbing the parent.
1599536Seric **
1609536Seric **	Parameters:
1619536Seric **		e -- the envelope to clear.
1629536Seric **
1639536Seric **	Returns:
1649536Seric **		none.
1659536Seric **
1669536Seric **	Side Effects:
1679536Seric **		Closes files associated with the envelope.
1689536Seric **		Marks the envelope as unallocated.
1699536Seric */
1709536Seric 
1719536Seric clearenvelope(e)
1729536Seric 	register ENVELOPE *e;
1739536Seric {
1749536Seric 	/* clear out any file information */
1759536Seric 	if (e->e_xfp != NULL)
1769536Seric 		(void) fclose(e->e_xfp);
1779536Seric 	if (e->e_dfp != NULL)
1789536Seric 		(void) fclose(e->e_dfp);
1799536Seric 	e->e_xfp = e->e_dfp = NULL;
1809536Seric 
1819536Seric 	/* now expunge names of objects */
1829536Seric 	e->e_df = e->e_id = NULL;
1839536Seric 
1849536Seric 	/* and the flags which are now meaningless */
1859536Seric 	e->e_flags = 0;
1869536Seric }
1879536Seric /*
1889536Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1899536Seric **
1909536Seric **	Parameters:
1919536Seric **		e -- the envelope to unlock.
1929536Seric **
1939536Seric **	Returns:
1949536Seric **		none
1959536Seric **
1969536Seric **	Side Effects:
1979536Seric **		unlocks the queue for `e'.
1989536Seric */
1999536Seric 
2009536Seric unlockqueue(e)
2019536Seric 	ENVELOPE *e;
2029536Seric {
2039536Seric 	/* remove the transcript */
2049536Seric #ifdef DEBUG
20510196Seric # ifdef LOG
20610196Seric 	if (LogLevel > 19)
20710196Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
20810196Seric # endif LOG
2099536Seric 	if (!tTd(51, 4))
2109536Seric #endif DEBUG
2119536Seric 		xunlink(queuename(e, 'x'));
2129536Seric 
21310754Seric # ifdef QUEUE
2149536Seric 	/* last but not least, remove the lock */
2159536Seric 	xunlink(queuename(e, 'l'));
21610754Seric # endif QUEUE
2179536Seric }
2189536Seric /*
2199536Seric **  INITSYS -- initialize instantiation of system
2209536Seric **
2219536Seric **	In Daemon mode, this is done in the child.
2229536Seric **
2239536Seric **	Parameters:
2249536Seric **		none.
2259536Seric **
2269536Seric **	Returns:
2279536Seric **		none.
2289536Seric **
2299536Seric **	Side Effects:
2309536Seric **		Initializes the system macros, some global variables,
2319536Seric **		etc.  In particular, the current time in various
2329536Seric **		forms is set.
2339536Seric */
2349536Seric 
2359536Seric initsys()
2369536Seric {
2379536Seric 	auto time_t now;
2389536Seric 	static char cbuf[5];			/* holds hop count */
2399536Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
2409536Seric 	static char pbuf[10];			/* holds pid */
2419536Seric 	static char tbuf[20];			/* holds "current" time */
2429536Seric 	static char ybuf[10];			/* holds tty id */
2439536Seric 	register char *p;
2449536Seric 	register struct tm *tm;
2459536Seric 	extern char *ttyname();
2469536Seric 	extern char *arpadate();
2479536Seric 	extern struct tm *gmtime();
2489536Seric 	extern char *macvalue();
2499536Seric 	extern char Version[];
2509536Seric 
2519536Seric 	/*
2529536Seric 	**  Give this envelope a reality.
2539536Seric 	**	I.e., an id, a transcript, and a creation time.
2549536Seric 	*/
2559536Seric 
2569536Seric 	openxscript(CurEnv);
2579536Seric 	CurEnv->e_ctime = curtime();
2589536Seric 
2599536Seric 	/*
2609536Seric 	**  Set OutChannel to something useful if stdout isn't it.
2619536Seric 	**	This arranges that any extra stuff the mailer produces
2629536Seric 	**	gets sent back to the user on error (because it is
2639536Seric 	**	tucked away in the transcript).
2649536Seric 	*/
2659536Seric 
2669536Seric 	if (OpMode == MD_DAEMON && QueueRun)
2679536Seric 		OutChannel = CurEnv->e_xfp;
2689536Seric 
2699536Seric 	/*
2709536Seric 	**  Set up some basic system macros.
2719536Seric 	*/
2729536Seric 
2739536Seric 	/* process id */
2749536Seric 	(void) sprintf(pbuf, "%d", getpid());
2759536Seric 	define('p', pbuf, CurEnv);
2769536Seric 
2779536Seric 	/* hop count */
2789536Seric 	(void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
2799536Seric 	define('c', cbuf, CurEnv);
2809536Seric 
2819536Seric 	/* time as integer, unix time, arpa time */
2829536Seric 	now = curtime();
2839536Seric 	tm = gmtime(&now);
28410710Seric 	(void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1,
2859536Seric 			tm->tm_mday, tm->tm_hour, tm->tm_min);
2869536Seric 	define('t', tbuf, CurEnv);
2879536Seric 	(void) strcpy(dbuf, ctime(&now));
2889536Seric 	*index(dbuf, '\n') = '\0';
2899536Seric 	if (macvalue('d', CurEnv) == NULL)
2909536Seric 		define('d', dbuf, CurEnv);
2919536Seric 	p = newstr(arpadate(dbuf));
2929536Seric 	if (macvalue('a', CurEnv) == NULL)
2939536Seric 		define('a', p, CurEnv);
2949536Seric 	define('b', p, CurEnv);
2959536Seric 
2969536Seric 	/* tty name */
2979536Seric 	if (macvalue('y', CurEnv) == NULL)
2989536Seric 	{
2999536Seric 		p = ttyname(2);
3009536Seric 		if (p != NULL)
3019536Seric 		{
3029536Seric 			if (rindex(p, '/') != NULL)
3039536Seric 				p = rindex(p, '/') + 1;
3049536Seric 			(void) strcpy(ybuf, p);
3059536Seric 			define('y', ybuf, CurEnv);
3069536Seric 		}
3079536Seric 	}
3089536Seric }
3099536Seric /*
3109536Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
3119536Seric **
3129536Seric **	Assigns an id code if one does not already exist.
3139536Seric **	This code is very careful to avoid trashing existing files
3149536Seric **	under any circumstances.
3159536Seric **		We first create an nf file that is only used when
3169536Seric **		assigning an id.  This file is always empty, so that
3179536Seric **		we can never accidently truncate an lf file.
3189536Seric **
3199536Seric **	Parameters:
3209536Seric **		e -- envelope to build it in/from.
3219536Seric **		type -- the file type, used as the first character
3229536Seric **			of the file name.
3239536Seric **
3249536Seric **	Returns:
3259536Seric **		a pointer to the new file name (in a static buffer).
3269536Seric **
3279536Seric **	Side Effects:
3289536Seric **		Will create the lf and qf files if no id code is
3299536Seric **		already assigned.  This will cause the envelope
3309536Seric **		to be modified.
3319536Seric */
3329536Seric 
3339536Seric char *
3349536Seric queuename(e, type)
3359536Seric 	register ENVELOPE *e;
3369536Seric 	char type;
3379536Seric {
3389536Seric 	static char buf[MAXNAME];
3399536Seric 	static int pid = -1;
3409536Seric 	char c1 = 'A';
3419536Seric 	char c2 = 'A';
3429536Seric 
3439536Seric 	if (e->e_id == NULL)
3449536Seric 	{
3459536Seric 		char qf[20];
3469536Seric 		char lf[20];
3479536Seric 		char nf[20];
3489536Seric 
3499536Seric 		/* find a unique id */
3509536Seric 		if (pid != getpid())
3519536Seric 		{
3529536Seric 			/* new process -- start back at "AA" */
3539536Seric 			pid = getpid();
3549536Seric 			c1 = 'A';
3559536Seric 			c2 = 'A' - 1;
3569536Seric 		}
3579536Seric 		(void) sprintf(qf, "qfAA%05d", pid);
3589536Seric 		strcpy(lf, qf);
3599536Seric 		lf[0] = 'l';
3609536Seric 		strcpy(nf, qf);
3619536Seric 		nf[0] = 'n';
3629536Seric 
3639536Seric 		while (c1 < '~' || c2 < 'Z')
3649536Seric 		{
3659536Seric 			int i;
3669536Seric 
3679536Seric 			if (c2 >= 'Z')
3689536Seric 			{
3699536Seric 				c1++;
3709536Seric 				c2 = 'A' - 1;
3719536Seric 			}
3729536Seric 			qf[2] = lf[2] = nf[2] = c1;
3739536Seric 			qf[3] = lf[3] = nf[3] = ++c2;
3749536Seric # ifdef DEBUG
3759536Seric 			if (tTd(7, 20))
3769536Seric 				printf("queuename: trying \"%s\"\n", nf);
3779536Seric # endif DEBUG
37810754Seric # ifdef QUEUE
3799536Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
3809536Seric 				continue;
3819536Seric 			errno = 0;
3829536Seric 			i = creat(nf, FileMode);
3839536Seric 			if (i < 0)
3849536Seric 			{
3859536Seric 				(void) unlink(nf);	/* kernel bug */
3869536Seric 				continue;
3879536Seric 			}
3889536Seric 			(void) close(i);
3899536Seric 			i = link(nf, lf);
3909536Seric 			(void) unlink(nf);
3919536Seric 			if (i < 0)
3929536Seric 				continue;
3939536Seric 			if (link(lf, qf) >= 0)
3949536Seric 				break;
3959536Seric 			(void) unlink(lf);
39610754Seric # else QUEUE
39710754Seric 			if (close(creat(qf, FileMode)) < 0)
39810754Seric 				continue;
39910754Seric # endif QUEUE
4009536Seric 		}
4019536Seric 		if (c1 >= '~' && c2 >= 'Z')
4029536Seric 		{
4039536Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
4049536Seric 				lf, QueueDir);
4059536Seric 			exit(EX_OSERR);
4069536Seric 		}
4079536Seric 		e->e_id = newstr(&qf[2]);
4089536Seric 		define('i', e->e_id, e);
4099536Seric # ifdef DEBUG
4109536Seric 		if (tTd(7, 1))
4119536Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
41210196Seric # ifdef LOG
41310196Seric 		if (LogLevel > 16)
41410196Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
41510196Seric # endif LOG
4169536Seric # endif DEBUG
4179536Seric 	}
4189536Seric 
4199536Seric 	if (type == '\0')
4209536Seric 		return (NULL);
4219536Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
4229536Seric # ifdef DEBUG
4239536Seric 	if (tTd(7, 2))
4249536Seric 		printf("queuename: %s\n", buf);
4259536Seric # endif DEBUG
4269536Seric 	return (buf);
4279536Seric }
4289536Seric /*
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 
4449536Seric openxscript(e)
4459536Seric 	register ENVELOPE *e;
4469536Seric {
4479536Seric 	register char *p;
4489536Seric 
44910196Seric # ifdef LOG
45010196Seric 	if (LogLevel > 19)
45110196Seric 		syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)");
45210196Seric # endif LOG
4539536Seric 	if (e->e_xfp != NULL)
4549536Seric 		return;
4559536Seric 	p = queuename(e, 'x');
4569536Seric 	e->e_xfp = fopen(p, "w");
4579536Seric 	if (e->e_xfp == NULL)
4589536Seric 		syserr("Can't create %s", p);
4599536Seric 	else
4609536Seric 		(void) chmod(p, 0644);
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 
47510196Seric closexscript(e)
47610196Seric 	register ENVELOPE *e;
47710196Seric {
47810196Seric 	if (e->e_xfp == NULL)
47910196Seric 		return;
48010196Seric 	(void) fclose(e->e_xfp);
48110196Seric 	e->e_xfp = NULL;
48210196Seric }
48310196Seric /*
4849536Seric **  SETSENDER -- set the person who this message is from
4859536Seric **
4869536Seric **	Under certain circumstances allow the user to say who
4879536Seric **	s/he is (using -f or -r).  These are:
4889536Seric **	1.  The user's uid is zero (root).
4899536Seric **	2.  The user's login name is in an approved list (typically
4909536Seric **	    from a network server).
4919536Seric **	3.  The address the user is trying to claim has a
4929536Seric **	    "!" character in it (since #2 doesn't do it for
4939536Seric **	    us if we are dialing out for UUCP).
4949536Seric **	A better check to replace #3 would be if the
4959536Seric **	effective uid is "UUCP" -- this would require me
4969536Seric **	to rewrite getpwent to "grab" uucp as it went by,
4979536Seric **	make getname more nasty, do another passwd file
4989536Seric **	scan, or compile the UID of "UUCP" into the code,
4999536Seric **	all of which are reprehensible.
5009536Seric **
5019536Seric **	Assuming all of these fail, we figure out something
5029536Seric **	ourselves.
5039536Seric **
5049536Seric **	Parameters:
5059536Seric **		from -- the person we would like to believe this message
5069536Seric **			is from, as specified on the command line.
5079536Seric **
5089536Seric **	Returns:
5099536Seric **		none.
5109536Seric **
5119536Seric **	Side Effects:
5129536Seric **		sets sendmail's notion of who the from person is.
5139536Seric */
5149536Seric 
5159536Seric setsender(from)
5169536Seric 	char *from;
5179536Seric {
5189536Seric 	register char **pvp;
5199536Seric 	register struct passwd *pw = NULL;
5209536Seric 	char *realname = NULL;
5219536Seric 	char buf[MAXNAME];
5229536Seric 	extern char *macvalue();
5239536Seric 	extern char **prescan();
5249536Seric 	extern bool safefile();
5259536Seric 	extern char *FullName;
5269536Seric 
5279536Seric # ifdef DEBUG
5289536Seric 	if (tTd(45, 1))
5299536Seric 		printf("setsender(%s)\n", from);
5309536Seric # endif DEBUG
5319536Seric 
5329536Seric 	/*
5339536Seric 	**  Figure out the real user executing us.
5349536Seric 	**	Username can return errno != 0 on non-errors.
5359536Seric 	*/
5369536Seric 
5379536Seric 	if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
5389536Seric 		realname = from;
5399536Seric 	if (realname == NULL || realname[0] == '\0')
5409536Seric 	{
5419536Seric 		extern char *username();
5429536Seric 
5439536Seric 		realname = username();
5449536Seric 		errno = 0;
5459536Seric 	}
5469536Seric 	if (realname == NULL || realname[0] == '\0')
5479536Seric 	{
5489536Seric 		extern struct passwd *getpwuid();
5499536Seric 
5509536Seric 		pw = getpwuid(getruid());
5519536Seric 		if (pw != NULL)
5529536Seric 			realname = pw->pw_name;
5539536Seric 	}
5549536Seric 	if (realname == NULL || realname[0] == '\0')
5559536Seric 	{
5569536Seric 		syserr("Who are you?");
5579536Seric 		realname = "root";
5589536Seric 	}
5599536Seric 
5609536Seric 	/*
5619536Seric 	**  Determine if this real person is allowed to alias themselves.
5629536Seric 	*/
5639536Seric 
5649536Seric 	if (from != NULL)
5659536Seric 	{
5669536Seric 		extern bool trusteduser();
5679536Seric 
5689536Seric 		if (!trusteduser(realname) &&
5699536Seric # ifdef DEBUG
5709536Seric 		    (!tTd(1, 9) || getuid() != geteuid()) &&
5719536Seric # endif DEBUG
5729536Seric 		    index(from, '!') == NULL && getuid() != 0)
5739536Seric 		{
5749536Seric 			/* network sends -r regardless (why why why?) */
5759536Seric 			/* syserr("%s, you cannot use the -f flag", realname); */
5769536Seric 			from = NULL;
5779536Seric 		}
5789536Seric 	}
5799536Seric 
5809536Seric 	SuprErrs = TRUE;
581*11447Seric 	if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL)
5829536Seric 	{
5839536Seric 		from = newstr(realname);
584*11447Seric 		(void) parseaddr(from, &CurEnv->e_from, 1, '\0');
5859536Seric 	}
5869536Seric 	else
5879536Seric 		FromFlag = TRUE;
5889536Seric 	CurEnv->e_from.q_flags |= QDONTSEND;
5899536Seric 	SuprErrs = FALSE;
5909536Seric 
5919536Seric 	if (pw == NULL && CurEnv->e_from.q_mailer == LocalMailer)
5929536Seric 	{
5939536Seric 		extern struct passwd *getpwnam();
5949536Seric 
5959536Seric 		pw = getpwnam(CurEnv->e_from.q_user);
5969536Seric 	}
5979536Seric 
5989536Seric 	/*
5999536Seric 	**  Process passwd file entry.
6009536Seric 	*/
6019536Seric 
6029536Seric 	if (pw != NULL)
6039536Seric 	{
6049536Seric 		/* extract home directory */
6059536Seric 		CurEnv->e_from.q_home = newstr(pw->pw_dir);
6069536Seric 
6079536Seric 		/* run user's .mailcf file */
6089536Seric 		define('z', CurEnv->e_from.q_home, CurEnv);
6099536Seric 		expand("$z/.mailcf", buf, &buf[sizeof buf - 1], CurEnv);
6109536Seric 		if (safefile(buf, getruid(), S_IREAD))
6119536Seric 			readcf(buf, FALSE);
6129536Seric 
6139536Seric 		/* if the user has given fullname already, don't redefine */
6149536Seric 		if (FullName == NULL)
6159536Seric 			FullName = macvalue('x', CurEnv);
6169536Seric 		if (FullName[0] == '\0')
6179536Seric 			FullName = NULL;
6189536Seric 
6199536Seric 		/* extract full name from passwd file */
6209582Seric 		if (FullName == NULL && pw->pw_gecos != NULL &&
6219582Seric 		    strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
6229536Seric 		{
6239536Seric 			buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
6249536Seric 			if (buf[0] != '\0')
6259536Seric 				FullName = newstr(buf);
6269536Seric 		}
6279536Seric 		if (FullName != NULL)
6289536Seric 			define('x', FullName, CurEnv);
6299536Seric 	}
6309536Seric 
6319536Seric #ifndef V6
6329536Seric 	if (CurEnv->e_from.q_home == NULL)
6339536Seric 		CurEnv->e_from.q_home = getenv("HOME");
6349536Seric #endif V6
6359536Seric 	CurEnv->e_from.q_uid = getuid();
6369536Seric 	CurEnv->e_from.q_gid = getgid();
6379536Seric 	if (CurEnv->e_from.q_uid != 0)
6389536Seric 	{
6399536Seric 		DefUid = CurEnv->e_from.q_uid;
6409536Seric 		DefGid = CurEnv->e_from.q_gid;
6419536Seric 	}
6429536Seric 
6439536Seric 	/*
6449536Seric 	**  Rewrite the from person to dispose of possible implicit
6459536Seric 	**	links in the net.
6469536Seric 	*/
6479536Seric 
6489536Seric 	pvp = prescan(from, '\0');
6499536Seric 	if (pvp == NULL)
6509536Seric 	{
6519536Seric 		syserr("cannot prescan from (%s)", from);
6529536Seric 		finis();
6539536Seric 	}
6549536Seric 	rewrite(pvp, 3);
6559536Seric 	rewrite(pvp, 1);
65611286Seric 	rewrite(pvp, 4);
6579536Seric 	cataddr(pvp, buf, sizeof buf);
6589536Seric 	define('f', newstr(buf), CurEnv);
6599536Seric 
6609536Seric 	/* save the domain spec if this mailer wants it */
66110690Seric 	if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
6629536Seric 	{
6639536Seric 		extern char **copyplist();
6649536Seric 
6659536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
6669536Seric 			pvp++;
6679536Seric 		if (*pvp != NULL)
6689536Seric 			CurEnv->e_fromdomain = copyplist(pvp, TRUE);
6699536Seric 	}
6709536Seric }
6719536Seric /*
6729536Seric **  TRUSTEDUSER -- tell us if this user is to be trusted.
6739536Seric **
6749536Seric **	Parameters:
6759536Seric **		user -- the user to be checked.
6769536Seric **
6779536Seric **	Returns:
6789536Seric **		TRUE if the user is in an approved list.
6799536Seric **		FALSE otherwise.
6809536Seric **
6819536Seric **	Side Effects:
6829536Seric **		none.
6839536Seric */
6849536Seric 
6859536Seric bool
6869536Seric trusteduser(user)
6879536Seric 	char *user;
6889536Seric {
6899536Seric 	register char **ulist;
6909536Seric 	extern char *TrustedUsers[];
6919536Seric 
6929536Seric 	for (ulist = TrustedUsers; *ulist != NULL; ulist++)
6939536Seric 		if (strcmp(*ulist, user) == 0)
6949536Seric 			return (TRUE);
6959536Seric 	return (FALSE);
6969536Seric }
697