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.32 (Berkeley) 04/30/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 (e->e_from.q_paddr != NULL &&
110 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
111 			return (hi->hi_flags);
112 	}
113 
114 	/* delete default value for this header */
115 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
116 	{
117 		if (strcasecmp(fname, h->h_field) == 0 &&
118 		    bitset(H_DEFAULT, h->h_flags) &&
119 		    !bitset(H_FORCE, h->h_flags))
120 			h->h_value = NULL;
121 	}
122 
123 	/* create a new node */
124 	h = (HDR *) xalloc(sizeof *h);
125 	h->h_field = newstr(fname);
126 	h->h_value = NULL;
127 	h->h_link = NULL;
128 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
129 	*hp = h;
130 	h->h_flags = hi->hi_flags;
131 	if (def)
132 		h->h_flags |= H_DEFAULT;
133 	if (cond)
134 		h->h_flags |= H_CHECK;
135 	if (h->h_value != NULL)
136 		free((char *) h->h_value);
137 	h->h_value = newstr(fvalue);
138 
139 	/* hack to see if this is a new format message */
140 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
141 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
142 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
143 	{
144 		e->e_flags &= ~EF_OLDSTYLE;
145 	}
146 
147 	return (h->h_flags);
148 }
149 /*
150 **  ADDHEADER -- add a header entry to the end of the queue.
151 **
152 **	This bypasses the special checking of chompheader.
153 **
154 **	Parameters:
155 **		field -- the name of the header field.
156 **		value -- the value of the field.
157 **		e -- the envelope to add them to.
158 **
159 **	Returns:
160 **		none.
161 **
162 **	Side Effects:
163 **		adds the field on the list of headers for this envelope.
164 */
165 
166 addheader(field, value, e)
167 	char *field;
168 	char *value;
169 	ENVELOPE *e;
170 {
171 	register HDR *h;
172 	register struct hdrinfo *hi;
173 	HDR **hp;
174 
175 	/* find info struct */
176 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
177 	{
178 		if (strcasecmp(field, hi->hi_field) == 0)
179 			break;
180 	}
181 
182 	/* find current place in list -- keep back pointer? */
183 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
184 	{
185 		if (strcasecmp(field, h->h_field) == 0)
186 			break;
187 	}
188 
189 	/* allocate space for new header */
190 	h = (HDR *) xalloc(sizeof *h);
191 	h->h_field = field;
192 	h->h_value = newstr(value);
193 	h->h_link = *hp;
194 	h->h_flags = hi->hi_flags | H_DEFAULT;
195 	clrbitmap(h->h_mflags);
196 	*hp = h;
197 }
198 /*
199 **  HVALUE -- return value of a header.
200 **
201 **	Only "real" fields (i.e., ones that have not been supplied
202 **	as a default) are used.
203 **
204 **	Parameters:
205 **		field -- the field name.
206 **		e -- the envelope containing the header.
207 **
208 **	Returns:
209 **		pointer to the value part.
210 **		NULL if not found.
211 **
212 **	Side Effects:
213 **		none.
214 */
215 
216 char *
217 hvalue(field, e)
218 	char *field;
219 	register ENVELOPE *e;
220 {
221 	register HDR *h;
222 
223 	for (h = e->e_header; h != NULL; h = h->h_link)
224 	{
225 		if (!bitset(H_DEFAULT, h->h_flags) &&
226 		    strcasecmp(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 **		full -- if set, do full processing (e.g., compute
267 **			message priority).
268 **
269 **	Returns:
270 **		none.
271 **
272 **	Side Effects:
273 **		Sets a bunch of global variables from information
274 **			in the collected header.
275 **		Aborts the message if the hop count is exceeded.
276 */
277 
278 eatheader(e, full)
279 	register ENVELOPE *e;
280 	bool full;
281 {
282 	register HDR *h;
283 	register char *p;
284 	int hopcnt = 0;
285 	char *msgid;
286 	char buf[MAXLINE];
287 
288 	/*
289 	**  Set up macros for possible expansion in headers.
290 	*/
291 
292 	define('f', e->e_sender, e);
293 	define('g', e->e_sender, e);
294 
295 	if (tTd(32, 1))
296 		printf("----- collected header -----\n");
297 	msgid = "<none>";
298 	for (h = e->e_header; h != NULL; h = h->h_link)
299 	{
300 		/* do early binding */
301 		if (bitset(H_DEFAULT, h->h_flags) && h->h_value != NULL)
302 		{
303 			expand(h->h_value, buf, &buf[sizeof buf], e);
304 			if (buf[0] != '\0')
305 			{
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", 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 (full && h->h_value != NULL &&
329 		    strcasecmp(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 (full)
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 (full && LogLevel > 4)
381 	{
382 		char *name;
383 		char hbuf[MAXNAME];
384 		char sbuf[MAXLINE];
385 		extern char *macvalue();
386 
387 		if (bitset(EF_RESPONSE, e->e_flags))
388 			name = "[RESPONSE]";
389 		else if ((name = macvalue('_', e)) != NULL)
390 			;
391 		else if (RealHostName[0] == '[')
392 			name = RealHostName;
393 		else
394 		{
395 			extern char *anynet_ntoa();
396 
397 			name = hbuf;
398 			(void) sprintf(hbuf, "%.80s", RealHostName);
399 			if (RealHostAddr.sa.sa_family != 0)
400 			{
401 				p = &hbuf[strlen(hbuf)];
402 				(void) sprintf(p, " (%s)",
403 					anynet_ntoa(&RealHostAddr));
404 			}
405 		}
406 
407 		/* some versions of syslog only take 5 printf args */
408 		sprintf(sbuf, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d, msgid=%.100s",
409 		    e->e_from.q_paddr, e->e_msgsize, e->e_class,
410 		    e->e_msgpriority, e->e_nrcpts, msgid);
411 		syslog(LOG_INFO, "%s: %s, relay=%s",
412 		    e->e_id, sbuf, name);
413 	}
414 # endif /* LOG */
415 }
416 /*
417 **  PRIENCODE -- encode external priority names into internal values.
418 **
419 **	Parameters:
420 **		p -- priority in ascii.
421 **
422 **	Returns:
423 **		priority as a numeric level.
424 **
425 **	Side Effects:
426 **		none.
427 */
428 
429 priencode(p)
430 	char *p;
431 {
432 	register int i;
433 
434 	for (i = 0; i < NumPriorities; i++)
435 	{
436 		if (!strcasecmp(p, Priorities[i].pri_name))
437 			return (Priorities[i].pri_val);
438 	}
439 
440 	/* unknown priority */
441 	return (0);
442 }
443 /*
444 **  CRACKADDR -- parse an address and turn it into a macro
445 **
446 **	This doesn't actually parse the address -- it just extracts
447 **	it and replaces it with "$g".  The parse is totally ad hoc
448 **	and isn't even guaranteed to leave something syntactically
449 **	identical to what it started with.  However, it does leave
450 **	something semantically identical.
451 **
452 **	This algorithm has been cleaned up to handle a wider range
453 **	of cases -- notably quoted and backslash escaped strings.
454 **	This modification makes it substantially better at preserving
455 **	the original syntax.
456 **
457 **	Parameters:
458 **		addr -- the address to be cracked.
459 **
460 **	Returns:
461 **		a pointer to the new version.
462 **
463 **	Side Effects:
464 **		none.
465 **
466 **	Warning:
467 **		The return value is saved in local storage and should
468 **		be copied if it is to be reused.
469 */
470 
471 char *
472 crackaddr(addr)
473 	register char *addr;
474 {
475 	register char *p;
476 	register char c;
477 	int cmtlev;
478 	int realcmtlev;
479 	int anglelev, realanglelev;
480 	int copylev;
481 	bool qmode;
482 	bool realqmode;
483 	bool skipping;
484 	bool putgmac = FALSE;
485 	bool quoteit = FALSE;
486 	register char *bp;
487 	char *buflim;
488 	static char buf[MAXNAME];
489 
490 	if (tTd(33, 1))
491 		printf("crackaddr(%s)\n", addr);
492 
493 	/* strip leading spaces */
494 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
495 		addr++;
496 
497 	/*
498 	**  Start by assuming we have no angle brackets.  This will be
499 	**  adjusted later if we find them.
500 	*/
501 
502 	bp = buf;
503 	buflim = &buf[sizeof buf - 5];
504 	p = addr;
505 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
506 	qmode = realqmode = FALSE;
507 
508 	while ((c = *p++) != '\0')
509 	{
510 		/*
511 		**  If the buffer is overful, go into a special "skipping"
512 		**  mode that tries to keep legal syntax but doesn't actually
513 		**  output things.
514 		*/
515 
516 		skipping = bp >= buflim;
517 
518 		if (copylev > 0 && !skipping)
519 			*bp++ = c;
520 
521 		/* check for backslash escapes */
522 		if (c == '\\')
523 		{
524 			/* arrange to quote the address */
525 			if (cmtlev <= 0 && !qmode)
526 				quoteit = TRUE;
527 
528 			if ((c = *p++) == '\0')
529 			{
530 				/* too far */
531 				p--;
532 				goto putg;
533 			}
534 			if (copylev > 0 && !skipping)
535 				*bp++ = c;
536 			goto putg;
537 		}
538 
539 		/* check for quoted strings */
540 		if (c == '"')
541 		{
542 			qmode = !qmode;
543 			if (copylev > 0 && !skipping)
544 				realqmode = !realqmode;
545 			continue;
546 		}
547 		if (qmode)
548 			goto putg;
549 
550 		/* check for comments */
551 		if (c == '(')
552 		{
553 			cmtlev++;
554 
555 			/* allow space for closing paren */
556 			if (!skipping)
557 			{
558 				buflim--;
559 				realcmtlev++;
560 				if (copylev++ <= 0)
561 				{
562 					*bp++ = ' ';
563 					*bp++ = c;
564 				}
565 			}
566 		}
567 		if (cmtlev > 0)
568 		{
569 			if (c == ')')
570 			{
571 				cmtlev--;
572 				copylev--;
573 				if (!skipping)
574 				{
575 					realcmtlev--;
576 					buflim++;
577 				}
578 			}
579 			continue;
580 		}
581 		else if (c == ')')
582 		{
583 			/* syntax error: unmatched ) */
584 			if (!skipping)
585 				bp--;
586 		}
587 
588 
589 		/* check for characters that may have to be quoted */
590 		if (strchr(".'@,;:\\()", c) != NULL)
591 		{
592 			/*
593 			**  If these occur as the phrase part of a <>
594 			**  construct, but are not inside of () or already
595 			**  quoted, they will have to be quoted.  Note that
596 			**  now (but don't actually do the quoting).
597 			*/
598 
599 			if (cmtlev <= 0 && !qmode)
600 				quoteit = TRUE;
601 		}
602 
603 		/* check for angle brackets */
604 		if (c == '<')
605 		{
606 			register char *q;
607 
608 			/* oops -- have to change our mind */
609 			anglelev++;
610 			if (!skipping)
611 				realanglelev++;
612 
613 			bp = buf;
614 			if (quoteit)
615 			{
616 				*bp++ = '"';
617 
618 				/* back up over the '<' and any spaces */
619 				--p;
620 				while (isascii(*--p) && isspace(*p))
621 					continue;
622 				p++;
623 			}
624 			for (q = addr; q < p; )
625 			{
626 				c = *q++;
627 				if (bp < buflim)
628 				{
629 					if (quoteit && c == '"')
630 						*bp++ = '\\';
631 					*bp++ = c;
632 				}
633 			}
634 			if (quoteit)
635 			{
636 				*bp++ = '"';
637 				while ((c = *p++) != '<')
638 				{
639 					if (bp < buflim)
640 						*bp++ = c;
641 				}
642 				*bp++ = c;
643 			}
644 			copylev = 0;
645 			putgmac = quoteit = FALSE;
646 			continue;
647 		}
648 
649 		if (c == '>')
650 		{
651 			if (anglelev > 0)
652 			{
653 				anglelev--;
654 				if (!skipping)
655 				{
656 					realanglelev--;
657 					buflim++;
658 				}
659 			}
660 			else if (!skipping)
661 			{
662 				/* syntax error: unmatched > */
663 				if (copylev > 0)
664 					bp--;
665 				continue;
666 			}
667 			if (copylev++ <= 0)
668 				*bp++ = c;
669 			continue;
670 		}
671 
672 		/* must be a real address character */
673 	putg:
674 		if (copylev <= 0 && !putgmac)
675 		{
676 			*bp++ = MACROEXPAND;
677 			*bp++ = 'g';
678 			putgmac = TRUE;
679 		}
680 	}
681 
682 	/* repair any syntactic damage */
683 	if (realqmode)
684 		*bp++ = '"';
685 	while (realcmtlev-- > 0)
686 		*bp++ = ')';
687 	while (realanglelev-- > 0)
688 		*bp++ = '>';
689 	*bp++ = '\0';
690 
691 	if (tTd(33, 1))
692 		printf("crackaddr=>`%s'\n", buf);
693 
694 	return (buf);
695 }
696 /*
697 **  PUTHEADER -- put the header part of a message from the in-core copy
698 **
699 **	Parameters:
700 **		fp -- file to put it on.
701 **		m -- mailer to use.
702 **		e -- envelope to use.
703 **
704 **	Returns:
705 **		none.
706 **
707 **	Side Effects:
708 **		none.
709 */
710 
711 /*
712  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
713  */
714 #ifndef MAX
715 # define MAX(a,b) (((a)>(b))?(a):(b))
716 #endif
717 
718 putheader(fp, m, e)
719 	register FILE *fp;
720 	register MAILER *m;
721 	register ENVELOPE *e;
722 {
723 	char buf[MAX(MAXLINE,BUFSIZ)];
724 	register HDR *h;
725 	char obuf[MAXLINE];
726 
727 	for (h = e->e_header; h != NULL; h = h->h_link)
728 	{
729 		register char *p;
730 		extern bool bitintersect();
731 
732 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
733 		    !bitintersect(h->h_mflags, m->m_flags))
734 			continue;
735 
736 		/* handle Resent-... headers specially */
737 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
738 			continue;
739 
740 		p = h->h_value;
741 		if (bitset(H_DEFAULT, h->h_flags))
742 		{
743 			/* macro expand value if generated internally */
744 			expand(p, buf, &buf[sizeof buf], e);
745 			p = buf;
746 			if (p == NULL || *p == '\0')
747 				continue;
748 		}
749 
750 		if (bitset(H_FROM|H_RCPT, h->h_flags))
751 		{
752 			/* address field */
753 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
754 
755 			if (bitset(H_FROM, h->h_flags))
756 				oldstyle = FALSE;
757 			commaize(h, p, fp, oldstyle, m, e);
758 		}
759 		else
760 		{
761 			/* vanilla header line */
762 			register char *nlp;
763 
764 			(void) sprintf(obuf, "%s: ", h->h_field);
765 			while ((nlp = strchr(p, '\n')) != NULL)
766 			{
767 				*nlp = '\0';
768 				(void) strcat(obuf, p);
769 				*nlp = '\n';
770 				putline(obuf, fp, m);
771 				p = ++nlp;
772 				obuf[0] = '\0';
773 			}
774 			(void) strcat(obuf, p);
775 			putline(obuf, fp, m);
776 		}
777 	}
778 }
779 /*
780 **  COMMAIZE -- output a header field, making a comma-translated list.
781 **
782 **	Parameters:
783 **		h -- the header field to output.
784 **		p -- the value to put in it.
785 **		fp -- file to put it to.
786 **		oldstyle -- TRUE if this is an old style header.
787 **		m -- a pointer to the mailer descriptor.  If NULL,
788 **			don't transform the name at all.
789 **		e -- the envelope containing the message.
790 **
791 **	Returns:
792 **		none.
793 **
794 **	Side Effects:
795 **		outputs "p" to file "fp".
796 */
797 
798 commaize(h, p, fp, oldstyle, m, e)
799 	register HDR *h;
800 	register char *p;
801 	FILE *fp;
802 	bool oldstyle;
803 	register MAILER *m;
804 	register ENVELOPE *e;
805 {
806 	register char *obp;
807 	int opos;
808 	bool firstone = TRUE;
809 	char obuf[MAXLINE + 3];
810 
811 	/*
812 	**  Output the address list translated by the
813 	**  mailer and with commas.
814 	*/
815 
816 	if (tTd(14, 2))
817 		printf("commaize(%s: %s)\n", h->h_field, p);
818 
819 	obp = obuf;
820 	(void) sprintf(obp, "%s: ", h->h_field);
821 	opos = strlen(h->h_field) + 2;
822 	obp += opos;
823 
824 	/*
825 	**  Run through the list of values.
826 	*/
827 
828 	while (*p != '\0')
829 	{
830 		register char *name;
831 		register int c;
832 		char savechar;
833 		int flags;
834 		auto int stat;
835 		extern char *remotename();
836 
837 		/*
838 		**  Find the end of the name.  New style names
839 		**  end with a comma, old style names end with
840 		**  a space character.  However, spaces do not
841 		**  necessarily delimit an old-style name -- at
842 		**  signs mean keep going.
843 		*/
844 
845 		/* find end of name */
846 		while ((isascii(*p) && isspace(*p)) || *p == ',')
847 			p++;
848 		name = p;
849 		for (;;)
850 		{
851 			auto char *oldp;
852 			char pvpbuf[PSBUFSIZE];
853 			extern char **prescan();
854 
855 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf, &oldp);
856 			p = oldp;
857 
858 			/* look to see if we have an at sign */
859 			while (*p != '\0' && isascii(*p) && isspace(*p))
860 				p++;
861 
862 			if (*p != '@')
863 			{
864 				p = oldp;
865 				break;
866 			}
867 			p += *p == '@' ? 1 : 2;
868 			while (*p != '\0' && isascii(*p) && isspace(*p))
869 				p++;
870 		}
871 		/* at the end of one complete name */
872 
873 		/* strip off trailing white space */
874 		while (p >= name &&
875 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
876 			p--;
877 		if (++p == name)
878 			continue;
879 		savechar = *p;
880 		*p = '\0';
881 
882 		/* translate the name to be relative */
883 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
884 		if (bitset(H_FROM, h->h_flags))
885 			flags |= RF_SENDERADDR;
886 		stat = EX_OK;
887 		name = remotename(name, m, flags, &stat, e);
888 		if (*name == '\0')
889 		{
890 			*p = savechar;
891 			continue;
892 		}
893 
894 		/* output the name with nice formatting */
895 		opos += strlen(name);
896 		if (!firstone)
897 			opos += 2;
898 		if (opos > 78 && !firstone)
899 		{
900 			(void) strcpy(obp, ",\n");
901 			putline(obuf, fp, m);
902 			obp = obuf;
903 			(void) sprintf(obp, "        ");
904 			opos = strlen(obp);
905 			obp += opos;
906 			opos += strlen(name);
907 		}
908 		else if (!firstone)
909 		{
910 			(void) sprintf(obp, ", ");
911 			obp += 2;
912 		}
913 
914 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
915 			*obp++ = c;
916 		firstone = FALSE;
917 		*p = savechar;
918 	}
919 	(void) strcpy(obp, "\n");
920 	putline(obuf, fp, m);
921 }
922 /*
923 **  COPYHEADER -- copy header list
924 **
925 **	This routine is the equivalent of newstr for header lists
926 **
927 **	Parameters:
928 **		header -- list of header structures to copy.
929 **
930 **	Returns:
931 **		a copy of 'header'.
932 **
933 **	Side Effects:
934 **		none.
935 */
936 
937 HDR *
938 copyheader(header)
939 	register HDR *header;
940 {
941 	register HDR *newhdr;
942 	HDR *ret;
943 	register HDR **tail = &ret;
944 
945 	while (header != NULL)
946 	{
947 		newhdr = (HDR *) xalloc(sizeof(HDR));
948 		STRUCTCOPY(*header, *newhdr);
949 		*tail = newhdr;
950 		tail = &newhdr->h_link;
951 		header = header->h_link;
952 	}
953 	*tail = NULL;
954 
955 	return ret;
956 }
957