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