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