1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * %sccs.include.redist.c%
7  */
8 
9 #ifndef lint
10 static char sccsid[] = "@(#)collect.c	8.6 (Berkeley) 10/27/93";
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 **		requeueflag -- this message will be requeued later, so
29 **			don't do final processing on it.
30 **		e -- the current envelope.
31 **
32 **	Returns:
33 **		none.
34 **
35 **	Side Effects:
36 **		Temp file is created and filled.
37 **		The from person may be set.
38 */
39 
40 collect(smtpmode, requeueflag, e)
41 	bool smtpmode;
42 	bool requeueflag;
43 	register ENVELOPE *e;
44 {
45 	register FILE *tf;
46 	bool ignrdot = smtpmode ? FALSE : IgnrDot;
47 	char buf[MAXLINE], buf2[MAXLINE];
48 	register char *workbuf, *freebuf;
49 	bool inputerr = FALSE;
50 	extern char *hvalue();
51 	extern bool isheader(), flusheol();
52 
53 	/*
54 	**  Create the temp file name and create the file.
55 	*/
56 
57 	e->e_df = queuename(e, 'd');
58 	e->e_df = newstr(e->e_df);
59 	if ((tf = dfopen(e->e_df, O_WRONLY|O_CREAT, FileMode)) == NULL)
60 	{
61 		syserr("Cannot create %s", e->e_df);
62 		NoReturn = TRUE;
63 		finis();
64 	}
65 
66 	/*
67 	**  Tell ARPANET to go ahead.
68 	*/
69 
70 	if (smtpmode)
71 		message("354 Enter mail, end with \".\" on a line by itself");
72 
73 	/*
74 	**  Try to read a UNIX-style From line
75 	*/
76 
77 	if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
78 			"initial message read") == NULL)
79 		goto readerr;
80 	fixcrlf(buf, FALSE);
81 # ifndef NOTUNIX
82 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
83 	{
84 		if (!flusheol(buf, InChannel))
85 			goto readerr;
86 		eatfrom(buf, e);
87 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
88 				"message header read") == NULL)
89 			goto readerr;
90 		fixcrlf(buf, FALSE);
91 	}
92 # endif /* NOTUNIX */
93 
94 	/*
95 	**  Copy InChannel to temp file & do message editing.
96 	**	To keep certain mailers from getting confused,
97 	**	and to keep the output clean, lines that look
98 	**	like UNIX "From" lines are deleted in the header.
99 	*/
100 
101 	workbuf = buf;		/* `workbuf' contains a header field */
102 	freebuf = buf2;		/* `freebuf' can be used for read-ahead */
103 	for (;;)
104 	{
105 		char *curbuf;
106 		int curbuffree;
107 		register int curbuflen;
108 		char *p;
109 
110 		/* first, see if the header is over */
111 		if (!isheader(workbuf))
112 		{
113 			fixcrlf(workbuf, TRUE);
114 			break;
115 		}
116 
117 		/* if the line is too long, throw the rest away */
118 		if (!flusheol(workbuf, InChannel))
119 			goto readerr;
120 
121 		/* it's okay to toss '\n' now (flusheol() needed it) */
122 		fixcrlf(workbuf, TRUE);
123 
124 		curbuf = workbuf;
125 		curbuflen = strlen(curbuf);
126 		curbuffree = MAXLINE - curbuflen;
127 		p = curbuf + curbuflen;
128 
129 		/* get the rest of this field */
130 		for (;;)
131 		{
132 			int clen;
133 
134 			if (sfgets(freebuf, MAXLINE, InChannel,
135 					TimeOuts.to_datablock,
136 					"message header read") == NULL)
137 				goto readerr;
138 
139 			/* is this a continuation line? */
140 			if (*freebuf != ' ' && *freebuf != '\t')
141 				break;
142 
143 			if (!flusheol(freebuf, InChannel))
144 				goto readerr;
145 
146 			fixcrlf(freebuf, TRUE);
147 			clen = strlen(freebuf) + 1;
148 
149 			/* if insufficient room, dynamically allocate buffer */
150 			if (clen >= curbuffree)
151 			{
152 				/* reallocate buffer */
153 				int nbuflen = ((p - curbuf) + clen) * 2;
154 				char *nbuf = xalloc(nbuflen);
155 
156 				p = nbuf + curbuflen;
157 				curbuffree = nbuflen - curbuflen;
158 				bcopy(curbuf, nbuf, curbuflen);
159 				if (curbuf != buf && curbuf != buf2)
160 					free(curbuf);
161 				curbuf = nbuf;
162 			}
163 			*p++ = '\n';
164 			bcopy(freebuf, p, clen - 1);
165 			p += clen - 1;
166 			curbuffree -= clen;
167 			curbuflen += clen;
168 		}
169 		*p++ = '\0';
170 
171 		e->e_msgsize += curbuflen;
172 
173 		/*
174 		**  The working buffer now becomes the free buffer, since
175 		**  the free buffer contains a new header field.
176 		**
177 		**  This is premature, since we still havent called
178 		**  chompheader() to process the field we just created
179 		**  (so the call to chompheader() will use `freebuf').
180 		**  This convolution is necessary so that if we break out
181 		**  of the loop due to H_EOH, `workbuf' will always be
182 		**  the next unprocessed buffer.
183 		*/
184 
185 		{
186 			register char *tmp = workbuf;
187 			workbuf = freebuf;
188 			freebuf = tmp;
189 		}
190 
191 		/*
192 		**  Snarf header away.
193 		*/
194 
195 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
196 			break;
197 
198 		/*
199 		**  If the buffer was dynamically allocated, free it.
200 		*/
201 
202 		if (curbuf != buf && curbuf != buf2)
203 			free(curbuf);
204 	}
205 
206 	if (tTd(30, 1))
207 		printf("EOH\n");
208 
209 	if (*workbuf == '\0')
210 	{
211 		/* throw away a blank line */
212 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
213 				"message separator read") == NULL)
214 			goto readerr;
215 	}
216 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
217 		(void) strcpy(buf, buf2);
218 
219 	/*
220 	**  Collect the body of the message.
221 	*/
222 
223 	for (;;)
224 	{
225 		register char *bp = buf;
226 
227 		fixcrlf(buf, TRUE);
228 
229 		/* check for end-of-message */
230 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
231 			break;
232 
233 		/* check for transparent dot */
234 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
235 			bp++;
236 
237 		/*
238 		**  Figure message length, output the line to the temp
239 		**  file, and insert a newline if missing.
240 		*/
241 
242 		e->e_msgsize += strlen(bp) + 1;
243 		fputs(bp, tf);
244 		fputs("\n", tf);
245 		if (ferror(tf))
246 			tferror(tf, e);
247 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
248 				"message body read") == NULL)
249 			goto readerr;
250 	}
251 
252 	if (feof(InChannel) || ferror(InChannel))
253 	{
254 readerr:
255 		inputerr = TRUE;
256 	}
257 
258 	if (fflush(tf) != 0)
259 		tferror(tf, e);
260 	if (fsync(fileno(tf)) < 0 || fclose(tf) < 0)
261 	{
262 		syserr("cannot sync message data to disk (%s)", e->e_df);
263 		finis();
264 	}
265 
266 	/* An EOF when running SMTP is an error */
267 	if (inputerr && OpMode == MD_SMTP)
268 	{
269 		char *host;
270 		char *problem;
271 
272 		host = RealHostName;
273 		if (host == NULL)
274 			host = "localhost";
275 
276 		if (feof(InChannel))
277 			problem = "unexpected close";
278 		else if (ferror(InChannel))
279 			problem = "I/O error";
280 		else
281 			problem = "read timeout";
282 # ifdef LOG
283 		if (LogLevel > 0 && feof(InChannel))
284 			syslog(LOG_NOTICE,
285 			    "collect: %s on connection from %s, sender=%s: %m\n",
286 			    problem, host, e->e_from.q_paddr);
287 # endif
288 		(feof(InChannel) ? usrerr : syserr)
289 			("451 collect: %s on connection from %s, from=%s",
290 				problem, host, e->e_from.q_paddr);
291 
292 		/* don't return an error indication */
293 		e->e_to = NULL;
294 		e->e_flags &= ~EF_FATALERRS;
295 		e->e_flags |= EF_CLRQUEUE;
296 
297 		/* and don't try to deliver the partial message either */
298 		if (InChild)
299 			ExitStat = EX_QUIT;
300 		finis();
301 	}
302 
303 	/*
304 	**  Find out some information from the headers.
305 	**	Examples are who is the from person & the date.
306 	*/
307 
308 	eatheader(e, !requeueflag);
309 
310 	/* collect statistics */
311 	if (OpMode != MD_VERIFY)
312 		markstats(e, (ADDRESS *) NULL);
313 
314 	/*
315 	**  Add an Apparently-To: line if we have no recipient lines.
316 	*/
317 
318 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
319 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
320 	{
321 		register ADDRESS *q;
322 
323 		/* create an Apparently-To: field */
324 		/*    that or reject the message.... */
325 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
326 		{
327 			if (q->q_alias != NULL)
328 				continue;
329 			if (tTd(30, 3))
330 				printf("Adding Apparently-To: %s\n", q->q_paddr);
331 			addheader("Apparently-To", q->q_paddr, e);
332 		}
333 	}
334 
335 	/* check for message too large */
336 	if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
337 	{
338 		usrerr("552 Message exceeds maximum fixed size (%ld)",
339 			MaxMessageSize);
340 	}
341 
342 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
343 	{
344 		/* we haven't acked receipt yet, so just chuck this */
345 		syserr("Cannot reopen %s", e->e_df);
346 		finis();
347 	}
348 }
349 /*
350 **  FLUSHEOL -- if not at EOL, throw away rest of input line.
351 **
352 **	Parameters:
353 **		buf -- last line read in (checked for '\n'),
354 **		fp -- file to be read from.
355 **
356 **	Returns:
357 **		FALSE on error from sfgets(), TRUE otherwise.
358 **
359 **	Side Effects:
360 **		none.
361 */
362 
363 bool
364 flusheol(buf, fp)
365 	char *buf;
366 	FILE *fp;
367 {
368 	register char *p = buf;
369 	bool printmsg = TRUE;
370 	char junkbuf[MAXLINE];
371 
372 	while (strchr(p, '\n') == NULL)
373 	{
374 		if (printmsg)
375 			usrerr("553 header line too long");
376 		printmsg = FALSE;
377 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock,
378 				"long line flush") == NULL)
379 			return (FALSE);
380 		p = junkbuf;
381 	}
382 
383 	return (TRUE);
384 }
385 /*
386 **  TFERROR -- signal error on writing the temporary file.
387 **
388 **	Parameters:
389 **		tf -- the file pointer for the temporary file.
390 **
391 **	Returns:
392 **		none.
393 **
394 **	Side Effects:
395 **		Gives an error message.
396 **		Arranges for following output to go elsewhere.
397 */
398 
399 tferror(tf, e)
400 	FILE *tf;
401 	register ENVELOPE *e;
402 {
403 	if (errno == ENOSPC)
404 	{
405 		(void) freopen(e->e_df, "w", tf);
406 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
407 		usrerr("452 Out of disk space for temp file");
408 	}
409 	else
410 		syserr("collect: Cannot write %s", e->e_df);
411 	(void) freopen("/dev/null", "w", tf);
412 }
413 /*
414 **  EATFROM -- chew up a UNIX style from line and process
415 **
416 **	This does indeed make some assumptions about the format
417 **	of UNIX messages.
418 **
419 **	Parameters:
420 **		fm -- the from line.
421 **
422 **	Returns:
423 **		none.
424 **
425 **	Side Effects:
426 **		extracts what information it can from the header,
427 **		such as the date.
428 */
429 
430 # ifndef NOTUNIX
431 
432 char	*DowList[] =
433 {
434 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
435 };
436 
437 char	*MonthList[] =
438 {
439 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
440 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
441 	NULL
442 };
443 
444 eatfrom(fm, e)
445 	char *fm;
446 	register ENVELOPE *e;
447 {
448 	register char *p;
449 	register char **dt;
450 
451 	if (tTd(30, 2))
452 		printf("eatfrom(%s)\n", fm);
453 
454 	/* find the date part */
455 	p = fm;
456 	while (*p != '\0')
457 	{
458 		/* skip a word */
459 		while (*p != '\0' && *p != ' ')
460 			p++;
461 		while (*p == ' ')
462 			p++;
463 		if (!(isascii(*p) && isupper(*p)) ||
464 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
465 			continue;
466 
467 		/* we have a possible date */
468 		for (dt = DowList; *dt != NULL; dt++)
469 			if (strncmp(*dt, p, 3) == 0)
470 				break;
471 		if (*dt == NULL)
472 			continue;
473 
474 		for (dt = MonthList; *dt != NULL; dt++)
475 			if (strncmp(*dt, &p[4], 3) == 0)
476 				break;
477 		if (*dt != NULL)
478 			break;
479 	}
480 
481 	if (*p != '\0')
482 	{
483 		char *q;
484 		extern char *arpadate();
485 
486 		/* we have found a date */
487 		q = xalloc(25);
488 		(void) strncpy(q, p, 25);
489 		q[24] = '\0';
490 		q = arpadate(q);
491 		define('a', newstr(q), e);
492 	}
493 }
494 
495 # endif /* NOTUNIX */
496