1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)headers.c	5.21 (Berkeley) 07/12/92";
11 #endif /* not lint */
12 
13 # include <sys/param.h>
14 # include <errno.h>
15 # include "sendmail.h"
16 
17 /*
18 **  CHOMPHEADER -- process and save a header line.
19 **
20 **	Called by collect and by readcf to deal with header lines.
21 **
22 **	Parameters:
23 **		line -- header as a text line.
24 **		def -- if set, this is a default value.
25 **		e -- the envelope including this header.
26 **
27 **	Returns:
28 **		flags for this header.
29 **
30 **	Side Effects:
31 **		The header is saved on the header list.
32 **		Contents of 'line' are destroyed.
33 */
34 
35 chompheader(line, def, e)
36 	char *line;
37 	bool def;
38 	register ENVELOPE *e;
39 {
40 	register char *p;
41 	register HDR *h;
42 	HDR **hp;
43 	char *fname;
44 	char *fvalue;
45 	struct hdrinfo *hi;
46 	bool cond = FALSE;
47 	BITMAP mopts;
48 	extern char *crackaddr();
49 
50 	if (tTd(31, 6))
51 		printf("chompheader: %s\n", line);
52 
53 	/* strip off options */
54 	clrbitmap(mopts);
55 	p = line;
56 	if (*p == '?')
57 	{
58 		/* have some */
59 		register char *q = index(p + 1, *p);
60 
61 		if (q != NULL)
62 		{
63 			*q++ = '\0';
64 			while (*++p != '\0')
65 				setbitn(*p, mopts);
66 			p = q;
67 		}
68 		else
69 			usrerr("chompheader: syntax error, line \"%s\"", line);
70 		cond = TRUE;
71 	}
72 
73 	/* find canonical name */
74 	fname = p;
75 	p = index(p, ':');
76 	if (p == NULL)
77 	{
78 		syserr("chompheader: syntax error, line \"%s\"", line);
79 		return (0);
80 	}
81 	fvalue = &p[1];
82 	while (isspace(*--p))
83 		continue;
84 	*++p = '\0';
85 	makelower(fname);
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 (strcmp(hi->hi_field, fname) == 0)
95 			break;
96 	}
97 
98 	/* see if this is a resent message */
99 	if (!def && bitset(H_RESENT, hi->hi_flags))
100 		e->e_flags |= EF_RESENT;
101 
102 	/* if this means "end of header" quit now */
103 	if (bitset(H_EOH, hi->hi_flags))
104 		return (hi->hi_flags);
105 
106 	/* drop explicit From: if same as what we would generate -- for MH */
107 	p = "resent-from";
108 	if (!bitset(EF_RESENT, e->e_flags))
109 		p += 7;
110 	if (!def && !QueueRun && strcmp(fname, p) == 0)
111 	{
112 		if (e->e_from.q_paddr != NULL &&
113 		    strcmp(fvalue, e->e_from.q_paddr) == 0)
114 			return (hi->hi_flags);
115 	}
116 
117 	/* delete default value for this header */
118 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
119 	{
120 		if (strcmp(fname, h->h_field) == 0 &&
121 		    bitset(H_DEFAULT, h->h_flags) &&
122 		    !bitset(H_FORCE, h->h_flags))
123 			h->h_value = NULL;
124 	}
125 
126 	/* create a new node */
127 	h = (HDR *) xalloc(sizeof *h);
128 	h->h_field = newstr(fname);
129 	h->h_value = NULL;
130 	h->h_link = NULL;
131 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
132 	*hp = h;
133 	h->h_flags = hi->hi_flags;
134 	if (def)
135 		h->h_flags |= H_DEFAULT;
136 	if (cond)
137 		h->h_flags |= H_CHECK;
138 	if (h->h_value != NULL)
139 		free((char *) h->h_value);
140 	h->h_value = newstr(fvalue);
141 
142 	/* hack to see if this is a new format message */
143 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
144 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
145 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
146 	{
147 		e->e_flags &= ~EF_OLDSTYLE;
148 	}
149 
150 	return (h->h_flags);
151 }
152 /*
153 **  ADDHEADER -- add a header entry to the end of the queue.
154 **
155 **	This bypasses the special checking of chompheader.
156 **
157 **	Parameters:
158 **		field -- the name of the header field.
159 **		value -- the value of the field.  It must be lower-cased.
160 **		e -- the envelope to add them to.
161 **
162 **	Returns:
163 **		none.
164 **
165 **	Side Effects:
166 **		adds the field on the list of headers for this envelope.
167 */
168 
169 addheader(field, value, e)
170 	char *field;
171 	char *value;
172 	ENVELOPE *e;
173 {
174 	register HDR *h;
175 	register struct hdrinfo *hi;
176 	HDR **hp;
177 
178 	/* find info struct */
179 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
180 	{
181 		if (strcmp(field, hi->hi_field) == 0)
182 			break;
183 	}
184 
185 	/* find current place in list -- keep back pointer? */
186 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
187 	{
188 		if (strcmp(field, h->h_field) == 0)
189 			break;
190 	}
191 
192 	/* allocate space for new header */
193 	h = (HDR *) xalloc(sizeof *h);
194 	h->h_field = field;
195 	h->h_value = newstr(value);
196 	h->h_link = *hp;
197 	h->h_flags = hi->hi_flags | H_DEFAULT;
198 	clrbitmap(h->h_mflags);
199 	*hp = h;
200 }
201 /*
202 **  HVALUE -- return value of a header.
203 **
204 **	Only "real" fields (i.e., ones that have not been supplied
205 **	as a default) are used.
206 **
207 **	Parameters:
208 **		field -- the field name.
209 **		e -- the envelope containing the header.
210 **
211 **	Returns:
212 **		pointer to the value part.
213 **		NULL if not found.
214 **
215 **	Side Effects:
216 **		none.
217 */
218 
219 char *
220 hvalue(field, e)
221 	char *field;
222 	register ENVELOPE *e;
223 {
224 	register HDR *h;
225 
226 	for (h = e->e_header; h != NULL; h = h->h_link)
227 	{
228 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
229 			return (h->h_value);
230 	}
231 	return (NULL);
232 }
233 /*
234 **  ISHEADER -- predicate telling if argument is a header.
235 **
236 **	A line is a header if it has a single word followed by
237 **	optional white space followed by a colon.
238 **
239 **	Parameters:
240 **		s -- string to check for possible headerness.
241 **
242 **	Returns:
243 **		TRUE if s is a header.
244 **		FALSE otherwise.
245 **
246 **	Side Effects:
247 **		none.
248 */
249 
250 bool
251 isheader(s)
252 	register char *s;
253 {
254 	while (*s > ' ' && *s != ':' && *s != '\0')
255 		s++;
256 
257 	/* following technically violates RFC822 */
258 	while (isspace(*s))
259 		s++;
260 
261 	return (*s == ':');
262 }
263 /*
264 **  EATHEADER -- run through the stored header and extract info.
265 **
266 **	Parameters:
267 **		e -- the envelope to process.
268 **
269 **	Returns:
270 **		none.
271 **
272 **	Side Effects:
273 **		Sets a bunch of global variables from information
274 **			in the collected header.
275 **		Aborts the message if the hop count is exceeded.
276 */
277 
278 eatheader(e)
279 	register ENVELOPE *e;
280 {
281 	register HDR *h;
282 	register char *p;
283 	int hopcnt = 0;
284 
285 	if (tTd(32, 1))
286 		printf("----- collected header -----\n");
287 	for (h = e->e_header; h != NULL; h = h->h_link)
288 	{
289 		extern char *capitalize();
290 
291 		if (tTd(32, 1))
292 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
293 		/* count the number of times it has been processed */
294 		if (bitset(H_TRACE, h->h_flags))
295 			hopcnt++;
296 
297 		/* send to this person if we so desire */
298 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
299 		    !bitset(H_DEFAULT, h->h_flags) &&
300 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
301 		{
302 			sendtolist(h->h_value, (ADDRESS *) NULL,
303 				   &e->e_sendqueue, e);
304 		}
305 
306 		/* log the message-id */
307 #ifdef LOG
308 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
309 		    strcmp(h->h_field, "message-id") == 0)
310 		{
311 			char buf[MAXNAME];
312 
313 			p = h->h_value;
314 			if (bitset(H_DEFAULT, h->h_flags))
315 			{
316 				expand(p, buf, &buf[sizeof buf], e);
317 				p = buf;
318 			}
319 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
320 		}
321 #endif LOG
322 	}
323 	if (tTd(32, 1))
324 		printf("----------------------------\n");
325 
326 	/* store hop count */
327 	if (hopcnt > e->e_hopcount)
328 		e->e_hopcount = hopcnt;
329 
330 	/* message priority */
331 	p = hvalue("precedence", e);
332 	if (p != NULL)
333 		e->e_class = priencode(p);
334 	if (!QueueRun)
335 		e->e_msgpriority = e->e_msgsize
336 				 - e->e_class * WkClassFact
337 				 + e->e_nrcpts * WkRecipFact;
338 
339 	/* return receipt to */
340 	p = hvalue("return-receipt-to", e);
341 	if (p != NULL)
342 		e->e_receiptto = p;
343 
344 	/* errors to */
345 	p = hvalue("errors-to", e);
346 	if (p != NULL)
347 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue, e);
348 
349 	/* full name of from person */
350 	p = hvalue("full-name", e);
351 	if (p != NULL)
352 		define('x', p, e);
353 
354 	/* date message originated */
355 	p = hvalue("posted-date", e);
356 	if (p == NULL)
357 		p = hvalue("date", e);
358 	if (p != NULL)
359 	{
360 		define('a', p, e);
361 		/* we don't have a good way to do canonical conversion ....
362 		define('d', newstr(arpatounix(p)), e);
363 		.... so we will ignore the problem for the time being */
364 	}
365 
366 	/*
367 	**  Log collection information.
368 	*/
369 
370 # ifdef LOG
371 	if (!QueueRun && LogLevel > 1)
372 	{
373 		char hbuf[100];
374 		char *name = hbuf;
375 		extern char *inet_ntoa();
376 
377 		if (RealHostName == NULL)
378 			name = "local";
379 		else if (RealHostName[0] == '[')
380 			name = RealHostName;
381 		else
382 			(void)sprintf(hbuf, "%.80s (%s)",
383 			    RealHostName, inet_ntoa(RealHostAddr.sin_addr));
384 		syslog(LOG_INFO,
385 		    "%s: from=%s, size=%ld, class=%d, received from %s\n",
386 		    e->e_id, e->e_from.q_paddr, e->e_msgsize,
387 		    e->e_class, name);
388 	}
389 # endif LOG
390 }
391 /*
392 **  PRIENCODE -- encode external priority names into internal values.
393 **
394 **	Parameters:
395 **		p -- priority in ascii.
396 **
397 **	Returns:
398 **		priority as a numeric level.
399 **
400 **	Side Effects:
401 **		none.
402 */
403 
404 priencode(p)
405 	char *p;
406 {
407 	register int i;
408 
409 	for (i = 0; i < NumPriorities; i++)
410 	{
411 		if (!strcasecmp(p, Priorities[i].pri_name))
412 			return (Priorities[i].pri_val);
413 	}
414 
415 	/* unknown priority */
416 	return (0);
417 }
418 /*
419 **  CRACKADDR -- parse an address and turn it into a macro
420 **
421 **	This doesn't actually parse the address -- it just extracts
422 **	it and replaces it with "$g".  The parse is totally ad hoc
423 **	and isn't even guaranteed to leave something syntactically
424 **	identical to what it started with.  However, it does leave
425 **	something semantically identical.
426 **
427 **	This algorithm has been cleaned up to handle a wider range
428 **	of cases -- notably quoted and backslash escaped strings.
429 **	This modification makes it substantially better at preserving
430 **	the original syntax.
431 **
432 **	Parameters:
433 **		addr -- the address to be cracked.
434 **
435 **	Returns:
436 **		a pointer to the new version.
437 **
438 **	Side Effects:
439 **		none.
440 **
441 **	Warning:
442 **		The return value is saved in local storage and should
443 **		be copied if it is to be reused.
444 */
445 
446 char *
447 crackaddr(addr)
448 	register char *addr;
449 {
450 	register char *p;
451 	register char c;
452 	int cmtlev;
453 	int copylev;
454 	bool qmode;
455 	bool putgmac = FALSE;
456 	register char *bp;
457 	static char buf[MAXNAME];
458 
459 	if (tTd(33, 1))
460 		printf("crackaddr(%s)\n", addr);
461 
462 	/* strip leading spaces */
463 	while (*addr != '\0' && isspace(*addr))
464 		addr++;
465 
466 	/*
467 	**  Start by assuming we have no angle brackets.  This will be
468 	**  adjusted later if we find them.
469 	*/
470 
471 	bp = buf;
472 	p = addr;
473 	copylev = cmtlev = 0;
474 	qmode = FALSE;
475 
476 	while ((c = *p++) != '\0')
477 	{
478 		if (copylev > 0 || c == ' ')
479 			*bp++ = c;
480 
481 		/* check for backslash escapes */
482 		if (c == '\\')
483 		{
484 			if ((c = *p++) == '\0')
485 			{
486 				/* too far */
487 				p--;
488 				goto putg;
489 			}
490 			if (copylev > 0)
491 				*bp++ = c;
492 			goto putg;
493 		}
494 
495 		/* check for quoted strings */
496 		if (c == '"')
497 		{
498 			qmode = !qmode;
499 			continue;
500 		}
501 		if (qmode)
502 			goto putg;
503 
504 		/* check for comments */
505 		if (c == '(')
506 		{
507 			cmtlev++;
508 			if (copylev++ <= 0)
509 				*bp++ = c;
510 		}
511 		if (cmtlev > 0)
512 		{
513 			if (c == ')')
514 			{
515 				cmtlev--;
516 				copylev--;
517 			}
518 			continue;
519 		}
520 
521 		/* check for angle brackets */
522 		if (c == '<')
523 		{
524 			/* oops -- have to change our mind */
525 			bcopy(addr, buf, p - addr);
526 			bp = &buf[p - addr];
527 			copylev = 0;
528 			putgmac = FALSE;
529 			continue;
530 		}
531 
532 		if (c == '>')
533 		{
534 			if (copylev++ <= 0)
535 				*bp++ = c;
536 			continue;
537 		}
538 
539 		/* must be a real address character */
540 	putg:
541 		if (copylev <= 0 && !putgmac)
542 		{
543 			*bp++ = '\001';
544 			*bp++ = 'g';
545 			putgmac = TRUE;
546 		}
547 	}
548 
549 	*bp++ = '\0';
550 
551 	if (tTd(33, 1))
552 		printf("crackaddr=>`%s'\n", buf);
553 
554 	return (buf);
555 }
556 /*
557 **  PUTHEADER -- put the header part of a message from the in-core copy
558 **
559 **	Parameters:
560 **		fp -- file to put it on.
561 **		m -- mailer to use.
562 **		e -- envelope to use.
563 **
564 **	Returns:
565 **		none.
566 **
567 **	Side Effects:
568 **		none.
569 */
570 
571 putheader(fp, m, e)
572 	register FILE *fp;
573 	register MAILER *m;
574 	register ENVELOPE *e;
575 {
576 	char buf[MAX(MAXFIELD,BUFSIZ)];
577 	register HDR *h;
578 	extern char *arpadate();
579 	extern char *capitalize();
580 	char obuf[MAX(MAXFIELD,MAXLINE)];
581 
582 	for (h = e->e_header; h != NULL; h = h->h_link)
583 	{
584 		register char *p;
585 		extern bool bitintersect();
586 
587 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
588 		    !bitintersect(h->h_mflags, m->m_flags))
589 			continue;
590 
591 		/* handle Resent-... headers specially */
592 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
593 			continue;
594 
595 		p = h->h_value;
596 		if (bitset(H_DEFAULT, h->h_flags))
597 		{
598 			/* macro expand value if generated internally */
599 			expand(p, buf, &buf[sizeof buf], e);
600 			p = buf;
601 			if (p == NULL || *p == '\0')
602 				continue;
603 		}
604 
605 		if (bitset(H_FROM|H_RCPT, h->h_flags))
606 		{
607 			/* address field */
608 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
609 
610 			if (bitset(H_FROM, h->h_flags))
611 				oldstyle = FALSE;
612 			commaize(h, p, fp, oldstyle, m, e);
613 		}
614 		else
615 		{
616 			/* vanilla header line */
617 			register char *nlp;
618 
619 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
620 			while ((nlp = index(p, '\n')) != NULL)
621 			{
622 				*nlp = '\0';
623 				(void) strcat(obuf, p);
624 				*nlp = '\n';
625 				putline(obuf, fp, m);
626 				p = ++nlp;
627 				obuf[0] = '\0';
628 			}
629 			(void) strcat(obuf, p);
630 			putline(obuf, fp, m);
631 		}
632 	}
633 }
634 /*
635 **  COMMAIZE -- output a header field, making a comma-translated list.
636 **
637 **	Parameters:
638 **		h -- the header field to output.
639 **		p -- the value to put in it.
640 **		fp -- file to put it to.
641 **		oldstyle -- TRUE if this is an old style header.
642 **		m -- a pointer to the mailer descriptor.  If NULL,
643 **			don't transform the name at all.
644 **		e -- the envelope containing the message.
645 **
646 **	Returns:
647 **		none.
648 **
649 **	Side Effects:
650 **		outputs "p" to file "fp".
651 */
652 
653 commaize(h, p, fp, oldstyle, m, e)
654 	register HDR *h;
655 	register char *p;
656 	FILE *fp;
657 	bool oldstyle;
658 	register MAILER *m;
659 	register ENVELOPE *e;
660 {
661 	register char *obp;
662 	int opos;
663 	bool firstone = TRUE;
664 	char obuf[MAXLINE + 3];
665 
666 	/*
667 	**  Output the address list translated by the
668 	**  mailer and with commas.
669 	*/
670 
671 	if (tTd(14, 2))
672 		printf("commaize(%s: %s)\n", h->h_field, p);
673 
674 	obp = obuf;
675 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
676 	opos = strlen(h->h_field) + 2;
677 	obp += opos;
678 
679 	/*
680 	**  Run through the list of values.
681 	*/
682 
683 	while (*p != '\0')
684 	{
685 		register char *name;
686 		register int c;
687 		char savechar;
688 		extern char *remotename();
689 		extern char *DelimChar;		/* defined in prescan */
690 
691 		/*
692 		**  Find the end of the name.  New style names
693 		**  end with a comma, old style names end with
694 		**  a space character.  However, spaces do not
695 		**  necessarily delimit an old-style name -- at
696 		**  signs mean keep going.
697 		*/
698 
699 		/* find end of name */
700 		while (isspace(*p) || *p == ',')
701 			p++;
702 		name = p;
703 		for (;;)
704 		{
705 			char *oldp;
706 			char pvpbuf[PSBUFSIZE];
707 			extern bool isatword();
708 			extern char **prescan();
709 
710 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
711 			p = DelimChar;
712 
713 			/* look to see if we have an at sign */
714 			oldp = p;
715 			while (*p != '\0' && isspace(*p))
716 				p++;
717 
718 			if (*p != '@' && !isatword(p))
719 			{
720 				p = oldp;
721 				break;
722 			}
723 			p += *p == '@' ? 1 : 2;
724 			while (*p != '\0' && isspace(*p))
725 				p++;
726 		}
727 		/* at the end of one complete name */
728 
729 		/* strip off trailing white space */
730 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
731 			p--;
732 		if (++p == name)
733 			continue;
734 		savechar = *p;
735 		*p = '\0';
736 
737 		/* translate the name to be relative */
738 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE, e);
739 		if (*name == '\0')
740 		{
741 			*p = savechar;
742 			continue;
743 		}
744 
745 		/* output the name with nice formatting */
746 		opos += strlen(name);
747 		if (!firstone)
748 			opos += 2;
749 		if (opos > 78 && !firstone)
750 		{
751 			(void) strcpy(obp, ",\n");
752 			putline(obuf, fp, m);
753 			obp = obuf;
754 			(void) sprintf(obp, "        ");
755 			opos = strlen(obp);
756 			obp += opos;
757 			opos += strlen(name);
758 		}
759 		else if (!firstone)
760 		{
761 			(void) sprintf(obp, ", ");
762 			obp += 2;
763 		}
764 
765 		/* strip off quote bits as we output */
766 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
767 		{
768 			if (bitnset(M_7BITS, m->m_flags))
769 				c &= 0177;
770 			*obp++ = c;
771 		}
772 		firstone = FALSE;
773 		*p = savechar;
774 	}
775 	(void) strcpy(obp, "\n");
776 	putline(obuf, fp, m);
777 }
778 /*
779 **  ISATWORD -- tell if the word we are pointing to is "at".
780 **
781 **	Parameters:
782 **		p -- word to check.
783 **
784 **	Returns:
785 **		TRUE -- if p is the word at.
786 **		FALSE -- otherwise.
787 **
788 **	Side Effects:
789 **		none.
790 */
791 
792 bool
793 isatword(p)
794 	register char *p;
795 {
796 	extern char lower();
797 
798 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
799 	    p[2] != '\0' && isspace(p[2]))
800 		return (TRUE);
801 	return (FALSE);
802 }
803