122697Sdist /*
2*33728Sbostic  * Copyright (c) 1988 Regents of the University of California.
3*33728Sbostic  * All rights reserved.
4*33728Sbostic  *
5*33728Sbostic  * Redistribution and use in source and binary forms are permitted
6*33728Sbostic  * provided that this notice is preserved and that due credit is given
7*33728Sbostic  * to the University of California at Berkeley. The name of the University
8*33728Sbostic  * may not be used to endorse or promote products derived from this
9*33728Sbostic  * software without specific prior written permission. This software
10*33728Sbostic  * is provided ``as is'' without express or implied warranty.
11*33728Sbostic  *
12*33728Sbostic  *  Sendmail
13*33728Sbostic  *  Copyright (c) 1983  Eric P. Allman
14*33728Sbostic  *  Berkeley, California
15*33728Sbostic  */
1622697Sdist 
1722697Sdist #ifndef lint
18*33728Sbostic static char sccsid[] = "@(#)collect.c	5.3 (Berkeley) 03/13/88";
19*33728Sbostic #endif /* not lint */
2022697Sdist 
211439Seric # include <errno.h>
223309Seric # include "sendmail.h"
231392Seric 
241392Seric /*
252969Seric **  COLLECT -- read & parse message header & make temp file.
261392Seric **
271392Seric **	Creates a temporary file name and copies the standard
289371Seric **	input to that file.  Leading UNIX-style "From" lines are
299371Seric **	stripped off (after important information is extracted).
301392Seric **
311392Seric **	Parameters:
324710Seric **		sayok -- if set, give an ARPANET style message
334710Seric **			to say we are ready to collect input.
341392Seric **
351392Seric **	Returns:
364162Seric **		none.
371392Seric **
381392Seric **	Side Effects:
391392Seric **		Temp file is created and filled.
404162Seric **		The from person may be set.
411392Seric */
421392Seric 
434710Seric collect(sayok)
444710Seric 	bool sayok;
451392Seric {
461392Seric 	register FILE *tf;
477852Seric 	char buf[MAXFIELD+2];
481392Seric 	register char *p;
492900Seric 	extern char *hvalue();
501392Seric 
511392Seric 	/*
521392Seric 	**  Create the temp file name and create the file.
531392Seric 	*/
541392Seric 
557809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
567809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
571392Seric 	{
587809Seric 		syserr("Cannot create %s", CurEnv->e_df);
595366Seric 		NoReturn = TRUE;
605366Seric 		finis();
611392Seric 	}
629047Seric 	(void) chmod(CurEnv->e_df, FileMode);
631392Seric 
644316Seric 	/*
654322Seric 	**  Tell ARPANET to go ahead.
664322Seric 	*/
674322Seric 
684710Seric 	if (sayok)
694710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
704322Seric 
714322Seric 	/*
724316Seric 	**  Try to read a UNIX-style From line
734316Seric 	*/
744316Seric 
7515532Seric 	(void) sfgets(buf, sizeof buf, InChannel);
764557Seric 	fixcrlf(buf, FALSE);
774321Seric # ifndef NOTUNIX
784322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
792900Seric 	{
802900Seric 		eatfrom(buf);
8113932Seric 		(void) sfgets(buf, sizeof buf, InChannel);
824557Seric 		fixcrlf(buf, FALSE);
832900Seric 	}
844321Seric # endif NOTUNIX
852900Seric 
861392Seric 	/*
875975Seric 	**  Copy InChannel to temp file & do message editing.
881392Seric 	**	To keep certain mailers from getting confused,
891392Seric 	**	and to keep the output clean, lines that look
9013932Seric 	**	like UNIX "From" lines are deleted in the header.
911392Seric 	*/
921392Seric 
9315532Seric 	do
941392Seric 	{
9515532Seric 		int c;
964316Seric 		extern bool isheader();
974316Seric 
9819036Seric 		/* drop out on error */
9919036Seric 		if (ferror(InChannel))
10019036Seric 			break;
10119036Seric 
1027681Seric 		/* if the line is too long, throw the rest away */
1037681Seric 		if (index(buf, '\n') == NULL)
1047681Seric 		{
10515532Seric 			while ((c = getc(InChannel)) != '\n' && c != EOF)
1067681Seric 				continue;
1077681Seric 			/* give an error? */
1087681Seric 		}
1097681Seric 
1107852Seric 		fixcrlf(buf, TRUE);
1114557Seric 
1122900Seric 		/* see if the header is over */
1132900Seric 		if (!isheader(buf))
1142900Seric 			break;
1152900Seric 
1162900Seric 		/* get the rest of this field */
1175975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1181392Seric 		{
1192900Seric 			p = &buf[strlen(buf)];
1207852Seric 			*p++ = '\n';
1212900Seric 			*p++ = c;
12213932Seric 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1232900Seric 				break;
1247852Seric 			fixcrlf(p, TRUE);
1251392Seric 		}
12619036Seric 		if (!feof(InChannel) && !ferror(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;
13715532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
1381392Seric 
1392900Seric # ifdef DEBUG
1407673Seric 	if (tTd(30, 1))
1412900Seric 		printf("EOH\n");
1422900Seric # endif DEBUG
1432900Seric 
1442900Seric 	/* throw away a blank line */
1457852Seric 	if (buf[0] == '\0')
14613932Seric 		(void) sfgets(buf, MAXFIELD, InChannel);
1472900Seric 
1482900Seric 	/*
1492900Seric 	**  Collect the body of the message.
1502900Seric 	*/
1512900Seric 
15215532Seric 	do
1532900Seric 	{
1544551Seric 		register char *bp = buf;
1554156Seric 
1567852Seric 		fixcrlf(buf, TRUE);
1574557Seric 
1582900Seric 		/* check for end-of-message */
1592900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1602900Seric 			break;
1612900Seric 
1624551Seric 		/* check for transparent dot */
1639371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1644551Seric 			bp++;
1654551Seric 
1664156Seric 		/*
1674156Seric 		**  Figure message length, output the line to the temp
1684156Seric 		**  file, and insert a newline if missing.
1694156Seric 		*/
1704156Seric 
1719371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1724551Seric 		fputs(bp, tf);
1737852Seric 		fputs("\n", tf);
1741392Seric 		if (ferror(tf))
17511544Seric 			tferror(tf);
17615532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
17711544Seric 	if (fflush(tf) != 0)
17811544Seric 		tferror(tf);
1794083Seric 	(void) fclose(tf);
1802900Seric 
18111145Seric 	/* An EOF when running SMTP is an error */
18219036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
18316136Seric 	{
18416136Seric 		syserr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
18511145Seric 
18616136Seric 		/* don't return an error indication */
18716136Seric 		CurEnv->e_to = NULL;
18816136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
18916136Seric 
19016136Seric 		/* and don't try to deliver the partial message either */
19116136Seric 		finis();
19216136Seric 	}
19316136Seric 
1942900Seric 	/*
1952900Seric 	**  Find out some information from the headers.
1963386Seric 	**	Examples are who is the from person & the date.
1972900Seric 	*/
1982900Seric 
1999371Seric 	eatheader(CurEnv);
2007673Seric 
2017782Seric 	/*
2027782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2037782Seric 	*/
2044622Seric 
2057367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2067367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2077367Seric 	{
2087367Seric 		register ADDRESS *q;
2097367Seric 
2107367Seric 		/* create an Apparently-To: field */
2117367Seric 		/*    that or reject the message.... */
2127367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2137367Seric 		{
2147389Seric 			if (q->q_alias != NULL)
2157389Seric 				continue;
2167367Seric # ifdef DEBUG
2177673Seric 			if (tTd(30, 3))
2187367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2197367Seric # endif DEBUG
2207367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2217367Seric 		}
2227367Seric 	}
2237367Seric 
2249539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2256986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2261392Seric }
2271392Seric /*
22811544Seric **  TFERROR -- signal error on writing the temporary file.
22911544Seric **
23011544Seric **	Parameters:
23111544Seric **		tf -- the file pointer for the temporary file.
23211544Seric **
23311544Seric **	Returns:
23411544Seric **		none.
23511544Seric **
23611544Seric **	Side Effects:
23711544Seric **		Gives an error message.
23811544Seric **		Arranges for following output to go elsewhere.
23911544Seric */
24011544Seric 
24111544Seric tferror(tf)
24211544Seric 	FILE *tf;
24311544Seric {
24411544Seric 	if (errno == ENOSPC)
24511544Seric 	{
24611544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
24711544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
24811544Seric 		usrerr("452 Out of disk space for temp file");
24911544Seric 	}
25011544Seric 	else
25111544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
25211544Seric 	(void) freopen("/dev/null", "w", tf);
25311544Seric }
25411544Seric /*
2552900Seric **  EATFROM -- chew up a UNIX style from line and process
2562900Seric **
2572900Seric **	This does indeed make some assumptions about the format
2582900Seric **	of UNIX messages.
2592900Seric **
2602900Seric **	Parameters:
2612900Seric **		fm -- the from line.
2622900Seric **
2632900Seric **	Returns:
2642900Seric **		none.
2652900Seric **
2662900Seric **	Side Effects:
2672900Seric **		extracts what information it can from the header,
2683386Seric **		such as the date.
2692900Seric */
2702900Seric 
2714321Seric # ifndef NOTUNIX
2724321Seric 
2734203Seric char	*DowList[] =
2744203Seric {
2754203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2764203Seric };
2774203Seric 
2782900Seric char	*MonthList[] =
2792900Seric {
2802900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2812900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2822900Seric 	NULL
2832900Seric };
2842900Seric 
2852900Seric eatfrom(fm)
2862900Seric 	char *fm;
2872900Seric {
2882900Seric 	register char *p;
2892900Seric 	register char **dt;
2902900Seric 
2914203Seric # ifdef DEBUG
2927673Seric 	if (tTd(30, 2))
2934203Seric 		printf("eatfrom(%s)\n", fm);
2944203Seric # endif DEBUG
2954203Seric 
2962900Seric 	/* find the date part */
2972900Seric 	p = fm;
2982900Seric 	while (*p != '\0')
2992900Seric 	{
3002900Seric 		/* skip a word */
3012900Seric 		while (*p != '\0' && *p != ' ')
30216896Seric 			p++;
3032900Seric 		while (*p == ' ')
30416896Seric 			p++;
3052900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3062900Seric 			continue;
3072900Seric 
3082900Seric 		/* we have a possible date */
3094203Seric 		for (dt = DowList; *dt != NULL; dt++)
3102900Seric 			if (strncmp(*dt, p, 3) == 0)
3112900Seric 				break;
3124203Seric 		if (*dt == NULL)
3134203Seric 			continue;
3142900Seric 
3154203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3164203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3174203Seric 				break;
3182900Seric 		if (*dt != NULL)
3192900Seric 			break;
3202900Seric 	}
3212900Seric 
3222900Seric 	if (*p != NULL)
3232900Seric 	{
3243386Seric 		char *q;
3255366Seric 		extern char *arpadate();
3263386Seric 
3272900Seric 		/* we have found a date */
3283386Seric 		q = xalloc(25);
32923103Seric 		(void) strncpy(q, p, 25);
3303386Seric 		q[24] = '\0';
3319371Seric 		define('d', q, CurEnv);
3325366Seric 		q = arpadate(q);
3339371Seric 		define('a', newstr(q), CurEnv);
3342900Seric 	}
3352900Seric }
3364321Seric 
3374321Seric # endif NOTUNIX
338