masqmail

annotate src/alias.c @ 415:0430194f7ef8

Debug message cosmetic.
author markus schnalke <meillo@marmaro.de>
date Wed, 29 Feb 2012 14:36:07 +0100
parents 68ae9182059c
children ddac877ced95
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@390 31 if (!addr->domain) {
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@390 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@390 41 a = create_address_qualified(addr_node->data, TRUE,
meillo@390 42 conf.host_name);
meillo@390 43 DEBUG(6) debugf("not_local_addresses: "
meillo@390 44 "addr_node->data=%s a->address=%s\n",
meillo@239 45 addr_node->data, a->address);
meillo@244 46 if (addr_isequal(a, addr, conf.localpartcmp)) {
meillo@390 47 /* also in not_local_addresses */
meillo@10 48 destroy_address(a);
meillo@239 49 return FALSE;
meillo@10 50 }
meillo@239 51 destroy_address(a);
meillo@10 52 }
meillo@239 53 /* in local_hosts */
meillo@239 54 return TRUE;
meillo@0 55 }
meillo@10 56 foreach(conf.local_addresses, addr_node) {
meillo@390 57 a = create_address_qualified(addr_node->data, TRUE,
meillo@390 58 conf.host_name);
meillo@390 59 DEBUG(6) debugf("local_addresses: addr_node->data=%s "
meillo@390 60 "a->address=%s\n",
meillo@114 61 addr_node->data, a->address);
meillo@244 62 if (addr_isequal(a, addr, conf.localpartcmp)) {
meillo@390 63 /* in local_addresses */
meillo@10 64 destroy_address(a);
meillo@10 65 return TRUE;
meillo@10 66 }
meillo@10 67 destroy_address(a);
meillo@10 68 }
meillo@10 69 return FALSE;
meillo@0 70 }
meillo@0 71
meillo@10 72 static GList*
meillo@366 73 parse_list(gchar *line)
meillo@0 74 {
meillo@10 75 GList *list = NULL;
meillo@10 76 gchar buf[256];
meillo@10 77 gchar *p, *q;
meillo@0 78
meillo@10 79 p = line;
meillo@390 80 while (*p) {
meillo@10 81 q = buf;
meillo@390 82 while (isspace(*p)) {
meillo@10 83 p++;
meillo@390 84 }
meillo@239 85 if (*p != '"') {
meillo@390 86 while (*p && (*p != ',') && (q < buf + 255)) {
meillo@10 87 *(q++) = *(p++);
meillo@390 88 }
meillo@14 89 *q = '\0';
meillo@10 90 } else {
meillo@10 91 gboolean escape = FALSE;
meillo@10 92 p++;
meillo@390 93 while (*p && (*p != '"' || escape) && (q < buf+255)) {
meillo@390 94 if ((*p == '\\') && !escape) {
meillo@10 95 escape = TRUE;
meillo@390 96 } else {
meillo@10 97 escape = FALSE;
meillo@10 98 *(q++) = *p;
meillo@10 99 }
meillo@10 100 p++;
meillo@10 101 }
meillo@14 102 *q = '\0';
meillo@390 103 while (*p && (*p != ',')) {
meillo@10 104 p++;
meillo@390 105 }
meillo@10 106 }
meillo@10 107 list = g_list_append(list, g_strdup(g_strchomp(buf)));
meillo@390 108 if (*p) {
meillo@10 109 p++;
meillo@390 110 }
meillo@0 111 }
meillo@10 112 return list;
meillo@0 113 }
meillo@0 114
meillo@387 115 static int
meillo@387 116 globaliascmp(const char *pattern, const char *addr)
meillo@387 117 {
meillo@390 118 if (conf.localpartcmp == strcasecmp) {
meillo@387 119 return fnmatch(pattern, addr, FNM_CASEFOLD);
meillo@387 120 } else if (strncasecmp(addr, "postmaster", 10)==0) {
meillo@390 121 /* postmaster must always be matched caseless
meillo@390 122 ** see RFC 822 and RFC 5321 */
meillo@387 123 return fnmatch(pattern, addr, FNM_CASEFOLD);
meillo@387 124 } else {
meillo@387 125 /* case-sensitive */
meillo@387 126 return fnmatch(pattern, addr, 0);
meillo@387 127 }
meillo@387 128 }
meillo@387 129
meillo@239 130 /*
meillo@367 131 ** addr is assumed to be local and no pipe address nor not-to-expand
meillo@239 132 */
meillo@239 133 static GList*
meillo@387 134 expand_one(GList *alias_table, address *addr, int doglob)
meillo@239 135 {
meillo@239 136 GList *val_list;
meillo@239 137 GList *val_node;
meillo@239 138 GList *alias_list = NULL;
meillo@239 139 GList *alias_node;
meillo@239 140 gchar *val;
meillo@239 141
meillo@239 142 /* expand the local alias */
meillo@387 143 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n",
meillo@387 144 doglob ? addr->address : addr->local_part);
meillo@239 145
meillo@387 146 if (doglob) {
meillo@387 147 val = (gchar *) table_find_func(alias_table, addr->address,
meillo@387 148 globaliascmp);
meillo@387 149
meillo@387 150 } else if (strcasecmp(addr->local_part, "postmaster") == 0) {
meillo@390 151 /* postmaster must always be matched caseless
meillo@390 152 ** see RFC 822 and RFC 5321 */
meillo@387 153 val = (gchar *) table_find_func(alias_table, addr->local_part,
meillo@387 154 strcasecmp);
meillo@239 155 } else {
meillo@387 156 val = (gchar *) table_find_func(alias_table, addr->local_part,
meillo@387 157 conf.localpartcmp);
meillo@239 158 }
meillo@239 159 if (!val) {
meillo@387 160 DEBUG(5) debugf("alias: '%s' is fully expanded, hence "
meillo@387 161 "completed\n",
meillo@387 162 doglob ? addr->address : addr->local_part);
meillo@239 163 return g_list_append(NULL, addr);
meillo@239 164 }
meillo@239 165
meillo@387 166 DEBUG(5) debugf("alias: '%s' -> '%s'\n",
meillo@387 167 doglob ? addr->address : addr->local_part, val);
meillo@239 168 val_list = parse_list(val);
meillo@239 169 alias_list = NULL;
meillo@239 170
meillo@239 171 foreach(val_list, val_node) {
meillo@239 172 gchar *val = (gchar *) (val_node->data);
meillo@239 173 address *alias_addr;
meillo@239 174
meillo@239 175 DEBUG(6) debugf("alias: processing '%s'\n", val);
meillo@239 176
meillo@399 177 if (*val == '\\') {
meillo@390 178 DEBUG(5) debugf("alias: '%s' is marked as final, "
meillo@390 179 "hence completed\n", val);
meillo@390 180 alias_addr = create_address_qualified(val+1, TRUE,
meillo@390 181 conf.host_name);
meillo@239 182 g_free(val);
meillo@239 183 DEBUG(6) debugf("alias: address generated: '%s'\n",
meillo@387 184 alias_addr->address);
meillo@239 185 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 186 continue;
meillo@239 187 }
meillo@239 188
meillo@399 189 if (*val == '|') {
meillo@390 190 DEBUG(5) debugf("alias: '%s' is a pipe address\n",
meillo@390 191 val);
meillo@309 192 alias_addr = create_address_pipe(val);
meillo@239 193 g_free(val);
meillo@239 194 DEBUG(6) debugf("alias: pipe generated: %s\n",
meillo@239 195 alias_addr->local_part);
meillo@239 196 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 197 continue;
meillo@239 198 }
meillo@239 199
meillo@390 200 alias_addr = create_address_qualified(val, TRUE,
meillo@390 201 conf.host_name);
meillo@239 202 g_free(val);
meillo@239 203
meillo@239 204 if (!addr_is_local(alias_addr)) {
meillo@390 205 DEBUG(5) debugf("alias: '%s' is non-local, "
meillo@390 206 "hence completed\n",
meillo@387 207 alias_addr->address);
meillo@239 208 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 209 continue;
meillo@239 210 }
meillo@239 211
meillo@242 212 /* addr is local and to expand at this point */
meillo@242 213 /* but first ... search in parents for loops: */
meillo@244 214 if (addr_isequal_parent(addr, alias_addr, conf.localpartcmp)) {
meillo@239 215 /* loop detected, ignore this path */
meillo@390 216 logwrite(LOG_ALERT, "alias: detected loop, "
meillo@390 217 "hence ignoring '%s'\n",
meillo@390 218 alias_addr->local_part);
meillo@239 219 continue;
meillo@239 220 }
meillo@239 221 alias_addr->parent = addr;
meillo@239 222
meillo@239 223 /* recurse */
meillo@239 224 DEBUG(6) debugf("alias: >>\n");
meillo@387 225 alias_node = expand_one(alias_table, alias_addr, doglob);
meillo@239 226 DEBUG(6) debugf("alias: <<\n");
meillo@239 227 if (alias_node) {
meillo@239 228 alias_list = g_list_concat(alias_list, alias_node);
meillo@239 229 }
meillo@239 230 }
meillo@239 231 g_list_free(val_list);
meillo@239 232 addr->children = g_list_copy(alias_list);
meillo@239 233
meillo@239 234 return alias_list;
meillo@239 235 }
meillo@239 236
meillo@10 237 GList*
meillo@387 238 alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list,
meillo@387 239 int doglob)
meillo@0 240 {
meillo@239 241 GList *rcpt_node = NULL;
meillo@239 242 GList *alias_list = NULL;
meillo@10 243 GList *done_list = NULL;
meillo@239 244 GList *rcpt_node_next = NULL;
meillo@239 245 address *addr = NULL;
meillo@0 246
meillo@399 247 for (rcpt_node=g_list_copy(rcpt_list); rcpt_node;
meillo@399 248 rcpt_node=g_list_next(rcpt_node)) {
meillo@0 249
meillo@239 250 addr = (address *) (rcpt_node->data);
meillo@239 251 if (addr_is_local(addr)) {
meillo@390 252 DEBUG(5) debugf("alias: (orig rcpt addr) "
meillo@390 253 "expand local '%s'\n",
meillo@390 254 doglob ? addr->address :
meillo@390 255 addr->local_part);
meillo@387 256 alias_list = expand_one(alias_table, addr, doglob);
meillo@239 257 if (alias_list) {
meillo@390 258 done_list = g_list_concat(done_list,
meillo@390 259 alias_list);
meillo@10 260 }
meillo@10 261 } else {
meillo@390 262 DEBUG(5) debugf("alias: (orig rcpt addr) don't "
meillo@390 263 "expand non-local '%s'\n",
meillo@387 264 addr->address);
meillo@10 265 done_list = g_list_append(done_list, addr);
meillo@10 266 }
meillo@0 267 }
meillo@0 268
meillo@239 269 /* we're done if we don't have to remove rcpts */
meillo@239 270 if (!non_rcpt_list) {
meillo@239 271 return done_list;
meillo@239 272 }
meillo@0 273
meillo@239 274 /* delete addresses of non_rcpt_list from done_list */
meillo@390 275 for (rcpt_node = g_list_first(done_list); rcpt_node;
meillo@390 276 rcpt_node = rcpt_node_next) {
meillo@239 277 address *addr = (address *) (rcpt_node->data);
meillo@239 278 GList *non_node;
meillo@0 279
meillo@239 280 rcpt_node_next = g_list_next(rcpt_node);
meillo@239 281 foreach(non_rcpt_list, non_node) {
meillo@239 282 address *non_addr = (address *) (non_node->data);
meillo@244 283 if (addr_isequal(addr, non_addr, conf.localpartcmp)) {
meillo@390 284 done_list = g_list_remove_link(done_list,
meillo@390 285 rcpt_node);
meillo@239 286 g_list_free_1(rcpt_node);
meillo@367 287 /*
meillo@367 288 ** this address is still in the children
meillo@367 289 ** lists of the original address, simply
meillo@367 290 ** mark them delivered
meillo@367 291 */
meillo@239 292 addr_mark_delivered(addr);
meillo@239 293 break;
meillo@10 294 }
meillo@10 295 }
meillo@0 296 }
meillo@10 297 return done_list;
meillo@0 298 }