masqmail

view src/alias.c @ 387:a408411ff8df

Added a glob-pattern aliasing facility. One use-case is virtual hosting another catch-all maildrops, but you may use it as a more flexible aliasing mechanism as well.
author markus schnalke <meillo@marmaro.de>
date Sat, 18 Feb 2012 12:35:12 +0100
parents 028bc124d744
children 68ae9182059c
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 == NULL) {
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, conf.host_name);
42 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n",
43 addr_node->data, a->address);
44 if (addr_isequal(a, addr, conf.localpartcmp)) {
45 destroy_address(a);
46 /* in local_hosts but also in not_local_addresses */
47 return FALSE;
48 }
49 destroy_address(a);
50 }
51 /* in local_hosts */
52 return TRUE;
53 }
54 foreach(conf.local_addresses, addr_node) {
55 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
56 DEBUG(6) debugf("local_addresses: addr_node->data=%s a->address=%s\n",
57 addr_node->data, a->address);
58 if (addr_isequal(a, addr, conf.localpartcmp)) {
59 destroy_address(a);
60 /* in local_addresses */
61 return TRUE;
62 }
63 destroy_address(a);
64 }
65 return FALSE;
66 }
68 static GList*
69 parse_list(gchar *line)
70 {
71 GList *list = NULL;
72 gchar buf[256];
73 gchar *p, *q;
75 p = line;
76 while (*p != '\0') {
77 q = buf;
78 while (isspace(*p))
79 p++;
80 if (*p != '"') {
81 while (*p && (*p != ',') && (q < buf + 255))
82 *(q++) = *(p++);
83 *q = '\0';
84 } else {
85 gboolean escape = FALSE;
86 p++;
87 while (*p && (*p != '"' || escape) && (q < buf + 255)) {
88 if ((*p == '\\') && !escape)
89 escape = TRUE;
90 else {
91 escape = FALSE;
92 *(q++) = *p;
93 }
94 p++;
95 }
96 *q = '\0';
97 while (*p && (*p != ','))
98 p++;
99 }
100 list = g_list_append(list, g_strdup(g_strchomp(buf)));
101 if (*p)
102 p++;
103 }
104 return list;
105 }
107 static int
108 globaliascmp(const char *pattern, const char *addr)
109 {
110 if (conf.localpartcmp==strcasecmp) {
111 return fnmatch(pattern, addr, FNM_CASEFOLD);
112 } else if (strncasecmp(addr, "postmaster", 10)==0) {
113 /*
114 ** postmaster must always be matched caseless
115 ** see RFC 822 and RFC 5321
116 */
117 return fnmatch(pattern, addr, FNM_CASEFOLD);
118 } else {
119 /* case-sensitive */
120 return fnmatch(pattern, addr, 0);
121 }
122 }
124 /*
125 ** addr is assumed to be local and no pipe address nor not-to-expand
126 */
127 static GList*
128 expand_one(GList *alias_table, address *addr, int doglob)
129 {
130 GList *val_list;
131 GList *val_node;
132 GList *alias_list = NULL;
133 GList *alias_node;
134 gchar *val;
136 /* expand the local alias */
137 DEBUG(6) debugf("alias: '%s' is local and will get expanded\n",
138 doglob ? addr->address : addr->local_part);
140 if (doglob) {
141 val = (gchar *) table_find_func(alias_table, addr->address,
142 globaliascmp);
144 } else if (strcasecmp(addr->local_part, "postmaster") == 0) {
145 /*
146 ** postmaster must always be matched caseless
147 ** see RFC 822 and RFC 5321
148 */
149 val = (gchar *) table_find_func(alias_table, addr->local_part,
150 strcasecmp);
151 } else {
152 val = (gchar *) table_find_func(alias_table, addr->local_part,
153 conf.localpartcmp);
154 }
155 if (!val) {
156 DEBUG(5) debugf("alias: '%s' is fully expanded, hence "
157 "completed\n",
158 doglob ? addr->address : addr->local_part);
159 return g_list_append(NULL, addr);
160 }
162 DEBUG(5) debugf("alias: '%s' -> '%s'\n",
163 doglob ? addr->address : addr->local_part, val);
164 val_list = parse_list(val);
165 alias_list = NULL;
167 foreach(val_list, val_node) {
168 gchar *val = (gchar *) (val_node->data);
169 address *alias_addr;
171 DEBUG(6) debugf("alias: processing '%s'\n", val);
173 if (val[0] == '\\') {
174 DEBUG(5) debugf("alias: '%s' is marked as final, hence completed\n", val);
175 alias_addr = create_address_qualified(val+1, TRUE, conf.host_name);
176 g_free(val);
177 DEBUG(6) debugf("alias: address generated: '%s'\n",
178 alias_addr->address);
179 alias_list = g_list_append(alias_list, alias_addr);
180 continue;
181 }
183 if (val[0] == '|') {
184 DEBUG(5) debugf("alias: '%s' is a pipe address\n", val);
185 alias_addr = create_address_pipe(val);
186 g_free(val);
187 DEBUG(6) debugf("alias: pipe generated: %s\n",
188 alias_addr->local_part);
189 alias_list = g_list_append(alias_list, alias_addr);
190 continue;
191 }
193 alias_addr = create_address_qualified(val, TRUE, conf.host_name);
194 g_free(val);
196 if (!addr_is_local(alias_addr)) {
197 DEBUG(5) debugf("alias: '%s' is non-local, hence completed\n",
198 alias_addr->address);
199 alias_list = g_list_append(alias_list, alias_addr);
200 continue;
201 }
203 /* addr is local and to expand at this point */
204 /* but first ... search in parents for loops: */
205 if (addr_isequal_parent(addr, alias_addr, conf.localpartcmp)) {
206 /* loop detected, ignore this path */
207 logwrite(LOG_ALERT, "alias: detected loop, hence ignoring '%s'\n",
208 alias_addr->local_part);
209 continue;
210 }
211 alias_addr->parent = addr;
213 /* recurse */
214 DEBUG(6) debugf("alias: >>\n");
215 alias_node = expand_one(alias_table, alias_addr, doglob);
216 DEBUG(6) debugf("alias: <<\n");
217 if (alias_node) {
218 alias_list = g_list_concat(alias_list, alias_node);
219 }
220 }
221 g_list_free(val_list);
222 addr->children = g_list_copy(alias_list);
224 return alias_list;
225 }
227 GList*
228 alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list,
229 int doglob)
230 {
231 GList *rcpt_node = NULL;
232 GList *alias_list = NULL;
233 GList *done_list = NULL;
234 GList *rcpt_node_next = NULL;
235 address *addr = NULL;
237 for (rcpt_node=g_list_copy(rcpt_list);
238 rcpt_node;
239 rcpt_node=g_list_next(rcpt_node)) {
241 addr = (address *) (rcpt_node->data);
242 if (addr_is_local(addr)) {
243 DEBUG(5) debugf("alias: (orig rcpt addr) expand local '%s'\n",
244 doglob ? addr->address : addr->local_part);
245 alias_list = expand_one(alias_table, addr, doglob);
246 if (alias_list) {
247 done_list = g_list_concat(done_list, alias_list);
248 }
249 } else {
250 DEBUG(5) debugf("alias: (orig rcpt addr) don't expand non-local '%s'\n",
251 addr->address);
252 done_list = g_list_append(done_list, addr);
253 }
254 }
256 /* we're done if we don't have to remove rcpts */
257 if (!non_rcpt_list) {
258 return done_list;
259 }
261 /* delete addresses of non_rcpt_list from done_list */
262 for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) {
263 address *addr = (address *) (rcpt_node->data);
264 GList *non_node;
266 rcpt_node_next = g_list_next(rcpt_node);
267 foreach(non_rcpt_list, non_node) {
268 address *non_addr = (address *) (non_node->data);
269 if (addr_isequal(addr, non_addr, conf.localpartcmp)) {
270 done_list = g_list_remove_link(done_list, rcpt_node);
271 g_list_free_1(rcpt_node);
272 /*
273 ** this address is still in the children
274 ** lists of the original address, simply
275 ** mark them delivered
276 */
277 addr_mark_delivered(addr);
278 break;
279 }
280 }
281 }
282 return done_list;
283 }