122697Sdist /*
2*34920Sbostic  * Copyright (c) 1983 Eric P. Allman
333728Sbostic  * Copyright (c) 1988 Regents of the University of California.
433728Sbostic  * All rights reserved.
533728Sbostic  *
633728Sbostic  * Redistribution and use in source and binary forms are permitted
7*34920Sbostic  * provided that the above copyright notice and this paragraph are
8*34920Sbostic  * duplicated in all such forms and that any documentation,
9*34920Sbostic  * advertising materials, and other materials related to such
10*34920Sbostic  * distribution and use acknowledge that the software was developed
11*34920Sbostic  * by the University of California, Berkeley.  The name of the
12*34920Sbostic  * University may not be used to endorse or promote products derived
13*34920Sbostic  * from this software without specific prior written permission.
14*34920Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15*34920Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16*34920Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733728Sbostic  */
1822697Sdist 
1922697Sdist #ifndef lint
20*34920Sbostic static char sccsid[] = "@(#)collect.c	5.4 (Berkeley) 06/30/88";
2133728Sbostic #endif /* not lint */
2222697Sdist 
231439Seric # include <errno.h>
243309Seric # include "sendmail.h"
251392Seric 
261392Seric /*
272969Seric **  COLLECT -- read & parse message header & make temp file.
281392Seric **
291392Seric **	Creates a temporary file name and copies the standard
309371Seric **	input to that file.  Leading UNIX-style "From" lines are
319371Seric **	stripped off (after important information is extracted).
321392Seric **
331392Seric **	Parameters:
344710Seric **		sayok -- if set, give an ARPANET style message
354710Seric **			to say we are ready to collect input.
361392Seric **
371392Seric **	Returns:
384162Seric **		none.
391392Seric **
401392Seric **	Side Effects:
411392Seric **		Temp file is created and filled.
424162Seric **		The from person may be set.
431392Seric */
441392Seric 
454710Seric collect(sayok)
464710Seric 	bool sayok;
471392Seric {
481392Seric 	register FILE *tf;
497852Seric 	char buf[MAXFIELD+2];
501392Seric 	register char *p;
512900Seric 	extern char *hvalue();
521392Seric 
531392Seric 	/*
541392Seric 	**  Create the temp file name and create the file.
551392Seric 	*/
561392Seric 
577809Seric 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
587809Seric 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
591392Seric 	{
607809Seric 		syserr("Cannot create %s", CurEnv->e_df);
615366Seric 		NoReturn = TRUE;
625366Seric 		finis();
631392Seric 	}
649047Seric 	(void) chmod(CurEnv->e_df, FileMode);
651392Seric 
664316Seric 	/*
674322Seric 	**  Tell ARPANET to go ahead.
684322Seric 	*/
694322Seric 
704710Seric 	if (sayok)
714710Seric 		message("354", "Enter mail, end with \".\" on a line by itself");
724322Seric 
734322Seric 	/*
744316Seric 	**  Try to read a UNIX-style From line
754316Seric 	*/
764316Seric 
7715532Seric 	(void) sfgets(buf, sizeof buf, InChannel);
784557Seric 	fixcrlf(buf, FALSE);
794321Seric # ifndef NOTUNIX
804322Seric 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
812900Seric 	{
822900Seric 		eatfrom(buf);
8313932Seric 		(void) sfgets(buf, sizeof buf, InChannel);
844557Seric 		fixcrlf(buf, FALSE);
852900Seric 	}
864321Seric # endif NOTUNIX
872900Seric 
881392Seric 	/*
895975Seric 	**  Copy InChannel to temp file & do message editing.
901392Seric 	**	To keep certain mailers from getting confused,
911392Seric 	**	and to keep the output clean, lines that look
9213932Seric 	**	like UNIX "From" lines are deleted in the header.
931392Seric 	*/
941392Seric 
9515532Seric 	do
961392Seric 	{
9715532Seric 		int c;
984316Seric 		extern bool isheader();
994316Seric 
10019036Seric 		/* drop out on error */
10119036Seric 		if (ferror(InChannel))
10219036Seric 			break;
10319036Seric 
1047681Seric 		/* if the line is too long, throw the rest away */
1057681Seric 		if (index(buf, '\n') == NULL)
1067681Seric 		{
10715532Seric 			while ((c = getc(InChannel)) != '\n' && c != EOF)
1087681Seric 				continue;
1097681Seric 			/* give an error? */
1107681Seric 		}
1117681Seric 
1127852Seric 		fixcrlf(buf, TRUE);
1134557Seric 
1142900Seric 		/* see if the header is over */
1152900Seric 		if (!isheader(buf))
1162900Seric 			break;
1172900Seric 
1182900Seric 		/* get the rest of this field */
1195975Seric 		while ((c = getc(InChannel)) == ' ' || c == '\t')
1201392Seric 		{
1212900Seric 			p = &buf[strlen(buf)];
1227852Seric 			*p++ = '\n';
1232900Seric 			*p++ = c;
12413932Seric 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
1252900Seric 				break;
1267852Seric 			fixcrlf(p, TRUE);
1271392Seric 		}
12819036Seric 		if (!feof(InChannel) && !ferror(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;
13915532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
1401392Seric 
1412900Seric # ifdef DEBUG
1427673Seric 	if (tTd(30, 1))
1432900Seric 		printf("EOH\n");
1442900Seric # endif DEBUG
1452900Seric 
1462900Seric 	/* throw away a blank line */
1477852Seric 	if (buf[0] == '\0')
14813932Seric 		(void) sfgets(buf, MAXFIELD, InChannel);
1492900Seric 
1502900Seric 	/*
1512900Seric 	**  Collect the body of the message.
1522900Seric 	*/
1532900Seric 
15415532Seric 	do
1552900Seric 	{
1564551Seric 		register char *bp = buf;
1574156Seric 
1587852Seric 		fixcrlf(buf, TRUE);
1594557Seric 
1602900Seric 		/* check for end-of-message */
1612900Seric 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
1622900Seric 			break;
1632900Seric 
1644551Seric 		/* check for transparent dot */
1659371Seric 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
1664551Seric 			bp++;
1674551Seric 
1684156Seric 		/*
1694156Seric 		**  Figure message length, output the line to the temp
1704156Seric 		**  file, and insert a newline if missing.
1714156Seric 		*/
1724156Seric 
1739371Seric 		CurEnv->e_msgsize += strlen(bp) + 1;
1744551Seric 		fputs(bp, tf);
1757852Seric 		fputs("\n", tf);
1761392Seric 		if (ferror(tf))
17711544Seric 			tferror(tf);
17815532Seric 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
17911544Seric 	if (fflush(tf) != 0)
18011544Seric 		tferror(tf);
1814083Seric 	(void) fclose(tf);
1822900Seric 
18311145Seric 	/* An EOF when running SMTP is an error */
18419036Seric 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
18516136Seric 	{
18616136Seric 		syserr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
18711145Seric 
18816136Seric 		/* don't return an error indication */
18916136Seric 		CurEnv->e_to = NULL;
19016136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
19116136Seric 
19216136Seric 		/* and don't try to deliver the partial message either */
19316136Seric 		finis();
19416136Seric 	}
19516136Seric 
1962900Seric 	/*
1972900Seric 	**  Find out some information from the headers.
1983386Seric 	**	Examples are who is the from person & the date.
1992900Seric 	*/
2002900Seric 
2019371Seric 	eatheader(CurEnv);
2027673Seric 
2037782Seric 	/*
2047782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2057782Seric 	*/
2064622Seric 
2077367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2087367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2097367Seric 	{
2107367Seric 		register ADDRESS *q;
2117367Seric 
2127367Seric 		/* create an Apparently-To: field */
2137367Seric 		/*    that or reject the message.... */
2147367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2157367Seric 		{
2167389Seric 			if (q->q_alias != NULL)
2177389Seric 				continue;
2187367Seric # ifdef DEBUG
2197673Seric 			if (tTd(30, 3))
2207367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2217367Seric # endif DEBUG
2227367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2237367Seric 		}
2247367Seric 	}
2257367Seric 
2269539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2276986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2281392Seric }
2291392Seric /*
23011544Seric **  TFERROR -- signal error on writing the temporary file.
23111544Seric **
23211544Seric **	Parameters:
23311544Seric **		tf -- the file pointer for the temporary file.
23411544Seric **
23511544Seric **	Returns:
23611544Seric **		none.
23711544Seric **
23811544Seric **	Side Effects:
23911544Seric **		Gives an error message.
24011544Seric **		Arranges for following output to go elsewhere.
24111544Seric */
24211544Seric 
24311544Seric tferror(tf)
24411544Seric 	FILE *tf;
24511544Seric {
24611544Seric 	if (errno == ENOSPC)
24711544Seric 	{
24811544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
24911544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
25011544Seric 		usrerr("452 Out of disk space for temp file");
25111544Seric 	}
25211544Seric 	else
25311544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
25411544Seric 	(void) freopen("/dev/null", "w", tf);
25511544Seric }
25611544Seric /*
2572900Seric **  EATFROM -- chew up a UNIX style from line and process
2582900Seric **
2592900Seric **	This does indeed make some assumptions about the format
2602900Seric **	of UNIX messages.
2612900Seric **
2622900Seric **	Parameters:
2632900Seric **		fm -- the from line.
2642900Seric **
2652900Seric **	Returns:
2662900Seric **		none.
2672900Seric **
2682900Seric **	Side Effects:
2692900Seric **		extracts what information it can from the header,
2703386Seric **		such as the date.
2712900Seric */
2722900Seric 
2734321Seric # ifndef NOTUNIX
2744321Seric 
2754203Seric char	*DowList[] =
2764203Seric {
2774203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2784203Seric };
2794203Seric 
2802900Seric char	*MonthList[] =
2812900Seric {
2822900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2832900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2842900Seric 	NULL
2852900Seric };
2862900Seric 
2872900Seric eatfrom(fm)
2882900Seric 	char *fm;
2892900Seric {
2902900Seric 	register char *p;
2912900Seric 	register char **dt;
2922900Seric 
2934203Seric # ifdef DEBUG
2947673Seric 	if (tTd(30, 2))
2954203Seric 		printf("eatfrom(%s)\n", fm);
2964203Seric # endif DEBUG
2974203Seric 
2982900Seric 	/* find the date part */
2992900Seric 	p = fm;
3002900Seric 	while (*p != '\0')
3012900Seric 	{
3022900Seric 		/* skip a word */
3032900Seric 		while (*p != '\0' && *p != ' ')
30416896Seric 			p++;
3052900Seric 		while (*p == ' ')
30616896Seric 			p++;
3072900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3082900Seric 			continue;
3092900Seric 
3102900Seric 		/* we have a possible date */
3114203Seric 		for (dt = DowList; *dt != NULL; dt++)
3122900Seric 			if (strncmp(*dt, p, 3) == 0)
3132900Seric 				break;
3144203Seric 		if (*dt == NULL)
3154203Seric 			continue;
3162900Seric 
3174203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3184203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3194203Seric 				break;
3202900Seric 		if (*dt != NULL)
3212900Seric 			break;
3222900Seric 	}
3232900Seric 
3242900Seric 	if (*p != NULL)
3252900Seric 	{
3263386Seric 		char *q;
3275366Seric 		extern char *arpadate();
3283386Seric 
3292900Seric 		/* we have found a date */
3303386Seric 		q = xalloc(25);
33123103Seric 		(void) strncpy(q, p, 25);
3323386Seric 		q[24] = '\0';
3339371Seric 		define('d', q, CurEnv);
3345366Seric 		q = arpadate(q);
3359371Seric 		define('a', newstr(q), CurEnv);
3362900Seric 	}
3372900Seric }
3384321Seric 
3394321Seric # endif NOTUNIX
340