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.22 (Berkeley) 03/19/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 && !bitset(EF_QUEUERUN, e->e_flags) && 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 **
267 **	Returns:
268 **		none.
269 **
270 **	Side Effects:
271 **		Sets a bunch of global variables from information
272 **			in the collected header.
273 **		Aborts the message if the hop count is exceeded.
274 */
275 
276 eatheader(e)
277 	register ENVELOPE *e;
278 {
279 	register HDR *h;
280 	register char *p;
281 	int hopcnt = 0;
282 	char *msgid;
283 	char buf[MAXLINE];
284 
285 	/*
286 	**  Set up macros for possible expansion in headers.
287 	*/
288 
289 	define('f', e->e_sender, e);
290 	define('g', e->e_sender, e);
291 
292 	if (tTd(32, 1))
293 		printf("----- collected header -----\n");
294 	msgid = "<none>";
295 	for (h = e->e_header; h != NULL; h = h->h_link)
296 	{
297 		extern char *capitalize();
298 
299 		/* do early binding */
300 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
301 		{
302 			expand(h->h_value, buf, &buf[sizeof buf], e);
303 			if (buf[0] != '\0')
304 			{
305 				free(h->h_value);
306 				h->h_value = newstr(buf);
307 				h->h_flags &= ~H_DEFAULT;
308 			}
309 		}
310 
311 		if (tTd(32, 1))
312 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
313 
314 		/* count the number of times it has been processed */
315 		if (bitset(H_TRACE, h->h_flags))
316 			hopcnt++;
317 
318 		/* send to this person if we so desire */
319 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
320 		    !bitset(H_DEFAULT, h->h_flags) &&
321 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
322 		{
323 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
324 					  &e->e_sendqueue, e);
325 		}
326 
327 		/* save the message-id for logging */
328 		if (!bitset(EF_QUEUERUN, e->e_flags) && h->h_value != NULL &&
329 		    strcmp(h->h_field, "message-id") == 0)
330 		{
331 			msgid = h->h_value;
332 		}
333 
334 		/* see if this is a return-receipt header */
335 		if (bitset(H_RECEIPTTO, h->h_flags))
336 			e->e_receiptto = h->h_value;
337 
338 		/* see if this is an errors-to header */
339 		if (bitset(H_ERRORSTO, h->h_flags))
340 			(void) sendtolist(h->h_value, (ADDRESS *) NULL,
341 					  &e->e_errorqueue, e);
342 	}
343 	if (tTd(32, 1))
344 		printf("----------------------------\n");
345 
346 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
347 	if (OpMode == MD_VERIFY)
348 		return;
349 
350 	/* store hop count */
351 	if (hopcnt > e->e_hopcount)
352 		e->e_hopcount = hopcnt;
353 
354 	/* message priority */
355 	p = hvalue("precedence", e);
356 	if (p != NULL)
357 		e->e_class = priencode(p);
358 	if (!bitset(EF_QUEUERUN, e->e_flags))
359 		e->e_msgpriority = e->e_msgsize
360 				 - e->e_class * WkClassFact
361 				 + e->e_nrcpts * WkRecipFact;
362 
363 	/* full name of from person */
364 	p = hvalue("full-name", e);
365 	if (p != NULL)
366 		define('x', p, e);
367 
368 	/* date message originated */
369 	p = hvalue("posted-date", e);
370 	if (p == NULL)
371 		p = hvalue("date", e);
372 	if (p != NULL)
373 		define('a', p, e);
374 
375 	/*
376 	**  Log collection information.
377 	*/
378 
379 # ifdef LOG
380 	if (!bitset(EF_QUEUERUN, e->e_flags) && LogLevel > 4)
381 	{
382 		char *name;
383 		char hbuf[MAXNAME];
384 		char sbuf[MAXLINE];
385 
386 		if (bitset(EF_RESPONSE, e->e_flags))
387 			name = "[RESPONSE]";
388 		else if (RealHostName[0] == '[')
389 			name = RealHostName;
390 		else
391 		{
392 			extern char *inet_ntoa();
393 
394 			name = hbuf;
395 			(void)sprintf(hbuf, "%.80s (%s)",
396 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
397 		}
398 
399 		/* some versions of syslog only take 5 printf args */
400 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
401 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
402 		    e->e_msgpriority, e->e_nrcpts, msgid);
403 		syslog(LOG_INFO, "%s: %s, relay=%s",
404 		    e->e_id, sbuf, name);
405 	}
406 # endif /* LOG */
407 }
408 /*
409 **  PRIENCODE -- encode external priority names into internal values.
410 **
411 **	Parameters:
412 **		p -- priority in ascii.
413 **
414 **	Returns:
415 **		priority as a numeric level.
416 **
417 **	Side Effects:
418 **		none.
419 */
420 
421 priencode(p)
422 	char *p;
423 {
424 	register int i;
425 
426 	for (i = 0; i < NumPriorities; i++)
427 	{
428 		if (!strcasecmp(p, Priorities[i].pri_name))
429 			return (Priorities[i].pri_val);
430 	}
431 
432 	/* unknown priority */
433 	return (0);
434 }
435 /*
436 **  CRACKADDR -- parse an address and turn it into a macro
437 **
438 **	This doesn't actually parse the address -- it just extracts
439 **	it and replaces it with "$g".  The parse is totally ad hoc
440 **	and isn't even guaranteed to leave something syntactically
441 **	identical to what it started with.  However, it does leave
442 **	something semantically identical.
443 **
444 **	This algorithm has been cleaned up to handle a wider range
445 **	of cases -- notably quoted and backslash escaped strings.
446 **	This modification makes it substantially better at preserving
447 **	the original syntax.
448 **
449 **	Parameters:
450 **		addr -- the address to be cracked.
451 **
452 **	Returns:
453 **		a pointer to the new version.
454 **
455 **	Side Effects:
456 **		none.
457 **
458 **	Warning:
459 **		The return value is saved in local storage and should
460 **		be copied if it is to be reused.
461 */
462 
463 char *
464 crackaddr(addr)
465 	register char *addr;
466 {
467 	register char *p;
468 	register char c;
469 	int cmtlev;
470 	int realcmtlev;
471 	int anglelev, realanglelev;
472 	int copylev;
473 	bool qmode;
474 	bool realqmode;
475 	bool skipping;
476 	bool putgmac = FALSE;
477 	bool quoteit = FALSE;
478 	register char *bp;
479 	char *buflim;
480 	static char buf[MAXNAME];
481 
482 	if (tTd(33, 1))
483 		printf("crackaddr(%s)\n", addr);
484 
485 	/* strip leading spaces */
486 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
487 		addr++;
488 
489 	/*
490 	**  Start by assuming we have no angle brackets.  This will be
491 	**  adjusted later if we find them.
492 	*/
493 
494 	bp = buf;
495 	buflim = &buf[sizeof buf - 5];
496 	p = addr;
497 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
498 	qmode = realqmode = FALSE;
499 
500 	while ((c = *p++) != '\0')
501 	{
502 		/*
503 		**  If the buffer is overful, go into a special "skipping"
504 		**  mode that tries to keep legal syntax but doesn't actually
505 		**  output things.
506 		*/
507 
508 		skipping = bp >= buflim;
509 
510 		if (copylev > 0 && !skipping)
511 			*bp++ = c;
512 
513 		/* check for backslash escapes */
514 		if (c == '\\')
515 		{
516 			if ((c = *p++) == '\0')
517 			{
518 				/* too far */
519 				p--;
520 				goto putg;
521 			}
522 			if (copylev > 0 && !skipping)
523 				*bp++ = c;
524 			goto putg;
525 		}
526 
527 		/* check for quoted strings */
528 		if (c == '"')
529 		{
530 			qmode = !qmode;
531 			if (copylev > 0 && !skipping)
532 				realqmode = !realqmode;
533 			continue;
534 		}
535 		if (qmode)
536 			goto putg;
537 
538 		/* check for comments */
539 		if (c == '(')
540 		{
541 			cmtlev++;
542 
543 			/* allow space for closing paren */
544 			if (!skipping)
545 			{
546 				buflim--;
547 				realcmtlev++;
548 				if (copylev++ <= 0)
549 				{
550 					*bp++ = ' ';
551 					*bp++ = c;
552 				}
553 			}
554 		}
555 		if (cmtlev > 0)
556 		{
557 			if (c == ')')
558 			{
559 				cmtlev--;
560 				copylev--;
561 				if (!skipping)
562 				{
563 					realcmtlev--;
564 					buflim++;
565 				}
566 			}
567 			continue;
568 		}
569 		else if (c == ')')
570 		{
571 			/* syntax error: unmatched ) */
572 			if (!skipping)
573 				bp--;
574 		}
575 
576 
577 		/* check for characters that may have to be quoted */
578 		if (strchr(".'@,;:\\()", c) != NULL)
579 		{
580 			/*
581 			**  If these occur as the phrase part of a <>
582 			**  construct, but are not inside of () or already
583 			**  quoted, they will have to be quoted.  Note that
584 			**  now (but don't actually do the quoting).
585 			*/
586 
587 			if (cmtlev <= 0 && !qmode)
588 				quoteit = TRUE;
589 		}
590 
591 		/* check for angle brackets */
592 		if (c == '<')
593 		{
594 			register char *q;
595 
596 			/* oops -- have to change our mind */
597 			anglelev++;
598 			if (!skipping)
599 				realanglelev++;
600 
601 			bp = buf;
602 			if (quoteit)
603 			{
604 				*bp++ = '"';
605 
606 				/* back up over the '<' and any spaces */
607 				--p;
608 				while (isascii(*--p) && isspace(*p))
609 					continue;
610 				p++;
611 			}
612 			for (q = addr; q < p; )
613 			{
614 				c = *q++;
615 				if (bp < buflim)
616 				{
617 					if (quoteit && c == '"')
618 						*bp++ = '\\';
619 					*bp++ = c;
620 				}
621 			}
622 			if (quoteit)
623 			{
624 				*bp++ = '"';
625 				while ((c = *p++) != '<')
626 				{
627 					if (bp < buflim)
628 						*bp++ = c;
629 				}
630 				*bp++ = c;
631 			}
632 			copylev = 0;
633 			putgmac = quoteit = FALSE;
634 			continue;
635 		}
636 
637 		if (c == '>')
638 		{
639 			if (anglelev > 0)
640 			{
641 				anglelev--;
642 				if (!skipping)
643 				{
644 					realanglelev--;
645 					buflim++;
646 				}
647 			}
648 			else if (!skipping)
649 			{
650 				/* syntax error: unmatched > */
651 				if (copylev > 0)
652 					bp--;
653 				continue;
654 			}
655 			if (copylev++ <= 0)
656 				*bp++ = c;
657 			continue;
658 		}
659 
660 		/* must be a real address character */
661 	putg:
662 		if (copylev <= 0 && !putgmac)
663 		{
664 			*bp++ = MACROEXPAND;
665 			*bp++ = 'g';
666 			putgmac = TRUE;
667 		}
668 	}
669 
670 	/* repair any syntactic damage */
671 	if (realqmode)
672 		*bp++ = '"';
673 	while (realcmtlev-- > 0)
674 		*bp++ = ')';
675 	while (realanglelev-- > 0)
676 		*bp++ = '>';
677 	*bp++ = '\0';
678 
679 	if (tTd(33, 1))
680 		printf("crackaddr=>`%s'\n", buf);
681 
682 	return (buf);
683 }
684 /*
685 **  PUTHEADER -- put the header part of a message from the in-core copy
686 **
687 **	Parameters:
688 **		fp -- file to put it on.
689 **		m -- mailer to use.
690 **		e -- envelope to use.
691 **
692 **	Returns:
693 **		none.
694 **
695 **	Side Effects:
696 **		none.
697 */
698 
699 /*
700  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
701  */
702 #ifndef MAX
703 # define MAX(a,b) (((a)>(b))?(a):(b))
704 #endif
705 
706 putheader(fp, m, e)
707 	register FILE *fp;
708 	register MAILER *m;
709 	register ENVELOPE *e;
710 {
711 	char buf[MAX(MAXLINE,BUFSIZ)];
712 	register HDR *h;
713 	char obuf[MAXLINE];
714 
715 	for (h = e->e_header; h != NULL; h = h->h_link)
716 	{
717 		register char *p;
718 		extern bool bitintersect();
719 
720 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
721 		    !bitintersect(h->h_mflags, m->m_flags))
722 			continue;
723 
724 		/* handle Resent-... headers specially */
725 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
726 			continue;
727 
728 		p = h->h_value;
729 		if (bitset(H_DEFAULT, h->h_flags))
730 		{
731 			/* macro expand value if generated internally */
732 			expand(p, buf, &buf[sizeof buf], e);
733 			p = buf;
734 			if (p == NULL || *p == '\0')
735 				continue;
736 		}
737 
738 		if (bitset(H_FROM|H_RCPT, h->h_flags))
739 		{
740 			/* address field */
741 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
742 
743 			if (bitset(H_FROM, h->h_flags))
744 				oldstyle = FALSE;
745 			commaize(h, p, fp, oldstyle, m, e);
746 		}
747 		else
748 		{
749 			/* vanilla header line */
750 			register char *nlp;
751 			extern char *capitalize();
752 
753 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
754 			while ((nlp = strchr(p, '\n')) != NULL)
755 			{
756 				*nlp = '\0';
757 				(void) strcat(obuf, p);
758 				*nlp = '\n';
759 				putline(obuf, fp, m);
760 				p = ++nlp;
761 				obuf[0] = '\0';
762 			}
763 			(void) strcat(obuf, p);
764 			putline(obuf, fp, m);
765 		}
766 	}
767 }
768 /*
769 **  COMMAIZE -- output a header field, making a comma-translated list.
770 **
771 **	Parameters:
772 **		h -- the header field to output.
773 **		p -- the value to put in it.
774 **		fp -- file to put it to.
775 **		oldstyle -- TRUE if this is an old style header.
776 **		m -- a pointer to the mailer descriptor.  If NULL,
777 **			don't transform the name at all.
778 **		e -- the envelope containing the message.
779 **
780 **	Returns:
781 **		none.
782 **
783 **	Side Effects:
784 **		outputs "p" to file "fp".
785 */
786 
787 commaize(h, p, fp, oldstyle, m, e)
788 	register HDR *h;
789 	register char *p;
790 	FILE *fp;
791 	bool oldstyle;
792 	register MAILER *m;
793 	register ENVELOPE *e;
794 {
795 	register char *obp;
796 	int opos;
797 	bool firstone = TRUE;
798 	char obuf[MAXLINE + 3];
799 	extern char *capitalize();
800 
801 	/*
802 	**  Output the address list translated by the
803 	**  mailer and with commas.
804 	*/
805 
806 	if (tTd(14, 2))
807 		printf("commaize(%s: %s)\n", h->h_field, p);
808 
809 	obp = obuf;
810 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
811 	opos = strlen(h->h_field) + 2;
812 	obp += opos;
813 
814 	/*
815 	**  Run through the list of values.
816 	*/
817 
818 	while (*p != '\0')
819 	{
820 		register char *name;
821 		register int c;
822 		char savechar;
823 		extern char *remotename();
824 
825 		/*
826 		**  Find the end of the name.  New style names
827 		**  end with a comma, old style names end with
828 		**  a space character.  However, spaces do not
829 		**  necessarily delimit an old-style name -- at
830 		**  signs mean keep going.
831 		*/
832 
833 		/* find end of name */
834 		while ((isascii(*p) && isspace(*p)) || *p == ',')
835 			p++;
836 		name = p;
837 		for (;;)
838 		{
839 			auto char *oldp;
840 			char pvpbuf[PSBUFSIZE];
841 			extern char **prescan();
842 
843 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
844 			p = oldp;
845 
846 			/* look to see if we have an at sign */
847 			while (*p != '\0' && isascii(*p) && isspace(*p))
848 				p++;
849 
850 			if (*p != '@')
851 			{
852 				p = oldp;
853 				break;
854 			}
855 			p += *p == '@' ? 1 : 2;
856 			while (*p != '\0' && isascii(*p) && isspace(*p))
857 				p++;
858 		}
859 		/* at the end of one complete name */
860 
861 		/* strip off trailing white space */
862 		while (p >= name &&
863 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
864 			p--;
865 		if (++p == name)
866 			continue;
867 		savechar = *p;
868 		*p = '\0';
869 
870 		/* translate the name to be relative */
871 		name = remotename(name, m, bitset(H_FROM, h->h_flags),
872 				  TRUE, FALSE, TRUE, e);
873 		if (*name == '\0')
874 		{
875 			*p = savechar;
876 			continue;
877 		}
878 
879 		/* output the name with nice formatting */
880 		opos += strlen(name);
881 		if (!firstone)
882 			opos += 2;
883 		if (opos > 78 && !firstone)
884 		{
885 			(void) strcpy(obp, ",\n");
886 			putline(obuf, fp, m);
887 			obp = obuf;
888 			(void) sprintf(obp, "        ");
889 			opos = strlen(obp);
890 			obp += opos;
891 			opos += strlen(name);
892 		}
893 		else if (!firstone)
894 		{
895 			(void) sprintf(obp, ", ");
896 			obp += 2;
897 		}
898 
899 		/* strip off quote bits as we output */
900 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
901 		{
902 			if (bitnset(M_7BITS, m->m_flags))
903 				c &= 0177;
904 			*obp++ = c;
905 		}
906 		firstone = FALSE;
907 		*p = savechar;
908 	}
909 	(void) strcpy(obp, "\n");
910 	putline(obuf, fp, m);
911 }
912 /*
913 **  COPYHEADER -- copy header list
914 **
915 **	This routine is the equivalent of newstr for header lists
916 **
917 **	Parameters:
918 **		header -- list of header structures to copy.
919 **
920 **	Returns:
921 **		a copy of 'header'.
922 **
923 **	Side Effects:
924 **		none.
925 */
926 
927 HDR *
928 copyheader(header)
929 	register HDR *header;
930 {
931 	register HDR *newhdr;
932 	HDR *ret;
933 	register HDR **tail = &ret;
934 
935 	while (header != NULL)
936 	{
937 		newhdr = (HDR *) xalloc(sizeof(HDR));
938 		STRUCTCOPY(*header, *newhdr);
939 		*tail = newhdr;
940 		tail = &newhdr->h_link;
941 		header = header->h_link;
942 	}
943 	*tail = NULL;
944 
945 	return ret;
946 }
947