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