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