masqmail

view src/route.c @ 367:b27f66555ba8

Reformated multiline comments to have leading asterisks on each line Now we use: /* ** comment */ This makes the indent style simpler, too.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Oct 2011 10:20:59 +0200
parents 41958685480d
children 4cab237ce923
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-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 <fnmatch.h>
23 #include "masqmail.h"
25 msgout_perhost*
26 create_msgout_perhost(gchar *host)
27 {
28 msgout_perhost *mo_ph = g_malloc(sizeof(msgout_perhost));
29 if (mo_ph) {
30 mo_ph->host = g_strdup(host);
31 mo_ph->msgout_list = NULL;
32 }
33 return mo_ph;
34 }
36 void
37 destroy_msgout_perhost(msgout_perhost *mo_ph)
38 {
39 GList *mo_node;
41 foreach(mo_ph->msgout_list, mo_node) {
42 msg_out *mo = (msg_out *) (mo_node->data);
43 /* the rcpt_list is owned by the msgout's, but not the rcpt's themselves */
44 g_list_free(mo->rcpt_list);
45 g_free(mo);
46 }
47 g_list_free(mo_ph->msgout_list);
48 g_free(mo_ph);
49 }
51 void
52 rewrite_headers(msg_out *msgout, connect_route *route)
53 {
54 /*
55 ** if set_h_from_domain is set, replace domain in all
56 ** From: headers.
57 */
58 msgout->hdr_list = g_list_copy(msgout->msg->hdr_list);
60 /* map from addresses */
61 if (route->map_h_from_addresses != NULL) {
62 GList *hdr_node;
63 foreach(msgout->hdr_list, hdr_node) {
64 header *hdr = (header *) (hdr_node->data);
65 if (hdr->id == HEAD_FROM) {
66 header *new_hdr = copy_header(hdr);
67 if (map_address_header(new_hdr, route->map_h_from_addresses)) {
68 hdr_node->data = new_hdr;
69 /* we need this list only to carefully free the extra headers: */
70 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
71 } else
72 g_free(new_hdr);
73 }
74 }
75 } else {
76 /* replace from domain */
77 if (route->set_h_from_domain != NULL) {
78 GList *hdr_node;
80 foreach(msgout->hdr_list, hdr_node) {
81 header *hdr = (header *) (hdr_node->data);
82 if (hdr->id == HEAD_FROM) {
83 header *new_hdr = copy_header(hdr);
85 DEBUG(5) debugf("setting From: domain to %s\n", route->set_h_from_domain);
86 if (set_address_header_domain(new_hdr, route->set_h_from_domain)) {
87 hdr_node->data = new_hdr;
88 /* we need this list only to carefully free the extra headers: */
89 DEBUG(6) debugf("header = %s\n", new_hdr->header);
90 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
91 } else {
92 logwrite(LOG_ALERT, "error in set_address_header_domain(%s, %s)\n",
93 new_hdr->value, route->set_h_from_domain);
94 }
95 }
96 }
97 }
98 }
100 /* map reply-to addresses */
101 if (route->map_h_reply_to_addresses != NULL) {
102 GList *hdr_node;
103 foreach(msgout->hdr_list, hdr_node) {
104 header *hdr = (header *) (hdr_node->data);
105 if (hdr->id == HEAD_REPLY_TO) {
106 header *new_hdr = copy_header(hdr);
107 if (map_address_header
108 (new_hdr, route->map_h_reply_to_addresses)) {
109 hdr_node->data = new_hdr;
110 /* we need this list only to carefully free the extra headers: */
111 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
112 } else
113 g_free(new_hdr);
114 }
115 }
116 } else {
117 /* replace Reply-to domain */
118 if (route->set_h_reply_to_domain != NULL) {
119 GList *hdr_node;
121 foreach(msgout->hdr_list, hdr_node) {
122 header *hdr = (header *) (hdr_node->data);
123 if (hdr->id == HEAD_REPLY_TO) {
124 header *new_hdr = copy_header(hdr);
126 set_address_header_domain(new_hdr, route-> set_h_reply_to_domain);
127 hdr_node->data = new_hdr;
128 /* we need this list only to carefully free the extra headers: */
129 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
130 }
131 }
132 }
133 }
135 /* map Mail-Followup-To addresses */
136 if (route->map_h_mail_followup_to_addresses != NULL) {
137 GList *hdr_node;
138 foreach(msgout->hdr_list, hdr_node) {
139 header *hdr = (header *) (hdr_node->data);
140 if (strncasecmp(hdr->header, "Mail-Followup-To", 16) == 0) {
141 header *new_hdr = copy_header(hdr);
142 if (map_address_header(new_hdr, route->map_h_mail_followup_to_addresses)) {
143 hdr_node->data = new_hdr;
144 /* we need this list only to carefully free the extra headers: */
145 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
146 } else
147 g_free(new_hdr);
148 }
149 }
150 }
152 /* set Sender: domain to return_path->domain */
153 if (route->expand_h_sender_domain) {
154 GList *hdr_node;
156 foreach(msgout->hdr_list, hdr_node) {
157 header *hdr = (header *) (hdr_node->data);
158 if (hdr->id == HEAD_SENDER) {
159 header *new_hdr = copy_header(hdr);
161 set_address_header_domain(new_hdr, msgout->return_path->domain);
162 hdr_node->data = new_hdr;
163 /* we need this list only to carefully free the extra headers: */
164 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
165 }
166 }
167 }
169 /* set Sender: domain to return_path->domain */
170 if (route->expand_h_sender_address) {
171 GList *hdr_node;
173 foreach(msgout->hdr_list, hdr_node) {
174 header *hdr = (header *) (hdr_node->data);
175 if (hdr->id == HEAD_SENDER) {
176 header *new_hdr;
178 new_hdr = create_header(HEAD_SENDER, "Sender: %s@%s\n",
179 msgout->return_path->local_part, msgout->return_path->domain);
180 hdr_node->data = new_hdr;
181 /* we need this list only to carefully free the extra headers: */
182 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
183 }
184 }
185 }
187 if (msgout->xtra_hdr_list == NULL) {
188 /* nothing was changed */
189 g_list_free(msgout->hdr_list);
190 msgout->hdr_list = NULL;
191 }
192 DEBUG(5) debugf("rewrite_headers() returning\n");
193 }
195 /*
196 ** Split a recipient list into the three groups:
197 ** - local recipients
198 ** - those maching the patterns
199 ** - those not matching the patterns
200 ** If patterns is NULL: only splitting between local and others is done.
201 */
202 void
203 split_rcpts(GList *rcpt_list, GList *patterns, GList **rl_local,
204 GList **rl_matching, 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 /*
222 ** if patterns is NULL, host_node will be NULL,
223 ** hence all non-locals are put to others
224 */
225 foreach(patterns, host_node) {
226 gchar *host = (gchar *) (host_node->data);
227 if (fnmatch(host, rcpt->domain, FNM_CASEFOLD) == 0)
228 break;
229 }
230 if (host_node) {
231 if (rl_matching)
232 *rl_matching = g_list_append(*rl_matching, rcpt);
233 } else {
234 if (rl_others)
235 *rl_others = g_list_append(*rl_others, rcpt);
236 }
237 }
238 }
239 }
241 /*
242 ** Return a new list of the local rcpts in the rcpt_list
243 ** TODO: This function is almost exactly the same as remote_rcpts(). Merge?
244 */
245 GList*
246 local_rcpts(GList *rcpt_list)
247 {
248 GList *rcpt_node;
249 GList *local_rcpts = NULL;
250 address *rcpt = NULL;
252 if (!rcpt_list) {
253 return NULL;
254 }
255 foreach(rcpt_list, rcpt_node) {
256 rcpt = (address *) (rcpt_node->data);
257 if (addr_is_local(rcpt)) {
258 local_rcpts = g_list_append(local_rcpts, rcpt);
259 }
260 }
261 return local_rcpts;
262 }
264 /*
265 ** Return a new list of non-local rcpts in the rcpt_list
266 ** TODO: This function is almost exactly the same as local_rcpts(). Merge?
267 */
268 GList*
269 remote_rcpts(GList *rcpt_list)
270 {
271 GList *rcpt_node;
272 GList *remote_rcpts = NULL;
273 address *rcpt = NULL;
275 if (!rcpt_list) {
276 return NULL;
277 }
278 foreach(rcpt_list, rcpt_node) {
279 rcpt = (address *) (rcpt_node->data);
280 if (!addr_is_local(rcpt)) {
281 remote_rcpts = g_list_append(remote_rcpts, rcpt);
282 }
283 }
284 return remote_rcpts;
285 }
287 static gint
288 _g_list_addrcmp(gconstpointer pattern, gconstpointer addr)
289 {
290 int res;
291 address *patternaddr = (address*) pattern;
292 address *stringaddr = (address*) addr;
294 DEBUG(6) debugf("_g_list_addrcmp: pattern `%s' `%s' on string `%s' `%s'\n",
295 patternaddr->local_part, patternaddr->domain,
296 stringaddr->local_part, stringaddr->domain);
297 /* TODO: check if we should match here dependent on caseless_matching */
298 res = fnmatch(patternaddr->local_part, stringaddr->local_part, 0);
299 if (res != 0) {
300 DEBUG(6) debugf("_g_list_addrcmp: ... failed on local_part\n");
301 return res;
302 }
303 res = fnmatch(patternaddr->domain, stringaddr->domain, FNM_CASEFOLD);
304 DEBUG(6) debugf("_g_list_addrcmp: ... %s\n", (res==0) ? "matched" : "failed on domain");
305 return res;
306 }
308 gboolean
309 route_sender_is_allowed(connect_route *route, address *ret_path)
310 {
311 if (route->denied_senders && g_list_find_custom(route->denied_senders, ret_path, _g_list_addrcmp)) {
312 return FALSE;
313 }
314 if (route->allowed_senders) {
315 if (g_list_find_custom(route->allowed_senders, ret_path, _g_list_addrcmp)) {
316 return TRUE;
317 } else {
318 return FALSE;
319 }
320 }
321 return TRUE;
322 }
324 /*
325 ** Make lists of matching/not matching rcpts.
326 ** Local domains are NOT regared here, these should be sorted out previously
327 */
328 void
329 route_split_rcpts(connect_route *route, GList *rcpt_list, GList **p_rcpt_list, GList **p_non_rcpt_list)
330 {
331 GList *tmp_list = NULL;
332 /* sort out those domains that can be sent over this connection: */
333 if (route->allowed_recipients) {
334 DEBUG(5) debugf("testing for route->allowed_recipients\n");
335 split_rcpts(rcpt_list, route->allowed_recipients, NULL, &tmp_list, p_non_rcpt_list);
336 } else {
337 DEBUG(5) debugf("route->allowed_recipients == NULL\n");
338 tmp_list = g_list_copy(rcpt_list);
339 }
341 /* sort out those domains that cannot be sent over this connection: */
342 split_rcpts(tmp_list, route->denied_recipients, NULL, p_non_rcpt_list, p_rcpt_list);
343 g_list_free(tmp_list);
344 }
346 msg_out*
347 route_prepare_msgout(connect_route *route, msg_out *msgout)
348 {
349 message *msg = msgout->msg;
350 GList *rcpt_list = msgout->rcpt_list;
352 if (rcpt_list != NULL) {
353 /* found a few */
354 DEBUG(5) {
355 GList *node;
356 debugf("rcpts for routed delivery, route = %s, id = %s\n", route->name, msg->uid);
357 foreach(rcpt_list, node) {
358 address *rcpt = (address *) (node->data);
359 debugf(" rcpt for routed delivery: <%s@%s>\n",
360 rcpt->local_part, rcpt->domain);
361 }
362 }
364 /*
365 ** rewrite return path if there is a table, use that
366 ** if an address is found and if it has a domain, use that
367 */
368 if (route->map_return_path_addresses) {
369 address *ret_path = NULL;
370 DEBUG(5) debugf("looking up %s in map_return_path_addresses\n", msg->return_path->local_part);
371 ret_path = (address *) table_find_fnmatch(route->map_return_path_addresses, msg->return_path->local_part);
372 if (ret_path) {
373 DEBUG(5) debugf("found <%s@%s>\n", ret_path->local_part, ret_path->domain);
374 if (ret_path->domain == NULL)
375 ret_path->domain = route->set_return_path_domain
376 ? route->set_return_path_domain
377 : msg->return_path->domain;
378 msgout->return_path = copy_address(ret_path);
379 }
380 }
381 if (msgout->return_path == NULL) {
382 DEBUG(5) debugf("setting return path to %s\n", route->set_return_path_domain);
383 msgout->return_path = copy_modify_address(msg->return_path, NULL, route->set_return_path_domain);
384 }
385 rewrite_headers(msgout, route);
387 return msgout;
388 }
389 return NULL;
390 }
392 /*
393 ** put msgout's is msgout_list into bins (msgout_perhost structs) for each
394 ** host. Used if there is no mail_host.
395 ** route param is not used, we leave it here because that may change.
396 */
397 GList*
398 route_msgout_list(connect_route *route, GList *msgout_list)
399 {
400 GList *mo_ph_list = NULL;
401 GList *msgout_node;
403 foreach(msgout_list, msgout_node) {
404 msg_out *msgout = (msg_out *) (msgout_node->data);
405 msg_out *msgout_new;
406 GList *rcpt_list = msgout->rcpt_list;
407 GList *rcpt_node;
409 foreach(rcpt_list, rcpt_node) {
410 address *rcpt = rcpt_node->data;
411 msgout_perhost *mo_ph = NULL;
412 GList *mo_ph_node = NULL;
414 /* search host in mo_ph_list */
415 foreach(mo_ph_list, mo_ph_node) {
416 mo_ph = (msgout_perhost *) (mo_ph_node->data);
417 if (strcasecmp(mo_ph->host, rcpt->domain) == 0)
418 break;
419 }
420 if (mo_ph_node != NULL) {
421 /* there is already a rcpt for this host */
422 msg_out *msgout_last = (msg_out *) ((g_list_last(mo_ph->msgout_list))->data);
423 if (msgout_last->msg == msgout->msg) {
424 /*
425 ** if it is also the same message,
426 ** it must be the last one
427 ** appended to mo_ph->msgout_list
428 ** (since outer loop goes through
429 ** msgout_list)
430 */
431 msgout_last->rcpt_list = g_list_append(msgout_last->rcpt_list, rcpt);
432 } else {
433 /* if not, we append a new msgout */
434 /* make a copy of msgout */
435 msgout_new = create_msg_out(msgout->msg);
436 msgout_new->return_path = msgout->return_path;
437 msgout_new->hdr_list = msgout->hdr_list;
439 /* append our rcpt to it */
440 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
441 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
442 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
443 }
444 } else {
445 /* this rcpt to goes to another host */
446 mo_ph = create_msgout_perhost(rcpt->domain);
447 mo_ph_list = g_list_append(mo_ph_list, mo_ph);
449 /* make a copy of msgout */
450 msgout_new = create_msg_out(msgout->msg);
451 msgout_new->return_path = msgout->return_path;
452 msgout_new->hdr_list = msgout->hdr_list;
454 /* append our rcpt to it */
455 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
456 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
457 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
458 } /* if mo_ph != NULL */
459 } /* foreach(rcpt_list, ... */
460 } /* foreach(msgout_list, ... */
462 return mo_ph_list;
463 }