masqmail

view src/alias.c @ 237:5f9f3a65032e

refactoring: new function split_rcpts() replaces two others split_rcpts() merges rcptlist_with_one_of_hostlist() and rcptlist_with_addr_is_local() into one with hardly adding complexity I'd actually say that the overall complexity decreased. Have a look at the comment for split_rcpts() in route.c
author markus schnalke <meillo@marmaro.de>
date Fri, 22 Oct 2010 11:56:47 -0300
parents a80ebfa16cd5
children 31ee44f45787
line source
1 /* MasqMail
2 Copyright (C) 2000-2001 Oliver Kurth
4 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 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
19 #include "masqmail.h"
20 #include <fnmatch.h>
22 gboolean
23 addr_is_local(address * addr)
24 {
25 GList *dom_node;
26 GList *addr_node;
27 address *a;
29 foreach(conf.local_hosts, dom_node) {
30 if (addr->domain == NULL)
31 return TRUE;
32 if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0) {
33 foreach(conf.not_local_addresses, addr_node) {
34 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
35 DEBUG(6) debugf("not_local_addresses: addr_node->data=%s a->address=%s\n",
36 addr_node->data, a->address);
37 if (addr_isequal(a, addr)) {
38 destroy_address(a);
39 return FALSE;
40 }
41 destroy_address(a);
42 }
43 return TRUE;
44 }
45 }
46 foreach(conf.local_addresses, addr_node) {
47 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",
49 addr_node->data, a->address);
50 if (addr_isequal(a, addr)) {
51 destroy_address(a);
52 return TRUE;
53 }
54 destroy_address(a);
55 }
56 return FALSE;
57 }
59 static gboolean
60 addr_isequal_alias(address * addr1, address * addr2)
61 {
62 return (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0)
63 && (strcasecmp(addr1->domain, addr2->domain) == 0);
64 }
66 static GList*
67 parse_list(gchar * line)
68 {
69 GList *list = NULL;
70 gchar buf[256];
71 gchar *p, *q;
73 p = line;
74 while (*p != '\0') {
75 q = buf;
76 while (isspace(*p))
77 p++;
78 if (*p != '\"') {
79 while (*p && (*p != ',') && (q < buf + 255))
80 *(q++) = *(p++);
81 *q = '\0';
82 } else {
83 gboolean escape = FALSE;
84 p++;
85 while (*p && (*p != '\"' || escape) && (q < buf + 255)) {
86 if ((*p == '\\') && !escape)
87 escape = TRUE;
88 else {
89 escape = FALSE;
90 *(q++) = *p;
91 }
92 p++;
93 }
94 *q = '\0';
95 while (*p && (*p != ','))
96 p++;
97 }
98 list = g_list_append(list, g_strdup(g_strchomp(buf)));
99 if (*p)
100 p++;
101 }
102 return list;
103 }
105 GList*
106 alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list)
107 {
108 GList *done_list = NULL;
109 GList *rcpt_node = g_list_copy(rcpt_list);
111 while (rcpt_node != NULL) {
112 address *addr = (address *) (rcpt_node->data);
113 DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain);
114 /* if(addr_is_local(addr) && (addr->local_part[0] != '|') && */
115 if (addr_is_local(addr) && !(addr->flags & ADDR_FLAG_NOEXPAND)) {
116 gchar *val;
118 DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part);
119 if (strcasecmp(addr->local_part, "postmaster") == 0)
120 /* postmaster needs always to be matched caseless
121 see RFC 822 and RFC 5321 */
122 val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp);
123 else
124 val = (gchar *) table_find_func(alias_table, addr->local_part, conf.alias_local_cmp);
126 if (val != NULL) {
127 GList *val_list = parse_list(val);
128 GList *val_node;
129 GList *alias_list = NULL;
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;
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);
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 }
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;
188 rcpt_node_next = g_list_next(rcpt_node);
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 }
199 }
200 }
201 return done_list;
202 }