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