1*22704Sdist /*
2*22704Sdist **  Sendmail
3*22704Sdist **  Copyright (c) 1983  Eric P. Allman
4*22704Sdist **  Berkeley, California
5*22704Sdist **
6*22704Sdist **  Copyright (c) 1983 Regents of the University of California.
7*22704Sdist **  All rights reserved.  The Berkeley software License Agreement
8*22704Sdist **  specifies the terms and conditions for redistribution.
9*22704Sdist */
10*22704Sdist 
11*22704Sdist #ifndef lint
12*22704Sdist static char	SccsId[] = "@(#)envelope.c	5.1 (Berkeley) 06/07/85";
13*22704Sdist #endif not lint
14*22704Sdist 
159536Seric #include <pwd.h>
1613587Swnj #include <sys/time.h>
179536Seric #include "sendmail.h"
189536Seric #include <sys/stat.h>
199536Seric 
20*22704Sdist SCCSID(@(#)envelope.c	5.1		06/07/85);
219536Seric 
229536Seric /*
239536Seric **  NEWENVELOPE -- allocate a new envelope
249536Seric **
259536Seric **	Supports inheritance.
269536Seric **
279536Seric **	Parameters:
289536Seric **		e -- the new envelope to fill in.
299536Seric **
309536Seric **	Returns:
319536Seric **		e.
329536Seric **
339536Seric **	Side Effects:
349536Seric **		none.
359536Seric */
369536Seric 
379536Seric ENVELOPE *
389536Seric newenvelope(e)
399536Seric 	register ENVELOPE *e;
409536Seric {
419536Seric 	register HDR *bh;
429536Seric 	register HDR **nhp;
439536Seric 	register ENVELOPE *parent;
449536Seric 	extern putheader(), putbody();
459536Seric 	extern ENVELOPE BlankEnvelope;
469536Seric 
479536Seric 	parent = CurEnv;
489536Seric 	if (e == CurEnv)
499536Seric 		parent = e->e_parent;
5016888Seric 	bzero((char *) e, sizeof *e);
5116888Seric 	bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from);
529536Seric 	e->e_parent = parent;
539536Seric 	e->e_ctime = curtime();
549536Seric 	e->e_puthdr = putheader;
559536Seric 	e->e_putbody = putbody;
569536Seric 	bh = BlankEnvelope.e_header;
579536Seric 	nhp = &e->e_header;
589536Seric 	while (bh != NULL)
599536Seric 	{
609536Seric 		*nhp = (HDR *) xalloc(sizeof *bh);
6116888Seric 		bcopy((char *) bh, (char *) *nhp, sizeof *bh);
629536Seric 		bh = bh->h_link;
639536Seric 		nhp = &(*nhp)->h_link;
649536Seric 	}
659536Seric 	if (CurEnv->e_xfp != NULL)
669536Seric 		(void) fflush(CurEnv->e_xfp);
679536Seric 
689536Seric 	return (e);
699536Seric }
709536Seric /*
719536Seric **  DROPENVELOPE -- deallocate an envelope.
729536Seric **
739536Seric **	Parameters:
749536Seric **		e -- the envelope to deallocate.
759536Seric **
769536Seric **	Returns:
779536Seric **		none.
789536Seric **
799536Seric **	Side Effects:
809536Seric **		housekeeping necessary to dispose of an envelope.
819536Seric **		Unlocks this queue file.
829536Seric */
839536Seric 
849536Seric dropenvelope(e)
859536Seric 	register ENVELOPE *e;
869536Seric {
879536Seric 	bool queueit = FALSE;
889536Seric 	register ADDRESS *q;
899536Seric 
909536Seric #ifdef DEBUG
919536Seric 	if (tTd(50, 1))
929536Seric 	{
939536Seric 		printf("dropenvelope %x id=", e);
949536Seric 		xputs(e->e_id);
959536Seric 		printf(" flags=%o\n", e->e_flags);
969536Seric 	}
979536Seric #endif DEBUG
989536Seric #ifdef LOG
999536Seric 	if (LogLevel > 10)
1009536Seric 		syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d",
1019536Seric 				  e->e_id == NULL ? "(none)" : e->e_id,
1029536Seric 				  e->e_flags, getpid());
1039536Seric #endif LOG
1049536Seric 
1059536Seric 	/* we must have an id to remove disk files */
1069536Seric 	if (e->e_id == NULL)
1079536Seric 		return;
1089536Seric 
1099536Seric 	/*
1109536Seric 	**  Extract state information from dregs of send list.
1119536Seric 	*/
1129536Seric 
1139536Seric 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1149536Seric 	{
1159536Seric 		if (bitset(QQUEUEUP, q->q_flags))
1169536Seric 			queueit = TRUE;
1179536Seric 	}
1189536Seric 
1199536Seric 	/*
1209536Seric 	**  Send back return receipts as requested.
1219536Seric 	*/
1229536Seric 
1239536Seric 	if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags))
1249536Seric 	{
12510844Seric 		auto ADDRESS *rlist = NULL;
1269536Seric 
1279621Seric 		sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist);
1289536Seric 		(void) returntosender("Return receipt", rlist, FALSE);
1299536Seric 	}
1309536Seric 
1319536Seric 	/*
1329536Seric 	**  Arrange to send error messages if there are fatal errors.
1339536Seric 	*/
1349536Seric 
13510754Seric 	if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET)
1369536Seric 		savemail(e);
1379536Seric 
1389536Seric 	/*
1399536Seric 	**  Instantiate or deinstantiate the queue.
1409536Seric 	*/
1419536Seric 
1429536Seric 	if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) ||
1439536Seric 	    bitset(EF_CLRQUEUE, e->e_flags))
1449536Seric 	{
1459536Seric 		if (e->e_dfp != NULL)
1469536Seric 			(void) fclose(e->e_dfp);
1479536Seric 		xunlink(queuename(e, 'q'));
1489536Seric 	}
1499536Seric 	else if (queueit || !bitset(EF_INQUEUE, e->e_flags))
15010754Seric 	{
15110754Seric #ifdef QUEUE
1529536Seric 		queueup(e, FALSE, FALSE);
15310754Seric #else QUEUE
15410754Seric 		syserr("dropenvelope: queueup");
15510754Seric #endif QUEUE
15610754Seric 	}
1579536Seric 
1589536Seric 	/* now unlock the job */
15910196Seric 	closexscript(e);
1609536Seric 	unlockqueue(e);
1619536Seric 
1629536Seric 	/* make sure that this envelope is marked unused */
16321750Seric 	if (e->e_df != NULL)
16421750Seric 		xunlink(e->e_df);
1659536Seric 	e->e_id = e->e_df = NULL;
16610196Seric 	e->e_dfp = NULL;
1679536Seric }
1689536Seric /*
1699536Seric **  CLEARENVELOPE -- clear an envelope without unlocking
1709536Seric **
1719536Seric **	This is normally used by a child process to get a clean
1729536Seric **	envelope without disturbing the parent.
1739536Seric **
1749536Seric **	Parameters:
1759536Seric **		e -- the envelope to clear.
1769536Seric **
1779536Seric **	Returns:
1789536Seric **		none.
1799536Seric **
1809536Seric **	Side Effects:
1819536Seric **		Closes files associated with the envelope.
1829536Seric **		Marks the envelope as unallocated.
1839536Seric */
1849536Seric 
1859536Seric clearenvelope(e)
1869536Seric 	register ENVELOPE *e;
1879536Seric {
1889536Seric 	/* clear out any file information */
1899536Seric 	if (e->e_xfp != NULL)
1909536Seric 		(void) fclose(e->e_xfp);
1919536Seric 	if (e->e_dfp != NULL)
1929536Seric 		(void) fclose(e->e_dfp);
1939536Seric 	e->e_xfp = e->e_dfp = NULL;
1949536Seric 
1959536Seric 	/* now expunge names of objects */
1969536Seric 	e->e_df = e->e_id = NULL;
1979536Seric 
1989536Seric 	/* and the flags which are now meaningless */
1999536Seric 	e->e_flags = 0;
2009536Seric }
2019536Seric /*
2029536Seric **  INITSYS -- initialize instantiation of system
2039536Seric **
2049536Seric **	In Daemon mode, this is done in the child.
2059536Seric **
2069536Seric **	Parameters:
2079536Seric **		none.
2089536Seric **
2099536Seric **	Returns:
2109536Seric **		none.
2119536Seric **
2129536Seric **	Side Effects:
2139536Seric **		Initializes the system macros, some global variables,
2149536Seric **		etc.  In particular, the current time in various
2159536Seric **		forms is set.
2169536Seric */
2179536Seric 
2189536Seric initsys()
2199536Seric {
2209536Seric 	static char cbuf[5];			/* holds hop count */
2219536Seric 	static char pbuf[10];			/* holds pid */
2229536Seric 	static char ybuf[10];			/* holds tty id */
2239536Seric 	register char *p;
2249536Seric 	extern char *ttyname();
2259536Seric 	extern char *macvalue();
2269536Seric 	extern char Version[];
2279536Seric 
2289536Seric 	/*
2299536Seric 	**  Give this envelope a reality.
2309536Seric 	**	I.e., an id, a transcript, and a creation time.
2319536Seric 	*/
2329536Seric 
2339536Seric 	openxscript(CurEnv);
2349536Seric 	CurEnv->e_ctime = curtime();
2359536Seric 
2369536Seric 	/*
2379536Seric 	**  Set OutChannel to something useful if stdout isn't it.
2389536Seric 	**	This arranges that any extra stuff the mailer produces
2399536Seric 	**	gets sent back to the user on error (because it is
2409536Seric 	**	tucked away in the transcript).
2419536Seric 	*/
2429536Seric 
2439536Seric 	if (OpMode == MD_DAEMON && QueueRun)
2449536Seric 		OutChannel = CurEnv->e_xfp;
2459536Seric 
2469536Seric 	/*
2479536Seric 	**  Set up some basic system macros.
2489536Seric 	*/
2499536Seric 
2509536Seric 	/* process id */
2519536Seric 	(void) sprintf(pbuf, "%d", getpid());
2529536Seric 	define('p', pbuf, CurEnv);
2539536Seric 
2549536Seric 	/* hop count */
2559536Seric 	(void) sprintf(cbuf, "%d", CurEnv->e_hopcount);
2569536Seric 	define('c', cbuf, CurEnv);
2579536Seric 
2589536Seric 	/* time as integer, unix time, arpa time */
25911932Seric 	settime();
2609536Seric 
26117472Seric #ifdef TTYNAME
2629536Seric 	/* tty name */
2639536Seric 	if (macvalue('y', CurEnv) == NULL)
2649536Seric 	{
2659536Seric 		p = ttyname(2);
2669536Seric 		if (p != NULL)
2679536Seric 		{
2689536Seric 			if (rindex(p, '/') != NULL)
2699536Seric 				p = rindex(p, '/') + 1;
2709536Seric 			(void) strcpy(ybuf, p);
2719536Seric 			define('y', ybuf, CurEnv);
2729536Seric 		}
2739536Seric 	}
27417472Seric #endif TTYNAME
2759536Seric }
2769536Seric /*
27711932Seric **  SETTIME -- set the current time.
27811932Seric **
27911932Seric **	Parameters:
28011932Seric **		none.
28111932Seric **
28211932Seric **	Returns:
28311932Seric **		none.
28411932Seric **
28511932Seric **	Side Effects:
28611932Seric **		Sets the various time macros -- $a, $b, $d, $t.
28711932Seric */
28811932Seric 
28911932Seric settime()
29011932Seric {
29111932Seric 	register char *p;
29211932Seric 	auto time_t now;
29311932Seric 	static char tbuf[20];			/* holds "current" time */
29411932Seric 	static char dbuf[30];			/* holds ctime(tbuf) */
29511932Seric 	register struct tm *tm;
29611932Seric 	extern char *arpadate();
29711932Seric 	extern struct tm *gmtime();
29811932Seric 	extern char *macvalue();
29911932Seric 
30011932Seric 	now = curtime();
30111932Seric 	tm = gmtime(&now);
30211932Seric 	(void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1,
30311932Seric 			tm->tm_mday, tm->tm_hour, tm->tm_min);
30411932Seric 	define('t', tbuf, CurEnv);
30511932Seric 	(void) strcpy(dbuf, ctime(&now));
30611932Seric 	*index(dbuf, '\n') = '\0';
30711932Seric 	if (macvalue('d', CurEnv) == NULL)
30811932Seric 		define('d', dbuf, CurEnv);
30911932Seric 	p = newstr(arpadate(dbuf));
31011932Seric 	if (macvalue('a', CurEnv) == NULL)
31111932Seric 		define('a', p, CurEnv);
31211932Seric 	define('b', p, CurEnv);
31311932Seric }
31411932Seric /*
3159536Seric **  OPENXSCRIPT -- Open transcript file
3169536Seric **
3179536Seric **	Creates a transcript file for possible eventual mailing or
3189536Seric **	sending back.
3199536Seric **
3209536Seric **	Parameters:
3219536Seric **		e -- the envelope to create the transcript in/for.
3229536Seric **
3239536Seric **	Returns:
3249536Seric **		none
3259536Seric **
3269536Seric **	Side Effects:
3279536Seric **		Creates the transcript file.
3289536Seric */
3299536Seric 
3309536Seric openxscript(e)
3319536Seric 	register ENVELOPE *e;
3329536Seric {
3339536Seric 	register char *p;
3349536Seric 
33510196Seric # ifdef LOG
33610196Seric 	if (LogLevel > 19)
33710196Seric 		syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)");
33810196Seric # endif LOG
3399536Seric 	if (e->e_xfp != NULL)
3409536Seric 		return;
3419536Seric 	p = queuename(e, 'x');
3429536Seric 	e->e_xfp = fopen(p, "w");
3439536Seric 	if (e->e_xfp == NULL)
3449536Seric 		syserr("Can't create %s", p);
3459536Seric 	else
3469536Seric 		(void) chmod(p, 0644);
3479536Seric }
3489536Seric /*
34910196Seric **  CLOSEXSCRIPT -- close the transcript file.
35010196Seric **
35110196Seric **	Parameters:
35210196Seric **		e -- the envelope containing the transcript to close.
35310196Seric **
35410196Seric **	Returns:
35510196Seric **		none.
35610196Seric **
35710196Seric **	Side Effects:
35810196Seric **		none.
35910196Seric */
36010196Seric 
36110196Seric closexscript(e)
36210196Seric 	register ENVELOPE *e;
36310196Seric {
36410196Seric 	if (e->e_xfp == NULL)
36510196Seric 		return;
36610196Seric 	(void) fclose(e->e_xfp);
36710196Seric 	e->e_xfp = NULL;
36810196Seric }
36910196Seric /*
3709536Seric **  SETSENDER -- set the person who this message is from
3719536Seric **
3729536Seric **	Under certain circumstances allow the user to say who
3739536Seric **	s/he is (using -f or -r).  These are:
3749536Seric **	1.  The user's uid is zero (root).
3759536Seric **	2.  The user's login name is in an approved list (typically
3769536Seric **	    from a network server).
3779536Seric **	3.  The address the user is trying to claim has a
3789536Seric **	    "!" character in it (since #2 doesn't do it for
3799536Seric **	    us if we are dialing out for UUCP).
3809536Seric **	A better check to replace #3 would be if the
3819536Seric **	effective uid is "UUCP" -- this would require me
3829536Seric **	to rewrite getpwent to "grab" uucp as it went by,
3839536Seric **	make getname more nasty, do another passwd file
3849536Seric **	scan, or compile the UID of "UUCP" into the code,
3859536Seric **	all of which are reprehensible.
3869536Seric **
3879536Seric **	Assuming all of these fail, we figure out something
3889536Seric **	ourselves.
3899536Seric **
3909536Seric **	Parameters:
3919536Seric **		from -- the person we would like to believe this message
3929536Seric **			is from, as specified on the command line.
3939536Seric **
3949536Seric **	Returns:
3959536Seric **		none.
3969536Seric **
3979536Seric **	Side Effects:
3989536Seric **		sets sendmail's notion of who the from person is.
3999536Seric */
4009536Seric 
4019536Seric setsender(from)
4029536Seric 	char *from;
4039536Seric {
4049536Seric 	register char **pvp;
4059536Seric 	char *realname = NULL;
40618665Seric 	register struct passwd *pw;
4079536Seric 	char buf[MAXNAME];
40816913Seric 	char pvpbuf[PSBUFSIZE];
40918665Seric 	extern struct passwd *getpwnam();
4109536Seric 	extern char *macvalue();
4119536Seric 	extern char **prescan();
4129536Seric 	extern bool safefile();
4139536Seric 	extern char *FullName;
4149536Seric 
4159536Seric # ifdef DEBUG
4169536Seric 	if (tTd(45, 1))
41714786Seric 		printf("setsender(%s)\n", from == NULL ? "" : from);
4189536Seric # endif DEBUG
4199536Seric 
4209536Seric 	/*
4219536Seric 	**  Figure out the real user executing us.
4229536Seric 	**	Username can return errno != 0 on non-errors.
4239536Seric 	*/
4249536Seric 
4259536Seric 	if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
4269536Seric 		realname = from;
4279536Seric 	if (realname == NULL || realname[0] == '\0')
4289536Seric 	{
4299536Seric 		extern char *username();
4309536Seric 
4319536Seric 		realname = username();
4329536Seric 	}
4339536Seric 
4349536Seric 	/*
4359536Seric 	**  Determine if this real person is allowed to alias themselves.
4369536Seric 	*/
4379536Seric 
4389536Seric 	if (from != NULL)
4399536Seric 	{
4409536Seric 		extern bool trusteduser();
4419536Seric 
4429536Seric 		if (!trusteduser(realname) &&
4439536Seric # ifdef DEBUG
4449536Seric 		    (!tTd(1, 9) || getuid() != geteuid()) &&
4459536Seric # endif DEBUG
4469536Seric 		    index(from, '!') == NULL && getuid() != 0)
4479536Seric 		{
4489536Seric 			/* network sends -r regardless (why why why?) */
4499536Seric 			/* syserr("%s, you cannot use the -f flag", realname); */
4509536Seric 			from = NULL;
4519536Seric 		}
4529536Seric 	}
4539536Seric 
4549536Seric 	SuprErrs = TRUE;
45511447Seric 	if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL)
4569536Seric 	{
45721750Seric 		/* log garbage addresses for traceback */
45821750Seric 		if (from != NULL)
45921750Seric 		{
46021750Seric 			syslog(LOG_ERR, "Unparseable user %s wants to be %s",
46121750Seric 					realname, from);
46221750Seric 		}
4639536Seric 		from = newstr(realname);
46411447Seric 		(void) parseaddr(from, &CurEnv->e_from, 1, '\0');
4659536Seric 	}
4669536Seric 	else
4679536Seric 		FromFlag = TRUE;
4689536Seric 	CurEnv->e_from.q_flags |= QDONTSEND;
46916162Seric 	loweraddr(&CurEnv->e_from);
4709536Seric 	SuprErrs = FALSE;
4719536Seric 
47218665Seric 	if (CurEnv->e_from.q_mailer == LocalMailer &&
47318665Seric 	    (pw = getpwnam(CurEnv->e_from.q_user)) != NULL)
4749536Seric 	{
47517472Seric 		/*
47617472Seric 		**  Process passwd file entry.
47717472Seric 		*/
47817472Seric 
4799536Seric 
4809536Seric 		/* extract home directory */
4819536Seric 		CurEnv->e_from.q_home = newstr(pw->pw_dir);
48216481Seric 		define('z', CurEnv->e_from.q_home, CurEnv);
4839536Seric 
48411625Seric 		/* extract user and group id */
48511625Seric 		CurEnv->e_from.q_uid = pw->pw_uid;
48611625Seric 		CurEnv->e_from.q_gid = pw->pw_gid;
48711625Seric 
4889536Seric 		/* if the user has given fullname already, don't redefine */
4899536Seric 		if (FullName == NULL)
4909536Seric 			FullName = macvalue('x', CurEnv);
49111932Seric 		if (FullName != NULL && FullName[0] == '\0')
4929536Seric 			FullName = NULL;
4939536Seric 
4949536Seric 		/* extract full name from passwd file */
4959582Seric 		if (FullName == NULL && pw->pw_gecos != NULL &&
4969582Seric 		    strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0)
4979536Seric 		{
4989536Seric 			buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf);
4999536Seric 			if (buf[0] != '\0')
5009536Seric 				FullName = newstr(buf);
5019536Seric 		}
5029536Seric 		if (FullName != NULL)
5039536Seric 			define('x', FullName, CurEnv);
5049536Seric 	}
50511625Seric 	else
50611625Seric 	{
5079536Seric #ifndef V6
50811625Seric 		if (CurEnv->e_from.q_home == NULL)
50911625Seric 			CurEnv->e_from.q_home = getenv("HOME");
5109536Seric #endif V6
51111625Seric 		CurEnv->e_from.q_uid = getuid();
51211625Seric 		CurEnv->e_from.q_gid = getgid();
51311625Seric 	}
51411625Seric 
5159536Seric 	if (CurEnv->e_from.q_uid != 0)
5169536Seric 	{
5179536Seric 		DefUid = CurEnv->e_from.q_uid;
5189536Seric 		DefGid = CurEnv->e_from.q_gid;
5199536Seric 	}
5209536Seric 
5219536Seric 	/*
5229536Seric 	**  Rewrite the from person to dispose of possible implicit
5239536Seric 	**	links in the net.
5249536Seric 	*/
5259536Seric 
52616913Seric 	pvp = prescan(from, '\0', pvpbuf);
5279536Seric 	if (pvp == NULL)
5289536Seric 	{
5299536Seric 		syserr("cannot prescan from (%s)", from);
5309536Seric 		finis();
5319536Seric 	}
5329536Seric 	rewrite(pvp, 3);
5339536Seric 	rewrite(pvp, 1);
53411286Seric 	rewrite(pvp, 4);
5359536Seric 	cataddr(pvp, buf, sizeof buf);
5369536Seric 	define('f', newstr(buf), CurEnv);
5379536Seric 
5389536Seric 	/* save the domain spec if this mailer wants it */
53910690Seric 	if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags))
5409536Seric 	{
5419536Seric 		extern char **copyplist();
5429536Seric 
5439536Seric 		while (*pvp != NULL && strcmp(*pvp, "@") != 0)
5449536Seric 			pvp++;
5459536Seric 		if (*pvp != NULL)
5469536Seric 			CurEnv->e_fromdomain = copyplist(pvp, TRUE);
5479536Seric 	}
5489536Seric }
5499536Seric /*
5509536Seric **  TRUSTEDUSER -- tell us if this user is to be trusted.
5519536Seric **
5529536Seric **	Parameters:
5539536Seric **		user -- the user to be checked.
5549536Seric **
5559536Seric **	Returns:
5569536Seric **		TRUE if the user is in an approved list.
5579536Seric **		FALSE otherwise.
5589536Seric **
5599536Seric **	Side Effects:
5609536Seric **		none.
5619536Seric */
5629536Seric 
5639536Seric bool
5649536Seric trusteduser(user)
5659536Seric 	char *user;
5669536Seric {
5679536Seric 	register char **ulist;
5689536Seric 	extern char *TrustedUsers[];
5699536Seric 
5709536Seric 	for (ulist = TrustedUsers; *ulist != NULL; ulist++)
5719536Seric 		if (strcmp(*ulist, user) == 0)
5729536Seric 			return (TRUE);
5739536Seric 	return (FALSE);
5749536Seric }
575