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