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*58294Seric static char sccsid[] = "@(#)recipient.c	6.19 (Berkeley) 02/27/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.
3358247Seric **		e -- the envelope in which to add these recipients.
344174Seric **
354174Seric **	Returns:
3658082Seric **		The number of addresses actually on the list.
374174Seric **
384174Seric **	Side Effects:
394174Seric **		none.
404174Seric */
414174Seric 
424174Seric # define MAXRCRSN	10
434174Seric 
4455012Seric sendtolist(list, ctladdr, sendq, e)
454174Seric 	char *list;
464399Seric 	ADDRESS *ctladdr;
475198Seric 	ADDRESS **sendq;
4855012Seric 	register ENVELOPE *e;
494174Seric {
504174Seric 	register char *p;
518223Seric 	register ADDRESS *al;	/* list of addresses to send to */
524423Seric 	bool firstone;		/* set on first address sent */
5311446Seric 	char delimiter;		/* the address delimiter */
5458082Seric 	int naddrs;
554174Seric 
567676Seric 	if (tTd(25, 1))
574444Seric 	{
584444Seric 		printf("sendto: %s\n   ctladdr=", list);
594444Seric 		printaddr(ctladdr, FALSE);
604444Seric 	}
614324Seric 
628223Seric 	/* heuristic to determine old versus new style addresses */
638230Seric 	if (ctladdr == NULL &&
6456795Seric 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
6556795Seric 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
6655012Seric 		e->e_flags &= ~EF_OLDSTYLE;
6711446Seric 	delimiter = ' ';
6855012Seric 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
6911446Seric 		delimiter = ',';
708223Seric 
714423Seric 	firstone = TRUE;
724324Seric 	al = NULL;
7358082Seric 	naddrs = 0;
748223Seric 
758081Seric 	for (p = list; *p != '\0'; )
764174Seric 	{
778081Seric 		register ADDRESS *a;
788081Seric 		extern char *DelimChar;		/* defined in prescan */
794319Seric 
808081Seric 		/* parse the address */
8158050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
824174Seric 			p++;
8355012Seric 		a = parseaddr(p, (ADDRESS *) NULL, 1, delimiter, e);
849297Seric 		p = DelimChar;
859297Seric 		if (a == NULL)
864174Seric 			continue;
874324Seric 		a->q_next = al;
884399Seric 		a->q_alias = ctladdr;
894444Seric 
904444Seric 		/* see if this should be marked as a primary address */
914423Seric 		if (ctladdr == NULL ||
928081Seric 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
934423Seric 			a->q_flags |= QPRIMARY;
944444Seric 
959379Seric 		if (ctladdr != NULL && sameaddr(ctladdr, a))
9658061Seric 			ctladdr->q_flags |= QSELFREF;
9757731Seric 		al = a;
984423Seric 		firstone = FALSE;
994324Seric 	}
1004324Seric 
1014324Seric 	/* arrange to send to everyone on the local send list */
1024324Seric 	while (al != NULL)
1034324Seric 	{
1044324Seric 		register ADDRESS *a = al;
10512613Seric 		extern ADDRESS *recipient();
1064324Seric 
1074324Seric 		al = a->q_next;
10855012Seric 		a = recipient(a, sendq, e);
1094993Seric 
1104998Seric 		/* arrange to inherit full name */
1114998Seric 		if (a->q_fullname == NULL && ctladdr != NULL)
1124998Seric 			a->q_fullname = ctladdr->q_fullname;
11358082Seric 		naddrs++;
1144174Seric 	}
1154324Seric 
11655012Seric 	e->e_to = NULL;
11758082Seric 	return (naddrs);
1184174Seric }
1194174Seric /*
1204174Seric **  RECIPIENT -- Designate a message recipient
1214174Seric **
1224174Seric **	Saves the named person for future mailing.
1234174Seric **
1244174Seric **	Parameters:
1254174Seric **		a -- the (preparsed) address header for the recipient.
1265006Seric **		sendq -- a pointer to the head of a queue to put the
1275006Seric **			recipient in.  Duplicate supression is done
1285006Seric **			in this queue.
12957731Seric **		e -- the current envelope.
1304174Seric **
1314174Seric **	Returns:
13212613Seric **		The actual address in the queue.  This will be "a" if
13312613Seric **		the address is not a duplicate, else the original address.
1344174Seric **
1354174Seric **	Side Effects:
1364174Seric **		none.
1374174Seric */
1384174Seric 
13946928Sbostic extern ADDRESS *getctladdr();
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 */
15458247Seric 	extern int 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 	{
16858151Seric 		usrerr("554 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;
19658151Seric 		usrerr("550 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 	{
210*58294Seric 		if (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))
22058151Seric 					message("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 
2314174Seric 	/*
23257402Seric 	**  Alias the name and handle special mailer types.
2334174Seric 	*/
2344174Seric 
23553735Seric   trylocaluser:
23655354Seric 	if (tTd(29, 7))
23755354Seric 		printf("at trylocaluser %s\n", a->q_user);
23855354Seric 
23958154Seric 	if (bitset(QDONTSEND|QVERIFIED, a->q_flags))
24057402Seric 		return (a);
24157402Seric 
24257402Seric 	if (m == InclMailer)
2434174Seric 	{
24457402Seric 		a->q_flags |= QDONTSEND;
24557731Seric 		if (a->q_alias == NULL)
2464174Seric 		{
24757402Seric 			a->q_flags |= QBADADDR;
24858151Seric 			usrerr("550 Cannot mail directly to :include:s");
2494174Seric 		}
2504174Seric 		else
25150556Seric 		{
25258247Seric 			int err;
25358247Seric 
25458151Seric 			message("including file %s", a->q_user);
25558247Seric 			err = include(a->q_user, FALSE, a, sendq, e);
25658247Seric 			if (transienterror(err))
25758247Seric 				a->q_flags |= QQUEUEUP|QDONTSEND;
25850556Seric 		}
2594174Seric 	}
26057642Seric 	else if (m == FileMailer)
2614174Seric 	{
2624329Seric 		struct stat stb;
2634329Seric 		extern bool writable();
2644174Seric 
26556795Seric 		p = strrchr(buf, '/');
26651317Seric 		/* check if writable or creatable */
26757731Seric 		if (a->q_alias == NULL && !QueueRun)
2684174Seric 		{
26951317Seric 			a->q_flags |= QDONTSEND|QBADADDR;
27058151Seric 			usrerr("550 Cannot mail directly to files");
2714174Seric 		}
27251317Seric 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
27358247Seric 		    (*p = '\0', safefile(buf, getruid(), S_IWRITE|S_IEXEC) != 0))
27451317Seric 		{
27551317Seric 			a->q_flags |= QBADADDR;
27655012Seric 			giveresponse(EX_CANTCREAT, m, e);
27751317Seric 		}
27851317Seric 	}
27951317Seric 
28057402Seric 	if (m != LocalMailer)
28157642Seric 	{
28257642Seric 		if (!bitset(QDONTSEND, a->q_flags))
28357642Seric 			e->e_nrcpts++;
28457402Seric 		return (a);
28557642Seric 	}
28657402Seric 
28757402Seric 	/* try aliasing */
28857402Seric 	alias(a, sendq, e);
28957402Seric 
29057402Seric # ifdef USERDB
29157402Seric 	/* if not aliased, look it up in the user database */
29257402Seric 	if (!bitset(QDONTSEND|QNOTREMOTE, a->q_flags))
29357402Seric 	{
29457402Seric 		extern int udbexpand();
29557402Seric 
29657402Seric 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
29757402Seric 		{
29858247Seric 			a->q_flags |= QQUEUEUP|QDONTSEND;
29957402Seric 			if (e->e_message == NULL)
30057402Seric 				e->e_message = newstr("Deferred: user database error");
30157402Seric # ifdef LOG
30258020Seric 			if (LogLevel > 8)
30357402Seric 				syslog(LOG_INFO, "%s: deferred: udbexpand",
30457402Seric 					e->e_id);
30557402Seric # endif
30658151Seric 			message("queued (user database error)");
30757642Seric 			e->e_nrcpts++;
30857402Seric 			return (a);
30957402Seric 		}
31057402Seric 	}
31157402Seric # endif
31257402Seric 
31357402Seric 	/* if it was an alias or a UDB expansion, just return now */
31458247Seric 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
31557402Seric 		return (a);
31657402Seric 
31751317Seric 	/*
31851317Seric 	**  If we have a level two config file, then pass the name through
31951317Seric 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
32051317Seric 	**  to send rewrite it to another mailer.  This gives us a hook
32151317Seric 	**  after local aliasing has been done.
32251317Seric 	*/
32351317Seric 
32451317Seric 	if (tTd(29, 5))
32551317Seric 	{
32651317Seric 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
32751317Seric 			ConfigLevel, RewriteRules[5]);
32851317Seric 		printaddr(a, FALSE);
32951317Seric 	}
33051317Seric 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
33151317Seric 	    RewriteRules[5] != NULL)
33251317Seric 	{
33355012Seric 		maplocaluser(a, sendq, e);
33451317Seric 	}
33551317Seric 
33651317Seric 	/*
33751317Seric 	**  If it didn't get rewritten to another mailer, go ahead
33851317Seric 	**  and deliver it.
33951317Seric 	*/
34051317Seric 
34158247Seric 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
34251317Seric 	{
34355354Seric 		auto bool fuzzy;
34451317Seric 		register struct passwd *pw;
34551317Seric 		extern struct passwd *finduser();
34651317Seric 
34751317Seric 		/* warning -- finduser may trash buf */
34855354Seric 		pw = finduser(buf, &fuzzy);
34951317Seric 		if (pw == NULL)
35051317Seric 		{
35151317Seric 			a->q_flags |= QBADADDR;
35255012Seric 			giveresponse(EX_NOUSER, m, e);
35351317Seric 		}
3544174Seric 		else
3554174Seric 		{
35651317Seric 			char nbuf[MAXNAME];
3574373Seric 
35855354Seric 			if (fuzzy)
3594174Seric 			{
36053735Seric 				/* name was a fuzzy match */
36151317Seric 				a->q_user = newstr(pw->pw_name);
36253735Seric 				if (findusercount++ > 3)
36353735Seric 				{
36458151Seric 					usrerr("554 aliasing/forwarding loop for %s broken",
36553735Seric 						pw->pw_name);
36653735Seric 					return (a);
36753735Seric 				}
36853735Seric 
36953735Seric 				/* see if it aliases */
37051317Seric 				(void) strcpy(buf, pw->pw_name);
37153735Seric 				goto trylocaluser;
3724174Seric 			}
37351317Seric 			a->q_home = newstr(pw->pw_dir);
37451317Seric 			a->q_uid = pw->pw_uid;
37551317Seric 			a->q_gid = pw->pw_gid;
37651317Seric 			a->q_flags |= QGOODUID;
37751317Seric 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
37851317Seric 			if (nbuf[0] != '\0')
37951317Seric 				a->q_fullname = newstr(nbuf);
38051317Seric 			if (!quoted)
38155012Seric 				forward(a, sendq, e);
3824174Seric 		}
3834174Seric 	}
38457642Seric 	if (!bitset(QDONTSEND, a->q_flags))
38557642Seric 		e->e_nrcpts++;
38612613Seric 	return (a);
3874174Seric }
3884174Seric /*
3894373Seric **  FINDUSER -- find the password entry for a user.
3904373Seric **
3914373Seric **	This looks a lot like getpwnam, except that it may want to
3924373Seric **	do some fancier pattern matching in /etc/passwd.
3934373Seric **
3949379Seric **	This routine contains most of the time of many sendmail runs.
3959379Seric **	It deserves to be optimized.
3969379Seric **
3974373Seric **	Parameters:
3984373Seric **		name -- the name to match against.
39955354Seric **		fuzzyp -- an outarg that is set to TRUE if this entry
40055354Seric **			was found using the fuzzy matching algorithm;
40155354Seric **			set to FALSE otherwise.
4024373Seric **
4034373Seric **	Returns:
4044373Seric **		A pointer to a pw struct.
4054373Seric **		NULL if name is unknown or ambiguous.
4064373Seric **
4074373Seric **	Side Effects:
4084407Seric **		may modify name.
4094373Seric */
4104373Seric 
4114373Seric struct passwd *
41255354Seric finduser(name, fuzzyp)
4134373Seric 	char *name;
41455354Seric 	bool *fuzzyp;
4154373Seric {
4164376Seric 	register struct passwd *pw;
4174407Seric 	register char *p;
41815325Seric 	extern struct passwd *getpwent();
41915325Seric 	extern struct passwd *getpwnam();
4204373Seric 
42155354Seric 	if (tTd(29, 4))
42255354Seric 		printf("finduser(%s): ", name);
42355354Seric 
42425777Seric 	/* map upper => lower case */
4254407Seric 	for (p = name; *p != '\0'; p++)
4264407Seric 	{
42725777Seric 		if (isascii(*p) && isupper(*p))
42825568Seric 			*p = tolower(*p);
4294407Seric 	}
43055354Seric 	*fuzzyp = FALSE;
4314407Seric 
43225777Seric 	/* look up this login name using fast path */
43312634Seric 	if ((pw = getpwnam(name)) != NULL)
43455354Seric 	{
43555354Seric 		if (tTd(29, 4))
43655354Seric 			printf("found (non-fuzzy)\n");
43712634Seric 		return (pw);
43855354Seric 	}
43912634Seric 
44053735Seric #ifdef MATCHGECOS
44153735Seric 	/* see if fuzzy matching allowed */
44253735Seric 	if (!MatchGecos)
44355354Seric 	{
44455354Seric 		if (tTd(29, 4))
44555354Seric 			printf("not found (fuzzy disabled)\n");
44653735Seric 		return NULL;
44755354Seric 	}
44853735Seric 
44912634Seric 	/* search for a matching full name instead */
45025777Seric 	for (p = name; *p != '\0'; p++)
45125777Seric 	{
45225777Seric 		if (*p == (SpaceSub & 0177) || *p == '_')
45325777Seric 			*p = ' ';
45425777Seric 	}
45523107Seric 	(void) setpwent();
4564376Seric 	while ((pw = getpwent()) != NULL)
4574376Seric 	{
4584998Seric 		char buf[MAXNAME];
4594376Seric 
4604998Seric 		buildfname(pw->pw_gecos, pw->pw_name, buf);
46156795Seric 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
4624381Seric 		{
46355354Seric 			if (tTd(29, 4))
46455354Seric 				printf("fuzzy matches %s\n", pw->pw_name);
46558151Seric 			message("sending to login name %s", pw->pw_name);
46655354Seric 			*fuzzyp = TRUE;
4674376Seric 			return (pw);
4684377Seric 		}
4694376Seric 	}
47053735Seric #endif
47155354Seric 	if (tTd(29, 4))
47255354Seric 		printf("no fuzzy match found\n");
4734376Seric 	return (NULL);
4744373Seric }
4754373Seric /*
4764329Seric **  WRITABLE -- predicate returning if the file is writable.
4774329Seric **
4784329Seric **	This routine must duplicate the algorithm in sys/fio.c.
4794329Seric **	Unfortunately, we cannot use the access call since we
4804329Seric **	won't necessarily be the real uid when we try to
4814329Seric **	actually open the file.
4824329Seric **
4834329Seric **	Notice that ANY file with ANY execute bit is automatically
4844329Seric **	not writable.  This is also enforced by mailfile.
4854329Seric **
4864329Seric **	Parameters:
4874329Seric **		s -- pointer to a stat struct for the file.
4884329Seric **
4894329Seric **	Returns:
4904329Seric **		TRUE -- if we will be able to write this file.
4914329Seric **		FALSE -- if we cannot write this file.
4924329Seric **
4934329Seric **	Side Effects:
4944329Seric **		none.
4954329Seric */
4964329Seric 
4974329Seric bool
4984329Seric writable(s)
4994329Seric 	register struct stat *s;
5004329Seric {
50155372Seric 	uid_t euid;
50255372Seric 	gid_t egid;
5034329Seric 	int bits;
5044329Seric 
5054329Seric 	if (bitset(0111, s->st_mode))
5064329Seric 		return (FALSE);
5074329Seric 	euid = getruid();
5084329Seric 	egid = getrgid();
5094329Seric 	if (geteuid() == 0)
5104329Seric 	{
5114329Seric 		if (bitset(S_ISUID, s->st_mode))
5124329Seric 			euid = s->st_uid;
5134329Seric 		if (bitset(S_ISGID, s->st_mode))
5144329Seric 			egid = s->st_gid;
5154329Seric 	}
5164329Seric 
5174329Seric 	if (euid == 0)
5184329Seric 		return (TRUE);
5194329Seric 	bits = S_IWRITE;
5204329Seric 	if (euid != s->st_uid)
5214329Seric 	{
5224329Seric 		bits >>= 3;
5234329Seric 		if (egid != s->st_gid)
5244329Seric 			bits >>= 3;
5254329Seric 	}
5264329Seric 	return ((s->st_mode & bits) != 0);
5274329Seric }
5284329Seric /*
5294174Seric **  INCLUDE -- handle :include: specification.
5304174Seric **
5314174Seric **	Parameters:
5324174Seric **		fname -- filename to include.
53353037Seric **		forwarding -- if TRUE, we are reading a .forward file.
53453037Seric **			if FALSE, it's a :include: file.
5354399Seric **		ctladdr -- address template to use to fill in these
5364399Seric **			addresses -- effective user/group id are
5374399Seric **			the important things.
5385006Seric **		sendq -- a pointer to the head of the send queue
5395006Seric **			to put these addresses in.
5404174Seric **
5414174Seric **	Returns:
54257136Seric **		open error status
5434174Seric **
5444174Seric **	Side Effects:
5454174Seric **		reads the :include: file and sends to everyone
5464174Seric **		listed in that file.
5474174Seric */
5484174Seric 
54953037Seric static jmp_buf	CtxIncludeTimeout;
55053037Seric 
55157136Seric int
55255012Seric include(fname, forwarding, ctladdr, sendq, e)
5534174Seric 	char *fname;
55453037Seric 	bool forwarding;
5554399Seric 	ADDRESS *ctladdr;
5565006Seric 	ADDRESS **sendq;
55755012Seric 	ENVELOPE *e;
5584174Seric {
5594174Seric 	register FILE *fp;
56055012Seric 	char *oldto = e->e_to;
5619379Seric 	char *oldfilename = FileName;
5629379Seric 	int oldlinenumber = LineNumber;
56353037Seric 	register EVENT *ev = NULL;
56458082Seric 	int nincludes;
56558247Seric 	int ret;
56653037Seric 	char buf[MAXLINE];
56753037Seric 	static int includetimeout();
5684174Seric 
56957186Seric 	if (tTd(27, 2))
57057186Seric 		printf("include(%s)\n", fname);
57157186Seric 
57253037Seric 	/*
57353037Seric 	**  If home directory is remote mounted but server is down,
57453037Seric 	**  this can hang or give errors; use a timeout to avoid this
57553037Seric 	*/
57653037Seric 
57753037Seric 	if (setjmp(CtxIncludeTimeout) != 0)
57853037Seric 	{
57953037Seric 		ctladdr->q_flags |= QQUEUEUP|QDONTSEND;
58053037Seric 		errno = 0;
58153037Seric 		usrerr("451 open timeout on %s", fname);
58257136Seric 		return ETIMEDOUT;
58353037Seric 	}
58453037Seric 	ev = setevent((time_t) 60, includetimeout, 0);
58553037Seric 
58653037Seric 	/* if forwarding, the input file must be marked safe */
58758247Seric 	if (forwarding && (ret = safefile(fname, ctladdr->q_uid, S_IREAD)) != 0)
58853037Seric 	{
58953037Seric 		/* don't use this .forward file */
59053037Seric 		clrevent(ev);
59157186Seric 		if (tTd(27, 4))
59258247Seric 			printf("include: not safe (uid=%d): %s\n",
59358247Seric 				ctladdr->q_uid, errstring(ret));
59458247Seric 		return ret;
59553037Seric 	}
59653037Seric 
5974174Seric 	fp = fopen(fname, "r");
5984174Seric 	if (fp == NULL)
5994174Seric 	{
60057136Seric 		int ret = errno;
60157136Seric 
60258061Seric 		clrevent(ev);
60358151Seric 		usrerr("550 Cannot open %s", fname);
60457136Seric 		return ret;
6054174Seric 	}
60653037Seric 
6074406Seric 	if (getctladdr(ctladdr) == NULL)
6084406Seric 	{
6094406Seric 		struct stat st;
6104174Seric 
6114406Seric 		if (fstat(fileno(fp), &st) < 0)
61258061Seric 		{
61358061Seric 			int ret = errno;
61458061Seric 
61558061Seric 			clrevent(ev);
6164406Seric 			syserr("Cannot fstat %s!", fname);
61758061Seric 			return ret;
61858061Seric 		}
6194406Seric 		ctladdr->q_uid = st.st_uid;
6204406Seric 		ctladdr->q_gid = st.st_gid;
6214406Seric 		ctladdr->q_flags |= QGOODUID;
6224406Seric 	}
6234406Seric 
62453037Seric 	clrevent(ev);
62553037Seric 
62658092Seric 	if (bitset(EF_VRFYONLY, e->e_flags))
62758092Seric 	{
62858092Seric 		/* don't do any more now */
62958092Seric 		fclose(fp);
63058092Seric 		return 0;
63158092Seric 	}
63258092Seric 
6334174Seric 	/* read the file -- each line is a comma-separated list. */
6349379Seric 	FileName = fname;
6359379Seric 	LineNumber = 0;
63658082Seric 	ctladdr->q_flags &= ~QSELFREF;
63758082Seric 	nincludes = 0;
6384174Seric 	while (fgets(buf, sizeof buf, fp) != NULL)
6394174Seric 	{
64056795Seric 		register char *p = strchr(buf, '\n');
6414174Seric 
64240963Sbostic 		LineNumber++;
6434174Seric 		if (p != NULL)
6444174Seric 			*p = '\0';
64557186Seric 		if (buf[0] == '#' || buf[0] == '\0')
64657139Seric 			continue;
64758008Seric 		e->e_to = NULL;
64858151Seric 		message("%s to %s",
64953037Seric 			forwarding ? "forwarding" : "sending", buf);
65057977Seric #ifdef LOG
65158020Seric 		if (forwarding && LogLevel > 9)
65257977Seric 			syslog(LOG_INFO, "%s: forward %s => %s",
65357977Seric 				e->e_id, oldto, buf);
65457977Seric #endif
65557977Seric 
6564176Seric 		AliasLevel++;
65758082Seric 		nincludes += sendtolist(buf, ctladdr, sendq, e);
6584176Seric 		AliasLevel--;
6594174Seric 	}
66058082Seric 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
66158065Seric 	{
66258065Seric 		if (tTd(27, 5))
66358065Seric 		{
66458065Seric 			printf("include: QDONTSEND ");
66558065Seric 			printaddr(ctladdr, FALSE);
66658065Seric 		}
66758065Seric 		ctladdr->q_flags |= QDONTSEND;
66858065Seric 	}
6694174Seric 
6704319Seric 	(void) fclose(fp);
6719379Seric 	FileName = oldfilename;
6729379Seric 	LineNumber = oldlinenumber;
67357136Seric 	return 0;
6744174Seric }
67553037Seric 
67653037Seric static
67753037Seric includetimeout()
67853037Seric {
67953037Seric 	longjmp(CtxIncludeTimeout, 1);
68053037Seric }
6814324Seric /*
6824324Seric **  SENDTOARGV -- send to an argument vector.
6834324Seric **
6844324Seric **	Parameters:
6854324Seric **		argv -- argument vector to send to.
68658247Seric **		e -- the current envelope.
6874324Seric **
6884324Seric **	Returns:
6894324Seric **		none.
6904324Seric **
6914324Seric **	Side Effects:
6924324Seric **		puts all addresses on the argument vector onto the
6934324Seric **			send queue.
6944324Seric */
6954324Seric 
69655012Seric sendtoargv(argv, e)
6974324Seric 	register char **argv;
69855012Seric 	register ENVELOPE *e;
6994324Seric {
7004324Seric 	register char *p;
7014324Seric 
7024324Seric 	while ((p = *argv++) != NULL)
7034324Seric 	{
70433725Sbostic 		if (argv[0] != NULL && argv[1] != NULL && !strcasecmp(argv[0], "at"))
7054324Seric 		{
7064324Seric 			char nbuf[MAXNAME];
7074324Seric 
7084324Seric 			if (strlen(p) + strlen(argv[1]) + 2 > sizeof nbuf)
70958151Seric 				usrerr("554 address overflow");
7104324Seric 			else
7114324Seric 			{
7124324Seric 				(void) strcpy(nbuf, p);
7134324Seric 				(void) strcat(nbuf, "@");
7144324Seric 				(void) strcat(nbuf, argv[1]);
7154324Seric 				p = newstr(nbuf);
7164324Seric 				argv += 2;
7174324Seric 			}
7184324Seric 		}
71958082Seric 		(void) sendtolist(p, (ADDRESS *) NULL, &e->e_sendqueue, e);
7204324Seric 	}
7214324Seric }
7224399Seric /*
7234399Seric **  GETCTLADDR -- get controlling address from an address header.
7244399Seric **
7254399Seric **	If none, get one corresponding to the effective userid.
7264399Seric **
7274399Seric **	Parameters:
7284399Seric **		a -- the address to find the controller of.
7294399Seric **
7304399Seric **	Returns:
7314399Seric **		the controlling address.
7324399Seric **
7334399Seric **	Side Effects:
7344399Seric **		none.
7354399Seric */
7364399Seric 
7374399Seric ADDRESS *
7384399Seric getctladdr(a)
7394399Seric 	register ADDRESS *a;
7404399Seric {
7414404Seric 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
7424399Seric 		a = a->q_alias;
7434399Seric 	return (a);
7444399Seric }
745