Mercurial > masqmail
comparison src/alias.c @ 239:31ee44f45787
refactored alias.c heavily
especially substituted the loop-based alias_expand()
with a recursive approach. Now alias_expand() wraps
alias_one() which recursively expands aliases.
In principle the ``data processing'' is the same but
now it's clearer structured and thus easier to understand
IMO.
The loop might have been faster but I don't care for
speed -- the most simple solution is the best. It's fast
enough, that is sufficient.
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Mon, 25 Oct 2010 15:35:28 -0300 |
parents | 3f33a0feeeb0 |
children | bc9d9cd9ee8e |
comparison
equal
deleted
inserted
replaced
238:ec28ce798b79 | 239:31ee44f45787 |
---|---|
1 /* MasqMail | 1 /* MasqMail |
2 Copyright (C) 2000-2001 Oliver Kurth | 2 Copyright (C) 2000-2001 Oliver Kurth |
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de> | |
3 | 4 |
4 This program is free software; you can redistribute it and/or modify | 5 This program is free software; you can redistribute it and/or modify |
5 it under the terms of the GNU General Public License as published by | 6 it under the terms of the GNU General Public License as published by |
6 the Free Software Foundation; either version 2 of the License, or | 7 the Free Software Foundation; either version 2 of the License, or |
7 (at your option) any later version. | 8 (at your option) any later version. |
24 { | 25 { |
25 GList *dom_node; | 26 GList *dom_node; |
26 GList *addr_node; | 27 GList *addr_node; |
27 address *a; | 28 address *a; |
28 | 29 |
30 if (addr->domain == NULL) { | |
31 return TRUE; | |
32 } | |
29 foreach(conf.local_hosts, dom_node) { | 33 foreach(conf.local_hosts, dom_node) { |
30 if (addr->domain == NULL) | 34 /* Note: FNM_CASEFOLD is a GNU extension */ |
31 return TRUE; | 35 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) != 0) { |
32 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0) { | 36 /* no match, try next */ |
33 foreach(conf.not_local_addresses, addr_node) { | 37 continue; |
34 a = create_address_qualified(addr_node->data, TRUE, conf.host_name); | 38 } |
35 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n", | 39 foreach(conf.not_local_addresses, addr_node) { |
36 addr_node->data, a->address); | 40 a = create_address_qualified(addr_node->data, TRUE, conf.host_name); |
37 if (addr_isequal(a, addr)) { | 41 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n", |
38 destroy_address(a); | 42 addr_node->data, a->address); |
39 return FALSE; | 43 if (addr_isequal(a, addr)) { |
40 } | |
41 destroy_address(a); | 44 destroy_address(a); |
42 } | 45 /* in local_hosts but also in not_local_addresses */ |
43 return TRUE; | 46 return FALSE; |
44 } | 47 } |
48 destroy_address(a); | |
49 } | |
50 /* in local_hosts */ | |
51 return TRUE; | |
45 } | 52 } |
46 foreach(conf.local_addresses, addr_node) { | 53 foreach(conf.local_addresses, addr_node) { |
47 a = create_address_qualified(addr_node->data, TRUE, conf.host_name); | 54 a = create_address_qualified(addr_node->data, TRUE, conf.host_name); |
48 DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n", | 55 DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n", |
49 addr_node->data, a->address); | 56 addr_node->data, a->address); |
50 if (addr_isequal(a, addr)) { | 57 if (addr_isequal(a, addr)) { |
51 destroy_address(a); | 58 destroy_address(a); |
59 /* in local_addresses */ | |
52 return TRUE; | 60 return TRUE; |
53 } | 61 } |
54 destroy_address(a); | 62 destroy_address(a); |
55 } | 63 } |
56 return FALSE; | 64 return FALSE; |
73 p = line; | 81 p = line; |
74 while (*p != '\0') { | 82 while (*p != '\0') { |
75 q = buf; | 83 q = buf; |
76 while (isspace(*p)) | 84 while (isspace(*p)) |
77 p++; | 85 p++; |
78 if (*p != '\"') { | 86 if (*p != '"') { |
79 while (*p && (*p != ',') && (q < buf + 255)) | 87 while (*p && (*p != ',') && (q < buf + 255)) |
80 *(q++) = *(p++); | 88 *(q++) = *(p++); |
81 *q = '\0'; | 89 *q = '\0'; |
82 } else { | 90 } else { |
83 gboolean escape = FALSE; | 91 gboolean escape = FALSE; |
84 p++; | 92 p++; |
85 while (*p && (*p != '\"' || escape) && (q < buf + 255)) { | 93 while (*p && (*p != '"' || escape) && (q < buf + 255)) { |
86 if ((*p == '\\') && !escape) | 94 if ((*p == '\\') && !escape) |
87 escape = TRUE; | 95 escape = TRUE; |
88 else { | 96 else { |
89 escape = FALSE; | 97 escape = FALSE; |
90 *(q++) = *p; | 98 *(q++) = *p; |
100 p++; | 108 p++; |
101 } | 109 } |
102 return list; | 110 return list; |
103 } | 111 } |
104 | 112 |
113 /* | |
114 addr is assumed to be local and no pipe address nor not-to-expand | |
115 */ | |
116 static GList* | |
117 expand_one(GList* alias_table, address* addr) | |
118 { | |
119 GList *val_list; | |
120 GList *val_node; | |
121 GList *alias_list = NULL; | |
122 GList *alias_node; | |
123 gchar *val; | |
124 address* alias_addr; | |
125 | |
126 /* expand the local alias */ | |
127 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n", addr->local_part); | |
128 | |
129 if (strcasecmp(addr->local_part, "postmaster") == 0) { | |
130 /* postmaster must always be matched caseless | |
131 see RFC 822 and RFC 5321 */ | |
132 val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp); | |
133 } else { | |
134 val = (gchar *) table_find_func(alias_table, addr->local_part, conf.alias_local_cmp); | |
135 } | |
136 if (!val) { | |
137 DEBUG(5) debugf("alias: '%s' is fully expanded, hence completed\n", | |
138 addr->local_part); | |
139 return g_list_append(NULL, addr); | |
140 } | |
141 | |
142 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val); | |
143 val_list = parse_list(val); | |
144 alias_list = NULL; | |
145 | |
146 foreach(val_list, val_node) { | |
147 gchar *val = (gchar *) (val_node->data); | |
148 address *alias_addr; | |
149 address *addr_parent = NULL; | |
150 | |
151 DEBUG(6) debugf("alias: processing '%s'\n", val); | |
152 | |
153 if (val[0] == '\\') { | |
154 DEBUG(5) debugf("alias: '%s' is marked as final, hence completed\n", val); | |
155 alias_addr = create_address_qualified(val+1, TRUE, conf.host_name); | |
156 g_free(val); | |
157 DEBUG(6) debugf("alias: address generated: '%s'\n", | |
158 alias_addr->local_part); | |
159 alias_list = g_list_append(alias_list, alias_addr); | |
160 continue; | |
161 } | |
162 | |
163 if (val[0] == '|') { | |
164 DEBUG(5) debugf("alias: '%s' is a pipe address\n", val); | |
165 alias_addr = create_address_pipe(val+1); | |
166 g_free(val); | |
167 DEBUG(6) debugf("alias: pipe generated: %s\n", | |
168 alias_addr->local_part); | |
169 alias_list = g_list_append(alias_list, alias_addr); | |
170 continue; | |
171 } | |
172 | |
173 alias_addr = create_address_qualified(val, TRUE, conf.host_name); | |
174 g_free(val); | |
175 | |
176 if (!addr_is_local(alias_addr)) { | |
177 DEBUG(5) debugf("alias: '%s@%s' is non-local, hence completed\n", | |
178 alias_addr->local_part, alias_addr->domain); | |
179 alias_list = g_list_append(alias_list, alias_addr); | |
180 continue; | |
181 } | |
182 | |
183 /* addr is local, so let's go into recursion and expand again, | |
184 but first ... search in parents for loops: */ | |
185 for (addr_parent=addr; addr_parent; addr_parent=addr_parent->parent) { | |
186 if (addr_isequal_alias(alias_addr, addr_parent)) { | |
187 break; | |
188 } | |
189 } | |
190 if (addr_parent) { | |
191 /* loop detected, ignore this path */ | |
192 logwrite(LOG_ALERT, | |
193 "alias: detected loop (%s -> %s), hence ignoring\n", | |
194 addr_parent->local_part, addr->local_part); | |
195 continue; | |
196 } | |
197 alias_addr->parent = addr; | |
198 | |
199 /* recurse */ | |
200 DEBUG(6) debugf("alias: >>\n"); | |
201 alias_node = expand_one(alias_table, alias_addr); | |
202 DEBUG(6) debugf("alias: <<\n"); | |
203 if (alias_node) { | |
204 alias_list = g_list_concat(alias_list, alias_node); | |
205 } | |
206 } | |
207 g_list_free(val_list); | |
208 addr->children = g_list_copy(alias_list); | |
209 | |
210 return alias_list; | |
211 } | |
212 | |
105 GList* | 213 GList* |
106 alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list) | 214 alias_expand(GList* alias_table, GList* rcpt_list, GList* non_rcpt_list) |
107 { | 215 { |
216 GList *rcpt_node = NULL; | |
217 GList *alias_list = NULL; | |
108 GList *done_list = NULL; | 218 GList *done_list = NULL; |
109 GList *rcpt_node = g_list_copy(rcpt_list); | 219 GList *rcpt_node_next = NULL; |
110 | 220 address *addr = NULL; |
111 while (rcpt_node != NULL) { | 221 |
222 for (rcpt_node=g_list_copy(rcpt_list); | |
223 rcpt_node; | |
224 rcpt_node=g_list_next(rcpt_node)) { | |
225 | |
226 addr = (address *) (rcpt_node->data); | |
227 if (addr_is_local(addr)) { | |
228 DEBUG(5) debugf("alias: (orig rcpt addr) expand local '%s'\n", | |
229 addr->local_part); | |
230 alias_list = expand_one(alias_table, addr); | |
231 if (alias_list) { | |
232 done_list = g_list_concat(done_list, alias_list); | |
233 } | |
234 } else { | |
235 DEBUG(5) debugf("alias: (orig rcpt addr) don't expand non-local '%s@%s'\n", | |
236 addr->local_part, addr->domain); | |
237 done_list = g_list_append(done_list, addr); | |
238 } | |
239 } | |
240 | |
241 /* we're done if we don't have to remove rcpts */ | |
242 if (!non_rcpt_list) { | |
243 return done_list; | |
244 } | |
245 | |
246 /* delete addresses of non_rcpt_list from done_list */ | |
247 for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) { | |
112 address *addr = (address *) (rcpt_node->data); | 248 address *addr = (address *) (rcpt_node->data); |
113 DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain); | 249 GList *non_node; |
114 /* if(addr_is_local(addr) && (addr->local_part[0] != '|') && */ | 250 |
115 if (addr_is_local(addr) && !(addr->flags & ADDR_FLAG_NOEXPAND)) { | 251 rcpt_node_next = g_list_next(rcpt_node); |
116 gchar *val; | 252 foreach(non_rcpt_list, non_node) { |
117 | 253 address *non_addr = (address *) (non_node->data); |
118 DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part); | 254 if (addr_isequal(addr, non_addr)) { |
119 if (strcasecmp(addr->local_part, "postmaster") == 0) | 255 done_list = g_list_remove_link(done_list, rcpt_node); |
120 /* postmaster needs always to be matched caseless | 256 g_list_free_1(rcpt_node); |
121 see RFC 822 and RFC 5321 */ | 257 /* this address is still in the children lists |
122 val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp); | 258 of the original address, simply mark them delivered */ |
123 else | 259 addr_mark_delivered(addr); |
124 val = (gchar *) table_find_func(alias_table, addr->local_part, conf.alias_local_cmp); | 260 break; |
125 | |
126 if (val != NULL) { | |
127 GList *val_list = parse_list(val); | |
128 GList *val_node; | |
129 GList *alias_list = NULL; | |
130 | |
131 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val); | |
132 foreach(val_list, val_node) { | |
133 gchar *val = (gchar *) (val_node->data); | |
134 address *alias_addr; | |
135 address *addr_parent = NULL; | |
136 | |
137 if (val[0] == '|') { | |
138 DEBUG(5) debugf("alias: %s is a pipe address\n", val); | |
139 alias_addr = create_address_pipe(val); | |
140 DEBUG(5) debugf("alias_pipe: %s is a pipe address\n", alias_addr->local_part); | |
141 } else if (val[0] == '\\') { | |
142 DEBUG(5) debugf("alias: shall not be expanded: '%s'\n", val); | |
143 alias_addr = create_address_qualified(&(val[1]), TRUE, conf.host_name); | |
144 alias_addr->flags |= ADDR_FLAG_NOEXPAND; | |
145 DEBUG(5) debugf("alias: not expanded: '%s'\n", alias_addr->local_part); | |
146 } else { | |
147 alias_addr = create_address_qualified(val, TRUE, conf.host_name); | |
148 | |
149 /* search in parents for loops: */ | |
150 for (addr_parent = addr; addr_parent; addr_parent = addr_parent->parent) { | |
151 if (addr_isequal_alias (alias_addr, addr_parent)) { | |
152 logwrite(LOG_ALERT, | |
153 "detected alias loop, (ignoring): %s@%s -> %s@%s\n", | |
154 addr_parent->local_part, | |
155 addr_parent->domain, | |
156 addr->local_part, addr->domain); | |
157 break; | |
158 } | |
159 } | |
160 } | |
161 if (!addr_parent) { | |
162 alias_list = g_list_append(alias_list, alias_addr); | |
163 alias_addr->parent = addr; | |
164 } | |
165 g_free(val); | |
166 } | |
167 g_list_free(val_list); | |
168 addr->children = g_list_copy(alias_list); | |
169 rcpt_node = g_list_concat(rcpt_node, alias_list); | |
170 } else { | |
171 DEBUG(5) debugf("alias: '%s' is completed\n", addr->local_part); | |
172 done_list = g_list_append(done_list, addr); | |
173 } | |
174 } else { | |
175 DEBUG(5) debugf("alias: '%s@%s' is not local\n", addr->local_part, addr->domain); | |
176 done_list = g_list_append(done_list, addr); | |
177 } | |
178 rcpt_node = g_list_next(rcpt_node); | |
179 } | |
180 | |
181 /* delete addresses from done_list if they are in the non_rcpt_list */ | |
182 if (non_rcpt_list) { | |
183 GList *rcpt_node_next; | |
184 for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) { | |
185 address *addr = (address *) (rcpt_node->data); | |
186 GList *non_node; | |
187 | |
188 rcpt_node_next = g_list_next(rcpt_node); | |
189 | |
190 foreach(non_rcpt_list, non_node) { | |
191 address *non_addr = (address *) (non_node->data); | |
192 if (addr_isequal(addr, non_addr)) { | |
193 done_list = g_list_remove_link(done_list, rcpt_node); | |
194 g_list_free_1(rcpt_node); | |
195 addr_mark_delivered(addr); /* this address is still in the children lists of the original address */ | |
196 break; | |
197 } | |
198 } | 261 } |
199 } | 262 } |
200 } | 263 } |
201 return done_list; | 264 return done_list; |
202 } | 265 } |