meillo@367: /* meillo@367: ** MasqMail meillo@367: ** Copyright (C) 1999-2001 Oliver Kurth meillo@367: ** Copyright (C) 2010 markus schnalke meillo@367: ** meillo@367: ** This program is free software; you can redistribute it and/or modify meillo@367: ** it under the terms of the GNU General Public License as published by meillo@367: ** the Free Software Foundation; either version 2 of the License, or meillo@367: ** (at your option) any later version. meillo@367: ** meillo@367: ** This program is distributed in the hope that it will be useful, meillo@367: ** but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@367: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@367: ** GNU General Public License for more details. meillo@367: ** meillo@367: ** You should have received a copy of the GNU General Public License meillo@367: ** along with this program; if not, write to the Free Software meillo@367: ** 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@366: 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@366: 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@366: rewrite_headers(msg_out *msgout, connect_route *route) meillo@0: { meillo@367: /* meillo@367: ** if set_h_from_domain is set, replace domain in all meillo@367: ** From: headers. meillo@367: */ 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@237: /* meillo@367: ** Split a recipient list into the three groups: meillo@367: ** - local recipients meillo@367: ** - those maching the patterns meillo@367: ** - those not matching the patterns meillo@367: ** If patterns is NULL: only splitting between local and others is done. meillo@237: */ meillo@10: void meillo@367: split_rcpts(GList *rcpt_list, GList *patterns, GList **rl_local, meillo@367: GList **rl_matching, GList **rl_others) meillo@0: { meillo@10: GList *rcpt_node; meillo@373: GList *pat_node = NULL; meillo@237: address *rcpt = NULL; meillo@0: meillo@10: if (rcpt_list == NULL) meillo@10: return; meillo@0: meillo@10: foreach(rcpt_list, rcpt_node) { meillo@237: rcpt = (address *) (rcpt_node->data); meillo@373: pat_node = NULL; meillo@0: meillo@237: if (addr_is_local(rcpt)) { meillo@237: if (rl_local) meillo@237: *rl_local = g_list_append(*rl_local, rcpt); meillo@237: } else { meillo@367: /* meillo@373: ** if patterns is NULL, pat_node will be NULL, meillo@367: ** hence all non-locals are put to others meillo@367: */ meillo@373: foreach(patterns, pat_node) { meillo@373: address *pat = (address *) (pat_node->data); meillo@373: if (fnmatch(pat->domain, rcpt->domain, FNM_CASEFOLD)==0 && fnmatch(pat->local_part, rcpt->local_part, 0)==0) { /* TODO: match local_part caseless? */ meillo@237: break; meillo@373: } meillo@237: } meillo@373: if (pat_node) { meillo@355: if (rl_matching) meillo@355: *rl_matching = g_list_append(*rl_matching, rcpt); meillo@237: } else { meillo@237: if (rl_others) meillo@237: *rl_others = g_list_append(*rl_others, rcpt); meillo@237: } meillo@10: } meillo@10: } meillo@0: } meillo@0: meillo@345: /* meillo@367: ** Return a new list of the local rcpts in the rcpt_list meillo@367: ** TODO: This function is almost exactly the same as remote_rcpts(). Merge? meillo@345: */ meillo@345: GList* meillo@366: local_rcpts(GList *rcpt_list) meillo@345: { meillo@345: GList *rcpt_node; meillo@345: GList *local_rcpts = NULL; meillo@345: address *rcpt = NULL; meillo@345: meillo@345: if (!rcpt_list) { meillo@345: return NULL; meillo@345: } meillo@345: foreach(rcpt_list, rcpt_node) { meillo@345: rcpt = (address *) (rcpt_node->data); meillo@345: if (addr_is_local(rcpt)) { meillo@345: local_rcpts = g_list_append(local_rcpts, rcpt); meillo@345: } meillo@345: } meillo@345: return local_rcpts; meillo@345: } meillo@345: meillo@345: /* meillo@367: ** Return a new list of non-local rcpts in the rcpt_list meillo@367: ** TODO: This function is almost exactly the same as local_rcpts(). Merge? meillo@345: */ meillo@345: GList* meillo@366: remote_rcpts(GList *rcpt_list) meillo@345: { meillo@345: GList *rcpt_node; meillo@345: GList *remote_rcpts = NULL; meillo@345: address *rcpt = NULL; meillo@345: meillo@345: if (!rcpt_list) { meillo@345: return NULL; meillo@345: } meillo@345: foreach(rcpt_list, rcpt_node) { meillo@345: rcpt = (address *) (rcpt_node->data); meillo@345: if (!addr_is_local(rcpt)) { meillo@345: remote_rcpts = g_list_append(remote_rcpts, rcpt); meillo@345: } meillo@345: } meillo@345: return remote_rcpts; meillo@345: } meillo@345: meillo@10: static gint meillo@317: _g_list_addrcmp(gconstpointer pattern, gconstpointer addr) meillo@0: { meillo@317: int res; meillo@366: address *patternaddr = (address*) pattern; meillo@366: address *stringaddr = (address*) addr; meillo@317: meillo@317: DEBUG(6) debugf("_g_list_addrcmp: pattern `%s' `%s' on string `%s' `%s'\n", meillo@317: patternaddr->local_part, patternaddr->domain, meillo@317: stringaddr->local_part, stringaddr->domain); meillo@317: /* TODO: check if we should match here dependent on caseless_matching */ meillo@317: res = fnmatch(patternaddr->local_part, stringaddr->local_part, 0); meillo@317: if (res != 0) { meillo@317: DEBUG(6) debugf("_g_list_addrcmp: ... failed on local_part\n"); meillo@317: return res; meillo@317: } meillo@317: res = fnmatch(patternaddr->domain, stringaddr->domain, FNM_CASEFOLD); meillo@317: DEBUG(6) debugf("_g_list_addrcmp: ... %s\n", (res==0) ? "matched" : "failed on domain"); meillo@317: return res; meillo@0: } meillo@0: meillo@10: gboolean meillo@366: route_sender_is_allowed(connect_route *route, address *ret_path) meillo@0: { meillo@317: if (route->denied_senders && g_list_find_custom(route->denied_senders, ret_path, _g_list_addrcmp)) { meillo@317: return FALSE; meillo@10: } meillo@317: if (route->allowed_senders) { meillo@317: if (g_list_find_custom(route->allowed_senders, ret_path, _g_list_addrcmp)) { 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: /* meillo@367: ** Make lists of matching/not matching rcpts. meillo@367: ** Local domains are NOT regared here, these should be sorted out previously meillo@0: */ meillo@10: void meillo@366: route_split_rcpts(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@317: if (route->allowed_recipients) { meillo@317: DEBUG(5) debugf("testing for route->allowed_recipients\n"); meillo@317: split_rcpts(rcpt_list, route->allowed_recipients, NULL, &tmp_list, p_non_rcpt_list); meillo@10: } else { meillo@317: DEBUG(5) debugf("route->allowed_recipients == 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@317: split_rcpts(tmp_list, route->denied_recipients, NULL, p_non_rcpt_list, p_rcpt_list); meillo@10: g_list_free(tmp_list); meillo@0: } meillo@0: meillo@429: gboolean meillo@429: route_from_hdr_is_allowed(connect_route *route, char *from_hdr) meillo@429: { meillo@429: address *addr = create_address_qualified(from_hdr, FALSE, meillo@429: conf.host_name); meillo@429: if (route->denied_from_hdrs && g_list_find_custom(route->denied_from_hdrs, addr, _g_list_addrcmp)) { meillo@429: return FALSE; meillo@429: } meillo@429: if (route->allowed_from_hdrs) { meillo@429: if (g_list_find_custom(route->allowed_from_hdrs, addr, meillo@429: _g_list_addrcmp)) { meillo@429: return TRUE; meillo@429: } else { meillo@429: return FALSE; meillo@429: } meillo@429: } meillo@429: return TRUE; meillo@429: } meillo@429: meillo@429: meillo@10: msg_out* meillo@366: 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@367: /* meillo@367: ** rewrite return path if there is a table, use that meillo@367: ** if an address is found and if it has a domain, use that meillo@367: */ 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@367: /* meillo@367: ** put msgout's is msgout_list into bins (msgout_perhost structs) for each meillo@367: ** host. Used if there is no mail_host. meillo@367: ** route param is not used, we leave it here because that may change. meillo@367: */ meillo@10: GList* meillo@366: 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@367: /* meillo@367: ** if it is also the same message, meillo@367: ** it must be the last one meillo@367: ** appended to mo_ph->msgout_list meillo@367: ** (since outer loop goes through meillo@367: ** msgout_list) meillo@367: */ 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: }