19536Seric #include <pwd.h>
213587Swnj #include <sys/time.h>
39536Seric #include "sendmail.h"
49536Seric #include <sys/stat.h>
59536Seric 
6*21750Seric SCCSID(@(#)envelope.c	4.10		06/01/85);
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;
3616888Seric 	bzero((char *) e, sizeof *e);
3716888Seric 	bcopy((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);
4716888Seric 		bcopy((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 		xunlink(queuename(e, 'q'));
1349536Seric 	}
1359536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
13610754Seric 	{
13710754Seric #ifdef QUEUE
1389536Seric 		queueup(e, FALSE, FALSE);
13910754Seric #else QUEUE
14010754Seric 		syserr("dropenvelope: queueup");
14110754Seric #endif QUEUE
14210754Seric 	}
1439536Seric 
1449536Seric 	/* now unlock the job */
14510196Seric 	closexscript(e);
1469536Seric 	unlockqueue(e);
1479536Seric 
1489536Seric 	/* make sure that this envelope is marked unused */
149*21750Seric 	if (e->e_df != NULL)
150*21750Seric 		xunlink(e->e_df);
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 **  INITSYS -- initialize instantiation of system
1899536Seric **
1909536Seric **	In Daemon mode, this is done in the child.
1919536Seric **
1929536Seric **	Parameters:
1939536Seric **		none.
1949536Seric **
1959536Seric **	Returns:
1969536Seric **		none.
1979536Seric **
1989536Seric **	Side Effects:
1999536Seric **		Initializes the system macros, some global variables,
2009536Seric **		etc.  In particular, the current time in various
2019536Seric **		forms is set.
2029536Seric */
2039536Seric 
2049536Seric initsys()
2059536Seric {
2069536Seric 	static char cbuf[5];			/* holds hop count */
2079536Seric 	static char pbuf[10];			/* holds pid */
2089536Seric 	static char ybuf[10];			/* holds tty id */
2099536Seric 	register char *p;
2109536Seric 	extern char *ttyname();
2119536Seric 	extern char *macvalue();
2129536Seric 	extern char Version[];
2139536Seric 
2149536Seric 	/*
2159536Seric 	**  Give this envelope a reality.
2169536Seric 	**	I.e., an id, a transcript, and a creation time.
2179536Seric 	*/
2189536Seric 
2199536Seric 	openxscript(CurEnv);
2209536Seric 	CurEnv->e_ctime = curtime();
2219536Seric 
2229536Seric 	/*
2239536Seric 	**  Set OutChannel to something useful if stdout isn't it.
2249536Seric 	**	This arranges that any extra stuff the mailer produces
2259536Seric 	**	gets sent back to the user on error (because it is
2269536Seric 	**	tucked away in the transcript).
2279536Seric 	*/
2289536Seric 
2299536Seric 	if (OpMode == MD_DAEMON && QueueRun)
2309536Seric 		OutChannel = CurEnv->e_xfp;
2319536Seric 
2329536Seric 	/*
2339536Seric 	**  Set up some basic system macros.
2349536Seric 	*/
2359536Seric 
2369536Seric 	/* process id */
2379536Seric 	(void) sprintf(pbuf, "%d", getpid());
2389536Seric 	define('p', pbuf, CurEnv);
2399536Seric 
2409536Seric 	/* hop count */
2419536Seric 	(void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
2429536Seric 	define('c', cbuf, CurEnv);
2439536Seric 
2449536Seric 	/* time as integer, unix time, arpa time */
24511932Seric 	settime();
2469536Seric 
24717472Seric #ifdef TTYNAME
2489536Seric 	/* tty name */
2499536Seric 	if (macvalue('y', CurEnv) == NULL)
2509536Seric 	{
2519536Seric 		p = ttyname(2);
2529536Seric 		if (p != NULL)
2539536Seric 		{
2549536Seric 			if (rindex(p, '/') != NULL)
2559536Seric 				p = rindex(p, '/') + 1;
2569536Seric 			(void) strcpy(ybuf, p);
2579536Seric 			define('y', ybuf, CurEnv);
2589536Seric 		}
2599536Seric 	}
26017472Seric #endif TTYNAME
2619536Seric }
2629536Seric /*
26311932Seric **  SETTIME -- set the current time.
26411932Seric **
26511932Seric **	Parameters:
26611932Seric **		none.
26711932Seric **
26811932Seric **	Returns:
26911932Seric **		none.
27011932Seric **
27111932Seric **	Side Effects:
27211932Seric **		Sets the various time macros -- $a, $b, $d, $t.
27311932Seric */
27411932Seric 
27511932Seric settime()
27611932Seric {
27711932Seric 	register char *p;
27811932Seric 	auto time_t now;
27911932Seric 	static char tbuf[20];			/* holds "current" time */
28011932Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
28111932Seric 	register struct tm *tm;
28211932Seric 	extern char *arpadate();
28311932Seric 	extern struct tm *gmtime();
28411932Seric 	extern char *macvalue();
28511932Seric 
28611932Seric 	now = curtime();
28711932Seric 	tm = gmtime(&now);
28811932Seric 	(void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1,
28911932Seric 			tm->tm_mday, tm->tm_hour, tm->tm_min);
29011932Seric 	define('t', tbuf, CurEnv);
29111932Seric 	(void) strcpy(dbuf, ctime(&now));
29211932Seric 	*index(dbuf, '\n') = '\0';
29311932Seric 	if (macvalue('d', CurEnv) == NULL)
29411932Seric 		define('d', dbuf, CurEnv);
29511932Seric 	p = newstr(arpadate(dbuf));
29611932Seric 	if (macvalue('a', CurEnv) == NULL)
29711932Seric 		define('a', p, CurEnv);
29811932Seric 	define('b', p, CurEnv);
29911932Seric }
30011932Seric /*
3019536Seric **  OPENXSCRIPT -- Open transcript file
3029536Seric **
3039536Seric **	Creates a transcript file for possible eventual mailing or
3049536Seric **	sending back.
3059536Seric **
3069536Seric **	Parameters:
3079536Seric **		e -- the envelope to create the transcript in/for.
3089536Seric **
3099536Seric **	Returns:
3109536Seric **		none
3119536Seric **
3129536Seric **	Side Effects:
3139536Seric **		Creates the transcript file.
3149536Seric */
3159536Seric 
3169536Seric openxscript(e)
3179536Seric 	register ENVELOPE *e;
3189536Seric {
3199536Seric 	register char *p;
3209536Seric 
32110196Seric # ifdef LOG
32210196Seric 	if (LogLevel > 19)
32310196Seric 		syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)");
32410196Seric # endif LOG
3259536Seric 	if (e->e_xfp != NULL)
3269536Seric 		return;
3279536Seric 	p = queuename(e, 'x');
3289536Seric 	e->e_xfp = fopen(p, "w");
3299536Seric 	if (e->e_xfp == NULL)
3309536Seric 		syserr("Can't create %s", p);
3319536Seric 	else
3329536Seric 		(void) chmod(p, 0644);
3339536Seric }
3349536Seric /*
33510196Seric **  CLOSEXSCRIPT -- close the transcript file.
33610196Seric **
33710196Seric **	Parameters:
33810196Seric **		e -- the envelope containing the transcript to close.
33910196Seric **
34010196Seric **	Returns:
34110196Seric **		none.
34210196Seric **
34310196Seric **	Side Effects:
34410196Seric **		none.
34510196Seric */
34610196Seric 
34710196Seric closexscript(e)
34810196Seric 	register ENVELOPE *e;
34910196Seric {
35010196Seric 	if (e->e_xfp == NULL)
35110196Seric 		return;
35210196Seric 	(void) fclose(e->e_xfp);
35310196Seric 	e->e_xfp = NULL;
35410196Seric }
35510196Seric /*
3569536Seric **  SETSENDER -- set the person who this message is from
3579536Seric **
3589536Seric **	Under certain circumstances allow the user to say who
3599536Seric **	s/he is (using -f or -r).  These are:
3609536Seric **	1.  The user's uid is zero (root).
3619536Seric **	2.  The user's login name is in an approved list (typically
3629536Seric **	    from a network server).
3639536Seric **	3.  The address the user is trying to claim has a
3649536Seric **	    "!" character in it (since #2 doesn't do it for
3659536Seric **	    us if we are dialing out for UUCP).
3669536Seric **	A better check to replace #3 would be if the
3679536Seric **	effective uid is "UUCP" -- this would require me
3689536Seric **	to rewrite getpwent to "grab" uucp as it went by,
3699536Seric **	make getname more nasty, do another passwd file
3709536Seric **	scan, or compile the UID of "UUCP" into the code,
3719536Seric **	all of which are reprehensible.
3729536Seric **
3739536Seric **	Assuming all of these fail, we figure out something
3749536Seric **	ourselves.
3759536Seric **
3769536Seric **	Parameters:
3779536Seric **		from -- the person we would like to believe this message
3789536Seric **			is from, as specified on the command line.
3799536Seric **
3809536Seric **	Returns:
3819536Seric **		none.
3829536Seric **
3839536Seric **	Side Effects:
3849536Seric **		sets sendmail's notion of who the from person is.
3859536Seric */
3869536Seric 
3879536Seric setsender(from)
3889536Seric 	char *from;
3899536Seric {
3909536Seric 	register char **pvp;
3919536Seric 	char *realname = NULL;
39218665Seric 	register struct passwd *pw;
3939536Seric 	char buf[MAXNAME];
39416913Seric 	char pvpbuf[PSBUFSIZE];
39518665Seric 	extern struct passwd *getpwnam();
3969536Seric 	extern char *macvalue();
3979536Seric 	extern char **prescan();
3989536Seric 	extern bool safefile();
3999536Seric 	extern char *FullName;
4009536Seric 
4019536Seric # ifdef DEBUG
4029536Seric 	if (tTd(45, 1))
40314786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
4049536Seric # endif DEBUG
4059536Seric 
4069536Seric 	/*
4079536Seric 	**  Figure out the real user executing us.
4089536Seric 	**	Username can return errno != 0 on non-errors.
4099536Seric 	*/
4109536Seric 
4119536Seric 	if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
4129536Seric 		realname = from;
4139536Seric 	if (realname == NULL || realname[0] == '\0')
4149536Seric 	{
4159536Seric 		extern char *username();
4169536Seric 
4179536Seric 		realname = username();
4189536Seric 	}
4199536Seric 
4209536Seric 	/*
4219536Seric 	**  Determine if this real person is allowed to alias themselves.
4229536Seric 	*/
4239536Seric 
4249536Seric 	if (from != NULL)
4259536Seric 	{
4269536Seric 		extern bool trusteduser();
4279536Seric 
4289536Seric 		if (!trusteduser(realname) &&
4299536Seric # ifdef DEBUG
4309536Seric 		    (!tTd(1, 9) || getuid() != geteuid()) &&
4319536Seric # endif DEBUG
4329536Seric 		    index(from, '!') == NULL && getuid() != 0)
4339536Seric 		{
4349536Seric 			/* network sends -r regardless (why why why?) */
4359536Seric 			/* syserr("%s, you cannot use the -f flag", realname); */
4369536Seric 			from = NULL;
4379536Seric 		}
4389536Seric 	}
4399536Seric 
4409536Seric 	SuprErrs = TRUE;
44111447Seric 	if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL)
4429536Seric 	{
443*21750Seric 		/* log garbage addresses for traceback */
444*21750Seric 		if (from != NULL)
445*21750Seric 		{
446*21750Seric 			syslog(LOG_ERR, "Unparseable user %s wants to be %s",
447*21750Seric 					realname, from);
448*21750Seric 		}
4499536Seric 		from = newstr(realname);
45011447Seric 		(void) parseaddr(from, &CurEnv->e_from, 1, '\0');
4519536Seric 	}
4529536Seric 	else
4539536Seric 		FromFlag = TRUE;
4549536Seric 	CurEnv->e_from.q_flags |= QDONTSEND;
45516162Seric 	loweraddr(&CurEnv->e_from);
4569536Seric 	SuprErrs = FALSE;
4579536Seric 
45818665Seric 	if (CurEnv->e_from.q_mailer == LocalMailer &&
45918665Seric 	    (pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
4609536Seric 	{
46117472Seric 		/*
46217472Seric 		**  Process passwd file entry.
46317472Seric 		*/
46417472Seric 
4659536Seric 
4669536Seric 		/* extract home directory */
4679536Seric 		CurEnv->e_from.q_home = newstr(pw->pw_dir);
46816481Seric 		define('z', CurEnv->e_from.q_home, CurEnv);
4699536Seric 
47011625Seric 		/* extract user and group id */
47111625Seric 		CurEnv->e_from.q_uid = pw->pw_uid;
47211625Seric 		CurEnv->e_from.q_gid = pw->pw_gid;
47311625Seric 
4749536Seric 		/* if the user has given fullname already, don't redefine */
4759536Seric 		if (FullName == NULL)
4769536Seric 			FullName = macvalue('x', CurEnv);
47711932Seric 		if (FullName != NULL && FullName[0] == '\0')
4789536Seric 			FullName = NULL;
4799536Seric 
4809536Seric 		/* extract full name from passwd file */
4819582Seric 		if (FullName == NULL && pw->pw_gecos != NULL &&
4829582Seric 		    strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
4839536Seric 		{
4849536Seric 			buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
4859536Seric 			if (buf[0] != '\0')
4869536Seric 				FullName = newstr(buf);
4879536Seric 		}
4889536Seric 		if (FullName != NULL)
4899536Seric 			define('x', FullName, CurEnv);
4909536Seric 	}
49111625Seric 	else
49211625Seric 	{
4939536Seric #ifndef V6
49411625Seric 		if (CurEnv->e_from.q_home == NULL)
49511625Seric 			CurEnv->e_from.q_home = getenv("HOME");
4969536Seric #endif V6
49711625Seric 		CurEnv->e_from.q_uid = getuid();
49811625Seric 		CurEnv->e_from.q_gid = getgid();
49911625Seric 	}
50011625Seric 
5019536Seric 	if (CurEnv->e_from.q_uid != 0)
5029536Seric 	{
5039536Seric 		DefUid = CurEnv->e_from.q_uid;
5049536Seric 		DefGid = CurEnv->e_from.q_gid;
5059536Seric 	}
5069536Seric 
5079536Seric 	/*
5089536Seric 	**  Rewrite the from person to dispose of possible implicit
5099536Seric 	**	links in the net.
5109536Seric 	*/
5119536Seric 
51216913Seric 	pvp = prescan(from, '\0', pvpbuf);
5139536Seric 	if (pvp == NULL)
5149536Seric 	{
5159536Seric 		syserr("cannot prescan from (%s)", from);
5169536Seric 		finis();
5179536Seric 	}
5189536Seric 	rewrite(pvp, 3);
5199536Seric 	rewrite(pvp, 1);
52011286Seric 	rewrite(pvp, 4);
5219536Seric 	cataddr(pvp, buf, sizeof buf);
5229536Seric 	define('f', newstr(buf), CurEnv);
5239536Seric 
5249536Seric 	/* save the domain spec if this mailer wants it */
52510690Seric 	if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
5269536Seric 	{
5279536Seric 		extern char **copyplist();
5289536Seric 
5299536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
5309536Seric 			pvp++;
5319536Seric 		if (*pvp != NULL)
5329536Seric 			CurEnv->e_fromdomain = copyplist(pvp, TRUE);
5339536Seric 	}
5349536Seric }
5359536Seric /*
5369536Seric **  TRUSTEDUSER -- tell us if this user is to be trusted.
5379536Seric **
5389536Seric **	Parameters:
5399536Seric **		user -- the user to be checked.
5409536Seric **
5419536Seric **	Returns:
5429536Seric **		TRUE if the user is in an approved list.
5439536Seric **		FALSE otherwise.
5449536Seric **
5459536Seric **	Side Effects:
5469536Seric **		none.
5479536Seric */
5489536Seric 
5499536Seric bool
5509536Seric trusteduser(user)
5519536Seric 	char *user;
5529536Seric {
5539536Seric 	register char **ulist;
5549536Seric 	extern char *TrustedUsers[];
5559536Seric 
5569536Seric 	for (ulist = TrustedUsers; *ulist != NULL; ulist++)
5579536Seric 		if (strcmp(*ulist, user) == 0)
5589536Seric 			return (TRUE);
5599536Seric 	return (FALSE);
5609536Seric }
561