masqmail-0.2
view src/route.c @ 13:49dab67fe461
code beautifying
author | meillo@marmaro.de |
---|---|
date | Wed, 29 Oct 2008 21:10:18 +0100 |
parents | 08114f7dcc23 |
children | f671821d8222 |
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 "masqmail.h"
20 #include <fnmatch.h>
22 msgout_perhost*
23 create_msgout_perhost(gchar * host)
24 {
25 msgout_perhost *mo_ph = g_malloc(sizeof(msgout_perhost));
26 if (mo_ph) {
27 mo_ph->host = g_strdup(host);
28 mo_ph->msgout_list = NULL;
29 }
30 return mo_ph;
31 }
33 void
34 destroy_msgout_perhost(msgout_perhost * mo_ph)
35 {
36 GList *mo_node;
38 foreach(mo_ph->msgout_list, mo_node) {
39 msg_out *mo = (msg_out *) (mo_node->data);
40 /* the rcpt_list is owned by the msgout's, but not the rcpt's themselves */
41 g_list_free(mo->rcpt_list);
42 g_free(mo);
43 }
44 g_list_free(mo_ph->msgout_list);
45 g_free(mo_ph);
46 }
48 void
49 rewrite_headers(msg_out * msgout, connect_route * route)
50 {
51 /* if set_h_from_domain is set, replace domain in all
52 From: headers.
53 */
54 msgout->hdr_list = g_list_copy(msgout->msg->hdr_list);
56 /* map from addresses */
57 if (route->map_h_from_addresses != NULL) {
58 GList *hdr_node;
59 foreach(msgout->hdr_list, hdr_node) {
60 header *hdr = (header *) (hdr_node->data);
61 if (hdr->id == HEAD_FROM) {
62 header *new_hdr = copy_header(hdr);
63 if (map_address_header(new_hdr, route->map_h_from_addresses)) {
64 hdr_node->data = new_hdr;
65 /* we need this list only to carefully free the extra headers: */
66 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
67 } else
68 g_free(new_hdr);
69 }
70 }
71 } else {
72 /* replace from domain */
73 if (route->set_h_from_domain != NULL) {
74 GList *hdr_node;
76 foreach(msgout->hdr_list, hdr_node) {
77 header *hdr = (header *) (hdr_node->data);
78 if (hdr->id == HEAD_FROM) {
79 header *new_hdr = copy_header(hdr);
81 DEBUG(5) debugf("setting From: domain to %s\n", route->set_h_from_domain);
82 if (set_address_header_domain(new_hdr, route->set_h_from_domain)) {
83 hdr_node->data = new_hdr;
84 /* we need this list only to carefully free the extra headers: */
85 DEBUG(6) debugf("header = %s\n", new_hdr->header);
86 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
87 } else {
88 logwrite(LOG_ALERT, "error in set_address_header_domain(%s, %s)\n", new_hdr->value, route->set_h_from_domain);
89 }
90 }
91 }
92 }
93 }
95 /* map reply-to addresses */
96 if (route->map_h_reply_to_addresses != NULL) {
97 GList *hdr_node;
98 foreach(msgout->hdr_list, hdr_node) {
99 header *hdr = (header *) (hdr_node->data);
100 if (hdr->id == HEAD_REPLY_TO) {
101 header *new_hdr = copy_header(hdr);
102 if (map_address_header
103 (new_hdr, route->map_h_reply_to_addresses)) {
104 hdr_node->data = new_hdr;
105 /* we need this list only to carefully free the extra headers: */
106 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
107 } else
108 g_free(new_hdr);
109 }
110 }
111 } else {
112 /* replace Reply-to domain */
113 if (route->set_h_reply_to_domain != NULL) {
114 GList *hdr_node;
116 foreach(msgout->hdr_list, hdr_node) {
117 header *hdr = (header *) (hdr_node->data);
118 if (hdr->id == HEAD_REPLY_TO) {
119 header *new_hdr = copy_header(hdr);
121 set_address_header_domain(new_hdr, route-> set_h_reply_to_domain);
122 hdr_node->data = new_hdr;
123 /* we need this list only to carefully free the extra headers: */
124 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
125 }
126 }
127 }
128 }
130 /* map Mail-Followup-To addresses */
131 if (route->map_h_mail_followup_to_addresses != NULL) {
132 GList *hdr_node;
133 foreach(msgout->hdr_list, hdr_node) {
134 header *hdr = (header *) (hdr_node->data);
135 if (strncasecmp(hdr->header, "Mail-Followup-To", 16) == 0) {
136 header *new_hdr = copy_header(hdr);
137 if (map_address_header(new_hdr, route->map_h_mail_followup_to_addresses)) {
138 hdr_node->data = new_hdr;
139 /* we need this list only to carefully free the extra headers: */
140 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
141 } else
142 g_free(new_hdr);
143 }
144 }
145 }
147 /* set Sender: domain to return_path->domain */
148 if (route->expand_h_sender_domain) {
149 GList *hdr_node;
151 foreach(msgout->hdr_list, hdr_node) {
152 header *hdr = (header *) (hdr_node->data);
153 if (hdr->id == HEAD_SENDER) {
154 header *new_hdr = copy_header(hdr);
156 set_address_header_domain(new_hdr, msgout->return_path->domain);
157 hdr_node->data = new_hdr;
158 /* we need this list only to carefully free the extra headers: */
159 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
160 }
161 }
162 }
164 /* set Sender: domain to return_path->domain */
165 if (route->expand_h_sender_address) {
166 GList *hdr_node;
168 foreach(msgout->hdr_list, hdr_node) {
169 header *hdr = (header *) (hdr_node->data);
170 if (hdr->id == HEAD_SENDER) {
171 header *new_hdr;
173 new_hdr = create_header(HEAD_SENDER, "Sender: %s@%s\n", msgout->return_path->local_part, msgout->return_path->domain);
174 hdr_node->data = new_hdr;
175 /* we need this list only to carefully free the extra headers: */
176 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
177 }
178 }
179 }
181 if (msgout->xtra_hdr_list == NULL) {
182 /* nothing was changed */
183 g_list_free(msgout->hdr_list);
184 msgout->hdr_list = NULL;
185 }
186 DEBUG(5) debugf("rewrite_headers() returning\n");
187 }
189 void
190 rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
191 {
192 GList *rcpt_node;
194 if (rcpt_list == NULL)
195 return;
197 foreach(rcpt_list, rcpt_node) {
198 address *rcpt = (address *) (rcpt_node->data);
199 GList *host_node = NULL;
201 foreach(host_list, host_node) {
202 gchar *host = (gchar *) (host_node->data);
203 if (fnmatch(host, rcpt->domain, FNM_CASEFOLD) == 0)
204 break;
205 }
206 if (host_node) {
207 if (p_rcpt_list)
208 *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt);
209 } else {
210 if (p_non_rcpt_list)
211 *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt);
212 }
214 }
215 }
217 void
218 rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
219 {
220 GList *rcpt_node;
222 if (rcpt_list == NULL)
223 return;
225 foreach(rcpt_list, rcpt_node) {
226 address *rcpt = (address *) (rcpt_node->data);
227 if (addr_is_local(rcpt)) {
228 if (p_rcpt_list)
229 *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt);
230 } else {
231 if (p_non_rcpt_list)
232 *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt);
233 }
235 }
236 }
238 static gint
239 _g_list_addrcmp(gconstpointer a, gconstpointer b)
240 {
241 return addr_match((address *) a, (address *) b);
242 }
244 gboolean
245 route_is_allowed_return_path(connect_route * route, address * ret_path)
246 {
247 if (route->not_allowed_return_paths != NULL) {
248 if (g_list_find_custom(route->not_allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
249 return FALSE;
250 }
251 }
252 if (route->allowed_return_paths != NULL) {
253 if (g_list_find_custom(route->allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
254 return TRUE;
255 } else {
256 return FALSE;
257 }
258 }
259 return TRUE;
260 }
262 static gint
263 _g_list_strcmp(gconstpointer a, gconstpointer b)
264 {
265 return (gint) strcmp(a, b);
266 }
268 gboolean
269 route_is_allowed_mail_local(connect_route * route, address * ret_path)
270 {
271 gchar *loc_part = ret_path->local_part;
273 if (route->not_allowed_mail_locals != NULL) {
274 if (g_list_find_custom(route->not_allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
275 return FALSE;
276 }
277 if (route->allowed_mail_locals != NULL) {
278 if (g_list_find_custom(route->allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
279 return TRUE;
280 else
281 return FALSE;
282 }
283 return TRUE;
284 }
286 /*
287 Make lists of matching/not matching rcpts.
288 Local domains are NOT regared here, these should be sorted out previously
289 */
290 void
291 msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
292 {
293 GList *tmp_list = NULL;
294 /* sort out those domains that can be sent over this connection: */
295 if (route->allowed_rcpt_domains) {
296 DEBUG(5) debugf("testing for route->allowed_rcpt_domains\n");
297 rcptlist_with_one_of_hostlist(rcpt_list, route->allowed_rcpt_domains, &tmp_list, p_non_rcpt_list);
298 } else {
299 DEBUG(5) debugf("route->allowed_rcpt_domains == NULL\n");
300 tmp_list = g_list_copy(rcpt_list);
301 }
303 /* sort out those domains that cannot be sent over this connection: */
304 rcptlist_with_one_of_hostlist(tmp_list, route->not_allowed_rcpt_domains, p_non_rcpt_list, p_rcpt_list);
305 g_list_free(tmp_list);
306 }
308 msg_out*
309 route_prepare_msgout(connect_route * route, msg_out * msgout)
310 {
311 message *msg = msgout->msg;
312 GList *rcpt_list = msgout->rcpt_list;
314 if (rcpt_list != NULL) {
315 /* found a few */
316 DEBUG(5) {
317 GList *node;
318 debugf("rcpts for routed delivery, route = %s, id = %s\n", route->name, msg->uid);
319 foreach(rcpt_list, node) {
320 address *rcpt = (address *) (node->data);
321 debugf("rcpt for routed delivery: <%s@%s>\n", rcpt->local_part, rcpt->domain);
322 }
323 }
325 /* rewrite return path
326 if there is a table, use that
327 if an address is found and if it has a domain, use that
328 */
329 if (route->map_return_path_addresses) {
330 address *ret_path = NULL;
331 DEBUG(5) debugf("looking up %s in map_return_path_addresses\n", msg->return_path->local_part);
332 ret_path = (address *) table_find_fnmatch(route->map_return_path_addresses, msg->return_path->local_part);
333 if (ret_path) {
334 DEBUG(5) debugf("found <%s@%s>\n", ret_path->local_part, ret_path->domain);
335 if (ret_path->domain == NULL)
336 ret_path->domain = route->set_return_path_domain
337 ? route->set_return_path_domain
338 : msg->return_path->domain;
339 msgout->return_path = copy_address(ret_path);
340 }
341 }
342 if (msgout->return_path == NULL) {
343 DEBUG(5) debugf("setting return path to %s\n", route->set_return_path_domain);
344 msgout->return_path = copy_modify_address(msg->return_path, NULL, route->set_return_path_domain);
345 }
346 rewrite_headers(msgout, route);
348 return msgout;
349 }
350 return NULL;
351 }
353 /* put msgout's is msgout_list into bins (msgout_perhost structs) for each
354 host. Used if there is no mail_host.
355 route param is not used, we leave it here because that may change.
356 */
358 GList*
359 route_msgout_list(connect_route * route, GList * msgout_list)
360 {
361 GList *mo_ph_list = NULL;
362 GList *msgout_node;
364 foreach(msgout_list, msgout_node) {
365 msg_out *msgout = (msg_out *) (msgout_node->data);
366 msg_out *msgout_new;
367 GList *rcpt_list = msgout->rcpt_list;
368 GList *rcpt_node;
370 foreach(rcpt_list, rcpt_node) {
371 address *rcpt = rcpt_node->data;
372 msgout_perhost *mo_ph = NULL;
373 GList *mo_ph_node = NULL;
375 /* search host in mo_ph_list */
376 foreach(mo_ph_list, mo_ph_node) {
377 mo_ph = (msgout_perhost *) (mo_ph_node->data);
378 if (strcasecmp(mo_ph->host, rcpt->domain) == 0)
379 break;
380 }
381 if (mo_ph_node != NULL) {
382 /* there is already a rcpt for this host */
383 msg_out *msgout_last = (msg_out *) ((g_list_last(mo_ph->msgout_list))->data);
384 if (msgout_last->msg == msgout->msg) {
385 /* if it is also the same message, it must be the last one
386 appended to mo_ph->msgout_list (since outer loop goes through
387 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 }