1 # include <signal.h>
2 # include <errno.h>
3 # include <sys/types.h>
4 # include <sys/stat.h>
5 # include "sendmail.h"
6 # ifdef LOG
7 # include <syslog.h>
8 # endif LOG
9 
10 static char SccsId[] = "@(#)deliver.c	3.44	09/29/81";
11 
12 /*
13 **  DELIVER -- Deliver a message to a list of addresses.
14 **
15 **	This routine delivers to everyone on the same host as the
16 **	user on the head of the list.  It is clever about mailers
17 **	that don't handle multiple users.  It is NOT guaranteed
18 **	that it will deliver to all these addresses however -- so
19 **	deliver should be called once for each address on the
20 **	list.
21 **
22 **	Parameters:
23 **		to -- head of the address list to deliver to.
24 **		editfcn -- if non-NULL, we want to call this function
25 **			to output the letter (instead of just out-
26 **			putting it raw).
27 **
28 **	Returns:
29 **		zero -- successfully delivered.
30 **		else -- some failure, see ExitStat for more info.
31 **
32 **	Side Effects:
33 **		The standard input is passed off to someone.
34 */
35 
36 deliver(to, editfcn)
37 	ADDRESS *to;
38 	int (*editfcn)();
39 {
40 	char *host;
41 	char *user;
42 	char **pvp;
43 	register char **mvp;
44 	register char *p;
45 	register struct mailer *m;
46 	register int i;
47 	extern putmessage();
48 	extern bool checkcompat();
49 	char *pv[MAXPV+1];
50 	char tobuf[MAXLINE];
51 	char buf[MAXNAME];
52 	ADDRESS *ctladdr;
53 	extern ADDRESS *getctladdr();
54 
55 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
56 		return (0);
57 
58 # ifdef DEBUG
59 	if (Debug)
60 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
61 			to->q_mailer, to->q_host, to->q_user);
62 # endif DEBUG
63 
64 	/*
65 	**  Do initial argv setup.
66 	**	Insert the mailer name.  Notice that $x expansion is
67 	**	NOT done on the mailer name.  Then, if the mailer has
68 	**	a picky -f flag, we insert it as appropriate.  This
69 	**	code does not check for 'pv' overflow; this places a
70 	**	manifest lower limit of 4 for MAXPV.
71 	*/
72 
73 	m = Mailer[to->q_mailer];
74 	host = to->q_host;
75 	define('g', m->m_from);		/* translated from address */
76 	define('h', host);		/* to host */
77 	Errors = 0;
78 	errno = 0;
79 	pvp = pv;
80 	*pvp++ = m->m_argv[0];
81 
82 	/* insert -f or -r flag as appropriate */
83 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
84 	{
85 		if (bitset(M_FOPT, m->m_flags))
86 			*pvp++ = "-f";
87 		else
88 			*pvp++ = "-r";
89 		(void) expand("$g", buf, &buf[sizeof buf - 1]);
90 		*pvp++ = newstr(buf);
91 	}
92 
93 	/*
94 	**  Append the other fixed parts of the argv.  These run
95 	**  up to the first entry containing "$u".  There can only
96 	**  be one of these, and there are only a few more slots
97 	**  in the pv after it.
98 	*/
99 
100 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
101 	{
102 		while ((p = index(p, '$')) != NULL)
103 			if (*++p == 'u')
104 				break;
105 		if (p != NULL)
106 			break;
107 
108 		/* this entry is safe -- go ahead and process it */
109 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
110 		*pvp++ = newstr(buf);
111 		if (pvp >= &pv[MAXPV - 3])
112 		{
113 			syserr("Too many parameters to %s before $u", pv[0]);
114 			return (-1);
115 		}
116 	}
117 	if (*mvp == NULL)
118 		syserr("No $u in mailer argv for %s", pv[0]);
119 
120 	/*
121 	**  At this point *mvp points to the argument with $u.  We
122 	**  run through our address list and append all the addresses
123 	**  we can.  If we run out of space, do not fret!  We can
124 	**  always send another copy later.
125 	*/
126 
127 	tobuf[0] = '\0';
128 	To = tobuf;
129 	ctladdr = NULL;
130 	for (; to != NULL; to = to->q_next)
131 	{
132 		/* avoid sending multiple recipients to dumb mailers */
133 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
134 			break;
135 
136 		/* if already sent or not for this host, don't send */
137 		if ((!ForceMail && bitset(QDONTSEND, to->q_flags)) ||
138 		    strcmp(to->q_host, host) != 0)
139 			continue;
140 
141 		/* compute effective uid/gid when sending */
142 		if (to->q_mailer == MN_PROG)
143 			ctladdr = getctladdr(to);
144 
145 		user = to->q_user;
146 		To = to->q_paddr;
147 		to->q_flags |= QDONTSEND;
148 # ifdef DEBUG
149 		if (Debug)
150 			printf("   send to `%s'\n", user);
151 # endif DEBUG
152 
153 		/*
154 		**  Check to see that these people are allowed to
155 		**  talk to each other.
156 		*/
157 
158 		if (!checkcompat(to))
159 		{
160 			giveresponse(EX_UNAVAILABLE, TRUE, m);
161 			continue;
162 		}
163 
164 		/*
165 		**  Strip quote bits from names if the mailer is dumb
166 		**	about them.
167 		*/
168 
169 		if (bitset(M_STRIPQ, m->m_flags))
170 		{
171 			stripquotes(user, TRUE);
172 			stripquotes(host, TRUE);
173 		}
174 		else
175 		{
176 			stripquotes(user, FALSE);
177 			stripquotes(host, FALSE);
178 		}
179 
180 		/*
181 		**  If an error message has already been given, don't
182 		**	bother to send to this address.
183 		**
184 		**	>>>>>>>>>> This clause assumes that the local mailer
185 		**	>> NOTE >> cannot do any further aliasing; that
186 		**	>>>>>>>>>> function is subsumed by sendmail.
187 		*/
188 
189 		if (bitset(QBADADDR, to->q_flags))
190 			continue;
191 
192 		/* save statistics.... */
193 		Stat.stat_nt[to->q_mailer]++;
194 		Stat.stat_bt[to->q_mailer] += kbytes(MsgSize);
195 
196 		/*
197 		**  See if this user name is "special".
198 		**	If the user name has a slash in it, assume that this
199 		**	is a file -- send it off without further ado.
200 		**	Note that this means that editfcn's will not
201 		**	be applied to the message.  Also note that
202 		**	this type of addresses is not processed along
203 		**	with the others, so we fudge on the To person.
204 		*/
205 
206 		if (m == Mailer[MN_LOCAL])
207 		{
208 			if (index(user, '/') != NULL)
209 			{
210 				i = mailfile(user, getctladdr(to));
211 				giveresponse(i, TRUE, m);
212 				continue;
213 			}
214 		}
215 
216 		/*
217 		**  Address is verified -- add this user to mailer
218 		**  argv, and add it to the print list of recipients.
219 		*/
220 
221 		/* create list of users for error messages */
222 		if (tobuf[0] != '\0')
223 			(void) strcat(tobuf, ",");
224 		(void) strcat(tobuf, to->q_paddr);
225 		define('u', user);		/* to user */
226 		define('z', to->q_home);	/* user's home */
227 
228 		/* expand out this user */
229 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
230 		*pvp++ = newstr(buf);
231 		if (pvp >= &pv[MAXPV - 2])
232 		{
233 			/* allow some space for trailing parms */
234 			break;
235 		}
236 	}
237 
238 	/* see if any addresses still exist */
239 	if (tobuf[0] == '\0')
240 		return (0);
241 
242 	/* print out messages as full list */
243 	To = tobuf;
244 
245 	/*
246 	**  Fill out any parameters after the $u parameter.
247 	*/
248 
249 	while (*++mvp != NULL)
250 	{
251 		(void) expand(*mvp, buf, &buf[sizeof buf - 1]);
252 		*pvp++ = newstr(buf);
253 		if (pvp >= &pv[MAXPV])
254 			syserr("deliver: pv overflow after $u for %s", pv[0]);
255 	}
256 	*pvp++ = NULL;
257 
258 	/*
259 	**  Call the mailer.
260 	**	The argument vector gets built, pipes
261 	**	are created as necessary, and we fork & exec as
262 	**	appropriate.
263 	*/
264 
265 	if (editfcn == NULL)
266 		editfcn = putmessage;
267 	if (ctladdr == NULL)
268 		ctladdr = &From;
269 	i = sendoff(m, pv, editfcn, ctladdr);
270 
271 	return (i);
272 }
273 /*
274 **  DOFORK -- do a fork, retrying a couple of times on failure.
275 **
276 **	This MUST be a macro, since after a vfork we are running
277 **	two processes on the same stack!!!
278 **
279 **	Parameters:
280 **		none.
281 **
282 **	Returns:
283 **		From a macro???  You've got to be kidding!
284 **
285 **	Side Effects:
286 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
287 **			pid of child in parent, zero in child.
288 **			-1 on unrecoverable error.
289 **
290 **	Notes:
291 **		I'm awfully sorry this looks so awful.  That's
292 **		vfork for you.....
293 */
294 
295 # define NFORKTRIES	5
296 # ifdef VFORK
297 # define XFORK	vfork
298 # else VFORK
299 # define XFORK	fork
300 # endif VFORK
301 
302 # define DOFORK(fORKfN) \
303 {\
304 	register int i;\
305 \
306 	for (i = NFORKTRIES; i-- > 0; )\
307 	{\
308 		pid = fORKfN();\
309 		if (pid >= 0)\
310 			break;\
311 		sleep((unsigned) NFORKTRIES - i);\
312 	}\
313 }
314 /*
315 **  SENDOFF -- send off call to mailer & collect response.
316 **
317 **	Parameters:
318 **		m -- mailer descriptor.
319 **		pvp -- parameter vector to send to it.
320 **		editfcn -- function to pipe it through.
321 **		ctladdr -- an address pointer controlling the
322 **			user/groupid etc. of the mailer.
323 **
324 **	Returns:
325 **		exit status of mailer.
326 **
327 **	Side Effects:
328 **		none.
329 */
330 
331 sendoff(m, pvp, editfcn, ctladdr)
332 	struct mailer *m;
333 	char **pvp;
334 	int (*editfcn)();
335 	ADDRESS *ctladdr;
336 {
337 	auto int st;
338 	register int i;
339 	int pid;
340 	int pvect[2];
341 	FILE *mfile;
342 	extern putmessage();
343 	extern FILE *fdopen();
344 
345 # ifdef DEBUG
346 	if (Debug)
347 	{
348 		printf("Sendoff:\n");
349 		printav(pvp);
350 	}
351 # endif DEBUG
352 
353 	/* create a pipe to shove the mail through */
354 	if (pipe(pvect) < 0)
355 	{
356 		syserr("pipe");
357 		return (-1);
358 	}
359 	DOFORK(XFORK);
360 	/* pid is set by DOFORK */
361 	if (pid < 0)
362 	{
363 		syserr("Cannot fork");
364 		(void) close(pvect[0]);
365 		(void) close(pvect[1]);
366 		return (-1);
367 	}
368 	else if (pid == 0)
369 	{
370 		/* child -- set up input & exec mailer */
371 		/* make diagnostic output be standard output */
372 		(void) signal(SIGINT, SIG_DFL);
373 		(void) signal(SIGHUP, SIG_DFL);
374 		(void) signal(SIGTERM, SIG_DFL);
375 		(void) close(2);
376 		(void) dup(1);
377 		(void) close(0);
378 		if (dup(pvect[0]) < 0)
379 		{
380 			syserr("Cannot dup to zero!");
381 			_exit(EX_OSERR);
382 		}
383 		(void) close(pvect[0]);
384 		(void) close(pvect[1]);
385 		if (!bitset(M_RESTR, m->m_flags))
386 		{
387 			if (ctladdr->q_uid == 0)
388 			{
389 				extern int DefUid, DefGid;
390 
391 				(void) setgid(DefGid);
392 				(void) setuid(DefUid);
393 			}
394 			else
395 			{
396 				(void) setgid(ctladdr->q_gid);
397 				(void) setuid(ctladdr->q_uid);
398 			}
399 		}
400 # ifndef VFORK
401 		/*
402 		**  We have to be careful with vfork - we can't mung up the
403 		**  memory but we don't want the mailer to inherit any extra
404 		**  open files.  Chances are the mailer won't
405 		**  care about an extra file, but then again you never know.
406 		**  Actually, we would like to close(fileno(pwf)), but it's
407 		**  declared static so we can't.  But if we fclose(pwf), which
408 		**  is what endpwent does, it closes it in the parent too and
409 		**  the next getpwnam will be slower.  If you have a weird
410 		**  mailer that chokes on the extra file you should do the
411 		**  endpwent().
412 		**
413 		**  Similar comments apply to log.  However, openlog is
414 		**  clever enough to set the FIOCLEX mode on the file,
415 		**  so it will be closed automatically on the exec.
416 		*/
417 
418 		endpwent();
419 # ifdef LOG
420 		closelog();
421 # endif LOG
422 # endif VFORK
423 		execv(m->m_mailer, pvp);
424 		/* syserr fails because log is closed */
425 		/* syserr("Cannot exec %s", m->m_mailer); */
426 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
427 		(void) fflush(stdout);
428 		_exit(EX_UNAVAILABLE);
429 	}
430 
431 	/* write out message to mailer */
432 	(void) close(pvect[0]);
433 	(void) signal(SIGPIPE, SIG_IGN);
434 	mfile = fdopen(pvect[1], "w");
435 	if (editfcn == NULL)
436 		editfcn = putmessage;
437 	(*editfcn)(mfile, m);
438 	(void) fclose(mfile);
439 
440 	/*
441 	**  Wait for child to die and report status.
442 	**	We should never get fatal errors (e.g., segmentation
443 	**	violation), so we report those specially.  For other
444 	**	errors, we choose a status message (into statmsg),
445 	**	and if it represents an error, we print it.
446 	*/
447 
448 	while ((i = wait(&st)) > 0 && i != pid)
449 		continue;
450 	if (i < 0)
451 	{
452 		syserr("wait");
453 		return (-1);
454 	}
455 	if ((st & 0377) != 0)
456 	{
457 		syserr("%s: stat %o", pvp[0], st);
458 		ExitStat = EX_UNAVAILABLE;
459 		return (-1);
460 	}
461 	i = (st >> 8) & 0377;
462 	giveresponse(i, TRUE, m);
463 	return (i);
464 }
465 /*
466 **  GIVERESPONSE -- Interpret an error response from a mailer
467 **
468 **	Parameters:
469 **		stat -- the status code from the mailer (high byte
470 **			only; core dumps must have been taken care of
471 **			already).
472 **		force -- if set, force an error message output, even
473 **			if the mailer seems to like to print its own
474 **			messages.
475 **		m -- the mailer descriptor for this mailer.
476 **
477 **	Returns:
478 **		none.
479 **
480 **	Side Effects:
481 **		Errors may be incremented.
482 **		ExitStat may be set.
483 */
484 
485 giveresponse(stat, force, m)
486 	int stat;
487 	int force;
488 	register struct mailer *m;
489 {
490 	register char *statmsg;
491 	extern char *SysExMsg[];
492 	register int i;
493 	extern int N_SysEx;
494 	char buf[30];
495 
496 	/*
497 	**  Compute status message from code.
498 	*/
499 
500 	i = stat - EX__BASE;
501 	if (i < 0 || i > N_SysEx)
502 		statmsg = NULL;
503 	else
504 		statmsg = SysExMsg[i];
505 	if (stat == 0)
506 	{
507 		if (bitset(M_LOCAL, m->m_flags))
508 			statmsg = "delivered";
509 		else
510 			statmsg = "queued";
511 		if (Verbose)
512 			message(Arpa_Info, statmsg);
513 	}
514 	else
515 	{
516 		Errors++;
517 		if (statmsg == NULL && m->m_badstat != 0)
518 		{
519 			stat = m->m_badstat;
520 			i = stat - EX__BASE;
521 # ifdef DEBUG
522 			if (i < 0 || i >= N_SysEx)
523 				syserr("Bad m_badstat %d", stat);
524 			else
525 # endif DEBUG
526 			statmsg = SysExMsg[i];
527 		}
528 		if (statmsg == NULL)
529 			usrerr("unknown mailer response %d", stat);
530 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
531 			usrerr("%s", statmsg);
532 	}
533 
534 	/*
535 	**  Final cleanup.
536 	**	Log a record of the transaction.  Compute the new
537 	**	ExitStat -- if we already had an error, stick with
538 	**	that.
539 	*/
540 
541 	if (statmsg == NULL)
542 	{
543 		(void) sprintf(buf, "error %d", stat);
544 		statmsg = buf;
545 	}
546 
547 # ifdef LOG
548 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
549 # endif LOG
550 	setstat(stat);
551 }
552 /*
553 **  PUTMESSAGE -- output a message to the final mailer.
554 **
555 **	This routine takes care of recreating the header from the
556 **	in-core copy, etc.
557 **
558 **	Parameters:
559 **		fp -- file to output onto.
560 **		m -- a mailer descriptor.
561 **
562 **	Returns:
563 **		none.
564 **
565 **	Side Effects:
566 **		The message is written onto fp.
567 */
568 
569 putmessage(fp, m)
570 	FILE *fp;
571 	struct mailer *m;
572 {
573 	char buf[BUFSIZ];
574 	register int i;
575 	register HDR *h;
576 	extern char *arpadate();
577 	bool anyheader = FALSE;
578 	extern char *capitalize();
579 	extern char *hvalue();
580 	extern bool samefrom();
581 	char *of_line;
582 
583 	/*
584 	**  Output "From" line unless supressed
585 	*/
586 
587 	if (!bitset(M_NHDR, m->m_flags))
588 	{
589 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
590 		fprintf(fp, "%s\n", buf);
591 	}
592 
593 	/*
594 	**  Output all header lines
595 	*/
596 
597 	of_line = hvalue("original-from");
598 	for (h = Header; h != NULL; h = h->h_link)
599 	{
600 		register char *p;
601 		char *origfrom = OrigFrom;
602 		bool nooutput;
603 
604 		nooutput = FALSE;
605 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
606 		{
607 			p = ")><(";		/* can't happen (I hope) */
608 			nooutput = TRUE;
609 		}
610 
611 		/* use From: line from message if generated is the same */
612 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
613 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
614 		{
615 			p = origfrom;
616 			origfrom = NULL;
617 		}
618 		else if (bitset(H_DEFAULT, h->h_flags))
619 		{
620 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
621 			p = buf;
622 		}
623 		else
624 			p = h->h_value;
625 		if (p == NULL || *p == '\0')
626 			continue;
627 
628 		/* hack, hack -- output Original-From field if different */
629 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
630 		{
631 			/* output new Original-From line if needed */
632 			if (of_line == NULL && !samefrom(p, origfrom))
633 			{
634 				fprintf(fp, "Original-From: %s\n", origfrom);
635 				anyheader = TRUE;
636 			}
637 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
638 			{
639 				/* delete Original-From: line if redundant */
640 				p = of_line;
641 				of_line = NULL;
642 			}
643 		}
644 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
645 			nooutput = TRUE;
646 
647 		/* finally, output the header line */
648 		if (!nooutput)
649 		{
650 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
651 			h->h_flags |= H_USED;
652 			anyheader = TRUE;
653 		}
654 	}
655 	if (anyheader)
656 		fprintf(fp, "\n");
657 
658 	/*
659 	**  Output the body of the message
660 	*/
661 
662 	rewind(TempFile);
663 	while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
664 		(void) fwrite(buf, 1, i, fp);
665 
666 	if (ferror(fp) && errno != EPIPE)
667 	{
668 		syserr("putmessage: write error");
669 		setstat(EX_IOERR);
670 	}
671 	errno = 0;
672 }
673 /*
674 **  SAMEFROM -- tell if two text addresses represent the same from address.
675 **
676 **	Parameters:
677 **		ifrom -- internally generated form of from address.
678 **		efrom -- external form of from address.
679 **
680 **	Returns:
681 **		TRUE -- if they convey the same info.
682 **		FALSE -- if any information has been lost.
683 **
684 **	Side Effects:
685 **		none.
686 */
687 
688 bool
689 samefrom(ifrom, efrom)
690 	char *ifrom;
691 	char *efrom;
692 {
693 	register char *p;
694 	char buf[MAXNAME + 4];
695 
696 # ifdef DEBUG
697 	if (Debug > 7)
698 		printf("samefrom(%s,%s)-->", ifrom, efrom);
699 # endif DEBUG
700 	if (strcmp(ifrom, efrom) == 0)
701 		goto success;
702 	p = index(ifrom, '@');
703 	if (p == NULL)
704 		goto failure;
705 	*p = '\0';
706 	strcpy(buf, ifrom);
707 	strcat(buf, " at ");
708 	*p++ = '@';
709 	strcat(buf, p);
710 	if (strcmp(buf, efrom) == 0)
711 		goto success;
712 
713   failure:
714 # ifdef DEBUG
715 	if (Debug > 7)
716 		printf("FALSE\n");
717 # endif DEBUG
718 	return (FALSE);
719 
720   success:
721 # ifdef DEBUG
722 	if (Debug > 7)
723 		printf("TRUE\n");
724 # endif DEBUG
725 	return (TRUE);
726 }
727 /*
728 **  MAILFILE -- Send a message to a file.
729 **
730 **	If the file has the setuid/setgid bits set, but NO execute
731 **	bits, sendmail will try to become the owner of that file
732 **	rather than the real user.  Obviously, this only works if
733 **	sendmail runs as root.
734 **
735 **	Parameters:
736 **		filename -- the name of the file to send to.
737 **		ctladdr -- the controlling address header -- includes
738 **			the userid/groupid to be when sending.
739 **
740 **	Returns:
741 **		The exit code associated with the operation.
742 **
743 **	Side Effects:
744 **		none.
745 */
746 
747 mailfile(filename, ctladdr)
748 	char *filename;
749 	ADDRESS *ctladdr;
750 {
751 	register FILE *f;
752 	register int pid;
753 
754 	/*
755 	**  Fork so we can change permissions here.
756 	**	Note that we MUST use fork, not vfork, because of
757 	**	the complications of calling subroutines, etc.
758 	*/
759 
760 	DOFORK(fork);
761 
762 	if (pid < 0)
763 		return (EX_OSERR);
764 	else if (pid == 0)
765 	{
766 		/* child -- actually write to file */
767 		struct stat stb;
768 		extern int DefUid, DefGid;
769 
770 		(void) signal(SIGINT, SIG_DFL);
771 		(void) signal(SIGHUP, SIG_DFL);
772 		(void) signal(SIGTERM, SIG_DFL);
773 		umask(OldUmask);
774 		if (stat(filename, &stb) < 0)
775 			stb.st_mode = 0666;
776 		if (bitset(0111, stb.st_mode))
777 			exit(EX_CANTCREAT);
778 		if (ctladdr == NULL)
779 			ctladdr = &From;
780 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
781 		{
782 			if (ctladdr->q_uid == 0)
783 				(void) setgid(DefGid);
784 			else
785 				(void) setgid(ctladdr->q_gid);
786 		}
787 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
788 		{
789 			if (ctladdr->q_uid == 0)
790 				(void) setuid(DefUid);
791 			else
792 				(void) setuid(ctladdr->q_uid);
793 		}
794 		f = fopen(filename, "a");
795 		if (f == NULL)
796 			exit(EX_CANTCREAT);
797 
798 		putmessage(f, Mailer[1]);
799 		fputs("\n", f);
800 		(void) fclose(f);
801 		(void) fflush(stdout);
802 
803 		/* reset ISUID & ISGID bits */
804 		(void) chmod(filename, stb.st_mode);
805 		exit(EX_OK);
806 		/*NOTREACHED*/
807 	}
808 	else
809 	{
810 		/* parent -- wait for exit status */
811 		register int i;
812 		auto int stat;
813 
814 		while ((i = wait(&stat)) != pid)
815 		{
816 			if (i < 0)
817 			{
818 				stat = EX_OSERR << 8;
819 				break;
820 			}
821 		}
822 		if ((stat & 0377) != 0)
823 			stat = EX_UNAVAILABLE << 8;
824 		return ((stat >> 8) & 0377);
825 	}
826 }
827