masqmail
view src/deliver.c @ 433:81c438e1891b
Marks version number as development.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 07 Feb 2015 11:40:21 +0100 |
parents | 8fedb93e9b72 |
children |
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,
435 msgout->rcpt_list,
436 "could not open "
437 "wrapper:\n\t%s",
438 strerror(errno));
439 } else {
440 ret = delivery_failures(msgout->msg,
441 msgout->rcpt_list,
442 "could not open "
443 "connection to %s:%d "
444 ":\n\t%s", host, port,
445 h_errno != 0 ?
446 hstrerror(h_errno) :
447 strerror(errno));
448 }
449 if (ret) {
450 deliver_finish(msgout);
451 }
452 }
453 }
454 return ok;
455 }
457 set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name,
458 route->do_correct_helo);
460 #ifdef ENABLE_AUTH
461 if (route->auth_name && route->auth_login && route->auth_secret) {
462 set_auth(psb, route->auth_name, route->auth_login,
463 route->auth_secret);
464 }
465 #endif
466 if (!smtp_out_init(psb, route->instant_helo)) {
467 /* smtp_out_init() failed */
468 if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) ||
469 (psb->error==smtp_syntax)) {
470 smtp_out_quit(psb);
472 foreach(msgout_list, msgout_node) {
473 msg_out *msgout =
474 (msg_out *)(msgout_node->data);
475 smtp_out_mark_rcpts(psb, msgout->rcpt_list);
477 if (delivery_failures(msgout->msg,
478 msgout->rcpt_list,
479 "while connected with %s, "
480 "the server replied\n\t%s",
481 (route->wrapper) ?
482 "<wrapper>" : host,
483 psb->buffer)) {
484 deliver_finish(msgout);
485 }
486 }
487 }
488 destroy_smtpbase(psb);
489 return ok;
490 }
492 if (!route->do_pipelining) {
493 psb->use_pipelining = FALSE;
494 }
496 foreach(msgout_list, msgout_node) {
497 msg_out *msgout = (msg_out *) (msgout_node->data);
498 gboolean flag, ok_msg = FALSE, ok_fail = FALSE;
499 message *msg = msgout->msg;
501 /* we may have to read the data at this point
502 ** and remember if we did */
503 flag = (msg->data_list == NULL);
504 if (flag && !spool_read_data(msg)) {
505 logwrite(LOG_ALERT, "could not open data spool "
506 "file %s\n", msg->uid);
507 break;
508 }
510 smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list,
511 msgout->hdr_list);
513 ok_fail = delivery_failures(msg, msgout->rcpt_list,
514 "while connected with %s, the server "
515 "replied\n\t%s", (route->wrapper) ?
516 "<wrapper>" : host, psb->buffer);
518 if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) {
519 /* connection lost */
520 break;
521 } else if (psb->error != smtp_ok) {
522 if (g_list_next(msgout_node) && !smtp_out_rset(psb)) {
523 break;
524 }
525 }
526 ok_msg = (psb->error == smtp_ok);
528 if (flag) {
529 msg_free_data(msg);
530 }
531 if (ok_msg) {
532 ok = TRUE;
533 }
534 if (ok_msg || ok_fail) {
535 deliver_finish(msgout);
536 }
537 }
538 if (psb->error == smtp_ok || (psb->error == smtp_fail) ||
539 (psb->error == smtp_trylater) ||
540 (psb->error == smtp_syntax)) {
541 smtp_out_quit(psb);
542 }
543 destroy_smtpbase(psb);
544 return ok;
545 }
547 gboolean
548 deliver_msglist_host(connect_route *route, GList *msgout_list, gchar *host,
549 GList *res_list)
550 {
552 if (route->pipe) {
553 DEBUG(5) debugf("with pipe\n");
554 return deliver_msglist_host_pipe(route, msgout_list,
555 host, res_list);
556 } else {
557 DEBUG(5) debugf("with smtp\n");
558 return deliver_msglist_host_smtp(route, msgout_list,
559 host, res_list);
560 }
561 }
563 /*
564 ** delivers messages in msgout_list using route
565 */
566 gboolean
567 deliver_route_msgout_list(connect_route *route, GList *msgout_list)
568 {
569 gboolean ok = FALSE;
570 GList *mo_ph_list;
571 GList *mo_ph_node;
573 DEBUG(5) debugf("deliver_route_msgout_list entered, route->name=%s\n",
574 route->name);
576 if (route->mail_host) {
577 /* easy: deliver everything to a smart host for relay */
578 return deliver_msglist_host(route, msgout_list, NULL,
579 route->resolve_list);
580 }
582 /* this is not easy... */
584 mo_ph_list = route_msgout_list(route, msgout_list);
585 /* okay, now we have ordered our messages by the hosts. */
586 if (!mo_ph_list) {
587 return FALSE;
588 }
590 /*
591 ** TODO: It would be nice to be able to fork for each host.
592 ** We cannot do that yet because of complications with finishing the
593 ** messages. Threads could be a solution because they use the same
594 ** memory. But we are not thread safe yet...
595 */
596 foreach(mo_ph_list, mo_ph_node) {
597 msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data);
598 if (deliver_msglist_host(route, mo_ph->msgout_list,
599 mo_ph->host, route->resolve_list)) {
600 ok = TRUE;
601 }
602 destroy_msgout_perhost(mo_ph);
603 }
604 g_list_free(mo_ph_list);
605 return ok;
606 }
608 /*
609 ** calls route_prepare_msg()
610 ** delivers messages in msg_list using route by calling
611 ** deliver_route_msgout_list()
612 */
613 gboolean
614 deliver_route_msg_list(connect_route *route, GList *msgout_list)
615 {
616 GList *msgout_list_deliver = NULL;
617 GList *msgout_node;
618 gboolean ok = TRUE;
620 DEBUG(6) debugf("deliver_route_msg_list()\n");
622 foreach(msgout_list, msgout_node) {
623 msg_out *msgout = (msg_out *) (msgout_node->data);
624 msg_out *msgout_cloned = clone_msg_out(msgout);
625 GList *rcpt_list_non_delivered = NULL;
626 GList *rcpt_node;
628 /*
629 ** we have to delete already delivered rcpt's because a
630 ** previous route may have delivered to it
631 */
632 foreach(msgout_cloned->rcpt_list, rcpt_node) {
633 address *rcpt = (address *) (rcpt_node->data);
634 /*
635 ** failed addresses already have been bounced;
636 ** there should be a better way to handle those.
637 */
638 if (!addr_is_delivered(rcpt) &&
639 !addr_is_failed(rcpt) &&
640 !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)){
641 rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt);
642 }
643 }
644 g_list_free(msgout_cloned->rcpt_list);
645 msgout_cloned->rcpt_list = rcpt_list_non_delivered;
647 if (!msgout_cloned->rcpt_list) {
648 destroy_msg_out(msgout_cloned);
649 continue;
650 }
652 /* filter by allowed envelope sender */
653 if (!route_sender_is_allowed(route, msgout->msg->return_path)){
654 DEBUG(6) debugf("sender `%s' is not allowed for this "
655 "route\n", msgout->msg->return_path);
656 destroy_msg_out(msgout_cloned);
657 continue;
658 }
660 /* filter by allowed envelope rcpts */
661 GList *rcpt_list_allowed = NULL;
662 GList *rcpt_list_notallowed = NULL;
663 route_split_rcpts(route, msgout_cloned->rcpt_list,
664 &rcpt_list_allowed, &rcpt_list_notallowed);
665 if (!rcpt_list_allowed) {
666 destroy_msg_out(msgout_cloned);
667 continue;
668 }
670 /* filter by allowed from header */
671 GList *from_hdrs = NULL;
672 char *from_hdr = NULL;
673 from_hdrs = find_header(msgout->msg->hdr_list, HEAD_FROM,
674 NULL);
675 if (from_hdrs) {
676 from_hdr = (char *) ((header *)from_hdrs->data)->value;
677 if (!route_from_hdr_is_allowed(route, from_hdr)){
678 DEBUG(6) debugf("from hdr `%s' is not allowed for this "
679 "route\n", from_hdr);
680 destroy_msg_out(msgout_cloned);
681 continue;
682 }
683 }
685 logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid,
686 route->name);
688 g_list_free(msgout_cloned->rcpt_list);
689 msgout_cloned->rcpt_list = rcpt_list_allowed;
691 if (route->last_route) {
692 GList *rcpt_node;
693 foreach(msgout_cloned->rcpt_list, rcpt_node) {
694 address *rcpt = (address *) (rcpt_node->data);
695 rcpt->flags |= ADDR_FLAG_LAST_ROUTE;
696 }
697 }
699 route_prepare_msgout(route, msgout_cloned);
700 msgout_list_deliver = g_list_append(msgout_list_deliver,
701 msgout_cloned);
702 }
704 if (msgout_list_deliver) {
705 if (deliver_route_msgout_list(route, msgout_list_deliver)) {
706 ok = TRUE;
707 }
708 destroy_msg_out_list(msgout_list_deliver);
709 }
710 return ok;
711 }
713 /*
714 ** copy pointers of delivered addresses to the msg's non_rcpt_list,
715 ** to make sure that they will not be delivered again.
716 */
717 void
718 update_non_rcpt_list(msg_out *msgout)
719 {
720 GList *rcpt_node;
721 message *msg = msgout->msg;
723 foreach(msgout->rcpt_list, rcpt_node) {
724 address *rcpt = (address *) (rcpt_node->data);
725 if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) {
726 msg->non_rcpt_list = g_list_append(msg->non_rcpt_list,
727 rcpt);
728 }
729 }
730 }
732 /*
733 ** after delivery attempts, we check if there are any rcpt addresses left in
734 ** the message. If all addresses have been completed, the spool files will be
735 ** deleted, otherwise the header spool will be written back. We never changed
736 ** the data spool, so there is no need to write that back.
737 **
738 ** returns TRUE if all went well.
739 */
740 gboolean
741 deliver_finish(msg_out *msgout)
742 {
743 GList *rcpt_node;
744 message *msg = msgout->msg;
745 gboolean finished = TRUE;
747 update_non_rcpt_list(msgout);
749 /*
750 ** we NEVER made copies of the addresses, flags affecting addresses
751 ** were always set on the original address structs
752 */
753 foreach(msg->rcpt_list, rcpt_node) {
754 address *rcpt = (address *) (rcpt_node->data);
755 if (!addr_is_finished_children(rcpt)) {
756 finished = FALSE;
757 } else {
758 /*
759 ** if ALL children have been delivered, mark parent as
760 ** delivered. if there is one or more not delivered,
761 ** it must have failed, we mark the parent as failed
762 ** as well.
763 */
764 if (addr_is_delivered_children(rcpt)) {
765 addr_mark_delivered(rcpt);
766 } else {
767 addr_mark_failed(rcpt);
768 }
769 }
770 }
772 if (finished) {
773 if (spool_delete_all(msg)) {
774 logwrite(LOG_NOTICE, "%s completed.\n", msg->uid);
775 return TRUE;
776 }
777 return FALSE;
778 }
780 /* one not delivered address was found */
781 if (!spool_write(msg, FALSE)) {
782 logwrite(LOG_ALERT, "could not write back spool header "
783 "for %s\n", msg->uid);
784 return FALSE;
785 }
787 DEBUG(2) debugf("spool header for %s written back.\n", msg->uid);
788 return TRUE;
789 }
791 int
792 deliver_remote(GList *remote_msgout_list)
793 {
794 int ok = TRUE;
795 GList *route_list = NULL;
796 GList *route_node;
797 GList *rf_list = NULL;
798 gchar *connect_name = NULL;
800 if (!remote_msgout_list) {
801 return FALSE;
802 }
804 /* perma routes */
805 if (conf.perma_routes) {
806 DEBUG(5) debugf("processing perma_routes\n");
808 route_list = read_route_list(conf.perma_routes, TRUE);
809 foreach(route_list, route_node) {
810 connect_route *route =
811 (connect_route *) (route_node->data);
812 if (!deliver_route_msg_list(route,
813 remote_msgout_list)) {
814 ok = FALSE;
815 }
816 }
817 destroy_route_list(route_list);
818 }
820 /* query routes */
821 connect_name = online_query();
822 if (!connect_name) {
823 DEBUG(5) debugf("online query returned false\n");
824 return FALSE;
825 }
827 /* we are online! */
828 DEBUG(5) debugf("processing query_routes\n");
829 logwrite(LOG_NOTICE, "detected online configuration `%s'\n",
830 connect_name);
832 rf_list = (GList *) table_find(conf.query_routes, connect_name);
833 if (!rf_list) {
834 logwrite(LOG_ALERT, "route list with name '%s' not found.\n",
835 connect_name);
836 return FALSE;
837 }
839 route_list = read_route_list(rf_list, FALSE);
840 if (!route_list) {
841 logwrite(LOG_ALERT, "could not read route list '%s'\n",
842 connect_name);
843 return FALSE;
844 }
846 foreach(route_list, route_node) {
847 connect_route *route = (connect_route *) (route_node->data);
848 /* TODO: ok gets overwritten */
849 ok = deliver_route_msg_list(route, remote_msgout_list);
850 }
851 destroy_route_list(route_list);
853 return ok;
854 }
856 /*
857 ** This function splits the list of rcpt addresses
858 ** into local and remote addresses and processes them accordingly.
859 */
860 gboolean
861 deliver_msg_list(GList *msg_list, guint flags)
862 {
863 GList *msgout_list = NULL;
864 GList *msg_node;
865 GList *local_msgout_list = NULL;
866 GList *remote_msgout_list = NULL;
867 GList *msgout_node;
868 GList *alias_table = NULL;
869 GList *globalias_table = NULL;
870 gboolean ok = TRUE;
872 /* create msgout_list */
873 foreach(msg_list, msg_node) {
874 message *msg = (message *) msg_node->data;
875 msgout_list = g_list_append(msgout_list, create_msg_out(msg));
876 }
878 if (conf.globalias_file) {
879 globalias_table = table_read(conf.globalias_file, ':');
880 }
881 if (conf.alias_file) {
882 alias_table = table_read(conf.alias_file, ':');
883 }
885 /* sort messages for different deliveries */
886 foreach(msgout_list, msgout_node) {
887 msg_out *msgout = (msg_out *) (msgout_node->data);
888 GList *rcpt_list;
889 GList *local_rcpt_list = NULL;
890 GList *other_rcpt_list = NULL;
892 if (!spool_lock(msgout->msg->uid)) {
893 DEBUG(5) debugf("spool_lock(%s) failed.\n",
894 msgout->msg->uid);
895 continue;
896 }
897 DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid);
899 rcpt_list = g_list_copy(msgout->msg->rcpt_list);
900 if (conf.log_user) {
901 address *addr = create_address_qualified(conf.log_user,
902 TRUE, conf.host_name);
903 if (addr) {
904 rcpt_list = g_list_prepend(rcpt_list, addr);
905 } else {
906 logwrite(LOG_ALERT, "invalid log_user "
907 "address `%s', ignoring\n",
908 conf.log_user);
909 }
910 }
911 if (globalias_table) {
912 GList *globaliased_rcpt_list;
914 DEBUG(5) debugf("Doing globalias expansion\n");
915 globaliased_rcpt_list = alias_expand(globalias_table,
916 rcpt_list,
917 msgout->msg->non_rcpt_list, 1);
918 g_list_free(rcpt_list);
919 rcpt_list = globaliased_rcpt_list;
920 }
921 if (alias_table) {
922 GList *aliased_rcpt_list;
924 DEBUG(5) debugf("Doing alias expansion\n");
925 aliased_rcpt_list = alias_expand(alias_table,
926 rcpt_list,
927 msgout->msg->non_rcpt_list, 0);
928 g_list_free(rcpt_list);
929 rcpt_list = aliased_rcpt_list;
930 }
932 /* split_rcpts(rcpt_list, NULL, &local_rcpt_list, NULL,
933 ** &other_rcpt_list); */
934 local_rcpt_list = local_rcpts(rcpt_list);
935 other_rcpt_list = remote_rcpts(rcpt_list);
936 g_list_free(rcpt_list);
938 /* local recipients */
939 if ((flags & DLVR_LOCAL) && local_rcpt_list) {
940 msg_out *local_msgout = clone_msg_out(msgout);
941 local_msgout->rcpt_list = local_rcpt_list;
942 local_msgout_list = g_list_append(local_msgout_list,
943 local_msgout);
944 }
946 /* remote recipients, requires online delivery */
947 if ((flags & DLVR_ONLINE) && other_rcpt_list) {
948 msg_out *remote_msgout = clone_msg_out(msgout);
949 remote_msgout->rcpt_list = other_rcpt_list;
950 remote_msgout_list = g_list_append(remote_msgout_list,
951 remote_msgout);
952 }
953 }
955 if (alias_table) {
956 destroy_table(alias_table);
957 }
958 if (globalias_table) {
959 destroy_table(globalias_table);
960 }
962 /* process local/remote msgout lists -> delivery */
964 if (local_msgout_list) {
965 DEBUG(5) debugf("local_msgout_list\n");
966 foreach(local_msgout_list, msgout_node) {
967 msg_out *msgout = (msg_out *) (msgout_node->data);
968 if (!deliver_local(msgout)) {
969 ok = FALSE;
970 }
971 }
972 destroy_msg_out_list(local_msgout_list);
973 }
975 if (remote_msgout_list) {
976 DEBUG(5) debugf("remote_msgout_list\n");
977 deliver_remote(remote_msgout_list);
978 destroy_msg_out_list(remote_msgout_list);
979 }
981 /* unlock spool files */
982 foreach(msgout_list, msgout_node) {
983 msg_out *msgout = (msg_out *) (msgout_node->data);
984 if (spool_unlock(msgout->msg->uid)) {
985 DEBUG(5) debugf("spool_unlock(%s)\n",
986 msgout->msg->uid);
987 } else {
988 DEBUG(5) debugf("spool_unlock(%s) failed.\n",
989 msgout->msg->uid);
990 }
991 }
992 destroy_msg_out_list(msgout_list);
994 return ok;
995 }
997 /*
998 ** deliver() is called when a message has just been received
999 ** (mode_accept and smtp_in) and should be delivered immediately
1000 ** (neither -odq nor do_queue). Only this one message will be tried to
1001 ** deliver then.
1002 */
1003 gboolean
1004 deliver(message *msg)
1005 {
1006 gboolean ok;
1007 GList *msg_list = g_list_append(NULL, msg);
1009 ok = deliver_msg_list(msg_list, DLVR_ALL);
1010 g_list_free(msg_list);
1012 return ok;
1013 }