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