masqmail

annotate src/route.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children f671821d8222
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 1999-2001 Oliver Kurth
meillo@0 3
meillo@0 4 This program is free software; you can redistribute it and/or modify
meillo@0 5 it under the terms of the GNU General Public License as published by
meillo@0 6 the Free Software Foundation; either version 2 of the License, or
meillo@0 7 (at your option) any later version.
meillo@0 8
meillo@0 9 This program is distributed in the hope that it will be useful,
meillo@0 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
meillo@0 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
meillo@0 12 GNU General Public License for more details.
meillo@0 13
meillo@0 14 You should have received a copy of the GNU General Public License
meillo@0 15 along with this program; if not, write to the Free Software
meillo@0 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
meillo@0 17 */
meillo@0 18
meillo@0 19 #include "masqmail.h"
meillo@0 20 #include <fnmatch.h>
meillo@0 21
meillo@10 22 msgout_perhost*
meillo@10 23 create_msgout_perhost(gchar * host)
meillo@0 24 {
meillo@10 25 msgout_perhost *mo_ph = g_malloc(sizeof(msgout_perhost));
meillo@10 26 if (mo_ph) {
meillo@10 27 mo_ph->host = g_strdup(host);
meillo@10 28 mo_ph->msgout_list = NULL;
meillo@10 29 }
meillo@10 30 return mo_ph;
meillo@0 31 }
meillo@0 32
meillo@10 33 void
meillo@10 34 destroy_msgout_perhost(msgout_perhost * mo_ph)
meillo@0 35 {
meillo@10 36 GList *mo_node;
meillo@0 37
meillo@10 38 foreach(mo_ph->msgout_list, mo_node) {
meillo@10 39 msg_out *mo = (msg_out *) (mo_node->data);
meillo@10 40 /* the rcpt_list is owned by the msgout's, but not the rcpt's themselves */
meillo@10 41 g_list_free(mo->rcpt_list);
meillo@10 42 g_free(mo);
meillo@10 43 }
meillo@10 44 g_list_free(mo_ph->msgout_list);
meillo@10 45 g_free(mo_ph);
meillo@0 46 }
meillo@0 47
meillo@10 48 void
meillo@10 49 rewrite_headers(msg_out * msgout, connect_route * route)
meillo@0 50 {
meillo@10 51 /* if set_h_from_domain is set, replace domain in all
meillo@10 52 From: headers.
meillo@10 53 */
meillo@10 54 msgout->hdr_list = g_list_copy(msgout->msg->hdr_list);
meillo@0 55
meillo@10 56 /* map from addresses */
meillo@10 57 if (route->map_h_from_addresses != NULL) {
meillo@10 58 GList *hdr_node;
meillo@10 59 foreach(msgout->hdr_list, hdr_node) {
meillo@10 60 header *hdr = (header *) (hdr_node->data);
meillo@10 61 if (hdr->id == HEAD_FROM) {
meillo@10 62 header *new_hdr = copy_header(hdr);
meillo@10 63 if (map_address_header(new_hdr, route->map_h_from_addresses)) {
meillo@10 64 hdr_node->data = new_hdr;
meillo@10 65 /* we need this list only to carefully free the extra headers: */
meillo@10 66 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 67 } else
meillo@10 68 g_free(new_hdr);
meillo@10 69 }
meillo@10 70 }
meillo@10 71 } else {
meillo@10 72 /* replace from domain */
meillo@10 73 if (route->set_h_from_domain != NULL) {
meillo@10 74 GList *hdr_node;
meillo@10 75
meillo@10 76 foreach(msgout->hdr_list, hdr_node) {
meillo@10 77 header *hdr = (header *) (hdr_node->data);
meillo@10 78 if (hdr->id == HEAD_FROM) {
meillo@10 79 header *new_hdr = copy_header(hdr);
meillo@10 80
meillo@10 81 DEBUG(5) debugf("setting From: domain to %s\n", route->set_h_from_domain);
meillo@10 82 if (set_address_header_domain(new_hdr, route->set_h_from_domain)) {
meillo@10 83 hdr_node->data = new_hdr;
meillo@10 84 /* we need this list only to carefully free the extra headers: */
meillo@10 85 DEBUG(6) debugf("header = %s\n", new_hdr->header);
meillo@10 86 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 87 } else {
meillo@10 88 logwrite(LOG_ALERT, "error in set_address_header_domain(%s, %s)\n", new_hdr->value, route->set_h_from_domain);
meillo@10 89 }
meillo@10 90 }
meillo@10 91 }
meillo@10 92 }
meillo@0 93 }
meillo@0 94
meillo@10 95 /* map reply-to addresses */
meillo@10 96 if (route->map_h_reply_to_addresses != NULL) {
meillo@10 97 GList *hdr_node;
meillo@10 98 foreach(msgout->hdr_list, hdr_node) {
meillo@10 99 header *hdr = (header *) (hdr_node->data);
meillo@10 100 if (hdr->id == HEAD_REPLY_TO) {
meillo@10 101 header *new_hdr = copy_header(hdr);
meillo@10 102 if (map_address_header
meillo@10 103 (new_hdr, route->map_h_reply_to_addresses)) {
meillo@10 104 hdr_node->data = new_hdr;
meillo@10 105 /* we need this list only to carefully free the extra headers: */
meillo@10 106 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 107 } else
meillo@10 108 g_free(new_hdr);
meillo@10 109 }
meillo@10 110 }
meillo@10 111 } else {
meillo@10 112 /* replace Reply-to domain */
meillo@10 113 if (route->set_h_reply_to_domain != NULL) {
meillo@10 114 GList *hdr_node;
meillo@10 115
meillo@10 116 foreach(msgout->hdr_list, hdr_node) {
meillo@10 117 header *hdr = (header *) (hdr_node->data);
meillo@10 118 if (hdr->id == HEAD_REPLY_TO) {
meillo@10 119 header *new_hdr = copy_header(hdr);
meillo@10 120
meillo@10 121 set_address_header_domain(new_hdr, route-> set_h_reply_to_domain);
meillo@10 122 hdr_node->data = new_hdr;
meillo@10 123 /* we need this list only to carefully free the extra headers: */
meillo@10 124 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 125 }
meillo@10 126 }
meillo@10 127 }
meillo@0 128 }
meillo@0 129
meillo@10 130 /* map Mail-Followup-To addresses */
meillo@10 131 if (route->map_h_mail_followup_to_addresses != NULL) {
meillo@10 132 GList *hdr_node;
meillo@10 133 foreach(msgout->hdr_list, hdr_node) {
meillo@10 134 header *hdr = (header *) (hdr_node->data);
meillo@10 135 if (strncasecmp(hdr->header, "Mail-Followup-To", 16) == 0) {
meillo@10 136 header *new_hdr = copy_header(hdr);
meillo@10 137 if (map_address_header(new_hdr, route->map_h_mail_followup_to_addresses)) {
meillo@10 138 hdr_node->data = new_hdr;
meillo@10 139 /* we need this list only to carefully free the extra headers: */
meillo@10 140 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 141 } else
meillo@10 142 g_free(new_hdr);
meillo@10 143 }
meillo@10 144 }
meillo@10 145 }
meillo@0 146
meillo@10 147 /* set Sender: domain to return_path->domain */
meillo@10 148 if (route->expand_h_sender_domain) {
meillo@10 149 GList *hdr_node;
meillo@0 150
meillo@10 151 foreach(msgout->hdr_list, hdr_node) {
meillo@10 152 header *hdr = (header *) (hdr_node->data);
meillo@10 153 if (hdr->id == HEAD_SENDER) {
meillo@10 154 header *new_hdr = copy_header(hdr);
meillo@0 155
meillo@10 156 set_address_header_domain(new_hdr, msgout->return_path->domain);
meillo@10 157 hdr_node->data = new_hdr;
meillo@10 158 /* we need this list only to carefully free the extra headers: */
meillo@10 159 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 160 }
meillo@10 161 }
meillo@10 162 }
meillo@0 163
meillo@10 164 /* set Sender: domain to return_path->domain */
meillo@10 165 if (route->expand_h_sender_address) {
meillo@10 166 GList *hdr_node;
meillo@0 167
meillo@10 168 foreach(msgout->hdr_list, hdr_node) {
meillo@10 169 header *hdr = (header *) (hdr_node->data);
meillo@10 170 if (hdr->id == HEAD_SENDER) {
meillo@10 171 header *new_hdr;
meillo@0 172
meillo@10 173 new_hdr = create_header(HEAD_SENDER, "Sender: %s@%s\n", msgout->return_path->local_part, msgout->return_path->domain);
meillo@10 174 hdr_node->data = new_hdr;
meillo@10 175 /* we need this list only to carefully free the extra headers: */
meillo@10 176 msgout->xtra_hdr_list = g_list_append(msgout->xtra_hdr_list, new_hdr);
meillo@10 177 }
meillo@10 178 }
meillo@10 179 }
meillo@0 180
meillo@10 181 if (msgout->xtra_hdr_list == NULL) {
meillo@10 182 /* nothing was changed */
meillo@10 183 g_list_free(msgout->hdr_list);
meillo@10 184 msgout->hdr_list = NULL;
meillo@10 185 }
meillo@10 186 DEBUG(5) debugf("rewrite_headers() returning\n");
meillo@0 187 }
meillo@0 188
meillo@10 189 void
meillo@10 190 rcptlist_with_one_of_hostlist(GList * rcpt_list, GList * host_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
meillo@0 191 {
meillo@10 192 GList *rcpt_node;
meillo@0 193
meillo@10 194 if (rcpt_list == NULL)
meillo@10 195 return;
meillo@0 196
meillo@10 197 foreach(rcpt_list, rcpt_node) {
meillo@10 198 address *rcpt = (address *) (rcpt_node->data);
meillo@10 199 GList *host_node = NULL;
meillo@0 200
meillo@10 201 foreach(host_list, host_node) {
meillo@10 202 gchar *host = (gchar *) (host_node->data);
meillo@10 203 if (fnmatch(host, rcpt->domain, FNM_CASEFOLD) == 0)
meillo@10 204 break;
meillo@10 205 }
meillo@10 206 if (host_node) {
meillo@10 207 if (p_rcpt_list)
meillo@10 208 *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt);
meillo@10 209 } else {
meillo@10 210 if (p_non_rcpt_list)
meillo@10 211 *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt);
meillo@10 212 }
meillo@0 213
meillo@10 214 }
meillo@0 215 }
meillo@0 216
meillo@10 217 void
meillo@10 218 rcptlist_with_addr_is_local(GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
meillo@0 219 {
meillo@10 220 GList *rcpt_node;
meillo@0 221
meillo@10 222 if (rcpt_list == NULL)
meillo@10 223 return;
meillo@0 224
meillo@10 225 foreach(rcpt_list, rcpt_node) {
meillo@10 226 address *rcpt = (address *) (rcpt_node->data);
meillo@10 227 if (addr_is_local(rcpt)) {
meillo@10 228 if (p_rcpt_list)
meillo@10 229 *p_rcpt_list = g_list_append(*p_rcpt_list, rcpt);
meillo@10 230 } else {
meillo@10 231 if (p_non_rcpt_list)
meillo@10 232 *p_non_rcpt_list = g_list_append(*p_non_rcpt_list, rcpt);
meillo@10 233 }
meillo@0 234
meillo@10 235 }
meillo@0 236 }
meillo@0 237
meillo@10 238 static gint
meillo@10 239 _g_list_addrcmp(gconstpointer a, gconstpointer b)
meillo@0 240 {
meillo@10 241 return addr_match((address *) a, (address *) b);
meillo@0 242 }
meillo@0 243
meillo@10 244 gboolean
meillo@10 245 route_is_allowed_return_path(connect_route * route, address * ret_path)
meillo@0 246 {
meillo@10 247 if (route->not_allowed_return_paths != NULL) {
meillo@10 248 if (g_list_find_custom(route->not_allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
meillo@10 249 return FALSE;
meillo@10 250 }
meillo@10 251 }
meillo@10 252 if (route->allowed_return_paths != NULL) {
meillo@10 253 if (g_list_find_custom(route->allowed_return_paths, ret_path, _g_list_addrcmp) != NULL) {
meillo@10 254 return TRUE;
meillo@10 255 } else {
meillo@10 256 return FALSE;
meillo@10 257 }
meillo@10 258 }
meillo@10 259 return TRUE;
meillo@0 260 }
meillo@0 261
meillo@10 262 static gint
meillo@10 263 _g_list_strcmp(gconstpointer a, gconstpointer b)
meillo@0 264 {
meillo@10 265 return (gint) strcmp(a, b);
meillo@0 266 }
meillo@0 267
meillo@10 268 gboolean
meillo@10 269 route_is_allowed_mail_local(connect_route * route, address * ret_path)
meillo@0 270 {
meillo@10 271 gchar *loc_part = ret_path->local_part;
meillo@0 272
meillo@10 273 if (route->not_allowed_mail_locals != NULL) {
meillo@10 274 if (g_list_find_custom(route->not_allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
meillo@10 275 return FALSE;
meillo@10 276 }
meillo@10 277 if (route->allowed_mail_locals != NULL) {
meillo@10 278 if (g_list_find_custom(route->allowed_mail_locals, loc_part, _g_list_strcmp) != NULL)
meillo@10 279 return TRUE;
meillo@10 280 else
meillo@10 281 return FALSE;
meillo@10 282 }
meillo@10 283 return TRUE;
meillo@0 284 }
meillo@0 285
meillo@10 286 /*
meillo@0 287 Make lists of matching/not matching rcpts.
meillo@0 288 Local domains are NOT regared here, these should be sorted out previously
meillo@0 289 */
meillo@10 290 void
meillo@10 291 msg_rcptlist_route(connect_route * route, GList * rcpt_list, GList ** p_rcpt_list, GList ** p_non_rcpt_list)
meillo@0 292 {
meillo@10 293 GList *tmp_list = NULL;
meillo@10 294 /* sort out those domains that can be sent over this connection: */
meillo@10 295 if (route->allowed_rcpt_domains) {
meillo@10 296 DEBUG(5) debugf("testing for route->allowed_rcpt_domains\n");
meillo@10 297 rcptlist_with_one_of_hostlist(rcpt_list, route->allowed_rcpt_domains, &tmp_list, p_non_rcpt_list);
meillo@10 298 } else {
meillo@10 299 DEBUG(5) debugf("route->allowed_rcpt_domains == NULL\n");
meillo@10 300 tmp_list = g_list_copy(rcpt_list);
meillo@10 301 }
meillo@0 302
meillo@10 303 /* sort out those domains that cannot be sent over this connection: */
meillo@10 304 rcptlist_with_one_of_hostlist(tmp_list, route->not_allowed_rcpt_domains, p_non_rcpt_list, p_rcpt_list);
meillo@10 305 g_list_free(tmp_list);
meillo@0 306 }
meillo@0 307
meillo@10 308 msg_out*
meillo@10 309 route_prepare_msgout(connect_route * route, msg_out * msgout)
meillo@0 310 {
meillo@10 311 message *msg = msgout->msg;
meillo@10 312 GList *rcpt_list = msgout->rcpt_list;
meillo@0 313
meillo@10 314 if (rcpt_list != NULL) {
meillo@10 315 /* found a few */
meillo@10 316 DEBUG(5) {
meillo@10 317 GList *node;
meillo@10 318 debugf("rcpts for routed delivery, route = %s, id = %s\n", route->name, msg->uid);
meillo@10 319 foreach(rcpt_list, node) {
meillo@10 320 address *rcpt = (address *) (node->data);
meillo@10 321 debugf("rcpt for routed delivery: <%s@%s>\n", rcpt->local_part, rcpt->domain);
meillo@10 322 }
meillo@10 323 }
meillo@0 324
meillo@10 325 /* rewrite return path
meillo@10 326 if there is a table, use that
meillo@10 327 if an address is found and if it has a domain, use that
meillo@10 328 */
meillo@10 329 if (route->map_return_path_addresses) {
meillo@10 330 address *ret_path = NULL;
meillo@10 331 DEBUG(5) debugf("looking up %s in map_return_path_addresses\n", msg->return_path->local_part);
meillo@10 332 ret_path = (address *) table_find_fnmatch(route->map_return_path_addresses, msg->return_path->local_part);
meillo@10 333 if (ret_path) {
meillo@10 334 DEBUG(5) debugf("found <%s@%s>\n", ret_path->local_part, ret_path->domain);
meillo@10 335 if (ret_path->domain == NULL)
meillo@10 336 ret_path->domain = route->set_return_path_domain
meillo@10 337 ? route->set_return_path_domain
meillo@10 338 : msg->return_path->domain;
meillo@10 339 msgout->return_path = copy_address(ret_path);
meillo@10 340 }
meillo@10 341 }
meillo@10 342 if (msgout->return_path == NULL) {
meillo@10 343 DEBUG(5) debugf("setting return path to %s\n", route->set_return_path_domain);
meillo@10 344 msgout->return_path = copy_modify_address(msg->return_path, NULL, route->set_return_path_domain);
meillo@10 345 }
meillo@10 346 rewrite_headers(msgout, route);
meillo@10 347
meillo@10 348 return msgout;
meillo@10 349 }
meillo@10 350 return NULL;
meillo@0 351 }
meillo@0 352
meillo@0 353 /* put msgout's is msgout_list into bins (msgout_perhost structs) for each
meillo@0 354 host. Used if there is no mail_host.
meillo@0 355 route param is not used, we leave it here because that may change.
meillo@0 356 */
meillo@0 357
meillo@10 358 GList*
meillo@10 359 route_msgout_list(connect_route * route, GList * msgout_list)
meillo@0 360 {
meillo@10 361 GList *mo_ph_list = NULL;
meillo@10 362 GList *msgout_node;
meillo@0 363
meillo@10 364 foreach(msgout_list, msgout_node) {
meillo@10 365 msg_out *msgout = (msg_out *) (msgout_node->data);
meillo@10 366 msg_out *msgout_new;
meillo@10 367 GList *rcpt_list = msgout->rcpt_list;
meillo@10 368 GList *rcpt_node;
meillo@0 369
meillo@10 370 foreach(rcpt_list, rcpt_node) {
meillo@10 371 address *rcpt = rcpt_node->data;
meillo@10 372 msgout_perhost *mo_ph = NULL;
meillo@10 373 GList *mo_ph_node = NULL;
meillo@0 374
meillo@10 375 /* search host in mo_ph_list */
meillo@10 376 foreach(mo_ph_list, mo_ph_node) {
meillo@10 377 mo_ph = (msgout_perhost *) (mo_ph_node->data);
meillo@10 378 if (strcasecmp(mo_ph->host, rcpt->domain) == 0)
meillo@10 379 break;
meillo@10 380 }
meillo@10 381 if (mo_ph_node != NULL) {
meillo@10 382 /* there is already a rcpt for this host */
meillo@10 383 msg_out *msgout_last = (msg_out *) ((g_list_last(mo_ph->msgout_list))->data);
meillo@10 384 if (msgout_last->msg == msgout->msg) {
meillo@10 385 /* if it is also the same message, it must be the last one
meillo@10 386 appended to mo_ph->msgout_list (since outer loop goes through
meillo@10 387 msgout_list) */
meillo@10 388 msgout_last->rcpt_list = g_list_append(msgout_last->rcpt_list, rcpt);
meillo@10 389 } else {
meillo@10 390 /* if not, we append a new msgout */
meillo@10 391 /* make a copy of msgout */
meillo@10 392 msgout_new = create_msg_out(msgout->msg);
meillo@10 393 msgout_new->return_path = msgout->return_path;
meillo@10 394 msgout_new->hdr_list = msgout->hdr_list;
meillo@0 395
meillo@10 396 /* append our rcpt to it */
meillo@10 397 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
meillo@10 398 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
meillo@10 399 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
meillo@10 400 }
meillo@10 401 } else {
meillo@10 402 /* this rcpt to goes to another host */
meillo@10 403 mo_ph = create_msgout_perhost(rcpt->domain);
meillo@10 404 mo_ph_list = g_list_append(mo_ph_list, mo_ph);
meillo@0 405
meillo@10 406 /* make a copy of msgout */
meillo@10 407 msgout_new = create_msg_out(msgout->msg);
meillo@10 408 msgout_new->return_path = msgout->return_path;
meillo@10 409 msgout_new->hdr_list = msgout->hdr_list;
meillo@0 410
meillo@10 411 /* append our rcpt to it */
meillo@10 412 /* It is the 1st rcpt for this msg to this host, therefore we safely give NULL */
meillo@10 413 msgout_new->rcpt_list = g_list_append(NULL, rcpt);
meillo@10 414 mo_ph->msgout_list = g_list_append(mo_ph->msgout_list, msgout_new);
meillo@10 415 } /* if mo_ph != NULL */
meillo@10 416 } /* foreach(rcpt_list, ... */
meillo@10 417 } /* foreach(msgout_list, ... */
meillo@10 418
meillo@10 419 return mo_ph_list;
meillo@0 420 }