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