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.17 (Berkeley) 08/21/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 	    !bitset(EF_QUEUERUN, e->e_flags))
201 	{
202 		a->q_flags |= QBADADDR;
203 		usrerr("550 Cannot mail directly to programs");
204 	}
205 
206 	/*
207 	**  Look up this person in the recipient list.
208 	**	If they are there already, return, otherwise continue.
209 	**	If the list is empty, just add it.  Notice the cute
210 	**	hack to make from addresses suppress things correctly:
211 	**	the QDONTSEND bit will be set in the send list.
212 	**	[Please note: the emphasis is on "hack."]
213 	*/
214 
215 	for (pq = sendq; (q = *pq) != NULL; pq = &q->q_next)
216 	{
217 		if (sameaddr(q, a))
218 		{
219 			if (tTd(26, 1))
220 			{
221 				printf("%s in sendq: ", a->q_paddr);
222 				printaddr(q, FALSE);
223 			}
224 			if (!bitset(QPRIMARY, q->q_flags))
225 			{
226 				if (!bitset(QDONTSEND, a->q_flags))
227 					message("duplicate suppressed");
228 				q->q_flags |= a->q_flags;
229 			}
230 			a = q;
231 			goto testselfdestruct;
232 		}
233 	}
234 
235 	/* add address on list */
236 	*pq = a;
237 	a->q_next = NULL;
238 
239 	/*
240 	**  Alias the name and handle special mailer types.
241 	*/
242 
243   trylocaluser:
244 	if (tTd(29, 7))
245 		printf("at trylocaluser %s\n", a->q_user);
246 
247 	if (bitset(QDONTSEND|QBADADDR|QVERIFIED, a->q_flags))
248 		goto testselfdestruct;
249 
250 	if (m == InclMailer)
251 	{
252 		a->q_flags |= QDONTSEND;
253 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
254 		{
255 			a->q_flags |= QBADADDR;
256 			usrerr("550 Cannot mail directly to :include:s");
257 		}
258 		else
259 		{
260 			int ret;
261 
262 			message("including file %s", a->q_user);
263 			ret = include(a->q_user, FALSE, a, sendq, e);
264 			if (transienterror(ret))
265 			{
266 #ifdef LOG
267 				if (LogLevel > 2)
268 					syslog(LOG_ERR, "%s: include %s: transient error: %e",
269 						e->e_id, a->q_user, errstring(ret));
270 #endif
271 				a->q_flags |= QQUEUEUP;
272 				usrerr("451 Cannot open %s: %s",
273 					a->q_user, errstring(ret));
274 			}
275 			else if (ret != 0)
276 			{
277 				a->q_flags |= QBADADDR;
278 				usrerr("550 Cannot open %s: %s",
279 					a->q_user, errstring(ret));
280 			}
281 		}
282 	}
283 	else if (m == FileMailer)
284 	{
285 		struct stat stb;
286 		extern bool writable();
287 
288 		p = strrchr(buf, '/');
289 		/* check if writable or creatable */
290 		if (a->q_alias == NULL && !bitset(EF_QUEUERUN, e->e_flags))
291 		{
292 			a->q_flags |= QBADADDR;
293 			usrerr("550 Cannot mail directly to files");
294 		}
295 		else if ((stat(buf, &stb) >= 0) ? (!writable(&stb)) :
296 		    (*p = '\0', safefile(buf, RealUid, RealGid, NULL, TRUE, S_IWRITE|S_IEXEC) != 0))
297 		{
298 			a->q_flags |= QBADADDR;
299 			giveresponse(EX_CANTCREAT, m, NULL, e);
300 		}
301 	}
302 
303 	if (m != LocalMailer)
304 	{
305 		if (!bitset(QDONTSEND, a->q_flags))
306 			e->e_nrcpts++;
307 		goto testselfdestruct;
308 	}
309 
310 	/* try aliasing */
311 	alias(a, sendq, e);
312 
313 # ifdef USERDB
314 	/* if not aliased, look it up in the user database */
315 	if (!bitset(QDONTSEND|QNOTREMOTE|QVERIFIED, a->q_flags))
316 	{
317 		extern int udbexpand();
318 		extern int errno;
319 
320 		if (udbexpand(a, sendq, e) == EX_TEMPFAIL)
321 		{
322 			a->q_flags |= QQUEUEUP;
323 			if (e->e_message == NULL)
324 				e->e_message = newstr("Deferred: user database error");
325 # ifdef LOG
326 			if (LogLevel > 8)
327 				syslog(LOG_INFO, "%s: deferred: udbexpand: %s",
328 					e->e_id, errstring(errno));
329 # endif
330 			message("queued (user database error): %s",
331 				errstring(errno));
332 			e->e_nrcpts++;
333 			goto testselfdestruct;
334 		}
335 	}
336 # endif
337 
338 	/* if it was an alias or a UDB expansion, just return now */
339 	if (bitset(QDONTSEND|QQUEUEUP|QVERIFIED, a->q_flags))
340 		goto testselfdestruct;
341 
342 	/*
343 	**  If we have a level two config file, then pass the name through
344 	**  Ruleset 5 before sending it off.  Ruleset 5 has the right
345 	**  to send rewrite it to another mailer.  This gives us a hook
346 	**  after local aliasing has been done.
347 	*/
348 
349 	if (tTd(29, 5))
350 	{
351 		printf("recipient: testing local?  cl=%d, rr5=%x\n\t",
352 			ConfigLevel, RewriteRules[5]);
353 		printaddr(a, FALSE);
354 	}
355 	if (!bitset(QNOTREMOTE, a->q_flags) && ConfigLevel >= 2 &&
356 	    RewriteRules[5] != NULL)
357 	{
358 		maplocaluser(a, sendq, e);
359 	}
360 
361 	/*
362 	**  If it didn't get rewritten to another mailer, go ahead
363 	**  and deliver it.
364 	*/
365 
366 	if (!bitset(QDONTSEND|QQUEUEUP, a->q_flags))
367 	{
368 		auto bool fuzzy;
369 		register struct passwd *pw;
370 		extern struct passwd *finduser();
371 
372 		/* warning -- finduser may trash buf */
373 		pw = finduser(buf, &fuzzy);
374 		if (pw == NULL)
375 		{
376 			a->q_flags |= QBADADDR;
377 			giveresponse(EX_NOUSER, m, NULL, e);
378 		}
379 		else
380 		{
381 			char nbuf[MAXNAME];
382 
383 			if (fuzzy)
384 			{
385 				/* name was a fuzzy match */
386 				a->q_user = newstr(pw->pw_name);
387 				if (findusercount++ > 3)
388 				{
389 					a->q_flags |= QBADADDR;
390 					usrerr("554 aliasing/forwarding loop for %s broken",
391 						pw->pw_name);
392 					return (a);
393 				}
394 
395 				/* see if it aliases */
396 				(void) strcpy(buf, pw->pw_name);
397 				goto trylocaluser;
398 			}
399 			a->q_home = newstr(pw->pw_dir);
400 			a->q_uid = pw->pw_uid;
401 			a->q_gid = pw->pw_gid;
402 			a->q_ruser = newstr(pw->pw_name);
403 			a->q_flags |= QGOODUID;
404 			buildfname(pw->pw_gecos, pw->pw_name, nbuf);
405 			if (nbuf[0] != '\0')
406 				a->q_fullname = newstr(nbuf);
407 			if (!quoted)
408 				forward(a, sendq, e);
409 		}
410 	}
411 	if (!bitset(QDONTSEND, a->q_flags))
412 		e->e_nrcpts++;
413 
414   testselfdestruct:
415 	if (tTd(26, 8))
416 	{
417 		printf("testselfdestruct: ");
418 		printaddr(a, TRUE);
419 	}
420 	if (a->q_alias == NULL && a != &e->e_from &&
421 	    bitset(QDONTSEND, a->q_flags))
422 	{
423 		q = *sendq;
424 		while (q != NULL && bitset(QDONTSEND, q->q_flags))
425 			q = q->q_next;
426 		if (q == NULL)
427 		{
428 			a->q_flags |= QBADADDR;
429 			usrerr("554 aliasing/forwarding loop broken");
430 		}
431 	}
432 	return (a);
433 }
434 /*
435 **  FINDUSER -- find the password entry for a user.
436 **
437 **	This looks a lot like getpwnam, except that it may want to
438 **	do some fancier pattern matching in /etc/passwd.
439 **
440 **	This routine contains most of the time of many sendmail runs.
441 **	It deserves to be optimized.
442 **
443 **	Parameters:
444 **		name -- the name to match against.
445 **		fuzzyp -- an outarg that is set to TRUE if this entry
446 **			was found using the fuzzy matching algorithm;
447 **			set to FALSE otherwise.
448 **
449 **	Returns:
450 **		A pointer to a pw struct.
451 **		NULL if name is unknown or ambiguous.
452 **
453 **	Side Effects:
454 **		may modify name.
455 */
456 
457 struct passwd *
458 finduser(name, fuzzyp)
459 	char *name;
460 	bool *fuzzyp;
461 {
462 	register struct passwd *pw;
463 	register char *p;
464 	extern struct passwd *getpwent();
465 	extern struct passwd *getpwnam();
466 
467 	if (tTd(29, 4))
468 		printf("finduser(%s): ", name);
469 
470 	*fuzzyp = FALSE;
471 
472 	/* look up this login name using fast path */
473 	if ((pw = getpwnam(name)) != NULL)
474 	{
475 		if (tTd(29, 4))
476 			printf("found (non-fuzzy)\n");
477 		return (pw);
478 	}
479 
480 #ifdef MATCHGECOS
481 	/* see if fuzzy matching allowed */
482 	if (!MatchGecos)
483 	{
484 		if (tTd(29, 4))
485 			printf("not found (fuzzy disabled)\n");
486 		return NULL;
487 	}
488 
489 	/* search for a matching full name instead */
490 	for (p = name; *p != '\0'; p++)
491 	{
492 		if (*p == (SpaceSub & 0177) || *p == '_')
493 			*p = ' ';
494 	}
495 	(void) setpwent();
496 	while ((pw = getpwent()) != NULL)
497 	{
498 		char buf[MAXNAME];
499 
500 		buildfname(pw->pw_gecos, pw->pw_name, buf);
501 		if (strchr(buf, ' ') != NULL && !strcasecmp(buf, name))
502 		{
503 			if (tTd(29, 4))
504 				printf("fuzzy matches %s\n", pw->pw_name);
505 			message("sending to login name %s", pw->pw_name);
506 			*fuzzyp = TRUE;
507 			return (pw);
508 		}
509 	}
510 	if (tTd(29, 4))
511 		printf("no fuzzy match found\n");
512 #else
513 	if (tTd(29, 4))
514 		printf("not found (fuzzy disabled)\n");
515 #endif
516 	return (NULL);
517 }
518 /*
519 **  WRITABLE -- predicate returning if the file is writable.
520 **
521 **	This routine must duplicate the algorithm in sys/fio.c.
522 **	Unfortunately, we cannot use the access call since we
523 **	won't necessarily be the real uid when we try to
524 **	actually open the file.
525 **
526 **	Notice that ANY file with ANY execute bit is automatically
527 **	not writable.  This is also enforced by mailfile.
528 **
529 **	Parameters:
530 **		s -- pointer to a stat struct for the file.
531 **
532 **	Returns:
533 **		TRUE -- if we will be able to write this file.
534 **		FALSE -- if we cannot write this file.
535 **
536 **	Side Effects:
537 **		none.
538 */
539 
540 bool
541 writable(s)
542 	register struct stat *s;
543 {
544 	uid_t euid;
545 	gid_t egid;
546 	int bits;
547 
548 	if (bitset(0111, s->st_mode))
549 		return (FALSE);
550 	euid = RealUid;
551 	egid = RealGid;
552 	if (geteuid() == 0)
553 	{
554 		if (bitset(S_ISUID, s->st_mode))
555 			euid = s->st_uid;
556 		if (bitset(S_ISGID, s->st_mode))
557 			egid = s->st_gid;
558 	}
559 
560 	if (euid == 0)
561 		return (TRUE);
562 	bits = S_IWRITE;
563 	if (euid != s->st_uid)
564 	{
565 		bits >>= 3;
566 		if (egid != s->st_gid)
567 			bits >>= 3;
568 	}
569 	return ((s->st_mode & bits) != 0);
570 }
571 /*
572 **  INCLUDE -- handle :include: specification.
573 **
574 **	Parameters:
575 **		fname -- filename to include.
576 **		forwarding -- if TRUE, we are reading a .forward file.
577 **			if FALSE, it's a :include: file.
578 **		ctladdr -- address template to use to fill in these
579 **			addresses -- effective user/group id are
580 **			the important things.
581 **		sendq -- a pointer to the head of the send queue
582 **			to put these addresses in.
583 **
584 **	Returns:
585 **		open error status
586 **
587 **	Side Effects:
588 **		reads the :include: file and sends to everyone
589 **		listed in that file.
590 */
591 
592 static jmp_buf	CtxIncludeTimeout;
593 static int	includetimeout();
594 
595 int
596 include(fname, forwarding, ctladdr, sendq, e)
597 	char *fname;
598 	bool forwarding;
599 	ADDRESS *ctladdr;
600 	ADDRESS **sendq;
601 	ENVELOPE *e;
602 {
603 	register FILE *fp;
604 	char *oldto = e->e_to;
605 	char *oldfilename = FileName;
606 	int oldlinenumber = LineNumber;
607 	register EVENT *ev = NULL;
608 	int nincludes;
609 	register ADDRESS *ca;
610 	uid_t saveduid, uid;
611 	gid_t savedgid, gid;
612 	char *uname;
613 	int rval = 0;
614 	char buf[MAXLINE];
615 
616 	if (tTd(27, 2))
617 		printf("include(%s)\n", fname);
618 	if (tTd(27, 4))
619 		printf("   ruid=%d euid=%d\n", getuid(), geteuid());
620 	if (tTd(27, 14))
621 	{
622 		printf("ctladdr ");
623 		printaddr(ctladdr, FALSE);
624 	}
625 
626 	if (tTd(27, 9))
627 		printf("include: old uid = %d/%d\n", getuid(), geteuid());
628 
629 	ca = getctladdr(ctladdr);
630 	if (ca == NULL)
631 	{
632 		uid = 0;
633 		gid = 0;
634 		uname = NULL;
635 		saveduid = -1;
636 	}
637 	else
638 	{
639 		uid = ca->q_uid;
640 		gid = ca->q_gid;
641 		uname = ca->q_user;
642 #ifdef HASSETREUID
643 		saveduid = geteuid();
644 		savedgid = getegid();
645 		if (saveduid == 0)
646 		{
647 			initgroups(uname, gid);
648 			if (uid != 0)
649 				(void) setreuid(0, uid);
650 		}
651 #endif
652 	}
653 
654 	if (tTd(27, 9))
655 		printf("include: new uid = %d/%d\n", getuid(), geteuid());
656 
657 	/*
658 	**  If home directory is remote mounted but server is down,
659 	**  this can hang or give errors; use a timeout to avoid this
660 	*/
661 
662 	if (setjmp(CtxIncludeTimeout) != 0)
663 	{
664 		ctladdr->q_flags |= QQUEUEUP;
665 		errno = 0;
666 		usrerr("451 open timeout on %s", fname);
667 
668 		/* return pseudo-error code */
669 		rval = EOPENTIMEOUT;
670 		goto resetuid;
671 	}
672 	ev = setevent((time_t) 60, includetimeout, 0);
673 
674 	/* the input file must be marked safe */
675 	rval = safefile(fname, uid, gid, uname, forwarding, S_IREAD);
676 	if (rval != 0)
677 	{
678 		/* don't use this :include: file */
679 		clrevent(ev);
680 		if (tTd(27, 4))
681 			printf("include: not safe (uid=%d): %s\n",
682 				uid, errstring(rval));
683 		goto resetuid;
684 	}
685 
686 	fp = fopen(fname, "r");
687 	if (fp == NULL)
688 	{
689 		rval = errno;
690 		clrevent(ev);
691 		if (tTd(27, 4))
692 			printf("include: open: %s\n", errstring(rval));
693 		goto resetuid;
694 	}
695 
696 	if (ca == NULL)
697 	{
698 		struct stat st;
699 
700 		if (fstat(fileno(fp), &st) < 0)
701 		{
702 			rval = errno;
703 			clrevent(ev);
704 			syserr("Cannot fstat %s!", fname);
705 			goto resetuid;
706 		}
707 		ctladdr->q_uid = st.st_uid;
708 		ctladdr->q_gid = st.st_gid;
709 		ctladdr->q_flags |= QGOODUID;
710 	}
711 
712 	clrevent(ev);
713 
714 	if (bitset(EF_VRFYONLY, e->e_flags))
715 	{
716 		/* don't do any more now */
717 		ctladdr->q_flags |= QVERIFIED;
718 		e->e_nrcpts++;
719 		xfclose(fp, "include", fname);
720 		goto resetuid;
721 	}
722 
723 	/* read the file -- each line is a comma-separated list. */
724 	FileName = fname;
725 	LineNumber = 0;
726 	ctladdr->q_flags &= ~QSELFREF;
727 	nincludes = 0;
728 	while (fgets(buf, sizeof buf, fp) != NULL)
729 	{
730 		register char *p = strchr(buf, '\n');
731 
732 		LineNumber++;
733 		if (p != NULL)
734 			*p = '\0';
735 		if (buf[0] == '#' || buf[0] == '\0')
736 			continue;
737 		e->e_to = NULL;
738 		message("%s to %s",
739 			forwarding ? "forwarding" : "sending", buf);
740 #ifdef LOG
741 		if (forwarding && LogLevel > 9)
742 			syslog(LOG_INFO, "%s: forward %s => %s",
743 				e->e_id, oldto, buf);
744 #endif
745 
746 		AliasLevel++;
747 		nincludes += sendtolist(buf, ctladdr, sendq, e);
748 		AliasLevel--;
749 	}
750 
751 	if (ferror(fp) && tTd(27, 3))
752 		printf("include: read error: %s\n", errstring(errno));
753 	if (nincludes > 0 && !bitset(QSELFREF, ctladdr->q_flags))
754 	{
755 		if (tTd(27, 5))
756 		{
757 			printf("include: QDONTSEND ");
758 			printaddr(ctladdr, FALSE);
759 		}
760 		ctladdr->q_flags |= QDONTSEND;
761 	}
762 
763 	(void) xfclose(fp, "include", fname);
764 	FileName = oldfilename;
765 	LineNumber = oldlinenumber;
766 	e->e_to = oldto;
767 
768 resetuid:
769 
770 #ifdef HASSETREUID
771 	if (saveduid == 0)
772 	{
773 		if (uid != 0)
774 			if (setreuid(-1, 0) < 0 || setreuid(RealUid, 0) < 0)
775 				syserr("setreuid(%d, 0) failure (real=%d, eff=%d)",
776 					RealUid, getuid(), geteuid());
777 		setgid(savedgid);
778 	}
779 #endif
780 
781 	if (tTd(27, 9))
782 		printf("include: reset uid = %d/%d\n", getuid(), geteuid());
783 	return rval;
784 }
785 
786 static
787 includetimeout()
788 {
789 	longjmp(CtxIncludeTimeout, 1);
790 }
791 /*
792 **  SENDTOARGV -- send to an argument vector.
793 **
794 **	Parameters:
795 **		argv -- argument vector to send to.
796 **		e -- the current envelope.
797 **
798 **	Returns:
799 **		none.
800 **
801 **	Side Effects:
802 **		puts all addresses on the argument vector onto the
803 **			send queue.
804 */
805 
806 sendtoargv(argv, e)
807 	register char **argv;
808 	register ENVELOPE *e;
809 {
810 	register char *p;
811 
812 	while ((p = *argv++) != NULL)
813 	{
814 		(void) sendtolist(p, NULLADDR, &e->e_sendqueue, e);
815 	}
816 }
817 /*
818 **  GETCTLADDR -- get controlling address from an address header.
819 **
820 **	If none, get one corresponding to the effective userid.
821 **
822 **	Parameters:
823 **		a -- the address to find the controller of.
824 **
825 **	Returns:
826 **		the controlling address.
827 **
828 **	Side Effects:
829 **		none.
830 */
831 
832 ADDRESS *
833 getctladdr(a)
834 	register ADDRESS *a;
835 {
836 	while (a != NULL && !bitset(QGOODUID, a->q_flags))
837 		a = a->q_alias;
838 	return (a);
839 }
840