masqmail

annotate src/alias.c @ 64:0379789a847b

improved comments in test cases
author meillo@marmaro.de
date Sun, 30 May 2010 16:52:35 +0200
parents 26e34ae9a3e3
children a80ebfa16cd5
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 2000-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include <fnmatch.h>
meillo@0 21
meillo@10 22 gboolean
meillo@10 23 addr_is_local(address * addr)
meillo@0 24 {
meillo@10 25 GList *dom_node;
meillo@10 26 GList *addr_node;
meillo@10 27 address *a;
meillo@0 28
meillo@10 29 foreach(conf.local_hosts, dom_node) {
meillo@10 30 if (addr->domain == NULL)
meillo@10 31 return TRUE;
meillo@10 32 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0) {
meillo@10 33 foreach(conf.not_local_addresses, addr_node) {
meillo@10 34 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@10 35 if (addr_isequal(a, addr)) {
meillo@10 36 destroy_address(a);
meillo@10 37 return FALSE;
meillo@10 38 }
meillo@10 39 destroy_address(a);
meillo@10 40 }
meillo@10 41 return TRUE;
meillo@10 42 }
meillo@0 43 }
meillo@10 44 foreach(conf.local_addresses, addr_node) {
meillo@10 45 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@10 46 if (addr_isequal(a, addr)) {
meillo@10 47 destroy_address(a);
meillo@10 48 return TRUE;
meillo@10 49 }
meillo@10 50 destroy_address(a);
meillo@10 51 }
meillo@10 52 return FALSE;
meillo@0 53 }
meillo@0 54
meillo@10 55 static gboolean
meillo@10 56 addr_isequal_alias(address * addr1, address * addr2)
meillo@0 57 {
meillo@10 58 return (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0)
meillo@10 59 && (strcasecmp(addr1->domain, addr2->domain) == 0);
meillo@0 60 }
meillo@0 61
meillo@10 62 static GList*
meillo@10 63 parse_list(gchar * line)
meillo@0 64 {
meillo@10 65 GList *list = NULL;
meillo@10 66 gchar buf[256];
meillo@10 67 gchar *p, *q;
meillo@0 68
meillo@10 69 p = line;
meillo@14 70 while (*p != '\0') {
meillo@10 71 q = buf;
meillo@10 72 while (isspace(*p))
meillo@10 73 p++;
meillo@10 74 if (*p != '\"') {
meillo@10 75 while (*p && (*p != ',') && (q < buf + 255))
meillo@10 76 *(q++) = *(p++);
meillo@14 77 *q = '\0';
meillo@10 78 } else {
meillo@10 79 gboolean escape = FALSE;
meillo@10 80 p++;
meillo@10 81 while (*p && (*p != '\"' || escape) && (q < buf + 255)) {
meillo@10 82 if ((*p == '\\') && !escape)
meillo@10 83 escape = TRUE;
meillo@10 84 else {
meillo@10 85 escape = FALSE;
meillo@10 86 *(q++) = *p;
meillo@10 87 }
meillo@10 88 p++;
meillo@10 89 }
meillo@14 90 *q = '\0';
meillo@10 91 while (*p && (*p != ','))
meillo@10 92 p++;
meillo@10 93 }
meillo@10 94 list = g_list_append(list, g_strdup(g_strchomp(buf)));
meillo@10 95 if (*p)
meillo@10 96 p++;
meillo@0 97 }
meillo@10 98 return list;
meillo@0 99 }
meillo@0 100
meillo@10 101 GList*
meillo@10 102 alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list)
meillo@0 103 {
meillo@10 104 GList *done_list = NULL;
meillo@10 105 GList *rcpt_node = g_list_copy(rcpt_list);
meillo@0 106
meillo@10 107 while (rcpt_node != NULL) {
meillo@10 108 address *addr = (address *) (rcpt_node->data);
meillo@10 109 DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain);
meillo@10 110 /* if(addr_is_local(addr) && (addr->local_part[0] != '|') && */
meillo@10 111 if (addr_is_local(addr) && !(addr->flags & ADDR_FLAG_NOEXPAND)) {
meillo@10 112 gchar *val;
meillo@0 113
meillo@10 114 /* special handling for postmaster */
meillo@10 115 if (strcasecmp(addr->local_part, "postmaster") == 0)
meillo@10 116 val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp);
meillo@10 117 else
meillo@10 118 val = (gchar *) table_find_func(alias_table, addr->local_part, conf.alias_local_cmp);
meillo@0 119
meillo@10 120 DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part);
meillo@10 121 if (val != NULL) {
meillo@10 122 GList *val_list = parse_list(val);
meillo@10 123 GList *val_node;
meillo@10 124 GList *alias_list = NULL;
meillo@0 125
meillo@10 126 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
meillo@10 127 foreach(val_list, val_node) {
meillo@10 128 gchar *val = (gchar *) (val_node->data);
meillo@10 129 address *alias_addr;
meillo@10 130 address *addr_parent = NULL;
meillo@0 131
meillo@10 132 if (val[0] == '|') {
meillo@10 133 DEBUG(5) debugf("alias: %s is a pipe address\n", val);
meillo@10 134 alias_addr = create_address_pipe(val);
meillo@10 135 DEBUG(5) debugf("alias_pipe: %s is a pipe address\n", alias_addr->local_part);
meillo@10 136 } else if (val[0] == '\\') {
meillo@10 137 DEBUG(5) debugf("alias: shall not be expanded: '%s'\n", val);
meillo@10 138 alias_addr = create_address_qualified(&(val[1]), TRUE, conf.host_name);
meillo@10 139 alias_addr->flags |= ADDR_FLAG_NOEXPAND;
meillo@10 140 DEBUG(5) debugf("alias: not expanded: '%s'\n", alias_addr->local_part);
meillo@10 141 } else {
meillo@10 142 alias_addr = create_address_qualified(val, TRUE, conf.host_name);
meillo@10 143
meillo@10 144 /* search in parents for loops: */
meillo@10 145 for (addr_parent = addr; addr_parent; addr_parent = addr_parent->parent) {
meillo@10 146 if (addr_isequal_alias (alias_addr, addr_parent)) {
meillo@10 147 logwrite(LOG_ALERT,
meillo@10 148 "detected alias loop, (ignoring): %s@%s -> %s@%s\n",
meillo@10 149 addr_parent->local_part,
meillo@10 150 addr_parent->domain,
meillo@10 151 addr->local_part, addr->domain);
meillo@10 152 break;
meillo@10 153 }
meillo@10 154 }
meillo@10 155 }
meillo@10 156 if (!addr_parent) {
meillo@10 157 alias_list = g_list_append(alias_list, alias_addr);
meillo@10 158 alias_addr->parent = addr;
meillo@10 159 }
meillo@10 160 g_free(val);
meillo@10 161 }
meillo@10 162 g_list_free(val_list);
meillo@10 163 addr->children = g_list_copy(alias_list);
meillo@10 164 rcpt_node = g_list_concat(rcpt_node, alias_list);
meillo@10 165 } else {
meillo@10 166 DEBUG(5) debugf("alias: '%s' is completed\n", addr->local_part);
meillo@10 167 done_list = g_list_append(done_list, addr);
meillo@10 168 }
meillo@10 169 } else {
meillo@10 170 DEBUG(5) debugf("alias: '%s@%s' is not local\n", addr->local_part, addr->domain);
meillo@10 171 done_list = g_list_append(done_list, addr);
meillo@10 172 }
meillo@10 173 rcpt_node = g_list_next(rcpt_node);
meillo@0 174 }
meillo@0 175
meillo@10 176 /* delete addresses from done_list if they are in the non_rcpt_list */
meillo@10 177 if (non_rcpt_list) {
meillo@10 178 GList *rcpt_node_next;
meillo@10 179 for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) {
meillo@10 180 address *addr = (address *) (rcpt_node->data);
meillo@10 181 GList *non_node;
meillo@0 182
meillo@10 183 rcpt_node_next = g_list_next(rcpt_node);
meillo@0 184
meillo@10 185 foreach(non_rcpt_list, non_node) {
meillo@10 186 address *non_addr = (address *) (non_node->data);
meillo@10 187 if (addr_isequal(addr, non_addr)) {
meillo@10 188 done_list = g_list_remove_link(done_list, rcpt_node);
meillo@10 189 g_list_free_1(rcpt_node);
meillo@10 190 addr_mark_delivered(addr); /* this address is still in the children lists of the original address */
meillo@10 191 break;
meillo@10 192 }
meillo@10 193 }
meillo@10 194 }
meillo@0 195 }
meillo@10 196 return done_list;
meillo@0 197 }