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.13 (Berkeley) 04/01/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 	if ((e->e_dfp = fopen(e->e_df, "r")) == NULL)
304 	{
305 		/* we haven't acked receipt yet, so just chuck this */
306 		syserr("Cannot reopen %s", e->e_df);
307 		finis();
308 	}
309 }
310 /*
311 **  FLUSHEOL -- if not at EOL, throw away rest of input line.
312 **
313 **	Parameters:
314 **		buf -- last line read in (checked for '\n'),
315 **		fp -- file to be read from.
316 **
317 **	Returns:
318 **		FALSE on error from sfgets(), TRUE otherwise.
319 **
320 **	Side Effects:
321 **		none.
322 */
323 
324 bool
325 flusheol(buf, fp)
326 	char *buf;
327 	FILE *fp;
328 {
329 	register char *p = buf;
330 	bool printmsg = TRUE;
331 	char junkbuf[MAXLINE];
332 	extern char *sfgets();
333 
334 	while (strchr(p, '\n') == NULL)
335 	{
336 		if (printmsg)
337 			usrerr("553 header line too long");
338 		printmsg = FALSE;
339 		if (sfgets(junkbuf, MAXLINE, fp, TimeOuts.to_datablock) == NULL)
340 			return (FALSE);
341 		p = junkbuf;
342 	}
343 
344 	return (TRUE);
345 }
346 /*
347 **  TFERROR -- signal error on writing the temporary file.
348 **
349 **	Parameters:
350 **		tf -- the file pointer for the temporary file.
351 **
352 **	Returns:
353 **		none.
354 **
355 **	Side Effects:
356 **		Gives an error message.
357 **		Arranges for following output to go elsewhere.
358 */
359 
360 tferror(tf, e)
361 	FILE *tf;
362 	register ENVELOPE *e;
363 {
364 	if (errno == ENOSPC)
365 	{
366 		(void) freopen(e->e_df, "w", tf);
367 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
368 		usrerr("452 Out of disk space for temp file");
369 	}
370 	else
371 		syserr("collect: Cannot write %s", e->e_df);
372 	(void) freopen("/dev/null", "w", tf);
373 }
374 /*
375 **  EATFROM -- chew up a UNIX style from line and process
376 **
377 **	This does indeed make some assumptions about the format
378 **	of UNIX messages.
379 **
380 **	Parameters:
381 **		fm -- the from line.
382 **
383 **	Returns:
384 **		none.
385 **
386 **	Side Effects:
387 **		extracts what information it can from the header,
388 **		such as the date.
389 */
390 
391 # ifndef NOTUNIX
392 
393 char	*DowList[] =
394 {
395 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
396 };
397 
398 char	*MonthList[] =
399 {
400 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
401 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
402 	NULL
403 };
404 
405 eatfrom(fm, e)
406 	char *fm;
407 	register ENVELOPE *e;
408 {
409 	register char *p;
410 	register char **dt;
411 
412 	if (tTd(30, 2))
413 		printf("eatfrom(%s)\n", fm);
414 
415 	/* find the date part */
416 	p = fm;
417 	while (*p != '\0')
418 	{
419 		/* skip a word */
420 		while (*p != '\0' && *p != ' ')
421 			p++;
422 		while (*p == ' ')
423 			p++;
424 		if (!(isascii(*p) && isupper(*p)) ||
425 		    p[3] != ' ' || p[13] != ':' || p[16] != ':')
426 			continue;
427 
428 		/* we have a possible date */
429 		for (dt = DowList; *dt != NULL; dt++)
430 			if (strncmp(*dt, p, 3) == 0)
431 				break;
432 		if (*dt == NULL)
433 			continue;
434 
435 		for (dt = MonthList; *dt != NULL; dt++)
436 			if (strncmp(*dt, &p[4], 3) == 0)
437 				break;
438 		if (*dt != NULL)
439 			break;
440 	}
441 
442 	if (*p != NULL)
443 	{
444 		char *q;
445 		extern char *arpadate();
446 
447 		/* we have found a date */
448 		q = xalloc(25);
449 		(void) strncpy(q, p, 25);
450 		q[24] = '\0';
451 		q = arpadate(q);
452 		define('a', newstr(q), e);
453 	}
454 }
455 
456 # endif /* NOTUNIX */
457