1 /*
2  * Copyright (c) 1983 Eric P. Allman
3  * Copyright (c) 1988 Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that the above copyright notice and this paragraph are
8  * duplicated in all such forms and that any documentation,
9  * advertising materials, and other materials related to such
10  * distribution and use acknowledge that the software was developed
11  * by the University of California, Berkeley.  The name of the
12  * University may not be used to endorse or promote products derived
13  * from this software without specific prior written permission.
14  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17  */
18 
19 #ifndef lint
20 static char sccsid[] = "@(#)collect.c	5.7 (Berkeley) 01/01/89";
21 #endif /* not lint */
22 
23 # include <errno.h>
24 # include "sendmail.h"
25 
26 /*
27 **  COLLECT -- read & parse message header & make temp file.
28 **
29 **	Creates a temporary file name and copies the standard
30 **	input to that file.  Leading UNIX-style "From" lines are
31 **	stripped off (after important information is extracted).
32 **
33 **	Parameters:
34 **		sayok -- if set, give an ARPANET style message
35 **			to say we are ready to collect input.
36 **
37 **	Returns:
38 **		none.
39 **
40 **	Side Effects:
41 **		Temp file is created and filled.
42 **		The from person may be set.
43 */
44 
45 collect(sayok)
46 	bool sayok;
47 {
48 	register FILE *tf;
49 	char buf[MAXFIELD+2];
50 	register char *p;
51 	extern char *hvalue();
52 
53 	/*
54 	**  Create the temp file name and create the file.
55 	*/
56 
57 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
58 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
59 	{
60 		syserr("Cannot create %s", CurEnv->e_df);
61 		NoReturn = TRUE;
62 		finis();
63 	}
64 	(void) chmod(CurEnv->e_df, FileMode);
65 
66 	/*
67 	**  Tell ARPANET to go ahead.
68 	*/
69 
70 	if (sayok)
71 		message("354", "Enter mail, end with \".\" on a line by itself");
72 
73 	/*
74 	**  Try to read a UNIX-style From line
75 	*/
76 
77 	(void) sfgets(buf, sizeof buf, InChannel);
78 	fixcrlf(buf, FALSE);
79 # ifndef NOTUNIX
80 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
81 	{
82 		eatfrom(buf);
83 		(void) sfgets(buf, sizeof buf, InChannel);
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 	do
96 	{
97 		int c;
98 		extern bool isheader();
99 
100 		/* drop out on error */
101 		if (ferror(InChannel))
102 			break;
103 
104 		/* if the line is too long, throw the rest away */
105 		if (index(buf, '\n') == NULL)
106 		{
107 			while ((c = getc(InChannel)) != '\n' && c != EOF)
108 				continue;
109 			/* give an error? */
110 		}
111 
112 		fixcrlf(buf, TRUE);
113 
114 		/* see if the header is over */
115 		if (!isheader(buf))
116 			break;
117 
118 		/* get the rest of this field */
119 		while ((c = getc(InChannel)) == ' ' || c == '\t')
120 		{
121 			p = &buf[strlen(buf)];
122 			*p++ = '\n';
123 			*p++ = c;
124 			if (sfgets(p, MAXFIELD - (p - buf), InChannel) == NULL)
125 				break;
126 			fixcrlf(p, TRUE);
127 		}
128 		if (!feof(InChannel) && !ferror(InChannel))
129 			(void) ungetc(c, InChannel);
130 
131 		CurEnv->e_msgsize += strlen(buf);
132 
133 		/*
134 		**  Snarf header away.
135 		*/
136 
137 		if (bitset(H_EOH, chompheader(buf, FALSE)))
138 			break;
139 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
140 
141 	if (tTd(30, 1))
142 		printf("EOH\n");
143 
144 	/* throw away a blank line */
145 	if (buf[0] == '\0')
146 		(void) sfgets(buf, MAXFIELD, InChannel);
147 
148 	/*
149 	**  Collect the body of the message.
150 	*/
151 
152 	do
153 	{
154 		register char *bp = buf;
155 
156 		fixcrlf(buf, TRUE);
157 
158 		/* check for end-of-message */
159 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
160 			break;
161 
162 		/* check for transparent dot */
163 		if (OpMode == MD_SMTP && !IgnrDot && bp[0] == '.' && bp[1] == '.')
164 			bp++;
165 
166 		/*
167 		**  Figure message length, output the line to the temp
168 		**  file, and insert a newline if missing.
169 		*/
170 
171 		CurEnv->e_msgsize += strlen(bp) + 1;
172 		fputs(bp, tf);
173 		fputs("\n", tf);
174 		if (ferror(tf))
175 			tferror(tf);
176 	} while (sfgets(buf, MAXFIELD, InChannel) != NULL);
177 	if (fflush(tf) != 0)
178 		tferror(tf);
179 	(void) fclose(tf);
180 
181 	/* An EOF when running SMTP is an error */
182 	if ((feof(InChannel) || ferror(InChannel)) && OpMode == MD_SMTP)
183 	{
184 # ifdef LOG
185 		if (RealHostName != NULL && LogLevel > 0)
186 			syslog(LOG_NOTICE,
187 			    "collect: unexpected close on connection from %s: %m\n",
188 			    CurEnv->e_from.q_paddr, RealHostName);
189 # endif
190 		usrerr("collect: unexpected close, from=%s", CurEnv->e_from.q_paddr);
191 
192 		/* don't return an error indication */
193 		CurEnv->e_to = NULL;
194 		CurEnv->e_flags &= ~EF_FATALERRS;
195 
196 		/* and don't try to deliver the partial message either */
197 		finis();
198 	}
199 
200 	/*
201 	**  Find out some information from the headers.
202 	**	Examples are who is the from person & the date.
203 	*/
204 
205 	eatheader(CurEnv);
206 
207 	/*
208 	**  Add an Apparently-To: line if we have no recipient lines.
209 	*/
210 
211 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
212 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
213 	{
214 		register ADDRESS *q;
215 
216 		/* create an Apparently-To: field */
217 		/*    that or reject the message.... */
218 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
219 		{
220 			if (q->q_alias != NULL)
221 				continue;
222 			if (tTd(30, 3))
223 				printf("Adding Apparently-To: %s\n", q->q_paddr);
224 			addheader("apparently-to", q->q_paddr, CurEnv);
225 		}
226 	}
227 
228 	if ((CurEnv->e_dfp = fopen(CurEnv->e_df, "r")) == NULL)
229 		syserr("Cannot reopen %s", CurEnv->e_df);
230 }
231 /*
232 **  TFERROR -- signal error on writing the temporary file.
233 **
234 **	Parameters:
235 **		tf -- the file pointer for the temporary file.
236 **
237 **	Returns:
238 **		none.
239 **
240 **	Side Effects:
241 **		Gives an error message.
242 **		Arranges for following output to go elsewhere.
243 */
244 
245 tferror(tf)
246 	FILE *tf;
247 {
248 	if (errno == ENOSPC)
249 	{
250 		(void) freopen(CurEnv->e_df, "w", tf);
251 		fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
252 		usrerr("452 Out of disk space for temp file");
253 	}
254 	else
255 		syserr("collect: Cannot write %s", CurEnv->e_df);
256 	(void) freopen("/dev/null", "w", tf);
257 }
258 /*
259 **  EATFROM -- chew up a UNIX style from line and process
260 **
261 **	This does indeed make some assumptions about the format
262 **	of UNIX messages.
263 **
264 **	Parameters:
265 **		fm -- the from line.
266 **
267 **	Returns:
268 **		none.
269 **
270 **	Side Effects:
271 **		extracts what information it can from the header,
272 **		such as the date.
273 */
274 
275 # ifndef NOTUNIX
276 
277 char	*DowList[] =
278 {
279 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
280 };
281 
282 char	*MonthList[] =
283 {
284 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
285 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
286 	NULL
287 };
288 
289 eatfrom(fm)
290 	char *fm;
291 {
292 	register char *p;
293 	register char **dt;
294 
295 	if (tTd(30, 2))
296 		printf("eatfrom(%s)\n", fm);
297 
298 	/* find the date part */
299 	p = fm;
300 	while (*p != '\0')
301 	{
302 		/* skip a word */
303 		while (*p != '\0' && *p != ' ')
304 			p++;
305 		while (*p == ' ')
306 			p++;
307 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
308 			continue;
309 
310 		/* we have a possible date */
311 		for (dt = DowList; *dt != NULL; dt++)
312 			if (strncmp(*dt, p, 3) == 0)
313 				break;
314 		if (*dt == NULL)
315 			continue;
316 
317 		for (dt = MonthList; *dt != NULL; dt++)
318 			if (strncmp(*dt, &p[4], 3) == 0)
319 				break;
320 		if (*dt != NULL)
321 			break;
322 	}
323 
324 	if (*p != NULL)
325 	{
326 		char *q;
327 		extern char *arpadate();
328 
329 		/* we have found a date */
330 		q = xalloc(25);
331 		(void) strncpy(q, p, 25);
332 		q[24] = '\0';
333 		define('d', q, CurEnv);
334 		q = arpadate(q);
335 		define('a', newstr(q), CurEnv);
336 	}
337 }
338 
339 # endif NOTUNIX
340