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