1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)headers.c	8.3 (Berkeley) 07/16/93";
11 #endif /* not lint */
12 
13 # include <errno.h>
14 # include "sendmail.h"
15 
16 /*
17 **  CHOMPHEADER -- process and save a header line.
18 **
19 **	Called by collect and by readcf to deal with header lines.
20 **
21 **	Parameters:
22 **		line -- header as a text line.
23 **		def -- if set, this is a default value.
24 **		e -- the envelope including this header.
25 **
26 **	Returns:
27 **		flags for this header.
28 **
29 **	Side Effects:
30 **		The header is saved on the header list.
31 **		Contents of 'line' are destroyed.
32 */
33 
34 chompheader(line, def, e)
35 	char *line;
36 	bool def;
37 	register ENVELOPE *e;
38 {
39 	register char *p;
40 	register HDR *h;
41 	HDR **hp;
42 	char *fname;
43 	char *fvalue;
44 	struct hdrinfo *hi;
45 	bool cond = FALSE;
46 	BITMAP mopts;
47 
48 	if (tTd(31, 6))
49 		printf("chompheader: %s\n", line);
50 
51 	/* strip off options */
52 	clrbitmap(mopts);
53 	p = line;
54 	if (*p == '?')
55 	{
56 		/* have some */
57 		register char *q = strchr(p + 1, *p);
58 
59 		if (q != NULL)
60 		{
61 			*q++ = '\0';
62 			while (*++p != '\0')
63 				setbitn(*p, mopts);
64 			p = q;
65 		}
66 		else
67 			usrerr("553 header syntax error, line \"%s\"", line);
68 		cond = TRUE;
69 	}
70 
71 	/* find canonical name */
72 	fname = p;
73 	p = strchr(p, ':');
74 	if (p == NULL)
75 	{
76 		syserr("553 header syntax error, line \"%s\"", line);
77 		return (0);
78 	}
79 	fvalue = &p[1];
80 	while (isascii(*--p) && isspace(*p))
81 		continue;
82 	*++p = '\0';
83 
84 	/* strip field value on front */
85 	if (*fvalue == ' ')
86 		fvalue++;
87 
88 	/* see if it is a known type */
89 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
90 	{
91 		if (strcasecmp(hi->hi_field, fname) == 0)
92 			break;
93 	}
94 
95 	/* see if this is a resent message */
96 	if (!def && bitset(H_RESENT, hi->hi_flags))
97 		e->e_flags |= EF_RESENT;
98 
99 	/* if this means "end of header" quit now */
100 	if (bitset(H_EOH, hi->hi_flags))
101 		return (hi->hi_flags);
102 
103 	/* drop explicit From: if same as what we would generate -- for MH */
104 	p = "resent-from";
105 	if (!bitset(EF_RESENT, e->e_flags))
106 		p += 7;
107 	if (!def && !bitset(EF_QUEUERUN, e->e_flags) && strcasecmp(fname, p) == 0)
108 	{
109 		if (tTd(31, 2))
110 		{
111 			printf("comparing header from (%s) against default (%s or %s)\n",
112 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
113 		}
114 		if (e->e_from.q_paddr != NULL &&
115 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
116 		     strcmp(fvalue, e->e_from.q_user) == 0))
117 			return (hi->hi_flags);
118 	}
119 
120 	/* delete default value for this header */
121 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
122 	{
123 		if (strcasecmp(fname, h->h_field) == 0 &&
124 		    bitset(H_DEFAULT, h->h_flags) &&
125 		    !bitset(H_FORCE, h->h_flags))
126 			h->h_value = NULL;
127 	}
128 
129 	/* create a new node */
130 	h = (HDR *) xalloc(sizeof *h);
131 	h->h_field = newstr(fname);
132 	h->h_value = NULL;
133 	h->h_link = NULL;
134 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
135 	*hp = h;
136 	h->h_flags = hi->hi_flags;
137 	if (def)
138 		h->h_flags |= H_DEFAULT;
139 	if (cond)
140 		h->h_flags |= H_CHECK;
141 	if (h->h_value != NULL)
142 		free((char *) h->h_value);
143 	h->h_value = newstr(fvalue);
144 
145 	/* hack to see if this is a new format message */
146 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
147 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
148 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
149 	{
150 		e->e_flags &= ~EF_OLDSTYLE;
151 	}
152 
153 	return (h->h_flags);
154 }
155 /*
156 **  ADDHEADER -- add a header entry to the end of the queue.
157 **
158 **	This bypasses the special checking of chompheader.
159 **
160 **	Parameters:
161 **		field -- the name of the header field.
162 **		value -- the value of the field.
163 **		e -- the envelope to add them to.
164 **
165 **	Returns:
166 **		none.
167 **
168 **	Side Effects:
169 **		adds the field on the list of headers for this envelope.
170 */
171 
172 addheader(field, value, e)
173 	char *field;
174 	char *value;
175 	ENVELOPE *e;
176 {
177 	register HDR *h;
178 	register struct hdrinfo *hi;
179 	HDR **hp;
180 
181 	/* find info struct */
182 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
183 	{
184 		if (strcasecmp(field, hi->hi_field) == 0)
185 			break;
186 	}
187 
188 	/* find current place in list -- keep back pointer? */
189 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
190 	{
191 		if (strcasecmp(field, h->h_field) == 0)
192 			break;
193 	}
194 
195 	/* allocate space for new header */
196 	h = (HDR *) xalloc(sizeof *h);
197 	h->h_field = field;
198 	h->h_value = newstr(value);
199 	h->h_link = *hp;
200 	h->h_flags = hi->hi_flags | H_DEFAULT;
201 	clrbitmap(h->h_mflags);
202 	*hp = h;
203 }
204 /*
205 **  HVALUE -- return value of a header.
206 **
207 **	Only "real" fields (i.e., ones that have not been supplied
208 **	as a default) are used.
209 **
210 **	Parameters:
211 **		field -- the field name.
212 **		e -- the envelope containing the header.
213 **
214 **	Returns:
215 **		pointer to the value part.
216 **		NULL if not found.
217 **
218 **	Side Effects:
219 **		none.
220 */
221 
222 char *
223 hvalue(field, e)
224 	char *field;
225 	register ENVELOPE *e;
226 {
227 	register HDR *h;
228 
229 	for (h = e->e_header; h != NULL; h = h->h_link)
230 	{
231 		if (!bitset(H_DEFAULT, h->h_flags) &&
232 		    strcasecmp(h->h_field, field) == 0)
233 			return (h->h_value);
234 	}
235 	return (NULL);
236 }
237 /*
238 **  ISHEADER -- predicate telling if argument is a header.
239 **
240 **	A line is a header if it has a single word followed by
241 **	optional white space followed by a colon.
242 **
243 **	Parameters:
244 **		s -- string to check for possible headerness.
245 **
246 **	Returns:
247 **		TRUE if s is a header.
248 **		FALSE otherwise.
249 **
250 **	Side Effects:
251 **		none.
252 */
253 
254 bool
255 isheader(s)
256 	register char *s;
257 {
258 	while (*s > ' ' && *s != ':' && *s != '\0')
259 		s++;
260 
261 	/* following technically violates RFC822 */
262 	while (isascii(*s) && isspace(*s))
263 		s++;
264 
265 	return (*s == ':');
266 }
267 /*
268 **  EATHEADER -- run through the stored header and extract info.
269 **
270 **	Parameters:
271 **		e -- the envelope to process.
272 **		full -- if set, do full processing (e.g., compute
273 **			message priority).
274 **
275 **	Returns:
276 **		none.
277 **
278 **	Side Effects:
279 **		Sets a bunch of global variables from information
280 **			in the collected header.
281 **		Aborts the message if the hop count is exceeded.
282 */
283 
284 eatheader(e, full)
285 	register ENVELOPE *e;
286 	bool full;
287 {
288 	register HDR *h;
289 	register char *p;
290 	int hopcnt = 0;
291 	char *msgid;
292 	char buf[MAXLINE];
293 
294 	/*
295 	**  Set up macros for possible expansion in headers.
296 	*/
297 
298 	define('f', e->e_sender, e);
299 	define('g', e->e_sender, e);
300 
301 	if (tTd(32, 1))
302 		printf("----- collected header -----\n");
303 	msgid = "<none>";
304 	for (h = e->e_header; h != NULL; h = h->h_link)
305 	{
306 		/* do early binding */
307 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
308 		{
309 			expand(h->h_value, buf, &buf[sizeof buf], e);
310 			if (buf[0] != '\0')
311 			{
312 				h->h_value = newstr(buf);
313 				h->h_flags &= ~H_DEFAULT;
314 			}
315 		}
316 
317 		if (tTd(32, 1))
318 			printf("%s: %s\n", h->h_field, h->h_value);
319 
320 		/* count the number of times it has been processed */
321 		if (bitset(H_TRACE, h->h_flags))
322 			hopcnt++;
323 
324 		/* send to this person if we so desire */
325 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
326 		    !bitset(H_DEFAULT, h->h_flags) &&
327 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
328 		{
329 			int saveflags = e->e_flags;
330 
331 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
332 					  &e->e_sendqueue, e);
333 
334 			/* delete fatal errors generated by this address */
335 			if (!bitset(EF_FATALERRS, saveflags))
336 				e->e_flags &= ~EF_FATALERRS;
337 		}
338 
339 		/* save the message-id for logging */
340 		if (full && h->h_value != NULL &&
341 		    strcasecmp(h->h_field, "message-id") == 0)
342 		{
343 			msgid = h->h_value;
344 			while (isascii(*msgid) && isspace(*msgid))
345 				msgid++;
346 		}
347 
348 		/* see if this is a return-receipt header */
349 		if (bitset(H_RECEIPTTO, h->h_flags))
350 			e->e_receiptto = h->h_value;
351 
352 		/* see if this is an errors-to header */
353 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
354 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
355 					  &e->e_errorqueue, e);
356 	}
357 	if (tTd(32, 1))
358 		printf("----------------------------\n");
359 
360 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
361 	if (OpMode == MD_VERIFY)
362 		return;
363 
364 	/* store hop count */
365 	if (hopcnt > e->e_hopcount)
366 		e->e_hopcount = hopcnt;
367 
368 	/* message priority */
369 	p = hvalue("precedence", e);
370 	if (p != NULL)
371 		e->e_class = priencode(p);
372 	if (full)
373 		e->e_msgpriority = e->e_msgsize
374 				 - e->e_class * WkClassFact
375 				 + e->e_nrcpts * WkRecipFact;
376 
377 	/* full name of from person */
378 	p = hvalue("full-name", e);
379 	if (p != NULL)
380 		define('x', p, e);
381 
382 	/* date message originated */
383 	p = hvalue("posted-date", e);
384 	if (p == NULL)
385 		p = hvalue("date", e);
386 	if (p != NULL)
387 		define('a', p, e);
388 
389 	/*
390 	**  Log collection information.
391 	*/
392 
393 # ifdef LOG
394 	if (full && LogLevel > 4)
395 	{
396 		char *name;
397 		register char *sbp;
398 		char hbuf[MAXNAME];
399 		char sbuf[MAXLINE];
400 
401 		if (bitset(EF_RESPONSE, e->e_flags))
402 			name = "[RESPONSE]";
403 		else if ((name = macvalue('_', e)) != NULL)
404 			;
405 		else if (RealHostName[0] == '[')
406 			name = RealHostName;
407 		else
408 		{
409 			name = hbuf;
410 			(void) sprintf(hbuf, "%.80s", RealHostName);
411 			if (RealHostAddr.sa.sa_family != 0)
412 			{
413 				p = &hbuf[strlen(hbuf)];
414 				(void) sprintf(p, " (%s)",
415 					anynet_ntoa(&RealHostAddr));
416 			}
417 		}
418 
419 		/* some versions of syslog only take 5 printf args */
420 		sbp = sbuf;
421 		sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
422 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
423 		    e->e_msgpriority, e->e_nrcpts, msgid);
424 		sbp += strlen(sbp);
425 		if (e->e_bodytype != NULL)
426 		{
427 			(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
428 			sbp += strlen(sbp);
429 		}
430 		p = macvalue('r', e);
431 		if (p != NULL)
432 			(void) sprintf(sbp, ", proto=%.20s", p);
433 		syslog(LOG_INFO, "%s: %s, relay=%s",
434 		    e->e_id, sbuf, name);
435 	}
436 # endif /* LOG */
437 }
438 /*
439 **  PRIENCODE -- encode external priority names into internal values.
440 **
441 **	Parameters:
442 **		p -- priority in ascii.
443 **
444 **	Returns:
445 **		priority as a numeric level.
446 **
447 **	Side Effects:
448 **		none.
449 */
450 
451 priencode(p)
452 	char *p;
453 {
454 	register int i;
455 
456 	for (i = 0; i < NumPriorities; i++)
457 	{
458 		if (!strcasecmp(p, Priorities[i].pri_name))
459 			return (Priorities[i].pri_val);
460 	}
461 
462 	/* unknown priority */
463 	return (0);
464 }
465 /*
466 **  CRACKADDR -- parse an address and turn it into a macro
467 **
468 **	This doesn't actually parse the address -- it just extracts
469 **	it and replaces it with "$g".  The parse is totally ad hoc
470 **	and isn't even guaranteed to leave something syntactically
471 **	identical to what it started with.  However, it does leave
472 **	something semantically identical.
473 **
474 **	This algorithm has been cleaned up to handle a wider range
475 **	of cases -- notably quoted and backslash escaped strings.
476 **	This modification makes it substantially better at preserving
477 **	the original syntax.
478 **
479 **	Parameters:
480 **		addr -- the address to be cracked.
481 **
482 **	Returns:
483 **		a pointer to the new version.
484 **
485 **	Side Effects:
486 **		none.
487 **
488 **	Warning:
489 **		The return value is saved in local storage and should
490 **		be copied if it is to be reused.
491 */
492 
493 char *
494 crackaddr(addr)
495 	register char *addr;
496 {
497 	register char *p;
498 	register char c;
499 	int cmtlev;
500 	int realcmtlev;
501 	int anglelev, realanglelev;
502 	int copylev;
503 	bool qmode;
504 	bool realqmode;
505 	bool skipping;
506 	bool putgmac = FALSE;
507 	bool quoteit = FALSE;
508 	register char *bp;
509 	char *buflim;
510 	static char buf[MAXNAME];
511 
512 	if (tTd(33, 1))
513 		printf("crackaddr(%s)\n", addr);
514 
515 	/* strip leading spaces */
516 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
517 		addr++;
518 
519 	/*
520 	**  Start by assuming we have no angle brackets.  This will be
521 	**  adjusted later if we find them.
522 	*/
523 
524 	bp = buf;
525 	buflim = &buf[sizeof buf - 5];
526 	p = addr;
527 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
528 	qmode = realqmode = FALSE;
529 
530 	while ((c = *p++) != '\0')
531 	{
532 		/*
533 		**  If the buffer is overful, go into a special "skipping"
534 		**  mode that tries to keep legal syntax but doesn't actually
535 		**  output things.
536 		*/
537 
538 		skipping = bp >= buflim;
539 
540 		if (copylev > 0 && !skipping)
541 			*bp++ = c;
542 
543 		/* check for backslash escapes */
544 		if (c == '\\')
545 		{
546 			/* arrange to quote the address */
547 			if (cmtlev <= 0 && !qmode)
548 				quoteit = TRUE;
549 
550 			if ((c = *p++) == '\0')
551 			{
552 				/* too far */
553 				p--;
554 				goto putg;
555 			}
556 			if (copylev > 0 && !skipping)
557 				*bp++ = c;
558 			goto putg;
559 		}
560 
561 		/* check for quoted strings */
562 		if (c == '"')
563 		{
564 			qmode = !qmode;
565 			if (copylev > 0 && !skipping)
566 				realqmode = !realqmode;
567 			continue;
568 		}
569 		if (qmode)
570 			goto putg;
571 
572 		/* check for comments */
573 		if (c == '(')
574 		{
575 			cmtlev++;
576 
577 			/* allow space for closing paren */
578 			if (!skipping)
579 			{
580 				buflim--;
581 				realcmtlev++;
582 				if (copylev++ <= 0)
583 				{
584 					*bp++ = ' ';
585 					*bp++ = c;
586 				}
587 			}
588 		}
589 		if (cmtlev > 0)
590 		{
591 			if (c == ')')
592 			{
593 				cmtlev--;
594 				copylev--;
595 				if (!skipping)
596 				{
597 					realcmtlev--;
598 					buflim++;
599 				}
600 			}
601 			continue;
602 		}
603 		else if (c == ')')
604 		{
605 			/* syntax error: unmatched ) */
606 			if (!skipping)
607 				bp--;
608 		}
609 
610 
611 		/* check for characters that may have to be quoted */
612 		if (strchr(".'@,;:\\()", c) != NULL)
613 		{
614 			/*
615 			**  If these occur as the phrase part of a <>
616 			**  construct, but are not inside of () or already
617 			**  quoted, they will have to be quoted.  Note that
618 			**  now (but don't actually do the quoting).
619 			*/
620 
621 			if (cmtlev <= 0 && !qmode)
622 				quoteit = TRUE;
623 		}
624 
625 		/* check for angle brackets */
626 		if (c == '<')
627 		{
628 			register char *q;
629 
630 			/* oops -- have to change our mind */
631 			anglelev++;
632 			if (!skipping)
633 				realanglelev++;
634 
635 			bp = buf;
636 			if (quoteit)
637 			{
638 				*bp++ = '"';
639 
640 				/* back up over the '<' and any spaces */
641 				--p;
642 				while (isascii(*--p) && isspace(*p))
643 					continue;
644 				p++;
645 			}
646 			for (q = addr; q < p; )
647 			{
648 				c = *q++;
649 				if (bp < buflim)
650 				{
651 					if (quoteit && c == '"')
652 						*bp++ = '\\';
653 					*bp++ = c;
654 				}
655 			}
656 			if (quoteit)
657 			{
658 				*bp++ = '"';
659 				while ((c = *p++) != '<')
660 				{
661 					if (bp < buflim)
662 						*bp++ = c;
663 				}
664 				*bp++ = c;
665 			}
666 			copylev = 0;
667 			putgmac = quoteit = FALSE;
668 			continue;
669 		}
670 
671 		if (c == '>')
672 		{
673 			if (anglelev > 0)
674 			{
675 				anglelev--;
676 				if (!skipping)
677 				{
678 					realanglelev--;
679 					buflim++;
680 				}
681 			}
682 			else if (!skipping)
683 			{
684 				/* syntax error: unmatched > */
685 				if (copylev > 0)
686 					bp--;
687 				continue;
688 			}
689 			if (copylev++ <= 0)
690 				*bp++ = c;
691 			continue;
692 		}
693 
694 		/* must be a real address character */
695 	putg:
696 		if (copylev <= 0 && !putgmac)
697 		{
698 			*bp++ = MACROEXPAND;
699 			*bp++ = 'g';
700 			putgmac = TRUE;
701 		}
702 	}
703 
704 	/* repair any syntactic damage */
705 	if (realqmode)
706 		*bp++ = '"';
707 	while (realcmtlev-- > 0)
708 		*bp++ = ')';
709 	while (realanglelev-- > 0)
710 		*bp++ = '>';
711 	*bp++ = '\0';
712 
713 	if (tTd(33, 1))
714 		printf("crackaddr=>`%s'\n", buf);
715 
716 	return (buf);
717 }
718 /*
719 **  PUTHEADER -- put the header part of a message from the in-core copy
720 **
721 **	Parameters:
722 **		fp -- file to put it on.
723 **		m -- mailer to use.
724 **		e -- envelope to use.
725 **
726 **	Returns:
727 **		none.
728 **
729 **	Side Effects:
730 **		none.
731 */
732 
733 /*
734  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
735  */
736 #ifndef MAX
737 # define MAX(a,b) (((a)>(b))?(a):(b))
738 #endif
739 
740 putheader(fp, m, e)
741 	register FILE *fp;
742 	register MAILER *m;
743 	register ENVELOPE *e;
744 {
745 	char buf[MAX(MAXLINE,BUFSIZ)];
746 	register HDR *h;
747 	char obuf[MAXLINE];
748 
749 	if (tTd(34, 1))
750 		printf("--- putheader, mailer = %s ---\n", m->m_name);
751 
752 	for (h = e->e_header; h != NULL; h = h->h_link)
753 	{
754 		register char *p;
755 		extern bool bitintersect();
756 
757 		if (tTd(34, 11))
758 		{
759 			printf("  %s: ", h->h_field);
760 			xputs(h->h_value);
761 		}
762 
763 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
764 		    !bitintersect(h->h_mflags, m->m_flags))
765 		{
766 			if (tTd(34, 11))
767 				printf(" (skipped)\n");
768 			continue;
769 		}
770 
771 		/* handle Resent-... headers specially */
772 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
773 		{
774 			if (tTd(34, 11))
775 				printf(" (skipped (resent))\n");
776 			continue;
777 		}
778 		if (tTd(34, 11))
779 			printf("\n");
780 
781 		p = h->h_value;
782 		if (bitset(H_DEFAULT, h->h_flags))
783 		{
784 			/* macro expand value if generated internally */
785 			expand(p, buf, &buf[sizeof buf], e);
786 			p = buf;
787 			if (p == NULL || *p == '\0')
788 				continue;
789 		}
790 
791 		if (bitset(H_FROM|H_RCPT, h->h_flags))
792 		{
793 			/* address field */
794 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
795 
796 			if (bitset(H_FROM, h->h_flags))
797 				oldstyle = FALSE;
798 			commaize(h, p, fp, oldstyle, m, e);
799 		}
800 		else
801 		{
802 			/* vanilla header line */
803 			register char *nlp;
804 
805 			(void) sprintf(obuf, "%s: ", h->h_field);
806 			while ((nlp = strchr(p, '\n')) != NULL)
807 			{
808 				*nlp = '\0';
809 				(void) strcat(obuf, p);
810 				*nlp = '\n';
811 				putline(obuf, fp, m);
812 				p = ++nlp;
813 				obuf[0] = '\0';
814 			}
815 			(void) strcat(obuf, p);
816 			putline(obuf, fp, m);
817 		}
818 	}
819 }
820 /*
821 **  COMMAIZE -- output a header field, making a comma-translated list.
822 **
823 **	Parameters:
824 **		h -- the header field to output.
825 **		p -- the value to put in it.
826 **		fp -- file to put it to.
827 **		oldstyle -- TRUE if this is an old style header.
828 **		m -- a pointer to the mailer descriptor.  If NULL,
829 **			don't transform the name at all.
830 **		e -- the envelope containing the message.
831 **
832 **	Returns:
833 **		none.
834 **
835 **	Side Effects:
836 **		outputs "p" to file "fp".
837 */
838 
839 commaize(h, p, fp, oldstyle, m, e)
840 	register HDR *h;
841 	register char *p;
842 	FILE *fp;
843 	bool oldstyle;
844 	register MAILER *m;
845 	register ENVELOPE *e;
846 {
847 	register char *obp;
848 	int opos;
849 	bool firstone = TRUE;
850 	char obuf[MAXLINE + 3];
851 
852 	/*
853 	**  Output the address list translated by the
854 	**  mailer and with commas.
855 	*/
856 
857 	if (tTd(14, 2))
858 		printf("commaize(%s: %s)\n", h->h_field, p);
859 
860 	obp = obuf;
861 	(void) sprintf(obp, "%s: ", h->h_field);
862 	opos = strlen(h->h_field) + 2;
863 	obp += opos;
864 
865 	/*
866 	**  Run through the list of values.
867 	*/
868 
869 	while (*p != '\0')
870 	{
871 		register char *name;
872 		register int c;
873 		char savechar;
874 		int flags;
875 		auto int stat;
876 
877 		/*
878 		**  Find the end of the name.  New style names
879 		**  end with a comma, old style names end with
880 		**  a space character.  However, spaces do not
881 		**  necessarily delimit an old-style name -- at
882 		**  signs mean keep going.
883 		*/
884 
885 		/* find end of name */
886 		while ((isascii(*p) && isspace(*p)) || *p == ',')
887 			p++;
888 		name = p;
889 		for (;;)
890 		{
891 			auto char *oldp;
892 			char pvpbuf[PSBUFSIZE];
893 
894 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
895 			p = oldp;
896 
897 			/* look to see if we have an at sign */
898 			while (*p != '\0' && isascii(*p) && isspace(*p))
899 				p++;
900 
901 			if (*p != '@')
902 			{
903 				p = oldp;
904 				break;
905 			}
906 			p += *p == '@' ? 1 : 2;
907 			while (*p != '\0' && isascii(*p) && isspace(*p))
908 				p++;
909 		}
910 		/* at the end of one complete name */
911 
912 		/* strip off trailing white space */
913 		while (p >= name &&
914 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
915 			p--;
916 		if (++p == name)
917 			continue;
918 		savechar = *p;
919 		*p = '\0';
920 
921 		/* translate the name to be relative */
922 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
923 		if (bitset(H_FROM, h->h_flags))
924 			flags |= RF_SENDERADDR;
925 		stat = EX_OK;
926 		name = remotename(name, m, flags, &stat, e);
927 		if (*name == '\0')
928 		{
929 			*p = savechar;
930 			continue;
931 		}
932 
933 		/* output the name with nice formatting */
934 		opos += strlen(name);
935 		if (!firstone)
936 			opos += 2;
937 		if (opos > 78 && !firstone)
938 		{
939 			(void) strcpy(obp, ",\n");
940 			putline(obuf, fp, m);
941 			obp = obuf;
942 			(void) sprintf(obp, "        ");
943 			opos = strlen(obp);
944 			obp += opos;
945 			opos += strlen(name);
946 		}
947 		else if (!firstone)
948 		{
949 			(void) sprintf(obp, ", ");
950 			obp += 2;
951 		}
952 
953 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
954 			*obp++ = c;
955 		firstone = FALSE;
956 		*p = savechar;
957 	}
958 	(void) strcpy(obp, "\n");
959 	putline(obuf, fp, m);
960 }
961 /*
962 **  COPYHEADER -- copy header list
963 **
964 **	This routine is the equivalent of newstr for header lists
965 **
966 **	Parameters:
967 **		header -- list of header structures to copy.
968 **
969 **	Returns:
970 **		a copy of 'header'.
971 **
972 **	Side Effects:
973 **		none.
974 */
975 
976 HDR *
977 copyheader(header)
978 	register HDR *header;
979 {
980 	register HDR *newhdr;
981 	HDR *ret;
982 	register HDR **tail = &ret;
983 
984 	while (header != NULL)
985 	{
986 		newhdr = (HDR *) xalloc(sizeof(HDR));
987 		STRUCTCOPY(*header, *newhdr);
988 		*tail = newhdr;
989 		tail = &newhdr->h_link;
990 		header = header->h_link;
991 	}
992 	*tail = NULL;
993 
994 	return ret;
995 }
996