14091Seric # include <errno.h>
24091Seric # include "sendmail.h"
34091Seric 
4*5937Seric SCCSID(@(#)headers.c	3.16		02/22/82);
54091Seric 
64091Seric /*
74091Seric **  CHOMPHEADER -- process and save a header line.
84091Seric **
94091Seric **	Called by collect and by readcf to deal with header lines.
104091Seric **
114091Seric **	Parameters:
124091Seric **		line -- header as a text line.
134091Seric **		def -- if set, this is a default value.
144091Seric **
154091Seric **	Returns:
164091Seric **		flags for this header.
174091Seric **
184091Seric **	Side Effects:
194091Seric **		The header is saved on the header list.
204319Seric **		Contents of 'line' are destroyed.
214091Seric */
224091Seric 
234091Seric chompheader(line, def)
244091Seric 	char *line;
254091Seric 	bool def;
264091Seric {
274091Seric 	register char *p;
284091Seric 	register HDR *h;
294091Seric 	HDR **hp;
304091Seric 	extern bool isheader();
314091Seric 	char *fname;
324091Seric 	char *fvalue;
334091Seric 	struct hdrinfo *hi;
344627Seric 	u_long mopts;
354627Seric 	extern u_long mfencode();
364091Seric 
374091Seric 	/* strip off trailing newline */
384091Seric 	p = rindex(line, '\n');
394091Seric 	if (p != NULL)
404091Seric 		*p = '\0';
414091Seric 
424627Seric 	/* strip off options */
434627Seric 	mopts = 0;
444627Seric 	p = line;
454627Seric 	if (*p == '?')
464627Seric 	{
474627Seric 		/* have some */
484627Seric 		register char *q = index(p + 1, *p);
494627Seric 
504627Seric 		if (q != NULL)
514627Seric 		{
524627Seric 			*q++ = '\0';
534627Seric 			mopts = mfencode(p + 1);
544627Seric 			p = q;
554627Seric 		}
564627Seric 		else
574627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
584627Seric 	}
594627Seric 
604091Seric 	/* find canonical name */
614627Seric 	fname = p;
624627Seric 	p = index(p, ':');
634091Seric 	fvalue = &p[1];
644091Seric 	while (isspace(*--p))
654091Seric 		continue;
664091Seric 	*++p = '\0';
674091Seric 	makelower(fname);
684091Seric 
694091Seric 	/* strip field value on front */
704091Seric 	if (*fvalue == ' ')
714091Seric 		fvalue++;
724091Seric 
734372Seric 	/* hack, hack -- save From: line specially */
744372Seric 	if (!def && strcmp(fname, "from") == 0)
754372Seric 	{
764372Seric 		OrigFrom = newstr(fvalue);
774372Seric 		return (0);
784372Seric 	}
794372Seric 
804091Seric 	/* search header list for this header */
814091Seric 	for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link)
824091Seric 	{
835187Seric 		if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags))
844091Seric 			break;
854091Seric 	}
864091Seric 
874091Seric 	/* see if it is a known type */
884091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
894091Seric 	{
904091Seric 		if (strcmp(hi->hi_field, fname) == 0)
914091Seric 			break;
924091Seric 	}
934091Seric 
944091Seric 	/* if this means "end of header" quit now */
954091Seric 	if (bitset(H_EOH, hi->hi_flags))
964091Seric 		return (hi->hi_flags);
974091Seric 
985187Seric 	/* don't put timestamps in every queue run */
995187Seric 	if (QueueRun && h != NULL && bitset(H_FORCE, h->h_flags))
1005187Seric 		return (h->h_flags);
1015187Seric 
1024091Seric 	/* create/fill in a new node */
1035187Seric 	if (h == NULL || bitset(H_FORCE, h->h_flags))
1044091Seric 	{
1054091Seric 		/* create a new node */
1065187Seric 		h = (HDR *) xalloc(sizeof *h);
1074091Seric 		h->h_field = newstr(fname);
1084091Seric 		h->h_value = NULL;
1095187Seric 		h->h_link = *hp;
1104091Seric 		h->h_flags = hi->hi_flags;
1114627Seric 		h->h_mflags = mopts | hi->hi_mflags;
1125187Seric 		*hp = h;
1134091Seric 	}
1144091Seric 	if (def)
1154091Seric 		h->h_flags |= H_DEFAULT;
1164627Seric 	else if (mopts == 0)
1174091Seric 		h->h_flags &= ~H_CHECK;
1184091Seric 	if (h->h_value != NULL)
1194091Seric 		free(h->h_value);
1204091Seric 	h->h_value = newstr(fvalue);
1215919Seric 	if (!def && GrabTo && bitset(H_RCPT, h->h_flags))
1225919Seric 		sendto(h->h_value, 0, (ADDRESS *) NULL, &SendQueue);
1234091Seric 
124*5937Seric 	/* hack to see if this is a new format message */
125*5937Seric 	if (bitset(H_RCPT, h->h_flags) &&
126*5937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
127*5937Seric 	     index(fvalue, '<') != NULL))
128*5937Seric 		OldStyle = FALSE;
129*5937Seric 
1304091Seric 	return (h->h_flags);
1314091Seric }
1324091Seric /*
1334091Seric **  HVALUE -- return value of a header.
1344091Seric **
1354091Seric **	Only "real" fields (i.e., ones that have not been supplied
1364091Seric **	as a default) are used.
1374091Seric **
1384091Seric **	Parameters:
1394091Seric **		field -- the field name.
1404091Seric **
1414091Seric **	Returns:
1424091Seric **		pointer to the value part.
1434091Seric **		NULL if not found.
1444091Seric **
1454091Seric **	Side Effects:
1464091Seric **		sets the H_USED bit in the header if found.
1474091Seric */
1484091Seric 
1494091Seric char *
1504091Seric hvalue(field)
1514091Seric 	char *field;
1524091Seric {
1534091Seric 	register HDR *h;
1544091Seric 
1554091Seric 	for (h = Header; h != NULL; h = h->h_link)
1564091Seric 	{
1574091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
1584091Seric 		{
1594091Seric 			h->h_flags |= H_USED;
1604091Seric 			return (h->h_value);
1614091Seric 		}
1624091Seric 	}
1634091Seric 	return (NULL);
1644091Seric }
1654091Seric /*
1664091Seric **  ISHEADER -- predicate telling if argument is a header.
1674091Seric **
1684319Seric **	A line is a header if it has a single word followed by
1694319Seric **	optional white space followed by a colon.
1704319Seric **
1714091Seric **	Parameters:
1724091Seric **		s -- string to check for possible headerness.
1734091Seric **
1744091Seric **	Returns:
1754091Seric **		TRUE if s is a header.
1764091Seric **		FALSE otherwise.
1774091Seric **
1784091Seric **	Side Effects:
1794091Seric **		none.
1804319Seric **
1814319Seric **	Bugs:
1824319Seric **		According to RFC733, there should be a newline
1834319Seric **		permitted after the word but before the colon.
1844319Seric **		We don't seem to support that.....
1854091Seric */
1864091Seric 
1874091Seric bool
1884091Seric isheader(s)
1894091Seric 	register char *s;
1904091Seric {
1914091Seric 	if (!isalnum(*s))
1924091Seric 		return (FALSE);
1934091Seric 	while (!isspace(*s) && *s != ':')
1944091Seric 		s++;
1954091Seric 	while (isspace(*s))
1964091Seric 		s++;
1974091Seric 	return (*s == ':');
1984091Seric }
1995919Seric /*
2005919Seric **  GETXPART -- extract the "signature" part of an address line.
2015919Seric **
2025919Seric **	Try to extract the full name from a general address
2035919Seric **	field.  We take anything which is a comment as a
2045919Seric **	first choice.  Failing in that, we see if there is
2055919Seric **	a "machine readable" name (in <angle brackets>); if
2065919Seric **	so we take anything preceeding that clause.
2075919Seric **
2085919Seric **	If we blow it here it's not all that serious.
2095919Seric **
2105919Seric **	Parameters:
2115919Seric **		p -- line to crack.
2125919Seric **
2135919Seric **	Returns:
2145919Seric **		signature part.
2155919Seric **		NULL if no signature part.
2165919Seric **
2175919Seric **	Side Effects:
2185919Seric **		none.
2195919Seric */
2205919Seric 
2215919Seric char *
2225919Seric getxpart(p)
2235919Seric 	register char *p;
2245919Seric {
2255919Seric 	register char *q;
2265919Seric 	register char *rval = NULL;
2275919Seric 
2285919Seric 	q = index(p, '(');
2295919Seric 	if (q != NULL)
2305919Seric 	{
2315919Seric 		int parenlev = 0;
2325919Seric 
2335919Seric 		for (p = q; *p != '\0'; p++)
2345919Seric 		{
2355919Seric 			if (*p == '(')
2365919Seric 				parenlev++;
2375919Seric 			else if (*p == ')' && --parenlev <= 0)
2385919Seric 				break;
2395919Seric 		}
2405919Seric 		if (*p == ')')
2415919Seric 		{
2425919Seric 			*p = '\0';
2435919Seric 			if (*++q != '\0')
2445919Seric 				rval = newstr(q);
2455919Seric 			*p = ')';
2465919Seric 		}
2475919Seric 	}
2485919Seric 	else if ((q = index(p, '<')) != NULL)
2495919Seric 	{
2505919Seric 		char savec;
2515919Seric 
2525919Seric 		while (*--q == ' ')
2535919Seric 			continue;
2545919Seric 		while (isspace(*p))
2555919Seric 			p++;
2565919Seric 		savec = *++q;
2575919Seric 		*q = '\0';
2585919Seric 		if (*p != '\0')
2595919Seric 			rval = newstr(p);
2605919Seric 		*q = savec;
2615919Seric 	}
2625919Seric 
2635919Seric 	return (rval);
2645919Seric }
265