11439Seric # include <errno.h>
23309Seric # include "sendmail.h"
31392Seric 
4*7809Seric SCCSID(@(#)collect.c	3.49		08/22/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 	extern char *hvalue();
405192Seric 	extern char *macvalue();
411392Seric 
421392Seric 	/*
431392Seric 	**  Create the temp file name and create the file.
441392Seric 	*/
451392Seric 
46*7809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
47*7809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
481392Seric 	{
49*7809Seric 		syserr("Cannot create %s", CurEnv->e_df);
505366Seric 		NoReturn = TRUE;
515366Seric 		finis();
521392Seric 	}
53*7809Seric 	(void) chmod(CurEnv->e_df, 0600);
541392Seric 
554316Seric 	/*
567788Seric 	**  Create the Received: line if we want to.
575185Seric 	*/
585185Seric 
595366Seric 	if (Smtp && macvalue('s') != NULL)
605185Seric 	{
615185Seric 		char xbuf[50];
625185Seric 
637788Seric 		/* this should be in the config file */
647788Seric 		(void) sprintf(xbuf, "Received: from $s by $i with SMTP; $b");
656975Seric 		expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv);
665192Seric 		(void) chompheader(buf, FALSE);
675185Seric 	}
685185Seric 
695185Seric 	/*
704322Seric 	**  Tell ARPANET to go ahead.
714322Seric 	*/
724322Seric 
734710Seric 	if (sayok)
744710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
754322Seric 
764322Seric 	/*
774316Seric 	**  Try to read a UNIX-style From line
784316Seric 	*/
794316Seric 
805975Seric 	if (fgets(buf, sizeof buf, InChannel) == NULL)
814162Seric 		return;
824557Seric 	fixcrlf(buf, FALSE);
834321Seric # ifndef NOTUNIX
844322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
852900Seric 	{
862900Seric 		eatfrom(buf);
875975Seric 		(void) fgets(buf, sizeof buf, InChannel);
884557Seric 		fixcrlf(buf, FALSE);
892900Seric 	}
904321Seric # endif NOTUNIX
912900Seric 
921392Seric 	/*
935975Seric 	**  Copy InChannel to temp file & do message editing.
941392Seric 	**	To keep certain mailers from getting confused,
951392Seric 	**	and to keep the output clean, lines that look
961392Seric 	**	like UNIX "From" lines are deleted in the header,
971392Seric 	**	and prepended with ">" in the body.
981392Seric 	*/
991392Seric 
1005975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1011392Seric 	{
1024316Seric 		register char c;
1034316Seric 		extern bool isheader();
1044316Seric 
1057681Seric 		/* if the line is too long, throw the rest away */
1067681Seric 		if (index(buf, '\n') == NULL)
1077681Seric 		{
1087681Seric 			while ((c = getc(InChannel)) != '\n')
1097681Seric 				continue;
1107681Seric 			/* give an error? */
1117681Seric 		}
1127681Seric 
1134557Seric 		fixcrlf(buf, FALSE);
1144557Seric 
1152900Seric 		/* see if the header is over */
1162900Seric 		if (!isheader(buf))
1172900Seric 			break;
1182900Seric 
1192900Seric 		/* get the rest of this field */
1205975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1211392Seric 		{
1222900Seric 			p = &buf[strlen(buf)];
1232900Seric 			*p++ = c;
1245975Seric 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
1252900Seric 				break;
1264557Seric 			fixcrlf(p, FALSE);
1271392Seric 		}
1285975Seric 		if (!feof(InChannel))
1295975Seric 			(void) ungetc(c, InChannel);
1301392Seric 
1316901Seric 		CurEnv->e_msgsize += strlen(buf);
1321392Seric 
1332900Seric 		/*
1342900Seric 		**  Snarf header away.
1352900Seric 		*/
1362900Seric 
1373391Seric 		if (bitset(H_EOH, chompheader(buf, FALSE)))
1383058Seric 			break;
1392900Seric 	}
1401392Seric 
1412900Seric # ifdef DEBUG
1427673Seric 	if (tTd(30, 1))
1432900Seric 		printf("EOH\n");
1442900Seric # endif DEBUG
1452900Seric 
1462900Seric 	/* throw away a blank line */
1472900Seric 	if (buf[0] == '\n')
1484557Seric 	{
1495975Seric 		(void) fgets(buf, sizeof buf, InChannel);
1504557Seric 		fixcrlf(buf, FALSE);
1514557Seric 	}
1522900Seric 
1532900Seric 	/*
1542900Seric 	**  Collect the body of the message.
1552900Seric 	*/
1562900Seric 
1575975Seric 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
1582900Seric 	{
1594156Seric 		register int i;
1604551Seric 		register char *bp = buf;
1614156Seric 
1624557Seric 		fixcrlf(buf, FALSE);
1634557Seric 
1642900Seric 		/* check for end-of-message */
1652900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1662900Seric 			break;
1672900Seric 
1684551Seric 		/* check for transparent dot */
1694551Seric 		if (Smtp && *bp == '.')
1704551Seric 			bp++;
1714551Seric 
1724321Seric # ifndef NOTUNIX
1732900Seric 		/* Hide UNIX-like From lines */
1744551Seric 		if (strncmp(bp, "From ", 5) == 0)
1751392Seric 		{
1762900Seric 			fputs(">", tf);
1776901Seric 			CurEnv->e_msgsize++;
1781392Seric 		}
1794321Seric # endif NOTUNIX
1804156Seric 
1814156Seric 		/*
1824156Seric 		**  Figure message length, output the line to the temp
1834156Seric 		**  file, and insert a newline if missing.
1844156Seric 		*/
1854156Seric 
1864551Seric 		i = strlen(bp);
1876901Seric 		CurEnv->e_msgsize += i;
1884551Seric 		fputs(bp, tf);
1894551Seric 		if (bp[i - 1] != '\n')
1904156Seric 			fputs("\n", tf);
1911392Seric 		if (ferror(tf))
1921392Seric 		{
1931439Seric 			if (errno == ENOSPC)
1941439Seric 			{
1956986Seric 				(void) freopen(CurEnv->e_df, "w", tf);
1961439Seric 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
1974557Seric 				usrerr("452 Out of disk space for temp file");
1981439Seric 			}
1991439Seric 			else
2006986Seric 				syserr("collect: Cannot write %s", CurEnv->e_df);
2014083Seric 			(void) freopen("/dev/null", "w", tf);
2021392Seric 		}
2031392Seric 	}
2044083Seric 	(void) fclose(tf);
2052900Seric 
2062900Seric 	/*
2072900Seric 	**  Find out some information from the headers.
2083386Seric 	**	Examples are who is the from person & the date.
2092900Seric 	*/
2102900Seric 
2117782Seric 	eatheader();
2127673Seric 
2137782Seric 	/*
2147782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2157782Seric 	*/
2164622Seric 
2177367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2187367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2197367Seric 	{
2207367Seric 		register ADDRESS *q;
2217367Seric 
2227367Seric 		/* create an Apparently-To: field */
2237367Seric 		/*    that or reject the message.... */
2247367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2257367Seric 		{
2267389Seric 			if (q->q_alias != NULL)
2277389Seric 				continue;
2287367Seric # ifdef DEBUG
2297673Seric 			if (tTd(30, 3))
2307367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2317367Seric # endif DEBUG
2327367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2337367Seric 		}
2347367Seric 	}
2357367Seric 
2367364Seric 	/* check for hop count overflow */
2377364Seric 	if (HopCount > MAXHOP)
2387364Seric 		syserr("Too many hops (%d max); probably forwarding loop", MAXHOP);
2397364Seric 
2406986Seric 	if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL)
2416986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2422900Seric 
2437673Seric 	/*
2447673Seric 	**  Log collection information.
2457673Seric 	*/
2467673Seric 
2477673Seric # ifdef LOG
2487673Seric 	if (LogLevel > 1)
249*7809Seric 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
250*7809Seric 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
2517758Seric 		       CurEnv->e_class);
2527673Seric # endif LOG
2534162Seric 	return;
2541392Seric }
2551392Seric /*
2562900Seric **  EATFROM -- chew up a UNIX style from line and process
2572900Seric **
2582900Seric **	This does indeed make some assumptions about the format
2592900Seric **	of UNIX messages.
2602900Seric **
2612900Seric **	Parameters:
2622900Seric **		fm -- the from line.
2632900Seric **
2642900Seric **	Returns:
2652900Seric **		none.
2662900Seric **
2672900Seric **	Side Effects:
2682900Seric **		extracts what information it can from the header,
2693386Seric **		such as the date.
2702900Seric */
2712900Seric 
2724321Seric # ifndef NOTUNIX
2734321Seric 
2744203Seric char	*DowList[] =
2754203Seric {
2764203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2774203Seric };
2784203Seric 
2792900Seric char	*MonthList[] =
2802900Seric {
2812900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2822900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2832900Seric 	NULL
2842900Seric };
2852900Seric 
2862900Seric eatfrom(fm)
2872900Seric 	char *fm;
2882900Seric {
2892900Seric 	register char *p;
2902900Seric 	register char **dt;
2912900Seric 
2924203Seric # ifdef DEBUG
2937673Seric 	if (tTd(30, 2))
2944203Seric 		printf("eatfrom(%s)\n", fm);
2954203Seric # endif DEBUG
2964203Seric 
2972900Seric 	/* find the date part */
2982900Seric 	p = fm;
2992900Seric 	while (*p != '\0')
3002900Seric 	{
3012900Seric 		/* skip a word */
3022900Seric 		while (*p != '\0' && *p != ' ')
3032900Seric 			*p++;
3042900Seric 		while (*p == ' ')
3052900Seric 			*p++;
3062900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3072900Seric 			continue;
3082900Seric 
3092900Seric 		/* we have a possible date */
3104203Seric 		for (dt = DowList; *dt != NULL; dt++)
3112900Seric 			if (strncmp(*dt, p, 3) == 0)
3122900Seric 				break;
3134203Seric 		if (*dt == NULL)
3144203Seric 			continue;
3152900Seric 
3164203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3174203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3184203Seric 				break;
3192900Seric 		if (*dt != NULL)
3202900Seric 			break;
3212900Seric 	}
3222900Seric 
3232900Seric 	if (*p != NULL)
3242900Seric 	{
3253386Seric 		char *q;
3265366Seric 		extern char *arpadate();
3273386Seric 
3282900Seric 		/* we have found a date */
3293386Seric 		q = xalloc(25);
3303386Seric 		strncpy(q, p, 25);
3313386Seric 		q[24] = '\0';
3323386Seric 		define('d', q);
3335366Seric 		q = arpadate(q);
3345366Seric 		define('a', newstr(q));
3352900Seric 	}
3362900Seric }
3374321Seric 
3384321Seric # endif NOTUNIX
339