1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)recipient.c	8.28 (Berkeley) 12/10/93";
11 #endif /* not lint */
12 
13 # include "sendmail.h"
14 # include <pwd.h>
15 
16 /*
17 **  SENDTOLIST -- Designate a send list.
18 **
19 **	The parameter is a comma-separated list of people to send to.
20 **	This routine arranges to send to all of them.
21 **
22 **	Parameters:
23 **		list -- the send list.
24 **		ctladdr -- the address template for the person to
25 **			send to -- effective uid/gid are important.
26 **			This is typically the alias that caused this
27 **			expansion.
28 **		sendq -- a pointer to the head of a queue to put
29 **			these people into.
30 **		e -- the envelope in which to add these recipients.
31 **
32 **	Returns:
33 **		The number of addresses actually on the list.
34 **
35 **	Side Effects:
36 **		none.
37 */
38 
39 # define MAXRCRSN	10
40 
41 sendtolist(list, ctladdr, sendq, e)
42 	char *list;
43 	ADDRESS *ctladdr;
44 	ADDRESS **sendq;
45 	register ENVELOPE *e;
46 {
47 	register char *p;
48 	register ADDRESS *al;	/* list of addresses to send to */
49 	bool firstone;		/* set on first address sent */
50 	char delimiter;		/* the address delimiter */
51 	int naddrs;
52 	char *oldto = e->e_to;
53 
54 	if (list == NULL)
55 	{
56 		syserr("sendtolist: null list");
57 		return 0;
58 	}
59 
60 	if (tTd(25, 1))
61 	{
62 		printf("sendto: %s\n   ctladdr=", list);
63 		printaddr(ctladdr, FALSE);
64 	}
65 
66 	/* heuristic to determine old versus new style addresses */
67 	if (ctladdr == NULL &&
68 	    (strchr(list, ',') != NULL || strchr(list, ';') != NULL ||
69 	     strchr(list, '<') != NULL || strchr(list, '(') != NULL))
70 		e->e_flags &= ~EF_OLDSTYLE;
71 	delimiter = ' ';
72 	if (!bitset(EF_OLDSTYLE, e->e_flags) || ctladdr != NULL)
73 		delimiter = ',';
74 
75 	firstone = TRUE;
76 	al = NULL;
77 	naddrs = 0;
78 
79 	for (p = list; *p != '\0'; )
80 	{
81 		auto char *delimptr;
82 		register ADDRESS *a;
83 
84 		/* parse the address */
85 		while ((isascii(*p) && isspace(*p)) || *p == ',')
86 			p++;
87 		a = parseaddr(p, NULLADDR, RF_COPYALL, delimiter, &delimptr, e);
88 		p = delimptr;
89 		if (a == NULL)
90 			continue;
91 		a->q_next = al;
92 		a->q_alias = ctladdr;
93 
94 		/* see if this should be marked as a primary address */
95 		if (ctladdr == NULL ||
96 		    (firstone && *p == '\0' && bitset(QPRIMARY, ctladdr->q_flags)))
97 			a->q_flags |= QPRIMARY;
98 
99 		if (ctladdr != NULL && sameaddr(ctladdr, a))
100 			ctladdr->q_flags |= QSELFREF;
101 		al = a;
102 		firstone = FALSE;
103 	}
104 
105 	/* arrange to send to everyone on the local send list */
106 	while (al != NULL)
107 	{
108 		register ADDRESS *a = al;
109 
110 		al = a->q_next;
111 		a = recipient(a, sendq, e);
112 
113 		/* arrange to inherit full name */
114 		if (a->q_fullname == NULL && ctladdr != NULL)
115 			a->q_fullname = ctladdr->q_fullname;
116 		naddrs++;
117 	}
118 
119 	e->e_to = oldto;
120 	return (naddrs);
121 }
122 /*
123 **  RECIPIENT -- Designate a message recipient
124 **
125 **	Saves the named person for future mailing.
126 **
127 **	Parameters:
128 **		a -- the (preparsed) address header for the recipient.
129 **		sendq -- a pointer to the head of a queue to put the
130 **			recipient in.  Duplicate supression is done
131 **			in this queue.
132 **		e -- the current envelope.
133 **
134 **	Returns:
135 **		The actual address in the queue.  This will be "a" if
136 **		the address is not a duplicate, else the original address.
137 **
138 **	Side Effects:
139 **		none.
140 */
141 
142 ADDRESS *
143 recipient(a, sendq, e)
144 	register ADDRESS *a;
145 	register ADDRESS **sendq;
146 	register ENVELOPE *e;
147 {
148 	register ADDRESS *q;
149 	ADDRESS **pq;
150 	register struct mailer *m;
151 	register char *p;
152 	bool quoted = FALSE;		/* set if the addr has a quote bit */
153 	int findusercount = 0;
154 	char buf[MAXNAME];		/* unquoted image of the user name */
155 	extern int safefile();
156 
157 	e->e_to = a->q_paddr;
158 	m = a->q_mailer;
159 	errno = 0;
160 	if (tTd(26, 1))
161 	{
162 		printf("\nrecipient: ");
163 		printaddr(a, FALSE);
164 	}
165 
166 	/* if this is primary, add it to the original recipient list */
167 	if (a->q_alias == NULL)
168 	{
169 		if (e->e_origrcpt == NULL)
170 			e->e_origrcpt = a->q_paddr;
171 		else if (e->e_origrcpt != a->q_paddr)
172 			e->e_origrcpt = "";
173 	}
174 
175 	/* break aliasing loops */
176 	if (AliasLevel > MAXRCRSN)
177 	{
178 		usrerr("554 aliasing/forwarding loop broken");
179 		return (a);
180 	}
181 
182 	/*
183 	**  Finish setting up address structure.
184 	*/
185 
186 	/* set the queue timeout */
187 	a->q_timeout = TimeOuts.to_q_return;
188 
189 	/* get unquoted user for file, program or user.name check */
190 	(void) strcpy(buf, a->q_user);
191 	for (p = buf; *p != '\0' && !quoted; p++)
192 	{
193 		if (*p == '\\')
194 			quoted = TRUE;
195 	}
196 	stripquotes(buf);
197 
198 	/* check for direct mailing to restricted mailers */
199 	if (a->q_alias == NULL && m == ProgMailer)
200 	{
201 		a->q_flags |= QBADADDR;
202 		usrerr("550 Cannot mail directly to programs");
203 	}
204 
205 	/*
206 	**  Look up this person in the recipient list.
207 	**	If they are there already, return, otherwise continue.
208 	**	If the list is empty, just add it.  Notice the cute
209 	**	hack to make from addresses suppress things correctly:
210 	**	the QDONTSEND bit will be set in the send list.
211 	**	[Please note: the emphasis is on "hack."]
212 	*/
213 
214 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
215 	{
216 		if (sameaddr(q, a))
217 		{
218 			if (tTd(26, 1))
219 			{
220 				printf("%s in sendq: ", a->q_paddr);
221 				printaddr(q, FALSE);
222 			}
223 			if (!bitset(QPRIMARY, q->q_flags))
224 			{
225 				if (!bitset(QDONTSEND, a->q_flags))
226 					message("duplicate suppressed");
227 				q->q_flags |= a->q_flags;
228 			}
229 			a = q;
230 			goto testselfdestruct;
231 		}
232 	}
233 
234 	/* add address on list */
235 	*pq = a;
236 	a->q_next = NULL;
237 
238 	/*
239 	**  Alias the name and handle special mailer types.
240 	*/
241 
242   trylocaluser:
243 	if (tTd(29, 7))
244 		printf("at trylocaluser %s\n", a->q_user);
245 
246 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
247 		goto testselfdestruct;
248 
249 	if (m == InclMailer)
250 	{
251 		a->q_flags |= QDONTSEND;
252 		if (a->q_alias == NULL)
253 		{
254 			a->q_flags |= QBADADDR;
255 			usrerr("550 Cannot mail directly to :include:s");
256 		}
257 		else
258 		{
259 			int ret;
260 
261 			message("including file %s", a->q_user);
262 			ret = include(a->q_user, FALSE, a, sendq, e);
263 			if (transienterror(ret))
264 			{
265 #ifdef LOG
266 				if (LogLevel > 2)
267 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
268 						e->e_id, a->q_user, errstring(ret));
269 #endif
270 				a->q_flags |= QQUEUEUP;
271 				usrerr("451 Cannot open %s: %s",
272 					a->q_user, errstring(ret));
273 			}
274 			else if (ret != 0)
275 			{
276 				a->q_flags |= QBADADDR;
277 				usrerr("550 Cannot open %s: %s",
278 					a->q_user, errstring(ret));
279 			}
280 		}
281 	}
282 	else if (m == FileMailer)
283 	{
284 		extern bool writable();
285 
286 		/* check if writable or creatable */
287 		if (a->q_alias == NULL)
288 		{
289 			a->q_flags |= QBADADDR;
290 			usrerr("550 Cannot mail directly to files");
291 		}
292 		else if (!writable(buf, SFF_ANYFILE))
293 		{
294 			a->q_flags |= QBADADDR;
295 			giveresponse(EX_CANTCREAT, m, NULL, a->q_alias, e);
296 		}
297 	}
298 
299 	if (m != LocalMailer)
300 	{
301 		if (!bitset(QDONTSEND, a->q_flags))
302 			e->e_nrcpts++;
303 		goto testselfdestruct;
304 	}
305 
306 	/* try aliasing */
307 	alias(a, sendq, e);
308 
309 # ifdef USERDB
310 	/* if not aliased, look it up in the user database */
311 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
312 	{
313 		extern int udbexpand();
314 
315 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
316 		{
317 			a->q_flags |= QQUEUEUP;
318 			if (e->e_message == NULL)
319 				e->e_message = newstr("Deferred: user database error");
320 # ifdef LOG
321 			if (LogLevel > 8)
322 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
323 					e->e_id, errstring(errno));
324 # endif
325 			message("queued (user database error): %s",
326 				errstring(errno));
327 			e->e_nrcpts++;
328 			goto testselfdestruct;
329 		}
330 	}
331 # endif
332 
333 	/* if it was an alias or a UDB expansion, just return now */
334 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
335 		goto testselfdestruct;
336 
337 	/*
338 	**  If we have a level two config file, then pass the name through
339 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
340 	**  to send rewrite it to another mailer.  This gives us a hook
341 	**  after local aliasing has been done.
342 	*/
343 
344 	if (tTd(29, 5))
345 	{
346 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
347 			ConfigLevel, RewriteRules[5]);
348 		printaddr(a, FALSE);
349 	}
350 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
351 	    RewriteRules[5] != NULL)
352 	{
353 		maplocaluser(a, sendq, e);
354 	}
355 
356 	/*
357 	**  If it didn't get rewritten to another mailer, go ahead
358 	**  and deliver it.
359 	*/
360 
361 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
362 	{
363 		auto bool fuzzy;
364 		register struct passwd *pw;
365 		extern struct passwd *finduser();
366 
367 		/* warning -- finduser may trash buf */
368 		pw = finduser(buf, &fuzzy);
369 		if (pw == NULL)
370 		{
371 			a->q_flags |= QBADADDR;
372 			giveresponse(EX_NOUSER, m, NULL, a->q_alias, e);
373 		}
374 		else
375 		{
376 			char nbuf[MAXNAME];
377 
378 			if (fuzzy)
379 			{
380 				/* name was a fuzzy match */
381 				a->q_user = newstr(pw->pw_name);
382 				if (findusercount++ > 3)
383 				{
384 					a->q_flags |= QBADADDR;
385 					usrerr("554 aliasing/forwarding loop for %s broken",
386 						pw->pw_name);
387 					return (a);
388 				}
389 
390 				/* see if it aliases */
391 				(void) strcpy(buf, pw->pw_name);
392 				goto trylocaluser;
393 			}
394 			a->q_home = newstr(pw->pw_dir);
395 			a->q_uid = pw->pw_uid;
396 			a->q_gid = pw->pw_gid;
397 			a->q_ruser = newstr(pw->pw_name);
398 			a->q_flags |= QGOODUID;
399 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
400 			if (nbuf[0] != '\0')
401 				a->q_fullname = newstr(nbuf);
402 			if (!quoted)
403 				forward(a, sendq, e);
404 		}
405 	}
406 	if (!bitset(QDONTSEND, a->q_flags))
407 		e->e_nrcpts++;
408 
409   testselfdestruct:
410 	if (tTd(26, 8))
411 	{
412 		printf("testselfdestruct: ");
413 		printaddr(a, TRUE);
414 	}
415 	if (a->q_alias == NULL && a != &e->e_from &&
416 	    bitset(QDONTSEND, a->q_flags))
417 	{
418 		q = *sendq;
419 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
420 			q = q->q_next;
421 		if (q == NULL)
422 		{
423 			a->q_flags |= QBADADDR;
424 			usrerr("554 aliasing/forwarding loop broken");
425 		}
426 	}
427 	return (a);
428 }
429 /*
430 **  FINDUSER -- find the password entry for a user.
431 **
432 **	This looks a lot like getpwnam, except that it may want to
433 **	do some fancier pattern matching in /etc/passwd.
434 **
435 **	This routine contains most of the time of many sendmail runs.
436 **	It deserves to be optimized.
437 **
438 **	Parameters:
439 **		name -- the name to match against.
440 **		fuzzyp -- an outarg that is set to TRUE if this entry
441 **			was found using the fuzzy matching algorithm;
442 **			set to FALSE otherwise.
443 **
444 **	Returns:
445 **		A pointer to a pw struct.
446 **		NULL if name is unknown or ambiguous.
447 **
448 **	Side Effects:
449 **		may modify name.
450 */
451 
452 struct passwd *
453 finduser(name, fuzzyp)
454 	char *name;
455 	bool *fuzzyp;
456 {
457 	register struct passwd *pw;
458 	register char *p;
459 	extern struct passwd *getpwent();
460 	extern struct passwd *getpwnam();
461 
462 	if (tTd(29, 4))
463 		printf("finduser(%s): ", name);
464 
465 	*fuzzyp = FALSE;
466 
467 	/* DEC Hesiod getpwnam accepts numeric strings -- short circuit it */
468 	for (p = name; *p != '\0'; p++)
469 		if (!isascii(*p) || !isdigit(*p))
470 			break;
471 	if (*p == '\0')
472 	{
473 		if (tTd(29, 4))
474 			printf("failed (numeric input)\n");
475 		return NULL;
476 	}
477 
478 	/* look up this login name using fast path */
479 	if ((pw = getpwnam(name)) != NULL)
480 	{
481 		if (tTd(29, 4))
482 			printf("found (non-fuzzy)\n");
483 		return (pw);
484 	}
485 
486 #ifdef MATCHGECOS
487 	/* see if fuzzy matching allowed */
488 	if (!MatchGecos)
489 	{
490 		if (tTd(29, 4))
491 			printf("not found (fuzzy disabled)\n");
492 		return NULL;
493 	}
494 
495 	/* search for a matching full name instead */
496 	for (p = name; *p != '\0'; p++)
497 	{
498 		if (*p == (SpaceSub & 0177) || *p == '_')
499 			*p = ' ';
500 	}
501 	(void) setpwent();
502 	while ((pw = getpwent()) != NULL)
503 	{
504 		char buf[MAXNAME];
505 
506 		buildfname(pw->pw_gecos, pw->pw_name, buf);
507 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
508 		{
509 			if (tTd(29, 4))
510 				printf("fuzzy matches %s\n", pw->pw_name);
511 			message("sending to login name %s", pw->pw_name);
512 			*fuzzyp = TRUE;
513 			return (pw);
514 		}
515 	}
516 	if (tTd(29, 4))
517 		printf("no fuzzy match found\n");
518 #else
519 	if (tTd(29, 4))
520 		printf("not found (fuzzy disabled)\n");
521 #endif
522 	return (NULL);
523 }
524 /*
525 **  WRITABLE -- predicate returning if the file is writable.
526 **
527 **	This routine must duplicate the algorithm in sys/fio.c.
528 **	Unfortunately, we cannot use the access call since we
529 **	won't necessarily be the real uid when we try to
530 **	actually open the file.
531 **
532 **	Notice that ANY file with ANY execute bit is automatically
533 **	not writable.  This is also enforced by mailfile.
534 **
535 **	Parameters:
536 **		filename -- the file name to check.
537 **		flags -- SFF_* flags to control the function.
538 **
539 **	Returns:
540 **		TRUE -- if we will be able to write this file.
541 **		FALSE -- if we cannot write this file.
542 **
543 **	Side Effects:
544 **		none.
545 */
546 
547 bool
548 writable(filename, flags)
549 	char *filename;
550 	int flags;
551 {
552 	uid_t euid;
553 	gid_t egid;
554 	int bits;
555 	register char *p;
556 	char *uname;
557 	struct stat stb;
558 	extern char RealUserName[];
559 
560 	if (tTd(29, 5))
561 		printf("writable(%s, %x)\n", filename, flags);
562 
563 #ifdef HASLSTAT
564 	if ((bitset(SFF_NOSLINK, flags) ? lstat(filename, &stb)
565 					: stat(filename, &stb)) < 0)
566 #else
567 	if (stat(filename, &stb) < 0)
568 #endif
569 	{
570 		/* file does not exist -- see if directory is safe */
571 		p = strrchr(filename, '/');
572 		if (p == NULL)
573 		{
574 			errno = ENOTDIR;
575 			return FALSE;
576 		}
577 		*p = '\0';
578 		errno = safefile(filename, RealUid, RealGid, RealUserName,
579 				 SFF_MUSTOWN, S_IWRITE|S_IEXEC);
580 		*p = '/';
581 		return errno == 0;
582 	}
583 
584 	/*
585 	**  File does exist -- check that it is writable.
586 	*/
587 
588 	if (bitset(0111, stb.st_mode))
589 	{
590 		if (tTd(29, 5))
591 			printf("failed (mode %o: x bits)\n", stb.st_mode);
592 		errno = EPERM;
593 		return (FALSE);
594 	}
595 
596 	euid = RealUid;
597 	uname = RealUserName;
598 	if (euid == 0)
599 	{
600 		euid = DefUid;
601 		uname = DefUser;
602 	}
603 	egid = RealGid;
604 	if (egid == 0)
605 		egid = DefGid;
606 	if (geteuid() == 0)
607 	{
608 		if (bitset(S_ISUID, stb.st_mode))
609 		{
610 			euid = stb.st_uid;
611 			uname = NULL;
612 		}
613 		if (bitset(S_ISGID, stb.st_mode))
614 			egid = stb.st_gid;
615 	}
616 
617 	if (tTd(29, 5))
618 		printf("\teu/gid=%d/%d, st_u/gid=%d/%d\n",
619 			euid, egid, stb.st_uid, stb.st_gid);
620 
621 	errno = safefile(filename, euid, egid, uname, flags, S_IWRITE);
622 	return errno == 0;
623 }
624 /*
625 **  INCLUDE -- handle :include: specification.
626 **
627 **	Parameters:
628 **		fname -- filename to include.
629 **		forwarding -- if TRUE, we are reading a .forward file.
630 **			if FALSE, it's a :include: file.
631 **		ctladdr -- address template to use to fill in these
632 **			addresses -- effective user/group id are
633 **			the important things.
634 **		sendq -- a pointer to the head of the send queue
635 **			to put these addresses in.
636 **
637 **	Returns:
638 **		open error status
639 **
640 **	Side Effects:
641 **		reads the :include: file and sends to everyone
642 **		listed in that file.
643 */
644 
645 static jmp_buf	CtxIncludeTimeout;
646 static int	includetimeout();
647 
648 int
649 include(fname, forwarding, ctladdr, sendq, e)
650 	char *fname;
651 	bool forwarding;
652 	ADDRESS *ctladdr;
653 	ADDRESS **sendq;
654 	ENVELOPE *e;
655 {
656 	register FILE *fp = NULL;
657 	char *oldto = e->e_to;
658 	char *oldfilename = FileName;
659 	int oldlinenumber = LineNumber;
660 	register EVENT *ev = NULL;
661 	int nincludes;
662 	register ADDRESS *ca;
663 	uid_t saveduid, uid;
664 	gid_t savedgid, gid;
665 	char *uname;
666 	int rval = 0;
667 	int sfflags = forwarding ? SFF_MUSTOWN : SFF_ANYFILE;
668 	char buf[MAXLINE];
669 
670 	if (tTd(27, 2))
671 		printf("include(%s)\n", fname);
672 	if (tTd(27, 4))
673 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
674 	if (tTd(27, 14))
675 	{
676 		printf("ctladdr ");
677 		printaddr(ctladdr, FALSE);
678 	}
679 
680 	if (tTd(27, 9))
681 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
682 
683 	ca = getctladdr(ctladdr);
684 	if (ca == NULL)
685 	{
686 		uid = DefUid;
687 		gid = DefGid;
688 		uname = DefUser;
689 		saveduid = -1;
690 	}
691 	else
692 	{
693 		uid = ca->q_uid;
694 		gid = ca->q_gid;
695 		uname = ca->q_user;
696 #ifdef HASSETREUID
697 		saveduid = geteuid();
698 		savedgid = getegid();
699 		if (saveduid == 0)
700 		{
701 			initgroups(uname, gid);
702 			if (uid != 0)
703 				(void) setreuid(0, uid);
704 		}
705 #endif
706 	}
707 
708 	if (tTd(27, 9))
709 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
710 
711 	/*
712 	**  If home directory is remote mounted but server is down,
713 	**  this can hang or give errors; use a timeout to avoid this
714 	*/
715 
716 	if (setjmp(CtxIncludeTimeout) != 0)
717 	{
718 		ctladdr->q_flags |= QQUEUEUP;
719 		errno = 0;
720 		usrerr("451 open timeout on %s", fname);
721 
722 		/* return pseudo-error code */
723 		rval = EOPENTIMEOUT;
724 		goto resetuid;
725 	}
726 	ev = setevent((time_t) 60, includetimeout, 0);
727 
728 	/* the input file must be marked safe */
729 	rval = safefile(fname, uid, gid, uname, sfflags, S_IREAD);
730 	if (rval != 0)
731 	{
732 		/* don't use this :include: file */
733 		clrevent(ev);
734 		if (tTd(27, 4))
735 			printf("include: not safe (uid=%d): %s\n",
736 				uid, errstring(rval));
737 		goto resetuid;
738 	}
739 
740 	fp = fopen(fname, "r");
741 	if (fp == NULL)
742 	{
743 		rval = errno;
744 		if (tTd(27, 4))
745 			printf("include: open: %s\n", errstring(rval));
746 	}
747 	else if (ca == NULL)
748 	{
749 		struct stat st;
750 
751 		if (fstat(fileno(fp), &st) < 0)
752 		{
753 			rval = errno;
754 			syserr("Cannot fstat %s!", fname);
755 		}
756 		else
757 		{
758 			ctladdr->q_uid = st.st_uid;
759 			ctladdr->q_gid = st.st_gid;
760 			ctladdr->q_flags |= QGOODUID;
761 		}
762 	}
763 
764 	clrevent(ev);
765 
766 resetuid:
767 
768 #ifdef HASSETREUID
769 	if (saveduid == 0)
770 	{
771 		if (uid != 0)
772 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
773 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
774 					RealUid, getuid(), geteuid());
775 		setgid(savedgid);
776 	}
777 #endif
778 
779 	if (tTd(27, 9))
780 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
781 
782 	if (fp == NULL)
783 		return rval;
784 
785 	if (bitset(EF_VRFYONLY, e->e_flags))
786 	{
787 		/* don't do any more now */
788 		ctladdr->q_flags |= QVERIFIED;
789 		e->e_nrcpts++;
790 		xfclose(fp, "include", fname);
791 		return rval;
792 	}
793 
794 	/* read the file -- each line is a comma-separated list. */
795 	FileName = fname;
796 	LineNumber = 0;
797 	ctladdr->q_flags &= ~QSELFREF;
798 	nincludes = 0;
799 	while (fgets(buf, sizeof buf, fp) != NULL)
800 	{
801 		register char *p = strchr(buf, '\n');
802 
803 		LineNumber++;
804 		if (p != NULL)
805 			*p = '\0';
806 		if (buf[0] == '#' || buf[0] == '\0')
807 			continue;
808 		e->e_to = NULL;
809 		message("%s to %s",
810 			forwarding ? "forwarding" : "sending", buf);
811 #ifdef LOG
812 		if (forwarding && LogLevel > 9)
813 			syslog(LOG_INFO, "%s: forward %s => %s",
814 				e->e_id, oldto, buf);
815 #endif
816 
817 		AliasLevel++;
818 		nincludes += sendtolist(buf, ctladdr, sendq, e);
819 		AliasLevel--;
820 	}
821 
822 	if (ferror(fp) && tTd(27, 3))
823 		printf("include: read error: %s\n", errstring(errno));
824 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
825 	{
826 		if (tTd(27, 5))
827 		{
828 			printf("include: QDONTSEND ");
829 			printaddr(ctladdr, FALSE);
830 		}
831 		ctladdr->q_flags |= QDONTSEND;
832 	}
833 
834 	(void) xfclose(fp, "include", fname);
835 	FileName = oldfilename;
836 	LineNumber = oldlinenumber;
837 	e->e_to = oldto;
838 	return rval;
839 }
840 
841 static
842 includetimeout()
843 {
844 	longjmp(CtxIncludeTimeout, 1);
845 }
846 /*
847 **  SENDTOARGV -- send to an argument vector.
848 **
849 **	Parameters:
850 **		argv -- argument vector to send to.
851 **		e -- the current envelope.
852 **
853 **	Returns:
854 **		none.
855 **
856 **	Side Effects:
857 **		puts all addresses on the argument vector onto the
858 **			send queue.
859 */
860 
861 sendtoargv(argv, e)
862 	register char **argv;
863 	register ENVELOPE *e;
864 {
865 	register char *p;
866 
867 	while ((p = *argv++) != NULL)
868 	{
869 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
870 	}
871 }
872 /*
873 **  GETCTLADDR -- get controlling address from an address header.
874 **
875 **	If none, get one corresponding to the effective userid.
876 **
877 **	Parameters:
878 **		a -- the address to find the controller of.
879 **
880 **	Returns:
881 **		the controlling address.
882 **
883 **	Side Effects:
884 **		none.
885 */
886 
887 ADDRESS *
888 getctladdr(a)
889 	register ADDRESS *a;
890 {
891 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
892 		a = a->q_alias;
893 	return (a);
894 }
895