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