meillo@0: /* MasqMail meillo@0: Copyright (C) 1999-2001 Oliver Kurth 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@15: #include meillo@15: meillo@0: #include "masqmail.h" meillo@0: meillo@10: msgout_perhost* meillo@10: create_msgout_perhost(gchar * host) meillo@0: { meillo@10: msgout_perhost *mo_ph = g_malloc(sizeof(msgout_perhost)); meillo@10: if (mo_ph) { meillo@10: mo_ph->host = g_strdup(host); meillo@10: mo_ph->msgout_list = NULL; meillo@10: } meillo@10: return mo_ph; meillo@0: } meillo@0: meillo@10: void meillo@10: destroy_msgout_perhost(msgout_perhost * mo_ph) meillo@0: { meillo@10: GList *mo_node; meillo@0: meillo@10: foreach(mo_ph->msgout_list, mo_node) { meillo@10: msg_out *mo = (msg_out *) (mo_node->data); meillo@10: /* the rcpt_list is owned by the msgout's, but not the rcpt's themselves */ meillo@10: g_list_free(mo->rcpt_list); meillo@10: g_free(mo); meillo@10: } meillo@10: g_list_free(mo_ph->msgout_list); meillo@10: g_free(mo_ph); meillo@0: } meillo@0: meillo@10: void meillo@10: rewrite_headers(msg_out * msgout, connect_route * route) meillo@0: { meillo@10: /* if set_h_from_domain is set, replace domain in all meillo@10: From: headers. meillo@10: */ meillo@10: msgout->hdr_list = g_list_copy(msgout->msg->hdr_list); meillo@0: meillo@10: /* map from addresses */ meillo@10: if (route->map_h_from_addresses != NULL) { meillo@10: GList *hdr_node; meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_FROM) { meillo@10: header *new_hdr = copy_header(hdr); meillo@10: if (map_address_header(new_hdr, route->map_h_from_addresses)) { meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } else meillo@10: g_free(new_hdr); meillo@10: } meillo@10: } meillo@10: } else { meillo@10: /* replace from domain */ meillo@10: if (route->set_h_from_domain != NULL) { meillo@10: GList *hdr_node; meillo@10: meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_FROM) { meillo@10: header *new_hdr = copy_header(hdr); meillo@10: meillo@10: DEBUG(5) debugf("setting From: domain to %s\n", route->set_h_from_domain); meillo@10: if (set_address_header_domain(new_hdr, route->set_h_from_domain)) { meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: DEBUG(6) debugf("header = %s\n", new_hdr->header); meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } else { meillo@15: logwrite(LOG_ALERT, "error in set_address_header_domain(%s, %s)\n", meillo@15: new_hdr->value, route->set_h_from_domain); meillo@10: } meillo@10: } meillo@10: } meillo@10: } meillo@0: } meillo@0: meillo@10: /* map reply-to addresses */ meillo@10: if (route->map_h_reply_to_addresses != NULL) { meillo@10: GList *hdr_node; meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_REPLY_TO) { meillo@10: header *new_hdr = copy_header(hdr); meillo@10: if (map_address_header meillo@10: (new_hdr, route->map_h_reply_to_addresses)) { meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } else meillo@10: g_free(new_hdr); meillo@10: } meillo@10: } meillo@10: } else { meillo@10: /* replace Reply-to domain */ meillo@10: if (route->set_h_reply_to_domain != NULL) { meillo@10: GList *hdr_node; meillo@10: meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_REPLY_TO) { meillo@10: header *new_hdr = copy_header(hdr); meillo@10: meillo@10: set_address_header_domain(new_hdr, route-> set_h_reply_to_domain); meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } meillo@10: } meillo@10: } meillo@0: } meillo@0: meillo@10: /* map Mail-Followup-To addresses */ meillo@10: if (route->map_h_mail_followup_to_addresses != NULL) { meillo@10: GList *hdr_node; meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (strncasecmp(hdr->header, "Mail-Followup-To", 16) == 0) { meillo@10: header *new_hdr = copy_header(hdr); meillo@10: if (map_address_header(new_hdr, route->map_h_mail_followup_to_addresses)) { meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } else meillo@10: g_free(new_hdr); meillo@10: } meillo@10: } meillo@10: } meillo@0: meillo@10: /* set Sender: domain to return_path->domain */ meillo@10: if (route->expand_h_sender_domain) { meillo@10: GList *hdr_node; meillo@0: meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_SENDER) { meillo@10: header *new_hdr = copy_header(hdr); meillo@0: meillo@10: set_address_header_domain(new_hdr, msgout->return_path->domain); meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } meillo@10: } meillo@10: } meillo@0: meillo@10: /* set Sender: domain to return_path->domain */ meillo@10: if (route->expand_h_sender_address) { meillo@10: GList *hdr_node; meillo@0: meillo@10: foreach(msgout->hdr_list, hdr_node) { meillo@10: header *hdr = (header *) (hdr_node->data); meillo@10: if (hdr->id == HEAD_SENDER) { meillo@10: header *new_hdr; meillo@0: meillo@15: new_hdr = create_header(HEAD_SENDER, "Sender: %s@%s\n", meillo@15: msgout->return_path->local_part, msgout->return_path->domain); meillo@10: hdr_node->data = new_hdr; meillo@10: /* we need this list only to carefully free the extra headers: */ meillo@10: msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr); meillo@10: } meillo@10: } meillo@10: } meillo@0: meillo@10: if (msgout->xtra_hdr_list == NULL) { meillo@10: /* nothing was changed */ meillo@10: g_list_free(msgout->hdr_list); meillo@10: msgout->hdr_list = NULL; meillo@10: } meillo@10: DEBUG(5) debugf("rewrite_headers() returning\n"); meillo@0: } meillo@0: meillo@10: void meillo@10: rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list) meillo@0: { meillo@10: GList *rcpt_node; meillo@0: meillo@10: if (rcpt_list == NULL) meillo@10: return; meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: GList *host_node = NULL; meillo@0: meillo@10: foreach(host_list, host_node) { meillo@10: gchar *host = (gchar *) (host_node->data); meillo@10: if (fnmatch(host, rcpt->domain, FNM_CASEFOLD) == 0) meillo@10: break; meillo@10: } meillo@10: if (host_node) { meillo@10: if (p_rcpt_list) meillo@10: *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt); meillo@10: } else { meillo@10: if (p_non_rcpt_list) meillo@10: *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt); meillo@10: } meillo@0: meillo@10: } meillo@0: } meillo@0: meillo@10: void meillo@10: rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list) meillo@0: { meillo@10: GList *rcpt_node; meillo@0: meillo@10: if (rcpt_list == NULL) meillo@10: return; meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = (address *) (rcpt_node->data); meillo@10: if (addr_is_local(rcpt)) { meillo@10: if (p_rcpt_list) meillo@10: *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt); meillo@10: } else { meillo@10: if (p_non_rcpt_list) meillo@10: *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt); meillo@10: } meillo@0: meillo@10: } meillo@0: } meillo@0: meillo@10: static gint meillo@10: _g_list_addrcmp(gconstpointer a, gconstpointer b) meillo@0: { meillo@10: return addr_match((address *) a, (address *) b); meillo@0: } meillo@0: meillo@10: gboolean meillo@10: route_is_allowed_return_path(connect_route * route, address * ret_path) meillo@0: { meillo@10: if (route->not_allowed_return_paths != NULL) { meillo@10: if (g_list_find_custom(route->not_allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) { meillo@10: return FALSE; meillo@10: } meillo@10: } meillo@10: if (route->allowed_return_paths != NULL) { meillo@10: if (g_list_find_custom(route->allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) { meillo@10: return TRUE; meillo@10: } else { meillo@10: return FALSE; meillo@10: } meillo@10: } meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: static gint meillo@10: _g_list_strcmp(gconstpointer a, gconstpointer b) meillo@0: { meillo@10: return (gint) strcmp(a, b); meillo@0: } meillo@0: meillo@10: gboolean meillo@10: route_is_allowed_mail_local(connect_route * route, address * ret_path) meillo@0: { meillo@10: gchar *loc_part = ret_path->local_part; meillo@0: meillo@10: if (route->not_allowed_mail_locals != NULL) { meillo@10: if (g_list_find_custom(route->not_allowed_mail_locals, loc_part, _g_list_strcmp) != NULL) meillo@10: return FALSE; meillo@10: } meillo@10: if (route->allowed_mail_locals != NULL) { meillo@10: if (g_list_find_custom(route->allowed_mail_locals, loc_part, _g_list_strcmp) != NULL) meillo@10: return TRUE; meillo@10: else meillo@10: return FALSE; meillo@10: } meillo@10: return TRUE; meillo@0: } meillo@0: meillo@10: /* meillo@0: Make lists of matching/not matching rcpts. meillo@0: Local domains are NOT regared here, these should be sorted out previously meillo@0: */ meillo@10: void meillo@10: msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list) meillo@0: { meillo@10: GList *tmp_list = NULL; meillo@10: /* sort out those domains that can be sent over this connection: */ meillo@10: if (route->allowed_rcpt_domains) { meillo@10: DEBUG(5) debugf("testing for route->allowed_rcpt_domains\n"); meillo@10: rcptlist_with_one_of_hostlist(rcpt_list, route->allowed_rcpt_domains, &tmp_list, p_non_rcpt_list); meillo@10: } else { meillo@10: DEBUG(5) debugf("route->allowed_rcpt_domains == NULL\n"); meillo@10: tmp_list = g_list_copy(rcpt_list); meillo@10: } meillo@0: meillo@10: /* sort out those domains that cannot be sent over this connection: */ meillo@10: rcptlist_with_one_of_hostlist(tmp_list, route->not_allowed_rcpt_domains, p_non_rcpt_list, p_rcpt_list); meillo@10: g_list_free(tmp_list); meillo@0: } meillo@0: meillo@10: msg_out* meillo@10: route_prepare_msgout(connect_route * route, msg_out * msgout) meillo@0: { meillo@10: message *msg = msgout->msg; meillo@10: GList *rcpt_list = msgout->rcpt_list; meillo@0: meillo@10: if (rcpt_list != NULL) { meillo@10: /* found a few */ meillo@10: DEBUG(5) { meillo@10: GList *node; meillo@10: debugf("rcpts for routed delivery, route = %s, id = %s\n", route->name, msg->uid); meillo@10: foreach(rcpt_list, node) { meillo@10: address *rcpt = (address *) (node->data); meillo@114: debugf(" rcpt for routed delivery: <%s@%s>\n", meillo@114: rcpt->local_part, rcpt->domain); meillo@10: } meillo@10: } meillo@0: meillo@15: /* rewrite return path if there is a table, use that meillo@10: if an address is found and if it has a domain, use that meillo@10: */ meillo@10: if (route->map_return_path_addresses) { meillo@10: address *ret_path = NULL; meillo@10: DEBUG(5) debugf("looking up %s in map_return_path_addresses\n", msg->return_path->local_part); meillo@10: ret_path = (address *) table_find_fnmatch(route->map_return_path_addresses, msg->return_path->local_part); meillo@10: if (ret_path) { meillo@10: DEBUG(5) debugf("found <%s@%s>\n", ret_path->local_part, ret_path->domain); meillo@10: if (ret_path->domain == NULL) meillo@10: ret_path->domain = route->set_return_path_domain meillo@10: ? route->set_return_path_domain meillo@10: : msg->return_path->domain; meillo@10: msgout->return_path = copy_address(ret_path); meillo@10: } meillo@10: } meillo@10: if (msgout->return_path == NULL) { meillo@10: DEBUG(5) debugf("setting return path to %s\n", route->set_return_path_domain); meillo@10: msgout->return_path = copy_modify_address(msg->return_path, NULL, route->set_return_path_domain); meillo@10: } meillo@10: rewrite_headers(msgout, route); meillo@10: meillo@10: return msgout; meillo@10: } meillo@10: return NULL; meillo@0: } meillo@0: meillo@0: /* put msgout's is msgout_list into bins (msgout_perhost structs) for each meillo@0: host. Used if there is no mail_host. meillo@0: route param is not used, we leave it here because that may change. meillo@0: */ meillo@0: meillo@10: GList* meillo@10: route_msgout_list(connect_route * route, GList * msgout_list) meillo@0: { meillo@10: GList *mo_ph_list = NULL; meillo@10: GList *msgout_node; meillo@0: meillo@10: foreach(msgout_list, msgout_node) { meillo@10: msg_out *msgout = (msg_out *) (msgout_node->data); meillo@10: msg_out *msgout_new; meillo@10: GList *rcpt_list = msgout->rcpt_list; meillo@10: GList *rcpt_node; meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@10: address *rcpt = rcpt_node->data; meillo@10: msgout_perhost *mo_ph = NULL; meillo@10: GList *mo_ph_node = NULL; meillo@0: meillo@10: /* search host in mo_ph_list */ meillo@10: foreach(mo_ph_list, mo_ph_node) { meillo@10: mo_ph = (msgout_perhost *) (mo_ph_node->data); meillo@10: if (strcasecmp(mo_ph->host, rcpt->domain) == 0) meillo@10: break; meillo@10: } meillo@10: if (mo_ph_node != NULL) { meillo@10: /* there is already a rcpt for this host */ meillo@10: msg_out *msgout_last = (msg_out *) ((g_list_last(mo_ph->msgout_list))->data); meillo@10: if (msgout_last->msg == msgout->msg) { meillo@15: /* if it is also the same message, it must be the last one appended meillo@15: to mo_ph->msgout_list (since outer loop goes through msgout_list) */ meillo@10: msgout_last->rcpt_list = g_list_append(msgout_last->rcpt_list, rcpt); meillo@10: } else { meillo@10: /* if not, we append a new msgout */ meillo@10: /* make a copy of msgout */ meillo@10: msgout_new = create_msg_out(msgout->msg); meillo@10: msgout_new->return_path = msgout->return_path; meillo@10: msgout_new->hdr_list = msgout->hdr_list; meillo@0: meillo@10: /* append our rcpt to it */ meillo@10: /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */ meillo@10: msgout_new->rcpt_list = g_list_append(NULL, rcpt); meillo@10: mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new); meillo@10: } meillo@10: } else { meillo@10: /* this rcpt to goes to another host */ meillo@10: mo_ph = create_msgout_perhost(rcpt->domain); meillo@10: mo_ph_list = g_list_append(mo_ph_list, mo_ph); meillo@0: meillo@10: /* make a copy of msgout */ meillo@10: msgout_new = create_msg_out(msgout->msg); meillo@10: msgout_new->return_path = msgout->return_path; meillo@10: msgout_new->hdr_list = msgout->hdr_list; meillo@0: meillo@10: /* append our rcpt to it */ meillo@10: /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */ meillo@10: msgout_new->rcpt_list = g_list_append(NULL, rcpt); meillo@10: mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new); meillo@10: } /* if mo_ph != NULL */ meillo@10: } /* foreach(rcpt_list, ... */ meillo@10: } /* foreach(msgout_list, ... */ meillo@10: meillo@10: return mo_ph_list; meillo@0: }