11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*7367Seric SCCSID(@(#)collect.c	3.42		07/05/82);
51392Seric 
61392Seric /*
72969Seric **  COLLECT -- read & parse message header & make temp file.
81392Seric **
91392Seric **	Creates a temporary file name and copies the standard
101392Seric **	input to that file.  While it is doing it, it looks for
111392Seric **	"From:" and "Sender:" fields to use as the from-person
121392Seric **	(but only if the -a flag is specified).  It prefers to
131392Seric **	to use the "Sender:" field.
141392Seric **
151392Seric **	MIT seems to like to produce "Sent-By:" fields instead
161392Seric **	of "Sender:" fields.  We used to catch this, but it turns
171392Seric **	out that the "Sent-By:" field doesn't always correspond
181392Seric **	to someone real ("___057", for instance), as required by
191392Seric **	the protocol.  So we limp by.....
201392Seric **
211392Seric **	Parameters:
224710Seric **		sayok -- if set, give an ARPANET style message
234710Seric **			to say we are ready to collect input.
241392Seric **
251392Seric **	Returns:
264162Seric **		none.
271392Seric **
281392Seric **	Side Effects:
291392Seric **		Temp file is created and filled.
304162Seric **		The from person may be set.
311392Seric */
321392Seric 
334710Seric collect(sayok)
344710Seric 	bool sayok;
351392Seric {
361392Seric 	register FILE *tf;
371392Seric 	char buf[MAXFIELD+1];
381392Seric 	register char *p;
392900Seric 	char *xfrom;
402900Seric 	extern char *hvalue();
414083Seric 	extern char *mktemp();
424622Seric 	static char tempfname[40];
435192Seric 	extern char *macvalue();
441392Seric 
451392Seric 	/*
461392Seric 	**  Create the temp file name and create the file.
471392Seric 	*/
481392Seric 
497004Seric 	(void) strcpy(tempfname, QueueDir);
507004Seric 	(void) strcat(tempfname, "/dfXXXXXX");
514622Seric 	(void) mktemp(tempfname);
526888Seric 	if ((tf = dfopen(tempfname, "w")) == NULL)
531392Seric 	{
544622Seric 		syserr("Cannot create %s", tempfname);
555366Seric 		NoReturn = TRUE;
565366Seric 		finis();
571392Seric 	}
587004Seric 	(void) chmod(tempfname, 0600);
596986Seric 	CurEnv->e_df = tempfname;
601392Seric 
614316Seric 	/*
625185Seric 	**  Create the Mail-From line if we want to.
635185Seric 	*/
645185Seric 
655366Seric 	if (Smtp && macvalue('s') != NULL)
665185Seric 	{
675185Seric 		char xbuf[50];
685185Seric 
695192Seric 		(void) sprintf(xbuf, "Mail-From: %s$s received by $i at $b",
705185Seric 			macvalue('r') == NULL ? "" : "$r host ");
716975Seric 		expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv);
725192Seric 		(void) chompheader(buf, FALSE);
735185Seric 	}
745185Seric 
755185Seric 	/*
764322Seric 	**  Tell ARPANET to go ahead.
774322Seric 	*/
784322Seric 
794710Seric 	if (sayok)
804710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
814322Seric 
824322Seric 	/*
834316Seric 	**  Try to read a UNIX-style From line
844316Seric 	*/
854316Seric 
865975Seric 	if (fgets(buf, sizeof buf, InChannel) == NULL)
874162Seric 		return;
884557Seric 	fixcrlf(buf, FALSE);
894321Seric # ifndef NOTUNIX
904322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
912900Seric 	{
922900Seric 		eatfrom(buf);
935975Seric 		(void) fgets(buf, sizeof buf, InChannel);
944557Seric 		fixcrlf(buf, FALSE);
952900Seric 	}
964321Seric # endif NOTUNIX
972900Seric 
981392Seric 	/*
995975Seric 	**  Copy InChannel to temp file & do message editing.
1001392Seric 	**	To keep certain mailers from getting confused,
1011392Seric 	**	and to keep the output clean, lines that look
1021392Seric 	**	like UNIX "From" lines are deleted in the header,
1031392Seric 	**	and prepended with ">" in the body.
1041392Seric 	*/
1051392Seric 
1065975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1071392Seric 	{
1084316Seric 		register char c;
1094316Seric 		extern bool isheader();
1104316Seric 
1114557Seric 		fixcrlf(buf, FALSE);
1124557Seric 
1132900Seric 		/* see if the header is over */
1142900Seric 		if (!isheader(buf))
1152900Seric 			break;
1162900Seric 
1172900Seric 		/* get the rest of this field */
1185975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1191392Seric 		{
1202900Seric 			p = &buf[strlen(buf)];
1212900Seric 			*p++ = c;
1225975Seric 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
1232900Seric 				break;
1244557Seric 			fixcrlf(p, FALSE);
1251392Seric 		}
1265975Seric 		if (!feof(InChannel))
1275975Seric 			(void) ungetc(c, InChannel);
1281392Seric 
1296901Seric 		CurEnv->e_msgsize += strlen(buf);
1301392Seric 
1312900Seric 		/*
1322900Seric 		**  Snarf header away.
1332900Seric 		*/
1342900Seric 
1353391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1363058Seric 			break;
1372900Seric 	}
1381392Seric 
1392900Seric # ifdef DEBUG
1402900Seric 	if (Debug)
1412900Seric 		printf("EOH\n");
1422900Seric # endif DEBUG
1432900Seric 
1442900Seric 	/* throw away a blank line */
1452900Seric 	if (buf[0] == '\n')
1464557Seric 	{
1475975Seric 		(void) fgets(buf, sizeof buf, InChannel);
1484557Seric 		fixcrlf(buf, FALSE);
1494557Seric 	}
1502900Seric 
1512900Seric 	/*
1522900Seric 	**  Collect the body of the message.
1532900Seric 	*/
1542900Seric 
1555975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1562900Seric 	{
1574156Seric 		register int i;
1584551Seric 		register char *bp = buf;
1594156Seric 
1604557Seric 		fixcrlf(buf, FALSE);
1614557Seric 
1622900Seric 		/* check for end-of-message */
1632900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1642900Seric 			break;
1652900Seric 
1664551Seric 		/* check for transparent dot */
1674551Seric 		if (Smtp && *bp == '.')
1684551Seric 			bp++;
1694551Seric 
1704321Seric # ifndef NOTUNIX
1712900Seric 		/* Hide UNIX-like From lines */
1724551Seric 		if (strncmp(bp, "From ", 5) == 0)
1731392Seric 		{
1742900Seric 			fputs(">", tf);
1756901Seric 			CurEnv->e_msgsize++;
1761392Seric 		}
1774321Seric # endif NOTUNIX
1784156Seric 
1794156Seric 		/*
1804156Seric 		**  Figure message length, output the line to the temp
1814156Seric 		**  file, and insert a newline if missing.
1824156Seric 		*/
1834156Seric 
1844551Seric 		i = strlen(bp);
1856901Seric 		CurEnv->e_msgsize += i;
1864551Seric 		fputs(bp, tf);
1874551Seric 		if (bp[i - 1] != '\n')
1884156Seric 			fputs("\n", tf);
1891392Seric 		if (ferror(tf))
1901392Seric 		{
1911439Seric 			if (errno == ENOSPC)
1921439Seric 			{
1936986Seric 				(void) freopen(CurEnv->e_df, "w", tf);
1941439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1954557Seric 				usrerr("452 Out of disk space for temp file");
1961439Seric 			}
1971439Seric 			else
1986986Seric 				syserr("collect: Cannot write %s", CurEnv->e_df);
1994083Seric 			(void) freopen("/dev/null", "w", tf);
2001392Seric 		}
2011392Seric 	}
2024083Seric 	(void) fclose(tf);
2032900Seric 
2042900Seric 	/*
2052900Seric 	**  Find out some information from the headers.
2063386Seric 	**	Examples are who is the from person & the date.
2072900Seric 	*/
2082900Seric 
2095982Seric 	/* message priority */
2105033Seric 	if (!QueueRun)
2115033Seric 	{
2125033Seric 		/* adjust total priority by message priority */
2136901Seric 		CurEnv->e_msgpriority = CurEnv->e_msgsize;
2145033Seric 		p = hvalue("priority");
2155033Seric 		if (p != NULL)
2166909Seric 			CurEnv->e_class = priencode(p);
2176909Seric 		else
2186909Seric 			CurEnv->e_class = PRI_NORMAL;
2196909Seric 		CurEnv->e_msgpriority -= CurEnv->e_class * WKPRIFACT;
2205033Seric 	}
2214622Seric 
2225982Seric 	/* special handling */
2235982Seric 	p = hvalue("special-handling");
2245982Seric 	if (p != NULL)
2255982Seric 		spechandling(p);
2265982Seric 
2272900Seric 	/* from person */
2282900Seric 	xfrom = hvalue("sender");
2292900Seric 	if (xfrom == NULL)
2306901Seric 		xfrom = CurEnv->e_origfrom;
2314710Seric 	if (ArpaMode)
2324316Seric 		setfrom(xfrom, (char *) NULL);
2332900Seric 
2343390Seric 	/* full name of from person */
2353390Seric 	p = hvalue("full-name");
2363390Seric 	if (p != NULL)
2373390Seric 		define('x', p);
2384210Seric 	else
2394210Seric 	{
2405917Seric 		extern char *getxpart();
2413390Seric 
2424210Seric 		/*
2434210Seric 		**  Try to extract the full name from a general From:
2444210Seric 		**  field.  We take anything which is a comment as a
2454210Seric 		**  first choice.  Failing in that, we see if there is
2464210Seric 		**  a "machine readable" name (in <angle brackets>); if
2474210Seric 		**  so we take anything preceeding that clause.
2484210Seric 		**
2494210Seric 		**  If we blow it here it's not all that serious.
2504210Seric 		*/
2514210Seric 
2524210Seric 		p = hvalue("original-from");
2534371Seric 		if (p == NULL)
2546901Seric 			p = CurEnv->e_origfrom;
2555917Seric 		p = getxpart(p);
2565917Seric 		if (p != NULL)
2575917Seric 			define('x', newstr(p));
2584210Seric 	}
2594210Seric 
2602900Seric 	/* date message originated */
2614149Seric 	p = hvalue("posted-date");
2624149Seric 	if (p == NULL)
2634149Seric 		p = hvalue("date");
2642900Seric 	if (p != NULL)
2652900Seric 	{
2663386Seric 		define('a', p);
2673386Seric 		/* we don't have a good way to do canonical conversion ....
2683386Seric 		define('d', newstr(arpatounix(p)));
2693386Seric 		.... so we will ignore the problem for the time being */
2702900Seric 	}
2712900Seric 
272*7367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
273*7367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
274*7367Seric 	{
275*7367Seric 		register ADDRESS *q;
276*7367Seric 
277*7367Seric 		/* create an Apparently-To: field */
278*7367Seric 		/*    that or reject the message.... */
279*7367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
280*7367Seric 		{
281*7367Seric # ifdef DEBUG
282*7367Seric 			if (Debug > 1)
283*7367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
284*7367Seric # endif DEBUG
285*7367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
286*7367Seric 		}
287*7367Seric 	}
288*7367Seric 
2897364Seric 	/* check for hop count overflow */
2907364Seric 	if (HopCount > MAXHOP)
2917364Seric 		syserr("Too many hops (%d max); probably forwarding loop", MAXHOP);
2927364Seric 
2936986Seric 	if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL)
2946986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2952900Seric 
2962900Seric # ifdef DEBUG
2972900Seric 	if (Debug)
2982900Seric 	{
2994316Seric 		HDR *h;
3004316Seric 		extern char *capitalize();
3014316Seric 
3022900Seric 		printf("----- collected header -----\n");
3036901Seric 		for (h = CurEnv->e_header; h != NULL; h = h->h_link)
3042900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
3052900Seric 		printf("----------------------------\n");
3062900Seric 	}
3072900Seric # endif DEBUG
3084162Seric 	return;
3091392Seric }
3101392Seric /*
3112900Seric **  EATFROM -- chew up a UNIX style from line and process
3122900Seric **
3132900Seric **	This does indeed make some assumptions about the format
3142900Seric **	of UNIX messages.
3152900Seric **
3162900Seric **	Parameters:
3172900Seric **		fm -- the from line.
3182900Seric **
3192900Seric **	Returns:
3202900Seric **		none.
3212900Seric **
3222900Seric **	Side Effects:
3232900Seric **		extracts what information it can from the header,
3243386Seric **		such as the date.
3252900Seric */
3262900Seric 
3274321Seric # ifndef NOTUNIX
3284321Seric 
3294203Seric char	*DowList[] =
3304203Seric {
3314203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3324203Seric };
3334203Seric 
3342900Seric char	*MonthList[] =
3352900Seric {
3362900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3372900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3382900Seric 	NULL
3392900Seric };
3402900Seric 
3412900Seric eatfrom(fm)
3422900Seric 	char *fm;
3432900Seric {
3442900Seric 	register char *p;
3452900Seric 	register char **dt;
3462900Seric 
3474203Seric # ifdef DEBUG
3484203Seric 	if (Debug > 1)
3494203Seric 		printf("eatfrom(%s)\n", fm);
3504203Seric # endif DEBUG
3514203Seric 
3522900Seric 	/* find the date part */
3532900Seric 	p = fm;
3542900Seric 	while (*p != '\0')
3552900Seric 	{
3562900Seric 		/* skip a word */
3572900Seric 		while (*p != '\0' && *p != ' ')
3582900Seric 			*p++;
3592900Seric 		while (*p == ' ')
3602900Seric 			*p++;
3612900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3622900Seric 			continue;
3632900Seric 
3642900Seric 		/* we have a possible date */
3654203Seric 		for (dt = DowList; *dt != NULL; dt++)
3662900Seric 			if (strncmp(*dt, p, 3) == 0)
3672900Seric 				break;
3684203Seric 		if (*dt == NULL)
3694203Seric 			continue;
3702900Seric 
3714203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3724203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3734203Seric 				break;
3742900Seric 		if (*dt != NULL)
3752900Seric 			break;
3762900Seric 	}
3772900Seric 
3782900Seric 	if (*p != NULL)
3792900Seric 	{
3803386Seric 		char *q;
3815366Seric 		extern char *arpadate();
3823386Seric 
3832900Seric 		/* we have found a date */
3843386Seric 		q = xalloc(25);
3853386Seric 		strncpy(q, p, 25);
3863386Seric 		q[24] = '\0';
3873386Seric 		define('d', q);
3885366Seric 		q = arpadate(q);
3895366Seric 		define('a', newstr(q));
3902900Seric 	}
3912900Seric }
3924321Seric 
3934321Seric # endif NOTUNIX
3944622Seric /*
3954622Seric **  PRIENCODE -- encode external priority names into internal values.
3964622Seric **
3974622Seric **	Parameters:
3984622Seric **		p -- priority in ascii.
3994622Seric **
4004622Seric **	Returns:
4014622Seric **		priority as a numeric level.
4024622Seric **
4034622Seric **	Side Effects:
4044622Seric **		none.
4054622Seric */
4064622Seric 
4074622Seric struct prio
4084622Seric {
4094622Seric 	char	*pri_name;	/* external name of priority */
4104622Seric 	int	pri_val;	/* internal value for same */
4114622Seric };
4124622Seric 
4134622Seric static struct prio	Prio[] =
4144622Seric {
4154633Seric 	"alert",		PRI_ALERT,
4164633Seric 	"quick",		PRI_QUICK,
4174633Seric 	"first-class",		PRI_FIRSTCL,
4184622Seric 	"normal",		PRI_NORMAL,
4194622Seric 	"second-class",		PRI_SECONDCL,
4204622Seric 	"third-class",		PRI_THIRDCL,
4216909Seric 	"junk",			PRI_JUNK,
4224622Seric 	NULL,			PRI_NORMAL,
4234622Seric };
4244622Seric 
4254622Seric priencode(p)
4264622Seric 	char *p;
4274622Seric {
4284622Seric 	register struct prio *pl;
4294633Seric 	extern bool sameword();
4304622Seric 
4314622Seric 	for (pl = Prio; pl->pri_name != NULL; pl++)
4324622Seric 	{
4334633Seric 		if (sameword(p, pl->pri_name))
4344622Seric 			break;
4354622Seric 	}
4364622Seric 	return (pl->pri_val);
4374622Seric }
4385982Seric /*
4395982Seric **  SPECHANDLE -- do special handling
4405982Seric **
4415982Seric **	Parameters:
4425982Seric **		p -- pointer to list of special handling words.
4435982Seric **
4445982Seric **	Returns:
4455982Seric **		none.
4465982Seric **
4475982Seric **	Side Effects:
4485982Seric **		Sets flags as indicated by p.
4495982Seric */
4505982Seric 
4515982Seric struct handling
4525982Seric {
4535982Seric 	char	*han_name;		/* word to get this magic */
4545982Seric 	int	han_what;		/* what to do, see below */
4555982Seric };
4565982Seric 
4575982Seric /* modes for han_what */
4585982Seric # define	HAN_NONE	0	/* nothing special */
4595982Seric # define	HAN_RRECEIPT	1	/* give return receipt */
4605982Seric 
4615982Seric struct handling	Handling[] =
4625982Seric {
4635982Seric 	"return-receipt-requested",	HAN_RRECEIPT,
4645982Seric 	NULL,				HAN_NONE
4655982Seric };
4665982Seric 
4675982Seric spechandling(p)
4685982Seric 	register char *p;
4695982Seric {
4705982Seric 	register char *w;
4715982Seric 	register struct handling *h;
4725982Seric 	extern bool sameword();
4735982Seric 
4745982Seric 	while (*p != '\0')
4755982Seric 	{
4765982Seric 		/* collect a word to compare to */
4775982Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
4785982Seric 			p++;
4795982Seric 		if (*p == '\0')
4805982Seric 			break;
4815982Seric 		w = p;
4825982Seric 		while (*p != '\0' && *p != ',' && !isspace(*p))
4835982Seric 			p++;
4845982Seric 		if (*p != '\0')
4855982Seric 			*p++ = '\0';
4865982Seric 
4875982Seric 		/* scan the special handling table */
4885982Seric 		for (h = Handling; h->han_name != NULL; h++)
4895982Seric 			if (sameword(h->han_name, w))
4905982Seric 				break;
4915982Seric 
4925982Seric 		/* see if we can do anything interesting */
4935982Seric 		switch (h->han_what)
4945982Seric 		{
4955982Seric 		  case HAN_NONE:	/* nothing to be done */
4965982Seric 			break;
4975982Seric 
4985982Seric 		  case HAN_RRECEIPT:	/* give return receipt */
4996901Seric 			CurEnv->e_retreceipt = TRUE;
5005982Seric # ifdef DEBUG
5015982Seric 			if (Debug > 2)
5025982Seric 				printf(">>> Return receipt requested\n");
5035982Seric # endif DEBUG
5045982Seric 			break;
5055982Seric 
5065982Seric 		  default:
5075982Seric 			syserr("spechandling: handling %d (%s)", h->han_what, w);
5085982Seric 		}
5095982Seric 	}
5105982Seric }
511