masqmail

view src/accept.c @ 269:fd1d77e5a5da

comment: problems with `To: alice, bob' and -t
author markus schnalke <meillo@marmaro.de>
date Fri, 03 Dec 2010 12:55:44 -0300
parents 7b12d081b939
children 0c44b239c7fe
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 else
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 <= 0) {
92 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
93 /* we got an EOF, and the last line was not terminated by a CR */
94 gint len1 = strlen(line1);
95 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
96 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
97 line1[len1] = '\n';
98 line1[len1 + 1] = '\0';
99 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
100 data_size += strlen(line1);
101 line_cnt++;
102 }
103 }
104 break;
105 } else {
106 g_free(line);
107 if (len == -1) {
108 return AERR_EOF;
109 } else if (len == -2) {
110 /* should not happen any more */
111 return AERR_OVERFLOW;
112 } else if (len == -3) {
113 return AERR_TIMEOUT;
114 } else {
115 /* does not happen */
116 DEBUG(5) debugf("read_sockline returned %d\n", len);
117 return AERR_UNKNOWN;
118 }
119 }
120 } else {
121 if (in_headers) {
123 /* some pop servers send the 'From ' line, skip it: */
124 if (msg->hdr_list == NULL)
125 if (strncmp(line1, "From ", 5) == 0)
126 continue;
128 if (line1[0] == ' ' || line1[0] == '\t') {
129 /* continuation of 'folded' header: */
130 if (hdr) {
131 hdr->header = g_strconcat(hdr->header, line1, NULL);
132 }
134 } else if (line1[0] == '\n') {
135 /* an empty line marks end of headers */
136 in_headers = FALSE;
137 } else {
138 /* in all other cases we expect another header */
139 if ((hdr = get_header(line1)))
140 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
141 else {
142 /* if get_header() returns NULL, no header was recognized,
143 so this seems to be the first data line of a broken mailer
144 which does not send an empty line after the headers */
145 in_headers = FALSE;
146 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
147 }
148 }
149 } else {
150 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
151 data_size += strlen(line1);
152 line_cnt++;
153 }
154 }
155 if (conf.max_msg_size && (data_size > conf.max_msg_size)) {
156 DEBUG(4) debugf("accept_message_stream(): "
157 "received %d bytes (conf.max_msg_size=%d)\n",
158 data_size, conf.max_msg_size);
159 return AERR_SIZE;
160 }
162 }
164 if (msg->data_list != NULL)
165 msg->data_list = g_list_reverse(msg->data_list);
166 else
167 /* make sure data list is not NULL: */
168 msg->data_list = g_list_append(NULL, g_strdup(""));
170 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
171 /* we get here after we succesfully received the mail data */
173 msg->data_size = data_size;
174 msg->received_time = time(NULL);
176 return AERR_OK;
177 }
179 accept_error
180 accept_message_prepare(message * msg, guint flags)
181 {
182 struct passwd *passwd = NULL;
183 GList *non_rcpt_list = NULL;
184 time_t rec_time = time(NULL);
186 DEBUG(5) debugf("accept_message_prepare()\n");
188 /* create unique message id */
189 msg->uid = g_malloc(14);
191 string_base62(msg->uid, rec_time, 6);
192 msg->uid[6] = '-';
193 string_base62(&(msg->uid[7]), getpid(), 3);
194 msg->uid[10] = '-';
195 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
196 msg->uid[13] = 0;
198 /* if local, get password entry */
199 if (msg->received_host == NULL) {
200 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
201 msg->ident = g_strdup(passwd->pw_name);
202 }
204 /* set return path if local */
205 if (msg->return_path == NULL && msg->received_host == NULL) {
206 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
207 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
208 msg->return_path = create_address(path, TRUE);
209 g_free(path);
210 }
212 /* -t option (see comment above) */
213 if (flags & ACC_RCPT_FROM_HEAD) {
214 non_rcpt_list = msg->rcpt_list;
215 msg->rcpt_list = NULL;
216 }
218 /* scan headers */
219 {
220 gboolean has_id = FALSE;
221 gboolean has_date = FALSE;
222 gboolean has_sender = FALSE;
223 gboolean has_from = FALSE;
224 gboolean has_to_or_cc = FALSE;
225 GList *hdr_node, *hdr_node_next;
226 header *hdr;
228 for (hdr_node = g_list_first(msg->hdr_list);
229 hdr_node != NULL; hdr_node = hdr_node_next) {
230 hdr_node_next = g_list_next(hdr_node);
231 hdr = ((header *) (hdr_node->data));
232 DEBUG(5) debugf("scanning headers: %s", hdr->header);
233 switch (hdr->id) {
234 case HEAD_MESSAGE_ID:
235 has_id = TRUE;
236 break;
237 case HEAD_DATE:
238 has_date = TRUE;
239 break;
240 case HEAD_FROM:
241 has_from = TRUE;
242 break;
243 case HEAD_SENDER:
244 has_sender = TRUE;
245 break;
246 case HEAD_TO:
247 case HEAD_CC:
248 has_to_or_cc = TRUE;
249 /* fall through */
250 case HEAD_BCC:
251 if (flags & ACC_RCPT_FROM_HEAD) {
252 /* -t option (see comment above) */
253 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
254 /* FIXME: `To: alice, bob' makes problems */
255 if (hdr->value) {
256 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
257 }
258 }
259 if (hdr->id == HEAD_BCC) {
260 DEBUG(3) debugf("removing 'Bcc' header\n");
261 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
262 g_list_free_1(hdr_node);
263 destroy_header(hdr);
264 }
265 break;
266 case HEAD_ENVELOPE_TO:
267 if (flags & ACC_SAVE_ENVELOPE_TO) {
268 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
269 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
270 "X-Orig-Envelope-to: %s", hdr->value));
271 }
272 DEBUG(3) debugf("removing 'Envelope-To' header\n");
273 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
274 g_list_free_1(hdr_node);
275 destroy_header(hdr);
276 break;
277 case HEAD_RETURN_PATH:
278 if (flags & ACC_MAIL_FROM_HEAD) {
279 /* usually POP3 accept */
280 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
281 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
282 }
283 DEBUG(3) debugf("removing 'Return-Path' header\n");
284 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
285 g_list_free_1(hdr_node);
286 destroy_header(hdr);
287 break;
288 default:
289 break; /* make compiler happy */
290 }
291 }
293 if (msg->return_path == NULL) {
294 /* this can happen for pop3 accept only and if no Return-path: header was given */
295 GList *hdr_list;
296 header *hdr;
298 DEBUG(3) debugf("return_path == NULL\n");
300 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
301 if (!hdr_list)
302 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
303 if (hdr_list) {
304 gchar *addr;
305 hdr = (header *) (g_list_first(hdr_list)->data);
307 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
309 addr = g_strdup(hdr->value);
310 g_strchomp(addr);
312 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
313 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
314 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
315 "X-Warning: return path set from %s address\n",
316 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
317 }
318 g_free(addr);
319 }
320 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
321 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
322 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
323 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
324 "X-Warning: real return path is unknown\n"));
325 }
326 }
328 if (flags & ACC_RCPT_FROM_HEAD) {
329 /* remove the recipients given on the command line
330 from the ones given in headers
331 -t option (see comment above) */
332 GList *rcpt_node;
333 foreach(non_rcpt_list, rcpt_node) {
334 address *rcpt = (address *) (rcpt_node->data);
335 GList *node;
336 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
337 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
338 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
339 destroy_address((address *) (node->data));
340 g_list_free_1(node);
341 }
342 }
343 }
345 /* here we should have our recipients, fail if not: */
346 if (msg->rcpt_list == NULL) {
347 logwrite(LOG_WARNING, "no recipients found in message\n");
348 return AERR_NORCPT;
349 }
351 if (!(has_sender || has_from)) {
352 DEBUG(3) debugf("adding 'From' header\n");
353 msg->hdr_list = g_list_append(msg->hdr_list,
354 msg->full_sender_name
355 ?
356 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
357 msg->return_path->local_part, msg->return_path->domain)
358 :
359 create_header(HEAD_FROM, "From: <%s@%s>\n",
360 msg->return_path->local_part, msg->return_path->domain)
361 );
362 }
363 if (!has_to_or_cc) {
364 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
365 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
366 }
367 if (!has_date) {
368 DEBUG(3) debugf("adding 'Date:' header\n");
369 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
370 }
371 if (!has_id) {
372 DEBUG(3) debugf("adding 'Message-ID:' header\n");
373 msg->hdr_list = g_list_append(msg->hdr_list,
374 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
375 }
376 }
378 /* Received header: */
379 /* At this point because we have to know the rcpts for the 'for' part */
380 gchar *for_string = NULL;
381 header *hdr = NULL;
383 DEBUG(3) debugf("adding 'Received:' header\n");
385 if (g_list_length(msg->rcpt_list) == 1) {
386 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
387 for_string = g_strdup_printf(" for %s", addr_string(addr));
388 }
390 if (msg->received_host == NULL) {
391 /* received locally */
392 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
393 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
394 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
395 } else {
396 /* received from remote */
397 #ifdef ENABLE_IDENT
398 DEBUG(5) debugf("adding 'Received:' header (5)\n");
399 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
400 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
401 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
402 rec_timestamp());
403 #else
404 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
405 msg->received_host, conf.host_name, prot_names[msg->received_prot],
406 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
407 #endif
408 }
409 header_fold(hdr);
410 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
412 if (for_string)
413 g_free(for_string);
415 return AERR_OK;
416 }
418 accept_error
419 accept_message(FILE * in, message * msg, guint flags)
420 {
421 accept_error err;
423 err = accept_message_stream(in, msg, flags);
424 if (err == AERR_OK)
425 err = accept_message_prepare(msg, flags);
427 return err;
428 }