masqmail
view src/deliver.c @ 403:7954b82040b3
Broke long lines.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Wed, 29 Feb 2012 09:46:34 +0100 |
parents | a408411ff8df |
children | 899c97e877a5 |
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2002 Oliver Kurth
4 ** Copyright (C) 2008, 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include <fnmatch.h>
22 #include <sysexits.h>
23 #include <netdb.h>
25 #include "masqmail.h"
26 #include "smtp_out.h"
28 /*
29 ** collect failed/defered rcpts for failure/warning messages
30 ** returns TRUE if either there are no failures or a failure message has
31 ** been successfully sent
32 */
33 gboolean
34 delivery_failures(message *msg, GList *rcpt_list, gchar *err_fmt, ...)
35 {
36 gboolean ok_fail = TRUE, ok_warn = TRUE;
37 time_t now = time(NULL);
39 GList *failed_list = NULL, *defered_list = NULL, *rcpt_node;
40 va_list args;
41 va_start(args, err_fmt);
43 foreach(rcpt_list, rcpt_node) {
44 address *rcpt = (address *) (rcpt_node->data);
46 if (addr_is_defered(rcpt)) {
47 if ((now - msg->received_time) >= conf.max_defer_time){
48 addr_mark_failed(rcpt);
49 } else {
50 defered_list = g_list_prepend(defered_list,
51 rcpt);
52 }
53 }
54 if (addr_is_failed(rcpt)) {
55 failed_list = g_list_prepend(failed_list, rcpt);
56 }
57 }
58 if (failed_list) {
59 ok_fail = fail_msg(msg, conf.errmsg_file, failed_list,
60 err_fmt, args);
61 g_list_free(failed_list);
62 }
63 if (defered_list) {
64 ok_warn = warn_msg(msg, conf.warnmsg_file, defered_list,
65 err_fmt, args);
66 g_list_free(defered_list);
67 }
68 va_end(args);
69 return ok_fail && ok_warn;
70 }
72 static gint
73 _g_list_strcasecmp(gconstpointer a, gconstpointer b)
74 {
75 return (gint) strcasecmp(a, b);
76 }
78 gboolean
79 deliver_local_mbox(message *msg, GList *hdr_list, address *rcpt,
80 address *env_addr)
81 {
82 DEBUG(1) debugf("attempting to deliver %s with mbox\n", msg->uid);
83 if (append_file(msg, hdr_list, rcpt->local_part)) {
84 if (env_addr != rcpt) {
85 logwrite(LOG_NOTICE, "%s => %s@%s <%s@%s> with mbox\n",
86 msg->uid, rcpt->local_part,
87 rcpt->domain, env_addr->local_part,
88 env_addr->domain);
89 } else {
90 logwrite(LOG_NOTICE, "%s => <%s@%s> with mbox\n",
91 msg->uid, rcpt->local_part,
92 rcpt->domain);
93 }
94 addr_mark_delivered(rcpt);
95 return TRUE;
96 }
98 /* prevents 'Resource temporarily unavailable (11)' */
99 if (errno != EAGAIN) {
100 addr_mark_failed(rcpt);
101 } else {
102 addr_mark_defered(rcpt);
103 }
104 return FALSE;
105 }
107 gboolean
108 deliver_local_pipe(message *msg, GList *hdr_list, address *rcpt,
109 address *env_addr)
110 {
111 guint flags = 0;
113 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
115 flags |= (conf.pipe_fromline) ? MSGSTR_FROMLINE : 0;
116 flags |= (conf.pipe_fromhack) ? MSGSTR_FROMHACK : 0;
117 if (pipe_out(msg, hdr_list, rcpt, &(rcpt->local_part[1]), flags)) {
118 logwrite(LOG_NOTICE, "%s => %s <%s@%s> with pipe\n",
119 msg->uid, rcpt->local_part,
120 env_addr->local_part, env_addr->domain);
121 addr_mark_delivered(rcpt);
122 return TRUE;
123 }
125 if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
126 addr_mark_failed(rcpt);
127 } else {
128 addr_mark_defered(rcpt);
129 /* has no effect yet, except that mail remains in spool */
130 }
131 return FALSE;
132 }
134 gboolean
135 deliver_local_mda(message *msg, GList *hdr_list, address *rcpt,
136 address *env_addr)
137 {
138 gboolean ok = FALSE;
139 gchar *cmd = g_malloc(256);
140 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
141 guint flags = 0;
143 DEBUG(1) debugf("attempting to deliver %s with mda\n", msg->uid);
145 if (!expand(var_table, conf.mda, cmd, 256)) {
146 logwrite(LOG_ALERT, "could not expand string %s\n", conf.mda);
147 destroy_table(var_table);
148 return FALSE;
149 }
151 flags |= (conf.mda_fromline) ? MSGSTR_FROMLINE : 0;
152 flags |= (conf.mda_fromhack) ? MSGSTR_FROMHACK : 0;
153 if (pipe_out(msg, hdr_list, rcpt, cmd, flags)) {
154 logwrite(LOG_NOTICE, "%s => %s@%s with mda (cmd = '%s')\n",
155 msg->uid, rcpt->local_part, rcpt->domain, cmd);
156 addr_mark_delivered(rcpt);
157 ok = TRUE;
158 } else if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
159 addr_mark_failed(rcpt);
160 } else {
161 addr_mark_defered(rcpt);
162 /* has no effect yet, except that mail remains in spool */
163 }
165 destroy_table(var_table);
166 return ok;
167 }
169 gboolean
170 deliver_local(msg_out *msgout)
171 {
172 message *msg = msgout->msg;
173 GList *rcpt_list = msgout->rcpt_list;
174 GList *rcpt_node;
175 gboolean ok = FALSE, flag = FALSE, ok_fail = FALSE;
177 DEBUG(5) debugf("deliver_local entered\n");
179 flag = (msg->data_list == NULL);
180 if (flag && !spool_read_data(msg)) {
181 logwrite(LOG_ALERT, "could not open data spool file for %s\n",
182 msg->uid);
183 return FALSE;
184 }
186 for (rcpt_node = g_list_first(rcpt_list); rcpt_node;
187 rcpt_node = g_list_next(rcpt_node)) {
188 GList *hdr_list;
189 address *rcpt = (address *) (rcpt_node->data);
190 address *env_addr = addr_find_ancestor(rcpt);
191 address *ret_path = msg->return_path;
192 header *retpath_hdr, *envto_hdr;
194 /*
195 ** we need a private copy of the hdr list because we add
196 ** headers here that belong to the rcpt only. g_list_copy
197 ** copies only the nodes, so it is safe to g_list_free it
198 */
199 hdr_list = g_list_copy(msg->hdr_list);
200 retpath_hdr = create_header(HEAD_ENVELOPE_TO,
201 "Envelope-to: %s\n", addr_string(env_addr));
202 envto_hdr = create_header(HEAD_RETURN_PATH,
203 "Return-path: %s\n", addr_string(ret_path));
205 hdr_list = g_list_prepend(hdr_list, envto_hdr);
206 hdr_list = g_list_prepend(hdr_list, retpath_hdr);
208 if (*rcpt->local_part == '|') {
209 /*
210 ** probably for expanded aliases, but why not done
211 ** like with the mda? //meillo 2010-12-06
212 */
213 if (deliver_local_pipe(msg, hdr_list, rcpt,
214 env_addr)) {
215 ok = TRUE;
216 }
217 } else {
218 /* figure out which mailbox type should be used
219 ** for this user */
220 gchar *user = rcpt->local_part;
221 gchar *mbox_type = conf.mbox_default;
223 if (g_list_find_custom(conf.mbox_users, user,
224 _g_list_strcasecmp)) {
225 mbox_type = "mbox";
226 } else if (g_list_find_custom (conf.mda_users, user,
227 _g_list_strcasecmp)) {
228 mbox_type = "mda";
229 }
231 if (strcmp(mbox_type, "mbox")==0) {
232 if (deliver_local_mbox(msg, hdr_list, rcpt,
233 env_addr)) {
234 ok = TRUE;
235 }
236 } else if (strcmp(mbox_type, "mda") == 0) {
237 if (conf.mda) {
238 if (deliver_local_mda(msg, hdr_list,
239 rcpt, env_addr)) {
240 ok = TRUE;
241 }
242 } else {
243 logwrite(LOG_ALERT, "mbox type is "
244 "mda, but no mda "
245 "command given in "
246 "configuration\n");
247 }
249 } else {
250 logwrite(LOG_ALERT, "unknown mbox type '%s'\n",
251 mbox_type);
252 }
253 }
255 destroy_header(retpath_hdr);
256 destroy_header(envto_hdr);
258 g_list_free(hdr_list);
259 }
260 ok_fail = delivery_failures(msg, rcpt_list, "%s (%d)",
261 ext_strerror(errno), errno);
263 if (flag) {
264 msg_free_data(msg);
265 }
266 if (ok || ok_fail) {
267 deliver_finish(msgout);
268 }
270 return ok;
271 }
273 /*
274 ** make a list of rcpt's of a message that are local
275 ** return a new copy of the list
276 */
277 void
278 msg_rcptlist_local(GList *rcpt_list, GList **p_local_list,
279 GList **p_nonlocal_list)
280 {
281 GList *rcpt_node;
283 foreach(rcpt_list, rcpt_node) {
284 address *rcpt = (address *) (rcpt_node->data);
285 GList *dom_node;
287 DEBUG(5) debugf("checking address %s\n", rcpt->address);
289 /* search for local host list: */
290 foreach(conf.local_hosts, dom_node) {
291 if (fnmatch(dom_node->data, rcpt->domain,
292 FNM_CASEFOLD)==0) {
293 *p_local_list = g_list_append(*p_local_list,
294 rcpt);
295 DEBUG(5) debugf("<%s@%s> is local\n",
296 rcpt->local_part,
297 rcpt->domain);
298 break;
299 } else {
300 *p_nonlocal_list = g_list_append(
301 *p_nonlocal_list, rcpt);
302 }
303 }
304 }
305 }
307 gboolean
308 deliver_msglist_host_pipe(connect_route *route, GList *msgout_list,
309 gchar *host, GList *res_list)
310 {
311 gboolean ok = TRUE;
312 GList *msgout_node;
314 DEBUG(5) debugf("deliver_msglist_host_pipe entered\n");
316 foreach(msgout_list, msgout_node) {
317 msg_out *msgout = (msg_out *) (msgout_node->data);
318 gboolean flag, ok_fail = FALSE;
319 message *msg = msgout->msg;
320 GList *rcpt_node, *rcpt_list = msgout->rcpt_list;
322 DEBUG(1) debugf("attempting to deliver %s with pipe\n",
323 msg->uid);
325 flag = (msg->data_list == NULL);
326 if (flag && !spool_read_data(msg)) {
327 logwrite(LOG_ALERT, "could not open data spool file "
328 "for %s\n", msg->uid);
329 continue;
330 }
332 ok = FALSE;
333 foreach(rcpt_list, rcpt_node) {
334 address *rcpt = (address *) (rcpt_node->data);
335 gchar *cmd = g_malloc(256);
336 GList *var_table = var_table_rcpt(var_table_msg(NULL,
337 msg), rcpt);
339 DEBUG(1) debugf("attempting to deliver %s to %s@%s "
340 "with pipe\n", msg->uid,
341 rcpt->local_part, rcpt->domain);
343 if (!expand(var_table, route->pipe, cmd, 256)) {
344 logwrite(LOG_ALERT, "could not expand string `%s'\n", route->pipe);
345 destroy_table(var_table);
346 continue;
347 }
349 if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0)
350 | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) {
351 logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n",
352 msg->uid, rcpt->local_part, rcpt->domain, cmd);
353 addr_mark_delivered(rcpt);
354 ok = TRUE;
355 } else {
356 logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe);
358 if (route->connect_error_fail) {
359 addr_mark_failed(rcpt);
360 } else {
361 addr_mark_defered(rcpt);
362 }
363 }
365 destroy_table(var_table);
366 }
367 ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno));
369 if (flag) {
370 msg_free_data(msg);
371 }
372 if (ok || ok_fail) {
373 deliver_finish(msgout);
374 }
375 }
377 return ok;
378 }
380 /*
381 ** deliver list of messages to one host and finishes them if the message was
382 ** delivered to at least one rcpt.
383 ** Returns TRUE if at least one msg was delivered to at least one rcpt.
384 */
385 gboolean
386 deliver_msglist_host_smtp(connect_route *route, GList *msgout_list,
387 gchar *host, GList *res_list)
388 {
389 gboolean ok = FALSE;
390 GList *msgout_node;
391 smtp_base *psb;
392 gint port = 25;
394 /* paranoid check: */
395 if (!msgout_list) {
396 logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n");
397 return FALSE;
398 }
400 if (!host) {
401 /* XXX: what if mail_host isn't set? Is this possible? */
402 host = route->mail_host->address;
403 port = route->mail_host->port;
404 }
406 if (route->wrapper) {
407 psb = smtp_out_open_child(route->wrapper);
408 if (psb) {
409 psb->remote_host = host;
410 }
411 } else {
412 psb = smtp_out_open(host, port, res_list);
413 }
415 if (!psb) {
416 /* smtp_out_open() failed */
417 foreach(msgout_list, msgout_node) {
418 msg_out *msgout = (msg_out *) (msgout_node->data);
419 GList *rcpt_node;
421 for (rcpt_node = g_list_first(msgout->rcpt_list);
422 rcpt_node;
423 rcpt_node = g_list_next(rcpt_node)) {
424 address *rcpt = (address *) (rcpt_node->data);
425 gboolean ret = FALSE;
427 addr_unmark_delivered(rcpt);
428 if (route->connect_error_fail) {
429 addr_mark_failed(rcpt);
430 } else {
431 addr_mark_defered(rcpt);
432 }
433 if (route->wrapper) {
434 ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open wrapper:\n\t%s", strerror(errno));
435 } else {
436 ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open connection to %s:%d :\n\t%s", host, port, h_errno != 0 ? hstrerror(h_errno) : strerror(errno));
437 }
438 if (ret) {
439 deliver_finish(msgout);
440 }
441 }
442 }
443 return ok;
444 }
446 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name, route->do_correct_helo);
448 #ifdef ENABLE_AUTH
449 if (route->auth_name && route->auth_login && route->auth_secret) {
450 set_auth(psb, route->auth_name, route->auth_login, route->auth_secret);
451 }
452 #endif
453 if (!smtp_out_init(psb, route->instant_helo)) {
454 /* smtp_out_init() failed */
455 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) || (psb->error==smtp_syntax)) {
456 smtp_out_quit(psb);
458 foreach(msgout_list, msgout_node) {
459 msg_out *msgout = (msg_out *) (msgout_node->data);
460 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
462 if (delivery_failures(msgout->msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", (route->wrapper) ? "<wrapper>" : host, psb->buffer)) {
463 deliver_finish(msgout);
464 }
465 }
466 }
467 destroy_smtpbase(psb);
468 return ok;
469 }
471 if (!route->do_pipelining) {
472 psb->use_pipelining = FALSE;
473 }
475 foreach(msgout_list, msgout_node) {
476 msg_out *msgout = (msg_out *) (msgout_node->data);
477 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
478 message *msg = msgout->msg;
480 /* we may have to read the data at this point and remember if we did */
481 flag = (msg->data_list == NULL);
482 if (flag && !spool_read_data(msg)) {
483 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
484 break;
485 }
487 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list, msgout->hdr_list);
489 ok_fail = delivery_failures(msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", (route->wrapper) ? "<wrapper>" : host, psb->buffer);
491 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
492 /* connection lost */
493 break;
494 } else if (psb->error != smtp_ok) {
495 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
496 break;
497 }
498 }
499 ok_msg = (psb->error == smtp_ok);
501 if (flag) {
502 msg_free_data(msg);
503 }
504 if (ok_msg) {
505 ok = TRUE;
506 }
507 if (ok_msg || ok_fail) {
508 deliver_finish(msgout);
509 }
510 }
511 if (psb->error == smtp_ok || (psb->error == smtp_fail)
512 || (psb->error == smtp_trylater) || (psb->error == smtp_syntax)) {
513 smtp_out_quit(psb);
514 }
515 destroy_smtpbase(psb);
516 return ok;
517 }
519 gboolean
520 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host,
521 GList *res_list)
522 {
524 if (route->pipe) {
525 DEBUG(5) debugf("with pipe\n");
526 return deliver_msglist_host_pipe(route, msgout_list, host, res_list);
527 } else {
528 DEBUG(5) debugf("with smtp\n");
529 return deliver_msglist_host_smtp(route, msgout_list, host, res_list);
530 }
531 }
533 /*
534 ** delivers messages in msgout_list using route
535 */
536 gboolean
537 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
538 {
539 gboolean ok = FALSE;
540 GList *mo_ph_list;
541 GList *mo_ph_node;
543 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name = %s\n", route->name);
545 if (route->mail_host) {
546 /* this is easy... deliver everything to a smart host for relay */
547 return deliver_msglist_host(route, msgout_list, NULL,
548 route->resolve_list);
549 }
551 /* this is not easy... */
553 mo_ph_list = route_msgout_list(route, msgout_list);
554 /* okay, now we have ordered our messages by the hosts. */
555 if (!mo_ph_list) {
556 return FALSE;
557 }
559 /*
560 ** TODO: It would be nice to be able to fork for each host.
561 ** We cannot do that yet because of complications with finishing the
562 ** messages. Threads could be a solution because they use the same
563 ** memory. But we are not thread safe yet...
564 */
565 foreach(mo_ph_list, mo_ph_node) {
566 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
567 if (deliver_msglist_host(route, mo_ph->msgout_list,
568 mo_ph->host, route->resolve_list)) {
569 ok = TRUE;
570 }
571 destroy_msgout_perhost(mo_ph);
572 }
573 g_list_free(mo_ph_list);
574 return ok;
575 }
577 /*
578 ** calls route_prepare_msg()
579 ** delivers messages in msg_list using route by calling
580 ** deliver_route_msgout_list()
581 */
582 gboolean
583 deliver_route_msg_list(connect_route *route, GList *msgout_list)
584 {
585 GList *msgout_list_deliver = NULL;
586 GList *msgout_node;
587 gboolean ok = TRUE;
589 DEBUG(6) debugf("deliver_route_msg_list()\n");
591 foreach(msgout_list, msgout_node) {
592 msg_out *msgout = (msg_out *) (msgout_node->data);
593 msg_out *msgout_cloned = clone_msg_out(msgout);
594 GList *rcpt_list_non_delivered = NULL;
595 GList *rcpt_node;
597 /*
598 ** we have to delete already delivered rcpt's because a
599 ** previous route may have delivered to it
600 */
601 foreach(msgout_cloned->rcpt_list, rcpt_node) {
602 address *rcpt = (address *) (rcpt_node->data);
603 /*
604 ** failed addresses already have been bounced;
605 ** there should be a better way to handle those.
606 */
607 if (!addr_is_delivered(rcpt) && !addr_is_failed(rcpt)
608 && !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)) {
609 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
610 }
611 }
612 g_list_free(msgout_cloned->rcpt_list);
613 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
615 if (!msgout_cloned->rcpt_list) {
616 destroy_msg_out(msgout_cloned);
617 continue;
618 }
620 /* filter by allowed envelope sender */
621 if (!route_sender_is_allowed(route, msgout->msg->return_path)) {
622 DEBUG(6) debugf("sender `%s' is not allowed for this route\n", msgout->msg->return_path);
623 destroy_msg_out(msgout_cloned);
624 continue;
625 }
627 /* filter by allowed envelope rcpts */
628 GList *rcpt_list_allowed = NULL;
629 GList *rcpt_list_notallowed = NULL;
630 route_split_rcpts(route, msgout_cloned->rcpt_list,
631 &rcpt_list_allowed, &rcpt_list_notallowed);
632 if (!rcpt_list_allowed) {
633 destroy_msg_out(msgout_cloned);
634 continue;
635 }
637 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid,
638 route->name);
640 g_list_free(msgout_cloned->rcpt_list);
641 msgout_cloned->rcpt_list = rcpt_list_allowed;
643 if (route->last_route) {
644 GList *rcpt_node;
645 foreach(msgout_cloned->rcpt_list, rcpt_node) {
646 address *rcpt = (address *) (rcpt_node->data);
647 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
648 }
649 }
651 route_prepare_msgout(route, msgout_cloned);
652 msgout_list_deliver = g_list_append(msgout_list_deliver, msgout_cloned);
653 }
655 if (msgout_list_deliver) {
656 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
657 ok = TRUE;
658 }
659 destroy_msg_out_list(msgout_list_deliver);
660 }
661 return ok;
662 }
664 /*
665 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
666 ** to make sure that they will not be delivered again.
667 */
668 void
669 update_non_rcpt_list(msg_out *msgout)
670 {
671 GList *rcpt_node;
672 message *msg = msgout->msg;
674 foreach(msgout->rcpt_list, rcpt_node) {
675 address *rcpt = (address *) (rcpt_node->data);
676 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
677 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, rcpt);
678 }
679 }
680 }
682 /*
683 ** after delivery attempts, we check if there are any rcpt addresses left in
684 ** the message. If all addresses have been completed, the spool files will be
685 ** deleted, otherwise the header spool will be written back. We never changed
686 ** the data spool, so there is no need to write that back.
687 **
688 ** returns TRUE if all went well.
689 */
690 gboolean
691 deliver_finish(msg_out *msgout)
692 {
693 GList *rcpt_node;
694 message *msg = msgout->msg;
695 gboolean finished = TRUE;
697 update_non_rcpt_list(msgout);
699 /*
700 ** we NEVER made copies of the addresses, flags affecting addresses
701 ** were always set on the original address structs
702 */
703 foreach(msg->rcpt_list, rcpt_node) {
704 address *rcpt = (address *) (rcpt_node->data);
705 if (!addr_is_finished_children(rcpt)) {
706 finished = FALSE;
707 } else {
708 /*
709 ** if ALL children have been delivered, mark parent as
710 ** delivered. if there is one or more not delivered,
711 ** it must have failed, we mark the parent as failed
712 ** as well.
713 */
714 if (addr_is_delivered_children(rcpt)) {
715 addr_mark_delivered(rcpt);
716 } else {
717 addr_mark_failed(rcpt);
718 }
719 }
720 }
722 if (finished) {
723 if (spool_delete_all(msg)) {
724 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
725 return TRUE;
726 }
727 return FALSE;
728 }
730 /* one not delivered address was found */
731 if (!spool_write(msg, FALSE)) {
732 logwrite(LOG_ALERT, "could not write back spool header for %s\n", msg->uid);
733 return FALSE;
734 }
736 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
737 return TRUE;
738 }
740 int
741 deliver_remote(GList *remote_msgout_list)
742 {
743 int ok = TRUE;
744 GList *route_list = NULL;
745 GList *route_node;
746 GList *rf_list = NULL;
747 gchar *connect_name = NULL;
749 if (!remote_msgout_list) {
750 return FALSE;
751 }
753 /* perma routes */
754 if (conf.perma_routes) {
755 DEBUG(5) debugf("processing perma_routes\n");
757 route_list = read_route_list(conf.perma_routes, TRUE);
758 foreach(route_list, route_node) {
759 connect_route *route = (connect_route *) (route_node->data);
760 if (!deliver_route_msg_list(route, remote_msgout_list)) {
761 ok = FALSE;
762 }
763 }
764 destroy_route_list(route_list);
765 }
767 /* query routes */
768 connect_name = online_query();
769 if (!connect_name) {
770 DEBUG(5) debugf("online query returned false\n");
771 return FALSE;
772 }
774 /* we are online! */
775 DEBUG(5) debugf("processing query_routes\n");
776 logwrite(LOG_NOTICE, "detected online configuration `%s'\n", connect_name);
778 rf_list = (GList *) table_find(conf.query_routes, connect_name);
779 if (!rf_list) {
780 logwrite(LOG_ALERT, "route list with name '%s' not found.\n", connect_name);
781 return FALSE;
782 }
784 route_list = read_route_list(rf_list, FALSE);
785 if (!route_list) {
786 logwrite(LOG_ALERT, "could not read route list '%s'\n", connect_name);
787 return FALSE;
788 }
790 foreach(route_list, route_node) {
791 connect_route *route = (connect_route *) (route_node->data);
792 /* TODO: ok gets overwritten */
793 ok = deliver_route_msg_list(route, remote_msgout_list);
794 }
795 destroy_route_list(route_list);
797 return ok;
798 }
800 /*
801 ** This function splits the list of rcpt addresses
802 ** into local and remote addresses and processes them accordingly.
803 */
804 gboolean
805 deliver_msg_list(GList *msg_list, guint flags)
806 {
807 GList *msgout_list = NULL;
808 GList *msg_node;
809 GList *local_msgout_list = NULL;
810 GList *remote_msgout_list = NULL;
811 GList *msgout_node;
812 GList *alias_table = NULL;
813 GList *globalias_table = NULL;
814 gboolean ok = TRUE;
816 /* create msgout_list */
817 foreach(msg_list, msg_node) {
818 message *msg = (message *) msg_node->data;
819 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
820 }
822 if (conf.globalias_file) {
823 globalias_table = table_read(conf.globalias_file, ':');
824 }
825 if (conf.alias_file) {
826 alias_table = table_read(conf.alias_file, ':');
827 }
829 /* sort messages for different deliveries */
830 foreach(msgout_list, msgout_node) {
831 msg_out *msgout = (msg_out *) (msgout_node->data);
832 GList *rcpt_list;
833 GList *local_rcpt_list = NULL;
834 GList *other_rcpt_list = NULL;
836 if (!spool_lock(msgout->msg->uid)) {
837 DEBUG(5) debugf("spool_lock(%s) failed.\n", msgout->msg->uid);
838 continue;
839 }
840 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
842 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
843 if (conf.log_user) {
844 address *addr = create_address_qualified(conf.log_user, TRUE, conf.host_name);
845 if (addr) {
846 rcpt_list = g_list_prepend(rcpt_list, addr);
847 } else {
848 logwrite(LOG_ALERT, "invalid log_user address `%s', ignoring\n", conf.log_user);
849 }
850 }
851 if (globalias_table) {
852 GList *globaliased_rcpt_list;
853 globaliased_rcpt_list = alias_expand(globalias_table,
854 rcpt_list,
855 msgout->msg->non_rcpt_list, 1);
856 g_list_free(rcpt_list);
857 rcpt_list = globaliased_rcpt_list;
858 }
859 if (alias_table) {
860 GList *aliased_rcpt_list;
861 aliased_rcpt_list = alias_expand(alias_table,
862 rcpt_list,
863 msgout->msg->non_rcpt_list, 0);
864 g_list_free(rcpt_list);
865 rcpt_list = aliased_rcpt_list;
866 }
868 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL, &other_rcpt_list); */
869 local_rcpt_list = local_rcpts(rcpt_list);
870 other_rcpt_list = remote_rcpts(rcpt_list);
871 g_list_free(rcpt_list);
873 /* local recipients */
874 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
875 msg_out *local_msgout = clone_msg_out(msgout);
876 local_msgout->rcpt_list = local_rcpt_list;
877 local_msgout_list = g_list_append(local_msgout_list, local_msgout);
878 }
880 /* remote recipients, requires online delivery */
881 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
882 msg_out *remote_msgout = clone_msg_out(msgout);
883 remote_msgout->rcpt_list = other_rcpt_list;
884 remote_msgout_list = g_list_append(remote_msgout_list, remote_msgout);
885 }
886 }
888 if (alias_table) {
889 destroy_table(alias_table);
890 }
891 if (globalias_table) {
892 destroy_table(globalias_table);
893 }
895 /* process local/remote msgout lists -> delivery */
897 if (local_msgout_list) {
898 DEBUG(5) debugf("local_msgout_list\n");
899 foreach(local_msgout_list, msgout_node) {
900 msg_out *msgout = (msg_out *) (msgout_node->data);
901 if (!deliver_local(msgout)) {
902 ok = FALSE;
903 }
904 }
905 destroy_msg_out_list(local_msgout_list);
906 }
908 if (remote_msgout_list) {
909 DEBUG(5) debugf("remote_msgout_list\n");
910 deliver_remote(remote_msgout_list);
911 destroy_msg_out_list(remote_msgout_list);
912 }
914 /* unlock spool files */
915 foreach(msgout_list, msgout_node) {
916 msg_out *msgout = (msg_out *) (msgout_node->data);
917 if (spool_unlock(msgout->msg->uid)) {
918 DEBUG(5) debugf("spool_unlock(%s)\n", msgout->msg->uid);
919 } else {
920 DEBUG(5) debugf("spool_unlock(%s) failed.\n", msgout->msg->uid);
921 }
922 }
923 destroy_msg_out_list(msgout_list);
925 return ok;
926 }
928 /*
929 ** deliver() is called when a message has just been received
930 ** (mode_accept and smtp_in) and should be delivered immediately
931 ** (neither -odq nor do_queue). Only this one message will be tried to
932 ** deliver then.
933 */
934 gboolean
935 deliver(message *msg)
936 {
937 gboolean ok;
938 GList *msg_list = g_list_append(NULL, msg);
940 ok = deliver_msg_list(msg_list, DLVR_ALL);
941 g_list_free(msg_list);
943 return ok;
944 }