masqmail

annotate src/alias.c @ 373:4cab237ce923

Fixed bug in matching of allowed recipients. Thanks to Juergen Daubert for finding and reporting the issue. The code had evaluated completely different data ...
author markus schnalke <meillo@marmaro.de>
date Sat, 14 Jan 2012 11:47:57 +0100
parents 41958685480d
children 028bc124d744
rev   line source
meillo@367 1 /*
meillo@367 2 ** MasqMail
meillo@367 3 ** Copyright (C) 2000-2001 Oliver Kurth
meillo@367 4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
meillo@367 5 **
meillo@367 6 ** This program is free software; you can redistribute it and/or modify
meillo@367 7 ** it under the terms of the GNU General Public License as published by
meillo@367 8 ** the Free Software Foundation; either version 2 of the License, or
meillo@367 9 ** (at your option) any later version.
meillo@367 10 **
meillo@367 11 ** This program is distributed in the hope that it will be useful,
meillo@367 12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@367 13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@367 14 ** GNU General Public License for more details.
meillo@367 15 **
meillo@367 16 ** You should have received a copy of the GNU General Public License
meillo@367 17 ** along with this program; if not, write to the Free Software
meillo@367 18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 19 */
meillo@0 20
meillo@0 21 #include "masqmail.h"
meillo@0 22 #include <fnmatch.h>
meillo@0 23
meillo@10 24 gboolean
meillo@366 25 addr_is_local(address *addr)
meillo@0 26 {
meillo@10 27 GList *dom_node;
meillo@10 28 GList *addr_node;
meillo@10 29 address *a;
meillo@0 30
meillo@239 31 if (addr->domain == NULL) {
meillo@239 32 return TRUE;
meillo@239 33 }
meillo@10 34 foreach(conf.local_hosts, dom_node) {
meillo@239 35 /* Note: FNM_CASEFOLD is a GNU extension */
meillo@239 36 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) != 0) {
meillo@239 37 /* no match, try next */
meillo@239 38 continue;
meillo@239 39 }
meillo@239 40 foreach(conf.not_local_addresses, addr_node) {
meillo@239 41 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@239 42 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n",
meillo@239 43 addr_node->data, a->address);
meillo@244 44 if (addr_isequal(a, addr, conf.localpartcmp)) {
meillo@10 45 destroy_address(a);
meillo@239 46 /* in local_hosts but also in not_local_addresses */
meillo@239 47 return FALSE;
meillo@10 48 }
meillo@239 49 destroy_address(a);
meillo@10 50 }
meillo@239 51 /* in local_hosts */
meillo@239 52 return TRUE;
meillo@0 53 }
meillo@10 54 foreach(conf.local_addresses, addr_node) {
meillo@10 55 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@114 56 DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n",
meillo@114 57 addr_node->data, a->address);
meillo@244 58 if (addr_isequal(a, addr, conf.localpartcmp)) {
meillo@10 59 destroy_address(a);
meillo@239 60 /* in local_addresses */
meillo@10 61 return TRUE;
meillo@10 62 }
meillo@10 63 destroy_address(a);
meillo@10 64 }
meillo@10 65 return FALSE;
meillo@0 66 }
meillo@0 67
meillo@10 68 static GList*
meillo@366 69 parse_list(gchar *line)
meillo@0 70 {
meillo@10 71 GList *list = NULL;
meillo@10 72 gchar buf[256];
meillo@10 73 gchar *p, *q;
meillo@0 74
meillo@10 75 p = line;
meillo@14 76 while (*p != '\0') {
meillo@10 77 q = buf;
meillo@10 78 while (isspace(*p))
meillo@10 79 p++;
meillo@239 80 if (*p != '"') {
meillo@10 81 while (*p && (*p != ',') && (q < buf + 255))
meillo@10 82 *(q++) = *(p++);
meillo@14 83 *q = '\0';
meillo@10 84 } else {
meillo@10 85 gboolean escape = FALSE;
meillo@10 86 p++;
meillo@239 87 while (*p && (*p != '"' || escape) && (q < buf + 255)) {
meillo@10 88 if ((*p == '\\') && !escape)
meillo@10 89 escape = TRUE;
meillo@10 90 else {
meillo@10 91 escape = FALSE;
meillo@10 92 *(q++) = *p;
meillo@10 93 }
meillo@10 94 p++;
meillo@10 95 }
meillo@14 96 *q = '\0';
meillo@10 97 while (*p && (*p != ','))
meillo@10 98 p++;
meillo@10 99 }
meillo@10 100 list = g_list_append(list, g_strdup(g_strchomp(buf)));
meillo@10 101 if (*p)
meillo@10 102 p++;
meillo@0 103 }
meillo@10 104 return list;
meillo@0 105 }
meillo@0 106
meillo@239 107 /*
meillo@367 108 ** addr is assumed to be local and no pipe address nor not-to-expand
meillo@239 109 */
meillo@239 110 static GList*
meillo@366 111 expand_one(GList *alias_table, address *addr)
meillo@239 112 {
meillo@239 113 GList *val_list;
meillo@239 114 GList *val_node;
meillo@239 115 GList *alias_list = NULL;
meillo@239 116 GList *alias_node;
meillo@239 117 gchar *val;
meillo@366 118 address *alias_addr;
meillo@239 119
meillo@239 120 /* expand the local alias */
meillo@239 121 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n", addr->local_part);
meillo@239 122
meillo@239 123 if (strcasecmp(addr->local_part, "postmaster") == 0) {
meillo@367 124 /*
meillo@367 125 ** postmaster must always be matched caseless
meillo@367 126 ** see RFC 822 and RFC 5321
meillo@367 127 */
meillo@239 128 val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp);
meillo@239 129 } else {
meillo@244 130 val = (gchar *) table_find_func(alias_table, addr->local_part, conf.localpartcmp);
meillo@239 131 }
meillo@239 132 if (!val) {
meillo@239 133 DEBUG(5) debugf("alias: '%s' is fully expanded, hence completed\n",
meillo@239 134 addr->local_part);
meillo@239 135 return g_list_append(NULL, addr);
meillo@239 136 }
meillo@239 137
meillo@239 138 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
meillo@239 139 val_list = parse_list(val);
meillo@239 140 alias_list = NULL;
meillo@239 141
meillo@239 142 foreach(val_list, val_node) {
meillo@239 143 gchar *val = (gchar *) (val_node->data);
meillo@239 144 address *alias_addr;
meillo@239 145 address *addr_parent = NULL;
meillo@239 146
meillo@239 147 DEBUG(6) debugf("alias: processing '%s'\n", val);
meillo@239 148
meillo@239 149 if (val[0] == '\\') {
meillo@239 150 DEBUG(5) debugf("alias: '%s' is marked as final, hence completed\n", val);
meillo@239 151 alias_addr = create_address_qualified(val+1, TRUE, conf.host_name);
meillo@239 152 g_free(val);
meillo@239 153 DEBUG(6) debugf("alias: address generated: '%s'\n",
meillo@239 154 alias_addr->local_part);
meillo@239 155 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 156 continue;
meillo@239 157 }
meillo@239 158
meillo@239 159 if (val[0] == '|') {
meillo@239 160 DEBUG(5) debugf("alias: '%s' is a pipe address\n", val);
meillo@309 161 alias_addr = create_address_pipe(val);
meillo@239 162 g_free(val);
meillo@239 163 DEBUG(6) debugf("alias: pipe generated: %s\n",
meillo@239 164 alias_addr->local_part);
meillo@239 165 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 166 continue;
meillo@239 167 }
meillo@239 168
meillo@239 169 alias_addr = create_address_qualified(val, TRUE, conf.host_name);
meillo@239 170 g_free(val);
meillo@239 171
meillo@239 172 if (!addr_is_local(alias_addr)) {
meillo@239 173 DEBUG(5) debugf("alias: '%s@%s' is non-local, hence completed\n",
meillo@239 174 alias_addr->local_part, alias_addr->domain);
meillo@239 175 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 176 continue;
meillo@239 177 }
meillo@239 178
meillo@242 179 /* addr is local and to expand at this point */
meillo@242 180 /* but first ... search in parents for loops: */
meillo@244 181 if (addr_isequal_parent(addr, alias_addr, conf.localpartcmp)) {
meillo@239 182 /* loop detected, ignore this path */
meillo@242 183 logwrite(LOG_ALERT, "alias: detected loop, hence ignoring '%s'\n",
meillo@242 184 alias_addr->local_part);
meillo@239 185 continue;
meillo@239 186 }
meillo@239 187 alias_addr->parent = addr;
meillo@239 188
meillo@239 189 /* recurse */
meillo@239 190 DEBUG(6) debugf("alias: >>\n");
meillo@239 191 alias_node = expand_one(alias_table, alias_addr);
meillo@239 192 DEBUG(6) debugf("alias: <<\n");
meillo@239 193 if (alias_node) {
meillo@239 194 alias_list = g_list_concat(alias_list, alias_node);
meillo@239 195 }
meillo@239 196 }
meillo@239 197 g_list_free(val_list);
meillo@239 198 addr->children = g_list_copy(alias_list);
meillo@239 199
meillo@239 200 return alias_list;
meillo@239 201 }
meillo@239 202
meillo@10 203 GList*
meillo@366 204 alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list)
meillo@0 205 {
meillo@239 206 GList *rcpt_node = NULL;
meillo@239 207 GList *alias_list = NULL;
meillo@10 208 GList *done_list = NULL;
meillo@239 209 GList *rcpt_node_next = NULL;
meillo@239 210 address *addr = NULL;
meillo@0 211
meillo@239 212 for (rcpt_node=g_list_copy(rcpt_list);
meillo@239 213 rcpt_node;
meillo@239 214 rcpt_node=g_list_next(rcpt_node)) {
meillo@0 215
meillo@239 216 addr = (address *) (rcpt_node->data);
meillo@239 217 if (addr_is_local(addr)) {
meillo@239 218 DEBUG(5) debugf("alias: (orig rcpt addr) expand local '%s'\n",
meillo@239 219 addr->local_part);
meillo@239 220 alias_list = expand_one(alias_table, addr);
meillo@239 221 if (alias_list) {
meillo@239 222 done_list = g_list_concat(done_list, alias_list);
meillo@10 223 }
meillo@10 224 } else {
meillo@239 225 DEBUG(5) debugf("alias: (orig rcpt addr) don't expand non-local '%s@%s'\n",
meillo@239 226 addr->local_part, addr->domain);
meillo@10 227 done_list = g_list_append(done_list, addr);
meillo@10 228 }
meillo@0 229 }
meillo@0 230
meillo@239 231 /* we're done if we don't have to remove rcpts */
meillo@239 232 if (!non_rcpt_list) {
meillo@239 233 return done_list;
meillo@239 234 }
meillo@0 235
meillo@239 236 /* delete addresses of non_rcpt_list from done_list */
meillo@239 237 for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) {
meillo@239 238 address *addr = (address *) (rcpt_node->data);
meillo@239 239 GList *non_node;
meillo@0 240
meillo@239 241 rcpt_node_next = g_list_next(rcpt_node);
meillo@239 242 foreach(non_rcpt_list, non_node) {
meillo@239 243 address *non_addr = (address *) (non_node->data);
meillo@244 244 if (addr_isequal(addr, non_addr, conf.localpartcmp)) {
meillo@239 245 done_list = g_list_remove_link(done_list, rcpt_node);
meillo@239 246 g_list_free_1(rcpt_node);
meillo@367 247 /*
meillo@367 248 ** this address is still in the children
meillo@367 249 ** lists of the original address, simply
meillo@367 250 ** mark them delivered
meillo@367 251 */
meillo@239 252 addr_mark_delivered(addr);
meillo@239 253 break;
meillo@10 254 }
meillo@10 255 }
meillo@0 256 }
meillo@10 257 return done_list;
meillo@0 258 }