masqmail

view src/accept.c @ 405:0dd84c8f2524

Broke long lines.
author markus schnalke <meillo@marmaro.de>
date Wed, 29 Feb 2012 10:28:23 +0100
parents 5781ba87df95
children 9b93c0a3bd8c
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include "masqmail.h"
22 #include "readsock.h"
24 /* must match PROT_* in masqmail.h */
25 gchar *prot_names[] = {
26 "local",
27 "SMTP",
28 "ESMTP",
29 "(unknown)" /* should not happen, but better than crashing. */
30 };
32 static gchar*
33 string_base62(gchar *res, guint value, gchar len)
34 {
35 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 "abcdefghijklmnopqrstuvwxyz";
37 gchar *p = res + len;
38 *p = '\0';
39 while (p > res) {
40 *(--p) = base62_chars[value % 62];
41 value /= 62;
42 }
43 return res;
44 }
46 /*
47 ** accept message from anywhere.
48 ** A message from local is indicated by msg->recieved_host == NULL
49 **
50 ** The -t option: With the ACC_RCPT_FROM_HEAD flag the addrs found found
51 ** in To/Cc/Bcc headers are added to the recipient list.
52 */
53 accept_error
54 accept_message_stream(FILE *in, message *msg, guint flags)
55 {
56 gchar *line, *line1;
57 int line_size = MAX_DATALINE;
58 gboolean in_headers = TRUE;
59 header *hdr = NULL;
60 gint line_cnt = 0, data_size = 0;
62 line = g_malloc(line_size);
63 *line = '\0';
65 while (1) {
66 int len = read_sockline1(in, &line, &line_size, 5 * 60,
67 READSOCKL_CVT_CRLF);
68 line1 = line;
70 if ((*line == '.') && (!(flags & ACC_DOT_IGNORE))) {
71 if (line[1] == '\n') {
72 g_free(line);
73 break;
74 }
75 line1++;
76 }
78 if (len==-1 && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
79 /* at EOF but last line was not terminated by CR */
80 /* some MUAs allow unterminated lines */
81 gint len1 = strlen(line1);
82 if (len1 > 0 && line1[len1-1] != '\n') {
83 line1[len1] = '\n';
84 line1[len1+1] = '\0';
85 msg->data_list = g_list_prepend(msg->data_list,
86 g_strdup(line1));
87 data_size += strlen(line1);
88 line_cnt++;
89 }
90 break;
92 } else if (len == -1) {
93 g_free(line);
94 return AERR_EOF;
96 } else if (len == -2) {
97 /* should not happen any more */
98 g_free(line);
99 return AERR_OVERFLOW;
101 } else if (len == -3) {
102 g_free(line);
103 return AERR_TIMEOUT;
105 } else if (len <= 0) {
106 /* does not happen */
107 g_free(line);
108 DEBUG(5) debugf("read_sockline returned %d\n", len);
109 return AERR_UNKNOWN;
111 }
113 if (in_headers) {
114 /* some pop servers send the 'From ' line, skip it: */
115 if (!msg->hdr_list && strncmp(line1, "From ", 5)==0) {
116 continue;
117 }
119 if (*line1 == ' ' || *line1 == '\t') {
120 /* continuation of 'folded' header: */
121 if (hdr) {
122 char *cp;
123 cp = g_strconcat(hdr->header, line1,
124 NULL);
125 hdr->value = cp + (hdr->value -
126 hdr->header);
127 free(hdr->header);
128 hdr->header = cp;
129 }
130 } else if (*line1 == '\n') {
131 /* an empty line marks end of headers */
132 in_headers = FALSE;
134 } else if ((hdr = get_header(line1))) {
135 /* another header */
136 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
137 } else {
138 /*
139 ** Should be another header but none was
140 ** recognized, so this seems to be the first
141 ** data line of a broken mailer which does
142 ** not add an empty line after the headers.
143 */
144 in_headers = FALSE;
145 msg->data_list = g_list_prepend(msg->data_list,
146 g_strdup(line1));
147 }
148 } else {
149 /* message body */
150 msg->data_list = g_list_prepend(msg->data_list,
151 g_strdup(line1));
152 data_size += strlen(line1);
153 line_cnt++;
154 }
156 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
157 DEBUG(4) debugf("accept_message_stream(): "
158 "received %d bytes (conf.max_msg_size=%d)\n",
159 data_size, conf.max_msg_size);
160 return AERR_SIZE;
161 }
162 }
163 DEBUG(4) debugf("received %d lines of data (%d bytes)\n",
164 line_cnt, data_size);
166 if (!msg->data_list) {
167 /* make sure data list is not NULL */
168 msg->data_list = g_list_append(NULL, g_strdup(""));
169 }
170 msg->data_list = g_list_reverse(msg->data_list);
172 /* we have succesfully received the mail data */
174 msg->data_size = data_size;
175 msg->received_time = time(NULL);
177 return AERR_OK;
178 }
180 static void
181 ensure_return_path(message *msg)
182 {
183 GList *hdr_list;
184 header *hdr;
185 gchar *addr;
187 if (msg->return_path) {
188 return;
189 }
191 DEBUG(3) debugf("return_path == NULL\n");
193 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
194 if (!hdr_list) {
195 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
196 }
197 if (hdr_list) {
198 hdr = (header *) (g_list_first(hdr_list)->data);
200 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
202 addr = g_strdup(hdr->value);
203 g_strchomp(addr);
204 msg->return_path = create_address_qualified(addr,
205 FALSE, msg->received_host);
206 if (msg->return_path) {
207 DEBUG(3) debugf("setting return_path to %s\n",
208 addr_string(msg->return_path));
209 msg->hdr_list = g_list_append(msg->hdr_list,
210 create_header(HEAD_UNKNOWN,
211 "X-Warning: return path set from %s "
212 "address\n",
213 (hdr->id == HEAD_SENDER) ?
214 "Sender:" : "From:"));
215 }
216 g_free(addr);
217 }
218 if (!msg->return_path) {
219 /* no Sender: or From: or create_address_qualified failed */
220 msg->return_path = create_address_qualified("postmaster",
221 TRUE, conf.host_name);
222 DEBUG(3) debugf("setting return_path to %s\n",
223 addr_string(msg->return_path));
224 msg->hdr_list = g_list_append(msg->hdr_list,
225 create_header(HEAD_UNKNOWN,
226 "X-Warning: real return path is unknown\n"));
227 }
228 }
230 static accept_error
231 scan_headers(message *msg, guint flags)
232 {
233 gboolean has_id = FALSE;
234 gboolean has_date = FALSE;
235 gboolean has_sender = FALSE;
236 gboolean has_from = FALSE;
237 gboolean has_to_or_cc = FALSE;
238 GList *hdr_node, *hdr_node_next;
239 header *hdr;
241 for (hdr_node = g_list_first(msg->hdr_list); hdr_node;
242 hdr_node = hdr_node_next) {
243 hdr_node_next = g_list_next(hdr_node);
244 hdr = ((header *) (hdr_node->data));
245 DEBUG(5) debugf("scanning headers: %s", hdr->header);
246 switch (hdr->id) {
247 case HEAD_MESSAGE_ID:
248 has_id = TRUE;
249 break;
250 case HEAD_DATE:
251 has_date = TRUE;
252 break;
253 case HEAD_FROM:
254 has_from = TRUE;
255 break;
256 case HEAD_SENDER:
257 has_sender = TRUE;
258 break;
259 case HEAD_TO:
260 case HEAD_CC:
261 has_to_or_cc = TRUE;
262 /* fall through */
263 case HEAD_BCC:
264 if (flags & ACC_RCPT_FROM_HEAD) {
265 /* -t option (see comment above) */
266 DEBUG(5) debugf("hdr->value = %s\n",
267 hdr->value);
268 if (hdr->value) {
269 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
270 }
271 }
272 if (hdr->id == HEAD_BCC) {
273 DEBUG(3) debugf("removing 'Bcc' header\n");
274 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
275 g_list_free_1(hdr_node);
276 destroy_header(hdr);
277 }
278 break;
279 case HEAD_ENVELOPE_TO:
280 if (flags & ACC_SAVE_ENVELOPE_TO) {
281 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
282 msg->hdr_list = g_list_prepend(msg->hdr_list,
283 create_header(HEAD_UNKNOWN,
284 "X-Orig-Envelope-To: %s",
285 hdr->value));
286 }
287 DEBUG(3) debugf("removing 'Envelope-To' header\n");
288 msg->hdr_list = g_list_remove_link(msg->hdr_list,
289 hdr_node);
290 g_list_free_1(hdr_node);
291 destroy_header(hdr);
292 break;
293 case HEAD_RETURN_PATH:
294 if (flags & ACC_MAIL_FROM_HEAD) {
295 /* usually POP3 accept */
296 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
297 DEBUG(3) debugf("setting return_path to %s\n",
298 addr_string(msg->return_path));
299 }
300 DEBUG(3) debugf("removing 'Return-Path' header\n");
301 msg->hdr_list = g_list_remove_link(msg->hdr_list,
302 hdr_node);
303 g_list_free_1(hdr_node);
304 destroy_header(hdr);
305 break;
306 default:
307 break; /* make compiler happy */
308 }
309 }
311 /*
312 ** TODO: do we still need this as we don't fetch
313 ** mail anymore?
314 ** This can happen for pop3 accept only and if no
315 ** Return-Path: header was given
316 */
317 ensure_return_path(msg);
319 /* here we should have our recipients, fail if not: */
320 if (!msg->rcpt_list) {
321 logwrite(LOG_WARNING, "no recipients found in message\n");
322 return AERR_NORCPT;
323 }
325 if (!has_sender && !has_from) {
326 DEBUG(3) debugf("adding 'From:' header\n");
327 if (msg->full_sender_name) {
328 msg->hdr_list = g_list_append(msg->hdr_list,
329 create_header(HEAD_FROM,
330 "From: \"%s\" <%s@%s>\n",
331 msg->full_sender_name,
332 msg->return_path->local_part,
333 msg->return_path->domain));
334 } else {
335 msg->hdr_list = g_list_append(msg->hdr_list,
336 create_header(HEAD_FROM,
337 "From: <%s@%s>\n",
338 msg->return_path->local_part,
339 msg->return_path->domain));
340 }
341 }
342 if (!has_to_or_cc) {
343 DEBUG(3) debugf("no To: or Cc: header, hence adding "
344 "`To: undisclosed recipients:;'\n");
345 msg->hdr_list = g_list_append(msg->hdr_list,
346 create_header(HEAD_TO,
347 "To: undisclosed-recipients:;\n"));
348 }
349 if (!has_date) {
350 DEBUG(3) debugf("adding 'Date:' header\n");
351 msg->hdr_list = g_list_append(msg->hdr_list,
352 create_header(HEAD_DATE, "Date: %s\n",
353 rec_timestamp()));
354 }
355 if (!has_id) {
356 DEBUG(3) debugf("adding 'Message-ID:' header\n");
357 msg->hdr_list = g_list_append(msg->hdr_list,
358 create_header(HEAD_MESSAGE_ID,
359 "Message-ID: <%s@%s>\n",
360 msg->uid, conf.host_name));
361 }
363 return AERR_OK;
364 }
366 static void
367 add_received_hdr(message *msg)
368 {
369 gchar *for_string = NULL;
370 header *hdr = NULL;
371 address *addr;
373 DEBUG(3) debugf("adding 'Received:' header\n");
374 if (g_list_length(msg->rcpt_list) == 1) {
375 /* The `for' part only if exactly one rcpt is present */
376 addr = (address *) (g_list_first(msg->rcpt_list)->data);
377 for_string = g_strdup_printf("\n\tfor %s", addr_string(addr));
378 }
379 if (!msg->received_host) {
380 /* received locally */
381 hdr = create_header(HEAD_RECEIVED,
382 "Received: by %s (%s %s, from userid %d)\n"
383 "\tid %s%s; %s\n",
384 conf.host_name, PACKAGE, VERSION, geteuid(),
385 msg->uid,
386 for_string ? for_string : "", rec_timestamp());
387 } else {
388 /* received from remote */
389 DEBUG(5) debugf("adding 'Received:' header (5)\n");
390 hdr = create_header(HEAD_RECEIVED,
391 "Received: from %s\n"
392 "\tby %s with %s (%s %s)\n"
393 "\tid %s%s; %s\n",
394 msg->received_host, conf.host_name,
395 prot_names[msg->received_prot], PACKAGE,
396 VERSION, msg->uid,
397 for_string ? for_string : "", rec_timestamp());
398 }
399 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
400 if (for_string) {
401 g_free(for_string);
402 }
403 }
405 accept_error
406 accept_message_prepare(message *msg, guint flags)
407 {
408 DEBUG(5) debugf("accept_message_prepare()\n");
410 /* generate unique message id */
411 msg->uid = g_malloc(14);
412 string_base62(msg->uid, time(NULL), 6);
413 msg->uid[6] = '-';
414 string_base62(msg->uid + 7, getpid(), 3);
415 msg->uid[10] = '-';
416 string_base62(msg->uid + 11, msg->transfer_id, 2);
417 msg->uid[13] = '\0';
419 /* if local, get password entry and set return path if missing */
420 if (!msg->received_host) {
421 struct passwd *passwd = NULL;
423 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
424 msg->ident = g_strdup(passwd->pw_name);
425 if (!msg->return_path) {
426 gchar *path = g_strdup_printf("<%s@%s>",
427 passwd->pw_name, conf.host_name);
428 DEBUG(3) debugf("setting return_path for local "
429 "accept: %s\n", path);
430 msg->return_path = create_address(path, TRUE);
431 g_free(path);
432 }
433 }
435 if (scan_headers(msg, flags) == AERR_NORCPT) {
436 return AERR_NORCPT;
437 }
439 /* after the hdrs are scanned because we need to know the rcpts */
440 add_received_hdr(msg);
442 return AERR_OK;
443 }
445 accept_error
446 accept_message(FILE *in, message *msg, guint flags)
447 {
448 accept_error err;
450 err = accept_message_stream(in, msg, flags);
451 if (err == AERR_OK) {
452 err = accept_message_prepare(msg, flags);
453 }
455 return err;
456 }