masqmail
view src/deliver.c @ 367:b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
Now we use:
/*
** comment
*/
This makes the indent style simpler, too.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Thu, 20 Oct 2011 10:20:59 +0200 |
parents | 41958685480d |
children | d86d7b4b8995 |
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, gchar *host, GList *res_list)
277 {
278 gboolean ok = TRUE;
279 GList *msgout_node;
281 DEBUG(5) debugf("deliver_msglist_host_pipe entered\n");
283 foreach(msgout_list, msgout_node) {
284 msg_out *msgout = (msg_out *) (msgout_node->data);
285 gboolean flag, ok_fail = FALSE;
286 message *msg = msgout->msg;
287 GList *rcpt_node, *rcpt_list = msgout->rcpt_list;
289 DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid);
291 flag = (msg->data_list == NULL);
292 if (flag && !spool_read_data(msg)) {
293 logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid);
294 continue;
295 }
297 ok = FALSE;
298 foreach(rcpt_list, rcpt_node) {
299 address *rcpt = (address *) (rcpt_node->data);
300 gchar *cmd = g_malloc(256);
301 GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt);
303 DEBUG(1) debugf("attempting to deliver %s to %s@%s with pipe\n",
304 msg->uid, rcpt->local_part, rcpt->domain);
306 if (!expand(var_table, route->pipe, cmd, 256)) {
307 logwrite(LOG_ALERT, "could not expand string `%s'\n", route->pipe);
308 destroy_table(var_table);
309 continue;
310 }
312 if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0)
313 | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) {
314 logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n",
315 msg->uid, rcpt->local_part, rcpt->domain, cmd);
316 addr_mark_delivered(rcpt);
317 ok = TRUE;
318 } else {
319 logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe);
321 if (route->connect_error_fail) {
322 addr_mark_failed(rcpt);
323 } else {
324 addr_mark_defered(rcpt);
325 }
326 }
328 destroy_table(var_table);
329 }
330 ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno));
332 if (flag) {
333 msg_free_data(msg);
334 }
335 if (ok || ok_fail) {
336 deliver_finish(msgout);
337 }
338 }
340 return ok;
341 }
343 /*
344 ** deliver list of messages to one host and finishes them if the message was
345 ** delivered to at least one rcpt.
346 ** Returns TRUE if at least one msg was delivered to at least one rcpt.
347 */
348 gboolean
349 deliver_msglist_host_smtp(connect_route *route, GList *msgout_list,
350 gchar *host, GList *res_list)
351 {
352 gboolean ok = FALSE;
353 GList *msgout_node;
354 smtp_base *psb;
355 gint port = 25;
357 /* paranoid check: */
358 if (!msgout_list) {
359 logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n");
360 return FALSE;
361 }
363 if (!host) {
364 /* XXX: what if mail_host isn't set? Is this possible? */
365 host = route->mail_host->address;
366 port = route->mail_host->port;
367 }
369 if (route->wrapper) {
370 psb = smtp_out_open_child(route->wrapper, host);
371 } else {
372 psb = smtp_out_open(host, port, res_list);
373 }
375 if (!psb) {
376 /* smtp_out_open() failed */
377 foreach(msgout_list, msgout_node) {
378 msg_out *msgout = (msg_out *) (msgout_node->data);
379 GList *rcpt_node;
381 for (rcpt_node = g_list_first(msgout->rcpt_list);
382 rcpt_node;
383 rcpt_node = g_list_next(rcpt_node)) {
384 address *rcpt = (address *) (rcpt_node->data);
385 gboolean ret = FALSE;
387 addr_unmark_delivered(rcpt);
388 if (route->connect_error_fail) {
389 addr_mark_failed(rcpt);
390 } else {
391 addr_mark_defered(rcpt);
392 }
393 if (route->wrapper) {
394 ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open wrapper:\n\t%s", strerror(errno));
395 } else {
396 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));
397 }
398 if (ret) {
399 deliver_finish(msgout);
400 }
401 }
402 }
403 return ok;
404 }
406 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name, route->do_correct_helo);
408 #ifdef ENABLE_AUTH
409 if (route->auth_name && route->auth_login && route->auth_secret) {
410 set_auth(psb, route->auth_name, route->auth_login, route->auth_secret);
411 }
412 #endif
413 if (!smtp_out_init(psb, route->instant_helo)) {
414 /* smtp_out_init() failed */
415 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) || (psb->error==smtp_syntax)) {
416 smtp_out_quit(psb);
418 foreach(msgout_list, msgout_node) {
419 msg_out *msgout = (msg_out *) (msgout_node->data);
420 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
422 if (delivery_failures(msgout->msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer)) {
423 deliver_finish(msgout);
424 }
425 }
426 }
427 destroy_smtpbase(psb);
428 return ok;
429 }
431 if (!route->do_pipelining) {
432 psb->use_pipelining = FALSE;
433 }
435 foreach(msgout_list, msgout_node) {
436 msg_out *msgout = (msg_out *) (msgout_node->data);
437 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
438 message *msg = msgout->msg;
440 /* we may have to read the data at this point and remember if we did */
441 flag = (msg->data_list == NULL);
442 if (flag && !spool_read_data(msg)) {
443 logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid);
444 break;
445 }
447 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list, msgout->hdr_list);
449 ok_fail = delivery_failures(msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer);
451 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
452 /* connection lost */
453 break;
454 } else if (psb->error != smtp_ok) {
455 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
456 break;
457 }
458 }
459 ok_msg = (psb->error == smtp_ok);
461 if (flag) {
462 msg_free_data(msg);
463 }
464 if (ok_msg) {
465 ok = TRUE;
466 }
467 if (ok_msg || ok_fail) {
468 deliver_finish(msgout);
469 }
470 }
471 if (psb->error == smtp_ok || (psb->error == smtp_fail)
472 || (psb->error == smtp_trylater) || (psb->error == smtp_syntax)) {
473 smtp_out_quit(psb);
474 }
475 destroy_smtpbase(psb);
476 return ok;
477 }
479 gboolean
480 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host, GList *res_list)
481 {
483 if (route->pipe) {
484 DEBUG(5) debugf("with pipe\n");
485 return deliver_msglist_host_pipe(route, msgout_list, host, res_list);
486 } else {
487 DEBUG(5) debugf("with smtp\n");
488 return deliver_msglist_host_smtp(route, msgout_list, host, res_list);
489 }
490 }
492 /*
493 ** delivers messages in msgout_list using route
494 */
495 gboolean
496 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
497 {
498 gboolean ok = FALSE;
499 GList *mo_ph_list;
500 GList *mo_ph_node;
502 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name = %s\n", route->name);
504 if (route->mail_host) {
505 /* this is easy... deliver everything to a smart host for relay */
506 return deliver_msglist_host(route, msgout_list, NULL, route->resolve_list);
507 }
509 /* this is not easy... */
511 mo_ph_list = route_msgout_list(route, msgout_list);
512 /* okay, now we have ordered our messages by the hosts. */
513 if (!mo_ph_list) {
514 return FALSE;
515 }
517 /*
518 ** TODO: It would be nice to be able to fork for each host.
519 ** We cannot do that yet because of complications with finishing the
520 ** messages. Threads could be a solution because they use the same
521 ** memory. But we are not thread safe yet...
522 */
523 foreach(mo_ph_list, mo_ph_node) {
524 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
525 if (deliver_msglist_host(route, mo_ph->msgout_list, mo_ph->host, route->resolve_list)) {
526 ok = TRUE;
527 }
528 destroy_msgout_perhost(mo_ph);
529 }
530 g_list_free(mo_ph_list);
531 return ok;
532 }
534 /*
535 ** calls route_prepare_msg()
536 ** delivers messages in msg_list using route by calling
537 ** deliver_route_msgout_list()
538 */
539 gboolean
540 deliver_route_msg_list(connect_route *route, GList *msgout_list)
541 {
542 GList *msgout_list_deliver = NULL;
543 GList *msgout_node;
544 gboolean ok = TRUE;
546 DEBUG(6) debugf("deliver_route_msg_list()\n");
548 foreach(msgout_list, msgout_node) {
549 msg_out *msgout = (msg_out *) (msgout_node->data);
550 msg_out *msgout_cloned = clone_msg_out(msgout);
551 GList *rcpt_list_non_delivered = NULL;
552 GList *rcpt_node;
554 /*
555 ** we have to delete already delivered rcpt's because a
556 ** previous route may have delivered to it
557 */
558 foreach(msgout_cloned->rcpt_list, rcpt_node) {
559 address *rcpt = (address *) (rcpt_node->data);
560 /*
561 ** failed addresses already have been bounced;
562 ** there should be a better way to handle those.
563 */
564 if (!addr_is_delivered(rcpt) && !addr_is_failed(rcpt)
565 && !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)) {
566 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
567 }
568 }
569 g_list_free(msgout_cloned->rcpt_list);
570 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
572 if (!msgout_cloned->rcpt_list) {
573 destroy_msg_out(msgout_cloned);
574 continue;
575 }
577 /* filter by allowed envelope sender */
578 if (!route_sender_is_allowed(route, msgout->msg->return_path)) {
579 destroy_msg_out(msgout_cloned);
580 continue;
581 }
583 /* filter by allowed envelope rcpts */
584 GList *rcpt_list_allowed = NULL;
585 GList *rcpt_list_notallowed = NULL;
586 route_split_rcpts(route, msgout_cloned->rcpt_list, &rcpt_list_allowed, &rcpt_list_notallowed);
587 if (!rcpt_list_allowed) {
588 destroy_msg_out(msgout_cloned);
589 continue;
590 }
592 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid, route->name);
594 g_list_free(msgout_cloned->rcpt_list);
595 msgout_cloned->rcpt_list = rcpt_list_allowed;
597 if (route->last_route) {
598 GList *rcpt_node;
599 foreach(msgout_cloned->rcpt_list, rcpt_node) {
600 address *rcpt = (address *) (rcpt_node->data);
601 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
602 }
603 }
605 route_prepare_msgout(route, msgout_cloned);
606 msgout_list_deliver = g_list_append(msgout_list_deliver, msgout_cloned);
607 }
609 if (msgout_list_deliver) {
610 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
611 ok = TRUE;
612 }
613 destroy_msg_out_list(msgout_list_deliver);
614 }
615 return ok;
616 }
618 /*
619 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
620 ** to make sure that they will not be delivered again.
621 */
622 void
623 update_non_rcpt_list(msg_out *msgout)
624 {
625 GList *rcpt_node;
626 message *msg = msgout->msg;
628 foreach(msgout->rcpt_list, rcpt_node) {
629 address *rcpt = (address *) (rcpt_node->data);
630 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
631 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, rcpt);
632 }
633 }
634 }
636 /*
637 ** after delivery attempts, we check if there are any rcpt addresses left in
638 ** the message. If all addresses have been completed, the spool files will be
639 ** deleted, otherwise the header spool will be written back. We never changed
640 ** the data spool, so there is no need to write that back.
641 **
642 ** returns TRUE if all went well.
643 */
644 gboolean
645 deliver_finish(msg_out *msgout)
646 {
647 GList *rcpt_node;
648 message *msg = msgout->msg;
649 gboolean finished = TRUE;
651 update_non_rcpt_list(msgout);
653 /*
654 ** we NEVER made copies of the addresses, flags affecting addresses
655 ** were always set on the original address structs
656 */
657 foreach(msg->rcpt_list, rcpt_node) {
658 address *rcpt = (address *) (rcpt_node->data);
659 if (!addr_is_finished_children(rcpt)) {
660 finished = FALSE;
661 } else {
662 /*
663 ** if ALL children have been delivered, mark parent as
664 ** delivered. if there is one or more not delivered,
665 ** it must have failed, we mark the parent as failed
666 ** as well.
667 */
668 if (addr_is_delivered_children(rcpt)) {
669 addr_mark_delivered(rcpt);
670 } else {
671 addr_mark_failed(rcpt);
672 }
673 }
674 }
676 if (finished) {
677 if (spool_delete_all(msg)) {
678 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
679 return TRUE;
680 }
681 return FALSE;
682 }
684 /* one not delivered address was found */
685 if (!spool_write(msg, FALSE)) {
686 logwrite(LOG_ALERT, "could not write back spool header for %s\n", msg->uid);
687 return FALSE;
688 }
690 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
691 return TRUE;
692 }
694 int
695 deliver_remote(GList *remote_msgout_list)
696 {
697 int ok = TRUE;
698 GList *route_list = NULL;
699 GList *route_node;
700 GList *rf_list = NULL;
701 gchar *connect_name = NULL;
703 if (!remote_msgout_list) {
704 return FALSE;
705 }
707 /* perma routes */
708 if (conf.perma_routes) {
709 DEBUG(5) debugf("processing perma_routes\n");
711 route_list = read_route_list(conf.perma_routes, TRUE);
712 foreach(route_list, route_node) {
713 connect_route *route = (connect_route *) (route_node->data);
714 if (!deliver_route_msg_list(route, remote_msgout_list)) {
715 ok = FALSE;
716 }
717 }
718 destroy_route_list(route_list);
719 }
721 /* query routes */
722 connect_name = online_query();
723 if (!connect_name) {
724 DEBUG(5) debugf("online query returned false\n");
725 return FALSE;
726 }
728 /* we are online! */
729 DEBUG(5) debugf("processing query_routes\n");
730 logwrite(LOG_NOTICE, "detected online configuration `%s'\n", connect_name);
732 rf_list = (GList *) table_find(conf.query_routes, connect_name);
733 if (!rf_list) {
734 logwrite(LOG_ALERT, "route list with name '%s' not found.\n", connect_name);
735 return FALSE;
736 }
738 route_list = read_route_list(rf_list, FALSE);
739 if (!route_list) {
740 logwrite(LOG_ALERT, "could not read route list '%s'\n", connect_name);
741 return FALSE;
742 }
744 foreach(route_list, route_node) {
745 connect_route *route = (connect_route *) (route_node->data);
746 /* TODO: ok gets overwritten */
747 ok = deliver_route_msg_list(route, remote_msgout_list);
748 }
749 destroy_route_list(route_list);
751 return ok;
752 }
754 /*
755 ** This function splits the list of rcpt addresses
756 ** into local and remote addresses and processes them accordingly.
757 */
758 gboolean
759 deliver_msg_list(GList *msg_list, guint flags)
760 {
761 GList *msgout_list = NULL;
762 GList *msg_node;
763 GList *local_msgout_list = NULL;
764 GList *remote_msgout_list = NULL;
765 GList *msgout_node;
766 GList *alias_table = NULL;
767 gboolean ok = TRUE;
769 /* create msgout_list */
770 foreach(msg_list, msg_node) {
771 message *msg = (message *) msg_node->data;
772 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
773 }
775 if (conf.alias_file) {
776 alias_table = table_read(conf.alias_file, ':');
777 }
779 /* sort messages for different deliveries */
780 foreach(msgout_list, msgout_node) {
781 msg_out *msgout = (msg_out *) (msgout_node->data);
782 GList *rcpt_list;
783 GList *local_rcpt_list = NULL;
784 GList *other_rcpt_list = NULL;
786 if (!spool_lock(msgout->msg->uid)) {
787 DEBUG(5) debugf("spool_lock(%s) failed.\n", msgout->msg->uid);
788 continue;
789 }
790 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
792 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
793 if (conf.log_user) {
794 address *addr = create_address_qualified(conf.log_user, TRUE, conf.host_name);
795 if (addr) {
796 rcpt_list = g_list_prepend(rcpt_list, addr);
797 } else {
798 logwrite(LOG_ALERT, "invalid log_user address `%s', ignoring\n", conf.log_user);
799 }
800 }
801 if (alias_table) {
802 GList *aliased_rcpt_list;
803 aliased_rcpt_list = alias_expand(alias_table, rcpt_list, msgout->msg->non_rcpt_list);
804 g_list_free(rcpt_list);
805 rcpt_list = aliased_rcpt_list;
806 }
808 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL, &other_rcpt_list); */
809 local_rcpt_list = local_rcpts(rcpt_list);
810 other_rcpt_list = remote_rcpts(rcpt_list);
811 g_list_free(rcpt_list);
813 /* local recipients */
814 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
815 msg_out *local_msgout = clone_msg_out(msgout);
816 local_msgout->rcpt_list = local_rcpt_list;
817 local_msgout_list = g_list_append(local_msgout_list, local_msgout);
818 }
820 /* remote recipients, requires online delivery */
821 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
822 msg_out *remote_msgout = clone_msg_out(msgout);
823 remote_msgout->rcpt_list = other_rcpt_list;
824 remote_msgout_list = g_list_append(remote_msgout_list, remote_msgout);
825 }
826 }
828 if (alias_table) {
829 destroy_table(alias_table);
830 }
832 /* process local/remote msgout lists -> delivery */
834 if (local_msgout_list) {
835 DEBUG(5) debugf("local_msgout_list\n");
836 foreach(local_msgout_list, msgout_node) {
837 msg_out *msgout = (msg_out *) (msgout_node->data);
838 if (!deliver_local(msgout)) {
839 ok = FALSE;
840 }
841 }
842 destroy_msg_out_list(local_msgout_list);
843 }
845 if (remote_msgout_list) {
846 DEBUG(5) debugf("remote_msgout_list\n");
847 deliver_remote(remote_msgout_list);
848 destroy_msg_out_list(remote_msgout_list);
849 }
851 /* unlock spool files */
852 foreach(msgout_list, msgout_node) {
853 msg_out *msgout = (msg_out *) (msgout_node->data);
854 if (spool_unlock(msgout->msg->uid)) {
855 DEBUG(5) debugf("spool_unlock(%s)\n", msgout->msg->uid);
856 } else {
857 DEBUG(5) debugf("spool_unlock(%s) failed.\n", msgout->msg->uid);
858 }
859 }
860 destroy_msg_out_list(msgout_list);
862 return ok;
863 }
865 /*
866 ** deliver() is called when a message has just been received
867 ** (mode_accept and smtp_in) and should be delivered immediately
868 ** (neither -odq nor do_queue). Only this one message will be tried to
869 ** deliver then.
870 */
871 gboolean
872 deliver(message *msg)
873 {
874 gboolean ok;
875 GList *msg_list = g_list_append(NULL, msg);
877 ok = deliver_msg_list(msg_list, DLVR_ALL);
878 g_list_free(msg_list);
880 return ok;
881 }