masqmail

annotate src/alias.c @ 434:f2a7271746d1

Removes Freshmeat.net from the docs The site, which was later renamed to freecode.com, is no longer maintained (contains only a static copy).
author markus schnalke <meillo@marmaro.de>
date Sat, 07 Feb 2015 11:45:07 +0100
parents c7cc3c03193c
children
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@416 141 char addrstr[BUFSIZ];
meillo@416 142
meillo@416 143 if (doglob) {
meillo@416 144 snprintf(addrstr, sizeof addrstr, "%s@%s",
meillo@416 145 addr->local_part, addr->domain);
meillo@416 146 } else {
meillo@416 147 snprintf(addrstr, sizeof addrstr, "%s", addr->local_part);
meillo@416 148 }
meillo@239 149
meillo@239 150 /* expand the local alias */
meillo@387 151 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n",
meillo@416 152 addrstr);
meillo@239 153
meillo@387 154 if (doglob) {
meillo@416 155 val = (gchar *) table_find_func(alias_table, addrstr,
meillo@387 156 globaliascmp);
meillo@387 157
meillo@387 158 } else if (strcasecmp(addr->local_part, "postmaster") == 0) {
meillo@390 159 /* postmaster must always be matched caseless
meillo@390 160 ** see RFC 822 and RFC 5321 */
meillo@416 161 val = (gchar *) table_find_func(alias_table, addrstr,
meillo@387 162 strcasecmp);
meillo@239 163 } else {
meillo@416 164 val = (gchar *) table_find_func(alias_table, addrstr,
meillo@387 165 conf.localpartcmp);
meillo@239 166 }
meillo@239 167 if (!val) {
meillo@387 168 DEBUG(5) debugf("alias: '%s' is fully expanded, hence "
meillo@416 169 "completed\n", addrstr);
meillo@239 170 return g_list_append(NULL, addr);
meillo@239 171 }
meillo@239 172
meillo@416 173 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addrstr, val);
meillo@239 174 val_list = parse_list(val);
meillo@239 175 alias_list = NULL;
meillo@239 176
meillo@239 177 foreach(val_list, val_node) {
meillo@239 178 gchar *val = (gchar *) (val_node->data);
meillo@239 179 address *alias_addr;
meillo@239 180
meillo@239 181 DEBUG(6) debugf("alias: processing '%s'\n", val);
meillo@239 182
meillo@399 183 if (*val == '\\') {
meillo@390 184 DEBUG(5) debugf("alias: '%s' is marked as final, "
meillo@390 185 "hence completed\n", val);
meillo@390 186 alias_addr = create_address_qualified(val+1, TRUE,
meillo@390 187 conf.host_name);
meillo@239 188 g_free(val);
meillo@239 189 DEBUG(6) debugf("alias: address generated: '%s'\n",
meillo@387 190 alias_addr->address);
meillo@239 191 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 192 continue;
meillo@239 193 }
meillo@239 194
meillo@399 195 if (*val == '|') {
meillo@390 196 DEBUG(5) debugf("alias: '%s' is a pipe address\n",
meillo@390 197 val);
meillo@309 198 alias_addr = create_address_pipe(val);
meillo@239 199 g_free(val);
meillo@239 200 DEBUG(6) debugf("alias: pipe generated: %s\n",
meillo@239 201 alias_addr->local_part);
meillo@239 202 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 203 continue;
meillo@239 204 }
meillo@239 205
meillo@390 206 alias_addr = create_address_qualified(val, TRUE,
meillo@390 207 conf.host_name);
meillo@239 208 g_free(val);
meillo@239 209
meillo@239 210 if (!addr_is_local(alias_addr)) {
meillo@390 211 DEBUG(5) debugf("alias: '%s' is non-local, "
meillo@390 212 "hence completed\n",
meillo@387 213 alias_addr->address);
meillo@239 214 alias_list = g_list_append(alias_list, alias_addr);
meillo@239 215 continue;
meillo@239 216 }
meillo@239 217
meillo@242 218 /* addr is local and to expand at this point */
meillo@242 219 /* but first ... search in parents for loops: */
meillo@244 220 if (addr_isequal_parent(addr, alias_addr, conf.localpartcmp)) {
meillo@239 221 /* loop detected, ignore this path */
meillo@390 222 logwrite(LOG_ALERT, "alias: detected loop, "
meillo@390 223 "hence ignoring '%s'\n",
meillo@416 224 alias_addr->address);
meillo@239 225 continue;
meillo@239 226 }
meillo@239 227 alias_addr->parent = addr;
meillo@239 228
meillo@239 229 /* recurse */
meillo@239 230 DEBUG(6) debugf("alias: >>\n");
meillo@387 231 alias_node = expand_one(alias_table, alias_addr, doglob);
meillo@239 232 DEBUG(6) debugf("alias: <<\n");
meillo@239 233 if (alias_node) {
meillo@239 234 alias_list = g_list_concat(alias_list, alias_node);
meillo@239 235 }
meillo@239 236 }
meillo@239 237 g_list_free(val_list);
meillo@239 238 addr->children = g_list_copy(alias_list);
meillo@239 239
meillo@239 240 return alias_list;
meillo@239 241 }
meillo@239 242
meillo@10 243 GList*
meillo@387 244 alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list,
meillo@387 245 int doglob)
meillo@0 246 {
meillo@239 247 GList *rcpt_node = NULL;
meillo@239 248 GList *alias_list = NULL;
meillo@10 249 GList *done_list = NULL;
meillo@239 250 GList *rcpt_node_next = NULL;
meillo@239 251 address *addr = NULL;
meillo@0 252
meillo@399 253 for (rcpt_node=g_list_copy(rcpt_list); rcpt_node;
meillo@399 254 rcpt_node=g_list_next(rcpt_node)) {
meillo@0 255
meillo@239 256 addr = (address *) (rcpt_node->data);
meillo@239 257 if (addr_is_local(addr)) {
meillo@416 258 DEBUG(5) debugf("alias: expand local '%s' "
meillo@416 259 "(orig rcpt addr)\n", addr->address);
meillo@387 260 alias_list = expand_one(alias_table, addr, doglob);
meillo@239 261 if (alias_list) {
meillo@390 262 done_list = g_list_concat(done_list,
meillo@390 263 alias_list);
meillo@10 264 }
meillo@10 265 } else {
meillo@416 266 DEBUG(5) debugf("alias: don't expand non-local '%s' "
meillo@416 267 "(orig rcpt addr)\n", addr->address);
meillo@10 268 done_list = g_list_append(done_list, addr);
meillo@10 269 }
meillo@0 270 }
meillo@0 271
meillo@239 272 /* we're done if we don't have to remove rcpts */
meillo@239 273 if (!non_rcpt_list) {
meillo@239 274 return done_list;
meillo@239 275 }
meillo@0 276
meillo@239 277 /* delete addresses of non_rcpt_list from done_list */
meillo@390 278 for (rcpt_node = g_list_first(done_list); rcpt_node;
meillo@390 279 rcpt_node = rcpt_node_next) {
meillo@239 280 address *addr = (address *) (rcpt_node->data);
meillo@239 281 GList *non_node;
meillo@0 282
meillo@239 283 rcpt_node_next = g_list_next(rcpt_node);
meillo@239 284 foreach(non_rcpt_list, non_node) {
meillo@239 285 address *non_addr = (address *) (non_node->data);
meillo@244 286 if (addr_isequal(addr, non_addr, conf.localpartcmp)) {
meillo@390 287 done_list = g_list_remove_link(done_list,
meillo@390 288 rcpt_node);
meillo@239 289 g_list_free_1(rcpt_node);
meillo@367 290 /*
meillo@367 291 ** this address is still in the children
meillo@367 292 ** lists of the original address, simply
meillo@367 293 ** mark them delivered
meillo@367 294 */
meillo@239 295 addr_mark_delivered(addr);
meillo@239 296 break;
meillo@10 297 }
meillo@10 298 }
meillo@0 299 }
meillo@10 300 return done_list;
meillo@0 301 }