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