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