meillo@367: /* meillo@367: ** MasqMail meillo@367: ** Copyright (C) 2000-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@0: #include "masqmail.h" meillo@0: #include meillo@0: meillo@10: gboolean meillo@366: addr_is_local(address *addr) meillo@0: { meillo@10: GList *dom_node; meillo@10: GList *addr_node; meillo@10: address *a; meillo@0: meillo@239: if (addr->domain == NULL) { meillo@239: return TRUE; meillo@239: } meillo@10: foreach(conf.local_hosts, dom_node) { meillo@239: /* Note: FNM_CASEFOLD is a GNU extension */ meillo@239: if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) != 0) { meillo@239: /* no match, try next */ meillo@239: continue; meillo@239: } meillo@239: foreach(conf.not_local_addresses, addr_node) { meillo@239: a = create_address_qualified(addr_node->data, TRUE, conf.host_name); meillo@239: DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n", meillo@239: addr_node->data, a->address); meillo@244: if (addr_isequal(a, addr, conf.localpartcmp)) { meillo@10: destroy_address(a); meillo@239: /* in local_hosts but also in not_local_addresses */ meillo@239: return FALSE; meillo@10: } meillo@239: destroy_address(a); meillo@10: } meillo@239: /* in local_hosts */ meillo@239: return TRUE; meillo@0: } meillo@10: foreach(conf.local_addresses, addr_node) { meillo@10: a = create_address_qualified(addr_node->data, TRUE, conf.host_name); meillo@114: DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n", meillo@114: addr_node->data, a->address); meillo@244: if (addr_isequal(a, addr, conf.localpartcmp)) { meillo@10: destroy_address(a); meillo@239: /* in local_addresses */ meillo@10: return TRUE; meillo@10: } meillo@10: destroy_address(a); meillo@10: } meillo@10: return FALSE; meillo@0: } meillo@0: meillo@10: static GList* meillo@366: parse_list(gchar *line) meillo@0: { meillo@10: GList *list = NULL; meillo@10: gchar buf[256]; meillo@10: gchar *p, *q; meillo@0: meillo@10: p = line; meillo@14: while (*p != '\0') { meillo@10: q = buf; meillo@10: while (isspace(*p)) meillo@10: p++; meillo@239: if (*p != '"') { meillo@10: while (*p && (*p != ',') && (q < buf + 255)) meillo@10: *(q++) = *(p++); meillo@14: *q = '\0'; meillo@10: } else { meillo@10: gboolean escape = FALSE; meillo@10: p++; meillo@239: while (*p && (*p != '"' || escape) && (q < buf + 255)) { meillo@10: if ((*p == '\\') && !escape) meillo@10: escape = TRUE; meillo@10: else { meillo@10: escape = FALSE; meillo@10: *(q++) = *p; meillo@10: } meillo@10: p++; meillo@10: } meillo@14: *q = '\0'; meillo@10: while (*p && (*p != ',')) meillo@10: p++; meillo@10: } meillo@10: list = g_list_append(list, g_strdup(g_strchomp(buf))); meillo@10: if (*p) meillo@10: p++; meillo@0: } meillo@10: return list; meillo@0: } meillo@0: meillo@239: /* meillo@367: ** addr is assumed to be local and no pipe address nor not-to-expand meillo@239: */ meillo@239: static GList* meillo@366: expand_one(GList *alias_table, address *addr) meillo@239: { meillo@239: GList *val_list; meillo@239: GList *val_node; meillo@239: GList *alias_list = NULL; meillo@239: GList *alias_node; meillo@239: gchar *val; meillo@366: address *alias_addr; meillo@239: meillo@239: /* expand the local alias */ meillo@239: DEBUG(6) debugf("alias: '%s' is local and will get expanded\n", addr->local_part); meillo@239: meillo@239: if (strcasecmp(addr->local_part, "postmaster") == 0) { meillo@367: /* meillo@367: ** postmaster must always be matched caseless meillo@367: ** see RFC 822 and RFC 5321 meillo@367: */ meillo@239: val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp); meillo@239: } else { meillo@244: val = (gchar *) table_find_func(alias_table, addr->local_part, conf.localpartcmp); meillo@239: } meillo@239: if (!val) { meillo@239: DEBUG(5) debugf("alias: '%s' is fully expanded, hence completed\n", meillo@239: addr->local_part); meillo@239: return g_list_append(NULL, addr); meillo@239: } meillo@239: meillo@239: DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val); meillo@239: val_list = parse_list(val); meillo@239: alias_list = NULL; meillo@239: meillo@239: foreach(val_list, val_node) { meillo@239: gchar *val = (gchar *) (val_node->data); meillo@239: address *alias_addr; meillo@239: address *addr_parent = NULL; meillo@239: meillo@239: DEBUG(6) debugf("alias: processing '%s'\n", val); meillo@239: meillo@239: if (val[0] == '\\') { meillo@239: DEBUG(5) debugf("alias: '%s' is marked as final, hence completed\n", val); meillo@239: alias_addr = create_address_qualified(val+1, TRUE, conf.host_name); meillo@239: g_free(val); meillo@239: DEBUG(6) debugf("alias: address generated: '%s'\n", meillo@239: alias_addr->local_part); meillo@239: alias_list = g_list_append(alias_list, alias_addr); meillo@239: continue; meillo@239: } meillo@239: meillo@239: if (val[0] == '|') { meillo@239: DEBUG(5) debugf("alias: '%s' is a pipe address\n", val); meillo@309: alias_addr = create_address_pipe(val); meillo@239: g_free(val); meillo@239: DEBUG(6) debugf("alias: pipe generated: %s\n", meillo@239: alias_addr->local_part); meillo@239: alias_list = g_list_append(alias_list, alias_addr); meillo@239: continue; meillo@239: } meillo@239: meillo@239: alias_addr = create_address_qualified(val, TRUE, conf.host_name); meillo@239: g_free(val); meillo@239: meillo@239: if (!addr_is_local(alias_addr)) { meillo@239: DEBUG(5) debugf("alias: '%s@%s' is non-local, hence completed\n", meillo@239: alias_addr->local_part, alias_addr->domain); meillo@239: alias_list = g_list_append(alias_list, alias_addr); meillo@239: continue; meillo@239: } meillo@239: meillo@242: /* addr is local and to expand at this point */ meillo@242: /* but first ... search in parents for loops: */ meillo@244: if (addr_isequal_parent(addr, alias_addr, conf.localpartcmp)) { meillo@239: /* loop detected, ignore this path */ meillo@242: logwrite(LOG_ALERT, "alias: detected loop, hence ignoring '%s'\n", meillo@242: alias_addr->local_part); meillo@239: continue; meillo@239: } meillo@239: alias_addr->parent = addr; meillo@239: meillo@239: /* recurse */ meillo@239: DEBUG(6) debugf("alias: >>\n"); meillo@239: alias_node = expand_one(alias_table, alias_addr); meillo@239: DEBUG(6) debugf("alias: <<\n"); meillo@239: if (alias_node) { meillo@239: alias_list = g_list_concat(alias_list, alias_node); meillo@239: } meillo@239: } meillo@239: g_list_free(val_list); meillo@239: addr->children = g_list_copy(alias_list); meillo@239: meillo@239: return alias_list; meillo@239: } meillo@239: meillo@10: GList* meillo@366: alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list) meillo@0: { meillo@239: GList *rcpt_node = NULL; meillo@239: GList *alias_list = NULL; meillo@10: GList *done_list = NULL; meillo@239: GList *rcpt_node_next = NULL; meillo@239: address *addr = NULL; meillo@0: meillo@239: for (rcpt_node=g_list_copy(rcpt_list); meillo@239: rcpt_node; meillo@239: rcpt_node=g_list_next(rcpt_node)) { meillo@0: meillo@239: addr = (address *) (rcpt_node->data); meillo@239: if (addr_is_local(addr)) { meillo@239: DEBUG(5) debugf("alias: (orig rcpt addr) expand local '%s'\n", meillo@239: addr->local_part); meillo@239: alias_list = expand_one(alias_table, addr); meillo@239: if (alias_list) { meillo@239: done_list = g_list_concat(done_list, alias_list); meillo@10: } meillo@10: } else { meillo@239: DEBUG(5) debugf("alias: (orig rcpt addr) don't expand non-local '%s@%s'\n", meillo@239: addr->local_part, addr->domain); meillo@10: done_list = g_list_append(done_list, addr); meillo@10: } meillo@0: } meillo@0: meillo@239: /* we're done if we don't have to remove rcpts */ meillo@239: if (!non_rcpt_list) { meillo@239: return done_list; meillo@239: } meillo@0: meillo@239: /* delete addresses of non_rcpt_list from done_list */ meillo@239: for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) { meillo@239: address *addr = (address *) (rcpt_node->data); meillo@239: GList *non_node; meillo@0: meillo@239: rcpt_node_next = g_list_next(rcpt_node); meillo@239: foreach(non_rcpt_list, non_node) { meillo@239: address *non_addr = (address *) (non_node->data); meillo@244: if (addr_isequal(addr, non_addr, conf.localpartcmp)) { meillo@239: done_list = g_list_remove_link(done_list, rcpt_node); meillo@239: g_list_free_1(rcpt_node); meillo@367: /* meillo@367: ** this address is still in the children meillo@367: ** lists of the original address, simply meillo@367: ** mark them delivered meillo@367: */ meillo@239: addr_mark_delivered(addr); meillo@239: break; meillo@10: } meillo@10: } meillo@0: } meillo@10: return done_list; meillo@0: }