122706Sdist /*
2*34921Sbostic  * Copyright (c) 1983 Eric P. Allman
333729Sbostic  * Copyright (c) 1988 Regents of the University of California.
433729Sbostic  * All rights reserved.
533729Sbostic  *
633729Sbostic  * Redistribution and use in source and binary forms are permitted
7*34921Sbostic  * provided that the above copyright notice and this paragraph are
8*34921Sbostic  * duplicated in all such forms and that any documentation,
9*34921Sbostic  * advertising materials, and other materials related to such
10*34921Sbostic  * distribution and use acknowledge that the software was developed
11*34921Sbostic  * by the University of California, Berkeley.  The name of the
12*34921Sbostic  * University may not be used to endorse or promote products derived
13*34921Sbostic  * from this software without specific prior written permission.
14*34921Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15*34921Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16*34921Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733729Sbostic  */
1822706Sdist 
1922706Sdist #ifndef lint
20*34921Sbostic static char sccsid[] = "@(#)headers.c	5.10 (Berkeley) 06/30/88";
2133729Sbostic #endif /* not lint */
2222706Sdist 
234091Seric # include <errno.h>
244091Seric # include "sendmail.h"
254091Seric 
264091Seric /*
274091Seric **  CHOMPHEADER -- process and save a header line.
284091Seric **
294091Seric **	Called by collect and by readcf to deal with header lines.
304091Seric **
314091Seric **	Parameters:
324091Seric **		line -- header as a text line.
334091Seric **		def -- if set, this is a default value.
344091Seric **
354091Seric **	Returns:
364091Seric **		flags for this header.
374091Seric **
384091Seric **	Side Effects:
394091Seric **		The header is saved on the header list.
404319Seric **		Contents of 'line' are destroyed.
414091Seric */
424091Seric 
434091Seric chompheader(line, def)
444091Seric 	char *line;
454091Seric 	bool def;
464091Seric {
474091Seric 	register char *p;
484091Seric 	register HDR *h;
494091Seric 	HDR **hp;
504091Seric 	char *fname;
514091Seric 	char *fvalue;
524091Seric 	struct hdrinfo *hi;
539059Seric 	bool cond = FALSE;
5410689Seric 	BITMAP mopts;
557890Seric 	extern char *crackaddr();
564091Seric 
577677Seric # ifdef DEBUG
587677Seric 	if (tTd(31, 6))
597677Seric 		printf("chompheader: %s\n", line);
607677Seric # endif DEBUG
617677Seric 
624627Seric 	/* strip off options */
6310689Seric 	clrbitmap(mopts);
644627Seric 	p = line;
654627Seric 	if (*p == '?')
664627Seric 	{
674627Seric 		/* have some */
684627Seric 		register char *q = index(p + 1, *p);
694627Seric 
704627Seric 		if (q != NULL)
714627Seric 		{
724627Seric 			*q++ = '\0';
7310689Seric 			while (*++p != '\0')
7410689Seric 				setbitn(*p, mopts);
754627Seric 			p = q;
764627Seric 		}
774627Seric 		else
784627Seric 			syserr("chompheader: syntax error, line \"%s\"", line);
799059Seric 		cond = TRUE;
804627Seric 	}
814627Seric 
824091Seric 	/* find canonical name */
834627Seric 	fname = p;
844627Seric 	p = index(p, ':');
8510118Seric 	if (p == NULL)
8610118Seric 	{
8710118Seric 		syserr("chompheader: syntax error, line \"%s\"", line);
8810118Seric 		return (0);
8910118Seric 	}
904091Seric 	fvalue = &p[1];
914091Seric 	while (isspace(*--p))
924091Seric 		continue;
934091Seric 	*++p = '\0';
944091Seric 	makelower(fname);
954091Seric 
964091Seric 	/* strip field value on front */
974091Seric 	if (*fvalue == ' ')
984091Seric 		fvalue++;
994091Seric 
1004091Seric 	/* see if it is a known type */
1014091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1024091Seric 	{
1034091Seric 		if (strcmp(hi->hi_field, fname) == 0)
1044091Seric 			break;
1054091Seric 	}
1064091Seric 
10711414Seric 	/* see if this is a resent message */
10811930Seric 	if (!def && bitset(H_RESENT, hi->hi_flags))
10911414Seric 		CurEnv->e_flags |= EF_RESENT;
11011414Seric 
1114091Seric 	/* if this means "end of header" quit now */
1124091Seric 	if (bitset(H_EOH, hi->hi_flags))
1134091Seric 		return (hi->hi_flags);
1144091Seric 
11511414Seric 	/* drop explicit From: if same as what we would generate -- for MH */
11614784Seric 	p = "resent-from";
11714784Seric 	if (!bitset(EF_RESENT, CurEnv->e_flags))
11814784Seric 		p += 7;
11914784Seric 	if (!def && !QueueRun && strcmp(fname, p) == 0)
12011414Seric 	{
12124941Seric 		if (CurEnv->e_from.q_paddr != NULL &&
12224941Seric 		    strcmp(fvalue, CurEnv->e_from.q_paddr) == 0)
12311414Seric 			return (hi->hi_flags);
12411414Seric 	}
12511414Seric 
12614784Seric 	/* delete default value for this header */
12714784Seric 	for (hp = &CurEnv->e_header; (h = *hp) != NULL; hp = &h->h_link)
12814784Seric 	{
12914784Seric 		if (strcmp(fname, h->h_field) == 0 &&
13014784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
13114784Seric 		    !bitset(H_FORCE, h->h_flags))
13214784Seric 			h->h_value = NULL;
13314784Seric 	}
13414784Seric 
13513012Seric 	/* create a new node */
13613012Seric 	h = (HDR *) xalloc(sizeof *h);
13713012Seric 	h->h_field = newstr(fname);
13813012Seric 	h->h_value = NULL;
13913012Seric 	h->h_link = NULL;
14023117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
14113012Seric 	*hp = h;
1428066Seric 	h->h_flags = hi->hi_flags;
1434091Seric 	if (def)
1444091Seric 		h->h_flags |= H_DEFAULT;
1459059Seric 	if (cond)
1469059Seric 		h->h_flags |= H_CHECK;
1474091Seric 	if (h->h_value != NULL)
1489351Seric 		free((char *) h->h_value);
1498066Seric 	h->h_value = newstr(fvalue);
1504091Seric 
1515937Seric 	/* hack to see if this is a new format message */
1528095Seric 	if (!def && bitset(H_RCPT|H_FROM, h->h_flags) &&
1535937Seric 	    (index(fvalue, ',') != NULL || index(fvalue, '(') != NULL ||
1548089Seric 	     index(fvalue, '<') != NULL || index(fvalue, ';') != NULL))
1558089Seric 	{
1569342Seric 		CurEnv->e_flags &= ~EF_OLDSTYLE;
1578089Seric 	}
1585937Seric 
1594091Seric 	return (h->h_flags);
1604091Seric }
1614091Seric /*
1626980Seric **  ADDHEADER -- add a header entry to the end of the queue.
1636980Seric **
1646980Seric **	This bypasses the special checking of chompheader.
1656980Seric **
1666980Seric **	Parameters:
1676980Seric **		field -- the name of the header field.
1686980Seric **		value -- the value of the field.  It must be lower-cased.
1696980Seric **		e -- the envelope to add them to.
1706980Seric **
1716980Seric **	Returns:
1726980Seric **		none.
1736980Seric **
1746980Seric **	Side Effects:
1756980Seric **		adds the field on the list of headers for this envelope.
1766980Seric */
1776980Seric 
1786980Seric addheader(field, value, e)
1796980Seric 	char *field;
1806980Seric 	char *value;
1816980Seric 	ENVELOPE *e;
1826980Seric {
1836980Seric 	register HDR *h;
1846980Seric 	register struct hdrinfo *hi;
1856980Seric 	HDR **hp;
1866980Seric 
1876980Seric 	/* find info struct */
1886980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1896980Seric 	{
1906980Seric 		if (strcmp(field, hi->hi_field) == 0)
1916980Seric 			break;
1926980Seric 	}
1936980Seric 
1946980Seric 	/* find current place in list -- keep back pointer? */
1956980Seric 	for (hp = &e->e_header; (h = *hp) != NULL; hp = &h->h_link)
1966980Seric 	{
1976980Seric 		if (strcmp(field, h->h_field) == 0)
1986980Seric 			break;
1996980Seric 	}
2006980Seric 
2016980Seric 	/* allocate space for new header */
2026980Seric 	h = (HDR *) xalloc(sizeof *h);
2036980Seric 	h->h_field = field;
2046980Seric 	h->h_value = newstr(value);
2057368Seric 	h->h_link = *hp;
2066980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
20710689Seric 	clrbitmap(h->h_mflags);
2086980Seric 	*hp = h;
2096980Seric }
2106980Seric /*
2114091Seric **  HVALUE -- return value of a header.
2124091Seric **
2134091Seric **	Only "real" fields (i.e., ones that have not been supplied
2144091Seric **	as a default) are used.
2154091Seric **
2164091Seric **	Parameters:
2174091Seric **		field -- the field name.
2184091Seric **
2194091Seric **	Returns:
2204091Seric **		pointer to the value part.
2214091Seric **		NULL if not found.
2224091Seric **
2234091Seric **	Side Effects:
2249382Seric **		none.
2254091Seric */
2264091Seric 
2274091Seric char *
2284091Seric hvalue(field)
2294091Seric 	char *field;
2304091Seric {
2314091Seric 	register HDR *h;
2324091Seric 
2336908Seric 	for (h = CurEnv->e_header; h != NULL; h = h->h_link)
2344091Seric 	{
2354091Seric 		if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0)
2364091Seric 			return (h->h_value);
2374091Seric 	}
2384091Seric 	return (NULL);
2394091Seric }
2404091Seric /*
2414091Seric **  ISHEADER -- predicate telling if argument is a header.
2424091Seric **
2434319Seric **	A line is a header if it has a single word followed by
2444319Seric **	optional white space followed by a colon.
2454319Seric **
2464091Seric **	Parameters:
2474091Seric **		s -- string to check for possible headerness.
2484091Seric **
2494091Seric **	Returns:
2504091Seric **		TRUE if s is a header.
2514091Seric **		FALSE otherwise.
2524091Seric **
2534091Seric **	Side Effects:
2544091Seric **		none.
2554091Seric */
2564091Seric 
2574091Seric bool
2584091Seric isheader(s)
2594091Seric 	register char *s;
2604091Seric {
2619382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
2624091Seric 		s++;
2639382Seric 
2649382Seric 	/* following technically violates RFC822 */
2654091Seric 	while (isspace(*s))
2664091Seric 		s++;
2679382Seric 
2684091Seric 	return (*s == ':');
2694091Seric }
2705919Seric /*
2717783Seric **  EATHEADER -- run through the stored header and extract info.
2727783Seric **
2737783Seric **	Parameters:
2749382Seric **		e -- the envelope to process.
2757783Seric **
2767783Seric **	Returns:
2777783Seric **		none.
2787783Seric **
2797783Seric **	Side Effects:
2807783Seric **		Sets a bunch of global variables from information
2819382Seric **			in the collected header.
2829382Seric **		Aborts the message if the hop count is exceeded.
2837783Seric */
2847783Seric 
2859382Seric eatheader(e)
2869382Seric 	register ENVELOPE *e;
2877783Seric {
2887783Seric 	register HDR *h;
2897783Seric 	register char *p;
2909382Seric 	int hopcnt = 0;
2917783Seric 
2929382Seric #ifdef DEBUG
2939382Seric 	if (tTd(32, 1))
2949382Seric 		printf("----- collected header -----\n");
2959382Seric #endif DEBUG
2969382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
2977783Seric 	{
2989382Seric #ifdef DEBUG
2997783Seric 		extern char *capitalize();
3007783Seric 
3019382Seric 		if (tTd(32, 1))
3027783Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
3039382Seric #endif DEBUG
30411414Seric 		/* count the number of times it has been processed */
3059382Seric 		if (bitset(H_TRACE, h->h_flags))
3069382Seric 			hopcnt++;
30711414Seric 
30811414Seric 		/* send to this person if we so desire */
30911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
31011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
31111414Seric 		    (!bitset(EF_RESENT, CurEnv->e_flags) || bitset(H_RESENT, h->h_flags)))
31211414Seric 		{
31311414Seric 			sendtolist(h->h_value, (ADDRESS *) NULL, &CurEnv->e_sendqueue);
31411414Seric 		}
31511414Seric 
31611414Seric 		/* log the message-id */
31711290Seric #ifdef LOG
31813103Seric 		if (!QueueRun && LogLevel > 8 && h->h_value != NULL &&
31911299Seric 		    strcmp(h->h_field, "message-id") == 0)
32011290Seric 		{
32111290Seric 			char buf[MAXNAME];
32211290Seric 
32311290Seric 			p = h->h_value;
32411290Seric 			if (bitset(H_DEFAULT, h->h_flags))
32511290Seric 			{
32611290Seric 				expand(p, buf, &buf[sizeof buf], e);
32711290Seric 				p = buf;
32811290Seric 			}
32911290Seric 			syslog(LOG_INFO, "%s: message-id=%s", e->e_id, p);
33011290Seric 		}
33111290Seric #endif LOG
3329382Seric 	}
3339382Seric #ifdef DEBUG
3349382Seric 	if (tTd(32, 1))
3357783Seric 		printf("----------------------------\n");
3369382Seric #endif DEBUG
3377783Seric 
3389382Seric 	/* store hop count */
3399382Seric 	if (hopcnt > e->e_hopcount)
3409382Seric 		e->e_hopcount = hopcnt;
3419382Seric 
3427783Seric 	/* message priority */
3439382Seric 	p = hvalue("precedence");
3449382Seric 	if (p != NULL)
3459382Seric 		e->e_class = priencode(p);
3467783Seric 	if (!QueueRun)
34725013Seric 		e->e_msgpriority = e->e_msgsize
34824981Seric 				 - e->e_class * WkClassFact
34924981Seric 				 + e->e_nrcpts * WkRecipFact;
3507783Seric 
3518066Seric 	/* return receipt to */
3528066Seric 	p = hvalue("return-receipt-to");
3537783Seric 	if (p != NULL)
3549382Seric 		e->e_receiptto = p;
3557783Seric 
3568253Seric 	/* errors to */
3578253Seric 	p = hvalue("errors-to");
3588253Seric 	if (p != NULL)
3599620Seric 		sendtolist(p, (ADDRESS *) NULL, &e->e_errorqueue);
3608253Seric 
3617783Seric 	/* from person */
3629285Seric 	if (OpMode == MD_ARPAFTP)
3638066Seric 	{
3648066Seric 		register struct hdrinfo *hi = HdrInfo;
3657783Seric 
3668066Seric 		for (p = NULL; p == NULL && hi->hi_field != NULL; hi++)
3678066Seric 		{
3688066Seric 			if (bitset(H_FROM, hi->hi_flags))
3698066Seric 				p = hvalue(hi->hi_field);
3708066Seric 		}
3718066Seric 		if (p != NULL)
3729382Seric 			setsender(p);
3738066Seric 	}
3748066Seric 
3757783Seric 	/* full name of from person */
3767783Seric 	p = hvalue("full-name");
3777783Seric 	if (p != NULL)
3789382Seric 		define('x', p, e);
3797783Seric 
3807783Seric 	/* date message originated */
3817783Seric 	p = hvalue("posted-date");
3827783Seric 	if (p == NULL)
3837783Seric 		p = hvalue("date");
3847783Seric 	if (p != NULL)
3857783Seric 	{
3869382Seric 		define('a', p, e);
3877783Seric 		/* we don't have a good way to do canonical conversion ....
3889382Seric 		define('d', newstr(arpatounix(p)), e);
3897783Seric 		.... so we will ignore the problem for the time being */
3907783Seric 	}
39111290Seric 
39211290Seric 	/*
39311290Seric 	**  Log collection information.
39411290Seric 	*/
39511290Seric 
39611290Seric # ifdef LOG
39711299Seric 	if (!QueueRun && LogLevel > 1)
39811290Seric 	{
39911290Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
40011290Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
40111290Seric 		       CurEnv->e_class);
40211290Seric 	}
40311290Seric # endif LOG
4047783Seric }
4057783Seric /*
4067783Seric **  PRIENCODE -- encode external priority names into internal values.
4077783Seric **
4087783Seric **	Parameters:
4097783Seric **		p -- priority in ascii.
4107783Seric **
4117783Seric **	Returns:
4127783Seric **		priority as a numeric level.
4137783Seric **
4147783Seric **	Side Effects:
4157783Seric **		none.
4167783Seric */
4177783Seric 
4187783Seric priencode(p)
4197783Seric 	char *p;
4207783Seric {
4218253Seric 	register int i;
4227783Seric 
4238253Seric 	for (i = 0; i < NumPriorities; i++)
4247783Seric 	{
42533725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
4268253Seric 			return (Priorities[i].pri_val);
4277783Seric 	}
4288253Seric 
4298253Seric 	/* unknown priority */
4308253Seric 	return (0);
4317783Seric }
4327783Seric /*
4337890Seric **  CRACKADDR -- parse an address and turn it into a macro
4347783Seric **
4357783Seric **	This doesn't actually parse the address -- it just extracts
4367783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
4377783Seric **	and isn't even guaranteed to leave something syntactically
4387783Seric **	identical to what it started with.  However, it does leave
4397783Seric **	something semantically identical.
4407783Seric **
4417783Seric **	The process is kind of strange.  There are a number of
4427783Seric **	interesting cases:
4437783Seric **		1.  comment <address> comment	==> comment <$g> comment
4447783Seric **		2.  address			==> address
4457783Seric **		3.  address (comment)		==> $g (comment)
4467783Seric **		4.  (comment) address		==> (comment) $g
4477783Seric **	And then there are the hard cases....
4487783Seric **		5.  add (comment) ress		==> $g (comment)
4497783Seric **		6.  comment <address (comment)>	==> comment <$g (comment)>
4507783Seric **		7.    .... etc ....
4517783Seric **
4527783Seric **	Parameters:
4537890Seric **		addr -- the address to be cracked.
4547783Seric **
4557783Seric **	Returns:
4567783Seric **		a pointer to the new version.
4577783Seric **
4587783Seric **	Side Effects:
4597890Seric **		none.
4607783Seric **
4617783Seric **	Warning:
4627783Seric **		The return value is saved in local storage and should
4637783Seric **		be copied if it is to be reused.
4647783Seric */
4657783Seric 
4667783Seric char *
4677890Seric crackaddr(addr)
4687890Seric 	register char *addr;
4697783Seric {
4707783Seric 	register char *p;
4717783Seric 	register int i;
4727783Seric 	static char buf[MAXNAME];
4737783Seric 	char *rhs;
4747783Seric 	bool gotaddr;
4757783Seric 	register char *bp;
4767783Seric 
4777783Seric # ifdef DEBUG
4787783Seric 	if (tTd(33, 1))
4797890Seric 		printf("crackaddr(%s)\n", addr);
4807783Seric # endif DEBUG
4817783Seric 
48223099Seric 	(void) strcpy(buf, "");
4837783Seric 	rhs = NULL;
4847783Seric 
4858082Seric 	/* strip leading spaces */
4868082Seric 	while (*addr != '\0' && isspace(*addr))
4878082Seric 		addr++;
4888082Seric 
4897783Seric 	/*
4907783Seric 	**  See if we have anything in angle brackets.  If so, that is
4917783Seric 	**  the address part, and the rest is the comment.
4927783Seric 	*/
4937783Seric 
4947890Seric 	p = index(addr, '<');
4957783Seric 	if (p != NULL)
4967783Seric 	{
4977890Seric 		/* copy the beginning of the addr field to the buffer */
4987783Seric 		*p = '\0';
49923099Seric 		(void) strcpy(buf, addr);
50023099Seric 		(void) strcat(buf, "<");
5018082Seric 		*p++ = '<';
5027783Seric 
5038082Seric 		/* skip spaces */
5048082Seric 		while (isspace(*p))
5058082Seric 			p++;
5068082Seric 
5077783Seric 		/* find the matching right angle bracket */
5088082Seric 		addr = p;
5097783Seric 		for (i = 0; *p != '\0'; p++)
5107783Seric 		{
5117783Seric 			switch (*p)
5127783Seric 			{
5137783Seric 			  case '<':
5147783Seric 				i++;
5157783Seric 				break;
5167783Seric 
5177783Seric 			  case '>':
5187783Seric 				i--;
5197783Seric 				break;
5207783Seric 			}
5217783Seric 			if (i < 0)
5227783Seric 				break;
5237783Seric 		}
5247783Seric 
5257783Seric 		/* p now points to the closing quote (or a null byte) */
5267783Seric 		if (*p != '\0')
5277783Seric 		{
5287783Seric 			/* make rhs point to the extra stuff at the end */
5297783Seric 			rhs = p;
5307783Seric 			*p++ = '\0';
5317783Seric 		}
5327783Seric 	}
5337783Seric 
5347783Seric 	/*
5357944Seric 	**  Now parse the real address part.  "addr" points to the (null
5367783Seric 	**  terminated) version of what we are inerested in; rhs points
5377783Seric 	**  to the extra stuff at the end of the line, if any.
5387783Seric 	*/
5397783Seric 
5407890Seric 	p = addr;
5417783Seric 
5427783Seric 	/* now strip out comments */
5437783Seric 	bp = &buf[strlen(buf)];
5447783Seric 	gotaddr = FALSE;
5457783Seric 	for (; *p != '\0'; p++)
5467783Seric 	{
5477783Seric 		if (*p == '(')
5487783Seric 		{
5497783Seric 			/* copy to matching close paren */
5507783Seric 			*bp++ = *p++;
5517783Seric 			for (i = 0; *p != '\0'; p++)
5527783Seric 			{
5537783Seric 				*bp++ = *p;
5547783Seric 				switch (*p)
5557783Seric 				{
5567783Seric 				  case '(':
5577783Seric 					i++;
5587783Seric 					break;
5597783Seric 
5607783Seric 				  case ')':
5617783Seric 					i--;
5627783Seric 					break;
5637783Seric 				}
5647783Seric 				if (i < 0)
5657783Seric 					break;
5667783Seric 			}
5677783Seric 			continue;
5687783Seric 		}
5697783Seric 
5707783Seric 		/*
5717783Seric 		**  If this is the first "real" character we have seen,
5727783Seric 		**  then we put the "$g" in the buffer now.
5737783Seric 		*/
5747783Seric 
5757783Seric 		if (isspace(*p))
5767783Seric 			*bp++ = *p;
5777783Seric 		else if (!gotaddr)
5787783Seric 		{
57923099Seric 			(void) strcpy(bp, "\001g");
5807783Seric 			bp += 2;
5817783Seric 			gotaddr = TRUE;
5827783Seric 		}
5837783Seric 	}
5847783Seric 
5857944Seric 	/* hack, hack.... strip trailing blanks */
5867944Seric 	do
5877944Seric 	{
5887944Seric 		*bp-- = '\0';
5897944Seric 	} while (isspace(*bp));
5907944Seric 	bp++;
5917783Seric 
5927944Seric 	/* put any right hand side back on */
5937783Seric 	if (rhs != NULL)
5947783Seric 	{
5957783Seric 		*rhs = '>';
59623099Seric 		(void) strcpy(bp, rhs);
5977783Seric 	}
5987783Seric 
5997783Seric # ifdef DEBUG
6007783Seric 	if (tTd(33, 1))
6017944Seric 		printf("crackaddr=>`%s'\n", buf);
6027783Seric # endif DEBUG
6037783Seric 
6047783Seric 	return (buf);
6057783Seric }
6069382Seric /*
6079382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
6089382Seric **
6099382Seric **	Parameters:
6109382Seric **		fp -- file to put it on.
6119382Seric **		m -- mailer to use.
6129382Seric **		e -- envelope to use.
6139382Seric **
6149382Seric **	Returns:
6159382Seric **		none.
6169382Seric **
6179382Seric **	Side Effects:
6189382Seric **		none.
6199382Seric */
6209382Seric 
62110176Seric putheader(fp, m, e)
6229382Seric 	register FILE *fp;
6239382Seric 	register MAILER *m;
6249382Seric 	register ENVELOPE *e;
6259382Seric {
6269382Seric 	char buf[BUFSIZ];
6279382Seric 	register HDR *h;
6289382Seric 	extern char *arpadate();
6299382Seric 	extern char *capitalize();
6309382Seric 	char obuf[MAXLINE];
6319382Seric 
6329382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
6339382Seric 	{
6349382Seric 		register char *p;
63510689Seric 		extern bool bitintersect();
6369382Seric 
6379382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
63810689Seric 		    !bitintersect(h->h_mflags, m->m_flags))
6399382Seric 			continue;
6409382Seric 
64111414Seric 		/* handle Resent-... headers specially */
64211414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
64311414Seric 			continue;
64411414Seric 
6459382Seric 		p = h->h_value;
6469382Seric 		if (bitset(H_DEFAULT, h->h_flags))
6479382Seric 		{
6489382Seric 			/* macro expand value if generated internally */
6499382Seric 			expand(p, buf, &buf[sizeof buf], e);
6509382Seric 			p = buf;
6519382Seric 			if (p == NULL || *p == '\0')
6529382Seric 				continue;
6539382Seric 		}
6549382Seric 
6559382Seric 		if (bitset(H_FROM|H_RCPT, h->h_flags))
6569382Seric 		{
6579382Seric 			/* address field */
6589382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
6599382Seric 
6609382Seric 			if (bitset(H_FROM, h->h_flags))
6619382Seric 				oldstyle = FALSE;
66210176Seric 			commaize(h, p, fp, oldstyle, m);
6639382Seric 		}
6649382Seric 		else
6659382Seric 		{
6669382Seric 			/* vanilla header line */
66712159Seric 			register char *nlp;
66812159Seric 
66912159Seric 			(void) sprintf(obuf, "%s: ", capitalize(h->h_field));
67012159Seric 			while ((nlp = index(p, '\n')) != NULL)
67112159Seric 			{
67212159Seric 				*nlp = '\0';
67312159Seric 				(void) strcat(obuf, p);
67412159Seric 				*nlp = '\n';
67512159Seric 				putline(obuf, fp, m);
67612159Seric 				p = ++nlp;
67712161Seric 				obuf[0] = '\0';
67812159Seric 			}
67912159Seric 			(void) strcat(obuf, p);
68010176Seric 			putline(obuf, fp, m);
6819382Seric 		}
6829382Seric 	}
6839382Seric }
6849382Seric /*
6859382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
6869382Seric **
6879382Seric **	Parameters:
6889382Seric **		h -- the header field to output.
6899382Seric **		p -- the value to put in it.
6909382Seric **		fp -- file to put it to.
6919382Seric **		oldstyle -- TRUE if this is an old style header.
6929382Seric **		m -- a pointer to the mailer descriptor.  If NULL,
6939382Seric **			don't transform the name at all.
6949382Seric **
6959382Seric **	Returns:
6969382Seric **		none.
6979382Seric **
6989382Seric **	Side Effects:
6999382Seric **		outputs "p" to file "fp".
7009382Seric */
7019382Seric 
70210176Seric commaize(h, p, fp, oldstyle, m)
7039382Seric 	register HDR *h;
7049382Seric 	register char *p;
7059382Seric 	FILE *fp;
7069382Seric 	bool oldstyle;
7079382Seric 	register MAILER *m;
7089382Seric {
7099382Seric 	register char *obp;
7109382Seric 	int opos;
7119382Seric 	bool firstone = TRUE;
71211157Seric 	char obuf[MAXLINE + 3];
7139382Seric 
7149382Seric 	/*
7159382Seric 	**  Output the address list translated by the
7169382Seric 	**  mailer and with commas.
7179382Seric 	*/
7189382Seric 
7199382Seric # ifdef DEBUG
7209382Seric 	if (tTd(14, 2))
7219382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
7229382Seric # endif DEBUG
7239382Seric 
7249382Seric 	obp = obuf;
7259382Seric 	(void) sprintf(obp, "%s: ", capitalize(h->h_field));
7269382Seric 	opos = strlen(h->h_field) + 2;
7279382Seric 	obp += opos;
7289382Seric 
7299382Seric 	/*
7309382Seric 	**  Run through the list of values.
7319382Seric 	*/
7329382Seric 
7339382Seric 	while (*p != '\0')
7349382Seric 	{
7359382Seric 		register char *name;
7369382Seric 		char savechar;
7379382Seric 		extern char *remotename();
7389382Seric 		extern char *DelimChar;		/* defined in prescan */
7399382Seric 
7409382Seric 		/*
7419382Seric 		**  Find the end of the name.  New style names
7429382Seric 		**  end with a comma, old style names end with
7439382Seric 		**  a space character.  However, spaces do not
7449382Seric 		**  necessarily delimit an old-style name -- at
7459382Seric 		**  signs mean keep going.
7469382Seric 		*/
7479382Seric 
7489382Seric 		/* find end of name */
7499382Seric 		while (isspace(*p) || *p == ',')
7509382Seric 			p++;
7519382Seric 		name = p;
7529382Seric 		for (;;)
7539382Seric 		{
7549382Seric 			char *oldp;
75516909Seric 			char pvpbuf[PSBUFSIZE];
7569382Seric 			extern bool isatword();
7579382Seric 			extern char **prescan();
7589382Seric 
75916909Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf);
7609382Seric 			p = DelimChar;
7619382Seric 
7629382Seric 			/* look to see if we have an at sign */
7639382Seric 			oldp = p;
7649382Seric 			while (*p != '\0' && isspace(*p))
7659382Seric 				p++;
7669382Seric 
7679382Seric 			if (*p != '@' && !isatword(p))
7689382Seric 			{
7699382Seric 				p = oldp;
7709382Seric 				break;
7719382Seric 			}
7729382Seric 			p += *p == '@' ? 1 : 2;
7739382Seric 			while (*p != '\0' && isspace(*p))
7749382Seric 				p++;
7759382Seric 		}
7769382Seric 		/* at the end of one complete name */
7779382Seric 
7789382Seric 		/* strip off trailing white space */
7799382Seric 		while (p >= name && (isspace(*p) || *p == ',' || *p == '\0'))
7809382Seric 			p--;
7819382Seric 		if (++p == name)
7829382Seric 			continue;
7839382Seric 		savechar = *p;
7849382Seric 		*p = '\0';
7859382Seric 
7869382Seric 		/* translate the name to be relative */
78710309Seric 		name = remotename(name, m, bitset(H_FROM, h->h_flags), FALSE);
7889382Seric 		if (*name == '\0')
7899382Seric 		{
7909382Seric 			*p = savechar;
7919382Seric 			continue;
7929382Seric 		}
7939382Seric 
7949382Seric 		/* output the name with nice formatting */
7959382Seric 		opos += qstrlen(name);
7969382Seric 		if (!firstone)
7979382Seric 			opos += 2;
7989382Seric 		if (opos > 78 && !firstone)
7999382Seric 		{
80010178Seric 			(void) strcpy(obp, ",\n");
80110176Seric 			putline(obuf, fp, m);
8029382Seric 			obp = obuf;
8039382Seric 			(void) sprintf(obp, "        ");
80410161Seric 			opos = strlen(obp);
80510161Seric 			obp += opos;
80610161Seric 			opos += qstrlen(name);
8079382Seric 		}
8089382Seric 		else if (!firstone)
8099382Seric 		{
8109382Seric 			(void) sprintf(obp, ", ");
8119382Seric 			obp += 2;
8129382Seric 		}
8139382Seric 
8149382Seric 		/* strip off quote bits as we output */
81511157Seric 		while (*name != '\0' && obp < &obuf[MAXLINE])
8169382Seric 		{
8179382Seric 			if (bitset(0200, *name))
8189382Seric 				*obp++ = '\\';
8199382Seric 			*obp++ = *name++ & ~0200;
8209382Seric 		}
8219382Seric 		firstone = FALSE;
8229382Seric 		*p = savechar;
8239382Seric 	}
8249382Seric 	(void) strcpy(obp, "\n");
82510176Seric 	putline(obuf, fp, m);
8269382Seric }
8279382Seric /*
8289382Seric **  ISATWORD -- tell if the word we are pointing to is "at".
8299382Seric **
8309382Seric **	Parameters:
8319382Seric **		p -- word to check.
8329382Seric **
8339382Seric **	Returns:
8349382Seric **		TRUE -- if p is the word at.
8359382Seric **		FALSE -- otherwise.
8369382Seric **
8379382Seric **	Side Effects:
8389382Seric **		none.
8399382Seric */
8409382Seric 
8419382Seric bool
8429382Seric isatword(p)
8439382Seric 	register char *p;
8449382Seric {
8459382Seric 	extern char lower();
8469382Seric 
8479382Seric 	if (lower(p[0]) == 'a' && lower(p[1]) == 't' &&
8489382Seric 	    p[2] != '\0' && isspace(p[2]))
8499382Seric 		return (TRUE);
8509382Seric 	return (FALSE);
8519382Seric }
852