masqmail-0.2

diff src/alias.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 a8f3424347dc
line diff
     1.1 --- a/src/alias.c	Mon Oct 27 16:21:27 2008 +0100
     1.2 +++ b/src/alias.c	Mon Oct 27 16:23:10 2008 +0100
     1.3 @@ -19,177 +19,179 @@
     1.4  #include "masqmail.h"
     1.5  #include <fnmatch.h>
     1.6  
     1.7 -gboolean addr_is_local(address *addr)
     1.8 +gboolean
     1.9 +addr_is_local(address * addr)
    1.10  {
    1.11 -  GList *dom_node;
    1.12 -  GList *addr_node;
    1.13 -  address *a;
    1.14 +	GList *dom_node;
    1.15 +	GList *addr_node;
    1.16 +	address *a;
    1.17  
    1.18 -  foreach(conf.local_hosts, dom_node){
    1.19 -    if(addr->domain == NULL)
    1.20 -      return TRUE;
    1.21 -    if(fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0){
    1.22 -      foreach(conf.not_local_addresses,addr_node){
    1.23 -	a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
    1.24 -	if(addr_isequal(a,addr)){
    1.25 -	  destroy_address(a);
    1.26 -	  return FALSE;
    1.27 +	foreach(conf.local_hosts, dom_node) {
    1.28 +		if (addr->domain == NULL)
    1.29 +			return TRUE;
    1.30 +		if (fnmatch(dom_node->data, addr->domain, FNM_CASEFOLD) == 0) {
    1.31 +			foreach(conf.not_local_addresses, addr_node) {
    1.32 +				a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
    1.33 +				if (addr_isequal(a, addr)) {
    1.34 +					destroy_address(a);
    1.35 +					return FALSE;
    1.36 +				}
    1.37 +				destroy_address(a);
    1.38 +			}
    1.39 +			return TRUE;
    1.40 +		}
    1.41  	}
    1.42 -	destroy_address(a);
    1.43 -      }
    1.44 -      return TRUE;
    1.45 -    }
    1.46 -  }
    1.47 -  foreach(conf.local_addresses,addr_node){
    1.48 -    a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
    1.49 -    if(addr_isequal(a,addr)){
    1.50 -      destroy_address(a);
    1.51 -      return TRUE;
    1.52 -    }
    1.53 -    destroy_address(a);
    1.54 -  }
    1.55 -  return FALSE;
    1.56 +	foreach(conf.local_addresses, addr_node) {
    1.57 +		a = create_address_qualified(addr_node->data, TRUE, conf.host_name);
    1.58 +		if (addr_isequal(a, addr)) {
    1.59 +			destroy_address(a);
    1.60 +			return TRUE;
    1.61 +		}
    1.62 +		destroy_address(a);
    1.63 +	}
    1.64 +	return FALSE;
    1.65  }
    1.66  
    1.67 -static
    1.68 -gboolean addr_isequal_alias(address *addr1, address *addr2)
    1.69 +static gboolean
    1.70 +addr_isequal_alias(address * addr1, address * addr2)
    1.71  {
    1.72 -  return
    1.73 -    (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0) &&
    1.74 -    (strcasecmp(addr1->domain, addr2->domain) == 0);
    1.75 +	return (conf.alias_local_cmp(addr1->local_part, addr2->local_part) == 0)
    1.76 +	       && (strcasecmp(addr1->domain, addr2->domain) == 0);
    1.77  }
    1.78  
    1.79 -static
    1.80 -GList *parse_list(gchar *line)
    1.81 +static GList*
    1.82 +parse_list(gchar * line)
    1.83  {
    1.84 -  GList *list = NULL;
    1.85 -  gchar buf[256];
    1.86 -  gchar *p, *q;
    1.87 +	GList *list = NULL;
    1.88 +	gchar buf[256];
    1.89 +	gchar *p, *q;
    1.90  
    1.91 -  p = line;
    1.92 -  while(*p != 0){
    1.93 -    q = buf;
    1.94 -    while(isspace(*p)) p++;
    1.95 -    if(*p != '\"'){
    1.96 -      while(*p && (*p != ',') && (q < buf+255))
    1.97 -	*(q++) = *(p++);
    1.98 -      *q = 0;
    1.99 -    }else{
   1.100 -      gboolean escape = FALSE;
   1.101 -      p++;
   1.102 -      while(*p && (*p != '\"' || escape) && (q < buf+255)){
   1.103 -	if((*p == '\\') && !escape)
   1.104 -	  escape = TRUE;
   1.105 -	else{
   1.106 -	  escape = FALSE;
   1.107 -	  *(q++) = *p;
   1.108 +	p = line;
   1.109 +	while (*p != 0) {
   1.110 +		q = buf;
   1.111 +		while (isspace(*p))
   1.112 +			p++;
   1.113 +		if (*p != '\"') {
   1.114 +			while (*p && (*p != ',') && (q < buf + 255))
   1.115 +				*(q++) = *(p++);
   1.116 +			*q = 0;
   1.117 +		} else {
   1.118 +			gboolean escape = FALSE;
   1.119 +			p++;
   1.120 +			while (*p && (*p != '\"' || escape) && (q < buf + 255)) {
   1.121 +				if ((*p == '\\') && !escape)
   1.122 +					escape = TRUE;
   1.123 +				else {
   1.124 +					escape = FALSE;
   1.125 +					*(q++) = *p;
   1.126 +				}
   1.127 +				p++;
   1.128 +			}
   1.129 +			*q = 0;
   1.130 +			while (*p && (*p != ','))
   1.131 +				p++;
   1.132 +		}
   1.133 +		list = g_list_append(list, g_strdup(g_strchomp(buf)));
   1.134 +		if (*p)
   1.135 +			p++;
   1.136  	}
   1.137 -	p++;
   1.138 -      }
   1.139 -      *q = 0;
   1.140 -      while(*p && (*p != ',')) p++;
   1.141 -    }
   1.142 -    list = g_list_append(list, g_strdup(g_strchomp(buf)));
   1.143 -    if(*p) p++;
   1.144 -  }
   1.145 -  return list;
   1.146 +	return list;
   1.147  }
   1.148  
   1.149 -GList *alias_expand(GList *alias_table, GList *rcpt_list, GList *non_rcpt_list)
   1.150 +GList*
   1.151 +alias_expand(GList * alias_table, GList * rcpt_list, GList * non_rcpt_list)
   1.152  {
   1.153 -  GList *done_list = NULL;
   1.154 -  GList *rcpt_node = g_list_copy(rcpt_list);
   1.155 +	GList *done_list = NULL;
   1.156 +	GList *rcpt_node = g_list_copy(rcpt_list);
   1.157  
   1.158 -  while(rcpt_node != NULL){
   1.159 -    address *addr = (address *)(rcpt_node->data);
   1.160 -    DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain);
   1.161 -//    if(addr_is_local(addr) && (addr->local_part[0] != '|') &&
   1.162 -    if(addr_is_local(addr) && 
   1.163 -        !(addr->flags & ADDR_FLAG_NOEXPAND)){
   1.164 -      gchar *val;
   1.165 -      
   1.166 -      /* special handling for postmaster */
   1.167 -      if(strcasecmp(addr->local_part, "postmaster") == 0)
   1.168 -	val = (gchar *)table_find_func(alias_table, addr->local_part, strcasecmp);
   1.169 -      else
   1.170 -	val = (gchar *)table_find_func(alias_table, addr->local_part, conf.alias_local_cmp);
   1.171 +	while (rcpt_node != NULL) {
   1.172 +		address *addr = (address *) (rcpt_node->data);
   1.173 +		DEBUG(5) debugf("alias_expand begin: '%s@%s'\n", addr->local_part, addr->domain);
   1.174 +		/* if(addr_is_local(addr) && (addr->local_part[0] != '|') && */
   1.175 +		if (addr_is_local(addr) && !(addr->flags & ADDR_FLAG_NOEXPAND)) {
   1.176 +			gchar *val;
   1.177  
   1.178 -      DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part);
   1.179 -      if(val != NULL){
   1.180 -	GList *val_list = parse_list(val);
   1.181 -	GList *val_node;
   1.182 -	GList *alias_list = NULL;
   1.183 +			/* special handling for postmaster */
   1.184 +			if (strcasecmp(addr->local_part, "postmaster") == 0)
   1.185 +				val = (gchar *) table_find_func(alias_table, addr->local_part, strcasecmp);
   1.186 +			else
   1.187 +				val = (gchar *) table_find_func(alias_table, addr->local_part, conf.alias_local_cmp);
   1.188  
   1.189 -	DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
   1.190 -	foreach(val_list, val_node){
   1.191 -	  gchar *val = (gchar *)(val_node->data);
   1.192 -	  address *alias_addr;
   1.193 -	  address *addr_parent = NULL;
   1.194 +			DEBUG(5) debugf("alias: '%s' is local\n", addr->local_part);
   1.195 +			if (val != NULL) {
   1.196 +				GList *val_list = parse_list(val);
   1.197 +				GList *val_node;
   1.198 +				GList *alias_list = NULL;
   1.199  
   1.200 -	  if(val[0] == '|'){
   1.201 -	    DEBUG(5) debugf("alias: %s is a pipe address\n", val);
   1.202 -	    alias_addr = create_address_pipe(val);
   1.203 -	    DEBUG(5) debugf("alias_pipe: %s is a pipe address\n", alias_addr->local_part);
   1.204 -	  }else if(val[0] == '\\'){
   1.205 -	    DEBUG(5) debugf("alias: shall not be expanded: '%s'\n", val);
   1.206 -	    alias_addr = create_address_qualified(&(val[1]), TRUE, conf.host_name);
   1.207 -	    alias_addr->flags |= ADDR_FLAG_NOEXPAND;
   1.208 -	    DEBUG(5) debugf("alias: not expanded: '%s'\n",alias_addr->local_part);
   1.209 -	  }else{
   1.210 -	    alias_addr = create_address_qualified(val, TRUE, conf.host_name);
   1.211 +				DEBUG(5) debugf("alias: '%s' -> '%s'\n", addr->local_part, val);
   1.212 +				foreach(val_list, val_node) {
   1.213 +					gchar *val = (gchar *) (val_node->data);
   1.214 +					address *alias_addr;
   1.215 +					address *addr_parent = NULL;
   1.216  
   1.217 -	    /* search in parents for loops: */
   1.218 -	    for(addr_parent = addr; addr_parent; addr_parent = addr_parent->parent){
   1.219 -	      if(addr_isequal_alias(alias_addr, addr_parent)){
   1.220 -		logwrite(LOG_ALERT, "detected alias loop, (ignoring): %s@%s -> %s@%s\n",
   1.221 -			 addr_parent->local_part, addr_parent->domain,
   1.222 -			 addr->local_part, addr->domain);
   1.223 -		break;
   1.224 -	      }
   1.225 -	    }
   1.226 -	  }
   1.227 -	  if(!addr_parent){
   1.228 -	    alias_list = g_list_append(alias_list, alias_addr);
   1.229 -	    alias_addr->parent = addr;
   1.230 -	  }
   1.231 -	  g_free(val);
   1.232 +					if (val[0] == '|') {
   1.233 +						DEBUG(5) debugf("alias: %s is a pipe address\n", val);
   1.234 +						alias_addr = create_address_pipe(val);
   1.235 +						DEBUG(5) debugf("alias_pipe: %s is a pipe address\n", alias_addr->local_part);
   1.236 +					} else if (val[0] == '\\') {
   1.237 +						DEBUG(5) debugf("alias: shall not be expanded: '%s'\n", val);
   1.238 +						alias_addr = create_address_qualified(&(val[1]), TRUE, conf.host_name);
   1.239 +						alias_addr->flags |= ADDR_FLAG_NOEXPAND;
   1.240 +						DEBUG(5) debugf("alias: not expanded: '%s'\n", alias_addr->local_part);
   1.241 +					} else {
   1.242 +						alias_addr = create_address_qualified(val, TRUE, conf.host_name);
   1.243 +
   1.244 +						/* search in parents for loops: */
   1.245 +						for (addr_parent = addr; addr_parent; addr_parent = addr_parent->parent) {
   1.246 +							if (addr_isequal_alias (alias_addr, addr_parent)) {
   1.247 +								logwrite(LOG_ALERT,
   1.248 +								         "detected alias loop, (ignoring): %s@%s -> %s@%s\n",
   1.249 +								         addr_parent->local_part,
   1.250 +								         addr_parent->domain,
   1.251 +								         addr->local_part, addr->domain);
   1.252 +								break;
   1.253 +							}
   1.254 +						}
   1.255 +					}
   1.256 +					if (!addr_parent) {
   1.257 +						alias_list = g_list_append(alias_list, alias_addr);
   1.258 +						alias_addr->parent = addr;
   1.259 +					}
   1.260 +					g_free(val);
   1.261 +				}
   1.262 +				g_list_free(val_list);
   1.263 +				addr->children = g_list_copy(alias_list);
   1.264 +				rcpt_node = g_list_concat(rcpt_node, alias_list);
   1.265 +			} else {
   1.266 +				DEBUG(5) debugf("alias: '%s' is completed\n", addr->local_part);
   1.267 +				done_list = g_list_append(done_list, addr);
   1.268 +			}
   1.269 +		} else {
   1.270 +			DEBUG(5) debugf("alias: '%s@%s' is not local\n", addr->local_part, addr->domain);
   1.271 +			done_list = g_list_append(done_list, addr);
   1.272 +		}
   1.273 +		rcpt_node = g_list_next(rcpt_node);
   1.274  	}
   1.275 -	g_list_free(val_list);
   1.276 -	addr->children = g_list_copy(alias_list);
   1.277 -	rcpt_node = g_list_concat(rcpt_node, alias_list);
   1.278 -      }else{
   1.279 -	DEBUG(5) debugf("alias: '%s' is completed\n", addr->local_part);
   1.280 -	done_list = g_list_append(done_list, addr);
   1.281 -      }
   1.282 -    }else{
   1.283 -      DEBUG(5) debugf("alias: '%s@%s' is not local\n", addr->local_part, addr->domain);
   1.284 -      done_list = g_list_append(done_list, addr);
   1.285 -    }
   1.286 -    rcpt_node = g_list_next(rcpt_node);
   1.287 -  }
   1.288  
   1.289 -  /* delete addresses from done_list if they are in the non_rcpt_list */
   1.290 -  if(non_rcpt_list){
   1.291 -    GList *rcpt_node_next;
   1.292 -    for(rcpt_node = g_list_first(done_list);
   1.293 -	 rcpt_node;
   1.294 -	 rcpt_node = rcpt_node_next){
   1.295 -      address *addr = (address *)(rcpt_node->data);
   1.296 -      GList *non_node;
   1.297 +	/* delete addresses from done_list if they are in the non_rcpt_list */
   1.298 +	if (non_rcpt_list) {
   1.299 +		GList *rcpt_node_next;
   1.300 +		for (rcpt_node = g_list_first(done_list); rcpt_node; rcpt_node = rcpt_node_next) {
   1.301 +			address *addr = (address *) (rcpt_node->data);
   1.302 +			GList *non_node;
   1.303  
   1.304 -      rcpt_node_next = g_list_next(rcpt_node);
   1.305 +			rcpt_node_next = g_list_next(rcpt_node);
   1.306  
   1.307 -      foreach(non_rcpt_list, non_node){
   1.308 -	address *non_addr = (address *)(non_node->data);
   1.309 -	if(addr_isequal(addr, non_addr)){
   1.310 -	  done_list = g_list_remove_link(done_list, rcpt_node);
   1.311 -	  g_list_free_1(rcpt_node);
   1.312 -	  addr_mark_delivered(addr); /* this address is still in the children lists
   1.313 -				      of the original address */
   1.314 -	  break;
   1.315 +			foreach(non_rcpt_list, non_node) {
   1.316 +				address *non_addr = (address *) (non_node->data);
   1.317 +				if (addr_isequal(addr, non_addr)) {
   1.318 +					done_list = g_list_remove_link(done_list, rcpt_node);
   1.319 +					g_list_free_1(rcpt_node);
   1.320 +					addr_mark_delivered(addr);  /* this address is still in the children lists of the original address */
   1.321 +					break;
   1.322 +				}
   1.323 +			}
   1.324 +		}
   1.325  	}
   1.326 -      }
   1.327 -    }
   1.328 -  }
   1.329 -  return done_list;
   1.330 +	return done_list;
   1.331  }