masqmail

annotate src/alias.c @ 2:653aef34b225

removed distribution specific data
author meillo@marmaro.de
date Fri, 26 Sep 2008 21:26:36 +0200
parents
children 26e34ae9a3e3
rev   line source
meillo@0 1 /* MasqMail
meillo@0 2 Copyright (C) 2000-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@0 22 gboolean addr_is_local(address *addr)
meillo@0 23 {
meillo@0 24 GList *dom_node;
meillo@0 25 GList *addr_node;
meillo@0 26 address *a;
meillo@0 27
meillo@0 28 foreach(conf.local_hosts, dom_node){
meillo@0 29 if(addr->domain == NULL)
meillo@0 30 return TRUE;
meillo@0 31 if(fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0){
meillo@0 32 foreach(conf.not_local_addresses,addr_node){
meillo@0 33 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@0 34 if(addr_isequal(a,addr)){
meillo@0 35 destroy_address(a);
meillo@0 36 return FALSE;
meillo@0 37 }
meillo@0 38 destroy_address(a);
meillo@0 39 }
meillo@0 40 return TRUE;
meillo@0 41 }
meillo@0 42 }
meillo@0 43 foreach(conf.local_addresses,addr_node){
meillo@0 44 a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
meillo@0 45 if(addr_isequal(a,addr)){
meillo@0 46 destroy_address(a);
meillo@0 47 return TRUE;
meillo@0 48 }
meillo@0 49 destroy_address(a);
meillo@0 50 }
meillo@0 51 return FALSE;
meillo@0 52 }
meillo@0 53
meillo@0 54 static
meillo@0 55 gboolean addr_isequal_alias(address *addr1, address *addr2)
meillo@0 56 {
meillo@0 57 return
meillo@0 58 (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0) &&
meillo@0 59 (strcasecmp(addr1->domain, addr2->domain) == 0);
meillo@0 60 }
meillo@0 61
meillo@0 62 static
meillo@0 63 GList *parse_list(gchar *line)
meillo@0 64 {
meillo@0 65 GList *list = NULL;
meillo@0 66 gchar buf[256];
meillo@0 67 gchar *p, *q;
meillo@0 68
meillo@0 69 p = line;
meillo@0 70 while(*p != 0){
meillo@0 71 q = buf;
meillo@0 72 while(isspace(*p)) p++;
meillo@0 73 if(*p != '\"'){
meillo@0 74 while(*p && (*p != ',') && (q < buf+255))
meillo@0 75 *(q++) = *(p++);
meillo@0 76 *q = 0;
meillo@0 77 }else{
meillo@0 78 gboolean escape = FALSE;
meillo@0 79 p++;
meillo@0 80 while(*p && (*p != '\"' || escape) && (q < buf+255)){
meillo@0 81 if((*p == '\\') && !escape)
meillo@0 82 escape = TRUE;
meillo@0 83 else{
meillo@0 84 escape = FALSE;
meillo@0 85 *(q++) = *p;
meillo@0 86 }
meillo@0 87 p++;
meillo@0 88 }
meillo@0 89 *q = 0;
meillo@0 90 while(*p && (*p != ',')) p++;
meillo@0 91 }
meillo@0 92 list = g_list_append(list, g_strdup(g_strchomp(buf)));
meillo@0 93 if(*p) p++;
meillo@0 94 }
meillo@0 95 return list;
meillo@0 96 }
meillo@0 97
meillo@0 98 GList *alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list)
meillo@0 99 {
meillo@0 100 GList *done_list = NULL;
meillo@0 101 GList *rcpt_node = g_list_copy(rcpt_list);
meillo@0 102
meillo@0 103 while(rcpt_node != NULL){
meillo@0 104 address *addr = (address *)(rcpt_node->data);
meillo@0 105 DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain);
meillo@0 106 // if(addr_is_local(addr) && (addr->local_part[0] != '|') &&
meillo@0 107 if(addr_is_local(addr) &&
meillo@0 108 !(addr->flags & ADDR_FLAG_NOEXPAND)){
meillo@0 109 gchar *val;
meillo@0 110
meillo@0 111 /* special handling for postmaster */
meillo@0 112 if(strcasecmp(addr->local_part, "postmaster") == 0)
meillo@0 113 val = (gchar *)table_find_func(alias_table, addr->local_part, strcasecmp);
meillo@0 114 else
meillo@0 115 val = (gchar *)table_find_func(alias_table, addr->local_part, conf.alias_local_cmp);
meillo@0 116
meillo@0 117 DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part);
meillo@0 118 if(val != NULL){
meillo@0 119 GList *val_list = parse_list(val);
meillo@0 120 GList *val_node;
meillo@0 121 GList *alias_list = NULL;
meillo@0 122
meillo@0 123 DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
meillo@0 124 foreach(val_list, val_node){
meillo@0 125 gchar *val = (gchar *)(val_node->data);
meillo@0 126 address *alias_addr;
meillo@0 127 address *addr_parent = NULL;
meillo@0 128
meillo@0 129 if(val[0] == '|'){
meillo@0 130 DEBUG(5) debugf("alias: %s is a pipe address\n", val);
meillo@0 131 alias_addr = create_address_pipe(val);
meillo@0 132 DEBUG(5) debugf("alias_pipe: %s is a pipe address\n", alias_addr->local_part);
meillo@0 133 }else if(val[0] == '\\'){
meillo@0 134 DEBUG(5) debugf("alias: shall not be expanded: '%s'\n", val);
meillo@0 135 alias_addr = create_address_qualified(&(val[1]), TRUE, conf.host_name);
meillo@0 136 alias_addr->flags |= ADDR_FLAG_NOEXPAND;
meillo@0 137 DEBUG(5) debugf("alias: not expanded: '%s'\n",alias_addr->local_part);
meillo@0 138 }else{
meillo@0 139 alias_addr = create_address_qualified(val, TRUE, conf.host_name);
meillo@0 140
meillo@0 141 /* search in parents for loops: */
meillo@0 142 for(addr_parent = addr; addr_parent; addr_parent = addr_parent->parent){
meillo@0 143 if(addr_isequal_alias(alias_addr, addr_parent)){
meillo@0 144 logwrite(LOG_ALERT, "detected alias loop, (ignoring): %s@%s -> %s@%s\n",
meillo@0 145 addr_parent->local_part, addr_parent->domain,
meillo@0 146 addr->local_part, addr->domain);
meillo@0 147 break;
meillo@0 148 }
meillo@0 149 }
meillo@0 150 }
meillo@0 151 if(!addr_parent){
meillo@0 152 alias_list = g_list_append(alias_list, alias_addr);
meillo@0 153 alias_addr->parent = addr;
meillo@0 154 }
meillo@0 155 g_free(val);
meillo@0 156 }
meillo@0 157 g_list_free(val_list);
meillo@0 158 addr->children = g_list_copy(alias_list);
meillo@0 159 rcpt_node = g_list_concat(rcpt_node, alias_list);
meillo@0 160 }else{
meillo@0 161 DEBUG(5) debugf("alias: '%s' is completed\n", addr->local_part);
meillo@0 162 done_list = g_list_append(done_list, addr);
meillo@0 163 }
meillo@0 164 }else{
meillo@0 165 DEBUG(5) debugf("alias: '%s@%s' is not local\n", addr->local_part, addr->domain);
meillo@0 166 done_list = g_list_append(done_list, addr);
meillo@0 167 }
meillo@0 168 rcpt_node = g_list_next(rcpt_node);
meillo@0 169 }
meillo@0 170
meillo@0 171 /* delete addresses from done_list if they are in the non_rcpt_list */
meillo@0 172 if(non_rcpt_list){
meillo@0 173 GList *rcpt_node_next;
meillo@0 174 for(rcpt_node = g_list_first(done_list);
meillo@0 175 rcpt_node;
meillo@0 176 rcpt_node = rcpt_node_next){
meillo@0 177 address *addr = (address *)(rcpt_node->data);
meillo@0 178 GList *non_node;
meillo@0 179
meillo@0 180 rcpt_node_next = g_list_next(rcpt_node);
meillo@0 181
meillo@0 182 foreach(non_rcpt_list, non_node){
meillo@0 183 address *non_addr = (address *)(non_node->data);
meillo@0 184 if(addr_isequal(addr, non_addr)){
meillo@0 185 done_list = g_list_remove_link(done_list, rcpt_node);
meillo@0 186 g_list_free_1(rcpt_node);
meillo@0 187 addr_mark_delivered(addr); /* this address is still in the children lists
meillo@0 188 of the original address */
meillo@0 189 break;
meillo@0 190 }
meillo@0 191 }
meillo@0 192 }
meillo@0 193 }
meillo@0 194 return done_list;
meillo@0 195 }