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.23 (Berkeley) 01/24/94";
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 		{
378 			printf("%s: ", h->h_field);
379 			xputs(h->h_value);
380 			printf("\n");
381 		}
382 
383 		/* count the number of times it has been processed */
384 		if (bitset(H_TRACE, h->h_flags))
385 			hopcnt++;
386 
387 		/* send to this person if we so desire */
388 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
389 		    !bitset(H_DEFAULT, h->h_flags) &&
390 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
391 		{
392 			int saveflags = e->e_flags;
393 
394 			(void) sendtolist(h->h_value, NULLADDR,
395 					  &e->e_sendqueue, e);
396 
397 			/* delete fatal errors generated by this address */
398 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
399 				e->e_flags &= ~EF_FATALERRS;
400 		}
401 
402 		/* save the message-id for logging */
403 		if (full && strcasecmp(h->h_field, "message-id") == 0)
404 		{
405 			msgid = h->h_value;
406 			while (isascii(*msgid) && isspace(*msgid))
407 				msgid++;
408 		}
409 
410 		/* see if this is a return-receipt header */
411 		if (bitset(H_RECEIPTTO, h->h_flags))
412 			e->e_receiptto = h->h_value;
413 
414 		/* see if this is an errors-to header */
415 		if (UseErrorsTo && bitset(H_ERRORSTO, h->h_flags))
416 			(void) sendtolist(h->h_value, NULLADDR,
417 					  &e->e_errorqueue, e);
418 	}
419 	if (tTd(32, 1))
420 		printf("----------------------------\n");
421 
422 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
423 	if (OpMode == MD_VERIFY)
424 		return;
425 
426 	/* store hop count */
427 	if (hopcnt > e->e_hopcount)
428 		e->e_hopcount = hopcnt;
429 
430 	/* message priority */
431 	p = hvalue("precedence", e);
432 	if (p != NULL)
433 		e->e_class = priencode(p);
434 	if (full)
435 		e->e_msgpriority = e->e_msgsize
436 				 - e->e_class * WkClassFact
437 				 + e->e_nrcpts * WkRecipFact;
438 
439 	/* date message originated */
440 	p = hvalue("posted-date", e);
441 	if (p == NULL)
442 		p = hvalue("date", e);
443 	if (p != NULL)
444 		define('a', p, e);
445 
446 	/*
447 	**  Log collection information.
448 	*/
449 
450 # ifdef LOG
451 	if (full && LogLevel > 4)
452 		logsender(e, msgid);
453 # endif /* LOG */
454 	e->e_flags &= ~EF_LOGSENDER;
455 }
456 /*
457 **  LOGSENDER -- log sender information
458 **
459 **	Parameters:
460 **		e -- the envelope to log
461 **		msgid -- the message id
462 **
463 **	Returns:
464 **		none
465 */
466 
467 logsender(e, msgid)
468 	register ENVELOPE *e;
469 	char *msgid;
470 {
471 	char *name;
472 	register char *sbp;
473 	register char *p;
474 	char hbuf[MAXNAME];
475 	char sbuf[MAXLINE];
476 
477 	if (bitset(EF_RESPONSE, e->e_flags))
478 		name = "[RESPONSE]";
479 	else if ((name = macvalue('_', e)) != NULL)
480 		;
481 	else if (RealHostName[0] == '[')
482 		name = RealHostName;
483 	else
484 	{
485 		name = hbuf;
486 		(void) sprintf(hbuf, "%.80s", RealHostName);
487 		if (RealHostAddr.sa.sa_family != 0)
488 		{
489 			p = &hbuf[strlen(hbuf)];
490 			(void) sprintf(p, " (%s)",
491 				anynet_ntoa(&RealHostAddr));
492 		}
493 	}
494 
495 	/* some versions of syslog only take 5 printf args */
496 #  if (SYSLOG_BUFSIZE) >= 256
497 	sbp = sbuf;
498 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
499 	    e->e_from.q_paddr, e->e_msgsize, e->e_class,
500 	    e->e_msgpriority, e->e_nrcpts);
501 	sbp += strlen(sbp);
502 	if (msgid != NULL)
503 	{
504 		sprintf(sbp, ", msgid=%.100s", msgid);
505 		sbp += strlen(sbp);
506 	}
507 	if (e->e_bodytype != NULL)
508 	{
509 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
510 		sbp += strlen(sbp);
511 	}
512 	p = macvalue('r', e);
513 	if (p != NULL)
514 		(void) sprintf(sbp, ", proto=%.20s", p);
515 	syslog(LOG_INFO, "%s: %s, relay=%s",
516 	    e->e_id, sbuf, name);
517 
518 #  else			/* short syslog buffer */
519 
520 	syslog(LOG_INFO, "%s: from=%s",
521 		e->e_id, shortenstring(e->e_from.q_paddr, 83));
522 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
523 		e->e_id, e->e_msgsize, e->e_class,
524 		e->e_msgpriority, e->e_nrcpts);
525 	if (msgid != NULL)
526 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, msgid);
527 	sbp = sbuf;
528 	sprintf(sbp, "%s:", e->e_id);
529 	sbp += strlen(sbp);
530 	if (e->e_bodytype != NULL)
531 	{
532 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
533 		sbp += strlen(sbp);
534 	}
535 	p = macvalue('r', e);
536 	if (p != NULL)
537 	{
538 		sprintf(sbp, " proto=%s,", p);
539 		sbp += strlen(sbp);
540 	}
541 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
542 #  endif
543 }
544 /*
545 **  PRIENCODE -- encode external priority names into internal values.
546 **
547 **	Parameters:
548 **		p -- priority in ascii.
549 **
550 **	Returns:
551 **		priority as a numeric level.
552 **
553 **	Side Effects:
554 **		none.
555 */
556 
557 priencode(p)
558 	char *p;
559 {
560 	register int i;
561 
562 	for (i = 0; i < NumPriorities; i++)
563 	{
564 		if (!strcasecmp(p, Priorities[i].pri_name))
565 			return (Priorities[i].pri_val);
566 	}
567 
568 	/* unknown priority */
569 	return (0);
570 }
571 /*
572 **  CRACKADDR -- parse an address and turn it into a macro
573 **
574 **	This doesn't actually parse the address -- it just extracts
575 **	it and replaces it with "$g".  The parse is totally ad hoc
576 **	and isn't even guaranteed to leave something syntactically
577 **	identical to what it started with.  However, it does leave
578 **	something semantically identical.
579 **
580 **	This algorithm has been cleaned up to handle a wider range
581 **	of cases -- notably quoted and backslash escaped strings.
582 **	This modification makes it substantially better at preserving
583 **	the original syntax.
584 **
585 **	Parameters:
586 **		addr -- the address to be cracked.
587 **
588 **	Returns:
589 **		a pointer to the new version.
590 **
591 **	Side Effects:
592 **		none.
593 **
594 **	Warning:
595 **		The return value is saved in local storage and should
596 **		be copied if it is to be reused.
597 */
598 
599 char *
600 crackaddr(addr)
601 	register char *addr;
602 {
603 	register char *p;
604 	register char c;
605 	int cmtlev;
606 	int realcmtlev;
607 	int anglelev, realanglelev;
608 	int copylev;
609 	bool qmode;
610 	bool realqmode;
611 	bool skipping;
612 	bool putgmac = FALSE;
613 	bool quoteit = FALSE;
614 	bool gotangle = FALSE;
615 	register char *bp;
616 	char *buflim;
617 	static char buf[MAXNAME];
618 
619 	if (tTd(33, 1))
620 		printf("crackaddr(%s)\n", addr);
621 
622 	/* strip leading spaces */
623 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
624 		addr++;
625 
626 	/*
627 	**  Start by assuming we have no angle brackets.  This will be
628 	**  adjusted later if we find them.
629 	*/
630 
631 	bp = buf;
632 	buflim = &buf[sizeof buf - 5];
633 	p = addr;
634 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
635 	qmode = realqmode = FALSE;
636 
637 	while ((c = *p++) != '\0')
638 	{
639 		/*
640 		**  If the buffer is overful, go into a special "skipping"
641 		**  mode that tries to keep legal syntax but doesn't actually
642 		**  output things.
643 		*/
644 
645 		skipping = bp >= buflim;
646 
647 		if (copylev > 0 && !skipping)
648 			*bp++ = c;
649 
650 		/* check for backslash escapes */
651 		if (c == '\\')
652 		{
653 			/* arrange to quote the address */
654 			if (cmtlev <= 0 && !qmode)
655 				quoteit = TRUE;
656 
657 			if ((c = *p++) == '\0')
658 			{
659 				/* too far */
660 				p--;
661 				goto putg;
662 			}
663 			if (copylev > 0 && !skipping)
664 				*bp++ = c;
665 			goto putg;
666 		}
667 
668 		/* check for quoted strings */
669 		if (c == '"' && cmtlev <= 0)
670 		{
671 			qmode = !qmode;
672 			if (copylev > 0 && !skipping)
673 				realqmode = !realqmode;
674 			continue;
675 		}
676 		if (qmode)
677 			goto putg;
678 
679 		/* check for comments */
680 		if (c == '(')
681 		{
682 			cmtlev++;
683 
684 			/* allow space for closing paren */
685 			if (!skipping)
686 			{
687 				buflim--;
688 				realcmtlev++;
689 				if (copylev++ <= 0)
690 				{
691 					*bp++ = ' ';
692 					*bp++ = c;
693 				}
694 			}
695 		}
696 		if (cmtlev > 0)
697 		{
698 			if (c == ')')
699 			{
700 				cmtlev--;
701 				copylev--;
702 				if (!skipping)
703 				{
704 					realcmtlev--;
705 					buflim++;
706 				}
707 			}
708 			continue;
709 		}
710 		else if (c == ')')
711 		{
712 			/* syntax error: unmatched ) */
713 			if (copylev > 0 && !skipping)
714 				bp--;
715 		}
716 
717 		/* check for characters that may have to be quoted */
718 		if (strchr(".'@,;:\\()[]", c) != NULL)
719 		{
720 			/*
721 			**  If these occur as the phrase part of a <>
722 			**  construct, but are not inside of () or already
723 			**  quoted, they will have to be quoted.  Note that
724 			**  now (but don't actually do the quoting).
725 			*/
726 
727 			if (cmtlev <= 0 && !qmode)
728 				quoteit = TRUE;
729 		}
730 
731 		/* check for angle brackets */
732 		if (c == '<')
733 		{
734 			register char *q;
735 
736 			/* assume first of two angles is bogus */
737 			if (gotangle)
738 				quoteit = TRUE;
739 			gotangle = TRUE;
740 
741 			/* oops -- have to change our mind */
742 			anglelev = 1;
743 			if (!skipping)
744 				realanglelev = 1;
745 
746 			bp = buf;
747 			if (quoteit)
748 			{
749 				*bp++ = '"';
750 
751 				/* back up over the '<' and any spaces */
752 				--p;
753 				while (isascii(*--p) && isspace(*p))
754 					continue;
755 				p++;
756 			}
757 			for (q = addr; q < p; )
758 			{
759 				c = *q++;
760 				if (bp < buflim)
761 				{
762 					if (quoteit && c == '"')
763 						*bp++ = '\\';
764 					*bp++ = c;
765 				}
766 			}
767 			if (quoteit)
768 			{
769 				if (bp == &buf[1])
770 					bp--;
771 				else
772 					*bp++ = '"';
773 				while ((c = *p++) != '<')
774 				{
775 					if (bp < buflim)
776 						*bp++ = c;
777 				}
778 				*bp++ = c;
779 			}
780 			copylev = 0;
781 			putgmac = quoteit = FALSE;
782 			continue;
783 		}
784 
785 		if (c == '>')
786 		{
787 			if (anglelev > 0)
788 			{
789 				anglelev--;
790 				if (!skipping)
791 				{
792 					realanglelev--;
793 					buflim++;
794 				}
795 			}
796 			else if (!skipping)
797 			{
798 				/* syntax error: unmatched > */
799 				if (copylev > 0)
800 					bp--;
801 				quoteit = TRUE;
802 				continue;
803 			}
804 			if (copylev++ <= 0)
805 				*bp++ = c;
806 			continue;
807 		}
808 
809 		/* must be a real address character */
810 	putg:
811 		if (copylev <= 0 && !putgmac)
812 		{
813 			*bp++ = MACROEXPAND;
814 			*bp++ = 'g';
815 			putgmac = TRUE;
816 		}
817 	}
818 
819 	/* repair any syntactic damage */
820 	if (realqmode)
821 		*bp++ = '"';
822 	while (realcmtlev-- > 0)
823 		*bp++ = ')';
824 	while (realanglelev-- > 0)
825 		*bp++ = '>';
826 	*bp++ = '\0';
827 
828 	if (tTd(33, 1))
829 		printf("crackaddr=>`%s'\n", buf);
830 
831 	return (buf);
832 }
833 /*
834 **  PUTHEADER -- put the header part of a message from the in-core copy
835 **
836 **	Parameters:
837 **		mci -- the connection information.
838 **		e -- envelope to use.
839 **
840 **	Returns:
841 **		none.
842 **
843 **	Side Effects:
844 **		none.
845 */
846 
847 /*
848  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
849  */
850 #ifndef MAX
851 # define MAX(a,b) (((a)>(b))?(a):(b))
852 #endif
853 
854 putheader(mci, e)
855 	register MCI *mci;
856 	register ENVELOPE *e;
857 {
858 	char buf[MAX(MAXLINE,BUFSIZ)];
859 	register HDR *h;
860 	char obuf[MAXLINE];
861 
862 	if (tTd(34, 1))
863 		printf("--- putheader, mailer = %s ---\n",
864 			mci->mci_mailer->m_name);
865 
866 	for (h = e->e_header; h != NULL; h = h->h_link)
867 	{
868 		register char *p;
869 		extern bool bitintersect();
870 
871 		if (tTd(34, 11))
872 		{
873 			printf("  %s: ", h->h_field);
874 			xputs(h->h_value);
875 		}
876 
877 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
878 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
879 		{
880 			if (tTd(34, 11))
881 				printf(" (skipped)\n");
882 			continue;
883 		}
884 
885 		/* handle Resent-... headers specially */
886 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
887 		{
888 			if (tTd(34, 11))
889 				printf(" (skipped (resent))\n");
890 			continue;
891 		}
892 
893 		/* macro expand value if generated internally */
894 		p = h->h_value;
895 		if (bitset(H_DEFAULT, h->h_flags))
896 		{
897 			expand(p, buf, &buf[sizeof buf], e);
898 			p = buf;
899 			if (p == NULL || *p == '\0')
900 			{
901 				if (tTd(34, 11))
902 					printf(" (skipped -- null value)\n");
903 				continue;
904 			}
905 		}
906 
907 		if (tTd(34, 11))
908 			printf("\n");
909 
910 		if (bitset(H_FROM|H_RCPT, h->h_flags))
911 		{
912 			/* address field */
913 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
914 
915 			if (bitset(H_FROM, h->h_flags))
916 				oldstyle = FALSE;
917 			commaize(h, p, oldstyle, mci, e);
918 		}
919 		else
920 		{
921 			/* vanilla header line */
922 			register char *nlp;
923 
924 			(void) sprintf(obuf, "%s: ", h->h_field);
925 			while ((nlp = strchr(p, '\n')) != NULL)
926 			{
927 				*nlp = '\0';
928 				(void) strcat(obuf, p);
929 				*nlp = '\n';
930 				putline(obuf, mci);
931 				p = ++nlp;
932 				obuf[0] = '\0';
933 			}
934 			(void) strcat(obuf, p);
935 			putline(obuf, mci);
936 		}
937 	}
938 }
939 /*
940 **  COMMAIZE -- output a header field, making a comma-translated list.
941 **
942 **	Parameters:
943 **		h -- the header field to output.
944 **		p -- the value to put in it.
945 **		oldstyle -- TRUE if this is an old style header.
946 **		mci -- the connection information.
947 **		e -- the envelope containing the message.
948 **
949 **	Returns:
950 **		none.
951 **
952 **	Side Effects:
953 **		outputs "p" to file "fp".
954 */
955 
956 void
957 commaize(h, p, oldstyle, mci, e)
958 	register HDR *h;
959 	register char *p;
960 	bool oldstyle;
961 	register MCI *mci;
962 	register ENVELOPE *e;
963 {
964 	register char *obp;
965 	int opos;
966 	bool firstone = TRUE;
967 	char obuf[MAXLINE + 3];
968 
969 	/*
970 	**  Output the address list translated by the
971 	**  mailer and with commas.
972 	*/
973 
974 	if (tTd(14, 2))
975 		printf("commaize(%s: %s)\n", h->h_field, p);
976 
977 	obp = obuf;
978 	(void) sprintf(obp, "%s: ", h->h_field);
979 	opos = strlen(h->h_field) + 2;
980 	obp += opos;
981 
982 	/*
983 	**  Run through the list of values.
984 	*/
985 
986 	while (*p != '\0')
987 	{
988 		register char *name;
989 		register int c;
990 		char savechar;
991 		int flags;
992 		auto int stat;
993 
994 		/*
995 		**  Find the end of the name.  New style names
996 		**  end with a comma, old style names end with
997 		**  a space character.  However, spaces do not
998 		**  necessarily delimit an old-style name -- at
999 		**  signs mean keep going.
1000 		*/
1001 
1002 		/* find end of name */
1003 		while ((isascii(*p) && isspace(*p)) || *p == ',')
1004 			p++;
1005 		name = p;
1006 		for (;;)
1007 		{
1008 			auto char *oldp;
1009 			char pvpbuf[PSBUFSIZE];
1010 
1011 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
1012 				       sizeof pvpbuf, &oldp);
1013 			p = oldp;
1014 
1015 			/* look to see if we have an at sign */
1016 			while (*p != '\0' && isascii(*p) && isspace(*p))
1017 				p++;
1018 
1019 			if (*p != '@')
1020 			{
1021 				p = oldp;
1022 				break;
1023 			}
1024 			p += *p == '@' ? 1 : 2;
1025 			while (*p != '\0' && isascii(*p) && isspace(*p))
1026 				p++;
1027 		}
1028 		/* at the end of one complete name */
1029 
1030 		/* strip off trailing white space */
1031 		while (p >= name &&
1032 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
1033 			p--;
1034 		if (++p == name)
1035 			continue;
1036 		savechar = *p;
1037 		*p = '\0';
1038 
1039 		/* translate the name to be relative */
1040 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
1041 		if (bitset(H_FROM, h->h_flags))
1042 			flags |= RF_SENDERADDR;
1043 		stat = EX_OK;
1044 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
1045 		if (*name == '\0')
1046 		{
1047 			*p = savechar;
1048 			continue;
1049 		}
1050 
1051 		/* output the name with nice formatting */
1052 		opos += strlen(name);
1053 		if (!firstone)
1054 			opos += 2;
1055 		if (opos > 78 && !firstone)
1056 		{
1057 			(void) strcpy(obp, ",\n");
1058 			putline(obuf, mci);
1059 			obp = obuf;
1060 			(void) sprintf(obp, "        ");
1061 			opos = strlen(obp);
1062 			obp += opos;
1063 			opos += strlen(name);
1064 		}
1065 		else if (!firstone)
1066 		{
1067 			(void) sprintf(obp, ", ");
1068 			obp += 2;
1069 		}
1070 
1071 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
1072 			*obp++ = c;
1073 		firstone = FALSE;
1074 		*p = savechar;
1075 	}
1076 	(void) strcpy(obp, "\n");
1077 	putline(obuf, mci);
1078 }
1079 /*
1080 **  COPYHEADER -- copy header list
1081 **
1082 **	This routine is the equivalent of newstr for header lists
1083 **
1084 **	Parameters:
1085 **		header -- list of header structures to copy.
1086 **
1087 **	Returns:
1088 **		a copy of 'header'.
1089 **
1090 **	Side Effects:
1091 **		none.
1092 */
1093 
1094 HDR *
1095 copyheader(header)
1096 	register HDR *header;
1097 {
1098 	register HDR *newhdr;
1099 	HDR *ret;
1100 	register HDR **tail = &ret;
1101 
1102 	while (header != NULL)
1103 	{
1104 		newhdr = (HDR *) xalloc(sizeof(HDR));
1105 		STRUCTCOPY(*header, *newhdr);
1106 		*tail = newhdr;
1107 		tail = &newhdr->h_link;
1108 		header = header->h_link;
1109 	}
1110 	*tail = NULL;
1111 
1112 	return ret;
1113 }
1114