masqmail

view src/accept.c @ 113:c93023f58cc7

Paolo's code and vanilla code differs what is the correct way? Someone should check. ;-)
author meillo@marmaro.de
date Wed, 30 Jun 2010 13:23:49 +0200
parents c678d0342451
children 5ec5e6637049
line source
1 /* MasqMail
2 Copyright (C) 1999-2001 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
20 #include "readsock.h"
22 gchar *prot_names[] = {
23 "local",
24 "bsmtp",
25 "smtp",
26 "esmtp",
27 "pop3",
28 "apop",
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[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
36 gchar *p = res + len;
37 *p = '\0';
38 while (p > res) {
39 *(--p) = base62_chars[value % 62];
40 value /= 62;
41 }
42 return res;
43 }
45 static gint
46 _g_list_addr_isequal(gconstpointer a, gconstpointer b)
47 {
48 address *addr1 = (address *) a;
49 address *addr2 = (address *) b;
50 int ret;
52 if ((ret = strcasecmp(addr1->domain, addr2->domain)) == 0)
53 return strcmp(addr1->local_part, addr2->local_part);
54 else
55 return ret;
56 }
58 /* accept message from anywhere.
59 A locally originating message is indicated by msg->recieved_host == NULL
61 The -t option:
62 The ACC_RCPT_FROM_HEAD flag adds the recipients found in To/Cc/Bcc
63 headers to the recipients list.
64 The ACC_DEL_RCPTS flag makes only sense if the ACC_RCPT_FROM_HEAD
65 flag is given too. With ACC_DEL_RCPTS the recipients given on the
66 command line are removed from the ones given in headers.
67 */
69 accept_error
70 accept_message_stream(FILE * in, message * msg, guint flags)
71 {
72 gchar *line, *line1;
73 int line_size = MAX_DATALINE;
74 gboolean in_headers = TRUE;
75 header *hdr = NULL;
76 gint line_cnt = 0, data_size = 0;
78 line = g_malloc(line_size);
79 line[0] = '\0';
81 while (TRUE) {
82 int len = read_sockline1(in, &line, &line_size, 5 * 60, READSOCKL_CVT_CRLF);
84 line1 = line;
86 if ((line[0] == '.') && (!(flags & ACC_DOT_IGNORE))) {
87 if (line[1] == '\n') {
88 g_free(line);
89 break;
90 }
91 line1++;
92 }
94 if (len <= 0) {
95 if ((len == -1) && (flags & (ACC_DOT_IGNORE | ACC_NODOT_RELAX))) {
96 /* we got an EOF, and the last line was not terminated by a CR */
97 gint len1 = strlen(line1);
98 if (len1 > 0) { /* == 0 is 'normal' (EOF after a CR) */
99 if (line1[len1 - 1] != '\n') { /* some mail clients allow unterminated lines */
100 line1[len1] = '\n';
101 line1[len1 + 1] = '\0';
102 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
103 data_size += strlen(line1);
104 line_cnt++;
105 }
106 }
107 break;
108 } else {
109 g_free(line);
110 if (len == -1) {
111 return AERR_EOF;
112 } else if (len == -2) {
113 /* should not happen any more */
114 return AERR_OVERFLOW;
115 } else if (len == -3) {
116 return AERR_TIMEOUT;
117 } else {
118 /* does not happen */
119 DEBUG(5) debugf("read_sockline returned %d\n", len);
120 return AERR_UNKNOWN;
121 }
122 }
123 } else {
124 if (in_headers) {
126 /* some pop servers send the 'From ' line, skip it: */
127 if (msg->hdr_list == NULL)
128 if (strncmp(line1, "From ", 5) == 0)
129 continue;
131 if (line1[0] == ' ' || line1[0] == '\t') {
132 /* continuation of 'folded' header: */
133 if (hdr) {
134 hdr->header = g_strconcat(hdr->header, line1, NULL);
135 }
137 } else if (line1[0] == '\n') {
138 /* an empty line marks end of headers */
139 in_headers = FALSE;
140 } else {
141 /* in all other cases we expect another header */
142 if ((hdr = get_header(line1)))
143 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
144 else {
145 /* if get_header() returns NULL, no header was recognized,
146 so this seems to be the first data line of a broken mailer
147 which does not send an empty line after the headers */
148 in_headers = FALSE;
149 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
150 }
151 }
152 } else {
153 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
154 data_size += strlen(line1);
155 line_cnt++;
156 }
157 }
158 }
160 if (msg->data_list != NULL)
161 msg->data_list = g_list_reverse(msg->data_list);
162 else
163 /* make sure data list is not NULL: */
164 msg->data_list = g_list_append(NULL, g_strdup(""));
166 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
167 /* we get here after we succesfully received the mail data */
169 msg->data_size = data_size;
170 msg->received_time = time(NULL);
172 return AERR_OK;
173 }
175 accept_error
176 accept_message_prepare(message * msg, guint flags)
177 {
178 struct passwd *passwd = NULL;
179 GList *non_rcpt_list = NULL;
180 time_t rec_time = time(NULL);
182 DEBUG(5) debugf("accept_message_prepare()\n");
184 /* create unique message id */
185 msg->uid = g_malloc(14);
187 string_base62(msg->uid, rec_time, 6);
188 msg->uid[6] = '-';
189 string_base62(&(msg->uid[7]), getpid(), 3);
190 msg->uid[10] = '-';
191 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
192 msg->uid[13] = 0;
194 /* if local, get password entry */
195 if (msg->received_host == NULL) {
196 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
197 msg->ident = g_strdup(passwd->pw_name);
198 }
200 /* set return path if local */
201 if (msg->return_path == NULL && msg->received_host == NULL) {
202 gchar *path = g_strdup_printf("<%s@%s>", passwd->pw_name, conf.host_name);
203 DEBUG(3) debugf("setting return_path for local accept: %s\n", path);
204 msg->return_path = create_address(path, TRUE);
205 g_free(path);
206 }
208 /* -t option (see comment above) */
209 if (flags & ACC_DEL_RCPTS) {
210 non_rcpt_list = msg->rcpt_list;
211 msg->rcpt_list = NULL;
212 }
214 /* scan headers */
215 {
216 gboolean has_id = FALSE;
217 gboolean has_date = FALSE;
218 gboolean has_sender = FALSE;
219 gboolean has_from = FALSE;
220 gboolean has_to_or_cc = FALSE;
221 GList *hdr_node, *hdr_node_next;
222 header *hdr;
224 for (hdr_node = g_list_first(msg->hdr_list);
225 hdr_node != NULL; hdr_node = hdr_node_next) {
226 hdr_node_next = g_list_next(hdr_node);
227 hdr = ((header *) (hdr_node->data));
228 DEBUG(5) debugf("scanning headers: %s", hdr->header);
229 switch (hdr->id) {
230 case HEAD_MESSAGE_ID:
231 has_id = TRUE;
232 break;
233 case HEAD_DATE:
234 has_date = TRUE;
235 break;
236 case HEAD_FROM:
237 has_from = TRUE;
238 break;
239 case HEAD_SENDER:
240 has_sender = TRUE;
241 break;
242 case HEAD_TO:
243 case HEAD_CC:
244 has_to_or_cc = TRUE;
245 /* fall through */
246 case HEAD_BCC:
247 if (flags & ACC_RCPT_FROM_HEAD) {
248 /* -t option (see comment above) */
249 DEBUG(5) debugf("hdr->value = %s\n", hdr->value);
250 if (hdr->value) {
251 msg->rcpt_list = addr_list_append_rfc822(msg->rcpt_list, hdr->value, conf.host_name);
252 }
253 }
254 if (hdr->id == HEAD_BCC) {
255 DEBUG(3) debugf("removing 'Bcc' header\n");
256 msg->hdr_list = g_list_remove_link(msg->hdr_list, hdr_node);
257 g_list_free_1(hdr_node);
258 destroy_header(hdr);
259 }
260 break;
261 case HEAD_ENVELOPE_TO:
262 if (flags & ACC_SAVE_ENVELOPE_TO) {
263 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
264 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN,
265 "X-Orig-Envelope-to: %s", hdr->value));
266 }
267 DEBUG(3) debugf("removing 'Envelope-To' 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 break;
272 case HEAD_RETURN_PATH:
273 if (flags & ACC_MAIL_FROM_HEAD) {
274 /* usually POP3 accept */
275 msg->return_path = create_address_qualified(hdr->value, TRUE, msg->received_host);
276 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
277 }
278 DEBUG(3) debugf("removing 'Return-Path' 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 default:
284 break; /* make compiler happy */
285 }
286 }
288 if (msg->return_path == NULL) {
289 /* this can happen for pop3 accept only and if no Return-path: header was given */
290 GList *hdr_list;
291 header *hdr;
293 DEBUG(3) debugf("return_path == NULL\n");
295 hdr_list = find_header(msg->hdr_list, HEAD_SENDER, NULL);
296 if (!hdr_list)
297 hdr_list = find_header(msg->hdr_list, HEAD_FROM, NULL);
298 if (hdr_list) {
299 gchar *addr;
300 hdr = (header *) (g_list_first(hdr_list)->data);
302 DEBUG(5) debugf("hdr->value = '%s'\n", hdr->value);
304 addr = g_strdup(hdr->value);
305 g_strchomp(addr);
307 if ((msg->return_path = create_address_qualified(addr, FALSE, msg->received_host)) != NULL) {
308 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
309 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
310 "X-Warning: return path set from %s address\n",
311 hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
312 }
313 g_free(addr);
314 }
315 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
316 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
317 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
318 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN,
319 "X-Warning: real return path is unknown\n"));
320 }
321 }
323 if (flags & ACC_DEL_RCPTS) {
324 /* remove the recipients given on the command line from the ones given in headers
325 -t option (see comment above) */
326 GList *rcpt_node;
327 foreach(non_rcpt_list, rcpt_node) {
328 address *rcpt = (address *) (rcpt_node->data);
329 GList *node;
330 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
331 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
332 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
333 destroy_address((address *) (node->data));
334 g_list_free_1(node);
335 }
336 }
337 }
339 /* here we should have our recipients, fail if not: */
340 if (msg->rcpt_list == NULL) {
341 logwrite(LOG_WARNING, "no recipients found in message\n");
342 return AERR_NORCPT;
343 }
345 if (!(has_sender || has_from)) {
346 DEBUG(3) debugf("adding 'From' header\n");
347 msg->hdr_list = g_list_append(msg->hdr_list,
348 msg->full_sender_name
349 ?
350 create_header(HEAD_FROM, "From: \"%s\" <%s@%s>\n", msg->full_sender_name,
351 msg->return_path->local_part, msg->return_path->domain)
352 :
353 create_header(HEAD_FROM, "From: <%s@%s>\n",
354 msg->return_path->local_part, msg->return_path->domain)
355 );
356 }
357 if (!has_to_or_cc) {
358 DEBUG(3) debugf("no To: or Cc: header, hence adding `To: undisclosed recipients:;'\n");
359 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: undisclosed-recipients:;\n"));
360 }
361 if (!has_date) {
362 DEBUG(3) debugf("adding 'Date:' header\n");
363 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
364 }
365 if (!has_id) {
366 DEBUG(3) debugf("adding 'Message-ID:' header\n");
367 msg->hdr_list = g_list_append(msg->hdr_list,
368 create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
369 }
370 }
372 /* Received header: */
373 /* At this point because we have to know the rcpts for the 'for' part */
374 gchar *for_string = NULL;
375 header *hdr = NULL;
377 DEBUG(3) debugf("adding 'Received:' header\n");
379 if (g_list_length(msg->rcpt_list) == 1) {
380 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
381 for_string = g_strdup_printf(" for %s", addr_string(addr));
382 }
384 if (msg->received_host == NULL) {
385 /* received locally */
386 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
387 passwd->pw_name, conf.host_name, prot_names[msg->received_prot],
388 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
389 } else {
390 /* received from remote */
391 #ifdef ENABLE_IDENT
392 DEBUG(5) debugf("adding 'Received:' header (5)\n");
393 hdr = create_header(HEAD_RECEIVED, "Received: from %s (ident=%s) by %s with %s (%s %s) id %s%s; %s\n",
394 msg->received_host, msg->ident ? msg->ident : "unknown", conf.host_name,
395 prot_names[msg->received_prot], PACKAGE, VERSION, msg->uid, for_string ? for_string : "",
396 rec_timestamp());
397 #else
398 hdr = create_header(HEAD_RECEIVED, "Received: from %s by %s with %s (%s %s) id %s%s; %s\n",
399 msg->received_host, conf.host_name, prot_names[msg->received_prot],
400 PACKAGE, VERSION, msg->uid, for_string ? for_string : "", rec_timestamp());
401 #endif
402 }
403 header_fold(hdr);
404 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
406 if (for_string)
407 g_free(for_string);
409 /* write message to spool: */
410 /* accept is no longer responsible for this
411 if (!spool_write(msg, TRUE))
412 return AERR_NOSPOOL;
413 */
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 }