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