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*57402Seric static char sccsid[] = "@(#)recipient.c	6.2 (Berkeley) 01/02/93";
1133731Sbostic #endif /* not lint */
1222710Sdist 
1336928Sbostic # include <sys/types.h>
1436928Sbostic # include <sys/stat.h>
1552046Seric # include <sys/file.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:
354998Seric **		none
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 */
524444Seric 	bool selfref;		/* set if this list includes ctladdr */
5311446Seric 	char delimiter;		/* the address delimiter */
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;
714444Seric 	selfref = FALSE;
724324Seric 	al = NULL;
738223Seric 
748081Seric 	for (p = list; *p != '\0'; )
754174Seric 	{
768081Seric 		register ADDRESS *a;
778081Seric 		extern char *DelimChar;		/* defined in prescan */
784319Seric 
798081Seric 		/* parse the address */
808081Seric 		while (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 
944444Seric 		/* put on send queue or suppress self-reference */
959379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
964444Seric 			selfref = TRUE;
974444Seric 		else
984444Seric 			al = a;
994423Seric 		firstone = FALSE;
1004324Seric 	}
1014324Seric 
1024444Seric 	/* if this alias doesn't include itself, delete ctladdr */
1034444Seric 	if (!selfref && ctladdr != NULL)
1044444Seric 		ctladdr->q_flags |= QDONTSEND;
1054444Seric 
1064324Seric 	/* arrange to send to everyone on the local send list */
1074324Seric 	while (al != NULL)
1084324Seric 	{
1094324Seric 		register ADDRESS *a = al;
11012613Seric 		extern ADDRESS *recipient();
1114324Seric 
1124324Seric 		al = a->q_next;
11355012Seric 		a = recipient(a, sendq, e);
1144993Seric 
1154998Seric 		/* arrange to inherit full name */
1164998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1174998Seric 			a->q_fullname = ctladdr->q_fullname;
1184174Seric 	}
1194324Seric 
12055012Seric 	e->e_to = NULL;
1214174Seric }
1224174Seric /*
1234174Seric **  RECIPIENT -- Designate a message recipient
1244174Seric **
1254174Seric **	Saves the named person for future mailing.
1264174Seric **
1274174Seric **	Parameters:
1284174Seric **		a -- the (preparsed) address header for the recipient.
1295006Seric **		sendq -- a pointer to the head of a queue to put the
1305006Seric **			recipient in.  Duplicate supression is done
1315006Seric **			in this queue.
1324174Seric **
1334174Seric **	Returns:
13412613Seric **		The actual address in the queue.  This will be "a" if
13512613Seric **		the address is not a duplicate, else the original address.
1364174Seric **
1374174Seric **	Side Effects:
1384174Seric **		none.
1394174Seric */
1404174Seric 
14146928Sbostic extern ADDRESS *getctladdr();
14252046Seric extern char	*RcptLogFile;
14346928Sbostic 
14412613Seric ADDRESS *
14555012Seric recipient(a, sendq, e)
1464174Seric 	register ADDRESS *a;
1475006Seric 	register ADDRESS **sendq;
14855012Seric 	register ENVELOPE *e;
1494174Seric {
1504174Seric 	register ADDRESS *q;
1514319Seric 	ADDRESS **pq;
1524174Seric 	register struct mailer *m;
1539210Seric 	register char *p;
1549210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
15553735Seric 	int findusercount = 0;
1569210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1574627Seric 	extern bool safefile();
1584174Seric 
15955012Seric 	e->e_to = a->q_paddr;
1604600Seric 	m = a->q_mailer;
1614174Seric 	errno = 0;
1627676Seric 	if (tTd(26, 1))
1634444Seric 	{
1644444Seric 		printf("\nrecipient: ");
1654444Seric 		printaddr(a, FALSE);
1664444Seric 	}
1674174Seric 
1684174Seric 	/* break aliasing loops */
1694174Seric 	if (AliasLevel > MAXRCRSN)
1704174Seric 	{
1714174Seric 		usrerr("aliasing/forwarding loop broken");
17212613Seric 		return (a);
1734174Seric 	}
1744174Seric 
1754174Seric 	/*
1764627Seric 	**  Finish setting up address structure.
1774174Seric 	*/
1784174Seric 
17916160Seric 	/* set the queue timeout */
1804627Seric 	a->q_timeout = TimeOut;
1814627Seric 
18216160Seric 	/* map user & host to lower case if requested on non-aliases */
18316160Seric 	if (a->q_alias == NULL)
18416160Seric 		loweraddr(a);
18516160Seric 
18616160Seric 	/* get unquoted user for file, program or user.name check */
1879210Seric 	(void) strcpy(buf, a->q_user);
1889210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1899210Seric 	{
19054993Seric 		if (*p == '\\')
1919210Seric 			quoted = TRUE;
1929210Seric 	}
19354983Seric 	stripquotes(buf);
1949210Seric 
195*57402Seric 	/* check for direct mailing to restricted mailers */
196*57402Seric 	if (a->q_alias == NULL && m == ProgMailer && !ForceMail)
1974174Seric 	{
198*57402Seric 		a->q_flags |= QDONTSEND|QBADADDR;
199*57402Seric 		usrerr("Cannot mail directly to programs", m->m_name);
2004174Seric 	}
2014174Seric 
2024174Seric 	/*
2034419Seric 	**  Look up this person in the recipient list.
2044419Seric 	**	If they are there already, return, otherwise continue.
2054419Seric 	**	If the list is empty, just add it.  Notice the cute
2064419Seric 	**	hack to make from addresses suppress things correctly:
2074419Seric 	**	the QDONTSEND bit will be set in the send list.
2084419Seric 	**	[Please note: the emphasis is on "hack."]
2094174Seric 	*/
2104174Seric 
2115006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2124174Seric 	{
2139379Seric 		if (!ForceMail && sameaddr(q, a))
2144174Seric 		{
2157676Seric 			if (tTd(26, 1))
2164444Seric 			{
2174444Seric 				printf("%s in sendq: ", a->q_paddr);
2184444Seric 				printaddr(q, FALSE);
2194444Seric 			}
2207054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2214324Seric 				message(Arpa_Info, "duplicate suppressed");
2224423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2234423Seric 				q->q_flags |= a->q_flags;
22412613Seric 			return (q);
2254174Seric 		}
2264319Seric 	}
2274174Seric 
2284319Seric 	/* add address on list */
2294319Seric 	*pq = a;
2304174Seric 	a->q_next = NULL;
23155012Seric 	e->e_nrcpts++;
2324174Seric 
23352046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
23452046Seric 	    !bitset(QDONTSEND, a->q_flags))
23552046Seric 	{
23652046Seric 		static int RcptLogFd = -1;
23752046Seric 
23852046Seric 		/*
23952046Seric 		**  Log the incoming recipient name before aliasing,
24052046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
24152046Seric 		**  We'll use this to track down out-of-date aliases,
24252046Seric 		**  host names, and so forth.
24352046Seric 		*/
24452046Seric 
24552046Seric 		if (RcptLogFd < 0)
24652046Seric 		{
24752046Seric 			/* try to open the log file */
24852046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
24952047Seric 			if (RcptLogFd >= 0)
25052047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
25152046Seric 		}
25252046Seric 		if (RcptLogFd >= 0)
25352046Seric 		{
25452046Seric 			int l = strlen(a->q_paddr);
25552046Seric 
25652046Seric 			a->q_paddr[l] = '\n';
25752046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
25852046Seric 			{
25952046Seric 				(void) close(RcptLogFd);
26052046Seric 				RcptLogFd = -1;
26152046Seric 			}
26252046Seric 			a->q_paddr[l] = '\0';
26352046Seric 		}
26452046Seric 	}
26552046Seric 
2664174Seric 	/*
267*57402Seric 	**  Alias the name and handle special mailer types.
2684174Seric 	*/
2694174Seric 
27053735Seric   trylocaluser:
27155354Seric 	if (tTd(29, 7))
27255354Seric 		printf("at trylocaluser %s\n", a->q_user);
27355354Seric 
274*57402Seric 	if (bitset(QDONTSEND, a->q_flags))
275*57402Seric 		return (a);
276*57402Seric 
277*57402Seric 	if (m == InclMailer)
2784174Seric 	{
279*57402Seric 		a->q_flags |= QDONTSEND;
280*57402Seric 		if (a->q_alias == NULL && !ForceMail)
2814174Seric 		{
282*57402Seric 			a->q_flags |= QBADADDR;
283*57402Seric 			usrerr("Cannot mail directly to :include:s");
2844174Seric 		}
2854174Seric 		else
28650556Seric 		{
287*57402Seric 			message(Arpa_Info, "including file %s", &a->q_user[9]);
288*57402Seric 			(void) include(&a->q_user[9], FALSE, a, sendq, e);
28950556Seric 		}
290*57402Seric 		return (a);
2914174Seric 	}
2924174Seric 
293*57402Seric 	if (m == FileMailer)
2944174Seric 	{
2954329Seric 		struct stat stb;
2964329Seric 		extern bool writable();
2974174Seric 
29856795Seric 		p = strrchr(buf, '/');
29951317Seric 		/* check if writable or creatable */
30051317Seric 		if (a->q_alias == NULL && !QueueRun && !ForceMail)
3014174Seric 		{
30251317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
30351317Seric 			usrerr("Cannot mail directly to files");
3044174Seric 		}
30551317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
30651317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
30751317Seric 		{
30851317Seric 			a->q_flags |= QBADADDR;
30955012Seric 			giveresponse(EX_CANTCREAT, m, e);
31051317Seric 		}
31151317Seric 		return (a);
31251317Seric 	}
31351317Seric 
314*57402Seric 	if (m != LocalMailer)
315*57402Seric 		return (a);
316*57402Seric 
317*57402Seric 	/* try aliasing */
318*57402Seric 	alias(a, sendq, e);
319*57402Seric 
320*57402Seric # ifdef USERDB
321*57402Seric 	/* if not aliased, look it up in the user database */
322*57402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
323*57402Seric 	{
324*57402Seric 		extern int udbexpand();
325*57402Seric 
326*57402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
327*57402Seric 		{
328*57402Seric 			a->q_flags |= QQUEUEUP;
329*57402Seric 			if (e->e_message == NULL)
330*57402Seric 				e->e_message = newstr("Deferred: user database error");
331*57402Seric # ifdef LOG
332*57402Seric 			if (LogLevel > 3)
333*57402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
334*57402Seric 					e->e_id);
335*57402Seric # endif
336*57402Seric 			message(Arpa_Info, "queued (user database error)");
337*57402Seric 			return (a);
338*57402Seric 		}
339*57402Seric 	}
340*57402Seric # endif
341*57402Seric 
342*57402Seric 	/* if it was an alias or a UDB expansion, just return now */
343*57402Seric 	if (bitset(QDONTSEND, a->q_flags))
344*57402Seric 		return (a);
345*57402Seric 
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 	}
41312613Seric 	return (a);
4144174Seric }
4154174Seric /*
4164373Seric **  FINDUSER -- find the password entry for a user.
4174373Seric **
4184373Seric **	This looks a lot like getpwnam, except that it may want to
4194373Seric **	do some fancier pattern matching in /etc/passwd.
4204373Seric **
4219379Seric **	This routine contains most of the time of many sendmail runs.
4229379Seric **	It deserves to be optimized.
4239379Seric **
4244373Seric **	Parameters:
4254373Seric **		name -- the name to match against.
42655354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
42755354Seric **			was found using the fuzzy matching algorithm;
42855354Seric **			set to FALSE otherwise.
4294373Seric **
4304373Seric **	Returns:
4314373Seric **		A pointer to a pw struct.
4324373Seric **		NULL if name is unknown or ambiguous.
4334373Seric **
4344373Seric **	Side Effects:
4354407Seric **		may modify name.
4364373Seric */
4374373Seric 
4384373Seric struct passwd *
43955354Seric finduser(name, fuzzyp)
4404373Seric 	char *name;
44155354Seric 	bool *fuzzyp;
4424373Seric {
4434376Seric 	register struct passwd *pw;
4444407Seric 	register char *p;
44515325Seric 	extern struct passwd *getpwent();
44615325Seric 	extern struct passwd *getpwnam();
4474373Seric 
44855354Seric 	if (tTd(29, 4))
44955354Seric 		printf("finduser(%s): ", name);
45055354Seric 
45125777Seric 	/* map upper => lower case */
4524407Seric 	for (p = name; *p != '\0'; p++)
4534407Seric 	{
45425777Seric 		if (isascii(*p) && isupper(*p))
45525568Seric 			*p = tolower(*p);
4564407Seric 	}
45755354Seric 	*fuzzyp = FALSE;
4584407Seric 
45925777Seric 	/* look up this login name using fast path */
46012634Seric 	if ((pw = getpwnam(name)) != NULL)
46155354Seric 	{
46255354Seric 		if (tTd(29, 4))
46355354Seric 			printf("found (non-fuzzy)\n");
46412634Seric 		return (pw);
46555354Seric 	}
46612634Seric 
46753735Seric #ifdef MATCHGECOS
46853735Seric 	/* see if fuzzy matching allowed */
46953735Seric 	if (!MatchGecos)
47055354Seric 	{
47155354Seric 		if (tTd(29, 4))
47255354Seric 			printf("not found (fuzzy disabled)\n");
47353735Seric 		return NULL;
47455354Seric 	}
47553735Seric 
47612634Seric 	/* search for a matching full name instead */
47725777Seric 	for (p = name; *p != '\0'; p++)
47825777Seric 	{
47925777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
48025777Seric 			*p = ' ';
48125777Seric 	}
48223107Seric 	(void) setpwent();
4834376Seric 	while ((pw = getpwent()) != NULL)
4844376Seric 	{
4854998Seric 		char buf[MAXNAME];
4864376Seric 
4874998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
48856795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4894381Seric 		{
49055354Seric 			if (tTd(29, 4))
49155354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
4927054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
49355354Seric 			*fuzzyp = TRUE;
4944376Seric 			return (pw);
4954377Seric 		}
4964376Seric 	}
49753735Seric #endif
49855354Seric 	if (tTd(29, 4))
49955354Seric 		printf("no fuzzy match found\n");
5004376Seric 	return (NULL);
5014373Seric }
5024373Seric /*
5034329Seric **  WRITABLE -- predicate returning if the file is writable.
5044329Seric **
5054329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5064329Seric **	Unfortunately, we cannot use the access call since we
5074329Seric **	won't necessarily be the real uid when we try to
5084329Seric **	actually open the file.
5094329Seric **
5104329Seric **	Notice that ANY file with ANY execute bit is automatically
5114329Seric **	not writable.  This is also enforced by mailfile.
5124329Seric **
5134329Seric **	Parameters:
5144329Seric **		s -- pointer to a stat struct for the file.
5154329Seric **
5164329Seric **	Returns:
5174329Seric **		TRUE -- if we will be able to write this file.
5184329Seric **		FALSE -- if we cannot write this file.
5194329Seric **
5204329Seric **	Side Effects:
5214329Seric **		none.
5224329Seric */
5234329Seric 
5244329Seric bool
5254329Seric writable(s)
5264329Seric 	register struct stat *s;
5274329Seric {
52855372Seric 	uid_t euid;
52955372Seric 	gid_t egid;
5304329Seric 	int bits;
5314329Seric 
5324329Seric 	if (bitset(0111, s->st_mode))
5334329Seric 		return (FALSE);
5344329Seric 	euid = getruid();
5354329Seric 	egid = getrgid();
5364329Seric 	if (geteuid() == 0)
5374329Seric 	{
5384329Seric 		if (bitset(S_ISUID, s->st_mode))
5394329Seric 			euid = s->st_uid;
5404329Seric 		if (bitset(S_ISGID, s->st_mode))
5414329Seric 			egid = s->st_gid;
5424329Seric 	}
5434329Seric 
5444329Seric 	if (euid == 0)
5454329Seric 		return (TRUE);
5464329Seric 	bits = S_IWRITE;
5474329Seric 	if (euid != s->st_uid)
5484329Seric 	{
5494329Seric 		bits >>= 3;
5504329Seric 		if (egid != s->st_gid)
5514329Seric 			bits >>= 3;
5524329Seric 	}
5534329Seric 	return ((s->st_mode & bits) != 0);
5544329Seric }
5554329Seric /*
5564174Seric **  INCLUDE -- handle :include: specification.
5574174Seric **
5584174Seric **	Parameters:
5594174Seric **		fname -- filename to include.
56053037Seric **		forwarding -- if TRUE, we are reading a .forward file.
56153037Seric **			if FALSE, it's a :include: file.
5624399Seric **		ctladdr -- address template to use to fill in these
5634399Seric **			addresses -- effective user/group id are
5644399Seric **			the important things.
5655006Seric **		sendq -- a pointer to the head of the send queue
5665006Seric **			to put these addresses in.
5674174Seric **
5684174Seric **	Returns:
56957136Seric **		open error status
5704174Seric **
5714174Seric **	Side Effects:
5724174Seric **		reads the :include: file and sends to everyone
5734174Seric **		listed in that file.
5744174Seric */
5754174Seric 
57653037Seric static jmp_buf	CtxIncludeTimeout;
57753037Seric 
57857136Seric int
57955012Seric include(fname, forwarding, ctladdr, sendq, e)
5804174Seric 	char *fname;
58153037Seric 	bool forwarding;
5824399Seric 	ADDRESS *ctladdr;
5835006Seric 	ADDRESS **sendq;
58455012Seric 	ENVELOPE *e;
5854174Seric {
5864174Seric 	register FILE *fp;
58755012Seric 	char *oldto = e->e_to;
5889379Seric 	char *oldfilename = FileName;
5899379Seric 	int oldlinenumber = LineNumber;
59053037Seric 	register EVENT *ev = NULL;
59153037Seric 	char buf[MAXLINE];
59253037Seric 	static int includetimeout();
5934174Seric 
59457186Seric 	if (tTd(27, 2))
59557186Seric 		printf("include(%s)\n", fname);
59657186Seric 
59753037Seric 	/*
59853037Seric 	**  If home directory is remote mounted but server is down,
59953037Seric 	**  this can hang or give errors; use a timeout to avoid this
60053037Seric 	*/
60153037Seric 
60253037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
60353037Seric 	{
60453037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
60553037Seric 		errno = 0;
60653037Seric 		usrerr("451 open timeout on %s", fname);
60757136Seric 		return ETIMEDOUT;
60853037Seric 	}
60953037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
61053037Seric 
61153037Seric 	/* if forwarding, the input file must be marked safe */
61253037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
61353037Seric 	{
61453037Seric 		/* don't use this .forward file */
61553037Seric 		clrevent(ev);
61657186Seric 		if (tTd(27, 4))
61757186Seric 			printf("include: not safe (uid=%d)\n", ctladdr->q_uid);
61857136Seric 		return EPERM;
61953037Seric 	}
62053037Seric 
6214174Seric 	fp = fopen(fname, "r");
6224174Seric 	if (fp == NULL)
6234174Seric 	{
62457136Seric 		int ret = errno;
62557136Seric 
6264174Seric 		usrerr("Cannot open %s", fname);
62757136Seric 		return ret;
6284174Seric 	}
62953037Seric 
6304406Seric 	if (getctladdr(ctladdr) == NULL)
6314406Seric 	{
6324406Seric 		struct stat st;
6334174Seric 
6344406Seric 		if (fstat(fileno(fp), &st) < 0)
6354406Seric 			syserr("Cannot fstat %s!", fname);
6364406Seric 		ctladdr->q_uid = st.st_uid;
6374406Seric 		ctladdr->q_gid = st.st_gid;
6384406Seric 		ctladdr->q_flags |= QGOODUID;
6394406Seric 	}
6404406Seric 
64153037Seric 	clrevent(ev);
64253037Seric 
6434174Seric 	/* read the file -- each line is a comma-separated list. */
6449379Seric 	FileName = fname;
6459379Seric 	LineNumber = 0;
6464174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6474174Seric 	{
64856795Seric 		register char *p = strchr(buf, '\n');
6494174Seric 
65040963Sbostic 		LineNumber++;
6514174Seric 		if (p != NULL)
6524174Seric 			*p = '\0';
65357186Seric 		if (buf[0] == '#' || buf[0] == '\0')
65457139Seric 			continue;
65555012Seric 		e->e_to = oldto;
65653037Seric 		message(Arpa_Info, "%s to %s",
65753037Seric 			forwarding ? "forwarding" : "sending", buf);
6584176Seric 		AliasLevel++;
65955012Seric 		sendtolist(buf, ctladdr, sendq, e);
6604176Seric 		AliasLevel--;
6614174Seric 	}
6624174Seric 
6634319Seric 	(void) fclose(fp);
6649379Seric 	FileName = oldfilename;
6659379Seric 	LineNumber = oldlinenumber;
66657136Seric 	return 0;
6674174Seric }
66853037Seric 
66953037Seric static
67053037Seric includetimeout()
67153037Seric {
67253037Seric 	longjmp(CtxIncludeTimeout, 1);
67353037Seric }
6744324Seric /*
6754324Seric **  SENDTOARGV -- send to an argument vector.
6764324Seric **
6774324Seric **	Parameters:
6784324Seric **		argv -- argument vector to send to.
6794324Seric **
6804324Seric **	Returns:
6814324Seric **		none.
6824324Seric **
6834324Seric **	Side Effects:
6844324Seric **		puts all addresses on the argument vector onto the
6854324Seric **			send queue.
6864324Seric */
6874324Seric 
68855012Seric sendtoargv(argv, e)
6894324Seric 	register char **argv;
69055012Seric 	register ENVELOPE *e;
6914324Seric {
6924324Seric 	register char *p;
6934324Seric 
6944324Seric 	while ((p = *argv++) != NULL)
6954324Seric 	{
69633725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
6974324Seric 		{
6984324Seric 			char nbuf[MAXNAME];
6994324Seric 
7004324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
7014324Seric 				usrerr("address overflow");
7024324Seric 			else
7034324Seric 			{
7044324Seric 				(void) strcpy(nbuf, p);
7054324Seric 				(void) strcat(nbuf, "@");
7064324Seric 				(void) strcat(nbuf, argv[1]);
7074324Seric 				p = newstr(nbuf);
7084324Seric 				argv += 2;
7094324Seric 			}
7104324Seric 		}
71155012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7124324Seric 	}
7134324Seric }
7144399Seric /*
7154399Seric **  GETCTLADDR -- get controlling address from an address header.
7164399Seric **
7174399Seric **	If none, get one corresponding to the effective userid.
7184399Seric **
7194399Seric **	Parameters:
7204399Seric **		a -- the address to find the controller of.
7214399Seric **
7224399Seric **	Returns:
7234399Seric **		the controlling address.
7244399Seric **
7254399Seric **	Side Effects:
7264399Seric **		none.
7274399Seric */
7284399Seric 
7294399Seric ADDRESS *
7304399Seric getctladdr(a)
7314399Seric 	register ADDRESS *a;
7324399Seric {
7334404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7344399Seric 		a = a->q_alias;
7354399Seric 	return (a);
7364399Seric }
737