masqmail
view src/deliver.c @ 370:d86d7b4b8995
Broke long lines.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Tue, 25 Oct 2011 13:50:27 +0200 |
parents | b27f66555ba8 |
children | b0708fac99dd |
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, rcpt);
51 }
52 }
53 if (addr_is_failed(rcpt)) {
54 failed_list = g_list_prepend(failed_list, rcpt);
55 }
56 }
57 if (failed_list) {
58 ok_fail = fail_msg(msg, conf.errmsg_file, failed_list, err_fmt, args);
59 g_list_free(failed_list);
60 }
61 if (defered_list) {
62 ok_warn = warn_msg(msg, conf.warnmsg_file, defered_list, err_fmt, args);
63 g_list_free(defered_list);
64 }
65 va_end(args);
66 return ok_fail && ok_warn;
67 }
69 static gint
70 _g_list_strcasecmp(gconstpointer a, gconstpointer b)
71 {
72 return (gint) strcasecmp(a, b);
73 }
75 gboolean
76 deliver_local_mbox(message *msg, GList *hdr_list, address *rcpt, address *env_addr)
77 {
78 DEBUG(1) debugf("attempting to deliver %s with mbox\n", msg->uid);
79 if (append_file(msg, hdr_list, rcpt->local_part)) {
80 if (env_addr != rcpt) {
81 logwrite(LOG_NOTICE, "%s => %s@%s <%s@%s> with mbox\n", msg->uid, rcpt->local_part, rcpt->domain, env_addr->local_part, env_addr->domain);
82 } else {
83 logwrite(LOG_NOTICE, "%s => <%s@%s> with mbox\n", msg->uid, rcpt->local_part, rcpt->domain);
84 }
85 addr_mark_delivered(rcpt);
86 return TRUE;
87 }
89 /* prevents 'Resource temporarily unavailable (11)' */
90 if (errno != EAGAIN) {
91 addr_mark_failed(rcpt);
92 } else {
93 addr_mark_defered(rcpt);
94 }
95 return FALSE;
96 }
98 gboolean
99 deliver_local_pipe(message *msg, GList *hdr_list, address *rcpt, address *env_addr)
100 {
101 guint flags;
103 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
105 flags = (conf.pipe_fromline) ? MSGSTR_FROMLINE : 0;
106 flags |= (conf.pipe_fromhack) ? MSGSTR_FROMHACK : 0;
107 if (pipe_out(msg, hdr_list, rcpt, &(rcpt->local_part[1]), flags)) {
108 logwrite(LOG_NOTICE, "%s => %s <%s@%s> with pipe\n",
109 msg->uid, rcpt->local_part, env_addr->local_part, env_addr->domain);
110 addr_mark_delivered(rcpt);
111 return TRUE;
112 }
114 if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
115 addr_mark_failed(rcpt);
116 } else {
117 addr_mark_defered(rcpt);
118 /* has no effect yet, except that mail remains in spool */
119 }
120 return FALSE;
121 }
123 gboolean
124 deliver_local_mda(message *msg, GList *hdr_list, address *rcpt, address *env_addr)
125 {
126 gboolean ok = FALSE;
127 gchar *cmd = g_malloc(256);
128 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
129 guint flags;
131 DEBUG(1) debugf("attempting to deliver %s with mda\n", msg->uid);
133 if (!expand(var_table, conf.mda, cmd, 256)) {
134 logwrite(LOG_ALERT, "could not expand string %s\n", conf.mda);
135 destroy_table(var_table);
136 return FALSE;
137 }
139 flags = (conf.mda_fromline) ? MSGSTR_FROMLINE : 0;
140 flags |= (conf.mda_fromhack) ? MSGSTR_FROMHACK : 0;
141 if (pipe_out(msg, hdr_list, rcpt, cmd, flags)) {
142 logwrite(LOG_NOTICE, "%s => %s@%s with mda (cmd = '%s')\n",
143 msg->uid, rcpt->local_part, rcpt->domain, cmd);
144 addr_mark_delivered(rcpt);
145 ok = TRUE;
146 } else if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) {
147 addr_mark_failed(rcpt);
148 } else {
149 addr_mark_defered(rcpt);
150 /* has no effect yet, except that mail remains in spool */
151 }
153 destroy_table(var_table);
154 return ok;
155 }
157 gboolean
158 deliver_local(msg_out *msgout)
159 {
160 message *msg = msgout->msg;
161 GList *rcpt_list = msgout->rcpt_list;
162 GList *rcpt_node;
163 gboolean ok = FALSE, flag = FALSE, ok_fail = FALSE;
165 DEBUG(5) debugf("deliver_local entered\n");
167 flag = (msg->data_list == NULL);
168 if (flag && !spool_read_data(msg)) {
169 logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid);
170 return FALSE;
171 }
173 for (rcpt_node = g_list_first(rcpt_list); rcpt_node; rcpt_node = g_list_next(rcpt_node)) {
174 GList *hdr_list;
175 address *rcpt = (address *) (rcpt_node->data);
176 address *env_addr = addr_find_ancestor(rcpt);
177 address *ret_path = msg->return_path;
178 header *retpath_hdr, *envto_hdr;
180 /*
181 ** we need a private copy of the hdr list because we add
182 ** headers here that belong to the rcpt only. g_list_copy
183 ** copies only the nodes, so it is safe to g_list_free it
184 */
185 hdr_list = g_list_copy(msg->hdr_list);
186 retpath_hdr = create_header(HEAD_ENVELOPE_TO, "Envelope-to: %s\n", addr_string(env_addr));
187 envto_hdr = create_header(HEAD_RETURN_PATH, "Return-path: %s\n", addr_string(ret_path));
189 hdr_list = g_list_prepend(hdr_list, envto_hdr);
190 hdr_list = g_list_prepend(hdr_list, retpath_hdr);
192 if (rcpt->local_part[0] == '|') {
193 /*
194 ** probably for expanded aliases, but why not done
195 ** like with the mda? //meillo 2010-12-06
196 */
197 if (deliver_local_pipe(msg, hdr_list, rcpt, env_addr)) {
198 ok = TRUE;
199 }
200 } else {
201 /* figure out which mailbox type should be used for this user */
202 gchar *user = rcpt->local_part;
203 gchar *mbox_type = conf.mbox_default;
205 if (g_list_find_custom (conf.mbox_users, user, _g_list_strcasecmp)) {
206 mbox_type = "mbox";
207 } else if (g_list_find_custom (conf.mda_users, user, _g_list_strcasecmp)) {
208 mbox_type = "mda";
209 }
211 if (strcmp(mbox_type, "mbox") == 0) {
212 if (deliver_local_mbox(msg, hdr_list, rcpt, env_addr)) {
213 ok = TRUE;
214 }
215 } else if (strcmp(mbox_type, "mda") == 0) {
216 if (conf.mda) {
217 if (deliver_local_mda(msg, hdr_list, rcpt, env_addr)) {
218 ok = TRUE;
219 }
220 } else {
221 logwrite(LOG_ALERT, "mbox type is mda, but no mda command given in configuration\n");
222 }
224 } else {
225 logwrite(LOG_ALERT, "unknown mbox type '%s'\n", mbox_type);
226 }
227 }
229 destroy_header(retpath_hdr);
230 destroy_header(envto_hdr);
232 g_list_free(hdr_list);
233 }
234 ok_fail = delivery_failures(msg, rcpt_list, "%s (%d)", ext_strerror(errno), errno);
236 if (flag) {
237 msg_free_data(msg);
238 }
239 if (ok || ok_fail) {
240 deliver_finish(msgout);
241 }
243 return ok;
244 }
246 /*
247 ** make a list of rcpt's of a message that are local
248 ** return a new copy of the list
249 */
250 void
251 msg_rcptlist_local(GList *rcpt_list, GList **p_local_list,
252 GList **p_nonlocal_list)
253 {
254 GList *rcpt_node;
256 foreach(rcpt_list, rcpt_node) {
257 address *rcpt = (address *) (rcpt_node->data);
258 GList *dom_node;
260 DEBUG(5) debugf("checking address %s\n", rcpt->address);
262 /* search for local host list: */
263 foreach(conf.local_hosts, dom_node) {
264 if (strcasecmp(dom_node->data, rcpt->domain) == 0) {
265 *p_local_list = g_list_append(*p_local_list, rcpt);
266 DEBUG(5) debugf("<%s@%s> is local\n", rcpt->local_part, rcpt->domain);
267 break;
268 } else {
269 *p_nonlocal_list = g_list_append(*p_nonlocal_list, rcpt);
270 }
271 }
272 }
273 }
275 gboolean
276 deliver_msglist_host_pipe(connect_route *route, GList *msgout_list,
277 gchar *host, GList *res_list)
278 {
279 gboolean ok = TRUE;
280 GList *msgout_node;
282 DEBUG(5) debugf("deliver_msglist_host_pipe entered\n");
284 foreach(msgout_list, msgout_node) {
285 msg_out *msgout = (msg_out *) (msgout_node->data);
286 gboolean flag, ok_fail = FALSE;
287 message *msg = msgout->msg;
288 GList *rcpt_node, *rcpt_list = msgout->rcpt_list;
290 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
292 flag = (msg->data_list == NULL);
293 if (flag && !spool_read_data(msg)) {
294 logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid);
295 continue;
296 }
298 ok = FALSE;
299 foreach(rcpt_list, rcpt_node) {
300 address *rcpt = (address *) (rcpt_node->data);
301 gchar *cmd = g_malloc(256);
302 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
304 DEBUG(1) debugf("attempting to deliver %s to %s@%s with pipe\n",
305 msg->uid, rcpt->local_part, rcpt->domain);
307 if (!expand(var_table, route->pipe, cmd, 256)) {
308 logwrite(LOG_ALERT, "could not expand string `%s'\n", route->pipe);
309 destroy_table(var_table);
310 continue;
311 }
313 if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0)
314 | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) {
315 logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n",
316 msg->uid, rcpt->local_part, rcpt->domain, cmd);
317 addr_mark_delivered(rcpt);
318 ok = TRUE;
319 } else {
320 logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe);
322 if (route->connect_error_fail) {
323 addr_mark_failed(rcpt);
324 } else {
325 addr_mark_defered(rcpt);
326 }
327 }
329 destroy_table(var_table);
330 }
331 ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno));
333 if (flag) {
334 msg_free_data(msg);
335 }
336 if (ok || ok_fail) {
337 deliver_finish(msgout);
338 }
339 }
341 return ok;
342 }
344 /*
345 ** deliver list of messages to one host and finishes them if the message was
346 ** delivered to at least one rcpt.
347 ** Returns TRUE if at least one msg was delivered to at least one rcpt.
348 */
349 gboolean
350 deliver_msglist_host_smtp(connect_route *route, GList *msgout_list,
351 gchar *host, GList *res_list)
352 {
353 gboolean ok = FALSE;
354 GList *msgout_node;
355 smtp_base *psb;
356 gint port = 25;
358 /* paranoid check: */
359 if (!msgout_list) {
360 logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n");
361 return FALSE;
362 }
364 if (!host) {
365 /* XXX: what if mail_host isn't set? Is this possible? */
366 host = route->mail_host->address;
367 port = route->mail_host->port;
368 }
370 if (route->wrapper) {
371 psb = smtp_out_open_child(route->wrapper, host);
372 } else {
373 psb = smtp_out_open(host, port, res_list);
374 }
376 if (!psb) {
377 /* smtp_out_open() failed */
378 foreach(msgout_list, msgout_node) {
379 msg_out *msgout = (msg_out *) (msgout_node->data);
380 GList *rcpt_node;
382 for (rcpt_node = g_list_first(msgout->rcpt_list);
383 rcpt_node;
384 rcpt_node = g_list_next(rcpt_node)) {
385 address *rcpt = (address *) (rcpt_node->data);
386 gboolean ret = FALSE;
388 addr_unmark_delivered(rcpt);
389 if (route->connect_error_fail) {
390 addr_mark_failed(rcpt);
391 } else {
392 addr_mark_defered(rcpt);
393 }
394 if (route->wrapper) {
395 ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open wrapper:\n\t%s", strerror(errno));
396 } else {
397 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));
398 }
399 if (ret) {
400 deliver_finish(msgout);
401 }
402 }
403 }
404 return ok;
405 }
407 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name, route->do_correct_helo);
409 #ifdef ENABLE_AUTH
410 if (route->auth_name && route->auth_login && route->auth_secret) {
411 set_auth(psb, route->auth_name, route->auth_login, route->auth_secret);
412 }
413 #endif
414 if (!smtp_out_init(psb, route->instant_helo)) {
415 /* smtp_out_init() failed */
416 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) || (psb->error==smtp_syntax)) {
417 smtp_out_quit(psb);
419 foreach(msgout_list, msgout_node) {
420 msg_out *msgout = (msg_out *) (msgout_node->data);
421 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
423 if (delivery_failures(msgout->msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer)) {
424 deliver_finish(msgout);
425 }
426 }
427 }
428 destroy_smtpbase(psb);
429 return ok;
430 }
432 if (!route->do_pipelining) {
433 psb->use_pipelining = FALSE;
434 }
436 foreach(msgout_list, msgout_node) {
437 msg_out *msgout = (msg_out *) (msgout_node->data);
438 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
439 message *msg = msgout->msg;
441 /* we may have to read the data at this point and remember if we did */
442 flag = (msg->data_list == NULL);
443 if (flag && !spool_read_data(msg)) {
444 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
445 break;
446 }
448 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list, msgout->hdr_list);
450 ok_fail = delivery_failures(msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer);
452 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
453 /* connection lost */
454 break;
455 } else if (psb->error != smtp_ok) {
456 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
457 break;
458 }
459 }
460 ok_msg = (psb->error == smtp_ok);
462 if (flag) {
463 msg_free_data(msg);
464 }
465 if (ok_msg) {
466 ok = TRUE;
467 }
468 if (ok_msg || ok_fail) {
469 deliver_finish(msgout);
470 }
471 }
472 if (psb->error == smtp_ok || (psb->error == smtp_fail)
473 || (psb->error == smtp_trylater) || (psb->error == smtp_syntax)) {
474 smtp_out_quit(psb);
475 }
476 destroy_smtpbase(psb);
477 return ok;
478 }
480 gboolean
481 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host,
482 GList *res_list)
483 {
485 if (route->pipe) {
486 DEBUG(5) debugf("with pipe\n");
487 return deliver_msglist_host_pipe(route, msgout_list, host, res_list);
488 } else {
489 DEBUG(5) debugf("with smtp\n");
490 return deliver_msglist_host_smtp(route, msgout_list, host, res_list);
491 }
492 }
494 /*
495 ** delivers messages in msgout_list using route
496 */
497 gboolean
498 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
499 {
500 gboolean ok = FALSE;
501 GList *mo_ph_list;
502 GList *mo_ph_node;
504 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name = %s\n", route->name);
506 if (route->mail_host) {
507 /* this is easy... deliver everything to a smart host for relay */
508 return deliver_msglist_host(route, msgout_list, NULL,
509 route->resolve_list);
510 }
512 /* this is not easy... */
514 mo_ph_list = route_msgout_list(route, msgout_list);
515 /* okay, now we have ordered our messages by the hosts. */
516 if (!mo_ph_list) {
517 return FALSE;
518 }
520 /*
521 ** TODO: It would be nice to be able to fork for each host.
522 ** We cannot do that yet because of complications with finishing the
523 ** messages. Threads could be a solution because they use the same
524 ** memory. But we are not thread safe yet...
525 */
526 foreach(mo_ph_list, mo_ph_node) {
527 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
528 if (deliver_msglist_host(route, mo_ph->msgout_list,
529 mo_ph->host, route->resolve_list)) {
530 ok = TRUE;
531 }
532 destroy_msgout_perhost(mo_ph);
533 }
534 g_list_free(mo_ph_list);
535 return ok;
536 }
538 /*
539 ** calls route_prepare_msg()
540 ** delivers messages in msg_list using route by calling
541 ** deliver_route_msgout_list()
542 */
543 gboolean
544 deliver_route_msg_list(connect_route *route, GList *msgout_list)
545 {
546 GList *msgout_list_deliver = NULL;
547 GList *msgout_node;
548 gboolean ok = TRUE;
550 DEBUG(6) debugf("deliver_route_msg_list()\n");
552 foreach(msgout_list, msgout_node) {
553 msg_out *msgout = (msg_out *) (msgout_node->data);
554 msg_out *msgout_cloned = clone_msg_out(msgout);
555 GList *rcpt_list_non_delivered = NULL;
556 GList *rcpt_node;
558 /*
559 ** we have to delete already delivered rcpt's because a
560 ** previous route may have delivered to it
561 */
562 foreach(msgout_cloned->rcpt_list, rcpt_node) {
563 address *rcpt = (address *) (rcpt_node->data);
564 /*
565 ** failed addresses already have been bounced;
566 ** there should be a better way to handle those.
567 */
568 if (!addr_is_delivered(rcpt) && !addr_is_failed(rcpt)
569 && !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)) {
570 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
571 }
572 }
573 g_list_free(msgout_cloned->rcpt_list);
574 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
576 if (!msgout_cloned->rcpt_list) {
577 destroy_msg_out(msgout_cloned);
578 continue;
579 }
581 /* filter by allowed envelope sender */
582 if (!route_sender_is_allowed(route, msgout->msg->return_path)) {
583 destroy_msg_out(msgout_cloned);
584 continue;
585 }
587 /* filter by allowed envelope rcpts */
588 GList *rcpt_list_allowed = NULL;
589 GList *rcpt_list_notallowed = NULL;
590 route_split_rcpts(route, msgout_cloned->rcpt_list,
591 &rcpt_list_allowed, &rcpt_list_notallowed);
592 if (!rcpt_list_allowed) {
593 destroy_msg_out(msgout_cloned);
594 continue;
595 }
597 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid,
598 route->name);
600 g_list_free(msgout_cloned->rcpt_list);
601 msgout_cloned->rcpt_list = rcpt_list_allowed;
603 if (route->last_route) {
604 GList *rcpt_node;
605 foreach(msgout_cloned->rcpt_list, rcpt_node) {
606 address *rcpt = (address *) (rcpt_node->data);
607 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
608 }
609 }
611 route_prepare_msgout(route, msgout_cloned);
612 msgout_list_deliver = g_list_append(msgout_list_deliver, msgout_cloned);
613 }
615 if (msgout_list_deliver) {
616 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
617 ok = TRUE;
618 }
619 destroy_msg_out_list(msgout_list_deliver);
620 }
621 return ok;
622 }
624 /*
625 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
626 ** to make sure that they will not be delivered again.
627 */
628 void
629 update_non_rcpt_list(msg_out *msgout)
630 {
631 GList *rcpt_node;
632 message *msg = msgout->msg;
634 foreach(msgout->rcpt_list, rcpt_node) {
635 address *rcpt = (address *) (rcpt_node->data);
636 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
637 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, rcpt);
638 }
639 }
640 }
642 /*
643 ** after delivery attempts, we check if there are any rcpt addresses left in
644 ** the message. If all addresses have been completed, the spool files will be
645 ** deleted, otherwise the header spool will be written back. We never changed
646 ** the data spool, so there is no need to write that back.
647 **
648 ** returns TRUE if all went well.
649 */
650 gboolean
651 deliver_finish(msg_out *msgout)
652 {
653 GList *rcpt_node;
654 message *msg = msgout->msg;
655 gboolean finished = TRUE;
657 update_non_rcpt_list(msgout);
659 /*
660 ** we NEVER made copies of the addresses, flags affecting addresses
661 ** were always set on the original address structs
662 */
663 foreach(msg->rcpt_list, rcpt_node) {
664 address *rcpt = (address *) (rcpt_node->data);
665 if (!addr_is_finished_children(rcpt)) {
666 finished = FALSE;
667 } else {
668 /*
669 ** if ALL children have been delivered, mark parent as
670 ** delivered. if there is one or more not delivered,
671 ** it must have failed, we mark the parent as failed
672 ** as well.
673 */
674 if (addr_is_delivered_children(rcpt)) {
675 addr_mark_delivered(rcpt);
676 } else {
677 addr_mark_failed(rcpt);
678 }
679 }
680 }
682 if (finished) {
683 if (spool_delete_all(msg)) {
684 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
685 return TRUE;
686 }
687 return FALSE;
688 }
690 /* one not delivered address was found */
691 if (!spool_write(msg, FALSE)) {
692 logwrite(LOG_ALERT, "could not write back spool header for %s\n", msg->uid);
693 return FALSE;
694 }
696 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
697 return TRUE;
698 }
700 int
701 deliver_remote(GList *remote_msgout_list)
702 {
703 int ok = TRUE;
704 GList *route_list = NULL;
705 GList *route_node;
706 GList *rf_list = NULL;
707 gchar *connect_name = NULL;
709 if (!remote_msgout_list) {
710 return FALSE;
711 }
713 /* perma routes */
714 if (conf.perma_routes) {
715 DEBUG(5) debugf("processing perma_routes\n");
717 route_list = read_route_list(conf.perma_routes, TRUE);
718 foreach(route_list, route_node) {
719 connect_route *route = (connect_route *) (route_node->data);
720 if (!deliver_route_msg_list(route, remote_msgout_list)) {
721 ok = FALSE;
722 }
723 }
724 destroy_route_list(route_list);
725 }
727 /* query routes */
728 connect_name = online_query();
729 if (!connect_name) {
730 DEBUG(5) debugf("online query returned false\n");
731 return FALSE;
732 }
734 /* we are online! */
735 DEBUG(5) debugf("processing query_routes\n");
736 logwrite(LOG_NOTICE, "detected online configuration `%s'\n", connect_name);
738 rf_list = (GList *) table_find(conf.query_routes, connect_name);
739 if (!rf_list) {
740 logwrite(LOG_ALERT, "route list with name '%s' not found.\n", connect_name);
741 return FALSE;
742 }
744 route_list = read_route_list(rf_list, FALSE);
745 if (!route_list) {
746 logwrite(LOG_ALERT, "could not read route list '%s'\n", connect_name);
747 return FALSE;
748 }
750 foreach(route_list, route_node) {
751 connect_route *route = (connect_route *) (route_node->data);
752 /* TODO: ok gets overwritten */
753 ok = deliver_route_msg_list(route, remote_msgout_list);
754 }
755 destroy_route_list(route_list);
757 return ok;
758 }
760 /*
761 ** This function splits the list of rcpt addresses
762 ** into local and remote addresses and processes them accordingly.
763 */
764 gboolean
765 deliver_msg_list(GList *msg_list, guint flags)
766 {
767 GList *msgout_list = NULL;
768 GList *msg_node;
769 GList *local_msgout_list = NULL;
770 GList *remote_msgout_list = NULL;
771 GList *msgout_node;
772 GList *alias_table = NULL;
773 gboolean ok = TRUE;
775 /* create msgout_list */
776 foreach(msg_list, msg_node) {
777 message *msg = (message *) msg_node->data;
778 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
779 }
781 if (conf.alias_file) {
782 alias_table = table_read(conf.alias_file, ':');
783 }
785 /* sort messages for different deliveries */
786 foreach(msgout_list, msgout_node) {
787 msg_out *msgout = (msg_out *) (msgout_node->data);
788 GList *rcpt_list;
789 GList *local_rcpt_list = NULL;
790 GList *other_rcpt_list = NULL;
792 if (!spool_lock(msgout->msg->uid)) {
793 DEBUG(5) debugf("spool_lock(%s) failed.\n", msgout->msg->uid);
794 continue;
795 }
796 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
798 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
799 if (conf.log_user) {
800 address *addr = create_address_qualified(conf.log_user, TRUE, conf.host_name);
801 if (addr) {
802 rcpt_list = g_list_prepend(rcpt_list, addr);
803 } else {
804 logwrite(LOG_ALERT, "invalid log_user address `%s', ignoring\n", conf.log_user);
805 }
806 }
807 if (alias_table) {
808 GList *aliased_rcpt_list;
809 aliased_rcpt_list = alias_expand(alias_table, rcpt_list, msgout->msg->non_rcpt_list);
810 g_list_free(rcpt_list);
811 rcpt_list = aliased_rcpt_list;
812 }
814 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL, &other_rcpt_list); */
815 local_rcpt_list = local_rcpts(rcpt_list);
816 other_rcpt_list = remote_rcpts(rcpt_list);
817 g_list_free(rcpt_list);
819 /* local recipients */
820 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
821 msg_out *local_msgout = clone_msg_out(msgout);
822 local_msgout->rcpt_list = local_rcpt_list;
823 local_msgout_list = g_list_append(local_msgout_list, local_msgout);
824 }
826 /* remote recipients, requires online delivery */
827 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
828 msg_out *remote_msgout = clone_msg_out(msgout);
829 remote_msgout->rcpt_list = other_rcpt_list;
830 remote_msgout_list = g_list_append(remote_msgout_list, remote_msgout);
831 }
832 }
834 if (alias_table) {
835 destroy_table(alias_table);
836 }
838 /* process local/remote msgout lists -> delivery */
840 if (local_msgout_list) {
841 DEBUG(5) debugf("local_msgout_list\n");
842 foreach(local_msgout_list, msgout_node) {
843 msg_out *msgout = (msg_out *) (msgout_node->data);
844 if (!deliver_local(msgout)) {
845 ok = FALSE;
846 }
847 }
848 destroy_msg_out_list(local_msgout_list);
849 }
851 if (remote_msgout_list) {
852 DEBUG(5) debugf("remote_msgout_list\n");
853 deliver_remote(remote_msgout_list);
854 destroy_msg_out_list(remote_msgout_list);
855 }
857 /* unlock spool files */
858 foreach(msgout_list, msgout_node) {
859 msg_out *msgout = (msg_out *) (msgout_node->data);
860 if (spool_unlock(msgout->msg->uid)) {
861 DEBUG(5) debugf("spool_unlock(%s)\n", msgout->msg->uid);
862 } else {
863 DEBUG(5) debugf("spool_unlock(%s) failed.\n", msgout->msg->uid);
864 }
865 }
866 destroy_msg_out_list(msgout_list);
868 return ok;
869 }
871 /*
872 ** deliver() is called when a message has just been received
873 ** (mode_accept and smtp_in) and should be delivered immediately
874 ** (neither -odq nor do_queue). Only this one message will be tried to
875 ** deliver then.
876 */
877 gboolean
878 deliver(message *msg)
879 {
880 gboolean ok;
881 GList *msg_list = g_list_append(NULL, msg);
883 ok = deliver_msg_list(msg_list, DLVR_ALL);
884 g_list_free(msg_list);
886 return ok;
887 }