masqmail
view src/route.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 | 87df0aa99cc7 |
line source
1 /* MasqMail
2 Copyright (C) 1999-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 <fnmatch.h>
21 #include "masqmail.h"
23 msgout_perhost*
24 create_msgout_perhost(gchar * host)
25 {
26 msgout_perhost *mo_ph = g_malloc(sizeof(msgout_perhost));
27 if (mo_ph) {
28 mo_ph->host = g_strdup(host);
29 mo_ph->msgout_list = NULL;
30 }
31 return mo_ph;
32 }
34 void
35 destroy_msgout_perhost(msgout_perhost * mo_ph)
36 {
37 GList *mo_node;
39 foreach(mo_ph->msgout_list, mo_node) {
40 msg_out *mo = (msg_out *) (mo_node->data);
41 /* the rcpt_list is owned by the msgout's, but not the rcpt's themselves */
42 g_list_free(mo->rcpt_list);
43 g_free(mo);
44 }
45 g_list_free(mo_ph->msgout_list);
46 g_free(mo_ph);
47 }
49 void
50 rewrite_headers(msg_out * msgout, connect_route * route)
51 {
52 /* if set_h_from_domain is set, replace domain in all
53 From: headers.
54 */
55 msgout->hdr_list = g_list_copy(msgout->msg->hdr_list);
57 /* map from addresses */
58 if (route->map_h_from_addresses != NULL) {
59 GList *hdr_node;
60 foreach(msgout->hdr_list, hdr_node) {
61 header *hdr = (header *) (hdr_node->data);
62 if (hdr->id == HEAD_FROM) {
63 header *new_hdr = copy_header(hdr);
64 if (map_address_header(new_hdr, route->map_h_from_addresses)) {
65 hdr_node->data = new_hdr;
66 /* we need this list only to carefully free the extra headers: */
67 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
68 } else
69 g_free(new_hdr);
70 }
71 }
72 } else {
73 /* replace from domain */
74 if (route->set_h_from_domain != NULL) {
75 GList *hdr_node;
77 foreach(msgout->hdr_list, hdr_node) {
78 header *hdr = (header *) (hdr_node->data);
79 if (hdr->id == HEAD_FROM) {
80 header *new_hdr = copy_header(hdr);
82 DEBUG(5) debugf("setting From: domain to %s\n", route->set_h_from_domain);
83 if (set_address_header_domain(new_hdr, route->set_h_from_domain)) {
84 hdr_node->data = new_hdr;
85 /* we need this list only to carefully free the extra headers: */
86 DEBUG(6) debugf("header = %s\n", new_hdr->header);
87 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
88 } else {
89 logwrite(LOG_ALERT, "error in set_address_header_domain(%s, %s)\n",
90 new_hdr->value, route->set_h_from_domain);
91 }
92 }
93 }
94 }
95 }
97 /* map reply-to addresses */
98 if (route->map_h_reply_to_addresses != NULL) {
99 GList *hdr_node;
100 foreach(msgout->hdr_list, hdr_node) {
101 header *hdr = (header *) (hdr_node->data);
102 if (hdr->id == HEAD_REPLY_TO) {
103 header *new_hdr = copy_header(hdr);
104 if (map_address_header
105 (new_hdr, route->map_h_reply_to_addresses)) {
106 hdr_node->data = new_hdr;
107 /* we need this list only to carefully free the extra headers: */
108 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
109 } else
110 g_free(new_hdr);
111 }
112 }
113 } else {
114 /* replace Reply-to domain */
115 if (route->set_h_reply_to_domain != NULL) {
116 GList *hdr_node;
118 foreach(msgout->hdr_list, hdr_node) {
119 header *hdr = (header *) (hdr_node->data);
120 if (hdr->id == HEAD_REPLY_TO) {
121 header *new_hdr = copy_header(hdr);
123 set_address_header_domain(new_hdr, route-> set_h_reply_to_domain);
124 hdr_node->data = new_hdr;
125 /* we need this list only to carefully free the extra headers: */
126 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
127 }
128 }
129 }
130 }
132 /* map Mail-Followup-To addresses */
133 if (route->map_h_mail_followup_to_addresses != NULL) {
134 GList *hdr_node;
135 foreach(msgout->hdr_list, hdr_node) {
136 header *hdr = (header *) (hdr_node->data);
137 if (strncasecmp(hdr->header, "Mail-Followup-To", 16) == 0) {
138 header *new_hdr = copy_header(hdr);
139 if (map_address_header(new_hdr, route->map_h_mail_followup_to_addresses)) {
140 hdr_node->data = new_hdr;
141 /* we need this list only to carefully free the extra headers: */
142 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
143 } else
144 g_free(new_hdr);
145 }
146 }
147 }
149 /* set Sender: domain to return_path->domain */
150 if (route->expand_h_sender_domain) {
151 GList *hdr_node;
153 foreach(msgout->hdr_list, hdr_node) {
154 header *hdr = (header *) (hdr_node->data);
155 if (hdr->id == HEAD_SENDER) {
156 header *new_hdr = copy_header(hdr);
158 set_address_header_domain(new_hdr, msgout->return_path->domain);
159 hdr_node->data = new_hdr;
160 /* we need this list only to carefully free the extra headers: */
161 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
162 }
163 }
164 }
166 /* set Sender: domain to return_path->domain */
167 if (route->expand_h_sender_address) {
168 GList *hdr_node;
170 foreach(msgout->hdr_list, hdr_node) {
171 header *hdr = (header *) (hdr_node->data);
172 if (hdr->id == HEAD_SENDER) {
173 header *new_hdr;
175 new_hdr = create_header(HEAD_SENDER, "Sender: %s@%s\n",
176 msgout->return_path->local_part, msgout->return_path->domain);
177 hdr_node->data = new_hdr;
178 /* we need this list only to carefully free the extra headers: */
179 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
180 }
181 }
182 }
184 if (msgout->xtra_hdr_list == NULL) {
185 /* nothing was changed */
186 g_list_free(msgout->hdr_list);
187 msgout->hdr_list = NULL;
188 }
189 DEBUG(5) debugf("rewrite_headers() returning\n");
190 }
192 /*
193 Split a recipient list into the three groups:
194 - local recipients
195 - local net recipients
196 - other/remote/online recipients
197 It should be possible to call the function like:
198 split_rcpts(rcpts, hostlist, local, others, others);
199 This would split online between local and localnet+online recipients.
200 (untested yet; remove this line if you saw it worked -- meillo 2010-10-21)
201 If host_list is NULL, only splitting between local and other is done.
202 */
203 void
204 split_rcpts(GList* rcpt_list, GList* localnets, GList** rl_local, GList** rl_localnet, GList** rl_others)
205 {
206 GList *rcpt_node;
207 GList *host_node = NULL;
208 address *rcpt = NULL;
210 if (rcpt_list == NULL)
211 return;
213 foreach(rcpt_list, rcpt_node) {
214 rcpt = (address *) (rcpt_node->data);
215 host_node = NULL;
217 if (addr_is_local(rcpt)) {
218 if (rl_local)
219 *rl_local = g_list_append(*rl_local, rcpt);
220 } else {
221 /* if localnets is NULL, host_node will be NULL,
222 hence all non-locals are put to others */
223 foreach(localnets, host_node) {
224 gchar *host = (gchar *) (host_node->data);
225 if (fnmatch(host, rcpt->domain, FNM_CASEFOLD) == 0)
226 break;
227 }
228 if (host_node) {
229 if (rl_localnet)
230 *rl_localnet = g_list_append(*rl_localnet, rcpt);
231 } else {
232 if (rl_others)
233 *rl_others = g_list_append(*rl_others, rcpt);
234 }
235 }
236 }
237 }
239 static gint
240 _g_list_addrcmp(gconstpointer a, gconstpointer b)
241 {
242 return addr_match((address *) a, (address *) b);
243 }
245 gboolean
246 route_is_allowed_return_path(connect_route * route, address * ret_path)
247 {
248 if (route->not_allowed_return_paths != NULL) {
249 if (g_list_find_custom(route->not_allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
250 return FALSE;
251 }
252 }
253 if (route->allowed_return_paths != NULL) {
254 if (g_list_find_custom(route->allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
255 return TRUE;
256 } else {
257 return FALSE;
258 }
259 }
260 return TRUE;
261 }
263 static gint
264 _g_list_strcmp(gconstpointer a, gconstpointer b)
265 {
266 return (gint) strcmp(a, b);
267 }
269 gboolean
270 route_is_allowed_mail_local(connect_route * route, address * ret_path)
271 {
272 gchar *loc_part = ret_path->local_part;
274 if (route->not_allowed_mail_locals != NULL) {
275 if (g_list_find_custom(route->not_allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
276 return FALSE;
277 }
278 if (route->allowed_mail_locals != NULL) {
279 if (g_list_find_custom(route->allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
280 return TRUE;
281 else
282 return FALSE;
283 }
284 return TRUE;
285 }
287 /*
288 Make lists of matching/not matching rcpts.
289 Local domains are NOT regared here, these should be sorted out previously
290 */
291 void
292 msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
293 {
294 GList *tmp_list = NULL;
295 /* sort out those domains that can be sent over this connection: */
296 if (route->allowed_rcpt_domains) {
297 DEBUG(5) debugf("testing for route->allowed_rcpt_domains\n");
298 split_rcpts(rcpt_list, route->allowed_rcpt_domains, NULL, &tmp_list, p_non_rcpt_list);
299 } else {
300 DEBUG(5) debugf("route->allowed_rcpt_domains == NULL\n");
301 tmp_list = g_list_copy(rcpt_list);
302 }
304 /* sort out those domains that cannot be sent over this connection: */
305 split_rcpts(tmp_list, route->not_allowed_rcpt_domains, NULL, p_non_rcpt_list, p_rcpt_list);
306 g_list_free(tmp_list);
307 }
309 msg_out*
310 route_prepare_msgout(connect_route * route, msg_out * msgout)
311 {
312 message *msg = msgout->msg;
313 GList *rcpt_list = msgout->rcpt_list;
315 if (rcpt_list != NULL) {
316 /* found a few */
317 DEBUG(5) {
318 GList *node;
319 debugf("rcpts for routed delivery, route = %s, id = %s\n", route->name, msg->uid);
320 foreach(rcpt_list, node) {
321 address *rcpt = (address *) (node->data);
322 debugf(" rcpt for routed delivery: <%s@%s>\n",
323 rcpt->local_part, rcpt->domain);
324 }
325 }
327 /* rewrite return path if there is a table, use that
328 if an address is found and if it has a domain, use that
329 */
330 if (route->map_return_path_addresses) {
331 address *ret_path = NULL;
332 DEBUG(5) debugf("looking up %s in map_return_path_addresses\n", msg->return_path->local_part);
333 ret_path = (address *) table_find_fnmatch(route->map_return_path_addresses, msg->return_path->local_part);
334 if (ret_path) {
335 DEBUG(5) debugf("found <%s@%s>\n", ret_path->local_part, ret_path->domain);
336 if (ret_path->domain == NULL)
337 ret_path->domain = route->set_return_path_domain
338 ? route->set_return_path_domain
339 : msg->return_path->domain;
340 msgout->return_path = copy_address(ret_path);
341 }
342 }
343 if (msgout->return_path == NULL) {
344 DEBUG(5) debugf("setting return path to %s\n", route->set_return_path_domain);
345 msgout->return_path = copy_modify_address(msg->return_path, NULL, route->set_return_path_domain);
346 }
347 rewrite_headers(msgout, route);
349 return msgout;
350 }
351 return NULL;
352 }
354 /* put msgout's is msgout_list into bins (msgout_perhost structs) for each
355 host. Used if there is no mail_host.
356 route param is not used, we leave it here because that may change.
357 */
359 GList*
360 route_msgout_list(connect_route * route, GList * msgout_list)
361 {
362 GList *mo_ph_list = NULL;
363 GList *msgout_node;
365 foreach(msgout_list, msgout_node) {
366 msg_out *msgout = (msg_out *) (msgout_node->data);
367 msg_out *msgout_new;
368 GList *rcpt_list = msgout->rcpt_list;
369 GList *rcpt_node;
371 foreach(rcpt_list, rcpt_node) {
372 address *rcpt = rcpt_node->data;
373 msgout_perhost *mo_ph = NULL;
374 GList *mo_ph_node = NULL;
376 /* search host in mo_ph_list */
377 foreach(mo_ph_list, mo_ph_node) {
378 mo_ph = (msgout_perhost *) (mo_ph_node->data);
379 if (strcasecmp(mo_ph->host, rcpt->domain) == 0)
380 break;
381 }
382 if (mo_ph_node != NULL) {
383 /* there is already a rcpt for this host */
384 msg_out *msgout_last = (msg_out *) ((g_list_last(mo_ph->msgout_list))->data);
385 if (msgout_last->msg == msgout->msg) {
386 /* if it is also the same message, it must be the last one appended
387 to mo_ph->msgout_list (since outer loop goes through msgout_list) */
388 msgout_last->rcpt_list = g_list_append(msgout_last->rcpt_list, rcpt);
389 } else {
390 /* if not, we append a new msgout */
391 /* make a copy of msgout */
392 msgout_new = create_msg_out(msgout->msg);
393 msgout_new->return_path = msgout->return_path;
394 msgout_new->hdr_list = msgout->hdr_list;
396 /* append our rcpt to it */
397 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
398 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
399 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
400 }
401 } else {
402 /* this rcpt to goes to another host */
403 mo_ph = create_msgout_perhost(rcpt->domain);
404 mo_ph_list = g_list_append(mo_ph_list, mo_ph);
406 /* make a copy of msgout */
407 msgout_new = create_msg_out(msgout->msg);
408 msgout_new->return_path = msgout->return_path;
409 msgout_new->hdr_list = msgout->hdr_list;
411 /* append our rcpt to it */
412 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
413 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
414 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
415 } /* if mo_ph != NULL */
416 } /* foreach(rcpt_list, ... */
417 } /* foreach(msgout_list, ... */
419 return mo_ph_list;
420 }