masqmail-0.2

view src/accept.c @ 12:9fb7ddbaf129

removed noop-code; some beautifying
author meillo@marmaro.de
date Wed, 29 Oct 2008 16:34:13 +0100
parents 08114f7dcc23
children 49dab67fe461
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 If the flags ACC_DEL_RCPTS is set, recipients in the msg->rcpt_list is
62 copied and items occuring in it will be removed from the newly constructed
63 (from To/Cc/Bcc headers if ACC_RCPT_TO is set) rcpt_list.
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_NODOT_TERM))) {
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_NODOT_TERM) || (flags & 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 =
132 g_strconcat(hdr->header, line1, NULL);
133 }
135 } else if (line1[0] == '\n') {
136 /* an empty line marks end of headers */
137 in_headers = FALSE;
138 } else {
139 /* in all other cases we expect another header */
140 if ((hdr = get_header(line1)))
141 msg->hdr_list = g_list_append(msg->hdr_list, hdr);
142 else {
143 /* if get_header() returns NULL, no header was recognized,
144 so this seems to be the first data line of a broken mailer
145 which does not send an empty line after the headers */
146 in_headers = FALSE;
147 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
148 }
149 }
150 } else {
151 msg->data_list = g_list_prepend(msg->data_list, g_strdup(line1));
152 data_size += strlen(line1);
153 line_cnt++;
154 }
155 }
156 }
158 if (msg->data_list != NULL)
159 msg->data_list = g_list_reverse(msg->data_list);
160 else
161 /* make sure data list is not NULL: */
162 msg->data_list = g_list_append(NULL, g_strdup(""));
164 DEBUG(4) debugf("received %d lines of data (%d bytes)\n", line_cnt, data_size);
165 /* we get here after we succesfully received the mail data */
167 msg->data_size = data_size;
168 msg->received_time = time(NULL);
170 return AERR_OK;
171 }
173 accept_error
174 accept_message_prepare(message * msg, guint flags)
175 {
176 struct passwd *passwd = NULL;
177 GList *non_rcpt_list = NULL;
178 time_t rec_time = time(NULL);
180 DEBUG(5) debugf("accept_message_prepare()\n");
182 /* create unique message id */
183 msg->uid = g_malloc(14);
185 string_base62(msg->uid, rec_time, 6);
186 msg->uid[6] = '-';
187 string_base62(&(msg->uid[7]), getpid(), 3);
188 msg->uid[10] = '-';
189 string_base62(&(msg->uid[11]), msg->transfer_id, 2);
190 msg->uid[13] = 0;
192 /* if local, get password entry */
193 if (msg->received_host == NULL) {
194 passwd = g_memdup(getpwuid(geteuid()), sizeof(struct passwd));
195 msg->ident = g_strdup(passwd->pw_name);
196 }
198 /* set return path if local */
199 if (msg->return_path == NULL) {
201 if (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 }
207 }
209 /* -t option */
210 if (flags & ACC_DEL_RCPTS) {
211 non_rcpt_list = msg->rcpt_list;
212 msg->rcpt_list = NULL;
213 }
215 /* scan headers */
216 {
217 gboolean has_id = FALSE;
218 gboolean has_date = FALSE;
219 gboolean has_sender = FALSE;
220 gboolean has_from = FALSE;
221 gboolean has_rcpt = FALSE;
222 gboolean has_to_or_cc = FALSE;
223 GList *hdr_node, *hdr_node_next;
224 header *hdr;
226 for (hdr_node = g_list_first(msg->hdr_list);
227 hdr_node != NULL; hdr_node = hdr_node_next) {
228 hdr_node_next = g_list_next(hdr_node);
229 hdr = ((header *) (hdr_node->data));
230 DEBUG(5) debugf("scanning headers: %s", hdr->header);
231 switch (hdr->id) {
232 case HEAD_MESSAGE_ID:
233 has_id = TRUE;
234 break;
235 case HEAD_DATE:
236 has_date = TRUE;
237 break;
238 case HEAD_FROM:
239 has_from = TRUE;
240 break;
241 case HEAD_SENDER:
242 has_sender = TRUE;
243 break;
244 case HEAD_TO:
245 case HEAD_CC:
246 case HEAD_BCC:
247 has_rcpt = TRUE;
248 if (flags & ACC_RCPT_FROM_HEAD) {
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 ((flags & ACC_DEL_BCC) && (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 } else
260 has_to_or_cc = TRUE;
261 break;
262 case HEAD_ENVELOPE_TO:
263 if (flags & ACC_SAVE_ENVELOPE_TO) {
264 DEBUG(3) debugf("creating 'X-Orig-Envelope-To' header\n");
265 msg->hdr_list = g_list_prepend(msg->hdr_list, create_header(HEAD_UNKNOWN, "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, "X-Warning: return path set from %s address\n", hdr->id == HEAD_SENDER ? "Sender:" : "From:"));
310 }
311 g_free(addr);
312 }
313 if (msg->return_path == NULL) { /* no Sender: or From: or create_address_qualified failed */
314 msg->return_path = create_address_qualified("postmaster", TRUE, conf.host_name);
315 DEBUG(3) debugf("setting return_path to %s\n", addr_string(msg->return_path));
316 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_UNKNOWN, "X-Warning: real return path is unkown\n"));
317 }
318 }
320 if (flags & ACC_DEL_RCPTS) {
321 GList *rcpt_node;
322 foreach(non_rcpt_list, rcpt_node) {
323 address *rcpt = (address *) (rcpt_node->data);
324 GList *node;
325 if ((node = g_list_find_custom(msg->rcpt_list, rcpt, _g_list_addr_isequal))) {
326 DEBUG(3) debugf("removing rcpt address %s\n", addr_string(node->data));
327 msg->rcpt_list = g_list_remove_link(msg->rcpt_list, node);
328 destroy_address((address *) (node->data));
329 g_list_free_1(node);
330 }
331 }
332 }
334 /* here we should have our recipients, fail if not: */
335 if (msg->rcpt_list == NULL) {
336 logwrite(LOG_WARNING, "no recipients found in message\n");
337 return AERR_NORCPT;
338 }
340 if (!(has_sender || has_from)) {
341 DEBUG(3) debugf("adding 'From' header\n");
342 msg->hdr_list = g_list_append(msg->hdr_list,
343 msg->full_sender_name
344 ?
345 create_header(HEAD_FROM,
346 "From: \"%s\" <%s@%s>\n",
347 msg->full_sender_name,
348 msg->return_path->local_part,
349 msg->return_path->
350 domain)
351 :
352 create_header(HEAD_FROM, "From: <%s@%s>\n",
353 msg->return_path->local_part,
354 msg->return_path->domain)
355 );
356 }
357 if ((flags & ACC_HEAD_FROM_RCPT) && !has_rcpt) {
358 GList *node;
359 DEBUG(3) debugf("adding 'To' header(s)\n");
360 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
361 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_TO, "To: %s\n", addr_string(msg-> return_path)));
362 }
363 }
364 if ((flags & ACC_DEL_BCC) && !has_to_or_cc) {
365 /* Bcc headers have been removed, and there are no remaining rcpt headers */
366 DEBUG(3) debugf("adding empty 'Bcc:' header\n");
367 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_BCC, "Bcc:\n"));
368 }
369 if (!has_date) {
370 DEBUG(3) debugf("adding 'Date:' header\n");
371 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_DATE, "Date: %s\n", rec_timestamp()));
372 }
373 if (!has_id) {
374 DEBUG(3) debugf("adding 'Message-ID:' header\n");
375 msg->hdr_list = g_list_append(msg->hdr_list, create_header(HEAD_MESSAGE_ID, "Message-ID: <%s@%s>\n", msg->uid, conf.host_name));
376 }
377 }
379 /* Received header: */
380 /* At this point because we have to know the rcpts for the 'for' part */
381 if (!(flags & ACC_NO_RECVD_HDR)) {
382 gchar *for_string = NULL;
383 header *hdr = NULL;
385 DEBUG(3) debugf("adding 'Received:' header\n");
387 if (g_list_length(msg->rcpt_list) == 1) {
388 address *addr = (address *) (g_list_first(msg->rcpt_list)->data);
389 for_string = g_strdup_printf(" for %s", addr_string(addr));
390 }
392 if (msg->received_host == NULL) {
393 hdr = create_header(HEAD_RECEIVED,
394 "Received: from %s by %s"
395 " with %s (%s %s) id %s%s;"
396 " %s\n",
397 passwd->pw_name, conf.host_name,
398 prot_names[msg->received_prot],
399 PACKAGE, VERSION,
400 msg->uid, for_string ? for_string : "",
401 rec_timestamp());
402 } else {
403 #ifdef ENABLE_IDENT
404 DEBUG(5) debugf("adding 'Received:' header (5)\n");
405 hdr = create_header(HEAD_RECEIVED,
406 "Received: from %s (ident=%s) by %s"
407 " with %s (%s %s) id %s%s;"
408 " %s\n",
409 msg->received_host,
410 msg->ident ? msg->ident : "unknown",
411 conf.host_name,
412 prot_names[msg->received_prot],
413 PACKAGE, VERSION,
414 msg->uid, for_string ? for_string : "",
415 rec_timestamp());
416 #else
417 hdr = create_header(HEAD_RECEIVED,
418 "Received: from %s by %s"
419 " with %s (%s %s) id %s%s;"
420 " %s\n",
421 msg->received_host,
422 conf.host_name,
423 prot_names[msg->received_prot],
424 PACKAGE, VERSION,
425 msg->uid, for_string ? for_string : "",
426 rec_timestamp());
427 #endif
428 }
429 header_fold(hdr);
430 msg->hdr_list = g_list_prepend(msg->hdr_list, hdr);
432 if (for_string)
433 g_free(for_string);
434 }
436 /* write message to spool: */
437 /* accept is no longer responsible for this
438 if(!spool_write(msg, TRUE))
439 return AERR_NOSPOOL;
440 */
441 return AERR_OK;
442 }
444 accept_error
445 accept_message(FILE * in, message * msg, guint flags)
446 {
447 accept_error err;
449 err = accept_message_stream(in, msg, flags);
450 if (err == AERR_OK)
451 err = accept_message_prepare(msg, flags);
453 return err;
454 }