1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)collect.c	5.11 (Berkeley) 01/04/92";
11 #endif /* not lint */
12 
13 # include <errno.h>
14 # include "sendmail.h"
15 
16 /*
17 **  COLLECT -- read & parse message header & make temp file.
18 **
19 **	Creates a temporary file name and copies the standard
20 **	input to that file.  Leading UNIX-style "From" lines are
21 **	stripped off (after important information is extracted).
22 **
23 **	Parameters:
24 **		smtpmode -- if set, we are running SMTP: give an RFC821
25 **			style message to say we are ready to collect
26 **			input, and never ignore a single dot to mean
27 **			end of message.
28 **
29 **	Returns:
30 **		none.
31 **
32 **	Side Effects:
33 **		Temp file is created and filled.
34 **		The from person may be set.
35 */
36 
37 collect(smtpmode)
38 	bool smtpmode;
39 {
40 	register FILE *tf;
41 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
42 	char buf[MAXFIELD], buf2[MAXFIELD];
43 	register char *workbuf, *freebuf;
44 	register int workbuflen;
45 	extern char *hvalue();
46 	extern bool isheader(), flusheol();
47 
48 	/*
49 	**  Create the temp file name and create the file.
50 	*/
51 
52 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
53 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
54 	{
55 		syserr("Cannot create %s", CurEnv->e_df);
56 		NoReturn = TRUE;
57 		finis();
58 	}
59 	(void) chmod(CurEnv->e_df, FileMode);
60 
61 	/*
62 	**  Tell ARPANET to go ahead.
63 	*/
64 
65 	if (smtpmode)
66 		message("354", "Enter mail, end with \".\" on a line by itself");
67 
68 	/*
69 	**  Try to read a UNIX-style From line
70 	*/
71 
72 	if (sfgets(buf, MAXFIELD, InChannel) == NULL)
73 		goto readerr;
74 	fixcrlf(buf, FALSE);
75 # ifndef NOTUNIX
76 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
77 	{
78 		if (!flusheol(buf, InChannel))
79 			goto readerr;
80 		eatfrom(buf);
81 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
82 			goto readerr;
83 		fixcrlf(buf, FALSE);
84 	}
85 # endif NOTUNIX
86 
87 	/*
88 	**  Copy InChannel to temp file & do message editing.
89 	**	To keep certain mailers from getting confused,
90 	**	and to keep the output clean, lines that look
91 	**	like UNIX "From" lines are deleted in the header.
92 	*/
93 
94 	workbuf = buf;		/* `workbuf' contains a header field */
95 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
96 	for (;;)
97 	{
98 		/* first, see if the header is over */
99 		if (!isheader(workbuf))
100 		{
101 			fixcrlf(workbuf, TRUE);
102 			break;
103 		}
104 
105 		/* if the line is too long, throw the rest away */
106 		if (!flusheol(workbuf, InChannel))
107 			goto readerr;
108 
109 		/* it's okay to toss '\n' now (flusheol() needed it) */
110 		fixcrlf(workbuf, TRUE);
111 
112 		workbuflen = strlen(workbuf);
113 
114 		/* get the rest of this field */
115 		for (;;)
116 		{
117 			if (sfgets(freebuf, MAXFIELD, InChannel) == NULL)
118 				goto readerr;
119 
120 			/* is this a continuation line? */
121 			if (*freebuf != ' ' && *freebuf != '\t')
122 				break;
123 
124 			if (!flusheol(freebuf, InChannel))
125 				goto readerr;
126 
127 			/* yes; append line to `workbuf' if there's room */
128 			if (workbuflen < MAXFIELD-3)
129 			{
130 				register char *p = workbuf + workbuflen;
131 				register char *q = freebuf;
132 
133 				/* we have room for more of this field */
134 				fixcrlf(freebuf, TRUE);
135 				*p++ = '\n'; workbuflen++;
136 				while(*q != '\0' && workbuflen < MAXFIELD-1)
137 				{
138 					*p++ = *q++;
139 					workbuflen++;
140 				}
141 				*p = '\0';
142 			}
143 		}
144 
145 		CurEnv->e_msgsize += workbuflen;
146 
147 		/*
148 		**  The working buffer now becomes the free buffer, since
149 		**  the free buffer contains a new header field.
150 		**
151 		**  This is premature, since we still havent called
152 		**  chompheader() to process the field we just created
153 		**  (so the call to chompheader() will use `freebuf').
154 		**  This convolution is necessary so that if we break out
155 		**  of the loop due to H_EOH, `workbuf' will always be
156 		**  the next unprocessed buffer.
157 		*/
158 
159 		{
160 			register char *tmp = workbuf;
161 			workbuf = freebuf;
162 			freebuf = tmp;
163 		}
164 
165 		/*
166 		**  Snarf header away.
167 		*/
168 
169 		if (bitset(H_EOH, chompheader(freebuf, FALSE)))
170 			break;
171 	}
172 
173 	if (tTd(30, 1))
174 		printf("EOH\n");
175 
176 	if (*workbuf == '\0')
177 	{
178 		/* throw away a blank line */
179 		if (sfgets(buf, MAXFIELD, InChannel) == NULL)
180 			goto readerr;
181 	}
182 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
183 		(void) strcpy(buf, buf2);
184 
185 	/*
186 	**  Collect the body of the message.
187 	*/
188 
189 	do
190 	{
191 		register char *bp = buf;
192 
193 		fixcrlf(buf, TRUE);
194 
195 		/* check for end-of-message */
196 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
197 			break;
198 
199 		/* check for transparent dot */
200 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
201 			bp++;
202 
203 		/*
204 		**  Figure message length, output the line to the temp
205 		**  file, and insert a newline if missing.
206 		*/
207 
208 		CurEnv->e_msgsize += strlen(bp) + 1;
209 		fputs(bp, tf);
210 		fputs("\n", tf);
211 		if (ferror(tf))
212 			tferror(tf);
213 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
214 
215 readerr:
216 	if (fflush(tf) != 0)
217 		tferror(tf);
218 	(void) fclose(tf);
219 
220 	/* An EOF when running SMTP is an error */
221 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
222 	{
223 		int usrerr(), syserr();
224 # ifdef LOG
225 		if (RealHostName != NULL && LogLevel > 0)
226 			syslog(LOG_NOTICE,
227 			    "collect: unexpected close on connection from %s: %m\n",
228 			    CurEnv->e_from.q_paddr, RealHostName);
229 # endif
230 		(feof(InChannel) ? usrerr: syserr)
231 			("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
232 
233 		/* don't return an error indication */
234 		CurEnv->e_to = NULL;
235 		CurEnv->e_flags &= ~EF_FATALERRS;
236 
237 		/* and don't try to deliver the partial message either */
238 		finis();
239 	}
240 
241 	/*
242 	**  Find out some information from the headers.
243 	**	Examples are who is the from person & the date.
244 	*/
245 
246 	eatheader(CurEnv);
247 
248 	/*
249 	**  Add an Apparently-To: line if we have no recipient lines.
250 	*/
251 
252 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
253 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
254 	{
255 		register ADDRESS *q;
256 
257 		/* create an Apparently-To: field */
258 		/*    that or reject the message.... */
259 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
260 		{
261 			if (q->q_alias != NULL)
262 				continue;
263 			if (tTd(30, 3))
264 				printf("Adding Apparently-To: %s\n", q->q_paddr);
265 			addheader("apparently-to", q->q_paddr, CurEnv);
266 		}
267 	}
268 
269 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
270 		syserr("Cannot reopen %s", CurEnv->e_df);
271 }
272 /*
273 **  FLUSHEOL -- if not at EOL, throw away rest of input line.
274 **
275 **	Parameters:
276 **		buf -- last line read in (checked for '\n'),
277 **		fp -- file to be read from.
278 **
279 **	Returns:
280 **		FALSE on error from sfgets(), TRUE otherwise.
281 **
282 **	Side Effects:
283 **		none.
284 */
285 
286 bool
287 flusheol(buf, fp)
288 	char *buf;
289 	FILE *fp;
290 {
291 	char junkbuf[MAXLINE], *sfgets();
292 	register char *p = buf;
293 
294 	while (index(p, '\n') == NULL) {
295 		if (sfgets(junkbuf,MAXLINE,fp) == NULL)
296 			return(FALSE);
297 		p = junkbuf;
298 	}
299 
300 	return(TRUE);
301 }
302 /*
303 **  TFERROR -- signal error on writing the temporary file.
304 **
305 **	Parameters:
306 **		tf -- the file pointer for the temporary file.
307 **
308 **	Returns:
309 **		none.
310 **
311 **	Side Effects:
312 **		Gives an error message.
313 **		Arranges for following output to go elsewhere.
314 */
315 
316 tferror(tf)
317 	FILE *tf;
318 {
319 	if (errno == ENOSPC)
320 	{
321 		(void) freopen(CurEnv->e_df, "w", tf);
322 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
323 		usrerr("452 Out of disk space for temp file");
324 	}
325 	else
326 		syserr("collect: Cannot write %s", CurEnv->e_df);
327 	(void) freopen("/dev/null", "w", tf);
328 }
329 /*
330 **  EATFROM -- chew up a UNIX style from line and process
331 **
332 **	This does indeed make some assumptions about the format
333 **	of UNIX messages.
334 **
335 **	Parameters:
336 **		fm -- the from line.
337 **
338 **	Returns:
339 **		none.
340 **
341 **	Side Effects:
342 **		extracts what information it can from the header,
343 **		such as the date.
344 */
345 
346 # ifndef NOTUNIX
347 
348 char	*DowList[] =
349 {
350 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
351 };
352 
353 char	*MonthList[] =
354 {
355 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
356 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
357 	NULL
358 };
359 
360 eatfrom(fm)
361 	char *fm;
362 {
363 	register char *p;
364 	register char **dt;
365 
366 	if (tTd(30, 2))
367 		printf("eatfrom(%s)\n", fm);
368 
369 	/* find the date part */
370 	p = fm;
371 	while (*p != '\0')
372 	{
373 		/* skip a word */
374 		while (*p != '\0' && *p != ' ')
375 			p++;
376 		while (*p == ' ')
377 			p++;
378 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
379 			continue;
380 
381 		/* we have a possible date */
382 		for (dt = DowList; *dt != NULL; dt++)
383 			if (strncmp(*dt, p, 3) == 0)
384 				break;
385 		if (*dt == NULL)
386 			continue;
387 
388 		for (dt = MonthList; *dt != NULL; dt++)
389 			if (strncmp(*dt, &p[4], 3) == 0)
390 				break;
391 		if (*dt != NULL)
392 			break;
393 	}
394 
395 	if (*p != NULL)
396 	{
397 		char *q;
398 		extern char *arpadate();
399 
400 		/* we have found a date */
401 		q = xalloc(25);
402 		(void) strncpy(q, p, 25);
403 		q[24] = '\0';
404 		define('d', q, CurEnv);
405 		q = arpadate(q);
406 		define('a', newstr(q), CurEnv);
407 	}
408 }
409 
410 # endif NOTUNIX
411