masqmail

view src/accept.c @ 268:7b12d081b939

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