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.48	10/19/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 	errno = 0;
58 	if (!ForceMail && bitset(QDONTSEND, to->q_flags))
59 		return (0);
60 
61 # ifdef DEBUG
62 	if (Debug)
63 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
64 			to->q_mailer, to->q_host, to->q_user);
65 # endif DEBUG
66 
67 	/*
68 	**  Do initial argv setup.
69 	**	Insert the mailer name.  Notice that $x expansion is
70 	**	NOT done on the mailer name.  Then, if the mailer has
71 	**	a picky -f flag, we insert it as appropriate.  This
72 	**	code does not check for 'pv' overflow; this places a
73 	**	manifest lower limit of 4 for MAXPV.
74 	*/
75 
76 	m = Mailer[to->q_mailer];
77 	host = to->q_host;
78 
79 	/* rewrite from address, using rewriting rules */
80 	(void) expand(m->m_from, buf, &buf[sizeof buf - 1]);
81 	mvp = prescan(buf, '\0');
82 	if (mvp == NULL)
83 	{
84 		syserr("bad mailer from translate \"%s\"", buf);
85 		return (EX_SOFTWARE);
86 	}
87 	rewrite(mvp, 2);
88 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
89 
90 	define('g', tfrombuf);		/* translated sender address */
91 	define('h', host);		/* to host */
92 	Errors = 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 	errno = 0;
286 	return (i);
287 }
288 /*
289 **  DOFORK -- do a fork, retrying a couple of times on failure.
290 **
291 **	This MUST be a macro, since after a vfork we are running
292 **	two processes on the same stack!!!
293 **
294 **	Parameters:
295 **		none.
296 **
297 **	Returns:
298 **		From a macro???  You've got to be kidding!
299 **
300 **	Side Effects:
301 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
302 **			pid of child in parent, zero in child.
303 **			-1 on unrecoverable error.
304 **
305 **	Notes:
306 **		I'm awfully sorry this looks so awful.  That's
307 **		vfork for you.....
308 */
309 
310 # define NFORKTRIES	5
311 # ifdef VFORK
312 # define XFORK	vfork
313 # else VFORK
314 # define XFORK	fork
315 # endif VFORK
316 
317 # define DOFORK(fORKfN) \
318 {\
319 	register int i;\
320 \
321 	for (i = NFORKTRIES; i-- > 0; )\
322 	{\
323 		pid = fORKfN();\
324 		if (pid >= 0)\
325 			break;\
326 		sleep((unsigned) NFORKTRIES - i);\
327 	}\
328 }
329 /*
330 **  SENDOFF -- send off call to mailer & collect response.
331 **
332 **	Parameters:
333 **		m -- mailer descriptor.
334 **		pvp -- parameter vector to send to it.
335 **		editfcn -- function to pipe it through.
336 **		ctladdr -- an address pointer controlling the
337 **			user/groupid etc. of the mailer.
338 **
339 **	Returns:
340 **		exit status of mailer.
341 **
342 **	Side Effects:
343 **		none.
344 */
345 
346 sendoff(m, pvp, editfcn, ctladdr)
347 	struct mailer *m;
348 	char **pvp;
349 	int (*editfcn)();
350 	ADDRESS *ctladdr;
351 {
352 	auto int st;
353 	register int i;
354 	int pid;
355 	int pvect[2];
356 	FILE *mfile;
357 	extern putmessage();
358 	extern FILE *fdopen();
359 
360 # ifdef DEBUG
361 	if (Debug)
362 	{
363 		printf("Sendoff:\n");
364 		printav(pvp);
365 	}
366 # endif DEBUG
367 	errno = 0;
368 
369 	/* create a pipe to shove the mail through */
370 	if (pipe(pvect) < 0)
371 	{
372 		syserr("pipe");
373 		return (-1);
374 	}
375 	DOFORK(XFORK);
376 	/* pid is set by DOFORK */
377 	if (pid < 0)
378 	{
379 		syserr("Cannot fork");
380 		(void) close(pvect[0]);
381 		(void) close(pvect[1]);
382 		return (-1);
383 	}
384 	else if (pid == 0)
385 	{
386 		/* child -- set up input & exec mailer */
387 		/* make diagnostic output be standard output */
388 		(void) signal(SIGINT, SIG_IGN);
389 		(void) signal(SIGHUP, SIG_IGN);
390 		(void) signal(SIGTERM, SIG_DFL);
391 		(void) close(2);
392 		(void) dup(1);
393 		(void) close(0);
394 		if (dup(pvect[0]) < 0)
395 		{
396 			syserr("Cannot dup to zero!");
397 			_exit(EX_OSERR);
398 		}
399 		(void) close(pvect[0]);
400 		(void) close(pvect[1]);
401 		if (!bitset(M_RESTR, m->m_flags))
402 		{
403 			if (ctladdr->q_uid == 0)
404 			{
405 				extern int DefUid, DefGid;
406 
407 				(void) setgid(DefGid);
408 				(void) setuid(DefUid);
409 			}
410 			else
411 			{
412 				(void) setgid(ctladdr->q_gid);
413 				(void) setuid(ctladdr->q_uid);
414 			}
415 		}
416 # ifndef VFORK
417 		/*
418 		**  We have to be careful with vfork - we can't mung up the
419 		**  memory but we don't want the mailer to inherit any extra
420 		**  open files.  Chances are the mailer won't
421 		**  care about an extra file, but then again you never know.
422 		**  Actually, we would like to close(fileno(pwf)), but it's
423 		**  declared static so we can't.  But if we fclose(pwf), which
424 		**  is what endpwent does, it closes it in the parent too and
425 		**  the next getpwnam will be slower.  If you have a weird
426 		**  mailer that chokes on the extra file you should do the
427 		**  endpwent().
428 		**
429 		**  Similar comments apply to log.  However, openlog is
430 		**  clever enough to set the FIOCLEX mode on the file,
431 		**  so it will be closed automatically on the exec.
432 		*/
433 
434 		endpwent();
435 # ifdef LOG
436 		closelog();
437 # endif LOG
438 # endif VFORK
439 		execv(m->m_mailer, pvp);
440 		/* syserr fails because log is closed */
441 		/* syserr("Cannot exec %s", m->m_mailer); */
442 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
443 		(void) fflush(stdout);
444 		_exit(EX_UNAVAILABLE);
445 	}
446 
447 	/* write out message to mailer */
448 	(void) close(pvect[0]);
449 	(void) signal(SIGPIPE, SIG_IGN);
450 	mfile = fdopen(pvect[1], "w");
451 	if (editfcn == NULL)
452 		editfcn = putmessage;
453 	(*editfcn)(mfile, m);
454 	(void) fclose(mfile);
455 
456 	/*
457 	**  Wait for child to die and report status.
458 	**	We should never get fatal errors (e.g., segmentation
459 	**	violation), so we report those specially.  For other
460 	**	errors, we choose a status message (into statmsg),
461 	**	and if it represents an error, we print it.
462 	*/
463 
464 	while ((i = wait(&st)) > 0 && i != pid)
465 		continue;
466 	if (i < 0)
467 	{
468 		syserr("wait");
469 		return (-1);
470 	}
471 	if ((st & 0377) != 0)
472 	{
473 		syserr("%s: stat %o", pvp[0], st);
474 		ExitStat = EX_UNAVAILABLE;
475 		return (-1);
476 	}
477 	i = (st >> 8) & 0377;
478 	giveresponse(i, TRUE, m);
479 	return (i);
480 }
481 /*
482 **  GIVERESPONSE -- Interpret an error response from a mailer
483 **
484 **	Parameters:
485 **		stat -- the status code from the mailer (high byte
486 **			only; core dumps must have been taken care of
487 **			already).
488 **		force -- if set, force an error message output, even
489 **			if the mailer seems to like to print its own
490 **			messages.
491 **		m -- the mailer descriptor for this mailer.
492 **
493 **	Returns:
494 **		none.
495 **
496 **	Side Effects:
497 **		Errors may be incremented.
498 **		ExitStat may be set.
499 */
500 
501 giveresponse(stat, force, m)
502 	int stat;
503 	int force;
504 	register struct mailer *m;
505 {
506 	register char *statmsg;
507 	extern char *SysExMsg[];
508 	register int i;
509 	extern int N_SysEx;
510 	char buf[30];
511 
512 	/*
513 	**  Compute status message from code.
514 	*/
515 
516 	i = stat - EX__BASE;
517 	if (i < 0 || i > N_SysEx)
518 		statmsg = NULL;
519 	else
520 		statmsg = SysExMsg[i];
521 	if (stat == 0)
522 	{
523 		if (bitset(M_LOCAL, m->m_flags))
524 			statmsg = "delivered";
525 		else
526 			statmsg = "queued";
527 		if (Verbose)
528 			message(Arpa_Info, statmsg);
529 	}
530 	else
531 	{
532 		Errors++;
533 		if (statmsg == NULL && m->m_badstat != 0)
534 		{
535 			stat = m->m_badstat;
536 			i = stat - EX__BASE;
537 # ifdef DEBUG
538 			if (i < 0 || i >= N_SysEx)
539 				syserr("Bad m_badstat %d", stat);
540 			else
541 # endif DEBUG
542 			statmsg = SysExMsg[i];
543 		}
544 		if (statmsg == NULL)
545 			usrerr("unknown mailer response %d", stat);
546 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
547 			usrerr("%s", statmsg);
548 	}
549 
550 	/*
551 	**  Final cleanup.
552 	**	Log a record of the transaction.  Compute the new
553 	**	ExitStat -- if we already had an error, stick with
554 	**	that.
555 	*/
556 
557 	if (statmsg == NULL)
558 	{
559 		(void) sprintf(buf, "error %d", stat);
560 		statmsg = buf;
561 	}
562 
563 # ifdef LOG
564 	syslog(LOG_INFO, "%s->%s: %ld: %s", From.q_paddr, To, MsgSize, statmsg);
565 # endif LOG
566 	setstat(stat);
567 }
568 /*
569 **  PUTMESSAGE -- output a message to the final mailer.
570 **
571 **	This routine takes care of recreating the header from the
572 **	in-core copy, etc.
573 **
574 **	Parameters:
575 **		fp -- file to output onto.
576 **		m -- a mailer descriptor.
577 **
578 **	Returns:
579 **		none.
580 **
581 **	Side Effects:
582 **		The message is written onto fp.
583 */
584 
585 putmessage(fp, m)
586 	FILE *fp;
587 	struct mailer *m;
588 {
589 	char buf[BUFSIZ];
590 	register int i;
591 	register HDR *h;
592 	extern char *arpadate();
593 	bool anyheader = FALSE;
594 	extern char *capitalize();
595 	extern char *hvalue();
596 	extern bool samefrom();
597 	char *of_line;
598 
599 	/*
600 	**  Output "From" line unless supressed
601 	*/
602 
603 	if (!bitset(M_NHDR, m->m_flags))
604 	{
605 		(void) expand("$l", buf, &buf[sizeof buf - 1]);
606 		fprintf(fp, "%s\n", buf);
607 	}
608 
609 	/*
610 	**  Output all header lines
611 	*/
612 
613 	of_line = hvalue("original-from");
614 	for (h = Header; h != NULL; h = h->h_link)
615 	{
616 		register char *p;
617 		char *origfrom = OrigFrom;
618 		bool nooutput;
619 
620 		nooutput = FALSE;
621 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
622 		{
623 			p = ")><(";		/* can't happen (I hope) */
624 			nooutput = TRUE;
625 		}
626 
627 		/* use From: line from message if generated is the same */
628 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL &&
629 		    strcmp(m->m_from, "$f") == 0 && of_line == NULL)
630 		{
631 			p = origfrom;
632 			origfrom = NULL;
633 		}
634 		else if (bitset(H_DEFAULT, h->h_flags))
635 		{
636 			(void) expand(h->h_value, buf, &buf[sizeof buf]);
637 			p = buf;
638 		}
639 		else
640 			p = h->h_value;
641 		if (p == NULL || *p == '\0')
642 			continue;
643 
644 		/* hack, hack -- output Original-From field if different */
645 		if (strcmp(h->h_field, "from") == 0 && origfrom != NULL)
646 		{
647 			/* output new Original-From line if needed */
648 			if (of_line == NULL && !samefrom(p, origfrom))
649 			{
650 				fprintf(fp, "Original-From: %s\n", origfrom);
651 				anyheader = TRUE;
652 			}
653 			if (of_line != NULL && !nooutput && samefrom(p, of_line))
654 			{
655 				/* delete Original-From: line if redundant */
656 				p = of_line;
657 				of_line = NULL;
658 			}
659 		}
660 		else if (strcmp(h->h_field, "original-from") == 0 && of_line == NULL)
661 			nooutput = TRUE;
662 
663 		/* finally, output the header line */
664 		if (!nooutput)
665 		{
666 			fprintf(fp, "%s: %s\n", capitalize(h->h_field), p);
667 			h->h_flags |= H_USED;
668 			anyheader = TRUE;
669 		}
670 	}
671 	if (anyheader)
672 		fprintf(fp, "\n");
673 
674 	/*
675 	**  Output the body of the message
676 	*/
677 
678 	if (TempFile != NULL)
679 	{
680 		rewind(TempFile);
681 		while (!ferror(fp) && (i = fread(buf, 1, BUFSIZ, TempFile)) > 0)
682 			(void) fwrite(buf, 1, i, fp);
683 
684 		if (ferror(TempFile))
685 		{
686 			syserr("putmessage: read error");
687 			setstat(EX_IOERR);
688 		}
689 	}
690 
691 	fflush(fp);
692 	if (ferror(fp) && errno != EPIPE)
693 	{
694 		syserr("putmessage: write error");
695 		setstat(EX_IOERR);
696 	}
697 	errno = 0;
698 }
699 /*
700 **  SAMEFROM -- tell if two text addresses represent the same from address.
701 **
702 **	Parameters:
703 **		ifrom -- internally generated form of from address.
704 **		efrom -- external form of from address.
705 **
706 **	Returns:
707 **		TRUE -- if they convey the same info.
708 **		FALSE -- if any information has been lost.
709 **
710 **	Side Effects:
711 **		none.
712 */
713 
714 bool
715 samefrom(ifrom, efrom)
716 	char *ifrom;
717 	char *efrom;
718 {
719 	register char *p;
720 	char buf[MAXNAME + 4];
721 
722 # ifdef DEBUG
723 	if (Debug > 7)
724 		printf("samefrom(%s,%s)-->", ifrom, efrom);
725 # endif DEBUG
726 	if (strcmp(ifrom, efrom) == 0)
727 		goto success;
728 	p = index(ifrom, '@');
729 	if (p == NULL)
730 		goto failure;
731 	*p = '\0';
732 	strcpy(buf, ifrom);
733 	strcat(buf, " at ");
734 	*p++ = '@';
735 	strcat(buf, p);
736 	if (strcmp(buf, efrom) == 0)
737 		goto success;
738 
739   failure:
740 # ifdef DEBUG
741 	if (Debug > 7)
742 		printf("FALSE\n");
743 # endif DEBUG
744 	return (FALSE);
745 
746   success:
747 # ifdef DEBUG
748 	if (Debug > 7)
749 		printf("TRUE\n");
750 # endif DEBUG
751 	return (TRUE);
752 }
753 /*
754 **  MAILFILE -- Send a message to a file.
755 **
756 **	If the file has the setuid/setgid bits set, but NO execute
757 **	bits, sendmail will try to become the owner of that file
758 **	rather than the real user.  Obviously, this only works if
759 **	sendmail runs as root.
760 **
761 **	Parameters:
762 **		filename -- the name of the file to send to.
763 **		ctladdr -- the controlling address header -- includes
764 **			the userid/groupid to be when sending.
765 **
766 **	Returns:
767 **		The exit code associated with the operation.
768 **
769 **	Side Effects:
770 **		none.
771 */
772 
773 mailfile(filename, ctladdr)
774 	char *filename;
775 	ADDRESS *ctladdr;
776 {
777 	register FILE *f;
778 	register int pid;
779 
780 	/*
781 	**  Fork so we can change permissions here.
782 	**	Note that we MUST use fork, not vfork, because of
783 	**	the complications of calling subroutines, etc.
784 	*/
785 
786 	DOFORK(fork);
787 
788 	if (pid < 0)
789 		return (EX_OSERR);
790 	else if (pid == 0)
791 	{
792 		/* child -- actually write to file */
793 		struct stat stb;
794 		extern int DefUid, DefGid;
795 
796 		(void) signal(SIGINT, SIG_DFL);
797 		(void) signal(SIGHUP, SIG_DFL);
798 		(void) signal(SIGTERM, SIG_DFL);
799 		umask(OldUmask);
800 		if (stat(filename, &stb) < 0)
801 			stb.st_mode = 0666;
802 		if (bitset(0111, stb.st_mode))
803 			exit(EX_CANTCREAT);
804 		if (ctladdr == NULL)
805 			ctladdr = &From;
806 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
807 		{
808 			if (ctladdr->q_uid == 0)
809 				(void) setgid(DefGid);
810 			else
811 				(void) setgid(ctladdr->q_gid);
812 		}
813 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
814 		{
815 			if (ctladdr->q_uid == 0)
816 				(void) setuid(DefUid);
817 			else
818 				(void) setuid(ctladdr->q_uid);
819 		}
820 		f = fopen(filename, "a");
821 		if (f == NULL)
822 			exit(EX_CANTCREAT);
823 
824 		putmessage(f, Mailer[1]);
825 		fputs("\n", f);
826 		(void) fclose(f);
827 		(void) fflush(stdout);
828 
829 		/* reset ISUID & ISGID bits */
830 		(void) chmod(filename, stb.st_mode);
831 		exit(EX_OK);
832 		/*NOTREACHED*/
833 	}
834 	else
835 	{
836 		/* parent -- wait for exit status */
837 		register int i;
838 		auto int stat;
839 
840 		while ((i = wait(&stat)) != pid)
841 		{
842 			if (i < 0)
843 			{
844 				stat = EX_OSERR << 8;
845 				break;
846 			}
847 		}
848 		if ((stat & 0377) != 0)
849 			stat = EX_UNAVAILABLE << 8;
850 		return ((stat >> 8) & 0377);
851 	}
852 }
853 /*
854 **  SENDALL -- actually send all the messages.
855 **
856 **	Parameters:
857 **		verifyonly -- if set, only give verification messages.
858 **
859 **	Returns:
860 **		none.
861 **
862 **	Side Effects:
863 **		Scans the send lists and sends everything it finds.
864 */
865 
866 sendall(verifyonly)
867 	bool verifyonly;
868 {
869 	register int i;
870 	typedef int (*fnptr)();
871 
872 	for (i = 0; Mailer[i] != NULL; i++)
873 	{
874 		ADDRESS *q;
875 
876 		for (q = Mailer[i]->m_sendq; q != NULL; q = q->q_next)
877 		{
878 			if (verifyonly)
879 			{
880 				To = q->q_paddr;
881 				if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
882 				{
883 					if (q->q_mailer == MN_LOCAL || q->q_mailer == MN_PROG)
884 						message(Arpa_Info, "deliverable");
885 					else
886 						message(Arpa_Info, "queueable");
887 				}
888 			}
889 			else
890 				(void) deliver(q, (fnptr) NULL);
891 		}
892 	}
893 }
894