masqmail

view src/accept.c @ 275:1405012509e3

removed comment, see previous commit
author markus schnalke <meillo@marmaro.de>
date Fri, 03 Dec 2010 19:35:34 -0300
parents 0c44b239c7fe
children 1abc1faeb45d
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 if (hdr->value) {
262 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
263 }
264 }
265 if (hdr->id == HEAD_BCC) {
266 DEBUG(3) debugf("removing 'Bcc' header\n");
267 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
268 g_list_free_1(hdr_node);
269 destroy_header(hdr);
270 }
271 break;
272 case HEAD_ENVELOPE_TO:
273 if (flags & ACC_SAVE_ENVELOPE_TO) {
274 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
275 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
276 "X-Orig-Envelope-to: %s", hdr->value));
277 }
278 DEBUG(3) debugf("removing 'Envelope-To' header\n");
279 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
280 g_list_free_1(hdr_node);
281 destroy_header(hdr);
282 break;
283 case HEAD_RETURN_PATH:
284 if (flags & ACC_MAIL_FROM_HEAD) {
285 /* usually POP3 accept */
286 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
287 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
288 }
289 DEBUG(3) debugf("removing 'Return-Path' header\n");
290 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
291 g_list_free_1(hdr_node);
292 destroy_header(hdr);
293 break;
294 default:
295 break; /* make compiler happy */
296 }
297 }
299 if (msg->return_path == NULL) {
300 /* this can happen for pop3 accept only and if no Return-path: header was given */
301 GList *hdr_list;
302 header *hdr;
304 DEBUG(3) debugf("return_path == NULL\n");
306 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
307 if (!hdr_list)
308 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
309 if (hdr_list) {
310 gchar *addr;
311 hdr = (header *) (g_list_first(hdr_list)->data);
313 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
315 addr = g_strdup(hdr->value);
316 g_strchomp(addr);
318 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
319 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
320 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
321 "X-Warning: return path set from %s address\n",
322 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
323 }
324 g_free(addr);
325 }
326 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
327 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
328 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
329 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
330 "X-Warning: real return path is unknown\n"));
331 }
332 }
334 if (flags & ACC_RCPT_FROM_HEAD) {
335 /* remove the recipients given on the command line
336 from the ones given in headers
337 -t option (see comment above) */
338 GList *rcpt_node;
339 foreach(non_rcpt_list, rcpt_node) {
340 address *rcpt = (address *) (rcpt_node->data);
341 GList *node;
342 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
343 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
344 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
345 destroy_address((address *) (node->data));
346 g_list_free_1(node);
347 }
348 }
349 }
351 /* here we should have our recipients, fail if not: */
352 if (msg->rcpt_list == NULL) {
353 logwrite(LOG_WARNING, "no recipients found in message\n");
354 return AERR_NORCPT;
355 }
357 if (!(has_sender || has_from)) {
358 DEBUG(3) debugf("adding 'From' header\n");
359 msg->hdr_list = g_list_append(msg->hdr_list,
360 msg->full_sender_name
361 ?
362 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
363 msg->return_path->local_part, msg->return_path->domain)
364 :
365 create_header(HEAD_FROM, "From: <%s@%s>\n",
366 msg->return_path->local_part, msg->return_path->domain)
367 );
368 }
369 if (!has_to_or_cc) {
370 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
371 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
372 }
373 if (!has_date) {
374 DEBUG(3) debugf("adding 'Date:' header\n");
375 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
376 }
377 if (!has_id) {
378 DEBUG(3) debugf("adding 'Message-ID:' header\n");
379 msg->hdr_list = g_list_append(msg->hdr_list,
380 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
381 }
382 }
384 /* Received header: */
385 /* At this point because we have to know the rcpts for the 'for' part */
386 gchar *for_string = NULL;
387 header *hdr = NULL;
389 DEBUG(3) debugf("adding 'Received:' header\n");
391 if (g_list_length(msg->rcpt_list) == 1) {
392 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
393 for_string = g_strdup_printf(" for %s", addr_string(addr));
394 }
396 if (msg->received_host == NULL) {
397 /* received locally */
398 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
399 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
400 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
401 } else {
402 /* received from remote */
403 #ifdef ENABLE_IDENT
404 DEBUG(5) debugf("adding 'Received:' header (5)\n");
405 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
406 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
407 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
408 rec_timestamp());
409 #else
410 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
411 msg->received_host, conf.host_name, prot_names[msg->received_prot],
412 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
413 #endif
414 }
415 header_fold(hdr);
416 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
418 if (for_string)
419 g_free(for_string);
421 return AERR_OK;
422 }
424 accept_error
425 accept_message(FILE * in, message * msg, guint flags)
426 {
427 accept_error err;
429 err = accept_message_stream(in, msg, flags);
430 if (err == AERR_OK) {
431 err = accept_message_prepare(msg, flags);
432 }
434 return err;
435 }