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