masqmail
view src/accept.c @ 271:899175e8dff0
heavy refactoring in the small of parse.c
I really hope I didn't change any behavior.
This reminds me that we really need a test framework.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Fri, 03 Dec 2010 13:05:59 -0300 |
parents | fd1d77e5a5da |
children | 1405012509e3 |
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include "masqmail.h"
21 #include "readsock.h"
23 gchar *prot_names[] = {
24 "local",
25 "bsmtp",
26 "smtp",
27 "esmtp",
28 "(unknown)" /* should not happen, but better than crashing. */
29 };
31 static gchar*
32 string_base62(gchar * res, guint value, gchar len)
33 {
34 static gchar base62_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
35 gchar *p = res + len;
36 *p = '\0';
37 while (p > res) {
38 *(--p) = base62_chars[value % 62];
39 value /= 62;
40 }
41 return res;
42 }
44 static gint
45 _g_list_addr_isequal(gconstpointer a, gconstpointer b)
46 {
47 address *addr1 = (address *) a;
48 address *addr2 = (address *) b;
49 int ret;
51 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0) {
52 return strcmp(addr1->local_part, addr2->local_part);
53 }
54 return ret;
55 }
57 /* accept message from anywhere.
58 A locally originating message is indicated by msg->recieved_host == NULL
60 The -t option:
61 The ACC_RCPT_FROM_HEAD flag adds the recipients found in To/Cc/Bcc
62 headers to the recipients list. The recipients given on the
63 command line are removed from the ones given in headers.
64 */
66 accept_error
67 accept_message_stream(FILE * in, message * msg, guint flags)
68 {
69 gchar *line, *line1;
70 int line_size = MAX_DATALINE;
71 gboolean in_headers = TRUE;
72 header *hdr = NULL;
73 gint line_cnt = 0, data_size = 0;
75 line = g_malloc(line_size);
76 line[0] = '\0';
78 while (TRUE) {
79 int len = read_sockline1(in, &line, &line_size, 5 * 60, READSOCKL_CVT_CRLF);
81 line1 = line;
83 if ((line[0] == '.') && (!(flags & ACC_DOT_IGNORE))) {
84 if (line[1] == '\n') {
85 g_free(line);
86 break;
87 }
88 line1++;
89 }
91 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
92 /* we got an EOF, and the last line was not terminated by a CR */
93 gint len1 = strlen(line1);
94 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
95 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
96 line1[len1] = '\n';
97 line1[len1 + 1] = '\0';
98 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
99 data_size += strlen(line1);
100 line_cnt++;
101 }
102 }
103 break;
105 } else if (len == -1) {
106 g_free(line);
107 return AERR_EOF;
109 } else if (len == -2) {
110 /* should not happen any more */
111 g_free(line);
112 return AERR_OVERFLOW;
114 } else if (len == -3) {
115 g_free(line);
116 return AERR_TIMEOUT;
118 } else if (len <= 0) {
119 /* does not happen */
120 g_free(line);
121 DEBUG(5) debugf("read_sockline returned %d\n", len);
122 return AERR_UNKNOWN;
124 }
126 if (in_headers) {
128 /* some pop servers send the 'From ' line, skip it: */
129 if (!msg->hdr_list && strncmp(line1, "From ", 5) == 0) {
130 continue;
131 }
133 if (line1[0] == ' ' || line1[0] == '\t') {
134 /* continuation of 'folded' header: */
135 if (hdr) {
136 hdr->header = g_strconcat(hdr->header, line1, NULL);
137 }
139 } else if (line1[0] == '\n') {
140 /* an empty line marks end of headers */
141 in_headers = FALSE;
142 } else {
143 /* in all other cases we expect another header */
144 if ((hdr = get_header(line1))) {
145 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
146 } else {
147 /* if get_header() returns NULL, no header was recognized,
148 so this seems to be the first data line of a broken mailer
149 which does not send an empty line after the headers */
150 in_headers = FALSE;
151 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
152 }
153 }
154 } else {
155 /* message body */
156 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
157 data_size += strlen(line1);
158 line_cnt++;
159 }
161 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
162 DEBUG(4) debugf("accept_message_stream(): "
163 "received %d bytes (conf.max_msg_size=%d)\n",
164 data_size, conf.max_msg_size);
165 return AERR_SIZE;
166 }
168 }
170 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
172 if (!msg->data_list) {
173 /* make sure data list is not NULL: */
174 msg->data_list = g_list_append(NULL, g_strdup(""));
175 }
176 msg->data_list = g_list_reverse(msg->data_list);
178 /* we get here after we succesfully received the mail data */
180 msg->data_size = data_size;
181 msg->received_time = time(NULL);
183 return AERR_OK;
184 }
186 accept_error
187 accept_message_prepare(message * msg, guint flags)
188 {
189 struct passwd *passwd = NULL;
190 GList *non_rcpt_list = NULL;
191 time_t rec_time = time(NULL);
193 DEBUG(5) debugf("accept_message_prepare()\n");
195 /* create unique message id */
196 msg->uid = g_malloc(14);
198 string_base62(msg->uid, rec_time, 6);
199 msg->uid[6] = '-';
200 string_base62(&(msg->uid[7]), getpid(), 3);
201 msg->uid[10] = '-';
202 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
203 msg->uid[13] = 0;
205 /* if local, get password entry */
206 if (msg->received_host == NULL) {
207 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
208 msg->ident = g_strdup(passwd->pw_name);
209 }
211 /* set return path if local */
212 if (msg->return_path == NULL && msg->received_host == NULL) {
213 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
214 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
215 msg->return_path = create_address(path, TRUE);
216 g_free(path);
217 }
219 /* -t option (see comment above) */
220 if (flags & ACC_RCPT_FROM_HEAD) {
221 non_rcpt_list = msg->rcpt_list;
222 msg->rcpt_list = NULL;
223 }
225 /* scan headers */
226 {
227 gboolean has_id = FALSE;
228 gboolean has_date = FALSE;
229 gboolean has_sender = FALSE;
230 gboolean has_from = FALSE;
231 gboolean has_to_or_cc = FALSE;
232 GList *hdr_node, *hdr_node_next;
233 header *hdr;
235 for (hdr_node = g_list_first(msg->hdr_list);
236 hdr_node != NULL; hdr_node = hdr_node_next) {
237 hdr_node_next = g_list_next(hdr_node);
238 hdr = ((header *) (hdr_node->data));
239 DEBUG(5) debugf("scanning headers: %s", hdr->header);
240 switch (hdr->id) {
241 case HEAD_MESSAGE_ID:
242 has_id = TRUE;
243 break;
244 case HEAD_DATE:
245 has_date = TRUE;
246 break;
247 case HEAD_FROM:
248 has_from = TRUE;
249 break;
250 case HEAD_SENDER:
251 has_sender = TRUE;
252 break;
253 case HEAD_TO:
254 case HEAD_CC:
255 has_to_or_cc = TRUE;
256 /* fall through */
257 case HEAD_BCC:
258 if (flags & ACC_RCPT_FROM_HEAD) {
259 /* -t option (see comment above) */
260 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
261 /* FIXME: `To: alice, bob' makes problems */
262 if (hdr->value) {
263 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
264 }
265 }
266 if (hdr->id == HEAD_BCC) {
267 DEBUG(3) debugf("removing 'Bcc' header\n");
268 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
269 g_list_free_1(hdr_node);
270 destroy_header(hdr);
271 }
272 break;
273 case HEAD_ENVELOPE_TO:
274 if (flags & ACC_SAVE_ENVELOPE_TO) {
275 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
276 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
277 "X-Orig-Envelope-to: %s", hdr->value));
278 }
279 DEBUG(3) debugf("removing 'Envelope-To' header\n");
280 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
281 g_list_free_1(hdr_node);
282 destroy_header(hdr);
283 break;
284 case HEAD_RETURN_PATH:
285 if (flags & ACC_MAIL_FROM_HEAD) {
286 /* usually POP3 accept */
287 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
288 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
289 }
290 DEBUG(3) debugf("removing 'Return-Path' header\n");
291 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
292 g_list_free_1(hdr_node);
293 destroy_header(hdr);
294 break;
295 default:
296 break; /* make compiler happy */
297 }
298 }
300 if (msg->return_path == NULL) {
301 /* this can happen for pop3 accept only and if no Return-path: header was given */
302 GList *hdr_list;
303 header *hdr;
305 DEBUG(3) debugf("return_path == NULL\n");
307 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
308 if (!hdr_list)
309 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
310 if (hdr_list) {
311 gchar *addr;
312 hdr = (header *) (g_list_first(hdr_list)->data);
314 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
316 addr = g_strdup(hdr->value);
317 g_strchomp(addr);
319 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
320 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
321 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
322 "X-Warning: return path set from %s address\n",
323 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
324 }
325 g_free(addr);
326 }
327 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
328 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
329 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
330 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
331 "X-Warning: real return path is unknown\n"));
332 }
333 }
335 if (flags & ACC_RCPT_FROM_HEAD) {
336 /* remove the recipients given on the command line
337 from the ones given in headers
338 -t option (see comment above) */
339 GList *rcpt_node;
340 foreach(non_rcpt_list, rcpt_node) {
341 address *rcpt = (address *) (rcpt_node->data);
342 GList *node;
343 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
344 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
345 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
346 destroy_address((address *) (node->data));
347 g_list_free_1(node);
348 }
349 }
350 }
352 /* here we should have our recipients, fail if not: */
353 if (msg->rcpt_list == NULL) {
354 logwrite(LOG_WARNING, "no recipients found in message\n");
355 return AERR_NORCPT;
356 }
358 if (!(has_sender || has_from)) {
359 DEBUG(3) debugf("adding 'From' header\n");
360 msg->hdr_list = g_list_append(msg->hdr_list,
361 msg->full_sender_name
362 ?
363 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
364 msg->return_path->local_part, msg->return_path->domain)
365 :
366 create_header(HEAD_FROM, "From: <%s@%s>\n",
367 msg->return_path->local_part, msg->return_path->domain)
368 );
369 }
370 if (!has_to_or_cc) {
371 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
372 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
373 }
374 if (!has_date) {
375 DEBUG(3) debugf("adding 'Date:' header\n");
376 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
377 }
378 if (!has_id) {
379 DEBUG(3) debugf("adding 'Message-ID:' header\n");
380 msg->hdr_list = g_list_append(msg->hdr_list,
381 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
382 }
383 }
385 /* Received header: */
386 /* At this point because we have to know the rcpts for the 'for' part */
387 gchar *for_string = NULL;
388 header *hdr = NULL;
390 DEBUG(3) debugf("adding 'Received:' header\n");
392 if (g_list_length(msg->rcpt_list) == 1) {
393 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
394 for_string = g_strdup_printf(" for %s", addr_string(addr));
395 }
397 if (msg->received_host == NULL) {
398 /* received locally */
399 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
400 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
401 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
402 } else {
403 /* received from remote */
404 #ifdef ENABLE_IDENT
405 DEBUG(5) debugf("adding 'Received:' header (5)\n");
406 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
407 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
408 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
409 rec_timestamp());
410 #else
411 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
412 msg->received_host, conf.host_name, prot_names[msg->received_prot],
413 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
414 #endif
415 }
416 header_fold(hdr);
417 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
419 if (for_string)
420 g_free(for_string);
422 return AERR_OK;
423 }
425 accept_error
426 accept_message(FILE * in, message * msg, guint flags)
427 {
428 accept_error err;
430 err = accept_message_stream(in, msg, flags);
431 if (err == AERR_OK) {
432 err = accept_message_prepare(msg, flags);
433 }
435 return err;
436 }