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*58065Seric static char sccsid[] = "@(#)recipient.c	6.12 (Berkeley) 02/19/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:
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 */
5211446Seric 	char delimiter;		/* the address delimiter */
534174Seric 
547676Seric 	if (tTd(25, 1))
554444Seric 	{
564444Seric 		printf("sendto: %s\n   ctladdr=", list);
574444Seric 		printaddr(ctladdr, FALSE);
584444Seric 	}
594324Seric 
608223Seric 	/* heuristic to determine old versus new style addresses */
618230Seric 	if (ctladdr == NULL &&
6256795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6356795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6455012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6511446Seric 	delimiter = ' ';
6655012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6711446Seric 		delimiter = ',';
688223Seric 
694423Seric 	firstone = TRUE;
704324Seric 	al = NULL;
718223Seric 
728081Seric 	for (p = list; *p != '\0'; )
734174Seric 	{
748081Seric 		register ADDRESS *a;
758081Seric 		extern char *DelimChar;		/* defined in prescan */
764319Seric 
778081Seric 		/* parse the address */
7858050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
794174Seric 			p++;
8055012Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e);
819297Seric 		p = DelimChar;
829297Seric 		if (a == NULL)
834174Seric 			continue;
844324Seric 		a->q_next = al;
854399Seric 		a->q_alias = ctladdr;
864444Seric 
874444Seric 		/* see if this should be marked as a primary address */
884423Seric 		if (ctladdr == NULL ||
898081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
904423Seric 			a->q_flags |= QPRIMARY;
914444Seric 
929379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9358061Seric 			ctladdr->q_flags |= QSELFREF;
9457731Seric 		al = a;
954423Seric 		firstone = FALSE;
964324Seric 	}
974324Seric 
984324Seric 	/* arrange to send to everyone on the local send list */
994324Seric 	while (al != NULL)
1004324Seric 	{
1014324Seric 		register ADDRESS *a = al;
10212613Seric 		extern ADDRESS *recipient();
1034324Seric 
1044324Seric 		al = a->q_next;
10555012Seric 		a = recipient(a, sendq, e);
1064993Seric 
1074998Seric 		/* arrange to inherit full name */
1084998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1094998Seric 			a->q_fullname = ctladdr->q_fullname;
1104174Seric 	}
1114324Seric 
11255012Seric 	e->e_to = NULL;
1134174Seric }
1144174Seric /*
1154174Seric **  RECIPIENT -- Designate a message recipient
1164174Seric **
1174174Seric **	Saves the named person for future mailing.
1184174Seric **
1194174Seric **	Parameters:
1204174Seric **		a -- the (preparsed) address header for the recipient.
1215006Seric **		sendq -- a pointer to the head of a queue to put the
1225006Seric **			recipient in.  Duplicate supression is done
1235006Seric **			in this queue.
12457731Seric **		e -- the current envelope.
1254174Seric **
1264174Seric **	Returns:
12712613Seric **		The actual address in the queue.  This will be "a" if
12812613Seric **		the address is not a duplicate, else the original address.
1294174Seric **
1304174Seric **	Side Effects:
1314174Seric **		none.
1324174Seric */
1334174Seric 
13446928Sbostic extern ADDRESS *getctladdr();
13552046Seric extern char	*RcptLogFile;
13646928Sbostic 
13712613Seric ADDRESS *
13855012Seric recipient(a, sendq, e)
1394174Seric 	register ADDRESS *a;
1405006Seric 	register ADDRESS **sendq;
14155012Seric 	register ENVELOPE *e;
1424174Seric {
1434174Seric 	register ADDRESS *q;
1444319Seric 	ADDRESS **pq;
1454174Seric 	register struct mailer *m;
1469210Seric 	register char *p;
1479210Seric 	bool quoted = FALSE;		/* set if the addr has a quote bit */
14853735Seric 	int findusercount = 0;
1499210Seric 	char buf[MAXNAME];		/* unquoted image of the user name */
1504627Seric 	extern bool safefile();
1514174Seric 
15255012Seric 	e->e_to = a->q_paddr;
1534600Seric 	m = a->q_mailer;
1544174Seric 	errno = 0;
1557676Seric 	if (tTd(26, 1))
1564444Seric 	{
1574444Seric 		printf("\nrecipient: ");
1584444Seric 		printaddr(a, FALSE);
1594444Seric 	}
1604174Seric 
1614174Seric 	/* break aliasing loops */
1624174Seric 	if (AliasLevel > MAXRCRSN)
1634174Seric 	{
1644174Seric 		usrerr("aliasing/forwarding loop broken");
16512613Seric 		return (a);
1664174Seric 	}
1674174Seric 
1684174Seric 	/*
1694627Seric 	**  Finish setting up address structure.
1704174Seric 	*/
1714174Seric 
17216160Seric 	/* set the queue timeout */
1734627Seric 	a->q_timeout = TimeOut;
1744627Seric 
17516160Seric 	/* map user & host to lower case if requested on non-aliases */
17616160Seric 	if (a->q_alias == NULL)
17716160Seric 		loweraddr(a);
17816160Seric 
17916160Seric 	/* get unquoted user for file, program or user.name check */
1809210Seric 	(void) strcpy(buf, a->q_user);
1819210Seric 	for (p = buf; *p != '\0' && !quoted; p++)
1829210Seric 	{
18354993Seric 		if (*p == '\\')
1849210Seric 			quoted = TRUE;
1859210Seric 	}
18654983Seric 	stripquotes(buf);
1879210Seric 
18857402Seric 	/* check for direct mailing to restricted mailers */
18957731Seric 	if (a->q_alias == NULL && m == ProgMailer)
1904174Seric 	{
19157402Seric 		a->q_flags |= QDONTSEND|QBADADDR;
19257402Seric 		usrerr("Cannot mail directly to programs", m->m_name);
1934174Seric 	}
1944174Seric 
1954174Seric 	/*
1964419Seric 	**  Look up this person in the recipient list.
1974419Seric 	**	If they are there already, return, otherwise continue.
1984419Seric 	**	If the list is empty, just add it.  Notice the cute
1994419Seric 	**	hack to make from addresses suppress things correctly:
2004419Seric 	**	the QDONTSEND bit will be set in the send list.
2014419Seric 	**	[Please note: the emphasis is on "hack."]
2024174Seric 	*/
2034174Seric 
2045006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2054174Seric 	{
2069379Seric 		if (!ForceMail && sameaddr(q, a))
2074174Seric 		{
2087676Seric 			if (tTd(26, 1))
2094444Seric 			{
2104444Seric 				printf("%s in sendq: ", a->q_paddr);
2114444Seric 				printaddr(q, FALSE);
2124444Seric 			}
2134423Seric 			if (!bitset(QPRIMARY, q->q_flags))
214*58065Seric 			{
215*58065Seric 				if (!bitset(QDONTSEND, a->q_flags))
216*58065Seric 					message(Arpa_Info, "duplicate suppressed");
2174423Seric 				q->q_flags |= a->q_flags;
218*58065Seric 			}
21912613Seric 			return (q);
2204174Seric 		}
2214319Seric 	}
2224174Seric 
2234319Seric 	/* add address on list */
2244319Seric 	*pq = a;
2254174Seric 	a->q_next = NULL;
2264174Seric 
22752046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
22852046Seric 	    !bitset(QDONTSEND, a->q_flags))
22952046Seric 	{
23052046Seric 		static int RcptLogFd = -1;
23152046Seric 
23252046Seric 		/*
23352046Seric 		**  Log the incoming recipient name before aliasing,
23452046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
23552046Seric 		**  We'll use this to track down out-of-date aliases,
23652046Seric 		**  host names, and so forth.
23752046Seric 		*/
23852046Seric 
23952046Seric 		if (RcptLogFd < 0)
24052046Seric 		{
24152046Seric 			/* try to open the log file */
24252046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
24352047Seric 			if (RcptLogFd >= 0)
24452047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
24552046Seric 		}
24652046Seric 		if (RcptLogFd >= 0)
24752046Seric 		{
24852046Seric 			int l = strlen(a->q_paddr);
24952046Seric 
25052046Seric 			a->q_paddr[l] = '\n';
25152046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
25252046Seric 			{
25352046Seric 				(void) close(RcptLogFd);
25452046Seric 				RcptLogFd = -1;
25552046Seric 			}
25652046Seric 			a->q_paddr[l] = '\0';
25752046Seric 		}
25852046Seric 	}
25952046Seric 
2604174Seric 	/*
26157402Seric 	**  Alias the name and handle special mailer types.
2624174Seric 	*/
2634174Seric 
26453735Seric   trylocaluser:
26555354Seric 	if (tTd(29, 7))
26655354Seric 		printf("at trylocaluser %s\n", a->q_user);
26755354Seric 
26857402Seric 	if (bitset(QDONTSEND, a->q_flags))
26957402Seric 		return (a);
27057402Seric 
27157402Seric 	if (m == InclMailer)
2724174Seric 	{
27357402Seric 		a->q_flags |= QDONTSEND;
27457731Seric 		if (a->q_alias == NULL)
2754174Seric 		{
27657402Seric 			a->q_flags |= QBADADDR;
27757402Seric 			usrerr("Cannot mail directly to :include:s");
2784174Seric 		}
2794174Seric 		else
28050556Seric 		{
28158008Seric 			message(Arpa_Info, "including file %s", a->q_user);
28258008Seric 			(void) include(a->q_user, FALSE, a, sendq, e);
28350556Seric 		}
2844174Seric 	}
28557642Seric 	else if (m == FileMailer)
2864174Seric 	{
2874329Seric 		struct stat stb;
2884329Seric 		extern bool writable();
2894174Seric 
29056795Seric 		p = strrchr(buf, '/');
29151317Seric 		/* check if writable or creatable */
29257731Seric 		if (a->q_alias == NULL && !QueueRun)
2934174Seric 		{
29451317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
29551317Seric 			usrerr("Cannot mail directly to files");
2964174Seric 		}
29751317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
29851317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
29951317Seric 		{
30051317Seric 			a->q_flags |= QBADADDR;
30155012Seric 			giveresponse(EX_CANTCREAT, m, e);
30251317Seric 		}
30351317Seric 	}
30451317Seric 
30557402Seric 	if (m != LocalMailer)
30657642Seric 	{
30757642Seric 		if (!bitset(QDONTSEND, a->q_flags))
30857642Seric 			e->e_nrcpts++;
30957402Seric 		return (a);
31057642Seric 	}
31157402Seric 
31257402Seric 	/* try aliasing */
31357402Seric 	alias(a, sendq, e);
31457402Seric 
31557402Seric # ifdef USERDB
31657402Seric 	/* if not aliased, look it up in the user database */
31757402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
31857402Seric 	{
31957402Seric 		extern int udbexpand();
32057402Seric 
32157402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
32257402Seric 		{
32357402Seric 			a->q_flags |= QQUEUEUP;
32457402Seric 			if (e->e_message == NULL)
32557402Seric 				e->e_message = newstr("Deferred: user database error");
32657402Seric # ifdef LOG
32758020Seric 			if (LogLevel > 8)
32857402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
32957402Seric 					e->e_id);
33057402Seric # endif
33157402Seric 			message(Arpa_Info, "queued (user database error)");
33257642Seric 			e->e_nrcpts++;
33357402Seric 			return (a);
33457402Seric 		}
33557402Seric 	}
33657402Seric # endif
33757402Seric 
33857402Seric 	/* if it was an alias or a UDB expansion, just return now */
33957402Seric 	if (bitset(QDONTSEND, a->q_flags))
34057402Seric 		return (a);
34157402Seric 
34251317Seric 	/*
34351317Seric 	**  If we have a level two config file, then pass the name through
34451317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
34551317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
34651317Seric 	**  after local aliasing has been done.
34751317Seric 	*/
34851317Seric 
34951317Seric 	if (tTd(29, 5))
35051317Seric 	{
35151317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
35251317Seric 			ConfigLevel, RewriteRules[5]);
35351317Seric 		printaddr(a, FALSE);
35451317Seric 	}
35551317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
35651317Seric 	    RewriteRules[5] != NULL)
35751317Seric 	{
35855012Seric 		maplocaluser(a, sendq, e);
35951317Seric 	}
36051317Seric 
36151317Seric 	/*
36251317Seric 	**  If it didn't get rewritten to another mailer, go ahead
36351317Seric 	**  and deliver it.
36451317Seric 	*/
36551317Seric 
36651317Seric 	if (!bitset(QDONTSEND, a->q_flags))
36751317Seric 	{
36855354Seric 		auto bool fuzzy;
36951317Seric 		register struct passwd *pw;
37051317Seric 		extern struct passwd *finduser();
37151317Seric 
37251317Seric 		/* warning -- finduser may trash buf */
37355354Seric 		pw = finduser(buf, &fuzzy);
37451317Seric 		if (pw == NULL)
37551317Seric 		{
37651317Seric 			a->q_flags |= QBADADDR;
37755012Seric 			giveresponse(EX_NOUSER, m, e);
37851317Seric 		}
3794174Seric 		else
3804174Seric 		{
38151317Seric 			char nbuf[MAXNAME];
3824373Seric 
38355354Seric 			if (fuzzy)
3844174Seric 			{
38553735Seric 				/* name was a fuzzy match */
38651317Seric 				a->q_user = newstr(pw->pw_name);
38753735Seric 				if (findusercount++ > 3)
38853735Seric 				{
38953735Seric 					usrerr("aliasing/forwarding loop for %s broken",
39053735Seric 						pw->pw_name);
39153735Seric 					return (a);
39253735Seric 				}
39353735Seric 
39453735Seric 				/* see if it aliases */
39551317Seric 				(void) strcpy(buf, pw->pw_name);
39653735Seric 				goto trylocaluser;
3974174Seric 			}
39851317Seric 			a->q_home = newstr(pw->pw_dir);
39951317Seric 			a->q_uid = pw->pw_uid;
40051317Seric 			a->q_gid = pw->pw_gid;
40151317Seric 			a->q_flags |= QGOODUID;
40251317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
40351317Seric 			if (nbuf[0] != '\0')
40451317Seric 				a->q_fullname = newstr(nbuf);
40551317Seric 			if (!quoted)
40655012Seric 				forward(a, sendq, e);
4074174Seric 		}
4084174Seric 	}
40957642Seric 	if (!bitset(QDONTSEND, a->q_flags))
41057642Seric 		e->e_nrcpts++;
41112613Seric 	return (a);
4124174Seric }
4134174Seric /*
4144373Seric **  FINDUSER -- find the password entry for a user.
4154373Seric **
4164373Seric **	This looks a lot like getpwnam, except that it may want to
4174373Seric **	do some fancier pattern matching in /etc/passwd.
4184373Seric **
4199379Seric **	This routine contains most of the time of many sendmail runs.
4209379Seric **	It deserves to be optimized.
4219379Seric **
4224373Seric **	Parameters:
4234373Seric **		name -- the name to match against.
42455354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
42555354Seric **			was found using the fuzzy matching algorithm;
42655354Seric **			set to FALSE otherwise.
4274373Seric **
4284373Seric **	Returns:
4294373Seric **		A pointer to a pw struct.
4304373Seric **		NULL if name is unknown or ambiguous.
4314373Seric **
4324373Seric **	Side Effects:
4334407Seric **		may modify name.
4344373Seric */
4354373Seric 
4364373Seric struct passwd *
43755354Seric finduser(name, fuzzyp)
4384373Seric 	char *name;
43955354Seric 	bool *fuzzyp;
4404373Seric {
4414376Seric 	register struct passwd *pw;
4424407Seric 	register char *p;
44315325Seric 	extern struct passwd *getpwent();
44415325Seric 	extern struct passwd *getpwnam();
4454373Seric 
44655354Seric 	if (tTd(29, 4))
44755354Seric 		printf("finduser(%s): ", name);
44855354Seric 
44925777Seric 	/* map upper => lower case */
4504407Seric 	for (p = name; *p != '\0'; p++)
4514407Seric 	{
45225777Seric 		if (isascii(*p) && isupper(*p))
45325568Seric 			*p = tolower(*p);
4544407Seric 	}
45555354Seric 	*fuzzyp = FALSE;
4564407Seric 
45725777Seric 	/* look up this login name using fast path */
45812634Seric 	if ((pw = getpwnam(name)) != NULL)
45955354Seric 	{
46055354Seric 		if (tTd(29, 4))
46155354Seric 			printf("found (non-fuzzy)\n");
46212634Seric 		return (pw);
46355354Seric 	}
46412634Seric 
46553735Seric #ifdef MATCHGECOS
46653735Seric 	/* see if fuzzy matching allowed */
46753735Seric 	if (!MatchGecos)
46855354Seric 	{
46955354Seric 		if (tTd(29, 4))
47055354Seric 			printf("not found (fuzzy disabled)\n");
47153735Seric 		return NULL;
47255354Seric 	}
47353735Seric 
47412634Seric 	/* search for a matching full name instead */
47525777Seric 	for (p = name; *p != '\0'; p++)
47625777Seric 	{
47725777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
47825777Seric 			*p = ' ';
47925777Seric 	}
48023107Seric 	(void) setpwent();
4814376Seric 	while ((pw = getpwent()) != NULL)
4824376Seric 	{
4834998Seric 		char buf[MAXNAME];
4844376Seric 
4854998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
48656795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4874381Seric 		{
48855354Seric 			if (tTd(29, 4))
48955354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
4907054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
49155354Seric 			*fuzzyp = TRUE;
4924376Seric 			return (pw);
4934377Seric 		}
4944376Seric 	}
49553735Seric #endif
49655354Seric 	if (tTd(29, 4))
49755354Seric 		printf("no fuzzy match found\n");
4984376Seric 	return (NULL);
4994373Seric }
5004373Seric /*
5014329Seric **  WRITABLE -- predicate returning if the file is writable.
5024329Seric **
5034329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5044329Seric **	Unfortunately, we cannot use the access call since we
5054329Seric **	won't necessarily be the real uid when we try to
5064329Seric **	actually open the file.
5074329Seric **
5084329Seric **	Notice that ANY file with ANY execute bit is automatically
5094329Seric **	not writable.  This is also enforced by mailfile.
5104329Seric **
5114329Seric **	Parameters:
5124329Seric **		s -- pointer to a stat struct for the file.
5134329Seric **
5144329Seric **	Returns:
5154329Seric **		TRUE -- if we will be able to write this file.
5164329Seric **		FALSE -- if we cannot write this file.
5174329Seric **
5184329Seric **	Side Effects:
5194329Seric **		none.
5204329Seric */
5214329Seric 
5224329Seric bool
5234329Seric writable(s)
5244329Seric 	register struct stat *s;
5254329Seric {
52655372Seric 	uid_t euid;
52755372Seric 	gid_t egid;
5284329Seric 	int bits;
5294329Seric 
5304329Seric 	if (bitset(0111, s->st_mode))
5314329Seric 		return (FALSE);
5324329Seric 	euid = getruid();
5334329Seric 	egid = getrgid();
5344329Seric 	if (geteuid() == 0)
5354329Seric 	{
5364329Seric 		if (bitset(S_ISUID, s->st_mode))
5374329Seric 			euid = s->st_uid;
5384329Seric 		if (bitset(S_ISGID, s->st_mode))
5394329Seric 			egid = s->st_gid;
5404329Seric 	}
5414329Seric 
5424329Seric 	if (euid == 0)
5434329Seric 		return (TRUE);
5444329Seric 	bits = S_IWRITE;
5454329Seric 	if (euid != s->st_uid)
5464329Seric 	{
5474329Seric 		bits >>= 3;
5484329Seric 		if (egid != s->st_gid)
5494329Seric 			bits >>= 3;
5504329Seric 	}
5514329Seric 	return ((s->st_mode & bits) != 0);
5524329Seric }
5534329Seric /*
5544174Seric **  INCLUDE -- handle :include: specification.
5554174Seric **
5564174Seric **	Parameters:
5574174Seric **		fname -- filename to include.
55853037Seric **		forwarding -- if TRUE, we are reading a .forward file.
55953037Seric **			if FALSE, it's a :include: file.
5604399Seric **		ctladdr -- address template to use to fill in these
5614399Seric **			addresses -- effective user/group id are
5624399Seric **			the important things.
5635006Seric **		sendq -- a pointer to the head of the send queue
5645006Seric **			to put these addresses in.
5654174Seric **
5664174Seric **	Returns:
56757136Seric **		open error status
5684174Seric **
5694174Seric **	Side Effects:
5704174Seric **		reads the :include: file and sends to everyone
5714174Seric **		listed in that file.
5724174Seric */
5734174Seric 
57453037Seric static jmp_buf	CtxIncludeTimeout;
57553037Seric 
57657136Seric int
57755012Seric include(fname, forwarding, ctladdr, sendq, e)
5784174Seric 	char *fname;
57953037Seric 	bool forwarding;
5804399Seric 	ADDRESS *ctladdr;
5815006Seric 	ADDRESS **sendq;
58255012Seric 	ENVELOPE *e;
5834174Seric {
5844174Seric 	register FILE *fp;
58555012Seric 	char *oldto = e->e_to;
5869379Seric 	char *oldfilename = FileName;
5879379Seric 	int oldlinenumber = LineNumber;
58853037Seric 	register EVENT *ev = NULL;
58953037Seric 	char buf[MAXLINE];
59053037Seric 	static int includetimeout();
5914174Seric 
59257186Seric 	if (tTd(27, 2))
59357186Seric 		printf("include(%s)\n", fname);
59457186Seric 
59553037Seric 	/*
59653037Seric 	**  If home directory is remote mounted but server is down,
59753037Seric 	**  this can hang or give errors; use a timeout to avoid this
59853037Seric 	*/
59953037Seric 
60053037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
60153037Seric 	{
60253037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
60353037Seric 		errno = 0;
60453037Seric 		usrerr("451 open timeout on %s", fname);
60557136Seric 		return ETIMEDOUT;
60653037Seric 	}
60753037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
60853037Seric 
60953037Seric 	/* if forwarding, the input file must be marked safe */
61053037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
61153037Seric 	{
61253037Seric 		/* don't use this .forward file */
61353037Seric 		clrevent(ev);
61457186Seric 		if (tTd(27, 4))
61557186Seric 			printf("include: not safe (uid=%d)\n", ctladdr->q_uid);
61657136Seric 		return EPERM;
61753037Seric 	}
61853037Seric 
6194174Seric 	fp = fopen(fname, "r");
6204174Seric 	if (fp == NULL)
6214174Seric 	{
62257136Seric 		int ret = errno;
62357136Seric 
62458061Seric 		clrevent(ev);
6254174Seric 		usrerr("Cannot open %s", fname);
62657136Seric 		return ret;
6274174Seric 	}
62853037Seric 
6294406Seric 	if (getctladdr(ctladdr) == NULL)
6304406Seric 	{
6314406Seric 		struct stat st;
6324174Seric 
6334406Seric 		if (fstat(fileno(fp), &st) < 0)
63458061Seric 		{
63558061Seric 			int ret = errno;
63658061Seric 
63758061Seric 			clrevent(ev);
6384406Seric 			syserr("Cannot fstat %s!", fname);
63958061Seric 			return ret;
64058061Seric 		}
6414406Seric 		ctladdr->q_uid = st.st_uid;
6424406Seric 		ctladdr->q_gid = st.st_gid;
6434406Seric 		ctladdr->q_flags |= QGOODUID;
6444406Seric 	}
6454406Seric 
64653037Seric 	clrevent(ev);
64753037Seric 
6484174Seric 	/* read the file -- each line is a comma-separated list. */
6499379Seric 	FileName = fname;
6509379Seric 	LineNumber = 0;
6514174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6524174Seric 	{
65356795Seric 		register char *p = strchr(buf, '\n');
6544174Seric 
65540963Sbostic 		LineNumber++;
6564174Seric 		if (p != NULL)
6574174Seric 			*p = '\0';
65857186Seric 		if (buf[0] == '#' || buf[0] == '\0')
65957139Seric 			continue;
66058008Seric 		e->e_to = NULL;
66153037Seric 		message(Arpa_Info, "%s to %s",
66253037Seric 			forwarding ? "forwarding" : "sending", buf);
66357977Seric #ifdef LOG
66458020Seric 		if (forwarding && LogLevel > 9)
66557977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
66657977Seric 				e->e_id, oldto, buf);
66757977Seric #endif
66857977Seric 
6694176Seric 		AliasLevel++;
67055012Seric 		sendtolist(buf, ctladdr, sendq, e);
6714176Seric 		AliasLevel--;
6724174Seric 	}
673*58065Seric 	if (!bitset(QSELFREF, ctladdr->q_flags))
674*58065Seric 	{
675*58065Seric 		if (tTd(27, 5))
676*58065Seric 		{
677*58065Seric 			printf("include: QDONTSEND ");
678*58065Seric 			printaddr(ctladdr, FALSE);
679*58065Seric 		}
680*58065Seric 		ctladdr->q_flags |= QDONTSEND;
681*58065Seric 	}
6824174Seric 
6834319Seric 	(void) fclose(fp);
6849379Seric 	FileName = oldfilename;
6859379Seric 	LineNumber = oldlinenumber;
68657136Seric 	return 0;
6874174Seric }
68853037Seric 
68953037Seric static
69053037Seric includetimeout()
69153037Seric {
69253037Seric 	longjmp(CtxIncludeTimeout, 1);
69353037Seric }
6944324Seric /*
6954324Seric **  SENDTOARGV -- send to an argument vector.
6964324Seric **
6974324Seric **	Parameters:
6984324Seric **		argv -- argument vector to send to.
6994324Seric **
7004324Seric **	Returns:
7014324Seric **		none.
7024324Seric **
7034324Seric **	Side Effects:
7044324Seric **		puts all addresses on the argument vector onto the
7054324Seric **			send queue.
7064324Seric */
7074324Seric 
70855012Seric sendtoargv(argv, e)
7094324Seric 	register char **argv;
71055012Seric 	register ENVELOPE *e;
7114324Seric {
7124324Seric 	register char *p;
7134324Seric 
7144324Seric 	while ((p = *argv++) != NULL)
7154324Seric 	{
71633725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
7174324Seric 		{
7184324Seric 			char nbuf[MAXNAME];
7194324Seric 
7204324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
7214324Seric 				usrerr("address overflow");
7224324Seric 			else
7234324Seric 			{
7244324Seric 				(void) strcpy(nbuf, p);
7254324Seric 				(void) strcat(nbuf, "@");
7264324Seric 				(void) strcat(nbuf, argv[1]);
7274324Seric 				p = newstr(nbuf);
7284324Seric 				argv += 2;
7294324Seric 			}
7304324Seric 		}
73155012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7324324Seric 	}
7334324Seric }
7344399Seric /*
7354399Seric **  GETCTLADDR -- get controlling address from an address header.
7364399Seric **
7374399Seric **	If none, get one corresponding to the effective userid.
7384399Seric **
7394399Seric **	Parameters:
7404399Seric **		a -- the address to find the controller of.
7414399Seric **
7424399Seric **	Returns:
7434399Seric **		the controlling address.
7444399Seric **
7454399Seric **	Side Effects:
7464399Seric **		none.
7474399Seric */
7484399Seric 
7494399Seric ADDRESS *
7504399Seric getctladdr(a)
7514399Seric 	register ADDRESS *a;
7524399Seric {
7534404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7544399Seric 		a = a->q_alias;
7554399Seric 	return (a);
7564399Seric }
757