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