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