11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*7389Seric SCCSID(@(#)collect.c	3.43		07/14/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 
2727367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2737367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2747367Seric 	{
2757367Seric 		register ADDRESS *q;
2767367Seric 
2777367Seric 		/* create an Apparently-To: field */
2787367Seric 		/*    that or reject the message.... */
2797367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2807367Seric 		{
281*7389Seric 			if (q->q_alias != NULL)
282*7389Seric 				continue;
2837367Seric # ifdef DEBUG
2847367Seric 			if (Debug > 1)
2857367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2867367Seric # endif DEBUG
2877367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2887367Seric 		}
2897367Seric 	}
2907367Seric 
2917364Seric 	/* check for hop count overflow */
2927364Seric 	if (HopCount > MAXHOP)
2937364Seric 		syserr("Too many hops (%d max); probably forwarding loop", MAXHOP);
2947364Seric 
2956986Seric 	if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL)
2966986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2972900Seric 
2982900Seric # ifdef DEBUG
2992900Seric 	if (Debug)
3002900Seric 	{
3014316Seric 		HDR *h;
3024316Seric 		extern char *capitalize();
3034316Seric 
3042900Seric 		printf("----- collected header -----\n");
3056901Seric 		for (h = CurEnv->e_header; h != NULL; h = h->h_link)
3062900Seric 			printf("%s: %s\n", capitalize(h->h_field), h->h_value);
3072900Seric 		printf("----------------------------\n");
3082900Seric 	}
3092900Seric # endif DEBUG
3104162Seric 	return;
3111392Seric }
3121392Seric /*
3132900Seric **  EATFROM -- chew up a UNIX style from line and process
3142900Seric **
3152900Seric **	This does indeed make some assumptions about the format
3162900Seric **	of UNIX messages.
3172900Seric **
3182900Seric **	Parameters:
3192900Seric **		fm -- the from line.
3202900Seric **
3212900Seric **	Returns:
3222900Seric **		none.
3232900Seric **
3242900Seric **	Side Effects:
3252900Seric **		extracts what information it can from the header,
3263386Seric **		such as the date.
3272900Seric */
3282900Seric 
3294321Seric # ifndef NOTUNIX
3304321Seric 
3314203Seric char	*DowList[] =
3324203Seric {
3334203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
3344203Seric };
3354203Seric 
3362900Seric char	*MonthList[] =
3372900Seric {
3382900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
3392900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
3402900Seric 	NULL
3412900Seric };
3422900Seric 
3432900Seric eatfrom(fm)
3442900Seric 	char *fm;
3452900Seric {
3462900Seric 	register char *p;
3472900Seric 	register char **dt;
3482900Seric 
3494203Seric # ifdef DEBUG
3504203Seric 	if (Debug > 1)
3514203Seric 		printf("eatfrom(%s)\n", fm);
3524203Seric # endif DEBUG
3534203Seric 
3542900Seric 	/* find the date part */
3552900Seric 	p = fm;
3562900Seric 	while (*p != '\0')
3572900Seric 	{
3582900Seric 		/* skip a word */
3592900Seric 		while (*p != '\0' && *p != ' ')
3602900Seric 			*p++;
3612900Seric 		while (*p == ' ')
3622900Seric 			*p++;
3632900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3642900Seric 			continue;
3652900Seric 
3662900Seric 		/* we have a possible date */
3674203Seric 		for (dt = DowList; *dt != NULL; dt++)
3682900Seric 			if (strncmp(*dt, p, 3) == 0)
3692900Seric 				break;
3704203Seric 		if (*dt == NULL)
3714203Seric 			continue;
3722900Seric 
3734203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3744203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3754203Seric 				break;
3762900Seric 		if (*dt != NULL)
3772900Seric 			break;
3782900Seric 	}
3792900Seric 
3802900Seric 	if (*p != NULL)
3812900Seric 	{
3823386Seric 		char *q;
3835366Seric 		extern char *arpadate();
3843386Seric 
3852900Seric 		/* we have found a date */
3863386Seric 		q = xalloc(25);
3873386Seric 		strncpy(q, p, 25);
3883386Seric 		q[24] = '\0';
3893386Seric 		define('d', q);
3905366Seric 		q = arpadate(q);
3915366Seric 		define('a', newstr(q));
3922900Seric 	}
3932900Seric }
3944321Seric 
3954321Seric # endif NOTUNIX
3964622Seric /*
3974622Seric **  PRIENCODE -- encode external priority names into internal values.
3984622Seric **
3994622Seric **	Parameters:
4004622Seric **		p -- priority in ascii.
4014622Seric **
4024622Seric **	Returns:
4034622Seric **		priority as a numeric level.
4044622Seric **
4054622Seric **	Side Effects:
4064622Seric **		none.
4074622Seric */
4084622Seric 
4094622Seric struct prio
4104622Seric {
4114622Seric 	char	*pri_name;	/* external name of priority */
4124622Seric 	int	pri_val;	/* internal value for same */
4134622Seric };
4144622Seric 
4154622Seric static struct prio	Prio[] =
4164622Seric {
4174633Seric 	"alert",		PRI_ALERT,
4184633Seric 	"quick",		PRI_QUICK,
4194633Seric 	"first-class",		PRI_FIRSTCL,
4204622Seric 	"normal",		PRI_NORMAL,
4214622Seric 	"second-class",		PRI_SECONDCL,
4224622Seric 	"third-class",		PRI_THIRDCL,
4236909Seric 	"junk",			PRI_JUNK,
4244622Seric 	NULL,			PRI_NORMAL,
4254622Seric };
4264622Seric 
4274622Seric priencode(p)
4284622Seric 	char *p;
4294622Seric {
4304622Seric 	register struct prio *pl;
4314633Seric 	extern bool sameword();
4324622Seric 
4334622Seric 	for (pl = Prio; pl->pri_name != NULL; pl++)
4344622Seric 	{
4354633Seric 		if (sameword(p, pl->pri_name))
4364622Seric 			break;
4374622Seric 	}
4384622Seric 	return (pl->pri_val);
4394622Seric }
4405982Seric /*
4415982Seric **  SPECHANDLE -- do special handling
4425982Seric **
4435982Seric **	Parameters:
4445982Seric **		p -- pointer to list of special handling words.
4455982Seric **
4465982Seric **	Returns:
4475982Seric **		none.
4485982Seric **
4495982Seric **	Side Effects:
4505982Seric **		Sets flags as indicated by p.
4515982Seric */
4525982Seric 
4535982Seric struct handling
4545982Seric {
4555982Seric 	char	*han_name;		/* word to get this magic */
4565982Seric 	int	han_what;		/* what to do, see below */
4575982Seric };
4585982Seric 
4595982Seric /* modes for han_what */
4605982Seric # define	HAN_NONE	0	/* nothing special */
4615982Seric # define	HAN_RRECEIPT	1	/* give return receipt */
4625982Seric 
4635982Seric struct handling	Handling[] =
4645982Seric {
4655982Seric 	"return-receipt-requested",	HAN_RRECEIPT,
4665982Seric 	NULL,				HAN_NONE
4675982Seric };
4685982Seric 
4695982Seric spechandling(p)
4705982Seric 	register char *p;
4715982Seric {
4725982Seric 	register char *w;
4735982Seric 	register struct handling *h;
4745982Seric 	extern bool sameword();
4755982Seric 
4765982Seric 	while (*p != '\0')
4775982Seric 	{
4785982Seric 		/* collect a word to compare to */
4795982Seric 		while (*p != '\0' && (*p == ',' || isspace(*p)))
4805982Seric 			p++;
4815982Seric 		if (*p == '\0')
4825982Seric 			break;
4835982Seric 		w = p;
4845982Seric 		while (*p != '\0' && *p != ',' && !isspace(*p))
4855982Seric 			p++;
4865982Seric 		if (*p != '\0')
4875982Seric 			*p++ = '\0';
4885982Seric 
4895982Seric 		/* scan the special handling table */
4905982Seric 		for (h = Handling; h->han_name != NULL; h++)
4915982Seric 			if (sameword(h->han_name, w))
4925982Seric 				break;
4935982Seric 
4945982Seric 		/* see if we can do anything interesting */
4955982Seric 		switch (h->han_what)
4965982Seric 		{
4975982Seric 		  case HAN_NONE:	/* nothing to be done */
4985982Seric 			break;
4995982Seric 
5005982Seric 		  case HAN_RRECEIPT:	/* give return receipt */
5016901Seric 			CurEnv->e_retreceipt = TRUE;
5025982Seric # ifdef DEBUG
5035982Seric 			if (Debug > 2)
5045982Seric 				printf(">>> Return receipt requested\n");
5055982Seric # endif DEBUG
5065982Seric 			break;
5075982Seric 
5085982Seric 		  default:
5095982Seric 			syserr("spechandling: handling %d (%s)", h->han_what, w);
5105982Seric 		}
5115982Seric 	}
5125982Seric }
513