masqmail

view src/alias.c @ 392:c5fd796ea06e

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