122697Sdist /*
234920Sbostic  * 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
734920Sbostic  * provided that the above copyright notice and this paragraph are
834920Sbostic  * duplicated in all such forms and that any documentation,
934920Sbostic  * advertising materials, and other materials related to such
1034920Sbostic  * distribution and use acknowledge that the software was developed
1134920Sbostic  * by the University of California, Berkeley.  The name of the
1234920Sbostic  * University may not be used to endorse or promote products derived
1334920Sbostic  * from this software without specific prior written permission.
1434920Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1534920Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1634920Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1733728Sbostic  */
1822697Sdist 
1922697Sdist #ifndef lint
20*36230Skarels static char sccsid[] = "@(#)collect.c	5.5 (Berkeley) 11/17/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 	{
186*36230Skarels 		if (RealHostName)
187*36230Skarels 			syslog(LOG_NOTICE,
188*36230Skarels 			    "collect: unexpected close on connection from %s: %m\n",
189*36230Skarels 			    CurEnv->e_from.q_paddr, RealHostName);
190*36230Skarels 		usrerr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
19111145Seric 
19216136Seric 		/* don't return an error indication */
19316136Seric 		CurEnv->e_to = NULL;
19416136Seric 		CurEnv->e_flags &= ~EF_FATALERRS;
19516136Seric 
19616136Seric 		/* and don't try to deliver the partial message either */
19716136Seric 		finis();
19816136Seric 	}
19916136Seric 
2002900Seric 	/*
2012900Seric 	**  Find out some information from the headers.
2023386Seric 	**	Examples are who is the from person & the date.
2032900Seric 	*/
2042900Seric 
2059371Seric 	eatheader(CurEnv);
2067673Seric 
2077782Seric 	/*
2087782Seric 	**  Add an Apparently-To: line if we have no recipient lines.
2097782Seric 	*/
2104622Seric 
2117367Seric 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
2127367Seric 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
2137367Seric 	{
2147367Seric 		register ADDRESS *q;
2157367Seric 
2167367Seric 		/* create an Apparently-To: field */
2177367Seric 		/*    that or reject the message.... */
2187367Seric 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
2197367Seric 		{
2207389Seric 			if (q->q_alias != NULL)
2217389Seric 				continue;
2227367Seric # ifdef DEBUG
2237673Seric 			if (tTd(30, 3))
2247367Seric 				printf("Adding Apparently-To: %s\n", q->q_paddr);
2257367Seric # endif DEBUG
2267367Seric 			addheader("apparently-to", q->q_paddr, CurEnv);
2277367Seric 		}
2287367Seric 	}
2297367Seric 
2309539Seric 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
2316986Seric 		syserr("Cannot reopen %s", CurEnv->e_df);
2321392Seric }
2331392Seric /*
23411544Seric **  TFERROR -- signal error on writing the temporary file.
23511544Seric **
23611544Seric **	Parameters:
23711544Seric **		tf -- the file pointer for the temporary file.
23811544Seric **
23911544Seric **	Returns:
24011544Seric **		none.
24111544Seric **
24211544Seric **	Side Effects:
24311544Seric **		Gives an error message.
24411544Seric **		Arranges for following output to go elsewhere.
24511544Seric */
24611544Seric 
24711544Seric tferror(tf)
24811544Seric 	FILE *tf;
24911544Seric {
25011544Seric 	if (errno == ENOSPC)
25111544Seric 	{
25211544Seric 		(void) freopen(CurEnv->e_df, "w", tf);
25311544Seric 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
25411544Seric 		usrerr("452 Out of disk space for temp file");
25511544Seric 	}
25611544Seric 	else
25711544Seric 		syserr("collect: Cannot write %s", CurEnv->e_df);
25811544Seric 	(void) freopen("/dev/null", "w", tf);
25911544Seric }
26011544Seric /*
2612900Seric **  EATFROM -- chew up a UNIX style from line and process
2622900Seric **
2632900Seric **	This does indeed make some assumptions about the format
2642900Seric **	of UNIX messages.
2652900Seric **
2662900Seric **	Parameters:
2672900Seric **		fm -- the from line.
2682900Seric **
2692900Seric **	Returns:
2702900Seric **		none.
2712900Seric **
2722900Seric **	Side Effects:
2732900Seric **		extracts what information it can from the header,
2743386Seric **		such as the date.
2752900Seric */
2762900Seric 
2774321Seric # ifndef NOTUNIX
2784321Seric 
2794203Seric char	*DowList[] =
2804203Seric {
2814203Seric 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
2824203Seric };
2834203Seric 
2842900Seric char	*MonthList[] =
2852900Seric {
2862900Seric 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2872900Seric 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2882900Seric 	NULL
2892900Seric };
2902900Seric 
2912900Seric eatfrom(fm)
2922900Seric 	char *fm;
2932900Seric {
2942900Seric 	register char *p;
2952900Seric 	register char **dt;
2962900Seric 
2974203Seric # ifdef DEBUG
2987673Seric 	if (tTd(30, 2))
2994203Seric 		printf("eatfrom(%s)\n", fm);
3004203Seric # endif DEBUG
3014203Seric 
3022900Seric 	/* find the date part */
3032900Seric 	p = fm;
3042900Seric 	while (*p != '\0')
3052900Seric 	{
3062900Seric 		/* skip a word */
3072900Seric 		while (*p != '\0' && *p != ' ')
30816896Seric 			p++;
3092900Seric 		while (*p == ' ')
31016896Seric 			p++;
3112900Seric 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
3122900Seric 			continue;
3132900Seric 
3142900Seric 		/* we have a possible date */
3154203Seric 		for (dt = DowList; *dt != NULL; dt++)
3162900Seric 			if (strncmp(*dt, p, 3) == 0)
3172900Seric 				break;
3184203Seric 		if (*dt == NULL)
3194203Seric 			continue;
3202900Seric 
3214203Seric 		for (dt = MonthList; *dt != NULL; dt++)
3224203Seric 			if (strncmp(*dt, &p[4], 3) == 0)
3234203Seric 				break;
3242900Seric 		if (*dt != NULL)
3252900Seric 			break;
3262900Seric 	}
3272900Seric 
3282900Seric 	if (*p != NULL)
3292900Seric 	{
3303386Seric 		char *q;
3315366Seric 		extern char *arpadate();
3323386Seric 
3332900Seric 		/* we have found a date */
3343386Seric 		q = xalloc(25);
33523103Seric 		(void) strncpy(q, p, 25);
3363386Seric 		q[24] = '\0';
3379371Seric 		define('d', q, CurEnv);
3385366Seric 		q = arpadate(q);
3399371Seric 		define('a', newstr(q), CurEnv);
3402900Seric 	}
3412900Seric }
3424321Seric 
3434321Seric # endif NOTUNIX
344