masqmail

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