1 # include <errno.h>
2 # include "sendmail.h"
3 
4 SCCSID(@(#)collect.c	3.49		08/22/82);
5 
6 /*
7 **  COLLECT -- read & parse message header & make temp file.
8 **
9 **	Creates a temporary file name and copies the standard
10 **	input to that file.  While it is doing it, it looks for
11 **	"From:" and "Sender:" fields to use as the from-person
12 **	(but only if the -a flag is specified).  It prefers to
13 **	to use the "Sender:" field.
14 **
15 **	MIT seems to like to produce "Sent-By:" fields instead
16 **	of "Sender:" fields.  We used to catch this, but it turns
17 **	out that the "Sent-By:" field doesn't always correspond
18 **	to someone real ("___057", for instance), as required by
19 **	the protocol.  So we limp by.....
20 **
21 **	Parameters:
22 **		sayok -- if set, give an ARPANET style message
23 **			to say we are ready to collect input.
24 **
25 **	Returns:
26 **		none.
27 **
28 **	Side Effects:
29 **		Temp file is created and filled.
30 **		The from person may be set.
31 */
32 
33 collect(sayok)
34 	bool sayok;
35 {
36 	register FILE *tf;
37 	char buf[MAXFIELD+1];
38 	register char *p;
39 	extern char *hvalue();
40 	extern char *macvalue();
41 
42 	/*
43 	**  Create the temp file name and create the file.
44 	*/
45 
46 	CurEnv->e_df = newstr(queuename(CurEnv, 'd'));
47 	if ((tf = dfopen(CurEnv->e_df, "w")) == NULL)
48 	{
49 		syserr("Cannot create %s", CurEnv->e_df);
50 		NoReturn = TRUE;
51 		finis();
52 	}
53 	(void) chmod(CurEnv->e_df, 0600);
54 
55 	/*
56 	**  Create the Received: line if we want to.
57 	*/
58 
59 	if (Smtp && macvalue('s') != NULL)
60 	{
61 		char xbuf[50];
62 
63 		/* this should be in the config file */
64 		(void) sprintf(xbuf, "Received: from $s by $i with SMTP; $b");
65 		expand(xbuf, buf, &buf[sizeof buf - 1], CurEnv);
66 		(void) chompheader(buf, FALSE);
67 	}
68 
69 	/*
70 	**  Tell ARPANET to go ahead.
71 	*/
72 
73 	if (sayok)
74 		message("354", "Enter mail, end with \".\" on a line by itself");
75 
76 	/*
77 	**  Try to read a UNIX-style From line
78 	*/
79 
80 	if (fgets(buf, sizeof buf, InChannel) == NULL)
81 		return;
82 	fixcrlf(buf, FALSE);
83 # ifndef NOTUNIX
84 	if (!SaveFrom && strncmp(buf, "From ", 5) == 0)
85 	{
86 		eatfrom(buf);
87 		(void) fgets(buf, sizeof buf, InChannel);
88 		fixcrlf(buf, FALSE);
89 	}
90 # endif NOTUNIX
91 
92 	/*
93 	**  Copy InChannel to temp file & do message editing.
94 	**	To keep certain mailers from getting confused,
95 	**	and to keep the output clean, lines that look
96 	**	like UNIX "From" lines are deleted in the header,
97 	**	and prepended with ">" in the body.
98 	*/
99 
100 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
101 	{
102 		register char c;
103 		extern bool isheader();
104 
105 		/* if the line is too long, throw the rest away */
106 		if (index(buf, '\n') == NULL)
107 		{
108 			while ((c = getc(InChannel)) != '\n')
109 				continue;
110 			/* give an error? */
111 		}
112 
113 		fixcrlf(buf, FALSE);
114 
115 		/* see if the header is over */
116 		if (!isheader(buf))
117 			break;
118 
119 		/* get the rest of this field */
120 		while ((c = getc(InChannel)) == ' ' || c == '\t')
121 		{
122 			p = &buf[strlen(buf)];
123 			*p++ = c;
124 			if (fgets(p, sizeof buf - (p - buf), InChannel) == NULL)
125 				break;
126 			fixcrlf(p, FALSE);
127 		}
128 		if (!feof(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 	}
140 
141 # ifdef DEBUG
142 	if (tTd(30, 1))
143 		printf("EOH\n");
144 # endif DEBUG
145 
146 	/* throw away a blank line */
147 	if (buf[0] == '\n')
148 	{
149 		(void) fgets(buf, sizeof buf, InChannel);
150 		fixcrlf(buf, FALSE);
151 	}
152 
153 	/*
154 	**  Collect the body of the message.
155 	*/
156 
157 	for (; !feof(InChannel); !feof(InChannel) && fgets(buf, sizeof buf, InChannel) != NULL)
158 	{
159 		register int i;
160 		register char *bp = buf;
161 
162 		fixcrlf(buf, FALSE);
163 
164 		/* check for end-of-message */
165 		if (!IgnrDot && buf[0] == '.' && (buf[1] == '\n' || buf[1] == '\0'))
166 			break;
167 
168 		/* check for transparent dot */
169 		if (Smtp && *bp == '.')
170 			bp++;
171 
172 # ifndef NOTUNIX
173 		/* Hide UNIX-like From lines */
174 		if (strncmp(bp, "From ", 5) == 0)
175 		{
176 			fputs(">", tf);
177 			CurEnv->e_msgsize++;
178 		}
179 # endif NOTUNIX
180 
181 		/*
182 		**  Figure message length, output the line to the temp
183 		**  file, and insert a newline if missing.
184 		*/
185 
186 		i = strlen(bp);
187 		CurEnv->e_msgsize += i;
188 		fputs(bp, tf);
189 		if (bp[i - 1] != '\n')
190 			fputs("\n", tf);
191 		if (ferror(tf))
192 		{
193 			if (errno == ENOSPC)
194 			{
195 				(void) freopen(CurEnv->e_df, "w", tf);
196 				fputs("\nMAIL DELETED BECAUSE OF LACK OF DISK SPACE\n\n", tf);
197 				usrerr("452 Out of disk space for temp file");
198 			}
199 			else
200 				syserr("collect: Cannot write %s", CurEnv->e_df);
201 			(void) freopen("/dev/null", "w", tf);
202 		}
203 	}
204 	(void) fclose(tf);
205 
206 	/*
207 	**  Find out some information from the headers.
208 	**	Examples are who is the from person & the date.
209 	*/
210 
211 	eatheader();
212 
213 	/*
214 	**  Add an Apparently-To: line if we have no recipient lines.
215 	*/
216 
217 	if (hvalue("to") == NULL && hvalue("cc") == NULL &&
218 	    hvalue("bcc") == NULL && hvalue("apparently-to") == NULL)
219 	{
220 		register ADDRESS *q;
221 
222 		/* create an Apparently-To: field */
223 		/*    that or reject the message.... */
224 		for (q = CurEnv->e_sendqueue; q != NULL; q = q->q_next)
225 		{
226 			if (q->q_alias != NULL)
227 				continue;
228 # ifdef DEBUG
229 			if (tTd(30, 3))
230 				printf("Adding Apparently-To: %s\n", q->q_paddr);
231 # endif DEBUG
232 			addheader("apparently-to", q->q_paddr, CurEnv);
233 		}
234 	}
235 
236 	/* check for hop count overflow */
237 	if (HopCount > MAXHOP)
238 		syserr("Too many hops (%d max); probably forwarding loop", MAXHOP);
239 
240 	if ((TempFile = fopen(CurEnv->e_df, "r")) == NULL)
241 		syserr("Cannot reopen %s", CurEnv->e_df);
242 
243 	/*
244 	**  Log collection information.
245 	*/
246 
247 # ifdef LOG
248 	if (LogLevel > 1)
249 		syslog(LOG_INFO, "%s: from=%s, size=%ld, class=%d\n",
250 		       CurEnv->e_id, CurEnv->e_from.q_paddr, CurEnv->e_msgsize,
251 		       CurEnv->e_class);
252 # endif LOG
253 	return;
254 }
255 /*
256 **  EATFROM -- chew up a UNIX style from line and process
257 **
258 **	This does indeed make some assumptions about the format
259 **	of UNIX messages.
260 **
261 **	Parameters:
262 **		fm -- the from line.
263 **
264 **	Returns:
265 **		none.
266 **
267 **	Side Effects:
268 **		extracts what information it can from the header,
269 **		such as the date.
270 */
271 
272 # ifndef NOTUNIX
273 
274 char	*DowList[] =
275 {
276 	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", NULL
277 };
278 
279 char	*MonthList[] =
280 {
281 	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
282 	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
283 	NULL
284 };
285 
286 eatfrom(fm)
287 	char *fm;
288 {
289 	register char *p;
290 	register char **dt;
291 
292 # ifdef DEBUG
293 	if (tTd(30, 2))
294 		printf("eatfrom(%s)\n", fm);
295 # endif DEBUG
296 
297 	/* find the date part */
298 	p = fm;
299 	while (*p != '\0')
300 	{
301 		/* skip a word */
302 		while (*p != '\0' && *p != ' ')
303 			*p++;
304 		while (*p == ' ')
305 			*p++;
306 		if (!isupper(*p) || p[3] != ' ' || p[13] != ':' || p[16] != ':')
307 			continue;
308 
309 		/* we have a possible date */
310 		for (dt = DowList; *dt != NULL; dt++)
311 			if (strncmp(*dt, p, 3) == 0)
312 				break;
313 		if (*dt == NULL)
314 			continue;
315 
316 		for (dt = MonthList; *dt != NULL; dt++)
317 			if (strncmp(*dt, &p[4], 3) == 0)
318 				break;
319 		if (*dt != NULL)
320 			break;
321 	}
322 
323 	if (*p != NULL)
324 	{
325 		char *q;
326 		extern char *arpadate();
327 
328 		/* we have found a date */
329 		q = xalloc(25);
330 		strncpy(q, p, 25);
331 		q[24] = '\0';
332 		define('d', q);
333 		q = arpadate(q);
334 		define('a', newstr(q));
335 	}
336 }
337 
338 # endif NOTUNIX
339