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*55372Seric static char sccsid[] = "@(#)recipient.c	5.35 (Berkeley) 07/19/92";
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 &&
638230Seric 	    (index(list, ',') != NULL || index(list, ';') != NULL ||
648230Seric 	     index(list, '<') != NULL || index(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 
1954627Seric 	/* do sickly crude mapping for program mailing, etc. */
1969210Seric 	if (m == LocalMailer && buf[0] == '|')
1974174Seric 	{
1989210Seric 		a->q_mailer = m = ProgMailer;
1999210Seric 		a->q_user++;
20054975Seric 		if (a->q_alias == NULL && !ForceMail)
2014174Seric 		{
20229915Seric 			a->q_flags |= QDONTSEND|QBADADDR;
2039210Seric 			usrerr("Cannot mail directly to programs");
2044174Seric 		}
2054174Seric 	}
2064174Seric 
2074174Seric 	/*
2084419Seric 	**  Look up this person in the recipient list.
2094419Seric 	**	If they are there already, return, otherwise continue.
2104419Seric 	**	If the list is empty, just add it.  Notice the cute
2114419Seric 	**	hack to make from addresses suppress things correctly:
2124419Seric 	**	the QDONTSEND bit will be set in the send list.
2134419Seric 	**	[Please note: the emphasis is on "hack."]
2144174Seric 	*/
2154174Seric 
2165006Seric 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
2174174Seric 	{
2189379Seric 		if (!ForceMail && sameaddr(q, a))
2194174Seric 		{
2207676Seric 			if (tTd(26, 1))
2214444Seric 			{
2224444Seric 				printf("%s in sendq: ", a->q_paddr);
2234444Seric 				printaddr(q, FALSE);
2244444Seric 			}
2257054Seric 			if (!bitset(QDONTSEND, a->q_flags))
2264324Seric 				message(Arpa_Info, "duplicate suppressed");
2274423Seric 			if (!bitset(QPRIMARY, q->q_flags))
2284423Seric 				q->q_flags |= a->q_flags;
22912613Seric 			return (q);
2304174Seric 		}
2314319Seric 	}
2324174Seric 
2334319Seric 	/* add address on list */
2344319Seric 	*pq = a;
2354174Seric 	a->q_next = NULL;
23655012Seric 	e->e_nrcpts++;
2374174Seric 
23852046Seric 	if (a->q_alias == NULL && RcptLogFile != NULL &&
23952046Seric 	    !bitset(QDONTSEND, a->q_flags))
24052046Seric 	{
24152046Seric 		static int RcptLogFd = -1;
24252046Seric 
24352046Seric 		/*
24452046Seric 		**  Log the incoming recipient name before aliasing,
24552046Seric 		**  expanding, forwarding, rewriting, and all that jazz.
24652046Seric 		**  We'll use this to track down out-of-date aliases,
24752046Seric 		**  host names, and so forth.
24852046Seric 		*/
24952046Seric 
25052046Seric 		if (RcptLogFd < 0)
25152046Seric 		{
25252046Seric 			/* try to open the log file */
25352046Seric 			RcptLogFd = open(RcptLogFile, O_WRONLY|O_APPEND|O_CREAT, 0666);
25452047Seric 			if (RcptLogFd >= 0)
25552047Seric 				(void) fcntl(RcptLogFd, F_SETFD, 1);
25652046Seric 		}
25752046Seric 		if (RcptLogFd >= 0)
25852046Seric 		{
25952046Seric 			int l = strlen(a->q_paddr);
26052046Seric 
26152046Seric 			a->q_paddr[l] = '\n';
26252046Seric 			if (write(RcptLogFd, a->q_paddr, l + 1) < 0)
26352046Seric 			{
26452046Seric 				(void) close(RcptLogFd);
26552046Seric 				RcptLogFd = -1;
26652046Seric 			}
26752046Seric 			a->q_paddr[l] = '\0';
26852046Seric 		}
26952046Seric 	}
27052046Seric 
2714174Seric 	/*
2724174Seric 	**  Alias the name and handle :include: specs.
2734174Seric 	*/
2744174Seric 
27553735Seric   trylocaluser:
27655354Seric 	if (tTd(29, 7))
27755354Seric 		printf("at trylocaluser %s\n", a->q_user);
27855354Seric 
2799210Seric 	if (m == LocalMailer && !bitset(QDONTSEND, a->q_flags))
2804174Seric 	{
2814174Seric 		if (strncmp(a->q_user, ":include:", 9) == 0)
2824174Seric 		{
2834174Seric 			a->q_flags |= QDONTSEND;
28454975Seric 			if (a->q_alias == NULL && !ForceMail)
28529915Seric 			{
28629915Seric 				a->q_flags |= QBADADDR;
2874399Seric 				usrerr("Cannot mail directly to :include:s");
28829915Seric 			}
2894399Seric 			else
2904399Seric 			{
2917054Seric 				message(Arpa_Info, "including file %s", &a->q_user[9]);
29255012Seric 				include(&a->q_user[9], FALSE, a, sendq, e);
2934399Seric 			}
2944174Seric 		}
2954174Seric 		else
29650556Seric 		{
29750556Seric 			/* try aliasing */
29855012Seric 			alias(a, sendq, e);
29950556Seric 
30050556Seric # ifdef USERDB
30151923Seric 			/* if not aliased, look it up in the user database */
30251360Seric 			if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
30351923Seric 			{
30451923Seric 				extern int udbexpand();
30551923Seric 
30655012Seric 				if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
30751923Seric 				{
30851923Seric 					a->q_flags |= QQUEUEUP;
30955012Seric 					if (e->e_message == NULL)
31055012Seric 						e->e_message = newstr("Deferred: user database error");
31151923Seric # ifdef LOG
31251923Seric 					if (LogLevel > 3)
31351923Seric 						syslog(LOG_INFO, "%s: deferred: udbexpand",
31455012Seric 							e->e_id);
31550556Seric # endif
31651923Seric 					message(Arpa_Info, "queued (user database error)");
31751923Seric 					return (a);
31851923Seric 				}
31951923Seric 			}
32051923Seric # endif
32150556Seric 		}
3224174Seric 	}
3234174Seric 
3244174Seric 	/*
3254174Seric 	**  If the user is local and still being sent, verify that
3264174Seric 	**  the address is good.  If it is, try to forward.
3274174Seric 	**  If the address is already good, we have a forwarding
3284174Seric 	**  loop.  This can be broken by just sending directly to
3294174Seric 	**  the user (which is probably correct anyway).
3304174Seric 	*/
3314174Seric 
33251317Seric 	if (bitset(QDONTSEND, a->q_flags) || m != LocalMailer)
33351317Seric 		return (a);
33451317Seric 
33551317Seric 	/* see if this is to a file */
33651317Seric 	if (buf[0] == '/')
3374174Seric 	{
3384329Seric 		struct stat stb;
3394329Seric 		extern bool writable();
3404174Seric 
34151317Seric 		p = rindex(buf, '/');
34251317Seric 		/* check if writable or creatable */
34351317Seric 		if (a->q_alias == NULL && !QueueRun && !ForceMail)
3444174Seric 		{
34551317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
34651317Seric 			usrerr("Cannot mail directly to files");
3474174Seric 		}
34851317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
34951317Seric 		    (*p = '\0', !safefile(buf, getruid(), S_IWRITE|S_IEXEC)))
35051317Seric 		{
35151317Seric 			a->q_flags |= QBADADDR;
35255012Seric 			giveresponse(EX_CANTCREAT, m, e);
35351317Seric 		}
35451317Seric 		return (a);
35551317Seric 	}
35651317Seric 
35751317Seric 	/*
35851317Seric 	**  If we have a level two config file, then pass the name through
35951317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
36051317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
36151317Seric 	**  after local aliasing has been done.
36251317Seric 	*/
36351317Seric 
36451317Seric 	if (tTd(29, 5))
36551317Seric 	{
36651317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
36751317Seric 			ConfigLevel, RewriteRules[5]);
36851317Seric 		printaddr(a, FALSE);
36951317Seric 	}
37051317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
37151317Seric 	    RewriteRules[5] != NULL)
37251317Seric 	{
37355012Seric 		maplocaluser(a, sendq, e);
37451317Seric 	}
37551317Seric 
37651317Seric 	/*
37751317Seric 	**  If it didn't get rewritten to another mailer, go ahead
37851317Seric 	**  and deliver it.
37951317Seric 	*/
38051317Seric 
38151317Seric 	if (!bitset(QDONTSEND, a->q_flags))
38251317Seric 	{
38355354Seric 		auto bool fuzzy;
38451317Seric 		register struct passwd *pw;
38551317Seric 		extern struct passwd *finduser();
38651317Seric 
38751317Seric 		/* warning -- finduser may trash buf */
38855354Seric 		pw = finduser(buf, &fuzzy);
38951317Seric 		if (pw == NULL)
39051317Seric 		{
39151317Seric 			a->q_flags |= QBADADDR;
39255012Seric 			giveresponse(EX_NOUSER, m, e);
39351317Seric 		}
3944174Seric 		else
3954174Seric 		{
39651317Seric 			char nbuf[MAXNAME];
3974373Seric 
39855354Seric 			if (fuzzy)
3994174Seric 			{
40053735Seric 				/* name was a fuzzy match */
40151317Seric 				a->q_user = newstr(pw->pw_name);
40253735Seric 				if (findusercount++ > 3)
40353735Seric 				{
40453735Seric 					usrerr("aliasing/forwarding loop for %s broken",
40553735Seric 						pw->pw_name);
40653735Seric 					return (a);
40753735Seric 				}
40853735Seric 
40953735Seric 				/* see if it aliases */
41051317Seric 				(void) strcpy(buf, pw->pw_name);
41153735Seric 				goto trylocaluser;
4124174Seric 			}
41351317Seric 			a->q_home = newstr(pw->pw_dir);
41451317Seric 			a->q_uid = pw->pw_uid;
41551317Seric 			a->q_gid = pw->pw_gid;
41651317Seric 			a->q_flags |= QGOODUID;
41751317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
41851317Seric 			if (nbuf[0] != '\0')
41951317Seric 				a->q_fullname = newstr(nbuf);
42051317Seric 			if (!quoted)
42155012Seric 				forward(a, sendq, e);
4224174Seric 		}
4234174Seric 	}
42412613Seric 	return (a);
4254174Seric }
4264174Seric /*
4274373Seric **  FINDUSER -- find the password entry for a user.
4284373Seric **
4294373Seric **	This looks a lot like getpwnam, except that it may want to
4304373Seric **	do some fancier pattern matching in /etc/passwd.
4314373Seric **
4329379Seric **	This routine contains most of the time of many sendmail runs.
4339379Seric **	It deserves to be optimized.
4349379Seric **
4354373Seric **	Parameters:
4364373Seric **		name -- the name to match against.
43755354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
43855354Seric **			was found using the fuzzy matching algorithm;
43955354Seric **			set to FALSE otherwise.
4404373Seric **
4414373Seric **	Returns:
4424373Seric **		A pointer to a pw struct.
4434373Seric **		NULL if name is unknown or ambiguous.
4444373Seric **
4454373Seric **	Side Effects:
4464407Seric **		may modify name.
4474373Seric */
4484373Seric 
4494373Seric struct passwd *
45055354Seric finduser(name, fuzzyp)
4514373Seric 	char *name;
45255354Seric 	bool *fuzzyp;
4534373Seric {
4544376Seric 	register struct passwd *pw;
4554407Seric 	register char *p;
45615325Seric 	extern struct passwd *getpwent();
45715325Seric 	extern struct passwd *getpwnam();
4584373Seric 
45955354Seric 	if (tTd(29, 4))
46055354Seric 		printf("finduser(%s): ", name);
46155354Seric 
46225777Seric 	/* map upper => lower case */
4634407Seric 	for (p = name; *p != '\0'; p++)
4644407Seric 	{
46525777Seric 		if (isascii(*p) && isupper(*p))
46625568Seric 			*p = tolower(*p);
4674407Seric 	}
46855354Seric 	*fuzzyp = FALSE;
4694407Seric 
47025777Seric 	/* look up this login name using fast path */
47112634Seric 	if ((pw = getpwnam(name)) != NULL)
47255354Seric 	{
47355354Seric 		if (tTd(29, 4))
47455354Seric 			printf("found (non-fuzzy)\n");
47512634Seric 		return (pw);
47655354Seric 	}
47712634Seric 
47853735Seric #ifdef MATCHGECOS
47953735Seric 	/* see if fuzzy matching allowed */
48053735Seric 	if (!MatchGecos)
48155354Seric 	{
48255354Seric 		if (tTd(29, 4))
48355354Seric 			printf("not found (fuzzy disabled)\n");
48453735Seric 		return NULL;
48555354Seric 	}
48653735Seric 
48712634Seric 	/* search for a matching full name instead */
48825777Seric 	for (p = name; *p != '\0'; p++)
48925777Seric 	{
49025777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
49125777Seric 			*p = ' ';
49225777Seric 	}
49323107Seric 	(void) setpwent();
4944376Seric 	while ((pw = getpwent()) != NULL)
4954376Seric 	{
4964998Seric 		char buf[MAXNAME];
4974376Seric 
4984998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
49933725Sbostic 		if (index(buf, ' ') != NULL && !strcasecmp(buf, name))
5004381Seric 		{
50155354Seric 			if (tTd(29, 4))
50255354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
5037054Seric 			message(Arpa_Info, "sending to login name %s", pw->pw_name);
50455354Seric 			*fuzzyp = TRUE;
5054376Seric 			return (pw);
5064377Seric 		}
5074376Seric 	}
50853735Seric #endif
50955354Seric 	if (tTd(29, 4))
51055354Seric 		printf("no fuzzy match found\n");
5114376Seric 	return (NULL);
5124373Seric }
5134373Seric /*
5144329Seric **  WRITABLE -- predicate returning if the file is writable.
5154329Seric **
5164329Seric **	This routine must duplicate the algorithm in sys/fio.c.
5174329Seric **	Unfortunately, we cannot use the access call since we
5184329Seric **	won't necessarily be the real uid when we try to
5194329Seric **	actually open the file.
5204329Seric **
5214329Seric **	Notice that ANY file with ANY execute bit is automatically
5224329Seric **	not writable.  This is also enforced by mailfile.
5234329Seric **
5244329Seric **	Parameters:
5254329Seric **		s -- pointer to a stat struct for the file.
5264329Seric **
5274329Seric **	Returns:
5284329Seric **		TRUE -- if we will be able to write this file.
5294329Seric **		FALSE -- if we cannot write this file.
5304329Seric **
5314329Seric **	Side Effects:
5324329Seric **		none.
5334329Seric */
5344329Seric 
5354329Seric bool
5364329Seric writable(s)
5374329Seric 	register struct stat *s;
5384329Seric {
539*55372Seric 	uid_t euid;
540*55372Seric 	gid_t egid;
5414329Seric 	int bits;
5424329Seric 
5434329Seric 	if (bitset(0111, s->st_mode))
5444329Seric 		return (FALSE);
5454329Seric 	euid = getruid();
5464329Seric 	egid = getrgid();
5474329Seric 	if (geteuid() == 0)
5484329Seric 	{
5494329Seric 		if (bitset(S_ISUID, s->st_mode))
5504329Seric 			euid = s->st_uid;
5514329Seric 		if (bitset(S_ISGID, s->st_mode))
5524329Seric 			egid = s->st_gid;
5534329Seric 	}
5544329Seric 
5554329Seric 	if (euid == 0)
5564329Seric 		return (TRUE);
5574329Seric 	bits = S_IWRITE;
5584329Seric 	if (euid != s->st_uid)
5594329Seric 	{
5604329Seric 		bits >>= 3;
5614329Seric 		if (egid != s->st_gid)
5624329Seric 			bits >>= 3;
5634329Seric 	}
5644329Seric 	return ((s->st_mode & bits) != 0);
5654329Seric }
5664329Seric /*
5674174Seric **  INCLUDE -- handle :include: specification.
5684174Seric **
5694174Seric **	Parameters:
5704174Seric **		fname -- filename to include.
57153037Seric **		forwarding -- if TRUE, we are reading a .forward file.
57253037Seric **			if FALSE, it's a :include: file.
5734399Seric **		ctladdr -- address template to use to fill in these
5744399Seric **			addresses -- effective user/group id are
5754399Seric **			the important things.
5765006Seric **		sendq -- a pointer to the head of the send queue
5775006Seric **			to put these addresses in.
5784174Seric **
5794174Seric **	Returns:
5804174Seric **		none.
5814174Seric **
5824174Seric **	Side Effects:
5834174Seric **		reads the :include: file and sends to everyone
5844174Seric **		listed in that file.
5854174Seric */
5864174Seric 
58753037Seric static jmp_buf	CtxIncludeTimeout;
58853037Seric 
58955012Seric include(fname, forwarding, ctladdr, sendq, e)
5904174Seric 	char *fname;
59153037Seric 	bool forwarding;
5924399Seric 	ADDRESS *ctladdr;
5935006Seric 	ADDRESS **sendq;
59455012Seric 	ENVELOPE *e;
5954174Seric {
5964174Seric 	register FILE *fp;
59755012Seric 	char *oldto = e->e_to;
5989379Seric 	char *oldfilename = FileName;
5999379Seric 	int oldlinenumber = LineNumber;
60053037Seric 	register EVENT *ev = NULL;
60153037Seric 	char buf[MAXLINE];
60253037Seric 	static int includetimeout();
6034174Seric 
60453037Seric 	/*
60553037Seric 	**  If home directory is remote mounted but server is down,
60653037Seric 	**  this can hang or give errors; use a timeout to avoid this
60753037Seric 	*/
60853037Seric 
60953037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
61053037Seric 	{
61153037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
61253037Seric 		errno = 0;
61353037Seric 		usrerr("451 open timeout on %s", fname);
61453037Seric 		return;
61553037Seric 	}
61653037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
61753037Seric 
61853037Seric 	/* if forwarding, the input file must be marked safe */
61953037Seric 	if (forwarding && !safefile(fname, ctladdr->q_uid, S_IREAD))
62053037Seric 	{
62153037Seric 		/* don't use this .forward file */
62253037Seric 		clrevent(ev);
62353037Seric 		return;
62453037Seric 	}
62553037Seric 
6264174Seric 	fp = fopen(fname, "r");
6274174Seric 	if (fp == NULL)
6284174Seric 	{
6294174Seric 		usrerr("Cannot open %s", fname);
6304174Seric 		return;
6314174Seric 	}
63253037Seric 
6334406Seric 	if (getctladdr(ctladdr) == NULL)
6344406Seric 	{
6354406Seric 		struct stat st;
6364174Seric 
6374406Seric 		if (fstat(fileno(fp), &st) < 0)
6384406Seric 			syserr("Cannot fstat %s!", fname);
6394406Seric 		ctladdr->q_uid = st.st_uid;
6404406Seric 		ctladdr->q_gid = st.st_gid;
6414406Seric 		ctladdr->q_flags |= QGOODUID;
6424406Seric 	}
6434406Seric 
64453037Seric 	clrevent(ev);
64553037Seric 
6464174Seric 	/* read the file -- each line is a comma-separated list. */
6479379Seric 	FileName = fname;
6489379Seric 	LineNumber = 0;
6494174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6504174Seric 	{
6514174Seric 		register char *p = index(buf, '\n');
6524174Seric 
65340963Sbostic 		LineNumber++;
6544174Seric 		if (p != NULL)
6554174Seric 			*p = '\0';
65651781Seric 		if (buf[0] == '\0' || buf[0] == '#')
6574174Seric 			continue;
65855012Seric 		e->e_to = oldto;
65953037Seric 		message(Arpa_Info, "%s to %s",
66053037Seric 			forwarding ? "forwarding" : "sending", buf);
6614176Seric 		AliasLevel++;
66255012Seric 		sendtolist(buf, ctladdr, sendq, e);
6634176Seric 		AliasLevel--;
6644174Seric 	}
6654174Seric 
6664319Seric 	(void) fclose(fp);
6679379Seric 	FileName = oldfilename;
6689379Seric 	LineNumber = oldlinenumber;
6694174Seric }
67053037Seric 
67153037Seric static
67253037Seric includetimeout()
67353037Seric {
67453037Seric 	longjmp(CtxIncludeTimeout, 1);
67553037Seric }
6764324Seric /*
6774324Seric **  SENDTOARGV -- send to an argument vector.
6784324Seric **
6794324Seric **	Parameters:
6804324Seric **		argv -- argument vector to send to.
6814324Seric **
6824324Seric **	Returns:
6834324Seric **		none.
6844324Seric **
6854324Seric **	Side Effects:
6864324Seric **		puts all addresses on the argument vector onto the
6874324Seric **			send queue.
6884324Seric */
6894324Seric 
69055012Seric sendtoargv(argv, e)
6914324Seric 	register char **argv;
69255012Seric 	register ENVELOPE *e;
6934324Seric {
6944324Seric 	register char *p;
6954324Seric 
6964324Seric 	while ((p = *argv++) != NULL)
6974324Seric 	{
69833725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
6994324Seric 		{
7004324Seric 			char nbuf[MAXNAME];
7014324Seric 
7024324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
7034324Seric 				usrerr("address overflow");
7044324Seric 			else
7054324Seric 			{
7064324Seric 				(void) strcpy(nbuf, p);
7074324Seric 				(void) strcat(nbuf, "@");
7084324Seric 				(void) strcat(nbuf, argv[1]);
7094324Seric 				p = newstr(nbuf);
7104324Seric 				argv += 2;
7114324Seric 			}
7124324Seric 		}
71355012Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7144324Seric 	}
7154324Seric }
7164399Seric /*
7174399Seric **  GETCTLADDR -- get controlling address from an address header.
7184399Seric **
7194399Seric **	If none, get one corresponding to the effective userid.
7204399Seric **
7214399Seric **	Parameters:
7224399Seric **		a -- the address to find the controller of.
7234399Seric **
7244399Seric **	Returns:
7254399Seric **		the controlling address.
7264399Seric **
7274399Seric **	Side Effects:
7284399Seric **		none.
7294399Seric */
7304399Seric 
7314399Seric ADDRESS *
7324399Seric getctladdr(a)
7334399Seric 	register ADDRESS *a;
7344399Seric {
7354404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7364399Seric 		a = a->q_alias;
7374399Seric 	return (a);
7384399Seric }
739