1 # include <signal.h>
2 # include <errno.h>
3 # include "sendmail.h"
4 # include <sys/stat.h>
5 
6 SCCSID(@(#)deliver.c	3.130		11/18/82);
7 
8 /*
9 **  DELIVER -- Deliver a message to a list of addresses.
10 **
11 **	This routine delivers to everyone on the same host as the
12 **	user on the head of the list.  It is clever about mailers
13 **	that don't handle multiple users.  It is NOT guaranteed
14 **	that it will deliver to all these addresses however -- so
15 **	deliver should be called once for each address on the
16 **	list.
17 **
18 **	Parameters:
19 **		firstto -- head of the address list to deliver to.
20 **
21 **	Returns:
22 **		zero -- successfully delivered.
23 **		else -- some failure, see ExitStat for more info.
24 **
25 **	Side Effects:
26 **		The standard input is passed off to someone.
27 */
28 
29 deliver(firstto)
30 	ADDRESS *firstto;
31 {
32 	char *host;			/* host being sent to */
33 	char *user;			/* user being sent to */
34 	char **pvp;
35 	register char **mvp;
36 	register char *p;
37 	register struct mailer *m;	/* mailer for this recipient */
38 	extern bool checkcompat();
39 	char *pv[MAXPV+1];
40 	char tobuf[MAXLINE-50];		/* text line of to people */
41 	char buf[MAXNAME];
42 	ADDRESS *ctladdr;
43 	extern ADDRESS *getctladdr();
44 	char tfrombuf[MAXNAME];		/* translated from person */
45 	extern char **prescan();
46 	register ADDRESS *to = firstto;
47 	bool clever = FALSE;		/* running user smtp to this mailer */
48 	ADDRESS *tochain = NULL;	/* chain of users in this mailer call */
49 	bool notopen = TRUE;		/* set if connection not quite open */
50 	register int rcode;		/* response code */
51 
52 	errno = 0;
53 	if (bitset(QDONTSEND, to->q_flags))
54 		return (0);
55 
56 	m = to->q_mailer;
57 	host = to->q_host;
58 
59 # ifdef DEBUG
60 	if (tTd(10, 1))
61 		printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n",
62 			m->m_mno, host, to->q_user);
63 # endif DEBUG
64 
65 	/*
66 	**  If this mailer is expensive, and if we don't want to make
67 	**  connections now, just mark these addresses and return.
68 	**	This is useful if we want to batch connections to
69 	**	reduce load.  This will cause the messages to be
70 	**	queued up, and a daemon will come along to send the
71 	**	messages later.
72 	**		This should be on a per-mailer basis.
73 	*/
74 
75 	if (NoConnect && !QueueRun && bitset(M_EXPENSIVE, m->m_flags) &&
76 	    !Verbose)
77 	{
78 		for (; to != NULL; to = to->q_next)
79 		{
80 			if (bitset(QDONTSEND, to->q_flags) || to->q_mailer != m)
81 				continue;
82 			to->q_flags |= QQUEUEUP|QDONTSEND;
83 			CurEnv->e_to = to->q_paddr;
84 			message(Arpa_Info, "queued");
85 			if (LogLevel > 4)
86 				logdelivery("queued");
87 		}
88 		CurEnv->e_to = NULL;
89 		return (0);
90 	}
91 
92 	/*
93 	**  Do initial argv setup.
94 	**	Insert the mailer name.  Notice that $x expansion is
95 	**	NOT done on the mailer name.  Then, if the mailer has
96 	**	a picky -f flag, we insert it as appropriate.  This
97 	**	code does not check for 'pv' overflow; this places a
98 	**	manifest lower limit of 4 for MAXPV.
99 	**		The from address rewrite is expected to make
100 	**		the address relative to the other end.
101 	*/
102 
103 	/* rewrite from address, using rewriting rules */
104 	expand("$f", buf, &buf[sizeof buf - 1], CurEnv);
105 	mvp = prescan(buf, '\0');
106 	rewrite(mvp, m->m_s_rwset);
107 	cataddr(mvp, tfrombuf, sizeof tfrombuf);
108 
109 	define('g', tfrombuf);		/* translated sender address */
110 	define('h', host);		/* to host */
111 	Errors = 0;
112 	pvp = pv;
113 	*pvp++ = m->m_argv[0];
114 
115 	/* insert -f or -r flag as appropriate */
116 	if (bitset(M_FOPT|M_ROPT, m->m_flags) && FromFlag)
117 	{
118 		if (bitset(M_FOPT, m->m_flags))
119 			*pvp++ = "-f";
120 		else
121 			*pvp++ = "-r";
122 		expand("$g", buf, &buf[sizeof buf - 1], CurEnv);
123 		*pvp++ = newstr(buf);
124 	}
125 
126 	/*
127 	**  Append the other fixed parts of the argv.  These run
128 	**  up to the first entry containing "$u".  There can only
129 	**  be one of these, and there are only a few more slots
130 	**  in the pv after it.
131 	*/
132 
133 	for (mvp = m->m_argv; (p = *++mvp) != NULL; )
134 	{
135 		while ((p = index(p, '$')) != NULL)
136 			if (*++p == 'u')
137 				break;
138 		if (p != NULL)
139 			break;
140 
141 		/* this entry is safe -- go ahead and process it */
142 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
143 		*pvp++ = newstr(buf);
144 		if (pvp >= &pv[MAXPV - 3])
145 		{
146 			syserr("Too many parameters to %s before $u", pv[0]);
147 			return (-1);
148 		}
149 	}
150 
151 	/*
152 	**  If we have no substitution for the user name in the argument
153 	**  list, we know that we must supply the names otherwise -- and
154 	**  SMTP is the answer!!
155 	*/
156 
157 	if (*mvp == NULL)
158 	{
159 		/* running SMTP */
160 # ifdef SMTP
161 		clever = TRUE;
162 		*pvp = NULL;
163 # else SMTP
164 		/* oops!  we don't implement SMTP */
165 		syserr("SMTP style mailer");
166 		return (EX_SOFTWARE);
167 # endif SMTP
168 	}
169 
170 	/*
171 	**  At this point *mvp points to the argument with $u.  We
172 	**  run through our address list and append all the addresses
173 	**  we can.  If we run out of space, do not fret!  We can
174 	**  always send another copy later.
175 	*/
176 
177 	tobuf[0] = '\0';
178 	CurEnv->e_to = tobuf;
179 	ctladdr = NULL;
180 	for (; to != NULL; to = to->q_next)
181 	{
182 		/* avoid sending multiple recipients to dumb mailers */
183 		if (tobuf[0] != '\0' && !bitset(M_MUSER, m->m_flags))
184 			break;
185 
186 		/* if already sent or not for this host, don't send */
187 		if (bitset(QDONTSEND, to->q_flags) ||
188 		    strcmp(to->q_host, host) != 0 ||
189 		    to->q_mailer != firstto->q_mailer)
190 			continue;
191 
192 		/* avoid overflowing tobuf */
193 		if (sizeof tobuf - (strlen(to->q_paddr) + strlen(tobuf) + 1) < 0)
194 			break;
195 
196 # ifdef DEBUG
197 		if (tTd(10, 1))
198 		{
199 			printf("\nsend to ");
200 			printaddr(to, FALSE);
201 		}
202 # endif DEBUG
203 
204 		/* compute effective uid/gid when sending */
205 		if (to->q_mailer == ProgMailer)
206 			ctladdr = getctladdr(to);
207 
208 		user = to->q_user;
209 		CurEnv->e_to = to->q_paddr;
210 		to->q_flags |= QDONTSEND;
211 
212 		/*
213 		**  Check to see that these people are allowed to
214 		**  talk to each other.
215 		*/
216 
217 		if (!checkcompat(to))
218 		{
219 			giveresponse(EX_UNAVAILABLE, TRUE, m);
220 			continue;
221 		}
222 
223 		/*
224 		**  Strip quote bits from names if the mailer is dumb
225 		**	about them.
226 		*/
227 
228 		if (bitset(M_STRIPQ, m->m_flags))
229 		{
230 			stripquotes(user, TRUE);
231 			stripquotes(host, TRUE);
232 		}
233 		else
234 		{
235 			stripquotes(user, FALSE);
236 			stripquotes(host, FALSE);
237 		}
238 
239 		/* hack attack -- delivermail compatibility */
240 		if (m == ProgMailer && *user == '|')
241 			user++;
242 
243 		/*
244 		**  Do initial connection setup if needed.
245 		*/
246 
247 		if (notopen)
248 		{
249 			message(Arpa_Info, "Connecting to %s.%s...", host, m->m_name);
250 # ifdef SMTP
251 			if (clever)
252 			{
253 				/* send the initial SMTP protocol */
254 				rcode = smtpinit(m, pv, (ADDRESS *) NULL);
255 			}
256 # ifdef SMTP
257 			notopen = FALSE;
258 		}
259 
260 		/*
261 		**  Pass it to the other host if we are running SMTP.
262 		*/
263 
264 		if (clever)
265 		{
266 # ifdef SMTP
267 			if (rcode == EX_OK)
268 				rcode = smtprcpt(to);
269 			if (rcode != EX_OK)
270 			{
271 				if (rcode == EX_TEMPFAIL)
272 					to->q_flags |= QQUEUEUP;
273 				else
274 					to->q_flags |= QBADADDR;
275 				giveresponse(rcode, TRUE, m);
276 			}
277 # else SMTP
278 			syserr("trying to be clever");
279 # endif SMTP
280 		}
281 
282 		/*
283 		**  If an error message has already been given, don't
284 		**	bother to send to this address.
285 		**
286 		**	>>>>>>>>>> This clause assumes that the local mailer
287 		**	>> NOTE >> cannot do any further aliasing; that
288 		**	>>>>>>>>>> function is subsumed by sendmail.
289 		*/
290 
291 		if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
292 			continue;
293 
294 		/* save statistics.... */
295 		Stat.stat_nt[to->q_mailer->m_mno]++;
296 		Stat.stat_bt[to->q_mailer->m_mno] += kbytes(CurEnv->e_msgsize);
297 
298 		/*
299 		**  See if this user name is "special".
300 		**	If the user name has a slash in it, assume that this
301 		**	is a file -- send it off without further ado.  Note
302 		**	that this type of addresses is not processed along
303 		**	with the others, so we fudge on the To person.
304 		*/
305 
306 		if (m == LocalMailer)
307 		{
308 			if (user[0] == '/')
309 			{
310 				rcode = mailfile(user, getctladdr(to));
311 				giveresponse(rcode, TRUE, m);
312 				continue;
313 			}
314 		}
315 
316 		/*
317 		**  Address is verified -- add this user to mailer
318 		**  argv, and add it to the print list of recipients.
319 		*/
320 
321 		/* link together the chain of recipients */
322 		to->q_tchain = tochain;
323 		tochain = to;
324 
325 		/* create list of users for error messages */
326 		if (tobuf[0] != '\0')
327 			(void) strcat(tobuf, ",");
328 		(void) strcat(tobuf, to->q_paddr);
329 		define('u', user);		/* to user */
330 		define('z', to->q_home);	/* user's home */
331 
332 		/*
333 		**  Expand out this user into argument list.
334 		*/
335 
336 		if (!clever)
337 		{
338 			expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
339 			*pvp++ = newstr(buf);
340 			if (pvp >= &pv[MAXPV - 2])
341 			{
342 				/* allow some space for trailing parms */
343 				break;
344 			}
345 		}
346 	}
347 
348 	/* see if any addresses still exist */
349 	if (tobuf[0] == '\0')
350 	{
351 # ifdef SMTP
352 		if (clever && !notopen)
353 			smtpquit(pv[0], FALSE);
354 # endif SMTP
355 		define('g', (char *) NULL);
356 		return (0);
357 	}
358 
359 	/* print out messages as full list */
360 	CurEnv->e_to = tobuf;
361 
362 	/*
363 	**  Fill out any parameters after the $u parameter.
364 	*/
365 
366 	while (!clever && *++mvp != NULL)
367 	{
368 		expand(*mvp, buf, &buf[sizeof buf - 1], CurEnv);
369 		*pvp++ = newstr(buf);
370 		if (pvp >= &pv[MAXPV])
371 			syserr("deliver: pv overflow after $u for %s", pv[0]);
372 	}
373 	*pvp++ = NULL;
374 
375 	/*
376 	**  Call the mailer.
377 	**	The argument vector gets built, pipes
378 	**	are created as necessary, and we fork & exec as
379 	**	appropriate.
380 	**	If we are running SMTP, we just need to clean up.
381 	*/
382 
383 	if (ctladdr == NULL)
384 		ctladdr = &CurEnv->e_from;
385 # ifdef SMTP
386 	if (clever)
387 	{
388 		rcode = smtpfinish(m, CurEnv);
389 		if (rcode != EX_OK)
390 			giveresponse(rcode, TRUE, m);
391 		smtpquit(pv[0], rcode == EX_OK);
392 	}
393 	else
394 # endif SMTP
395 		rcode = sendoff(m, pv, ctladdr);
396 
397 	/*
398 	**  If we got a temporary failure, arrange to queue the
399 	**  addressees.
400 	*/
401 
402 	if (rcode == EX_TEMPFAIL)
403 	{
404 		for (to = tochain; to != NULL; to = to->q_tchain)
405 			to->q_flags |= QQUEUEUP;
406 	}
407 
408 	errno = 0;
409 	define('g', (char *) NULL);
410 	return (rcode);
411 }
412 /*
413 **  DOFORK -- do a fork, retrying a couple of times on failure.
414 **
415 **	This MUST be a macro, since after a vfork we are running
416 **	two processes on the same stack!!!
417 **
418 **	Parameters:
419 **		none.
420 **
421 **	Returns:
422 **		From a macro???  You've got to be kidding!
423 **
424 **	Side Effects:
425 **		Modifies the ==> LOCAL <== variable 'pid', leaving:
426 **			pid of child in parent, zero in child.
427 **			-1 on unrecoverable error.
428 **
429 **	Notes:
430 **		I'm awfully sorry this looks so awful.  That's
431 **		vfork for you.....
432 */
433 
434 # define NFORKTRIES	5
435 # ifdef VMUNIX
436 # define XFORK	vfork
437 # else VMUNIX
438 # define XFORK	fork
439 # endif VMUNIX
440 
441 # define DOFORK(fORKfN) \
442 {\
443 	register int i;\
444 \
445 	for (i = NFORKTRIES; i-- > 0; )\
446 	{\
447 		pid = fORKfN();\
448 		if (pid >= 0)\
449 			break;\
450 		sleep(NFORKTRIES - i);\
451 	}\
452 }
453 /*
454 **  DOFORK -- simple fork interface to DOFORK.
455 **
456 **	Parameters:
457 **		none.
458 **
459 **	Returns:
460 **		pid of child in parent.
461 **		zero in child.
462 **		-1 on error.
463 **
464 **	Side Effects:
465 **		returns twice, once in parent and once in child.
466 */
467 
468 dofork()
469 {
470 	register int pid;
471 
472 	DOFORK(fork);
473 	return (pid);
474 }
475 /*
476 **  SENDOFF -- send off call to mailer & collect response.
477 **
478 **	Parameters:
479 **		m -- mailer descriptor.
480 **		pvp -- parameter vector to send to it.
481 **		ctladdr -- an address pointer controlling the
482 **			user/groupid etc. of the mailer.
483 **
484 **	Returns:
485 **		exit status of mailer.
486 **
487 **	Side Effects:
488 **		none.
489 */
490 
491 sendoff(m, pvp, ctladdr)
492 	struct mailer *m;
493 	char **pvp;
494 	ADDRESS *ctladdr;
495 {
496 	auto FILE *mfile;
497 	auto FILE *rfile;
498 	register int i;
499 	int pid;
500 
501 	/*
502 	**  Create connection to mailer.
503 	*/
504 
505 	pid = openmailer(m, pvp, ctladdr, FALSE, &mfile, &rfile);
506 	if (pid < 0)
507 		return (-1);
508 
509 	/*
510 	**  Format and send message.
511 	*/
512 
513 	(void) signal(SIGPIPE, SIG_IGN);
514 	putfromline(mfile, m);
515 	(*CurEnv->e_puthdr)(mfile, m, CurEnv);
516 	fprintf(mfile, "\n");
517 	(*CurEnv->e_putbody)(mfile, m, FALSE);
518 	(void) fclose(mfile);
519 
520 	i = endmailer(pid, pvp[0]);
521 	giveresponse(i, TRUE, m);
522 
523 	/* arrange a return receipt if requested */
524 	if (CurEnv->e_receiptto != NULL && bitset(M_LOCAL, m->m_flags))
525 	{
526 		CurEnv->e_sendreceipt = TRUE;
527 		if (ExitStat == EX_OK)
528 			fprintf(Xscript, "%s... successfully delivered\n",
529 				CurEnv->e_to);
530 		/* do we want to send back more info? */
531 	}
532 
533 	return (i);
534 }
535 /*
536 **  ENDMAILER -- Wait for mailer to terminate.
537 **
538 **	We should never get fatal errors (e.g., segmentation
539 **	violation), so we report those specially.  For other
540 **	errors, we choose a status message (into statmsg),
541 **	and if it represents an error, we print it.
542 **
543 **	Parameters:
544 **		pid -- pid of mailer.
545 **		name -- name of mailer (for error messages).
546 **
547 **	Returns:
548 **		exit code of mailer.
549 **
550 **	Side Effects:
551 **		none.
552 */
553 
554 endmailer(pid, name)
555 	int pid;
556 	char *name;
557 {
558 	register int i;
559 	auto int st;
560 
561 	/* in the IPC case there is nothing to wait for */
562 	if (pid == 0)
563 		return (EX_OK);
564 
565 	/* wait for the mailer process to die and collect status */
566 	do
567 	{
568 		errno = 0;
569 		i = wait(&st);
570 	} while ((i >= 0 && i != pid) || errno == EINTR);
571 	if (i < 0)
572 	{
573 		syserr("wait");
574 		return (-1);
575 	}
576 
577 	/* see if it died a horrid death */
578 	if ((st & 0377) != 0)
579 	{
580 		syserr("%s: stat %o", name, st);
581 		ExitStat = EX_UNAVAILABLE;
582 		return (-1);
583 	}
584 
585 	/* normal death -- return status */
586 	i = (st >> 8) & 0377;
587 	return (i);
588 }
589 /*
590 **  OPENMAILER -- open connection to mailer.
591 **
592 **	Parameters:
593 **		m -- mailer descriptor.
594 **		pvp -- parameter vector to pass to mailer.
595 **		ctladdr -- controlling address for user.
596 **		clever -- create a full duplex connection.
597 **		pmfile -- pointer to mfile (to mailer) connection.
598 **		prfile -- pointer to rfile (from mailer) connection.
599 **
600 **	Returns:
601 **		pid of mailer ( > 0 ).
602 **		-1 on error.
603 **		zero on an IPC connection.
604 **
605 **	Side Effects:
606 **		creates a mailer in a subprocess.
607 */
608 
609 openmailer(m, pvp, ctladdr, clever, pmfile, prfile)
610 	struct mailer *m;
611 	char **pvp;
612 	ADDRESS *ctladdr;
613 	bool clever;
614 	FILE **pmfile;
615 	FILE **prfile;
616 {
617 	int pid;
618 	int mpvect[2];
619 	int rpvect[2];
620 	FILE *mfile;
621 	FILE *rfile;
622 	extern FILE *fdopen();
623 
624 # ifdef DEBUG
625 	if (tTd(11, 1))
626 	{
627 		printf("openmailer:");
628 		printav(pvp);
629 	}
630 # endif DEBUG
631 	errno = 0;
632 
633 # ifdef DAEMON
634 	/*
635 	**  Deal with the special case of mail handled through an IPC
636 	**  connection.
637 	**	In this case we don't actually fork.  We must be
638 	**	running SMTP for this to work.  We will return a
639 	**	zero pid to indicate that we are running IPC.
640 	*/
641 
642 	if (strcmp(m->m_mailer, "[IPC]") == 0)
643 	{
644 		register int i;
645 		register u_short port;
646 
647 		if (!clever)
648 			syserr("non-clever IPC");
649 		if (pvp[2] != NULL)
650 			port = atoi(pvp[2]);
651 		else
652 			port = 0;
653 		i = makeconnection(pvp[1], port, pmfile, prfile);
654 		if (i != EX_OK)
655 		{
656 			ExitStat = i;
657 			return (-1);
658 		}
659 		else
660 			return (0);
661 	}
662 # endif DAEMON
663 
664 	/* create a pipe to shove the mail through */
665 	if (pipe(mpvect) < 0)
666 	{
667 		syserr("pipe (to mailer)");
668 		return (-1);
669 	}
670 
671 # ifdef SMTP
672 	/* if this mailer speaks smtp, create a return pipe */
673 	if (clever && pipe(rpvect) < 0)
674 	{
675 		syserr("pipe (from mailer)");
676 		(void) close(mpvect[0]);
677 		(void) close(mpvect[1]);
678 		return (-1);
679 	}
680 # endif SMTP
681 
682 	/*
683 	**  Actually fork the mailer process.
684 	**	DOFORK is clever about retrying.
685 	*/
686 
687 	(void) fflush(Xscript);				/* for debugging */
688 	DOFORK(XFORK);
689 	/* pid is set by DOFORK */
690 	if (pid < 0)
691 	{
692 		/* failure */
693 		syserr("Cannot fork");
694 		(void) close(mpvect[0]);
695 		(void) close(mpvect[1]);
696 		if (clever)
697 		{
698 			(void) close(rpvect[0]);
699 			(void) close(rpvect[1]);
700 		}
701 		return (-1);
702 	}
703 	else if (pid == 0)
704 	{
705 		/* child -- set up input & exec mailer */
706 		/* make diagnostic output be standard output */
707 		(void) signal(SIGINT, SIG_IGN);
708 		(void) signal(SIGHUP, SIG_IGN);
709 		(void) signal(SIGTERM, SIG_DFL);
710 
711 		/* arrange to filter standard & diag output of command */
712 		if (clever)
713 		{
714 			(void) close(rpvect[0]);
715 			(void) close(1);
716 			(void) dup(rpvect[1]);
717 			(void) close(rpvect[1]);
718 		}
719 		else if (OpMode == MD_SMTP || HoldErrs)
720 		{
721 			(void) close(1);
722 			(void) dup(fileno(Xscript));
723 		}
724 		(void) close(2);
725 		(void) dup(1);
726 
727 		/* arrange to get standard input */
728 		(void) close(mpvect[1]);
729 		(void) close(0);
730 		if (dup(mpvect[0]) < 0)
731 		{
732 			syserr("Cannot dup to zero!");
733 			_exit(EX_OSERR);
734 		}
735 		(void) close(mpvect[0]);
736 		if (!bitset(M_RESTR, m->m_flags))
737 		{
738 			if (ctladdr->q_uid == 0)
739 			{
740 				(void) setgid(DefGid);
741 				(void) setuid(DefUid);
742 			}
743 			else
744 			{
745 				(void) setgid(ctladdr->q_gid);
746 				(void) setuid(ctladdr->q_uid);
747 			}
748 		}
749 # ifndef VMUNIX
750 		/*
751 		**  We have to be careful with vfork - we can't mung up the
752 		**  memory but we don't want the mailer to inherit any extra
753 		**  open files.  Chances are the mailer won't
754 		**  care about an extra file, but then again you never know.
755 		**  Actually, we would like to close(fileno(pwf)), but it's
756 		**  declared static so we can't.  But if we fclose(pwf), which
757 		**  is what endpwent does, it closes it in the parent too and
758 		**  the next getpwnam will be slower.  If you have a weird
759 		**  mailer that chokes on the extra file you should do the
760 		**  endpwent().			-MRH
761 		**
762 		**  Similar comments apply to log.  However, openlog is
763 		**  clever enough to set the FIOCLEX mode on the file,
764 		**  so it will be closed automatically on the exec.
765 		*/
766 
767 		endpwent();
768 # ifdef LOG
769 		closelog();
770 # endif LOG
771 # endif VMUNIX
772 
773 		/* try to execute the mailer */
774 		execv(m->m_mailer, pvp);
775 
776 		/* syserr fails because log is closed */
777 		/* syserr("Cannot exec %s", m->m_mailer); */
778 		printf("Cannot exec '%s' errno=%d\n", m->m_mailer, errno);
779 		(void) fflush(stdout);
780 		_exit(EX_UNAVAILABLE);
781 	}
782 
783 	/*
784 	**  Set up return value.
785 	*/
786 
787 	(void) close(mpvect[0]);
788 	mfile = fdopen(mpvect[1], "w");
789 	if (clever)
790 	{
791 		(void) close(rpvect[1]);
792 		rfile = fdopen(rpvect[0], "r");
793 	}
794 
795 	*pmfile = mfile;
796 	*prfile = rfile;
797 
798 	return (pid);
799 }
800 /*
801 **  GIVERESPONSE -- Interpret an error response from a mailer
802 **
803 **	Parameters:
804 **		stat -- the status code from the mailer (high byte
805 **			only; core dumps must have been taken care of
806 **			already).
807 **		force -- if set, force an error message output, even
808 **			if the mailer seems to like to print its own
809 **			messages.
810 **		m -- the mailer descriptor for this mailer.
811 **
812 **	Returns:
813 **		none.
814 **
815 **	Side Effects:
816 **		Errors may be incremented.
817 **		ExitStat may be set.
818 */
819 
820 giveresponse(stat, force, m)
821 	int stat;
822 	bool force;
823 	register struct mailer *m;
824 {
825 	register char *statmsg;
826 	extern char *SysExMsg[];
827 	register int i;
828 	extern int N_SysEx;
829 	char buf[30];
830 
831 	/*
832 	**  Compute status message from code.
833 	*/
834 
835 	i = stat - EX__BASE;
836 	if (i < 0 || i > N_SysEx)
837 		statmsg = NULL;
838 	else
839 		statmsg = SysExMsg[i];
840 	if (stat == 0)
841 	{
842 		statmsg = "250 sent";
843 		message(Arpa_Info, &statmsg[4]);
844 	}
845 	else if (stat == EX_TEMPFAIL)
846 	{
847 		message(Arpa_Info, "deferred");
848 	}
849 	else
850 	{
851 		Errors++;
852 		FatalErrors = TRUE;
853 		if (statmsg == NULL && m->m_badstat != 0)
854 		{
855 			stat = m->m_badstat;
856 			i = stat - EX__BASE;
857 # ifdef DEBUG
858 			if (i < 0 || i >= N_SysEx)
859 				syserr("Bad m_badstat %d", stat);
860 			else
861 # endif DEBUG
862 				statmsg = SysExMsg[i];
863 		}
864 		if (statmsg == NULL)
865 			usrerr("unknown mailer response %d", stat);
866 		else if (force || !bitset(M_QUIET, m->m_flags) || Verbose)
867 			usrerr(statmsg);
868 		else
869 			fprintf(Xscript, "%s\n", &statmsg[4]);
870 	}
871 
872 	/*
873 	**  Final cleanup.
874 	**	Log a record of the transaction.  Compute the new
875 	**	ExitStat -- if we already had an error, stick with
876 	**	that.
877 	*/
878 
879 	if (statmsg == NULL)
880 	{
881 		(void) sprintf(buf, "554 error %d", stat);
882 		statmsg = buf;
883 	}
884 
885 	/* log it in the system log */
886 	if (LogLevel > ((stat == 0 || stat == EX_TEMPFAIL) ? 3 : 2))
887 		logdelivery(&statmsg[4]);
888 
889 	/* set the exit status appropriately */
890 	if (stat != EX_TEMPFAIL)
891 		setstat(stat);
892 }
893 /*
894 **  LOGDELIVERY -- log the delivery in the system log
895 **
896 **	Parameters:
897 **		stat -- the message to print for the status
898 **
899 **	Returns:
900 **		none
901 **
902 **	Side Effects:
903 **		none
904 */
905 
906 logdelivery(stat)
907 	char *stat;
908 {
909 	extern char *pintvl();
910 
911 # ifdef LOG
912 	syslog(LOG_INFO, "%s: to=%s, delay=%s, stat=%s", CurEnv->e_id,
913 	       CurEnv->e_to, pintvl(curtime() - CurEnv->e_ctime, TRUE), stat);
914 # endif LOG
915 }
916 /*
917 **  PUTFROMLINE -- output a UNIX-style from line (or whatever)
918 **
919 **	This can be made an arbitrary message separator by changing $l
920 **
921 **	One of the ugliest hacks seen by human eyes is
922 **	contained herein: UUCP wants those stupid
923 **	"remote from <host>" lines.  Why oh why does a
924 **	well-meaning programmer such as myself have to
925 **	deal with this kind of antique garbage????
926 **
927 **	Parameters:
928 **		fp -- the file to output to.
929 **		m -- the mailer describing this entry.
930 **
931 **	Returns:
932 **		none
933 **
934 **	Side Effects:
935 **		outputs some text to fp.
936 */
937 
938 putfromline(fp, m)
939 	register FILE *fp;
940 	register MAILER *m;
941 {
942 	char buf[MAXLINE];
943 
944 	if (bitset(M_NHDR, m->m_flags))
945 		return;
946 
947 # ifdef UGLYUUCP
948 	if (bitset(M_UGLYUUCP, m->m_flags))
949 	{
950 		extern char *macvalue();
951 		char *sys = macvalue('g', CurEnv);
952 		char *bang = index(sys, '!');
953 
954 		if (bang == NULL)
955 			syserr("No ! in UUCP! (%s)", sys);
956 		else
957 			*bang = '\0';
958 		expand("From $f  $d remote from $g\n", buf,
959 				&buf[sizeof buf - 1], CurEnv);
960 		*bang = '!';
961 	}
962 	else
963 # endif UGLYUUCP
964 		expand("$l\n", buf, &buf[sizeof buf - 1], CurEnv);
965 	putline(buf, fp, bitset(M_FULLSMTP, m->m_flags));
966 }
967 /*
968 **  PUTHEADER -- put the header part of a message from the in-core copy
969 **
970 **	Parameters:
971 **		fp -- file to put it on.
972 **		m -- mailer to use.
973 **		e -- envelope to use.
974 **
975 **	Returns:
976 **		none.
977 **
978 **	Side Effects:
979 **		none.
980 */
981 
982 putheader(fp, m, e)
983 	register FILE *fp;
984 	register struct mailer *m;
985 	register ENVELOPE *e;
986 {
987 	char buf[BUFSIZ];
988 	register HDR *h;
989 	extern char *arpadate();
990 	extern char *capitalize();
991 	extern bool samefrom();
992 	char obuf[MAXLINE];
993 	register char *obp;
994 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
995 
996 	if (bitset(M_LOCAL, m->m_flags) && fullsmtp)
997 		fprintf(fp, "Return-Path: <%s>\n", e->e_from);
998 	for (h = e->e_header; h != NULL; h = h->h_link)
999 	{
1000 		register char *p;
1001 
1002 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) && !bitset(h->h_mflags, m->m_flags))
1003 			continue;
1004 
1005 		p = h->h_value;
1006 		if (bitset(H_DEFAULT, h->h_flags))
1007 		{
1008 			/* macro expand value if generated internally */
1009 			expand(p, buf, &buf[sizeof buf], e);
1010 			p = buf;
1011 		}
1012 		if (p == NULL || *p == '\0')
1013 			continue;
1014 
1015 		if (bitset(H_FROM|H_RCPT, h->h_flags))
1016 		{
1017 			/* address field */
1018 			bool oldstyle = e->e_oldstyle;
1019 
1020 			if (bitset(H_FROM, h->h_flags))
1021 				oldstyle = FALSE;
1022 			commaize(h, p, fp, oldstyle, m);
1023 		}
1024 		else
1025 		{
1026 			/* vanilla header line */
1027 			(void) sprintf(obuf, "%s: %s\n", capitalize(h->h_field), p);
1028 			putline(obuf, fp, fullsmtp);
1029 		}
1030 		h->h_flags |= H_USED;
1031 	}
1032 }
1033 /*
1034 **  COMMAIZE -- output a header field, making a comma-translated list.
1035 **
1036 **	Parameters:
1037 **		h -- the header field to output.
1038 **		p -- the value to put in it.
1039 **		fp -- file to put it to.
1040 **		oldstyle -- TRUE if this is an old style header.
1041 **		m -- a pointer to the mailer descriptor.  If NULL,
1042 **			don't transform the name at all.
1043 **
1044 **	Returns:
1045 **		none.
1046 **
1047 **	Side Effects:
1048 **		outputs "p" to file "fp".
1049 */
1050 
1051 commaize(h, p, fp, oldstyle, m)
1052 	register HDR *h;
1053 	register char *p;
1054 	FILE *fp;
1055 	bool oldstyle;
1056 	register MAILER *m;
1057 {
1058 	register char *obp;
1059 	int opos;
1060 	bool fullsmtp = FALSE;
1061 	bool firstone = TRUE;
1062 	char obuf[MAXLINE];
1063 
1064 	/*
1065 	**  Output the address list translated by the
1066 	**  mailer and with commas.
1067 	*/
1068 
1069 # ifdef DEBUG
1070 	if (tTd(14, 2))
1071 		printf("commaize(%s: %s)\n", h->h_field, p);
1072 # endif DEBUG
1073 
1074 	if (m != NULL && bitset(M_FULLSMTP, m->m_flags))
1075 		fullsmtp = TRUE;
1076 
1077 	obp = obuf;
1078 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
1079 	opos = strlen(h->h_field) + 2;
1080 	obp += opos;
1081 
1082 	/*
1083 	**  Run through the list of values.
1084 	*/
1085 
1086 	while (*p != '\0')
1087 	{
1088 		register char *name;
1089 		char savechar;
1090 		extern char *remotename();
1091 		extern char *DelimChar;		/* defined in prescan */
1092 
1093 		/*
1094 		**  Find the end of the name.  New style names
1095 		**  end with a comma, old style names end with
1096 		**  a space character.  However, spaces do not
1097 		**  necessarily delimit an old-style name -- at
1098 		**  signs mean keep going.
1099 		*/
1100 
1101 		/* find end of name */
1102 		while (isspace(*p) || *p == ',')
1103 			p++;
1104 		name = p;
1105 		for (;;)
1106 		{
1107 			char *oldp;
1108 			extern bool isatword();
1109 
1110 			(void) prescan(p, oldstyle ? ' ' : ',');
1111 			p = DelimChar;
1112 
1113 			/* look to see if we have an at sign */
1114 			oldp = p;
1115 			while (*p != '\0' && isspace(*p))
1116 				p++;
1117 
1118 			if (*p != '@' && !isatword(p))
1119 			{
1120 				p = oldp;
1121 				break;
1122 			}
1123 			p += *p == '@' ? 1 : 2;
1124 			while (*p != '\0' && isspace(*p))
1125 				p++;
1126 		}
1127 		/* at the end of one complete name */
1128 
1129 		/* strip off trailing white space */
1130 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
1131 			p--;
1132 		if (++p == name)
1133 			continue;
1134 		savechar = *p;
1135 		*p = '\0';
1136 
1137 		/* translate the name to be relative */
1138 		if (m != NULL)
1139 			name = remotename(name, m, bitset(H_FROM, h->h_flags));
1140 		if (*name == '\0')
1141 		{
1142 			*p = savechar;
1143 			continue;
1144 		}
1145 
1146 		/* output the name with nice formatting */
1147 		opos += qstrlen(name);
1148 		if (!firstone)
1149 			opos += 2;
1150 		if (opos > 78 && !firstone)
1151 		{
1152 			(void) sprintf(obp, ",\n");
1153 			putline(obuf, fp, fullsmtp);
1154 			obp = obuf;
1155 			(void) sprintf(obp, "        ");
1156 			obp += strlen(obp);
1157 			opos = 8 + strlen(name);
1158 		}
1159 		else if (!firstone)
1160 		{
1161 			(void) sprintf(obp, ", ");
1162 			obp += 2;
1163 		}
1164 
1165 		/* strip off quote bits as we output */
1166 		while (*name != '\0')
1167 		{
1168 			if (bitset(0200, *name))
1169 				*obp++ = '\\';
1170 			*obp++ = *name++ & ~0200;
1171 		}
1172 		firstone = FALSE;
1173 		*p = savechar;
1174 	}
1175 	(void) strcpy(obp, "\n");
1176 	putline(obuf, fp, fullsmtp);
1177 }
1178 /*
1179 **  PUTBODY -- put the body of a message.
1180 **
1181 **	Parameters:
1182 **		fp -- file to output onto.
1183 **		m -- a mailer descriptor.
1184 **		xdot -- if set, use SMTP hidden dot algorithm.
1185 **
1186 **	Returns:
1187 **		none.
1188 **
1189 **	Side Effects:
1190 **		The message is written onto fp.
1191 */
1192 
1193 putbody(fp, m, xdot)
1194 	FILE *fp;
1195 	struct mailer *m;
1196 	bool xdot;
1197 {
1198 	char buf[MAXLINE + 1];
1199 	bool fullsmtp = bitset(M_FULLSMTP, m->m_flags);
1200 
1201 	/*
1202 	**  Output the body of the message
1203 	*/
1204 
1205 #ifdef lint
1206 	/* m will be needed later for complete smtp emulation */
1207 	if (m == NULL)
1208 		return;
1209 #endif lint
1210 
1211 	if (TempFile != NULL)
1212 	{
1213 		rewind(TempFile);
1214 		buf[0] = '.';
1215 		while (!ferror(fp) && fgets(&buf[1], sizeof buf - 1, TempFile) != NULL)
1216 			putline((xdot && buf[1] == '.') ? buf : &buf[1], fp, fullsmtp);
1217 
1218 		if (ferror(TempFile))
1219 		{
1220 			syserr("putbody: read error");
1221 			ExitStat = EX_IOERR;
1222 		}
1223 	}
1224 
1225 	(void) fflush(fp);
1226 	if (ferror(fp) && errno != EPIPE)
1227 	{
1228 		syserr("putbody: write error");
1229 		ExitStat = EX_IOERR;
1230 	}
1231 	errno = 0;
1232 }
1233 /*
1234 **  ISATWORD -- tell if the word we are pointing to is "at".
1235 **
1236 **	Parameters:
1237 **		p -- word to check.
1238 **
1239 **	Returns:
1240 **		TRUE -- if p is the word at.
1241 **		FALSE -- otherwise.
1242 **
1243 **	Side Effects:
1244 **		none.
1245 */
1246 
1247 bool
1248 isatword(p)
1249 	register char *p;
1250 {
1251 	extern char lower();
1252 
1253 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
1254 	    p[2] != '\0' && isspace(p[2]))
1255 		return (TRUE);
1256 	return (FALSE);
1257 }
1258 /*
1259 **  SAMEFROM -- tell if two text addresses represent the same from address.
1260 **
1261 **	Parameters:
1262 **		ifrom -- internally generated form of from address.
1263 **		efrom -- external form of from address.
1264 **
1265 **	Returns:
1266 **		TRUE -- if they convey the same info.
1267 **		FALSE -- if any information has been lost.
1268 **
1269 **	Side Effects:
1270 **		none.
1271 */
1272 
1273 bool
1274 samefrom(ifrom, efrom)
1275 	char *ifrom;
1276 	char *efrom;
1277 {
1278 	register char *p;
1279 	char buf[MAXNAME + 4];
1280 
1281 # ifdef DEBUG
1282 	if (tTd(3, 8))
1283 		printf("samefrom(%s,%s)-->", ifrom, efrom);
1284 # endif DEBUG
1285 	if (strcmp(ifrom, efrom) == 0)
1286 		goto success;
1287 	p = index(ifrom, '@');
1288 	if (p == NULL)
1289 		goto failure;
1290 	*p = '\0';
1291 	(void) strcpy(buf, ifrom);
1292 	(void) strcat(buf, " at ");
1293 	*p++ = '@';
1294 	(void) strcat(buf, p);
1295 	if (strcmp(buf, efrom) == 0)
1296 		goto success;
1297 
1298   failure:
1299 # ifdef DEBUG
1300 	if (tTd(3, 8))
1301 		printf("FALSE\n");
1302 # endif DEBUG
1303 	return (FALSE);
1304 
1305   success:
1306 # ifdef DEBUG
1307 	if (tTd(3, 8))
1308 		printf("TRUE\n");
1309 # endif DEBUG
1310 	return (TRUE);
1311 }
1312 /*
1313 **  MAILFILE -- Send a message to a file.
1314 **
1315 **	If the file has the setuid/setgid bits set, but NO execute
1316 **	bits, sendmail will try to become the owner of that file
1317 **	rather than the real user.  Obviously, this only works if
1318 **	sendmail runs as root.
1319 **
1320 **	Parameters:
1321 **		filename -- the name of the file to send to.
1322 **		ctladdr -- the controlling address header -- includes
1323 **			the userid/groupid to be when sending.
1324 **
1325 **	Returns:
1326 **		The exit code associated with the operation.
1327 **
1328 **	Side Effects:
1329 **		none.
1330 */
1331 
1332 mailfile(filename, ctladdr)
1333 	char *filename;
1334 	ADDRESS *ctladdr;
1335 {
1336 	register FILE *f;
1337 	register int pid;
1338 
1339 	/*
1340 	**  Fork so we can change permissions here.
1341 	**	Note that we MUST use fork, not vfork, because of
1342 	**	the complications of calling subroutines, etc.
1343 	*/
1344 
1345 	DOFORK(fork);
1346 
1347 	if (pid < 0)
1348 		return (EX_OSERR);
1349 	else if (pid == 0)
1350 	{
1351 		/* child -- actually write to file */
1352 		struct stat stb;
1353 
1354 		(void) signal(SIGINT, SIG_DFL);
1355 		(void) signal(SIGHUP, SIG_DFL);
1356 		(void) signal(SIGTERM, SIG_DFL);
1357 		umask(OldUmask);
1358 		if (stat(filename, &stb) < 0)
1359 			stb.st_mode = 0666;
1360 		if (bitset(0111, stb.st_mode))
1361 			exit(EX_CANTCREAT);
1362 		if (ctladdr == NULL)
1363 			ctladdr = &CurEnv->e_from;
1364 		if (!bitset(S_ISGID, stb.st_mode) || setgid(stb.st_gid) < 0)
1365 		{
1366 			if (ctladdr->q_uid == 0)
1367 				(void) setgid(DefGid);
1368 			else
1369 				(void) setgid(ctladdr->q_gid);
1370 		}
1371 		if (!bitset(S_ISUID, stb.st_mode) || setuid(stb.st_uid) < 0)
1372 		{
1373 			if (ctladdr->q_uid == 0)
1374 				(void) setuid(DefUid);
1375 			else
1376 				(void) setuid(ctladdr->q_uid);
1377 		}
1378 		f = dfopen(filename, "a");
1379 		if (f == NULL)
1380 			exit(EX_CANTCREAT);
1381 
1382 		putfromline(f, Mailer[1]);
1383 		(*CurEnv->e_puthdr)(f, Mailer[1], CurEnv);
1384 		fputs("\n", f);
1385 		(*CurEnv->e_putbody)(f, Mailer[1], FALSE);
1386 		fputs("\n", f);
1387 		(void) fclose(f);
1388 		(void) fflush(stdout);
1389 
1390 		/* reset ISUID & ISGID bits for paranoid systems */
1391 		(void) chmod(filename, (int) stb.st_mode);
1392 		exit(EX_OK);
1393 		/*NOTREACHED*/
1394 	}
1395 	else
1396 	{
1397 		/* parent -- wait for exit status */
1398 		register int i;
1399 		auto int stat;
1400 
1401 		while ((i = wait(&stat)) != pid)
1402 		{
1403 			if (i < 0)
1404 			{
1405 				stat = EX_OSERR << 8;
1406 				break;
1407 			}
1408 		}
1409 		if ((stat & 0377) != 0)
1410 			stat = EX_UNAVAILABLE << 8;
1411 		return ((stat >> 8) & 0377);
1412 	}
1413 }
1414 /*
1415 **  SENDALL -- actually send all the messages.
1416 **
1417 **	Parameters:
1418 **		e -- the envelope to send.
1419 **		mode -- the delivery mode to use.
1420 **
1421 **	Returns:
1422 **		none.
1423 **
1424 **	Side Effects:
1425 **		Scans the send lists and sends everything it finds.
1426 **		Delivers any appropriate error messages.
1427 **		If we are running in a non-interactive mode, takes the
1428 **			appropriate action.
1429 */
1430 
1431 sendall(e, mode)
1432 	ENVELOPE *e;
1433 	char mode;
1434 {
1435 	register ADDRESS *q;
1436 	bool oldverbose;
1437 	int pid;
1438 
1439 # ifdef DEBUG
1440 	if (tTd(13, 1))
1441 	{
1442 		printf("\nSENDALL: mode %c, sendqueue:\n", mode);
1443 		printaddr(e->e_sendqueue, TRUE);
1444 	}
1445 # endif DEBUG
1446 
1447 	/*
1448 	**  Do any preprocessing necessary for the mode we are running.
1449 	*/
1450 
1451 # ifdef QUEUE
1452 	if (mode == SM_QUEUE)
1453 	{
1454 		register ADDRESS *q;
1455 
1456 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1457 		{
1458 			if (!bitset(QDONTSEND, q->q_flags))
1459 			{
1460 				e->e_to = q->q_paddr;
1461 				message(Arpa_Info, "queued");
1462 				if (LogLevel > 4)
1463 					logdelivery("queued");
1464 			}
1465 			e->e_to = NULL;
1466 		}
1467 	}
1468 	if (mode == SM_QUEUE || mode == SM_FORK ||
1469 	    (mode != SM_VERIFY && SuperSafe))
1470 		queueup(e, TRUE);
1471 #endif QUEUE
1472 
1473 	oldverbose = Verbose;
1474 	switch (mode)
1475 	{
1476 	  case SM_VERIFY:
1477 		Verbose = TRUE;
1478 		break;
1479 
1480 	  case SM_QUEUE:
1481 		e->e_df = e->e_qf = NULL;
1482 		e->e_dontqueue = TRUE;
1483 		finis();
1484 		return;
1485 
1486 	  case SM_FORK:
1487 		(void) fflush(Xscript);
1488 		pid = fork();
1489 		if (pid < 0)
1490 		{
1491 			mode = SM_DELIVER;
1492 			break;
1493 		}
1494 		else if (pid > 0)
1495 		{
1496 			/* be sure we leave the temp files to our child */
1497 			e->e_id = e->e_df = e->e_qf = NULL;
1498 			e->e_dontqueue = TRUE;
1499 			Transcript = NULL;
1500 			return;
1501 		}
1502 
1503 		/* double fork to avoid zombies */
1504 		if (fork() > 0)
1505 			exit(EX_OK);
1506 
1507 		/* be sure we are immune from the terminal */
1508 		disconnect(TRUE);
1509 
1510 		break;
1511 	}
1512 
1513 	/*
1514 	**  Run through the list and send everything.
1515 	*/
1516 
1517 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1518 	{
1519 		if (mode == SM_VERIFY)
1520 		{
1521 			e->e_to = q->q_paddr;
1522 			if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
1523 				message(Arpa_Info, "deliverable");
1524 		}
1525 		else
1526 			(void) deliver(q);
1527 	}
1528 	Verbose = oldverbose;
1529 
1530 	/*
1531 	**  Now run through and check for errors.
1532 	*/
1533 
1534 	if (mode == SM_VERIFY)
1535 		return;
1536 
1537 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1538 	{
1539 		register ADDRESS *qq;
1540 
1541 # ifdef DEBUG
1542 		if (tTd(13, 3))
1543 		{
1544 			printf("Checking ");
1545 			printaddr(q, FALSE);
1546 		}
1547 # endif DEBUG
1548 
1549 		if (bitset(QQUEUEUP, q->q_flags))
1550 			e->e_queueup = TRUE;
1551 
1552 		/* we have an address that failed -- find the parent */
1553 		for (qq = q; qq != NULL; qq = qq->q_alias)
1554 		{
1555 			char obuf[MAXNAME + 6];
1556 			extern char *aliaslookup();
1557 
1558 			/* we can only have owners for local addresses */
1559 			if (!bitset(M_LOCAL, qq->q_mailer->m_flags))
1560 				continue;
1561 
1562 			/* see if the owner list exists */
1563 			(void) strcpy(obuf, "owner-");
1564 			if (strncmp(qq->q_user, "owner-", 6) == 0)
1565 				(void) strcat(obuf, "owner");
1566 			else
1567 				(void) strcat(obuf, qq->q_user);
1568 			if (aliaslookup(obuf) == NULL)
1569 				continue;
1570 
1571 # ifdef DEBUG
1572 			if (tTd(13, 4))
1573 				printf("Errors to %s\n", obuf);
1574 # endif DEBUG
1575 
1576 			/* add in an errors-to field */
1577 				/*   ugh... must happen before delivery.....
1578 			addheader("errors-to", newstr(obuf), e);
1579 				.... i guess this should go in sendto */
1580 
1581 			/* only send errors if the message failed */
1582 			if (!bitset(QBADADDR, q->q_flags))
1583 				break;
1584 
1585 			/* owner list exists -- add it to the error queue */
1586 			qq->q_flags &= ~QPRIMARY;
1587 			sendto(obuf, qq, &e->e_errorqueue);
1588 			MailBack = TRUE;
1589 			break;
1590 		}
1591 
1592 		/* if we did not find an owner, send to the sender */
1593 		if (qq == NULL && bitset(QBADADDR, q->q_flags))
1594 			sendto(e->e_from.q_paddr, qq, &e->e_errorqueue);
1595 	}
1596 
1597 	if (mode == SM_FORK)
1598 		finis();
1599 }
1600 /*
1601 **  CHECKERRORS -- check a queue of addresses and process errors.
1602 **
1603 **	Parameters:
1604 **		e -- the envelope to check.
1605 **
1606 **	Returns:
1607 **		none.
1608 **
1609 **	Side Effects:
1610 **		Arranges to queue all tempfailed messages in q
1611 **			or deliver error responses.
1612 */
1613 
1614 checkerrors(e)
1615 	register ENVELOPE *e;
1616 {
1617 	register ADDRESS *q;
1618 
1619 # ifdef DEBUG
1620 	if (tTd(4, 1))
1621 	{
1622 		printf("\ncheckerrors: FatalErrors %d, errorqueue:\n");
1623 		printaddr(e->e_errorqueue, TRUE);
1624 	}
1625 # endif DEBUG
1626 
1627 	/* mail back the transcript on errors */
1628 	if (FatalErrors)
1629 		savemail();
1630 
1631 	/* queue up anything laying around */
1632 	if (e->e_dontqueue)
1633 		return;
1634 	for (q = e->e_sendqueue; q != NULL; q = q->q_next)
1635 	{
1636 		if (bitset(QQUEUEUP, q->q_flags))
1637 		{
1638 # ifdef QUEUE
1639 			queueup(e, FALSE);
1640 			e->e_df = e->e_qf = NULL;
1641 			e->e_dontqueue = TRUE;
1642 # else QUEUE
1643 			syserr("checkerrors: trying to queue %s", e->e_df);
1644 # endif QUEUE
1645 			break;
1646 		}
1647 	}
1648 }
1649