19536Seric #include <pwd.h>
29536Seric #include <time.h>
39536Seric #include "sendmail.h"
49536Seric #include <sys/stat.h>
59536Seric 
6*10196Seric SCCSID(@(#)envelope.c	3.6		01/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 	{
1119536Seric 		auto ADDRESS *rlist;
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 
12110094Seric 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags))
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))
1389536Seric 		queueup(e, FALSE, FALSE);
1399536Seric 
1409536Seric 	/* now unlock the job */
141*10196Seric 	closexscript(e);
1429536Seric 	unlockqueue(e);
1439536Seric 
1449536Seric 	/* make sure that this envelope is marked unused */
1459536Seric 	e->e_id = e->e_df = NULL;
146*10196Seric 	e->e_dfp = NULL;
1479536Seric }
1489536Seric /*
1499536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
1509536Seric **
1519536Seric **	This is normally used by a child process to get a clean
1529536Seric **	envelope without disturbing the parent.
1539536Seric **
1549536Seric **	Parameters:
1559536Seric **		e -- the envelope to clear.
1569536Seric **
1579536Seric **	Returns:
1589536Seric **		none.
1599536Seric **
1609536Seric **	Side Effects:
1619536Seric **		Closes files associated with the envelope.
1629536Seric **		Marks the envelope as unallocated.
1639536Seric */
1649536Seric 
1659536Seric clearenvelope(e)
1669536Seric 	register ENVELOPE *e;
1679536Seric {
1689536Seric 	/* clear out any file information */
1699536Seric 	if (e->e_xfp != NULL)
1709536Seric 		(void) fclose(e->e_xfp);
1719536Seric 	if (e->e_dfp != NULL)
1729536Seric 		(void) fclose(e->e_dfp);
1739536Seric 	e->e_xfp = e->e_dfp = NULL;
1749536Seric 
1759536Seric 	/* now expunge names of objects */
1769536Seric 	e->e_df = e->e_id = NULL;
1779536Seric 
1789536Seric 	/* and the flags which are now meaningless */
1799536Seric 	e->e_flags = 0;
1809536Seric }
1819536Seric /*
1829536Seric **  UNLOCKQUEUE -- unlock the queue entry for a specified envelope
1839536Seric **
1849536Seric **	Parameters:
1859536Seric **		e -- the envelope to unlock.
1869536Seric **
1879536Seric **	Returns:
1889536Seric **		none
1899536Seric **
1909536Seric **	Side Effects:
1919536Seric **		unlocks the queue for `e'.
1929536Seric */
1939536Seric 
1949536Seric unlockqueue(e)
1959536Seric 	ENVELOPE *e;
1969536Seric {
1979536Seric 	/* remove the transcript */
1989536Seric #ifdef DEBUG
199*10196Seric # ifdef LOG
200*10196Seric 	if (LogLevel > 19)
201*10196Seric 		syslog(LOG_DEBUG, "%s: unlock", e->e_id);
202*10196Seric # endif LOG
2039536Seric 	if (!tTd(51, 4))
2049536Seric #endif DEBUG
2059536Seric 		xunlink(queuename(e, 'x'));
2069536Seric 
2079536Seric 	/* last but not least, remove the lock */
2089536Seric 	xunlink(queuename(e, 'l'));
2099536Seric }
2109536Seric /*
2119536Seric **  INITSYS -- initialize instantiation of system
2129536Seric **
2139536Seric **	In Daemon mode, this is done in the child.
2149536Seric **
2159536Seric **	Parameters:
2169536Seric **		none.
2179536Seric **
2189536Seric **	Returns:
2199536Seric **		none.
2209536Seric **
2219536Seric **	Side Effects:
2229536Seric **		Initializes the system macros, some global variables,
2239536Seric **		etc.  In particular, the current time in various
2249536Seric **		forms is set.
2259536Seric */
2269536Seric 
2279536Seric initsys()
2289536Seric {
2299536Seric 	auto time_t now;
2309536Seric 	static char cbuf[5];			/* holds hop count */
2319536Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
2329536Seric 	static char pbuf[10];			/* holds pid */
2339536Seric 	static char tbuf[20];			/* holds "current" time */
2349536Seric 	static char ybuf[10];			/* holds tty id */
2359536Seric 	register char *p;
2369536Seric 	register struct tm *tm;
2379536Seric 	extern char *ttyname();
2389536Seric 	extern char *arpadate();
2399536Seric 	extern struct tm *gmtime();
2409536Seric 	extern char *macvalue();
2419536Seric 	extern char Version[];
2429536Seric 
2439536Seric 	/*
2449536Seric 	**  Give this envelope a reality.
2459536Seric 	**	I.e., an id, a transcript, and a creation time.
2469536Seric 	*/
2479536Seric 
2489536Seric 	openxscript(CurEnv);
2499536Seric 	CurEnv->e_ctime = curtime();
2509536Seric 
2519536Seric 	/*
2529536Seric 	**  Set OutChannel to something useful if stdout isn't it.
2539536Seric 	**	This arranges that any extra stuff the mailer produces
2549536Seric 	**	gets sent back to the user on error (because it is
2559536Seric 	**	tucked away in the transcript).
2569536Seric 	*/
2579536Seric 
2589536Seric 	if (OpMode == MD_DAEMON && QueueRun)
2599536Seric 		OutChannel = CurEnv->e_xfp;
2609536Seric 
2619536Seric 	/*
2629536Seric 	**  Set up some basic system macros.
2639536Seric 	*/
2649536Seric 
2659536Seric 	/* process id */
2669536Seric 	(void) sprintf(pbuf, "%d", getpid());
2679536Seric 	define('p', pbuf, CurEnv);
2689536Seric 
2699536Seric 	/* hop count */
2709536Seric 	(void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
2719536Seric 	define('c', cbuf, CurEnv);
2729536Seric 
2739536Seric 	/* time as integer, unix time, arpa time */
2749536Seric 	now = curtime();
2759536Seric 	tm = gmtime(&now);
2769536Seric 	(void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon,
2779536Seric 			tm->tm_mday, tm->tm_hour, tm->tm_min);
2789536Seric 	define('t', tbuf, CurEnv);
2799536Seric 	(void) strcpy(dbuf, ctime(&now));
2809536Seric 	*index(dbuf, '\n') = '\0';
2819536Seric 	if (macvalue('d', CurEnv) == NULL)
2829536Seric 		define('d', dbuf, CurEnv);
2839536Seric 	p = newstr(arpadate(dbuf));
2849536Seric 	if (macvalue('a', CurEnv) == NULL)
2859536Seric 		define('a', p, CurEnv);
2869536Seric 	define('b', p, CurEnv);
2879536Seric 
2889536Seric 	/* version */
2899536Seric 	define('v', Version, CurEnv);
2909536Seric 
2919536Seric 	/* tty name */
2929536Seric 	if (macvalue('y', CurEnv) == NULL)
2939536Seric 	{
2949536Seric 		p = ttyname(2);
2959536Seric 		if (p != NULL)
2969536Seric 		{
2979536Seric 			if (rindex(p, '/') != NULL)
2989536Seric 				p = rindex(p, '/') + 1;
2999536Seric 			(void) strcpy(ybuf, p);
3009536Seric 			define('y', ybuf, CurEnv);
3019536Seric 		}
3029536Seric 	}
3039536Seric }
3049536Seric /*
3059536Seric **  QUEUENAME -- build a file name in the queue directory for this envelope.
3069536Seric **
3079536Seric **	Assigns an id code if one does not already exist.
3089536Seric **	This code is very careful to avoid trashing existing files
3099536Seric **	under any circumstances.
3109536Seric **		We first create an nf file that is only used when
3119536Seric **		assigning an id.  This file is always empty, so that
3129536Seric **		we can never accidently truncate an lf file.
3139536Seric **
3149536Seric **	Parameters:
3159536Seric **		e -- envelope to build it in/from.
3169536Seric **		type -- the file type, used as the first character
3179536Seric **			of the file name.
3189536Seric **
3199536Seric **	Returns:
3209536Seric **		a pointer to the new file name (in a static buffer).
3219536Seric **
3229536Seric **	Side Effects:
3239536Seric **		Will create the lf and qf files if no id code is
3249536Seric **		already assigned.  This will cause the envelope
3259536Seric **		to be modified.
3269536Seric */
3279536Seric 
3289536Seric char *
3299536Seric queuename(e, type)
3309536Seric 	register ENVELOPE *e;
3319536Seric 	char type;
3329536Seric {
3339536Seric 	static char buf[MAXNAME];
3349536Seric 	static int pid = -1;
3359536Seric 	char c1 = 'A';
3369536Seric 	char c2 = 'A';
3379536Seric 
3389536Seric 	if (e->e_id == NULL)
3399536Seric 	{
3409536Seric 		char qf[20];
3419536Seric 		char lf[20];
3429536Seric 		char nf[20];
3439536Seric 
3449536Seric 		/* find a unique id */
3459536Seric 		if (pid != getpid())
3469536Seric 		{
3479536Seric 			/* new process -- start back at "AA" */
3489536Seric 			pid = getpid();
3499536Seric 			c1 = 'A';
3509536Seric 			c2 = 'A' - 1;
3519536Seric 		}
3529536Seric 		(void) sprintf(qf, "qfAA%05d", pid);
3539536Seric 		strcpy(lf, qf);
3549536Seric 		lf[0] = 'l';
3559536Seric 		strcpy(nf, qf);
3569536Seric 		nf[0] = 'n';
3579536Seric 
3589536Seric 		while (c1 < '~' || c2 < 'Z')
3599536Seric 		{
3609536Seric 			int i;
3619536Seric 
3629536Seric 			if (c2 >= 'Z')
3639536Seric 			{
3649536Seric 				c1++;
3659536Seric 				c2 = 'A' - 1;
3669536Seric 			}
3679536Seric 			qf[2] = lf[2] = nf[2] = c1;
3689536Seric 			qf[3] = lf[3] = nf[3] = ++c2;
3699536Seric # ifdef DEBUG
3709536Seric 			if (tTd(7, 20))
3719536Seric 				printf("queuename: trying \"%s\"\n", nf);
3729536Seric # endif DEBUG
3739536Seric 			if (access(lf, 0) >= 0 || access(qf, 0) >= 0)
3749536Seric 				continue;
3759536Seric 			errno = 0;
3769536Seric 			i = creat(nf, FileMode);
3779536Seric 			if (i < 0)
3789536Seric 			{
3799536Seric 				(void) unlink(nf);	/* kernel bug */
3809536Seric 				continue;
3819536Seric 			}
3829536Seric 			(void) close(i);
3839536Seric 			i = link(nf, lf);
3849536Seric 			(void) unlink(nf);
3859536Seric 			if (i < 0)
3869536Seric 				continue;
3879536Seric 			if (link(lf, qf) >= 0)
3889536Seric 				break;
3899536Seric 			(void) unlink(lf);
3909536Seric 		}
3919536Seric 		if (c1 >= '~' && c2 >= 'Z')
3929536Seric 		{
3939536Seric 			syserr("queuename: Cannot create \"%s\" in \"%s\"",
3949536Seric 				lf, QueueDir);
3959536Seric 			exit(EX_OSERR);
3969536Seric 		}
3979536Seric 		e->e_id = newstr(&qf[2]);
3989536Seric 		define('i', e->e_id, e);
3999536Seric # ifdef DEBUG
4009536Seric 		if (tTd(7, 1))
4019536Seric 			printf("queuename: assigned id %s, env=%x\n", e->e_id, e);
402*10196Seric # ifdef LOG
403*10196Seric 		if (LogLevel > 16)
404*10196Seric 			syslog(LOG_DEBUG, "%s: assigned id", e->e_id);
405*10196Seric # endif LOG
4069536Seric # endif DEBUG
4079536Seric 	}
4089536Seric 
4099536Seric 	if (type == '\0')
4109536Seric 		return (NULL);
4119536Seric 	(void) sprintf(buf, "%cf%s", type, e->e_id);
4129536Seric # ifdef DEBUG
4139536Seric 	if (tTd(7, 2))
4149536Seric 		printf("queuename: %s\n", buf);
4159536Seric # endif DEBUG
4169536Seric 	return (buf);
4179536Seric }
4189536Seric /*
4199536Seric **  OPENXSCRIPT -- Open transcript file
4209536Seric **
4219536Seric **	Creates a transcript file for possible eventual mailing or
4229536Seric **	sending back.
4239536Seric **
4249536Seric **	Parameters:
4259536Seric **		e -- the envelope to create the transcript in/for.
4269536Seric **
4279536Seric **	Returns:
4289536Seric **		none
4299536Seric **
4309536Seric **	Side Effects:
4319536Seric **		Creates the transcript file.
4329536Seric */
4339536Seric 
4349536Seric openxscript(e)
4359536Seric 	register ENVELOPE *e;
4369536Seric {
4379536Seric 	register char *p;
4389536Seric 
439*10196Seric # ifdef LOG
440*10196Seric 	if (LogLevel > 19)
441*10196Seric 		syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)");
442*10196Seric # endif LOG
4439536Seric 	if (e->e_xfp != NULL)
4449536Seric 		return;
4459536Seric 	p = queuename(e, 'x');
4469536Seric 	e->e_xfp = fopen(p, "w");
4479536Seric 	if (e->e_xfp == NULL)
4489536Seric 		syserr("Can't create %s", p);
4499536Seric 	else
4509536Seric 		(void) chmod(p, 0644);
4519536Seric }
4529536Seric /*
453*10196Seric **  CLOSEXSCRIPT -- close the transcript file.
454*10196Seric **
455*10196Seric **	Parameters:
456*10196Seric **		e -- the envelope containing the transcript to close.
457*10196Seric **
458*10196Seric **	Returns:
459*10196Seric **		none.
460*10196Seric **
461*10196Seric **	Side Effects:
462*10196Seric **		none.
463*10196Seric */
464*10196Seric 
465*10196Seric closexscript(e)
466*10196Seric 	register ENVELOPE *e;
467*10196Seric {
468*10196Seric 	if (e->e_xfp == NULL)
469*10196Seric 		return;
470*10196Seric 	(void) fclose(e->e_xfp);
471*10196Seric 	e->e_xfp = NULL;
472*10196Seric }
473*10196Seric /*
4749536Seric **  SETSENDER -- set the person who this message is from
4759536Seric **
4769536Seric **	Under certain circumstances allow the user to say who
4779536Seric **	s/he is (using -f or -r).  These are:
4789536Seric **	1.  The user's uid is zero (root).
4799536Seric **	2.  The user's login name is in an approved list (typically
4809536Seric **	    from a network server).
4819536Seric **	3.  The address the user is trying to claim has a
4829536Seric **	    "!" character in it (since #2 doesn't do it for
4839536Seric **	    us if we are dialing out for UUCP).
4849536Seric **	A better check to replace #3 would be if the
4859536Seric **	effective uid is "UUCP" -- this would require me
4869536Seric **	to rewrite getpwent to "grab" uucp as it went by,
4879536Seric **	make getname more nasty, do another passwd file
4889536Seric **	scan, or compile the UID of "UUCP" into the code,
4899536Seric **	all of which are reprehensible.
4909536Seric **
4919536Seric **	Assuming all of these fail, we figure out something
4929536Seric **	ourselves.
4939536Seric **
4949536Seric **	Parameters:
4959536Seric **		from -- the person we would like to believe this message
4969536Seric **			is from, as specified on the command line.
4979536Seric **
4989536Seric **	Returns:
4999536Seric **		none.
5009536Seric **
5019536Seric **	Side Effects:
5029536Seric **		sets sendmail's notion of who the from person is.
5039536Seric */
5049536Seric 
5059536Seric setsender(from)
5069536Seric 	char *from;
5079536Seric {
5089536Seric 	register char **pvp;
5099536Seric 	register struct passwd *pw = NULL;
5109536Seric 	char *realname = NULL;
5119536Seric 	char buf[MAXNAME];
5129536Seric 	extern char *macvalue();
5139536Seric 	extern char **prescan();
5149536Seric 	extern bool safefile();
5159536Seric 	extern char *FullName;
5169536Seric 
5179536Seric # ifdef DEBUG
5189536Seric 	if (tTd(45, 1))
5199536Seric 		printf("setsender(%s)\n", from);
5209536Seric # endif DEBUG
5219536Seric 
5229536Seric 	/*
5239536Seric 	**  Figure out the real user executing us.
5249536Seric 	**	Username can return errno != 0 on non-errors.
5259536Seric 	*/
5269536Seric 
5279536Seric 	if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
5289536Seric 		realname = from;
5299536Seric 	if (realname == NULL || realname[0] == '\0')
5309536Seric 	{
5319536Seric 		extern char *username();
5329536Seric 
5339536Seric 		realname = username();
5349536Seric 		errno = 0;
5359536Seric 	}
5369536Seric 	if (realname == NULL || realname[0] == '\0')
5379536Seric 	{
5389536Seric 		extern struct passwd *getpwuid();
5399536Seric 
5409536Seric 		pw = getpwuid(getruid());
5419536Seric 		if (pw != NULL)
5429536Seric 			realname = pw->pw_name;
5439536Seric 	}
5449536Seric 	if (realname == NULL || realname[0] == '\0')
5459536Seric 	{
5469536Seric 		syserr("Who are you?");
5479536Seric 		realname = "root";
5489536Seric 	}
5499536Seric 
5509536Seric 	/*
5519536Seric 	**  Determine if this real person is allowed to alias themselves.
5529536Seric 	*/
5539536Seric 
5549536Seric 	if (from != NULL)
5559536Seric 	{
5569536Seric 		extern bool trusteduser();
5579536Seric 
5589536Seric 		if (!trusteduser(realname) &&
5599536Seric # ifdef DEBUG
5609536Seric 		    (!tTd(1, 9) || getuid() != geteuid()) &&
5619536Seric # endif DEBUG
5629536Seric 		    index(from, '!') == NULL && getuid() != 0)
5639536Seric 		{
5649536Seric 			/* network sends -r regardless (why why why?) */
5659536Seric 			/* syserr("%s, you cannot use the -f flag", realname); */
5669536Seric 			from = NULL;
5679536Seric 		}
5689536Seric 	}
5699536Seric 
5709536Seric 	SuprErrs = TRUE;
5719887Seric 	if (from == NULL || parseaddr(from, &CurEnv->e_from, 1) == NULL)
5729536Seric 	{
5739536Seric 		from = newstr(realname);
5749887Seric 		(void) parseaddr(from, &CurEnv->e_from, 1);
5759536Seric 	}
5769536Seric 	else
5779536Seric 		FromFlag = TRUE;
5789536Seric 	CurEnv->e_from.q_flags |= QDONTSEND;
5799536Seric 	SuprErrs = FALSE;
5809536Seric 
5819536Seric 	if (pw == NULL && CurEnv->e_from.q_mailer == LocalMailer)
5829536Seric 	{
5839536Seric 		extern struct passwd *getpwnam();
5849536Seric 
5859536Seric 		pw = getpwnam(CurEnv->e_from.q_user);
5869536Seric 	}
5879536Seric 
5889536Seric 	/*
5899536Seric 	**  Process passwd file entry.
5909536Seric 	*/
5919536Seric 
5929536Seric 	if (pw != NULL)
5939536Seric 	{
5949536Seric 		/* extract home directory */
5959536Seric 		CurEnv->e_from.q_home = newstr(pw->pw_dir);
5969536Seric 
5979536Seric 		/* run user's .mailcf file */
5989536Seric 		define('z', CurEnv->e_from.q_home, CurEnv);
5999536Seric 		expand("$z/.mailcf", buf, &buf[sizeof buf - 1], CurEnv);
6009536Seric 		if (safefile(buf, getruid(), S_IREAD))
6019536Seric 			readcf(buf, FALSE);
6029536Seric 
6039536Seric 		/* if the user has given fullname already, don't redefine */
6049536Seric 		if (FullName == NULL)
6059536Seric 			FullName = macvalue('x', CurEnv);
6069536Seric 		if (FullName[0] == '\0')
6079536Seric 			FullName = NULL;
6089536Seric 
6099536Seric 		/* extract full name from passwd file */
6109582Seric 		if (FullName == NULL && pw->pw_gecos != NULL &&
6119582Seric 		    strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
6129536Seric 		{
6139536Seric 			buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
6149536Seric 			if (buf[0] != '\0')
6159536Seric 				FullName = newstr(buf);
6169536Seric 		}
6179536Seric 		if (FullName != NULL)
6189536Seric 			define('x', FullName, CurEnv);
6199536Seric 	}
6209536Seric 
6219536Seric #ifndef V6
6229536Seric 	if (CurEnv->e_from.q_home == NULL)
6239536Seric 		CurEnv->e_from.q_home = getenv("HOME");
6249536Seric #endif V6
6259536Seric 	CurEnv->e_from.q_uid = getuid();
6269536Seric 	CurEnv->e_from.q_gid = getgid();
6279536Seric 	if (CurEnv->e_from.q_uid != 0)
6289536Seric 	{
6299536Seric 		DefUid = CurEnv->e_from.q_uid;
6309536Seric 		DefGid = CurEnv->e_from.q_gid;
6319536Seric 	}
6329536Seric 
6339536Seric 	/*
6349536Seric 	**  Rewrite the from person to dispose of possible implicit
6359536Seric 	**	links in the net.
6369536Seric 	*/
6379536Seric 
6389536Seric 	pvp = prescan(from, '\0');
6399536Seric 	if (pvp == NULL)
6409536Seric 	{
6419536Seric 		syserr("cannot prescan from (%s)", from);
6429536Seric 		finis();
6439536Seric 	}
6449536Seric 	rewrite(pvp, 3);
6459536Seric 	rewrite(pvp, 1);
6469536Seric 	cataddr(pvp, buf, sizeof buf);
6479536Seric 	define('f', newstr(buf), CurEnv);
6489536Seric 
6499536Seric 	/* save the domain spec if this mailer wants it */
6509536Seric 	if (bitset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
6519536Seric 	{
6529536Seric 		extern char **copyplist();
6539536Seric 
6549536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
6559536Seric 			pvp++;
6569536Seric 		if (*pvp != NULL)
6579536Seric 			CurEnv->e_fromdomain = copyplist(pvp, TRUE);
6589536Seric 	}
6599536Seric }
6609536Seric /*
6619536Seric **  TRUSTEDUSER -- tell us if this user is to be trusted.
6629536Seric **
6639536Seric **	Parameters:
6649536Seric **		user -- the user to be checked.
6659536Seric **
6669536Seric **	Returns:
6679536Seric **		TRUE if the user is in an approved list.
6689536Seric **		FALSE otherwise.
6699536Seric **
6709536Seric **	Side Effects:
6719536Seric **		none.
6729536Seric */
6739536Seric 
6749536Seric bool
6759536Seric trusteduser(user)
6769536Seric 	char *user;
6779536Seric {
6789536Seric 	register char **ulist;
6799536Seric 	extern char *TrustedUsers[];
6809536Seric 
6819536Seric 	for (ulist = TrustedUsers; *ulist != NULL; ulist++)
6829536Seric 		if (strcmp(*ulist, user) == 0)
6839536Seric 			return (TRUE);
6849536Seric 	return (FALSE);
6859536Seric }
686