masqmail-0.2

view src/alias.c @ 3:8c55886cacd8

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