masqmail
view 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 |
line source
1 /* MasqMail
2 Copyright (C) 2000-2001 Oliver Kurth
3 Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
20 #include "masqmail.h"
21 #include <fnmatch.h>
23 gboolean
24 addr_is_local(address * addr)
25 {
26 GList *dom_node;
27 GList *addr_node;
28 address *a;
30 if (addr->domain == NULL) {
31 return TRUE;
32 }
33 foreach(conf.local_hosts, dom_node) {
34 /* Note: FNM_CASEFOLD is a GNU extension */
35 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) != 0) {
36 /* no match, try next */
37 continue;
38 }
39 foreach(conf.not_local_addresses, addr_node) {
40 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
41 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n",
42 addr_node->data, a->address);
43 if (addr_isequal(a, addr)) {
44 destroy_address(a);
45 /* in local_hosts but also in not_local_addresses */
46 return FALSE;
47 }
48 destroy_address(a);
49 }
50 /* in local_hosts */
51 return TRUE;
52 }
53 foreach(conf.local_addresses, addr_node) {
54 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
55 DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n",
56 addr_node->data, a->address);
57 if (addr_isequal(a, addr)) {
58 destroy_address(a);
59 /* in local_addresses */
60 return TRUE;
61 }
62 destroy_address(a);
63 }
64 return FALSE;
65 }
67 static gboolean
68 addr_isequal_alias(address * addr1, address * addr2)
69 {
70 return (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0)
71 && (strcasecmp(addr1->domain, addr2->domain) == 0);
72 }
74 static GList*
75 parse_list(gchar * line)
76 {
77 GList *list = NULL;
78 gchar buf[256];
79 gchar *p, *q;
81 p = line;
82 while (*p != '\0') {
83 q = buf;
84 while (isspace(*p))
85 p++;
86 if (*p != '"') {
87 while (*p && (*p != ',') && (q < buf + 255))
88 *(q++) = *(p++);
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 list = g_list_append(list, g_strdup(g_strchomp(buf)));
107 if (*p)
108 p++;
109 }
110 return list;
111 }
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;
126 /* expand the local alias */
127 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n", addr->local_part);
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 }
142 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
143 val_list = parse_list(val);
144 alias_list = NULL;
146 foreach(val_list, val_node) {
147 gchar *val = (gchar *) (val_node->data);
148 address *alias_addr;
149 address *addr_parent = NULL;
151 DEBUG(6) debugf("alias: processing '%s'\n", val);
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 }
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 }
173 alias_addr = create_address_qualified(val, TRUE, conf.host_name);
174 g_free(val);
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 }
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;
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);
210 return alias_list;
211 }
213 GList*
214 alias_expand(GList* alias_table, GList* rcpt_list, GList* non_rcpt_list)
215 {
216 GList *rcpt_node = NULL;
217 GList *alias_list = NULL;
218 GList *done_list = NULL;
219 GList *rcpt_node_next = NULL;
220 address *addr = NULL;
222 for (rcpt_node=g_list_copy(rcpt_list);
223 rcpt_node;
224 rcpt_node=g_list_next(rcpt_node)) {
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 }
241 /* we're done if we don't have to remove rcpts */
242 if (!non_rcpt_list) {
243 return done_list;
244 }
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) {
248 address *addr = (address *) (rcpt_node->data);
249 GList *non_node;
251 rcpt_node_next = g_list_next(rcpt_node);
252 foreach(non_rcpt_list, non_node) {
253 address *non_addr = (address *) (non_node->data);
254 if (addr_isequal(addr, non_addr)) {
255 done_list = g_list_remove_link(done_list, rcpt_node);
256 g_list_free_1(rcpt_node);
257 /* this address is still in the children lists
258 of the original address, simply mark them delivered */
259 addr_mark_delivered(addr);
260 break;
261 }
262 }
263 }
264 return done_list;
265 }