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.7 (Berkeley) 11/17/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 			{
138 				freebuf[0] = '\0';
139 				break;
140 			}
141 
142 			/* is this a continuation line? */
143 			if (*freebuf != ' ' && *freebuf != '\t')
144 				break;
145 
146 			if (!flusheol(freebuf, InChannel))
147 				goto readerr;
148 
149 			fixcrlf(freebuf, TRUE);
150 			clen = strlen(freebuf) + 1;
151 
152 			/* if insufficient room, dynamically allocate buffer */
153 			if (clen >= curbuffree)
154 			{
155 				/* reallocate buffer */
156 				int nbuflen = ((p - curbuf) + clen) * 2;
157 				char *nbuf = xalloc(nbuflen);
158 
159 				p = nbuf + curbuflen;
160 				curbuffree = nbuflen - curbuflen;
161 				bcopy(curbuf, nbuf, curbuflen);
162 				if (curbuf != buf && curbuf != buf2)
163 					free(curbuf);
164 				curbuf = nbuf;
165 			}
166 			*p++ = '\n';
167 			bcopy(freebuf, p, clen - 1);
168 			p += clen - 1;
169 			curbuffree -= clen;
170 			curbuflen += clen;
171 		}
172 		*p++ = '\0';
173 
174 		e->e_msgsize += curbuflen;
175 
176 		/*
177 		**  The working buffer now becomes the free buffer, since
178 		**  the free buffer contains a new header field.
179 		**
180 		**  This is premature, since we still havent called
181 		**  chompheader() to process the field we just created
182 		**  (so the call to chompheader() will use `freebuf').
183 		**  This convolution is necessary so that if we break out
184 		**  of the loop due to H_EOH, `workbuf' will always be
185 		**  the next unprocessed buffer.
186 		*/
187 
188 		{
189 			register char *tmp = workbuf;
190 			workbuf = freebuf;
191 			freebuf = tmp;
192 		}
193 
194 		/*
195 		**  Snarf header away.
196 		*/
197 
198 		if (bitset(H_EOH, chompheader(curbuf, FALSE, e)))
199 			break;
200 
201 		/*
202 		**  If the buffer was dynamically allocated, free it.
203 		*/
204 
205 		if (curbuf != buf && curbuf != buf2)
206 			free(curbuf);
207 	}
208 
209 	if (tTd(30, 1))
210 		printf("EOH\n");
211 
212 	if (*workbuf == '\0')
213 	{
214 		/* throw away a blank line */
215 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
216 				"message separator read") == NULL)
217 			goto readerr;
218 	}
219 	else if (workbuf == buf2)	/* guarantee `buf' contains data */
220 		(void) strcpy(buf, buf2);
221 
222 	/*
223 	**  Collect the body of the message.
224 	*/
225 
226 	for (;;)
227 	{
228 		register char *bp = buf;
229 
230 		fixcrlf(buf, TRUE);
231 
232 		/* check for end-of-message */
233 		if (!ignrdot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
234 			break;
235 
236 		/* check for transparent dot */
237 		if (OpMode == MD_SMTP && bp[0] == '.' && bp[1] == '.')
238 			bp++;
239 
240 		/*
241 		**  Figure message length, output the line to the temp
242 		**  file, and insert a newline if missing.
243 		*/
244 
245 		e->e_msgsize += strlen(bp) + 1;
246 		fputs(bp, tf);
247 		fputs("\n", tf);
248 		if (ferror(tf))
249 			tferror(tf, e);
250 		if (sfgets(buf, MAXLINE, InChannel, TimeOuts.to_datablock,
251 				"message body read") == NULL)
252 			goto readerr;
253 	}
254 
255 	if (feof(InChannel) || ferror(InChannel))
256 	{
257 readerr:
258 		if (tTd(30, 1))
259 			printf("collect: read error\n");
260 		inputerr = TRUE;
261 	}
262 
263 	if (fflush(tf) != 0)
264 		tferror(tf, e);
265 	if (fsync(fileno(tf)) < 0 || fclose(tf) < 0)
266 	{
267 		syserr("cannot sync message data to disk (%s)", e->e_df);
268 		finis();
269 	}
270 
271 	/* An EOF when running SMTP is an error */
272 	if (inputerr && OpMode == MD_SMTP)
273 	{
274 		char *host;
275 		char *problem;
276 
277 		host = RealHostName;
278 		if (host == NULL)
279 			host = "localhost";
280 
281 		if (feof(InChannel))
282 			problem = "unexpected close";
283 		else if (ferror(InChannel))
284 			problem = "I/O error";
285 		else
286 			problem = "read timeout";
287 # ifdef LOG
288 		if (LogLevel > 0 && feof(InChannel))
289 			syslog(LOG_NOTICE,
290 			    "collect: %s on connection from %s, sender=%s: %m\n",
291 			    problem, host, e->e_from.q_paddr);
292 # endif
293 		(feof(InChannel) ? usrerr : syserr)
294 			("451 collect: %s on connection from %s, from=%s",
295 				problem, host, e->e_from.q_paddr);
296 
297 		/* don't return an error indication */
298 		e->e_to = NULL;
299 		e->e_flags &= ~EF_FATALERRS;
300 		e->e_flags |= EF_CLRQUEUE;
301 
302 		/* and don't try to deliver the partial message either */
303 		if (InChild)
304 			ExitStat = EX_QUIT;
305 		finis();
306 	}
307 
308 	/*
309 	**  Find out some information from the headers.
310 	**	Examples are who is the from person & the date.
311 	*/
312 
313 	eatheader(e, !requeueflag);
314 
315 	/* collect statistics */
316 	if (OpMode != MD_VERIFY)
317 		markstats(e, (ADDRESS *) NULL);
318 
319 	/*
320 	**  Add an Apparently-To: line if we have no recipient lines.
321 	*/
322 
323 	if (hvalue("to", e) == NULL && hvalue("cc", e) == NULL &&
324 	    hvalue("bcc", e) == NULL && hvalue("apparently-to", e) == NULL)
325 	{
326 		register ADDRESS *q;
327 
328 		/* create an Apparently-To: field */
329 		/*    that or reject the message.... */
330 		for (q = e->e_sendqueue; q != NULL; q = q->q_next)
331 		{
332 			if (q->q_alias != NULL)
333 				continue;
334 			if (tTd(30, 3))
335 				printf("Adding Apparently-To: %s\n", q->q_paddr);
336 			addheader("Apparently-To", q->q_paddr, e);
337 		}
338 	}
339 
340 	/* check for message too large */
341 	if (MaxMessageSize > 0 && e->e_msgsize > MaxMessageSize)
342 	{
343 		usrerr("552 Message exceeds maximum fixed size (%ld)",
344 			MaxMessageSize);
345 	}
346 
347 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
348 	{
349 		/* we haven't acked receipt yet, so just chuck this */
350 		syserr("Cannot reopen %s", e->e_df);
351 		finis();
352 	}
353 }
354 /*
355 **  FLUSHEOL -- if not at EOL, throw away rest of input line.
356 **
357 **	Parameters:
358 **		buf -- last line read in (checked for '\n'),
359 **		fp -- file to be read from.
360 **
361 **	Returns:
362 **		FALSE on error from sfgets(), TRUE otherwise.
363 **
364 **	Side Effects:
365 **		none.
366 */
367 
368 bool
369 flusheol(buf, fp)
370 	char *buf;
371 	FILE *fp;
372 {
373 	register char *p = buf;
374 	bool printmsg = TRUE;
375 	char junkbuf[MAXLINE];
376 
377 	while (strchr(p, '\n') == NULL)
378 	{
379 		if (printmsg)
380 			usrerr("553 header line too long");
381 		printmsg = FALSE;
382 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock,
383 				"long line flush") == NULL)
384 			return (FALSE);
385 		p = junkbuf;
386 	}
387 
388 	return (TRUE);
389 }
390 /*
391 **  TFERROR -- signal error on writing the temporary file.
392 **
393 **	Parameters:
394 **		tf -- the file pointer for the temporary file.
395 **
396 **	Returns:
397 **		none.
398 **
399 **	Side Effects:
400 **		Gives an error message.
401 **		Arranges for following output to go elsewhere.
402 */
403 
404 tferror(tf, e)
405 	FILE *tf;
406 	register ENVELOPE *e;
407 {
408 	if (errno == ENOSPC)
409 	{
410 		(void) freopen(e->e_df, "w", tf);
411 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
412 		usrerr("452 Out of disk space for temp file");
413 	}
414 	else
415 		syserr("collect: Cannot write %s", e->e_df);
416 	(void) freopen("/dev/null", "w", tf);
417 }
418 /*
419 **  EATFROM -- chew up a UNIX style from line and process
420 **
421 **	This does indeed make some assumptions about the format
422 **	of UNIX messages.
423 **
424 **	Parameters:
425 **		fm -- the from line.
426 **
427 **	Returns:
428 **		none.
429 **
430 **	Side Effects:
431 **		extracts what information it can from the header,
432 **		such as the date.
433 */
434 
435 # ifndef NOTUNIX
436 
437 char	*DowList[] =
438 {
439 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
440 };
441 
442 char	*MonthList[] =
443 {
444 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
445 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
446 	NULL
447 };
448 
449 eatfrom(fm, e)
450 	char *fm;
451 	register ENVELOPE *e;
452 {
453 	register char *p;
454 	register char **dt;
455 
456 	if (tTd(30, 2))
457 		printf("eatfrom(%s)\n", fm);
458 
459 	/* find the date part */
460 	p = fm;
461 	while (*p != '\0')
462 	{
463 		/* skip a word */
464 		while (*p != '\0' && *p != ' ')
465 			p++;
466 		while (*p == ' ')
467 			p++;
468 		if (!(isascii(*p) && isupper(*p)) ||
469 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
470 			continue;
471 
472 		/* we have a possible date */
473 		for (dt = DowList; *dt != NULL; dt++)
474 			if (strncmp(*dt, p, 3) == 0)
475 				break;
476 		if (*dt == NULL)
477 			continue;
478 
479 		for (dt = MonthList; *dt != NULL; dt++)
480 			if (strncmp(*dt, &p[4], 3) == 0)
481 				break;
482 		if (*dt != NULL)
483 			break;
484 	}
485 
486 	if (*p != '\0')
487 	{
488 		char *q;
489 		extern char *arpadate();
490 
491 		/* we have found a date */
492 		q = xalloc(25);
493 		(void) strncpy(q, p, 25);
494 		q[24] = '\0';
495 		q = arpadate(q);
496 		define('a', newstr(q), e);
497 	}
498 }
499 
500 # endif /* NOTUNIX */
501