122706Sdist /*
234921Sbostic  * Copyright (c) 1983 Eric P. Allman
362525Sbostic  * Copyright (c) 1988, 1993
462525Sbostic  *	The Regents of the University of California.  All rights reserved.
533729Sbostic  *
642826Sbostic  * %sccs.include.redist.c%
733729Sbostic  */
822706Sdist 
922706Sdist #ifndef lint
10*68812Seric static char sccsid[] = "@(#)headers.c	8.57 (Berkeley) 04/20/95";
1133729Sbostic #endif /* not lint */
1222706Sdist 
134091Seric # include <errno.h>
144091Seric # include "sendmail.h"
154091Seric 
164091Seric /*
174091Seric **  CHOMPHEADER -- process and save a header line.
184091Seric **
194091Seric **	Called by collect and by readcf to deal with header lines.
204091Seric **
214091Seric **	Parameters:
224091Seric **		line -- header as a text line.
234091Seric **		def -- if set, this is a default value.
2468717Seric **		hdrp -- a pointer to the place to save the header.
2555012Seric **		e -- the envelope including this header.
264091Seric **
274091Seric **	Returns:
284091Seric **		flags for this header.
294091Seric **
304091Seric **	Side Effects:
314091Seric **		The header is saved on the header list.
324319Seric **		Contents of 'line' are destroyed.
334091Seric */
344091Seric 
3568717Seric chompheader(line, def, hdrp, e)
364091Seric 	char *line;
374091Seric 	bool def;
3868717Seric 	HDR **hdrp;
3955012Seric 	register ENVELOPE *e;
404091Seric {
414091Seric 	register char *p;
424091Seric 	register HDR *h;
434091Seric 	HDR **hp;
444091Seric 	char *fname;
454091Seric 	char *fvalue;
464091Seric 	struct hdrinfo *hi;
479059Seric 	bool cond = FALSE;
4868717Seric 	bool headeronly;
4910689Seric 	BITMAP mopts;
5068528Seric 	char buf[MAXNAME + 1];
514091Seric 
527677Seric 	if (tTd(31, 6))
53*68812Seric 	{
54*68812Seric 		printf("chompheader: ");
55*68812Seric 		xputs(line);
56*68812Seric 		printf("\n");
57*68812Seric 	}
587677Seric 
5968717Seric 	headeronly = hdrp != NULL;
6068717Seric 	if (!headeronly)
6168717Seric 		hdrp = &e->e_header;
6268717Seric 
634627Seric 	/* strip off options */
6410689Seric 	clrbitmap(mopts);
654627Seric 	p = line;
6657405Seric 	if (*p == '?')
674627Seric 	{
684627Seric 		/* have some */
6956795Seric 		register char *q = strchr(p + 1, *p);
704627Seric 
714627Seric 		if (q != NULL)
724627Seric 		{
734627Seric 			*q++ = '\0';
7410689Seric 			while (*++p != '\0')
7510689Seric 				setbitn(*p, mopts);
764627Seric 			p = q;
774627Seric 		}
784627Seric 		else
7968690Seric 			syserr("553 header syntax error, line \"%s\"", line);
809059Seric 		cond = TRUE;
814627Seric 	}
824627Seric 
834091Seric 	/* find canonical name */
844627Seric 	fname = p;
8564149Seric 	while (isascii(*p) && isgraph(*p) && *p != ':')
8664149Seric 		p++;
8764149Seric 	fvalue = p;
8864149Seric 	while (isascii(*p) && isspace(*p))
8964149Seric 		p++;
9064150Seric 	if (*p++ != ':' || fname == fvalue)
9110118Seric 	{
9258151Seric 		syserr("553 header syntax error, line \"%s\"", line);
9310118Seric 		return (0);
9410118Seric 	}
9564149Seric 	*fvalue = '\0';
9664149Seric 	fvalue = p;
974091Seric 
984091Seric 	/* strip field value on front */
994091Seric 	if (*fvalue == ' ')
1004091Seric 		fvalue++;
1014091Seric 
1024091Seric 	/* see if it is a known type */
1034091Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
1044091Seric 	{
10559579Seric 		if (strcasecmp(hi->hi_field, fname) == 0)
1064091Seric 			break;
1074091Seric 	}
1084091Seric 
10964283Seric 	if (tTd(31, 9))
11064283Seric 	{
11164283Seric 		if (hi->hi_field == NULL)
11264283Seric 			printf("no header match\n");
11364283Seric 		else
11467694Seric 			printf("header match, hi_flags=%x\n", hi->hi_flags);
11564283Seric 	}
11664283Seric 
11711414Seric 	/* see if this is a resent message */
11868717Seric 	if (!def && !headeronly && bitset(H_RESENT, hi->hi_flags))
11955012Seric 		e->e_flags |= EF_RESENT;
12011414Seric 
12168558Seric 	/* if this is an Errors-To: header keep track of it now */
12268717Seric 	if (UseErrorsTo && !def && !headeronly &&
12368717Seric 	    bitset(H_ERRORSTO, hi->hi_flags))
12468558Seric 		(void) sendtolist(fvalue, NULLADDR, &e->e_errorqueue, 0, e);
12568558Seric 
1264091Seric 	/* if this means "end of header" quit now */
1274091Seric 	if (bitset(H_EOH, hi->hi_flags))
1284091Seric 		return (hi->hi_flags);
1294091Seric 
13064653Seric 	/*
13164653Seric 	**  Drop explicit From: if same as what we would generate.
13264653Seric 	**  This is to make MH (which doesn't always give a full name)
13364653Seric 	**  insert the full name information in all circumstances.
13464653Seric 	*/
13564653Seric 
13614784Seric 	p = "resent-from";
13755012Seric 	if (!bitset(EF_RESENT, e->e_flags))
13814784Seric 		p += 7;
13968717Seric 	if (!def && !headeronly && !bitset(EF_QUEUERUN, e->e_flags) &&
14068717Seric 	    strcasecmp(fname, p) == 0)
14111414Seric 	{
14263753Seric 		if (tTd(31, 2))
14363753Seric 		{
14463753Seric 			printf("comparing header from (%s) against default (%s or %s)\n",
14563753Seric 				fvalue, e->e_from.q_paddr, e->e_from.q_user);
14663753Seric 		}
14755012Seric 		if (e->e_from.q_paddr != NULL &&
14863753Seric 		    (strcmp(fvalue, e->e_from.q_paddr) == 0 ||
14963753Seric 		     strcmp(fvalue, e->e_from.q_user) == 0))
15011414Seric 			return (hi->hi_flags);
15164351Seric #ifdef MAYBENEXTRELEASE		/* XXX UNTESTED XXX UNTESTED XXX UNTESTED XXX */
15264351Seric #ifdef USERDB
15364351Seric 		else
15464351Seric 		{
15564351Seric 			auto ADDRESS a;
15664351Seric 			char *fancy;
15766123Seric 			bool oldSuprErrs = SuprErrs;
15864351Seric 			extern char *crackaddr();
15964351Seric 			extern char *udbsender();
16064351Seric 
16164653Seric 			/*
16264653Seric 			**  Try doing USERDB rewriting even on fully commented
16364653Seric 			**  names; this saves the "comment" information (such
16464653Seric 			**  as full name) and rewrites the electronic part.
16566106Seric 			**
16666106Seric 			** XXX	This code doesn't belong here -- parsing should
16766106Seric 			** XXX	not be done during collect() phase because
16866106Seric 			** XXX	error messages can confuse the SMTP phase.
16966123Seric 			** XXX	Setting SuprErrs is a crude hack around this
17066106Seric 			** XXX	problem.
17164653Seric 			*/
17264653Seric 
17366106Seric 			if (OpMode == MD_SMTP || OpMode == MD_ARPAFTP)
17466123Seric 				SuprErrs = TRUE;
17564351Seric 			fancy = crackaddr(fvalue);
17664351Seric 			if (parseaddr(fvalue, &a, RF_COPYNONE, '\0', NULL, e) != NULL &&
17767472Seric 			    bitnset(M_CHECKUDB, a.q_mailer->m_flags) &&
17864351Seric 			    (p = udbsender(a.q_user)) != NULL)
17964351Seric 			{
18064351Seric 				char *oldg = macvalue('g', e);
18164351Seric 
18264351Seric 				define('g', p, e);
18368529Seric 				expand(fancy, buf, sizeof buf, e);
18464351Seric 				define('g', oldg, e);
18564351Seric 				fvalue = buf;
18664351Seric 			}
18766123Seric 			SuprErrs = oldSuprErrs;
18864351Seric 		}
18964351Seric #endif
19064351Seric #endif
19111414Seric 	}
19211414Seric 
19314784Seric 	/* delete default value for this header */
19468717Seric 	for (hp = hdrp; (h = *hp) != NULL; hp = &h->h_link)
19514784Seric 	{
19659579Seric 		if (strcasecmp(fname, h->h_field) == 0 &&
19714784Seric 		    bitset(H_DEFAULT, h->h_flags) &&
19814784Seric 		    !bitset(H_FORCE, h->h_flags))
19914784Seric 			h->h_value = NULL;
20014784Seric 	}
20114784Seric 
20213012Seric 	/* create a new node */
20313012Seric 	h = (HDR *) xalloc(sizeof *h);
20413012Seric 	h->h_field = newstr(fname);
20564134Seric 	h->h_value = newstr(fvalue);
20613012Seric 	h->h_link = NULL;
20723117Seric 	bcopy((char *) mopts, (char *) h->h_mflags, sizeof mopts);
20813012Seric 	*hp = h;
2098066Seric 	h->h_flags = hi->hi_flags;
2104091Seric 	if (def)
2114091Seric 		h->h_flags |= H_DEFAULT;
2129059Seric 	if (cond)
2139059Seric 		h->h_flags |= H_CHECK;
2144091Seric 
2155937Seric 	/* hack to see if this is a new format message */
21668717Seric 	if (!def && !headeronly && bitset(H_RCPT|H_FROM, h->h_flags) &&
21756795Seric 	    (strchr(fvalue, ',') != NULL || strchr(fvalue, '(') != NULL ||
21856795Seric 	     strchr(fvalue, '<') != NULL || strchr(fvalue, ';') != NULL))
2198089Seric 	{
22055012Seric 		e->e_flags &= ~EF_OLDSTYLE;
2218089Seric 	}
2225937Seric 
2234091Seric 	return (h->h_flags);
2244091Seric }
2254091Seric /*
2266980Seric **  ADDHEADER -- add a header entry to the end of the queue.
2276980Seric **
2286980Seric **	This bypasses the special checking of chompheader.
2296980Seric **
2306980Seric **	Parameters:
23159579Seric **		field -- the name of the header field.
23258789Seric **		value -- the value of the field.
23367546Seric **		hp -- an indirect pointer to the header structure list.
2346980Seric **
2356980Seric **	Returns:
2366980Seric **		none.
2376980Seric **
2386980Seric **	Side Effects:
2396980Seric **		adds the field on the list of headers for this envelope.
2406980Seric */
2416980Seric 
24267546Seric addheader(field, value, hdrlist)
2436980Seric 	char *field;
2446980Seric 	char *value;
24567546Seric 	HDR **hdrlist;
2466980Seric {
2476980Seric 	register HDR *h;
2486980Seric 	register struct hdrinfo *hi;
2496980Seric 	HDR **hp;
2506980Seric 
2516980Seric 	/* find info struct */
2526980Seric 	for (hi = HdrInfo; hi->hi_field != NULL; hi++)
2536980Seric 	{
25459579Seric 		if (strcasecmp(field, hi->hi_field) == 0)
2556980Seric 			break;
2566980Seric 	}
2576980Seric 
2586980Seric 	/* find current place in list -- keep back pointer? */
25967546Seric 	for (hp = hdrlist; (h = *hp) != NULL; hp = &h->h_link)
2606980Seric 	{
26159579Seric 		if (strcasecmp(field, h->h_field) == 0)
2626980Seric 			break;
2636980Seric 	}
2646980Seric 
2656980Seric 	/* allocate space for new header */
2666980Seric 	h = (HDR *) xalloc(sizeof *h);
2676980Seric 	h->h_field = field;
2686980Seric 	h->h_value = newstr(value);
2697368Seric 	h->h_link = *hp;
2706980Seric 	h->h_flags = hi->hi_flags | H_DEFAULT;
27110689Seric 	clrbitmap(h->h_mflags);
2726980Seric 	*hp = h;
2736980Seric }
2746980Seric /*
2754091Seric **  HVALUE -- return value of a header.
2764091Seric **
2774091Seric **	Only "real" fields (i.e., ones that have not been supplied
2784091Seric **	as a default) are used.
2794091Seric **
2804091Seric **	Parameters:
2814091Seric **		field -- the field name.
28267546Seric **		header -- the header list.
2834091Seric **
2844091Seric **	Returns:
2854091Seric **		pointer to the value part.
2864091Seric **		NULL if not found.
2874091Seric **
2884091Seric **	Side Effects:
2899382Seric **		none.
2904091Seric */
2914091Seric 
2924091Seric char *
29367546Seric hvalue(field, header)
2944091Seric 	char *field;
29567546Seric 	HDR *header;
2964091Seric {
2974091Seric 	register HDR *h;
2984091Seric 
29967546Seric 	for (h = header; h != NULL; h = h->h_link)
3004091Seric 	{
30159579Seric 		if (!bitset(H_DEFAULT, h->h_flags) &&
30259579Seric 		    strcasecmp(h->h_field, field) == 0)
3034091Seric 			return (h->h_value);
3044091Seric 	}
3054091Seric 	return (NULL);
3064091Seric }
3074091Seric /*
3084091Seric **  ISHEADER -- predicate telling if argument is a header.
3094091Seric **
3104319Seric **	A line is a header if it has a single word followed by
3114319Seric **	optional white space followed by a colon.
3124319Seric **
31368717Seric **	Header fields beginning with two dashes, although technically
31468717Seric **	permitted by RFC822, are automatically rejected in order
31568717Seric **	to make MIME work out.  Without this we could have a technically
31668717Seric **	legal header such as ``--"foo:bar"'' that would also be a legal
31768717Seric **	MIME separator.
31868717Seric **
3194091Seric **	Parameters:
32068690Seric **		h -- string to check for possible headerness.
3214091Seric **
3224091Seric **	Returns:
32368690Seric **		TRUE if h is a header.
3244091Seric **		FALSE otherwise.
3254091Seric **
3264091Seric **	Side Effects:
3274091Seric **		none.
3284091Seric */
3294091Seric 
3304091Seric bool
33168690Seric isheader(h)
33268690Seric 	char *h;
3334091Seric {
33468690Seric 	register char *s = h;
33568690Seric 
33668717Seric 	if (s[0] == '-' && s[1] == '-')
33768717Seric 		return FALSE;
33868717Seric 
3399382Seric 	while (*s > ' ' && *s != ':' && *s != '\0')
3404091Seric 		s++;
3419382Seric 
34268690Seric 	if (h == s)
34368690Seric 		return FALSE;
34468690Seric 
3459382Seric 	/* following technically violates RFC822 */
34658050Seric 	while (isascii(*s) && isspace(*s))
3474091Seric 		s++;
3489382Seric 
3494091Seric 	return (*s == ':');
3504091Seric }
3515919Seric /*
3527783Seric **  EATHEADER -- run through the stored header and extract info.
3537783Seric **
3547783Seric **	Parameters:
3559382Seric **		e -- the envelope to process.
35658929Seric **		full -- if set, do full processing (e.g., compute
35758929Seric **			message priority).
3587783Seric **
3597783Seric **	Returns:
3607783Seric **		none.
3617783Seric **
3627783Seric **	Side Effects:
3637783Seric **		Sets a bunch of global variables from information
3649382Seric **			in the collected header.
3659382Seric **		Aborts the message if the hop count is exceeded.
3667783Seric */
3677783Seric 
36858929Seric eatheader(e, full)
3699382Seric 	register ENVELOPE *e;
37058929Seric 	bool full;
3717783Seric {
3727783Seric 	register HDR *h;
3737783Seric 	register char *p;
3749382Seric 	int hopcnt = 0;
37557208Seric 	char *msgid;
37658688Seric 	char buf[MAXLINE];
3777783Seric 
37858688Seric 	/*
37958688Seric 	**  Set up macros for possible expansion in headers.
38058688Seric 	*/
38158688Seric 
38258704Seric 	define('f', e->e_sender, e);
38358704Seric 	define('g', e->e_sender, e);
38464148Seric 	if (e->e_origrcpt != NULL && *e->e_origrcpt != '\0')
38564148Seric 		define('u', e->e_origrcpt, e);
38664148Seric 	else
38764148Seric 		define('u', NULL, e);
38858688Seric 
38965052Seric 	/* full name of from person */
39067546Seric 	p = hvalue("full-name", e->e_header);
39165052Seric 	if (p != NULL)
39265052Seric 		define('x', p, e);
39365052Seric 
3949382Seric 	if (tTd(32, 1))
3959382Seric 		printf("----- collected header -----\n");
39657208Seric 	msgid = "<none>";
3979382Seric 	for (h = e->e_header; h != NULL; h = h->h_link)
3987783Seric 	{
39964156Seric 		if (h->h_value == NULL)
40064156Seric 		{
40164156Seric 			if (tTd(32, 1))
40264156Seric 				printf("%s: <NULL>\n", h->h_field);
40364156Seric 			continue;
40464156Seric 		}
40564156Seric 
40658688Seric 		/* do early binding */
40764156Seric 		if (bitset(H_DEFAULT, h->h_flags))
40858688Seric 		{
40968529Seric 			expand(h->h_value, buf, sizeof buf, e);
41058688Seric 			if (buf[0] != '\0')
41158688Seric 			{
41258688Seric 				h->h_value = newstr(buf);
41358688Seric 				h->h_flags &= ~H_DEFAULT;
41458688Seric 			}
41558688Seric 		}
41658688Seric 
4179382Seric 		if (tTd(32, 1))
41865152Seric 		{
41965152Seric 			printf("%s: ", h->h_field);
42065152Seric 			xputs(h->h_value);
42165152Seric 			printf("\n");
42265152Seric 		}
42357359Seric 
42411414Seric 		/* count the number of times it has been processed */
4259382Seric 		if (bitset(H_TRACE, h->h_flags))
4269382Seric 			hopcnt++;
42711414Seric 
42811414Seric 		/* send to this person if we so desire */
42911414Seric 		if (GrabTo && bitset(H_RCPT, h->h_flags) &&
43011414Seric 		    !bitset(H_DEFAULT, h->h_flags) &&
43155012Seric 		    (!bitset(EF_RESENT, e->e_flags) || bitset(H_RESENT, h->h_flags)))
43211414Seric 		{
43363839Seric 			int saveflags = e->e_flags;
43463839Seric 
43564283Seric 			(void) sendtolist(h->h_value, NULLADDR,
43667982Seric 					  &e->e_sendqueue, 0, e);
43763839Seric 
43863839Seric 			/* delete fatal errors generated by this address */
43964148Seric 			if (!GrabTo && !bitset(EF_FATALERRS, saveflags))
44063839Seric 				e->e_flags &= ~EF_FATALERRS;
44111414Seric 		}
44211414Seric 
44357208Seric 		/* save the message-id for logging */
44464156Seric 		if (full && strcasecmp(h->h_field, "message-id") == 0)
44511290Seric 		{
44657208Seric 			msgid = h->h_value;
44759859Seric 			while (isascii(*msgid) && isspace(*msgid))
44859859Seric 				msgid++;
44911290Seric 		}
45057359Seric 
45157359Seric 		/* see if this is a return-receipt header */
45257359Seric 		if (bitset(H_RECEIPTTO, h->h_flags))
45357359Seric 			e->e_receiptto = h->h_value;
4549382Seric 	}
4559382Seric 	if (tTd(32, 1))
4567783Seric 		printf("----------------------------\n");
4577783Seric 
45858145Seric 	/* if we are just verifying (that is, sendmail -t -bv), drop out now */
45958145Seric 	if (OpMode == MD_VERIFY)
46058145Seric 		return;
46158145Seric 
4629382Seric 	/* store hop count */
4639382Seric 	if (hopcnt > e->e_hopcount)
4649382Seric 		e->e_hopcount = hopcnt;
4659382Seric 
4667783Seric 	/* message priority */
46767546Seric 	p = hvalue("precedence", e->e_header);
4689382Seric 	if (p != NULL)
4699382Seric 		e->e_class = priencode(p);
47058929Seric 	if (full)
47167730Seric 	{
47225013Seric 		e->e_msgpriority = e->e_msgsize
47324981Seric 				 - e->e_class * WkClassFact
47424981Seric 				 + e->e_nrcpts * WkRecipFact;
47567730Seric 		if (e->e_class < 0)
47667730Seric 			e->e_timeoutclass = TOC_NONURGENT;
47767730Seric 		else if (e->e_class > 0)
47867730Seric 			e->e_timeoutclass = TOC_URGENT;
47967730Seric 	}
4807783Seric 
48167730Seric 	/* message timeout priority */
48267730Seric 	p = hvalue("priority", e->e_header);
48367730Seric 	if (full && p != NULL)
48467730Seric 	{
48567730Seric 		/* (this should be in the configuration file) */
48667730Seric 		if (strcasecmp(p, "urgent"))
48767730Seric 			e->e_timeoutclass = TOC_URGENT;
48867730Seric 		else if (strcasecmp(p, "normal"))
48967730Seric 			e->e_timeoutclass = TOC_NORMAL;
49067730Seric 		else if (strcasecmp(p, "non-urgent"))
49167730Seric 			e->e_timeoutclass = TOC_NONURGENT;
49267730Seric 	}
49367730Seric 
4947783Seric 	/* date message originated */
49567546Seric 	p = hvalue("posted-date", e->e_header);
4967783Seric 	if (p == NULL)
49767546Seric 		p = hvalue("date", e->e_header);
4987783Seric 	if (p != NULL)
4999382Seric 		define('a', p, e);
50011290Seric 
50111290Seric 	/*
50265983Seric 	**  From person in antiquated ARPANET mode
50365983Seric 	**	required by UK Grey Book e-mail gateways (sigh)
50465983Seric 	*/
50565983Seric 
50665983Seric 	if (OpMode == MD_ARPAFTP)
50765983Seric 	{
50865983Seric 		register struct hdrinfo *hi;
50965983Seric 
51065983Seric 		for (hi = HdrInfo; hi->hi_field != NULL; hi++)
51165983Seric 		{
51265983Seric 			if (bitset(H_FROM, hi->hi_flags) &&
51365983Seric 			    (!bitset(H_RESENT, hi->hi_flags) ||
51465983Seric 			     bitset(EF_RESENT, e->e_flags)) &&
51567546Seric 			    (p = hvalue(hi->hi_field, e->e_header)) != NULL)
51665983Seric 				break;
51765983Seric 		}
51865983Seric 		if (hi->hi_field != NULL)
51965983Seric 		{
52065983Seric 			if (tTd(32, 2))
52165983Seric 				printf("eatheader: setsender(*%s == %s)\n",
52265983Seric 					hi->hi_field, p);
52365983Seric 			setsender(p, e, NULL, TRUE);
52465983Seric 		}
52565983Seric 	}
52665983Seric 
52765983Seric 	/*
52811290Seric 	**  Log collection information.
52911290Seric 	*/
53011290Seric 
53111290Seric # ifdef LOG
53268036Seric 	if (bitset(EF_LOGSENDER, e->e_flags) && LogLevel > 4)
53365089Seric 		logsender(e, msgid);
53465089Seric # endif /* LOG */
53565089Seric 	e->e_flags &= ~EF_LOGSENDER;
53665089Seric }
53765089Seric /*
53865089Seric **  LOGSENDER -- log sender information
53965089Seric **
54065089Seric **	Parameters:
54165089Seric **		e -- the envelope to log
54265089Seric **		msgid -- the message id
54365089Seric **
54465089Seric **	Returns:
54565089Seric **		none
54665089Seric */
54765089Seric 
54865089Seric logsender(e, msgid)
54965089Seric 	register ENVELOPE *e;
55065089Seric 	char *msgid;
55165089Seric {
55266748Seric # ifdef LOG
55365089Seric 	char *name;
55465089Seric 	register char *sbp;
55565089Seric 	register char *p;
55667901Seric 	int l;
55768528Seric 	char hbuf[MAXNAME + 1];
55868528Seric 	char sbuf[MAXLINE + 1];
55968528Seric 	char mbuf[MAXNAME + 1];
56065089Seric 
56167901Seric 	/* don't allow newlines in the message-id */
56267901Seric 	if (msgid != NULL)
56367901Seric 	{
56467901Seric 		l = strlen(msgid);
56567901Seric 		if (l > sizeof mbuf - 1)
56667901Seric 			l = sizeof mbuf - 1;
56767901Seric 		bcopy(msgid, mbuf, l);
56867901Seric 		mbuf[l] = '\0';
56967901Seric 		p = mbuf;
57067901Seric 		while ((p = strchr(p, '\n')) != NULL)
57167901Seric 			*p++ = ' ';
57267901Seric 	}
57367901Seric 
57465089Seric 	if (bitset(EF_RESPONSE, e->e_flags))
57565089Seric 		name = "[RESPONSE]";
57665089Seric 	else if ((name = macvalue('_', e)) != NULL)
57765089Seric 		;
57866003Seric 	else if (RealHostName == NULL)
57966003Seric 		name = "localhost";
58065089Seric 	else if (RealHostName[0] == '[')
58165089Seric 		name = RealHostName;
58265089Seric 	else
58311290Seric 	{
58465089Seric 		name = hbuf;
58565089Seric 		(void) sprintf(hbuf, "%.80s", RealHostName);
58665089Seric 		if (RealHostAddr.sa.sa_family != 0)
58757359Seric 		{
58865089Seric 			p = &hbuf[strlen(hbuf)];
58965089Seric 			(void) sprintf(p, " (%s)",
59065089Seric 				anynet_ntoa(&RealHostAddr));
59157359Seric 		}
59265089Seric 	}
59357359Seric 
59465089Seric 	/* some versions of syslog only take 5 printf args */
59565059Seric #  if (SYSLOG_BUFSIZE) >= 256
59665089Seric 	sbp = sbuf;
59765089Seric 	sprintf(sbp, "from=%.200s, size=%ld, class=%d, pri=%ld, nrcpts=%d",
59867788Seric 	    e->e_from.q_paddr == NULL ? "<NONE>" : e->e_from.q_paddr,
59967788Seric 	    e->e_msgsize, e->e_class, e->e_msgpriority, e->e_nrcpts);
60065089Seric 	sbp += strlen(sbp);
60165089Seric 	if (msgid != NULL)
60265089Seric 	{
60367901Seric 		sprintf(sbp, ", msgid=%.100s", mbuf);
60460575Seric 		sbp += strlen(sbp);
60565089Seric 	}
60665089Seric 	if (e->e_bodytype != NULL)
60765089Seric 	{
60865089Seric 		(void) sprintf(sbp, ", bodytype=%.20s", e->e_bodytype);
60965089Seric 		sbp += strlen(sbp);
61065089Seric 	}
61165089Seric 	p = macvalue('r', e);
61265089Seric 	if (p != NULL)
61365089Seric 		(void) sprintf(sbp, ", proto=%.20s", p);
61465089Seric 	syslog(LOG_INFO, "%s: %s, relay=%s",
61565089Seric 	    e->e_id, sbuf, name);
61665059Seric 
61765059Seric #  else			/* short syslog buffer */
61865059Seric 
61965089Seric 	syslog(LOG_INFO, "%s: from=%s",
62067788Seric 		e->e_id, e->e_from.q_paddr == NULL ? "<NONE>" :
62167788Seric 				shortenstring(e->e_from.q_paddr, 83));
62265089Seric 	syslog(LOG_INFO, "%s: size=%ld, class=%ld, pri=%ld, nrcpts=%d",
62365089Seric 		e->e_id, e->e_msgsize, e->e_class,
62465089Seric 		e->e_msgpriority, e->e_nrcpts);
62565089Seric 	if (msgid != NULL)
62667901Seric 		syslog(LOG_INFO, "%s: msgid=%s", e->e_id, mbuf);
62765650Seric 	sbp = sbuf;
62865650Seric 	sprintf(sbp, "%s:", e->e_id);
62965650Seric 	sbp += strlen(sbp);
63065089Seric 	if (e->e_bodytype != NULL)
63165650Seric 	{
63265650Seric 		sprintf(sbp, " bodytype=%s,", e->e_bodytype);
63365650Seric 		sbp += strlen(sbp);
63465650Seric 	}
63565089Seric 	p = macvalue('r', e);
63665089Seric 	if (p != NULL)
63765650Seric 	{
63865731Seric 		sprintf(sbp, " proto=%s,", p);
63965650Seric 		sbp += strlen(sbp);
64065650Seric 	}
64165650Seric 	syslog(LOG_INFO, "%s relay=%s", sbuf, name);
64265059Seric #  endif
64366748Seric # endif
6447783Seric }
6457783Seric /*
6467783Seric **  PRIENCODE -- encode external priority names into internal values.
6477783Seric **
6487783Seric **	Parameters:
6497783Seric **		p -- priority in ascii.
6507783Seric **
6517783Seric **	Returns:
6527783Seric **		priority as a numeric level.
6537783Seric **
6547783Seric **	Side Effects:
6557783Seric **		none.
6567783Seric */
6577783Seric 
6587783Seric priencode(p)
6597783Seric 	char *p;
6607783Seric {
6618253Seric 	register int i;
6627783Seric 
6638253Seric 	for (i = 0; i < NumPriorities; i++)
6647783Seric 	{
66533725Sbostic 		if (!strcasecmp(p, Priorities[i].pri_name))
6668253Seric 			return (Priorities[i].pri_val);
6677783Seric 	}
6688253Seric 
6698253Seric 	/* unknown priority */
6708253Seric 	return (0);
6717783Seric }
6727783Seric /*
6737890Seric **  CRACKADDR -- parse an address and turn it into a macro
6747783Seric **
6757783Seric **	This doesn't actually parse the address -- it just extracts
6767783Seric **	it and replaces it with "$g".  The parse is totally ad hoc
6777783Seric **	and isn't even guaranteed to leave something syntactically
6787783Seric **	identical to what it started with.  However, it does leave
6797783Seric **	something semantically identical.
6807783Seric **
68151379Seric **	This algorithm has been cleaned up to handle a wider range
68251379Seric **	of cases -- notably quoted and backslash escaped strings.
68351379Seric **	This modification makes it substantially better at preserving
68451379Seric **	the original syntax.
6857783Seric **
6867783Seric **	Parameters:
6877890Seric **		addr -- the address to be cracked.
6887783Seric **
6897783Seric **	Returns:
6907783Seric **		a pointer to the new version.
6917783Seric **
6927783Seric **	Side Effects:
6937890Seric **		none.
6947783Seric **
6957783Seric **	Warning:
6967783Seric **		The return value is saved in local storage and should
6977783Seric **		be copied if it is to be reused.
6987783Seric */
6997783Seric 
7007783Seric char *
7017890Seric crackaddr(addr)
7027890Seric 	register char *addr;
7037783Seric {
7047783Seric 	register char *p;
70551379Seric 	register char c;
70651379Seric 	int cmtlev;
70756764Seric 	int realcmtlev;
70856764Seric 	int anglelev, realanglelev;
70951379Seric 	int copylev;
71051379Seric 	bool qmode;
71156764Seric 	bool realqmode;
71256764Seric 	bool skipping;
71351379Seric 	bool putgmac = FALSE;
71456735Seric 	bool quoteit = FALSE;
71564148Seric 	bool gotangle = FALSE;
71668756Seric 	bool gotcolon = FALSE;
71751379Seric 	register char *bp;
71856764Seric 	char *buflim;
71968756Seric 	char *bufhead;
72068756Seric 	char *addrhead;
72168528Seric 	static char buf[MAXNAME + 1];
7227783Seric 
7237783Seric 	if (tTd(33, 1))
7247890Seric 		printf("crackaddr(%s)\n", addr);
7257783Seric 
7268082Seric 	/* strip leading spaces */
72758050Seric 	while (*addr != '\0' && isascii(*addr) && isspace(*addr))
7288082Seric 		addr++;
7298082Seric 
7307783Seric 	/*
73151379Seric 	**  Start by assuming we have no angle brackets.  This will be
73251379Seric 	**  adjusted later if we find them.
7337783Seric 	*/
7347783Seric 
73568756Seric 	bp = bufhead = buf;
73656764Seric 	buflim = &buf[sizeof buf - 5];
73768756Seric 	p = addrhead = addr;
73856764Seric 	copylev = anglelev = realanglelev = cmtlev = realcmtlev = 0;
73956764Seric 	qmode = realqmode = FALSE;
74051379Seric 
74151379Seric 	while ((c = *p++) != '\0')
7427783Seric 	{
74356764Seric 		/*
74456764Seric 		**  If the buffer is overful, go into a special "skipping"
74556764Seric 		**  mode that tries to keep legal syntax but doesn't actually
74656764Seric 		**  output things.
74756764Seric 		*/
7487783Seric 
74956764Seric 		skipping = bp >= buflim;
75056735Seric 
75156764Seric 		if (copylev > 0 && !skipping)
75256764Seric 			*bp++ = c;
75356735Seric 
75451379Seric 		/* check for backslash escapes */
75551379Seric 		if (c == '\\')
7567783Seric 		{
75758890Seric 			/* arrange to quote the address */
75858890Seric 			if (cmtlev <= 0 && !qmode)
75958890Seric 				quoteit = TRUE;
76058890Seric 
76151379Seric 			if ((c = *p++) == '\0')
7627783Seric 			{
76351379Seric 				/* too far */
76451379Seric 				p--;
76551379Seric 				goto putg;
7667783Seric 			}
76756764Seric 			if (copylev > 0 && !skipping)
76851379Seric 				*bp++ = c;
76951379Seric 			goto putg;
7707783Seric 		}
7717783Seric 
77251379Seric 		/* check for quoted strings */
77364967Seric 		if (c == '"' && cmtlev <= 0)
7747783Seric 		{
77551379Seric 			qmode = !qmode;
77656764Seric 			if (copylev > 0 && !skipping)
77756764Seric 				realqmode = !realqmode;
77851379Seric 			continue;
7797783Seric 		}
78051379Seric 		if (qmode)
78151379Seric 			goto putg;
7827783Seric 
78351379Seric 		/* check for comments */
78451379Seric 		if (c == '(')
7857783Seric 		{
78651379Seric 			cmtlev++;
78756764Seric 
78856764Seric 			/* allow space for closing paren */
78956764Seric 			if (!skipping)
79056764Seric 			{
79156764Seric 				buflim--;
79256764Seric 				realcmtlev++;
79356764Seric 				if (copylev++ <= 0)
79456764Seric 				{
79556764Seric 					*bp++ = ' ';
79656764Seric 					*bp++ = c;
79756764Seric 				}
79856764Seric 			}
79951379Seric 		}
80051379Seric 		if (cmtlev > 0)
80151379Seric 		{
80251379Seric 			if (c == ')')
8037783Seric 			{
80451379Seric 				cmtlev--;
80551379Seric 				copylev--;
80656764Seric 				if (!skipping)
80756764Seric 				{
80856764Seric 					realcmtlev--;
80956764Seric 					buflim++;
81056764Seric 				}
8117783Seric 			}
8127783Seric 			continue;
8137783Seric 		}
81456764Seric 		else if (c == ')')
81556764Seric 		{
81656764Seric 			/* syntax error: unmatched ) */
81765544Seric 			if (copylev > 0 && !skipping)
81856764Seric 				bp--;
81956764Seric 		}
8207783Seric 
82168756Seric 		/* check for group: list; syntax */
82268760Seric 		if (c == ':' && anglelev <= 0 && !gotcolon && !ColonOkInAddr)
82368756Seric 		{
82468756Seric 			register char *q;
82568756Seric 
82668760Seric 			if (*p == ':')
82768760Seric 			{
82868760Seric 				/* special case -- :: syntax */
82968760Seric 				if (cmtlev <= 0 && !qmode)
83068760Seric 					quoteit = TRUE;
83168760Seric 				if (copylev > 0 && !skipping)
83268760Seric 				{
83368760Seric 					*bp++ = c;
83468760Seric 					*bp++ = c;
83568760Seric 				}
83668760Seric 				p++;
83768760Seric 				goto putg;
83868760Seric 			}
83968760Seric 
84068756Seric 			gotcolon = TRUE;
84168756Seric 
84268760Seric 			bp = bufhead;
84368760Seric 			if (quoteit)
84468760Seric 			{
84568760Seric 				*bp++ = '"';
84668760Seric 
84768760Seric 				/* back up over the ':' and any spaces */
84868760Seric 				--p;
84968760Seric 				while (isascii(*--p) && isspace(*p))
85068760Seric 					continue;
85168756Seric 				p++;
85268760Seric 			}
85368756Seric 			for (q = addrhead; q < p; )
85468756Seric 			{
85568756Seric 				c = *q++;
85668756Seric 				if (bp < buflim)
85768756Seric 				{
85868760Seric 					if (quoteit && c == '"')
85968760Seric 						*bp++ = '\\';
86068756Seric 					*bp++ = c;
86168756Seric 				}
86268756Seric 			}
86368760Seric 			if (quoteit)
86468760Seric 			{
86568760Seric 				if (bp == &bufhead[1])
86668760Seric 					bp--;
86768760Seric 				else
86868760Seric 					*bp++ = '"';
86968760Seric 				while ((c = *p++) != ':')
87068760Seric 				{
87168760Seric 					if (bp < buflim)
87268760Seric 						*bp++ = c;
87368760Seric 				}
87468760Seric 				*bp++ = c;
87568760Seric 			}
87668760Seric 
87768760Seric 			/* any trailing white space is part of group: */
87868760Seric 			while (isascii(*p) && isspace(*p) && bp < buflim)
87968760Seric 				*bp++ = *p++;
88068756Seric 			copylev = 0;
88168760Seric 			putgmac = quoteit = FALSE;
88268756Seric 			bufhead = bp;
88368756Seric 			addrhead = p;
88468756Seric 			continue;
88568756Seric 		}
88668756Seric 
88768756Seric 		if (c == ';' && copylev <= 0 && !ColonOkInAddr)
88868756Seric 		{
88968756Seric 			register char *q = p;
89068756Seric 
89168756Seric 			if (bp < buflim)
89268756Seric 				*bp++ = c;
89368756Seric 		}
89468756Seric 
89556764Seric 		/* check for characters that may have to be quoted */
89664148Seric 		if (strchr(".'@,;:\\()[]", c) != NULL)
89756764Seric 		{
89856764Seric 			/*
89956764Seric 			**  If these occur as the phrase part of a <>
90056764Seric 			**  construct, but are not inside of () or already
90156764Seric 			**  quoted, they will have to be quoted.  Note that
90256764Seric 			**  now (but don't actually do the quoting).
90356764Seric 			*/
90456764Seric 
90556764Seric 			if (cmtlev <= 0 && !qmode)
90656764Seric 				quoteit = TRUE;
90756764Seric 		}
90856764Seric 
90951379Seric 		/* check for angle brackets */
91051379Seric 		if (c == '<')
91151379Seric 		{
91256735Seric 			register char *q;
91356735Seric 
91464148Seric 			/* assume first of two angles is bogus */
91564148Seric 			if (gotangle)
91664148Seric 				quoteit = TRUE;
91764148Seric 			gotangle = TRUE;
91864148Seric 
91951379Seric 			/* oops -- have to change our mind */
92064752Seric 			anglelev = 1;
92156764Seric 			if (!skipping)
92264752Seric 				realanglelev = 1;
92356764Seric 
92468756Seric 			bp = bufhead;
92556735Seric 			if (quoteit)
92656735Seric 			{
92756735Seric 				*bp++ = '"';
92856735Seric 
92956735Seric 				/* back up over the '<' and any spaces */
93056735Seric 				--p;
93158050Seric 				while (isascii(*--p) && isspace(*p))
93256735Seric 					continue;
93356735Seric 				p++;
93456735Seric 			}
93568756Seric 			for (q = addrhead; q < p; )
93656735Seric 			{
93756735Seric 				c = *q++;
93856764Seric 				if (bp < buflim)
93956764Seric 				{
94056764Seric 					if (quoteit && c == '"')
94156764Seric 						*bp++ = '\\';
94256764Seric 					*bp++ = c;
94356764Seric 				}
94456735Seric 			}
94556735Seric 			if (quoteit)
94656735Seric 			{
94764148Seric 				if (bp == &buf[1])
94864148Seric 					bp--;
94964148Seric 				else
95064148Seric 					*bp++ = '"';
95156764Seric 				while ((c = *p++) != '<')
95256764Seric 				{
95356764Seric 					if (bp < buflim)
95456764Seric 						*bp++ = c;
95556764Seric 				}
95656764Seric 				*bp++ = c;
95756735Seric 			}
95851379Seric 			copylev = 0;
95956735Seric 			putgmac = quoteit = FALSE;
96051379Seric 			continue;
96151379Seric 		}
9627783Seric 
96351379Seric 		if (c == '>')
9647783Seric 		{
96556764Seric 			if (anglelev > 0)
96656764Seric 			{
96756764Seric 				anglelev--;
96856764Seric 				if (!skipping)
96956764Seric 				{
97056764Seric 					realanglelev--;
97156764Seric 					buflim++;
97256764Seric 				}
97356764Seric 			}
97456764Seric 			else if (!skipping)
97556764Seric 			{
97656764Seric 				/* syntax error: unmatched > */
97756764Seric 				if (copylev > 0)
97856764Seric 					bp--;
97964752Seric 				quoteit = TRUE;
98056764Seric 				continue;
98156764Seric 			}
98251379Seric 			if (copylev++ <= 0)
98351379Seric 				*bp++ = c;
98451379Seric 			continue;
9857783Seric 		}
98651379Seric 
98751379Seric 		/* must be a real address character */
98851379Seric 	putg:
98951379Seric 		if (copylev <= 0 && !putgmac)
99051379Seric 		{
99158050Seric 			*bp++ = MACROEXPAND;
99251379Seric 			*bp++ = 'g';
99351379Seric 			putgmac = TRUE;
99451379Seric 		}
9957783Seric 	}
9967783Seric 
99756764Seric 	/* repair any syntactic damage */
99856764Seric 	if (realqmode)
99956764Seric 		*bp++ = '"';
100056764Seric 	while (realcmtlev-- > 0)
100156764Seric 		*bp++ = ')';
100256764Seric 	while (realanglelev-- > 0)
100356764Seric 		*bp++ = '>';
100451379Seric 	*bp++ = '\0';
10057783Seric 
10067783Seric 	if (tTd(33, 1))
10077944Seric 		printf("crackaddr=>`%s'\n", buf);
10087783Seric 
10097783Seric 	return (buf);
10107783Seric }
10119382Seric /*
10129382Seric **  PUTHEADER -- put the header part of a message from the in-core copy
10139382Seric **
10149382Seric **	Parameters:
101565870Seric **		mci -- the connection information.
101667546Seric **		h -- the header to put.
10179382Seric **		e -- envelope to use.
10189382Seric **
10199382Seric **	Returns:
10209382Seric **		none.
10219382Seric **
10229382Seric **	Side Effects:
10239382Seric **		none.
10249382Seric */
10259382Seric 
102657589Seric /*
102757589Seric  * Macro for fast max (not available in e.g. DG/UX, 386/ix).
102857589Seric  */
102957589Seric #ifndef MAX
103057589Seric # define MAX(a,b) (((a)>(b))?(a):(b))
103157589Seric #endif
103257589Seric 
103368228Seric putheader(mci, h, e)
103465870Seric 	register MCI *mci;
103567546Seric 	register HDR *h;
10369382Seric 	register ENVELOPE *e;
10379382Seric {
103857135Seric 	char buf[MAX(MAXLINE,BUFSIZ)];
103957135Seric 	char obuf[MAXLINE];
10409382Seric 
104159882Seric 	if (tTd(34, 1))
104265870Seric 		printf("--- putheader, mailer = %s ---\n",
104365870Seric 			mci->mci_mailer->m_name);
104459882Seric 
104567546Seric 	mci->mci_flags |= MCIF_INHEADER;
104667546Seric 	for (; h != NULL; h = h->h_link)
10479382Seric 	{
10489382Seric 		register char *p;
104910689Seric 		extern bool bitintersect();
10509382Seric 
105159882Seric 		if (tTd(34, 11))
105259882Seric 		{
105359882Seric 			printf("  %s: ", h->h_field);
105459882Seric 			xputs(h->h_value);
105559882Seric 		}
105659882Seric 
10579382Seric 		if (bitset(H_CHECK|H_ACHECK, h->h_flags) &&
105865870Seric 		    !bitintersect(h->h_mflags, mci->mci_mailer->m_flags))
105959882Seric 		{
106059882Seric 			if (tTd(34, 11))
106159882Seric 				printf(" (skipped)\n");
10629382Seric 			continue;
106359882Seric 		}
10649382Seric 
106511414Seric 		/* handle Resent-... headers specially */
106611414Seric 		if (bitset(H_RESENT, h->h_flags) && !bitset(EF_RESENT, e->e_flags))
106759882Seric 		{
106859882Seric 			if (tTd(34, 11))
106959882Seric 				printf(" (skipped (resent))\n");
107011414Seric 			continue;
107159882Seric 		}
107211414Seric 
107366784Seric 		/* suppress return receipts if requested */
107466784Seric 		if (bitset(H_RECEIPTTO, h->h_flags) &&
107566784Seric 		    bitset(EF_NORECEIPT, e->e_flags))
107666784Seric 		{
107766784Seric 			if (tTd(34, 11))
107866784Seric 				printf(" (skipped (receipt))\n");
107966784Seric 			continue;
108066784Seric 		}
108166784Seric 
108267694Seric 		/* suppress Content-Transfer-Encoding: if we are MIMEing */
108367694Seric 		if (bitset(H_CTE, h->h_flags) &&
108468228Seric 		    bitset(MCIF_CVT8TO7, mci->mci_flags))
108567694Seric 		{
108667694Seric 			if (tTd(34, 11))
108767694Seric 				printf(" (skipped (content-transfer-encoding))\n");
108867694Seric 			continue;
108967694Seric 		}
109067694Seric 
109165152Seric 		/* macro expand value if generated internally */
10929382Seric 		p = h->h_value;
10939382Seric 		if (bitset(H_DEFAULT, h->h_flags))
10949382Seric 		{
109568529Seric 			expand(p, buf, sizeof buf, e);
10969382Seric 			p = buf;
10979382Seric 			if (p == NULL || *p == '\0')
109865152Seric 			{
109965152Seric 				if (tTd(34, 11))
110065152Seric 					printf(" (skipped -- null value)\n");
11019382Seric 				continue;
110265152Seric 			}
11039382Seric 		}
11049382Seric 
110565152Seric 		if (tTd(34, 11))
110665152Seric 			printf("\n");
110765152Seric 
110868449Seric 		if (bitset(H_STRIPVAL, h->h_flags))
11099382Seric 		{
111068449Seric 			/* empty field */
111168449Seric 			(void) sprintf(obuf, "%s:", h->h_field);
111268449Seric 			putline(obuf, mci);
111368449Seric 		}
111468449Seric 		else if (bitset(H_FROM|H_RCPT, h->h_flags))
111568449Seric 		{
11169382Seric 			/* address field */
11179382Seric 			bool oldstyle = bitset(EF_OLDSTYLE, e->e_flags);
11189382Seric 
11199382Seric 			if (bitset(H_FROM, h->h_flags))
11209382Seric 				oldstyle = FALSE;
112165870Seric 			commaize(h, p, oldstyle, mci, e);
11229382Seric 		}
11239382Seric 		else
11249382Seric 		{
11259382Seric 			/* vanilla header line */
112612159Seric 			register char *nlp;
112712159Seric 
112859579Seric 			(void) sprintf(obuf, "%s: ", h->h_field);
112956795Seric 			while ((nlp = strchr(p, '\n')) != NULL)
113012159Seric 			{
113112159Seric 				*nlp = '\0';
113212159Seric 				(void) strcat(obuf, p);
113312159Seric 				*nlp = '\n';
113465870Seric 				putline(obuf, mci);
113512159Seric 				p = ++nlp;
113612161Seric 				obuf[0] = '\0';
113712159Seric 			}
113812159Seric 			(void) strcat(obuf, p);
113965870Seric 			putline(obuf, mci);
11409382Seric 		}
11419382Seric 	}
114267887Seric 
114367887Seric 	/*
114467887Seric 	**  If we are converting this to a MIME message, add the
114567889Seric 	**  MIME headers.
114667887Seric 	*/
114767887Seric 
114867887Seric 	if (bitset(MM_MIME8BIT, MimeMode) &&
114967887Seric 	    bitset(EF_HAS8BIT, e->e_flags) &&
115067887Seric 	    !bitnset(M_8BITS, mci->mci_mailer->m_flags) &&
115167889Seric 	    !bitset(MCIF_CVT8TO7, mci->mci_flags))
115267887Seric 	{
115367889Seric 		if (hvalue("MIME-Version", e->e_header) == NULL)
115467889Seric 			putline("MIME-Version: 1.0", mci);
115567889Seric 		if (hvalue("Content-Type", e->e_header) == NULL)
115667889Seric 		{
115767889Seric 			sprintf(obuf, "Content-Type: text/plain; charset=%s",
115867896Seric 				defcharset(e));
115967889Seric 			putline(obuf, mci);
116067889Seric 		}
116167889Seric 		if (hvalue("Content-Transfer-Encoding", e->e_header) == NULL)
116267889Seric 			putline("Content-Transfer-Encoding: 8bit", mci);
116367887Seric 	}
11649382Seric }
11659382Seric /*
11669382Seric **  COMMAIZE -- output a header field, making a comma-translated list.
11679382Seric **
11689382Seric **	Parameters:
11699382Seric **		h -- the header field to output.
11709382Seric **		p -- the value to put in it.
11719382Seric **		oldstyle -- TRUE if this is an old style header.
117265870Seric **		mci -- the connection information.
117355012Seric **		e -- the envelope containing the message.
11749382Seric **
11759382Seric **	Returns:
11769382Seric **		none.
11779382Seric **
11789382Seric **	Side Effects:
11799382Seric **		outputs "p" to file "fp".
11809382Seric */
11819382Seric 
118265870Seric void
118365870Seric commaize(h, p, oldstyle, mci, e)
11849382Seric 	register HDR *h;
11859382Seric 	register char *p;
11869382Seric 	bool oldstyle;
118765870Seric 	register MCI *mci;
118855012Seric 	register ENVELOPE *e;
11899382Seric {
11909382Seric 	register char *obp;
11919382Seric 	int opos;
119266004Seric 	int omax;
11939382Seric 	bool firstone = TRUE;
119411157Seric 	char obuf[MAXLINE + 3];
11959382Seric 
11969382Seric 	/*
11979382Seric 	**  Output the address list translated by the
11989382Seric 	**  mailer and with commas.
11999382Seric 	*/
12009382Seric 
12019382Seric 	if (tTd(14, 2))
12029382Seric 		printf("commaize(%s: %s)\n", h->h_field, p);
12039382Seric 
12049382Seric 	obp = obuf;
120559579Seric 	(void) sprintf(obp, "%s: ", h->h_field);
12069382Seric 	opos = strlen(h->h_field) + 2;
12079382Seric 	obp += opos;
120866254Seric 	omax = mci->mci_mailer->m_linelimit - 2;
120966254Seric 	if (omax < 0 || omax > 78)
121066254Seric 		omax = 78;
12119382Seric 
12129382Seric 	/*
12139382Seric 	**  Run through the list of values.
12149382Seric 	*/
12159382Seric 
12169382Seric 	while (*p != '\0')
12179382Seric 	{
12189382Seric 		register char *name;
121954983Seric 		register int c;
12209382Seric 		char savechar;
122159163Seric 		int flags;
122259163Seric 		auto int stat;
12239382Seric 
12249382Seric 		/*
12259382Seric 		**  Find the end of the name.  New style names
12269382Seric 		**  end with a comma, old style names end with
12279382Seric 		**  a space character.  However, spaces do not
12289382Seric 		**  necessarily delimit an old-style name -- at
12299382Seric 		**  signs mean keep going.
12309382Seric 		*/
12319382Seric 
12329382Seric 		/* find end of name */
123358050Seric 		while ((isascii(*p) && isspace(*p)) || *p == ',')
12349382Seric 			p++;
12359382Seric 		name = p;
12369382Seric 		for (;;)
12379382Seric 		{
123858333Seric 			auto char *oldp;
123916909Seric 			char pvpbuf[PSBUFSIZE];
12409382Seric 
124165066Seric 			(void) prescan(p, oldstyle ? ' ' : ',', pvpbuf,
124268711Seric 				       sizeof pvpbuf, &oldp, NULL);
124358333Seric 			p = oldp;
12449382Seric 
12459382Seric 			/* look to see if we have an at sign */
124658050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
12479382Seric 				p++;
12489382Seric 
124958170Seric 			if (*p != '@')
12509382Seric 			{
12519382Seric 				p = oldp;
12529382Seric 				break;
12539382Seric 			}
12549382Seric 			p += *p == '@' ? 1 : 2;
125558050Seric 			while (*p != '\0' && isascii(*p) && isspace(*p))
12569382Seric 				p++;
12579382Seric 		}
12589382Seric 		/* at the end of one complete name */
12599382Seric 
12609382Seric 		/* strip off trailing white space */
126158050Seric 		while (p >= name &&
126258050Seric 		       ((isascii(*p) && isspace(*p)) || *p == ',' || *p == '\0'))
12639382Seric 			p--;
12649382Seric 		if (++p == name)
12659382Seric 			continue;
12669382Seric 		savechar = *p;
12679382Seric 		*p = '\0';
12689382Seric 
12699382Seric 		/* translate the name to be relative */
127059163Seric 		flags = RF_HEADERADDR|RF_ADDDOMAIN;
127159163Seric 		if (bitset(H_FROM, h->h_flags))
127259163Seric 			flags |= RF_SENDERADDR;
127368522Seric #ifdef USERDB
127468522Seric 		else if (e->e_from.q_mailer != NULL &&
127568522Seric 			 bitnset(M_UDBRECIPIENT, e->e_from.q_mailer->m_flags))
127668522Seric 		{
127768522Seric 			extern char *udbsender();
127868522Seric 
127968522Seric 			name = udbsender(name);
128068522Seric 		}
128168522Seric #endif
128259163Seric 		stat = EX_OK;
128365870Seric 		name = remotename(name, mci->mci_mailer, flags, &stat, e);
12849382Seric 		if (*name == '\0')
12859382Seric 		{
12869382Seric 			*p = savechar;
12879382Seric 			continue;
12889382Seric 		}
12899382Seric 
12909382Seric 		/* output the name with nice formatting */
129154983Seric 		opos += strlen(name);
12929382Seric 		if (!firstone)
12939382Seric 			opos += 2;
129466004Seric 		if (opos > omax && !firstone)
12959382Seric 		{
129610178Seric 			(void) strcpy(obp, ",\n");
129765870Seric 			putline(obuf, mci);
12989382Seric 			obp = obuf;
129966255Seric 			(void) strcpy(obp, "        ");
130010161Seric 			opos = strlen(obp);
130110161Seric 			obp += opos;
130254983Seric 			opos += strlen(name);
13039382Seric 		}
13049382Seric 		else if (!firstone)
13059382Seric 		{
130666255Seric 			(void) strcpy(obp, ", ");
13079382Seric 			obp += 2;
13089382Seric 		}
13099382Seric 
131054983Seric 		while ((c = *name++) != '\0' && obp < &obuf[MAXLINE])
131154983Seric 			*obp++ = c;
13129382Seric 		firstone = FALSE;
13139382Seric 		*p = savechar;
13149382Seric 	}
13159382Seric 	(void) strcpy(obp, "\n");
131665870Seric 	putline(obuf, mci);
13179382Seric }
13189382Seric /*
131958170Seric **  COPYHEADER -- copy header list
13209382Seric **
132158170Seric **	This routine is the equivalent of newstr for header lists
132258170Seric **
13239382Seric **	Parameters:
132458170Seric **		header -- list of header structures to copy.
13259382Seric **
13269382Seric **	Returns:
132758170Seric **		a copy of 'header'.
13289382Seric **
13299382Seric **	Side Effects:
13309382Seric **		none.
13319382Seric */
13329382Seric 
133358170Seric HDR *
133458170Seric copyheader(header)
133558170Seric 	register HDR *header;
13369382Seric {
133758170Seric 	register HDR *newhdr;
133858170Seric 	HDR *ret;
133958170Seric 	register HDR **tail = &ret;
13409382Seric 
134158170Seric 	while (header != NULL)
134258170Seric 	{
134358170Seric 		newhdr = (HDR *) xalloc(sizeof(HDR));
134458170Seric 		STRUCTCOPY(*header, *newhdr);
134558170Seric 		*tail = newhdr;
134658170Seric 		tail = &newhdr->h_link;
134758170Seric 		header = header->h_link;
134858170Seric 	}
134958170Seric 	*tail = NULL;
135058170Seric 
135158170Seric 	return ret;
13529382Seric }
1353