meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2002 Oliver Kurth meillo@224: Copyright (C) 2008, 2010 markus schnalke meillo@0: meillo@0: This program is free software; you can redistribute it and/or modify meillo@0: it under the terms of the GNU General Public License as published by meillo@0: the Free Software Foundation; either version 2 of the License, or meillo@0: (at your option) any later version. meillo@0: meillo@0: This program is distributed in the hope that it will be useful, meillo@0: but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@0: GNU General Public License for more details. meillo@0: meillo@0: You should have received a copy of the GNU General Public License meillo@0: along with this program; if not, write to the Free Software meillo@0: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: meillo@0: #include meillo@0: #include meillo@0: #include meillo@0: meillo@15: #include "masqmail.h" meillo@15: #include "smtp_out.h" meillo@15: meillo@0: /* collect failed/defered rcpts for failure/warning messages */ meillo@15: /* returns TRUE if either there are no failures or a failure message has been successfully sent */ meillo@10: gboolean meillo@10: delivery_failures(message * msg, GList * rcpt_list, gchar * err_fmt, ...) meillo@0: { meillo@10: gboolean ok_fail = TRUE, ok_warn = TRUE; meillo@10: time_t now = time(NULL); meillo@0: meillo@10: GList *failed_list = NULL, *defered_list = NULL, *rcpt_node; meillo@10: va_list args; meillo@10: va_start(args, err_fmt); meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: meillo@10: if (addr_is_defered(rcpt)) { meillo@10: if ((now - msg->received_time) >= conf.max_defer_time) { meillo@10: addr_mark_failed(rcpt); meillo@280: } else { meillo@10: defered_list = g_list_prepend(defered_list, rcpt); meillo@280: } meillo@10: } meillo@280: if (addr_is_failed(rcpt)) { meillo@10: failed_list = g_list_prepend(failed_list, rcpt); meillo@280: } meillo@10: } meillo@280: if (failed_list) { meillo@10: ok_fail = fail_msg(msg, conf.errmsg_file, failed_list, err_fmt, args); meillo@10: g_list_free(failed_list); meillo@10: } meillo@280: if (defered_list) { meillo@10: ok_warn = warn_msg(msg, conf.warnmsg_file, defered_list, err_fmt, args); meillo@10: g_list_free(defered_list); meillo@10: } meillo@10: va_end(args); meillo@10: return ok_fail && ok_warn; meillo@0: } meillo@0: meillo@10: static gint meillo@10: _g_list_strcasecmp(gconstpointer a, gconstpointer b) meillo@0: { meillo@10: return (gint) strcasecmp(a, b); meillo@0: } meillo@0: meillo@10: gboolean meillo@280: deliver_local_mbox(message* msg, GList* hdr_list, address* rcpt, address* env_addr) meillo@280: { meillo@280: DEBUG(1) debugf("attempting to deliver %s with mbox\n", msg->uid); meillo@280: if (append_file(msg, hdr_list, rcpt->local_part)) { meillo@280: if (env_addr != rcpt) { meillo@280: 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); meillo@280: } else { meillo@280: logwrite(LOG_NOTICE, "%s => <%s@%s> with mbox\n", msg->uid, rcpt->local_part, rcpt->domain); meillo@280: } meillo@280: addr_mark_delivered(rcpt); meillo@280: return TRUE; meillo@280: } meillo@280: meillo@280: /* prevents 'Resource temporarily unavailable (11)' */ meillo@280: if (errno != EAGAIN) { meillo@280: addr_mark_failed(rcpt); meillo@280: } else { meillo@280: addr_mark_defered(rcpt); meillo@280: } meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: gboolean meillo@280: deliver_local_pipe(message* msg, GList* hdr_list, address* rcpt, address* env_addr) meillo@280: { meillo@280: guint flags; meillo@280: meillo@280: DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid); meillo@280: meillo@280: flags = (conf.pipe_fromline) ? MSGSTR_FROMLINE : 0; meillo@280: flags |= (conf.pipe_fromhack) ? MSGSTR_FROMHACK : 0; meillo@280: if (pipe_out(msg, hdr_list, rcpt, &(rcpt->local_part[1]), flags)) { meillo@280: logwrite(LOG_NOTICE, "%s => %s <%s@%s> with pipe\n", meillo@280: msg->uid, rcpt->local_part, env_addr->local_part, env_addr->domain); meillo@280: addr_mark_delivered(rcpt); meillo@280: return TRUE; meillo@280: } meillo@280: meillo@280: if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) { meillo@280: addr_mark_failed(rcpt); meillo@280: } else { meillo@280: addr_mark_defered(rcpt); meillo@280: /* has no effect yet, except that mail remains in spool */ meillo@280: } meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: gboolean meillo@280: deliver_local_mda(message* msg, GList* hdr_list, address* rcpt, address* env_addr) meillo@280: { meillo@280: gboolean ok = FALSE; meillo@280: gchar *cmd = g_malloc(256); meillo@280: GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt); meillo@280: guint flags; meillo@280: meillo@280: DEBUG(1) debugf("attempting to deliver %s with mda\n", msg->uid); meillo@280: meillo@280: if (!expand(var_table, conf.mda, cmd, 256)) { meillo@280: logwrite(LOG_ALERT, "could not expand string %s\n", conf.mda); meillo@280: destroy_table(var_table); meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: flags = (conf.mda_fromline) ? MSGSTR_FROMLINE : 0; meillo@280: flags |= (conf.mda_fromhack) ? MSGSTR_FROMHACK : 0; meillo@280: if (pipe_out(msg, hdr_list, rcpt, cmd, flags)) { meillo@280: logwrite(LOG_NOTICE, "%s => %s@%s with mda (cmd = '%s')\n", meillo@280: msg->uid, rcpt->local_part, rcpt->domain, cmd); meillo@280: addr_mark_delivered(rcpt); meillo@280: ok = TRUE; meillo@280: } else if ((errno != (1024 + EX_TEMPFAIL)) && (errno != EAGAIN)) { meillo@280: addr_mark_failed(rcpt); meillo@280: } else { meillo@280: addr_mark_defered(rcpt); meillo@280: /* has no effect yet, except that mail remains in spool */ meillo@280: } meillo@280: meillo@280: destroy_table(var_table); meillo@280: return ok; meillo@280: } meillo@280: meillo@280: gboolean meillo@10: deliver_local(msg_out * msgout) meillo@0: { meillo@10: message *msg = msgout->msg; meillo@10: GList *rcpt_list = msgout->rcpt_list; meillo@10: GList *rcpt_node; meillo@280: gboolean ok = FALSE, flag = FALSE, ok_fail = FALSE; meillo@0: meillo@10: DEBUG(5) debugf("deliver_local entered\n"); meillo@0: meillo@10: flag = (msg->data_list == NULL); meillo@280: if (flag && !spool_read_data(msg)) { meillo@280: logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid); meillo@280: return FALSE; meillo@10: } meillo@0: meillo@10: for (rcpt_node = g_list_first(rcpt_list); rcpt_node; rcpt_node = g_list_next(rcpt_node)) { meillo@10: GList *hdr_list; meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: address *env_addr = addr_find_ancestor(rcpt); meillo@10: address *ret_path = msg->return_path; meillo@10: header *retpath_hdr, *envto_hdr; meillo@0: meillo@280: /* we need a private copy of the hdr list because we add headers meillo@280: here that belong to the rcpt only. g_list_copy copies only meillo@280: the nodes, so it is safe to g_list_free it */ meillo@10: hdr_list = g_list_copy(msg->hdr_list); meillo@10: retpath_hdr = create_header(HEAD_ENVELOPE_TO, "Envelope-to: %s\n", addr_string(env_addr)); meillo@10: envto_hdr = create_header(HEAD_RETURN_PATH, "Return-path: %s\n", addr_string(ret_path)); meillo@0: meillo@10: hdr_list = g_list_prepend(hdr_list, envto_hdr); meillo@10: hdr_list = g_list_prepend(hdr_list, retpath_hdr); meillo@0: meillo@10: if (rcpt->local_part[0] == '|') { meillo@280: /* probably for expanded aliases, but why not done meillo@280: like with the mda? //meillo 2010-12-06 */ meillo@280: if (deliver_local_pipe(msg, hdr_list, rcpt, env_addr)) { meillo@10: ok = TRUE; meillo@10: } meillo@10: } else { meillo@10: /* figure out which mailbox type should be used for this user */ meillo@10: gchar *user = rcpt->local_part; meillo@10: gchar *mbox_type = conf.mbox_default; meillo@0: meillo@280: if (g_list_find_custom (conf.mbox_users, user, _g_list_strcasecmp)) { meillo@10: mbox_type = "mbox"; meillo@280: } else if (g_list_find_custom (conf.mda_users, user, _g_list_strcasecmp)) { meillo@10: mbox_type = "mda"; meillo@280: } meillo@10: meillo@10: if (strcmp(mbox_type, "mbox") == 0) { meillo@280: if (deliver_local_mbox(msg, hdr_list, rcpt, env_addr)) { meillo@280: ok = TRUE; meillo@280: } meillo@280: } else if (strcmp(mbox_type, "mda") == 0) { meillo@280: if (conf.mda) { meillo@280: if (deliver_local_mda(msg, hdr_list, rcpt, env_addr)) { meillo@280: ok = TRUE; meillo@10: } meillo@10: } else { meillo@280: logwrite(LOG_ALERT, "mbox type is mda, but no mda command given in configuration\n"); meillo@10: } meillo@10: meillo@280: } else { meillo@10: logwrite(LOG_ALERT, "unknown mbox type '%s'\n", mbox_type); meillo@280: } meillo@10: } meillo@0: meillo@10: destroy_header(retpath_hdr); meillo@10: destroy_header(envto_hdr); meillo@0: meillo@10: g_list_free(hdr_list); meillo@10: } meillo@10: ok_fail = delivery_failures(msg, rcpt_list, "%s (%d)", ext_strerror(errno), errno); meillo@0: meillo@280: if (flag) { meillo@10: msg_free_data(msg); meillo@280: } meillo@280: if (ok || ok_fail) { meillo@10: deliver_finish(msgout); meillo@280: } meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@315: /* make a list of rcpt's of a message that are local meillo@315: return a new copy of the list */ meillo@10: void meillo@10: msg_rcptlist_local(GList * rcpt_list, GList ** p_local_list, GList ** p_nonlocal_list) meillo@0: { meillo@10: GList *rcpt_node; meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: GList *dom_node; meillo@0: meillo@10: DEBUG(5) debugf("checking address %s\n", rcpt->address); meillo@0: meillo@10: /* search for local host list: */ meillo@10: foreach(conf.local_hosts, dom_node) { meillo@10: if (strcasecmp(dom_node->data, rcpt->domain) == 0) { meillo@10: *p_local_list = g_list_append(*p_local_list, rcpt); meillo@10: DEBUG(5) debugf("<%s@%s> is local\n", rcpt->local_part, rcpt->domain); meillo@10: break; meillo@10: } else { meillo@10: *p_nonlocal_list = g_list_append(*p_nonlocal_list, rcpt); meillo@10: } meillo@10: } meillo@10: } meillo@0: } meillo@0: meillo@10: gboolean meillo@10: deliver_msglist_host_pipe(connect_route * route, GList * msgout_list, gchar * host, GList * res_list) meillo@0: { meillo@10: gboolean ok = TRUE; meillo@10: GList *msgout_node; meillo@0: meillo@10: DEBUG(5) debugf("deliver_msglist_host_pipe entered\n"); meillo@0: meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@280: gboolean flag, ok_fail = FALSE; meillo@10: message *msg = msgout->msg; meillo@10: GList *rcpt_node, *rcpt_list = msgout->rcpt_list; meillo@0: meillo@10: DEBUG(1) debugf("attempting to deliver %s with pipe\n", msg->uid); meillo@0: meillo@10: flag = (msg->data_list == NULL); meillo@280: if (flag && !spool_read_data(msg)) { meillo@280: logwrite(LOG_ALERT, "could not open data spool file for %s\n", msg->uid); meillo@280: continue; meillo@10: } meillo@0: meillo@10: ok = FALSE; meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: gchar *cmd = g_malloc(256); meillo@10: GList *var_table = var_table_rcpt(var_table_msg(NULL, msg), rcpt); meillo@0: meillo@10: DEBUG(1) debugf("attempting to deliver %s to %s@%s with pipe\n", msg->uid, rcpt->local_part, rcpt->domain); meillo@10: meillo@280: if (!expand(var_table, route->pipe, cmd, 256)) { meillo@280: logwrite(LOG_ALERT, "could not expand string %s\n", route->pipe); meillo@280: } else { meillo@10: meillo@10: if (pipe_out(msg, msg->hdr_list, rcpt, cmd, (route->pipe_fromline ? MSGSTR_FROMLINE : 0) meillo@10: | (route->pipe_fromhack ? MSGSTR_FROMHACK : 0))) { meillo@10: logwrite(LOG_NOTICE, "%s => %s@%s with pipe (cmd = '%s')\n", meillo@10: msg->uid, rcpt->local_part, rcpt->domain, cmd); meillo@10: addr_mark_delivered(rcpt); meillo@10: ok = TRUE; meillo@10: } else { meillo@10: logwrite(LOG_ALERT, "pipe_out '%s' failed\n", route->pipe); meillo@10: meillo@10: if (route->connect_error_fail) { meillo@10: addr_mark_failed(rcpt); meillo@10: } else { meillo@10: addr_mark_defered(rcpt); meillo@10: } meillo@10: } meillo@280: } meillo@10: meillo@10: destroy_table(var_table); meillo@10: } meillo@10: ok_fail = delivery_failures(msg, rcpt_list, "%s", strerror(errno)); meillo@10: meillo@280: if (flag) { meillo@10: msg_free_data(msg); meillo@280: } meillo@280: if (ok || ok_fail) { meillo@10: deliver_finish(msgout); meillo@280: } meillo@0: } meillo@0: meillo@10: return ok; meillo@0: } meillo@0: meillo@280: /* deliver list of messages to one host and finishes them if the message was meillo@280: delivered to at least one rcpt. meillo@10: Returns TRUE if at least one msg was delivered to at least one rcpt. meillo@0: */ meillo@10: gboolean meillo@10: deliver_msglist_host_smtp(connect_route * route, GList * msgout_list, gchar * host, GList * res_list) meillo@0: { meillo@10: gboolean ok = FALSE; meillo@10: GList *msgout_node; meillo@10: smtp_base *psb; meillo@178: gint port = 25; meillo@0: meillo@10: /* paranoid check: */ meillo@280: if (!msgout_list) { meillo@10: logwrite(LOG_ALERT, "Ooops: empty list of messages in deliver_msglist_host()\n"); meillo@10: return FALSE; meillo@10: } meillo@10: meillo@280: if (!host) { meillo@179: /* XXX: what if mail_host isn't set? Is this possible? */ meillo@10: host = route->mail_host->address; meillo@10: port = route->mail_host->port; meillo@178: } meillo@10: meillo@280: if (route->wrapper) { meillo@280: psb = smtp_out_open_child(route->wrapper); meillo@280: } else { meillo@280: psb = smtp_out_open(host, port, res_list); meillo@280: } meillo@0: meillo@280: if (!psb) { meillo@10: /* smtp_out_open() failed */ meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@10: GList *rcpt_node; meillo@10: meillo@280: for (rcpt_node = g_list_first(msgout->rcpt_list); meillo@280: rcpt_node; meillo@280: rcpt_node = g_list_next(rcpt_node)) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@280: gboolean ret = FALSE; meillo@10: meillo@10: addr_unmark_delivered(rcpt); meillo@10: if (route->connect_error_fail) { meillo@10: addr_mark_failed(rcpt); meillo@10: } else { meillo@10: addr_mark_defered(rcpt); meillo@10: } meillo@280: if (route->wrapper) { meillo@280: ret = delivery_failures(msgout->msg, msgout->rcpt_list, "could not open wrapper:\n\t%s", strerror(errno)); meillo@280: } else { meillo@280: 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)); meillo@280: } meillo@280: if (ret) { meillo@10: deliver_finish(msgout); meillo@280: } meillo@10: } meillo@10: } meillo@280: return ok; meillo@0: } meillo@280: meillo@280: meillo@280: if (route->wrapper) { meillo@280: /* it seems as if the remote_host is only set for logging meillo@280: /* XXX: this could probably be moved into smtp_out_open_child() */ meillo@280: psb->remote_host = host; meillo@280: } meillo@280: meillo@280: set_heloname(psb, route->helo_name ? route->helo_name : conf.host_name, route->do_correct_helo); meillo@280: meillo@280: #ifdef ENABLE_AUTH meillo@280: if ((route->auth_name) && (route->auth_login) && (route->auth_secret)) { meillo@280: set_auth(psb, route->auth_name, route->auth_login, route->auth_secret); meillo@280: } meillo@280: #endif meillo@280: if (!smtp_out_init(psb, route->instant_helo)) { meillo@280: /* smtp_out_init() failed */ meillo@280: if ((psb->error==smtp_fail) || (psb->error==smtp_trylater) || (psb->error==smtp_syntax)) { meillo@280: smtp_out_quit(psb); meillo@280: meillo@280: foreach(msgout_list, msgout_node) { meillo@280: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@280: smtp_out_mark_rcpts(psb, msgout->rcpt_list); meillo@280: meillo@280: if (delivery_failures(msgout->msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer)) { meillo@280: deliver_finish(msgout); meillo@280: } meillo@280: } meillo@280: } meillo@280: destroy_smtpbase(psb); meillo@280: return ok; meillo@280: } meillo@280: meillo@280: if (!route->do_pipelining) { meillo@280: psb->use_pipelining = FALSE; meillo@280: } meillo@280: meillo@280: foreach(msgout_list, msgout_node) { meillo@280: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@280: gboolean flag, ok_msg = FALSE, ok_fail = FALSE; meillo@280: message *msg = msgout->msg; meillo@280: meillo@280: /* we may have to read the data at this point and remember if we did */ meillo@280: flag = (msg->data_list == NULL); meillo@280: if (flag && !spool_read_data(msg)) { meillo@280: logwrite(LOG_ALERT, "could not open data spool file %s\n", msg->uid); meillo@280: break; meillo@280: } meillo@280: meillo@280: smtp_out_msg(psb, msg, msgout->return_path, msgout->rcpt_list, msgout->hdr_list); meillo@280: meillo@280: ok_fail = delivery_failures(msg, msgout->rcpt_list, "while connected with %s, the server replied\n\t%s", host, psb->buffer); meillo@280: meillo@280: if ((psb->error == smtp_eof) || (psb->error == smtp_timeout)) { meillo@280: /* connection lost */ meillo@280: break; meillo@280: } else if (psb->error != smtp_ok) { meillo@280: if (g_list_next(msgout_node) && !smtp_out_rset(psb)) { meillo@280: break; meillo@280: } meillo@280: } meillo@280: ok_msg = (psb->error == smtp_ok); meillo@280: meillo@280: if (flag) { meillo@280: msg_free_data(msg); meillo@280: } meillo@280: if (ok_msg) { meillo@280: ok = TRUE; meillo@280: } meillo@280: if (ok_msg || ok_fail) { meillo@280: deliver_finish(msgout); meillo@280: } meillo@280: } meillo@280: if (psb->error == smtp_ok || (psb->error == smtp_fail) meillo@280: || (psb->error == smtp_trylater) || (psb->error == smtp_syntax)) { meillo@280: smtp_out_quit(psb); meillo@280: } meillo@280: destroy_smtpbase(psb); meillo@10: return ok; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: deliver_msglist_host(connect_route * route, GList * msgout_list, gchar * host, GList * res_list) meillo@0: { meillo@0: meillo@311: if (route->pipe) { meillo@311: DEBUG(5) debugf("with pipe\n"); meillo@10: return deliver_msglist_host_pipe(route, msgout_list, host, res_list); meillo@10: } else { meillo@311: DEBUG(5) debugf("with smtp\n"); meillo@10: return deliver_msglist_host_smtp(route, msgout_list, host, res_list); meillo@10: } meillo@0: } meillo@0: meillo@0: /* meillo@0: delivers messages in msgout_list using route meillo@0: */ meillo@10: gboolean meillo@10: deliver_route_msgout_list(connect_route * route, GList * msgout_list) meillo@0: { meillo@10: gboolean ok = FALSE; meillo@280: GList *mo_ph_list; meillo@280: GList *mo_ph_node; meillo@0: meillo@15: DEBUG(5) debugf("deliver_route_msgout_list entered, route->name = %s\n", route->name); meillo@0: meillo@179: if (route->mail_host) { meillo@179: /* this is easy... deliver everything to a smart host for relay */ meillo@280: return deliver_msglist_host(route, msgout_list, NULL, route->resolve_list); meillo@280: } meillo@280: meillo@280: /* this is not easy... */ meillo@280: meillo@280: mo_ph_list = route_msgout_list(route, msgout_list); meillo@280: /* okay, now we have ordered our messages by the hosts. */ meillo@280: if (!mo_ph_list) { meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: /* TODO: It would be nice to be able to fork for each host. meillo@280: We cannot do that yet because of complications with finishing the meillo@280: messages. Threads could be a solution because they use the same meillo@280: memory. But we are not thread safe yet... meillo@280: */ meillo@280: foreach(mo_ph_list, mo_ph_node) { meillo@280: msgout_perhost *mo_ph = (msgout_perhost *) (mo_ph_node->data); meillo@280: if (deliver_msglist_host(route, mo_ph->msgout_list, mo_ph->host, route->resolve_list)) { meillo@10: ok = TRUE; meillo@10: } meillo@280: destroy_msgout_perhost(mo_ph); meillo@10: } meillo@280: g_list_free(mo_ph_list); meillo@10: return ok; meillo@0: } meillo@0: meillo@0: /* meillo@0: calls route_prepare_msg() meillo@15: delivers messages in msg_list using route by calling deliver_route_msgout_list() meillo@0: */ meillo@10: gboolean meillo@10: deliver_route_msg_list(connect_route * route, GList * msgout_list) meillo@0: { meillo@10: GList *msgout_list_deliver = NULL; meillo@10: GList *msgout_node; meillo@10: gboolean ok = TRUE; meillo@0: meillo@10: DEBUG(6) debugf("deliver_route_msg_list()\n"); meillo@0: meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@10: msg_out *msgout_cloned = clone_msg_out(msgout); meillo@10: GList *rcpt_list_non_delivered = NULL; meillo@10: GList *rcpt_node; meillo@0: meillo@280: /* we have to delete already delivered rcpt's because a meillo@280: previous route may have delivered to it */ meillo@10: foreach(msgout_cloned->rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@280: /* failed addresses already have been bounced; meillo@280: there should be a better way to handle those. */ meillo@10: if (!addr_is_delivered(rcpt) && !addr_is_failed(rcpt) meillo@280: && !(rcpt->flags & ADDR_FLAG_LAST_ROUTE)) { meillo@10: rcpt_list_non_delivered = g_list_append(rcpt_list_non_delivered, rcpt); meillo@280: } meillo@10: } meillo@10: g_list_free(msgout_cloned->rcpt_list); meillo@10: msgout_cloned->rcpt_list = rcpt_list_non_delivered; meillo@0: meillo@280: if (!msgout_cloned->rcpt_list) { meillo@280: destroy_msg_out(msgout_cloned); meillo@280: continue; meillo@280: } meillo@0: meillo@315: /* filter by allowed return paths (= envelope sender) */ meillo@280: if (!route_is_allowed_mail_local(route, msgout->msg->return_path) meillo@315: || !route_is_allowed_return_path(route, msgout->msg->return_path)) { meillo@280: destroy_msg_out(msgout_cloned); meillo@280: continue; meillo@280: } meillo@0: meillo@315: /* filter by allowed rcpt addrs (= envelope rcpts) */ meillo@280: GList *rcpt_list_allowed = NULL, *rcpt_list_notallowed = NULL; meillo@280: msg_rcptlist_route(route, msgout_cloned->rcpt_list, &rcpt_list_allowed, &rcpt_list_notallowed); meillo@0: meillo@280: if (!rcpt_list_allowed) { meillo@280: destroy_msg_out(msgout_cloned); meillo@280: continue; meillo@280: } meillo@280: logwrite(LOG_NOTICE, "%s using '%s'\n", msgout->msg->uid, route->name); meillo@10: meillo@280: g_list_free(msgout_cloned->rcpt_list); meillo@280: msgout_cloned->rcpt_list = rcpt_list_allowed; meillo@280: meillo@280: if (route->last_route) { meillo@280: GList *rcpt_node; meillo@280: foreach(msgout_cloned->rcpt_list, rcpt_node) { meillo@280: address *rcpt = (address *) (rcpt_node->data); meillo@280: rcpt->flags |= ADDR_FLAG_LAST_ROUTE; meillo@280: } meillo@280: } meillo@280: meillo@280: route_prepare_msgout(route, msgout_cloned); meillo@280: msgout_list_deliver = g_list_append(msgout_list_deliver, msgout_cloned); meillo@10: } meillo@10: meillo@280: if (msgout_list_deliver) { meillo@280: if (deliver_route_msgout_list(route, msgout_list_deliver)) { meillo@10: ok = TRUE; meillo@280: } meillo@10: destroy_msg_out_list(msgout_list_deliver); meillo@10: } meillo@10: return ok; meillo@0: } meillo@0: meillo@0: /* copy pointers of delivered addresses to the msg's non_rcpt_list, meillo@0: to make sure that they will not be delivered again. meillo@0: */ meillo@10: void meillo@10: update_non_rcpt_list(msg_out * msgout) meillo@0: { meillo@10: GList *rcpt_node; meillo@10: message *msg = msgout->msg; meillo@0: meillo@10: foreach(msgout->rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@280: if (addr_is_delivered(rcpt) || addr_is_failed(rcpt)) { meillo@10: msg->non_rcpt_list = g_list_append(msg->non_rcpt_list, rcpt); meillo@280: } meillo@10: } meillo@0: } meillo@0: meillo@280: /* after delivery attempts, we check if there are any rcpt addresses left in meillo@280: the message. If all addresses have been completed, the spool files will be meillo@280: deleted, otherwise the header spool will be written back. We never changed meillo@280: the data spool, so there is no need to write that back. meillo@0: meillo@0: returns TRUE if all went well. meillo@0: */ meillo@10: gboolean meillo@10: deliver_finish(msg_out * msgout) meillo@0: { meillo@10: GList *rcpt_node; meillo@10: message *msg = msgout->msg; meillo@10: gboolean finished = TRUE; meillo@0: meillo@10: update_non_rcpt_list(msgout); meillo@0: meillo@10: /* we NEVER made copies of the addresses, flags affecting addresses meillo@10: were always set on the original address structs */ meillo@10: foreach(msg->rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@280: if (!addr_is_finished_children(rcpt)) { meillo@10: finished = FALSE; meillo@280: } else { meillo@280: /* if ALL children have been delivered, mark parent as meillo@280: delivered. if there is one or more not delivered, meillo@280: it must have failed, we mark the parent as failed meillo@280: as well. meillo@10: */ meillo@10: if (addr_is_delivered_children(rcpt)) { meillo@10: addr_mark_delivered(rcpt); meillo@10: } else { meillo@10: addr_mark_failed(rcpt); meillo@10: } meillo@10: } meillo@10: } meillo@10: meillo@280: if (finished) { meillo@280: if (spool_delete_all(msg)) { meillo@10: logwrite(LOG_NOTICE, "%s completed.\n", msg->uid); meillo@280: return TRUE; meillo@280: } meillo@280: return FALSE; meillo@10: } meillo@280: meillo@280: /* one not delivered address was found */ meillo@280: if (!spool_write(msg, FALSE)) { meillo@280: logwrite(LOG_ALERT, "could not write back spool header for %s\n", msg->uid); meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: DEBUG(2) debugf("spool header for %s written back.\n", msg->uid); meillo@280: return TRUE; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: deliver_finish_list(GList * msgout_list) meillo@0: { meillo@10: gboolean ok = TRUE; meillo@10: GList *msgout_node; meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@280: if (!deliver_finish(msgout)) { meillo@10: ok = FALSE; meillo@280: } meillo@0: } meillo@10: return ok; meillo@0: } meillo@0: meillo@10: gboolean meillo@10: deliver_msgout_list_online(GList * msgout_list) meillo@10: { meillo@10: GList *rf_list = NULL; meillo@280: gchar *connect_name = NULL; meillo@10: gboolean ok = FALSE; meillo@0: meillo@310: connect_name = online_query(); meillo@280: if (!connect_name) { meillo@280: return FALSE; meillo@10: } meillo@280: meillo@280: /* we are online! */ meillo@280: logwrite(LOG_NOTICE, "detected online configuration %s\n", connect_name); meillo@280: meillo@280: rf_list = (GList *) table_find(conf.connect_routes, connect_name); meillo@280: if (!rf_list) { meillo@280: logwrite(LOG_ALERT, "route list with name '%s' not found.\n", connect_name); meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: GList *route_list = read_route_list(rf_list, FALSE); meillo@280: if (!route_list) { meillo@280: logwrite(LOG_ALERT, "could not read route list '%s'\n", connect_name); meillo@280: return FALSE; meillo@280: } meillo@280: meillo@280: GList *route_node; meillo@280: foreach(route_list, route_node) { meillo@280: connect_route *route = (connect_route *) (route_node->data); meillo@280: /* TODO: ok gets overwritten */ meillo@280: ok = deliver_route_msg_list(route, msgout_list); meillo@280: } meillo@280: destroy_route_list(route_list); meillo@10: return ok; meillo@10: } meillo@0: meillo@10: gboolean meillo@10: deliver_msg_list(GList * msg_list, guint flags) meillo@10: { meillo@10: GList *msgout_list = create_msg_out_list(msg_list); meillo@280: GList *local_msgout_list = NULL; meillo@280: GList *localnet_msgout_list = NULL; meillo@280: GList *other_msgout_list = NULL; meillo@10: GList *msgout_node; meillo@10: GList *alias_table = NULL; meillo@10: gboolean ok = TRUE; meillo@0: meillo@10: if (conf.alias_file) { meillo@17: alias_table = table_read(conf.alias_file, ':'); meillo@10: } meillo@0: meillo@10: /* sort messages for different deliveries */ meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@10: GList *rcpt_list; meillo@10: GList *local_rcpt_list = NULL; meillo@10: GList *localnet_rcpt_list = NULL; meillo@237: GList *other_rcpt_list = NULL; meillo@0: meillo@114: if (!spool_lock(msgout->msg->uid)) { meillo@114: DEBUG(5) debugf("spool_lock(%s) failed.\n", msgout->msg->uid); meillo@10: continue; meillo@114: } meillo@114: DEBUG(5) debugf("spool_lock(%s)\n", msgout->msg->uid); meillo@0: meillo@10: rcpt_list = g_list_copy(msgout->msg->rcpt_list); meillo@10: if (conf.log_user) { meillo@10: address *addr = create_address_qualified(conf.log_user, TRUE, conf.host_name); meillo@280: if (addr) { meillo@10: rcpt_list = g_list_prepend(rcpt_list, addr); meillo@280: } else { meillo@280: logwrite(LOG_ALERT, "invalid log_user address `%s', ignoring\n", conf.log_user); meillo@280: } meillo@10: } meillo@10: if (alias_table) { meillo@10: GList *aliased_rcpt_list; meillo@10: aliased_rcpt_list = alias_expand(alias_table, rcpt_list, msgout->msg->non_rcpt_list); meillo@10: g_list_free(rcpt_list); meillo@10: rcpt_list = aliased_rcpt_list; meillo@10: } meillo@0: meillo@237: split_rcpts(rcpt_list, conf.local_nets, &local_rcpt_list, &localnet_rcpt_list, &other_rcpt_list); meillo@237: g_list_free(rcpt_list); meillo@237: meillo@10: /* local recipients */ meillo@237: if ((flags & DLVR_LOCAL) && local_rcpt_list) { meillo@237: msg_out *local_msgout = clone_msg_out(msgout); meillo@237: local_msgout->rcpt_list = local_rcpt_list; meillo@237: local_msgout_list = g_list_append(local_msgout_list, local_msgout); meillo@10: } meillo@0: meillo@10: /* local net recipients */ meillo@237: if ((flags & DLVR_LAN) && localnet_rcpt_list) { meillo@237: msg_out *localnet_msgout = clone_msg_out(msgout); meillo@237: localnet_msgout->rcpt_list = localnet_rcpt_list; meillo@237: localnet_msgout_list = g_list_append(localnet_msgout_list, localnet_msgout); meillo@10: } meillo@0: meillo@237: /* remote recipients (the rest), requires online delivery */ meillo@237: if ((flags & DLVR_ONLINE) && other_rcpt_list) { meillo@237: msg_out *other_msgout = clone_msg_out(msgout); meillo@237: other_msgout->rcpt_list = other_rcpt_list; meillo@237: other_msgout_list = g_list_append(other_msgout_list, other_msgout); meillo@10: } meillo@10: } meillo@0: meillo@280: if (alias_table) { meillo@10: destroy_table(alias_table); meillo@280: } meillo@0: meillo@10: /* actual delivery */ meillo@237: meillo@237: if (local_msgout_list) { meillo@114: DEBUG(5) debugf("local_msgout_list\n"); meillo@10: foreach(local_msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@280: if (!deliver_local(msgout)) { meillo@10: ok = FALSE; meillo@280: } meillo@10: } meillo@10: destroy_msg_out_list(local_msgout_list); meillo@10: } meillo@0: meillo@237: if (localnet_msgout_list) { meillo@10: GList *route_list = NULL; meillo@10: GList *route_node; meillo@0: meillo@114: DEBUG(5) debugf("localnet_msgout_list\n"); meillo@280: if (conf.local_net_routes) { meillo@10: route_list = read_route_list(conf.local_net_routes, TRUE); meillo@280: } else { meillo@10: route_list = g_list_append(NULL, create_local_route()); meillo@280: } meillo@0: meillo@10: foreach(route_list, route_node) { meillo@10: connect_route *route = (connect_route *) (route_node->data); meillo@280: if (!deliver_route_msg_list(route, localnet_msgout_list)) { meillo@10: ok = FALSE; meillo@280: } meillo@10: } meillo@10: destroy_msg_out_list(localnet_msgout_list); meillo@10: destroy_route_list(route_list); meillo@10: } meillo@10: meillo@237: if (other_msgout_list) { meillo@114: DEBUG(5) debugf("other_msgout_list\n"); meillo@280: if (!deliver_msgout_list_online(other_msgout_list)) { meillo@10: ok = FALSE; meillo@280: } meillo@10: destroy_msg_out_list(other_msgout_list); meillo@10: } meillo@10: meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@114: if (spool_unlock(msgout->msg->uid)) { meillo@114: DEBUG(5) debugf("spool_unlock(%s)\n", msgout->msg->uid); meillo@114: } else { meillo@114: DEBUG(5) debugf("spool_unlock(%s) failed.\n", msgout->msg->uid); meillo@114: } meillo@114: meillo@10: } meillo@10: meillo@10: destroy_msg_out_list(msgout_list); meillo@10: meillo@10: return ok; meillo@0: } meillo@0: meillo@0: /* This function searches in the list of rcpt addresses meillo@0: for local and 'local net' addresses. Remote addresses meillo@0: which are reachable only when online are treated specially meillo@0: in another function. meillo@0: meillo@0: deliver() is called when a message has just been received and should meillo@0: be delivered immediately. meillo@0: */ meillo@10: gboolean meillo@10: deliver(message * msg) meillo@0: { meillo@10: gboolean ok; meillo@10: GList *msg_list = g_list_append(NULL, msg); meillo@0: meillo@10: ok = deliver_msg_list(msg_list, DLVR_ALL); meillo@10: g_list_free(msg_list); meillo@0: meillo@10: return ok; meillo@0: }