masqmail

view src/alias.c @ 409:6874a4f265c1

Broke long lines.
author markus schnalke <meillo@marmaro.de>
date Wed, 29 Feb 2012 11:03:46 +0100
parents 68ae9182059c
children ddac877ced95
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 == '\\') {
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 == '|') {
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); rcpt_node;
248 rcpt_node=g_list_next(rcpt_node)) {
250 addr = (address *) (rcpt_node->data);
251 if (addr_is_local(addr)) {
252 DEBUG(5) debugf("alias: (orig rcpt addr) "
253 "expand local '%s'\n",
254 doglob ? addr->address :
255 addr->local_part);
256 alias_list = expand_one(alias_table, addr, doglob);
257 if (alias_list) {
258 done_list = g_list_concat(done_list,
259 alias_list);
260 }
261 } else {
262 DEBUG(5) debugf("alias: (orig rcpt addr) don't "
263 "expand non-local '%s'\n",
264 addr->address);
265 done_list = g_list_append(done_list, addr);
266 }
267 }
269 /* we're done if we don't have to remove rcpts */
270 if (!non_rcpt_list) {
271 return done_list;
272 }
274 /* delete addresses of non_rcpt_list from done_list */
275 for (rcpt_node = g_list_first(done_list); rcpt_node;
276 rcpt_node = rcpt_node_next) {
277 address *addr = (address *) (rcpt_node->data);
278 GList *non_node;
280 rcpt_node_next = g_list_next(rcpt_node);
281 foreach(non_rcpt_list, non_node) {
282 address *non_addr = (address *) (non_node->data);
283 if (addr_isequal(addr, non_addr, conf.localpartcmp)) {
284 done_list = g_list_remove_link(done_list,
285 rcpt_node);
286 g_list_free_1(rcpt_node);
287 /*
288 ** this address is still in the children
289 ** lists of the original address, simply
290 ** mark them delivered
291 */
292 addr_mark_delivered(addr);
293 break;
294 }
295 }
296 }
297 return done_list;
298 }