122710Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
333731Sbostic  * Copyright (c) 1988 Regents of the University of California.
433731Sbostic  * All rights reserved.
533731Sbostic  *
642829Sbostic  * %sccs.include.redist.c%
733731Sbostic  */
822710Sdist 
922710Sdist #ifndef lint
10*58092Seric static char sccsid[] = "@(#)recipient.c	6.14 (Berkeley) 02/20/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1336928Sbostic # include <sys/types.h>
1436928Sbostic # include <sys/stat.h>
1557737Seric # include <fcntl.h>
164174Seric # include <pwd.h>
174627Seric # include "sendmail.h"
184174Seric 
194174Seric /*
209622Seric **  SENDTOLIST -- Designate a send list.
214174Seric **
224174Seric **	The parameter is a comma-separated list of people to send to.
234174Seric **	This routine arranges to send to all of them.
244174Seric **
254174Seric **	Parameters:
264174Seric **		list -- the send list.
274399Seric **		ctladdr -- the address template for the person to
284399Seric **			send to -- effective uid/gid are important.
295006Seric **			This is typically the alias that caused this
305006Seric **			expansion.
315006Seric **		sendq -- a pointer to the head of a queue to put
325006Seric **			these people into.
334174Seric **
344174Seric **	Returns:
3558082Seric **		The number of addresses actually on the list.
364174Seric **
374174Seric **	Side Effects:
384174Seric **		none.
394174Seric */
404174Seric 
414174Seric # define MAXRCRSN	10
424174Seric 
4355012Seric sendtolist(list, ctladdr, sendq, e)
444174Seric 	char *list;
454399Seric 	ADDRESS *ctladdr;
465198Seric 	ADDRESS **sendq;
4755012Seric 	register ENVELOPE *e;
484174Seric {
494174Seric 	register char *p;
508223Seric 	register ADDRESS *al;	/* list of addresses to send to */
514423Seric 	bool firstone;		/* set on first address sent */
5211446Seric 	char delimiter;		/* the address delimiter */
5358082Seric 	int naddrs;
544174Seric 
557676Seric 	if (tTd(25, 1))
564444Seric 	{
574444Seric 		printf("sendto: %s\n   ctladdr=", list);
584444Seric 		printaddr(ctladdr, FALSE);
594444Seric 	}
604324Seric 
618223Seric 	/* heuristic to determine old versus new style addresses */
628230Seric 	if (ctladdr == NULL &&
6356795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6456795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6555012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6611446Seric 	delimiter = ' ';
6755012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6811446Seric 		delimiter = ',';
698223Seric 
704423Seric 	firstone = TRUE;
714324Seric 	al = NULL;
7258082Seric 	naddrs = 0;
738223Seric 
748081Seric 	for (p = list; *p != '\0'; )
754174Seric 	{
768081Seric 		register ADDRESS *a;
778081Seric 		extern char *DelimChar;		/* defined in prescan */
784319Seric 
798081Seric 		/* parse the address */
8058050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
814174Seric 			p++;
8255012Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e);
839297Seric 		p = DelimChar;
849297Seric 		if (a == NULL)
854174Seric 			continue;
864324Seric 		a->q_next = al;
874399Seric 		a->q_alias = ctladdr;
884444Seric 
894444Seric 		/* see if this should be marked as a primary address */
904423Seric 		if (ctladdr == NULL ||
918081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
924423Seric 			a->q_flags |= QPRIMARY;
934444Seric 
949379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9558061Seric 			ctladdr->q_flags |= QSELFREF;
9657731Seric 		al = a;
974423Seric 		firstone = FALSE;
984324Seric 	}
994324Seric 
1004324Seric 	/* arrange to send to everyone on the local send list */
1014324Seric 	while (al != NULL)
1024324Seric 	{
1034324Seric 		register ADDRESS *a = al;
10412613Seric 		extern ADDRESS *recipient();
1054324Seric 
1064324Seric 		al = a->q_next;
10755012Seric 		a = recipient(a, sendq, e);
1084993Seric 
1094998Seric 		/* arrange to inherit full name */
1104998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1114998Seric 			a->q_fullname = ctladdr->q_fullname;
11258082Seric 		naddrs++;
1134174Seric 	}
1144324Seric 
11555012Seric 	e->e_to = NULL;
11658082Seric 	return (naddrs);
1174174Seric }
1184174Seric /*
1194174Seric **  RECIPIENT -- Designate a message recipient
1204174Seric **
1214174Seric **	Saves the named person for future mailing.
1224174Seric **
1234174Seric **	Parameters:
1244174Seric **		a -- the (preparsed) address header for the recipient.
1255006Seric **		sendq -- a pointer to the head of a queue to put the
1265006Seric **			recipient in.  Duplicate supression is done
1275006Seric **			in this queue.
12857731Seric **		e -- the current envelope.
1294174Seric **
1304174Seric **	Returns:
13112613Seric **		The actual address in the queue.  This will be "a" if
13212613Seric **		the address is not a duplicate, else the original address.
1334174Seric **
1344174Seric **	Side Effects:
1354174Seric **		none.
1364174Seric */
1374174Seric 
13846928Sbostic extern ADDRESS *getctladdr();
13952046Seric extern char	*RcptLogFile;
14046928Sbostic 
14112613Seric ADDRESS *
14255012Seric recipient(a, sendq, e)
1434174Seric 	register ADDRESS *a;
1445006Seric 	register ADDRESS **sendq;
14555012Seric 	register ENVELOPE *e;
1464174Seric {
1474174Seric 	register ADDRESS *q;
1484319Seric 	ADDRESS **pq;
1494174Seric 	register struct mailer *m;
1509210Seric 	register char *p;
1519210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15253735Seric 	int findusercount = 0;
1539210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1544627Seric 	extern bool safefile();
1554174Seric 
15655012Seric 	e->e_to = a->q_paddr;
1574600Seric 	m = a->q_mailer;
1584174Seric 	errno = 0;
1597676Seric 	if (tTd(26, 1))
1604444Seric 	{
1614444Seric 		printf("\nrecipient: ");
1624444Seric 		printaddr(a, FALSE);
1634444Seric 	}
1644174Seric 
1654174Seric 	/* break aliasing loops */
1664174Seric 	if (AliasLevel > MAXRCRSN)
1674174Seric 	{
1684174Seric 		usrerr("aliasing/forwarding loop broken");
16912613Seric 		return (a);
1704174Seric 	}
1714174Seric 
1724174Seric 	/*
1734627Seric 	**  Finish setting up address structure.
1744174Seric 	*/
1754174Seric 
17616160Seric 	/* set the queue timeout */
1774627Seric 	a->q_timeout = TimeOut;
1784627Seric 
17916160Seric 	/* map user & host to lower case if requested on non-aliases */
18016160Seric 	if (a->q_alias == NULL)
18116160Seric 		loweraddr(a);
18216160Seric 
18316160Seric 	/* get unquoted user for file, program or user.name check */
1849210Seric 	(void) strcpy(buf, a->q_user);
1859210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1869210Seric 	{
18754993Seric 		if (*p == '\\')
1889210Seric 			quoted = TRUE;
1899210Seric 	}
19054983Seric 	stripquotes(buf);
1919210Seric 
19257402Seric 	/* check for direct mailing to restricted mailers */
19357731Seric 	if (a->q_alias == NULL && m == ProgMailer)
1944174Seric 	{
19557402Seric 		a->q_flags |= QDONTSEND|QBADADDR;
19657402Seric 		usrerr("Cannot mail directly to programs", m->m_name);
1974174Seric 	}
1984174Seric 
1994174Seric 	/*
2004419Seric 	**  Look up this person in the recipient list.
2014419Seric 	**	If they are there already, return, otherwise continue.
2024419Seric 	**	If the list is empty, just add it.  Notice the cute
2034419Seric 	**	hack to make from addresses suppress things correctly:
2044419Seric 	**	the QDONTSEND bit will be set in the send list.
2054419Seric 	**	[Please note: the emphasis is on "hack."]
2064174Seric 	*/
2074174Seric 
2085006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2094174Seric 	{
2109379Seric 		if (!ForceMail && sameaddr(q, a))
2114174Seric 		{
2127676Seric 			if (tTd(26, 1))
2134444Seric 			{
2144444Seric 				printf("%s in sendq: ", a->q_paddr);
2154444Seric 				printaddr(q, FALSE);
2164444Seric 			}
2174423Seric 			if (!bitset(QPRIMARY, q->q_flags))
21858065Seric 			{
21958065Seric 				if (!bitset(QDONTSEND, a->q_flags))
22058065Seric 					message(Arpa_Info, "duplicate suppressed");
2214423Seric 				q->q_flags |= a->q_flags;
22258065Seric 			}
22312613Seric 			return (q);
2244174Seric 		}
2254319Seric 	}
2264174Seric 
2274319Seric 	/* add address on list */
2284319Seric 	*pq = a;
2294174Seric 	a->q_next = NULL;
2304174Seric 
23152046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
23252046Seric 	    !bitset(QDONTSEND, a->q_flags))
23352046Seric 	{
23452046Seric 		static int RcptLogFd = -1;
23552046Seric 
23652046Seric 		/*
23752046Seric 		**  Log the incoming recipient name before aliasing,
23852046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
23952046Seric 		**  We'll use this to track down out-of-date aliases,
24052046Seric 		**  host names, and so forth.
24152046Seric 		*/
24252046Seric 
24352046Seric 		if (RcptLogFd < 0)
24452046Seric 		{
24552046Seric 			/* try to open the log file */
24652046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
24752047Seric 			if (RcptLogFd >= 0)
24852047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
24952046Seric 		}
25052046Seric 		if (RcptLogFd >= 0)
25152046Seric 		{
25252046Seric 			int l = strlen(a->q_paddr);
25352046Seric 
25452046Seric 			a->q_paddr[l] = '\n';
25552046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
25652046Seric 			{
25752046Seric 				(void) close(RcptLogFd);
25852046Seric 				RcptLogFd = -1;
25952046Seric 			}
26052046Seric 			a->q_paddr[l] = '\0';
26152046Seric 		}
26252046Seric 	}
26352046Seric 
2644174Seric 	/*
26557402Seric 	**  Alias the name and handle special mailer types.
2664174Seric 	*/
2674174Seric 
26853735Seric   trylocaluser:
26955354Seric 	if (tTd(29, 7))
27055354Seric 		printf("at trylocaluser %s\n", a->q_user);
27155354Seric 
27257402Seric 	if (bitset(QDONTSEND, a->q_flags))
27357402Seric 		return (a);
27457402Seric 
27557402Seric 	if (m == InclMailer)
2764174Seric 	{
27757402Seric 		a->q_flags |= QDONTSEND;
27857731Seric 		if (a->q_alias == NULL)
2794174Seric 		{
28057402Seric 			a->q_flags |= QBADADDR;
28157402Seric 			usrerr("Cannot mail directly to :include:s");
2824174Seric 		}
2834174Seric 		else
28450556Seric 		{
28558008Seric 			message(Arpa_Info, "including file %s", a->q_user);
28658008Seric 			(void) include(a->q_user, FALSE, a, sendq, e);
28750556Seric 		}
2884174Seric 	}
28957642Seric 	else if (m == FileMailer)
2904174Seric 	{
2914329Seric 		struct stat stb;
2924329Seric 		extern bool writable();
2934174Seric 
29456795Seric 		p = strrchr(buf, '/');
29551317Seric 		/* check if writable or creatable */
29657731Seric 		if (a->q_alias == NULL && !QueueRun)
2974174Seric 		{
29851317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
29951317Seric 			usrerr("Cannot mail directly to files");
3004174Seric 		}
30151317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
30251317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
30351317Seric 		{
30451317Seric 			a->q_flags |= QBADADDR;
30555012Seric 			giveresponse(EX_CANTCREAT, m, e);
30651317Seric 		}
30751317Seric 	}
30851317Seric 
30957402Seric 	if (m != LocalMailer)
31057642Seric 	{
31157642Seric 		if (!bitset(QDONTSEND, a->q_flags))
31257642Seric 			e->e_nrcpts++;
31357402Seric 		return (a);
31457642Seric 	}
31557402Seric 
31657402Seric 	/* try aliasing */
31757402Seric 	alias(a, sendq, e);
31857402Seric 
31957402Seric # ifdef USERDB
32057402Seric 	/* if not aliased, look it up in the user database */
32157402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
32257402Seric 	{
32357402Seric 		extern int udbexpand();
32457402Seric 
32557402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
32657402Seric 		{
32757402Seric 			a->q_flags |= QQUEUEUP;
32857402Seric 			if (e->e_message == NULL)
32957402Seric 				e->e_message = newstr("Deferred: user database error");
33057402Seric # ifdef LOG
33158020Seric 			if (LogLevel > 8)
33257402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
33357402Seric 					e->e_id);
33457402Seric # endif
33557402Seric 			message(Arpa_Info, "queued (user database error)");
33657642Seric 			e->e_nrcpts++;
33757402Seric 			return (a);
33857402Seric 		}
33957402Seric 	}
34057402Seric # endif
34157402Seric 
34257402Seric 	/* if it was an alias or a UDB expansion, just return now */
34357402Seric 	if (bitset(QDONTSEND, a->q_flags))
34457402Seric 		return (a);
34557402Seric 
34651317Seric 	/*
34751317Seric 	**  If we have a level two config file, then pass the name through
34851317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
34951317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
35051317Seric 	**  after local aliasing has been done.
35151317Seric 	*/
35251317Seric 
35351317Seric 	if (tTd(29, 5))
35451317Seric 	{
35551317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
35651317Seric 			ConfigLevel, RewriteRules[5]);
35751317Seric 		printaddr(a, FALSE);
35851317Seric 	}
35951317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
36051317Seric 	    RewriteRules[5] != NULL)
36151317Seric 	{
36255012Seric 		maplocaluser(a, sendq, e);
36351317Seric 	}
36451317Seric 
36551317Seric 	/*
36651317Seric 	**  If it didn't get rewritten to another mailer, go ahead
36751317Seric 	**  and deliver it.
36851317Seric 	*/
36951317Seric 
37051317Seric 	if (!bitset(QDONTSEND, a->q_flags))
37151317Seric 	{
37255354Seric 		auto bool fuzzy;
37351317Seric 		register struct passwd *pw;
37451317Seric 		extern struct passwd *finduser();
37551317Seric 
37651317Seric 		/* warning -- finduser may trash buf */
37755354Seric 		pw = finduser(buf, &fuzzy);
37851317Seric 		if (pw == NULL)
37951317Seric 		{
38051317Seric 			a->q_flags |= QBADADDR;
38155012Seric 			giveresponse(EX_NOUSER, m, e);
38251317Seric 		}
3834174Seric 		else
3844174Seric 		{
38551317Seric 			char nbuf[MAXNAME];
3864373Seric 
38755354Seric 			if (fuzzy)
3884174Seric 			{
38953735Seric 				/* name was a fuzzy match */
39051317Seric 				a->q_user = newstr(pw->pw_name);
39153735Seric 				if (findusercount++ > 3)
39253735Seric 				{
39353735Seric 					usrerr("aliasing/forwarding loop for %s broken",
39453735Seric 						pw->pw_name);
39553735Seric 					return (a);
39653735Seric 				}
39753735Seric 
39853735Seric 				/* see if it aliases */
39951317Seric 				(void) strcpy(buf, pw->pw_name);
40053735Seric 				goto trylocaluser;
4014174Seric 			}
40251317Seric 			a->q_home = newstr(pw->pw_dir);
40351317Seric 			a->q_uid = pw->pw_uid;
40451317Seric 			a->q_gid = pw->pw_gid;
40551317Seric 			a->q_flags |= QGOODUID;
40651317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
40751317Seric 			if (nbuf[0] != '\0')
40851317Seric 				a->q_fullname = newstr(nbuf);
40951317Seric 			if (!quoted)
41055012Seric 				forward(a, sendq, e);
4114174Seric 		}
4124174Seric 	}
41357642Seric 	if (!bitset(QDONTSEND, a->q_flags))
41457642Seric 		e->e_nrcpts++;
41512613Seric 	return (a);
4164174Seric }
4174174Seric /*
4184373Seric **  FINDUSER -- find the password entry for a user.
4194373Seric **
4204373Seric **	This looks a lot like getpwnam, except that it may want to
4214373Seric **	do some fancier pattern matching in /etc/passwd.
4224373Seric **
4239379Seric **	This routine contains most of the time of many sendmail runs.
4249379Seric **	It deserves to be optimized.
4259379Seric **
4264373Seric **	Parameters:
4274373Seric **		name -- the name to match against.
42855354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
42955354Seric **			was found using the fuzzy matching algorithm;
43055354Seric **			set to FALSE otherwise.
4314373Seric **
4324373Seric **	Returns:
4334373Seric **		A pointer to a pw struct.
4344373Seric **		NULL if name is unknown or ambiguous.
4354373Seric **
4364373Seric **	Side Effects:
4374407Seric **		may modify name.
4384373Seric */
4394373Seric 
4404373Seric struct passwd *
44155354Seric finduser(name, fuzzyp)
4424373Seric 	char *name;
44355354Seric 	bool *fuzzyp;
4444373Seric {
4454376Seric 	register struct passwd *pw;
4464407Seric 	register char *p;
44715325Seric 	extern struct passwd *getpwent();
44815325Seric 	extern struct passwd *getpwnam();
4494373Seric 
45055354Seric 	if (tTd(29, 4))
45155354Seric 		printf("finduser(%s): ", name);
45255354Seric 
45325777Seric 	/* map upper => lower case */
4544407Seric 	for (p = name; *p != '\0'; p++)
4554407Seric 	{
45625777Seric 		if (isascii(*p) && isupper(*p))
45725568Seric 			*p = tolower(*p);
4584407Seric 	}
45955354Seric 	*fuzzyp = FALSE;
4604407Seric 
46125777Seric 	/* look up this login name using fast path */
46212634Seric 	if ((pw = getpwnam(name)) != NULL)
46355354Seric 	{
46455354Seric 		if (tTd(29, 4))
46555354Seric 			printf("found (non-fuzzy)\n");
46612634Seric 		return (pw);
46755354Seric 	}
46812634Seric 
46953735Seric #ifdef MATCHGECOS
47053735Seric 	/* see if fuzzy matching allowed */
47153735Seric 	if (!MatchGecos)
47255354Seric 	{
47355354Seric 		if (tTd(29, 4))
47455354Seric 			printf("not found (fuzzy disabled)\n");
47553735Seric 		return NULL;
47655354Seric 	}
47753735Seric 
47812634Seric 	/* search for a matching full name instead */
47925777Seric 	for (p = name; *p != '\0'; p++)
48025777Seric 	{
48125777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
48225777Seric 			*p = ' ';
48325777Seric 	}
48423107Seric 	(void) setpwent();
4854376Seric 	while ((pw = getpwent()) != NULL)
4864376Seric 	{
4874998Seric 		char buf[MAXNAME];
4884376Seric 
4894998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
49056795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4914381Seric 		{
49255354Seric 			if (tTd(29, 4))
49355354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
4947054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
49555354Seric 			*fuzzyp = TRUE;
4964376Seric 			return (pw);
4974377Seric 		}
4984376Seric 	}
49953735Seric #endif
50055354Seric 	if (tTd(29, 4))
50155354Seric 		printf("no fuzzy match found\n");
5024376Seric 	return (NULL);
5034373Seric }
5044373Seric /*
5054329Seric **  WRITABLE -- predicate returning if the file is writable.
5064329Seric **
5074329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5084329Seric **	Unfortunately, we cannot use the access call since we
5094329Seric **	won't necessarily be the real uid when we try to
5104329Seric **	actually open the file.
5114329Seric **
5124329Seric **	Notice that ANY file with ANY execute bit is automatically
5134329Seric **	not writable.  This is also enforced by mailfile.
5144329Seric **
5154329Seric **	Parameters:
5164329Seric **		s -- pointer to a stat struct for the file.
5174329Seric **
5184329Seric **	Returns:
5194329Seric **		TRUE -- if we will be able to write this file.
5204329Seric **		FALSE -- if we cannot write this file.
5214329Seric **
5224329Seric **	Side Effects:
5234329Seric **		none.
5244329Seric */
5254329Seric 
5264329Seric bool
5274329Seric writable(s)
5284329Seric 	register struct stat *s;
5294329Seric {
53055372Seric 	uid_t euid;
53155372Seric 	gid_t egid;
5324329Seric 	int bits;
5334329Seric 
5344329Seric 	if (bitset(0111, s->st_mode))
5354329Seric 		return (FALSE);
5364329Seric 	euid = getruid();
5374329Seric 	egid = getrgid();
5384329Seric 	if (geteuid() == 0)
5394329Seric 	{
5404329Seric 		if (bitset(S_ISUID, s->st_mode))
5414329Seric 			euid = s->st_uid;
5424329Seric 		if (bitset(S_ISGID, s->st_mode))
5434329Seric 			egid = s->st_gid;
5444329Seric 	}
5454329Seric 
5464329Seric 	if (euid == 0)
5474329Seric 		return (TRUE);
5484329Seric 	bits = S_IWRITE;
5494329Seric 	if (euid != s->st_uid)
5504329Seric 	{
5514329Seric 		bits >>= 3;
5524329Seric 		if (egid != s->st_gid)
5534329Seric 			bits >>= 3;
5544329Seric 	}
5554329Seric 	return ((s->st_mode & bits) != 0);
5564329Seric }
5574329Seric /*
5584174Seric **  INCLUDE -- handle :include: specification.
5594174Seric **
5604174Seric **	Parameters:
5614174Seric **		fname -- filename to include.
56253037Seric **		forwarding -- if TRUE, we are reading a .forward file.
56353037Seric **			if FALSE, it's a :include: file.
5644399Seric **		ctladdr -- address template to use to fill in these
5654399Seric **			addresses -- effective user/group id are
5664399Seric **			the important things.
5675006Seric **		sendq -- a pointer to the head of the send queue
5685006Seric **			to put these addresses in.
5694174Seric **
5704174Seric **	Returns:
57157136Seric **		open error status
5724174Seric **
5734174Seric **	Side Effects:
5744174Seric **		reads the :include: file and sends to everyone
5754174Seric **		listed in that file.
5764174Seric */
5774174Seric 
57853037Seric static jmp_buf	CtxIncludeTimeout;
57953037Seric 
58057136Seric int
58155012Seric include(fname, forwarding, ctladdr, sendq, e)
5824174Seric 	char *fname;
58353037Seric 	bool forwarding;
5844399Seric 	ADDRESS *ctladdr;
5855006Seric 	ADDRESS **sendq;
58655012Seric 	ENVELOPE *e;
5874174Seric {
5884174Seric 	register FILE *fp;
58955012Seric 	char *oldto = e->e_to;
5909379Seric 	char *oldfilename = FileName;
5919379Seric 	int oldlinenumber = LineNumber;
59253037Seric 	register EVENT *ev = NULL;
59358082Seric 	int nincludes;
59453037Seric 	char buf[MAXLINE];
59553037Seric 	static int includetimeout();
5964174Seric 
59757186Seric 	if (tTd(27, 2))
59857186Seric 		printf("include(%s)\n", fname);
59957186Seric 
60053037Seric 	/*
60153037Seric 	**  If home directory is remote mounted but server is down,
60253037Seric 	**  this can hang or give errors; use a timeout to avoid this
60353037Seric 	*/
60453037Seric 
60553037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
60653037Seric 	{
60753037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
60853037Seric 		errno = 0;
60953037Seric 		usrerr("451 open timeout on %s", fname);
61057136Seric 		return ETIMEDOUT;
61153037Seric 	}
61253037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
61353037Seric 
61453037Seric 	/* if forwarding, the input file must be marked safe */
61553037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
61653037Seric 	{
61753037Seric 		/* don't use this .forward file */
61853037Seric 		clrevent(ev);
61957186Seric 		if (tTd(27, 4))
62057186Seric 			printf("include: not safe (uid=%d)\n", ctladdr->q_uid);
62157136Seric 		return EPERM;
62253037Seric 	}
62353037Seric 
6244174Seric 	fp = fopen(fname, "r");
6254174Seric 	if (fp == NULL)
6264174Seric 	{
62757136Seric 		int ret = errno;
62857136Seric 
62958061Seric 		clrevent(ev);
6304174Seric 		usrerr("Cannot open %s", fname);
63157136Seric 		return ret;
6324174Seric 	}
63353037Seric 
6344406Seric 	if (getctladdr(ctladdr) == NULL)
6354406Seric 	{
6364406Seric 		struct stat st;
6374174Seric 
6384406Seric 		if (fstat(fileno(fp), &st) < 0)
63958061Seric 		{
64058061Seric 			int ret = errno;
64158061Seric 
64258061Seric 			clrevent(ev);
6434406Seric 			syserr("Cannot fstat %s!", fname);
64458061Seric 			return ret;
64558061Seric 		}
6464406Seric 		ctladdr->q_uid = st.st_uid;
6474406Seric 		ctladdr->q_gid = st.st_gid;
6484406Seric 		ctladdr->q_flags |= QGOODUID;
6494406Seric 	}
6504406Seric 
65153037Seric 	clrevent(ev);
65253037Seric 
653*58092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
654*58092Seric 	{
655*58092Seric 		/* don't do any more now */
656*58092Seric 		fclose(fp);
657*58092Seric 		return 0;
658*58092Seric 	}
659*58092Seric 
6604174Seric 	/* read the file -- each line is a comma-separated list. */
6619379Seric 	FileName = fname;
6629379Seric 	LineNumber = 0;
66358082Seric 	ctladdr->q_flags &= ~QSELFREF;
66458082Seric 	nincludes = 0;
6654174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6664174Seric 	{
66756795Seric 		register char *p = strchr(buf, '\n');
6684174Seric 
66940963Sbostic 		LineNumber++;
6704174Seric 		if (p != NULL)
6714174Seric 			*p = '\0';
67257186Seric 		if (buf[0] == '#' || buf[0] == '\0')
67357139Seric 			continue;
67458008Seric 		e->e_to = NULL;
67553037Seric 		message(Arpa_Info, "%s to %s",
67653037Seric 			forwarding ? "forwarding" : "sending", buf);
67757977Seric #ifdef LOG
67858020Seric 		if (forwarding && LogLevel > 9)
67957977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
68057977Seric 				e->e_id, oldto, buf);
68157977Seric #endif
68257977Seric 
6834176Seric 		AliasLevel++;
68458082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6854176Seric 		AliasLevel--;
6864174Seric 	}
68758082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
68858065Seric 	{
68958065Seric 		if (tTd(27, 5))
69058065Seric 		{
69158065Seric 			printf("include: QDONTSEND ");
69258065Seric 			printaddr(ctladdr, FALSE);
69358065Seric 		}
69458065Seric 		ctladdr->q_flags |= QDONTSEND;
69558065Seric 	}
6964174Seric 
6974319Seric 	(void) fclose(fp);
6989379Seric 	FileName = oldfilename;
6999379Seric 	LineNumber = oldlinenumber;
70057136Seric 	return 0;
7014174Seric }
70253037Seric 
70353037Seric static
70453037Seric includetimeout()
70553037Seric {
70653037Seric 	longjmp(CtxIncludeTimeout, 1);
70753037Seric }
7084324Seric /*
7094324Seric **  SENDTOARGV -- send to an argument vector.
7104324Seric **
7114324Seric **	Parameters:
7124324Seric **		argv -- argument vector to send to.
7134324Seric **
7144324Seric **	Returns:
7154324Seric **		none.
7164324Seric **
7174324Seric **	Side Effects:
7184324Seric **		puts all addresses on the argument vector onto the
7194324Seric **			send queue.
7204324Seric */
7214324Seric 
72255012Seric sendtoargv(argv, e)
7234324Seric 	register char **argv;
72455012Seric 	register ENVELOPE *e;
7254324Seric {
7264324Seric 	register char *p;
7274324Seric 
7284324Seric 	while ((p = *argv++) != NULL)
7294324Seric 	{
73033725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
7314324Seric 		{
7324324Seric 			char nbuf[MAXNAME];
7334324Seric 
7344324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
7354324Seric 				usrerr("address overflow");
7364324Seric 			else
7374324Seric 			{
7384324Seric 				(void) strcpy(nbuf, p);
7394324Seric 				(void) strcat(nbuf, "@");
7404324Seric 				(void) strcat(nbuf, argv[1]);
7414324Seric 				p = newstr(nbuf);
7424324Seric 				argv += 2;
7434324Seric 			}
7444324Seric 		}
74558082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7464324Seric 	}
7474324Seric }
7484399Seric /*
7494399Seric **  GETCTLADDR -- get controlling address from an address header.
7504399Seric **
7514399Seric **	If none, get one corresponding to the effective userid.
7524399Seric **
7534399Seric **	Parameters:
7544399Seric **		a -- the address to find the controller of.
7554399Seric **
7564399Seric **	Returns:
7574399Seric **		the controlling address.
7584399Seric **
7594399Seric **	Side Effects:
7604399Seric **		none.
7614399Seric */
7624399Seric 
7634399Seric ADDRESS *
7644399Seric getctladdr(a)
7654399Seric 	register ADDRESS *a;
7664399Seric {
7674404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7684399Seric 		a = a->q_alias;
7694399Seric 	return (a);
7704399Seric }
771