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