-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
303 lines (249 loc) · 6.74 KB
/
list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* $Id: list.c,v 1.18 2009/02/09 04:12:07 manu Exp $ */
/*
* Copyright (c) 2006 Emmanuel Dreyfus
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Emmanuel Dreyfus
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#ifdef HAVE_SYS_CDEFS_H
#include <sys/cdefs.h>
#ifdef __RCSID
__RCSID("$Id: list.c,v 1.18 2009/02/09 04:12:07 manu Exp $");
#endif
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <errno.h>
#include <sysexits.h>
#include <sys/types.h>
#include <regex.h>
#ifdef HAVE_OLD_QUEUE_H
#include "queue.h"
#else
#include <sys/queue.h>
#endif
#include <netinet/in.h>
#include "milter-greylist.h"
#include "conf.h"
#include "spf.h"
#include "acl.h"
#ifdef USE_DNSRBL
#include "dnsrbl.h"
#endif
#ifdef USE_CURL
#include "urlcheck.h"
#endif
#include "macro.h"
#include "list.h"
#include "acl.h"
#ifdef USE_DMALLOC
#include <dmalloc.h>
#endif
struct all_list all_list_head;
struct all_list_entry *glist;
void
all_list_init(void)
{
LIST_INIT(&all_list_head);
glist_init();
return;
}
void
all_list_clear(void) /* acllist must be write locked */
{
struct all_list_entry *ale;
while(!LIST_EMPTY(&all_list_head)) {
ale = LIST_FIRST(&all_list_head);
LIST_REMOVE(ale, al_list);
all_list_put(ale);
free(ale);
}
all_list_init();
return;
}
struct all_list_entry *
all_list_get(type, name)
acl_clause_t type;
char *name;
{
struct all_list_entry *ale;
if ((ale = malloc(sizeof(*ale))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
ale->al_acr = get_acl_clause_rec(type);
strncpy(ale->al_name, name, sizeof(ale->al_name));
ale->al_name[sizeof(ale->al_name) - 1] = '\0';
TAILQ_INIT(&ale->al_head);
LIST_INSERT_HEAD(&all_list_head, ale, al_list);
return ale;
}
void
all_list_put(ale)
struct all_list_entry *ale;
{
struct list_entry *le;
while(!TAILQ_EMPTY(&ale->al_head)) {
le = TAILQ_FIRST(&ale->al_head);
TAILQ_REMOVE(&ale->al_head, le, l_list);
if (le->l_acr->acr_free)
(*le->l_acr->acr_free)(&le->l_data);
free(le);
}
return;
}
void
list_add(ale, type, data)
struct all_list_entry *ale;
acl_clause_t type;
void *data;
{
struct list_entry *le;
if ((le = malloc(sizeof(*le))) == NULL) {
mg_log(LOG_ERR, "malloc failed: %s", strerror(errno));
exit(EX_OSERR);
}
le->l_acr = get_acl_clause_rec(type);
(*le->l_acr->acr_add)(&le->l_data, data);
TAILQ_INSERT_TAIL(&ale->al_head, le, l_list);
if (conf.c_debug || conf.c_acldebug) {
char buf[1024];
mg_log(LOG_INFO, "load list item %s",
(*le->l_acr->acr_print)(&le->l_data, buf, sizeof(buf)));
}
}
void
all_list_settype(ale, l_type)
struct all_list_entry *ale;
acl_clause_t l_type;
{
struct acl_clause_rec *list_acr;
struct list_entry *le;
acl_clause_t i_type;
char *string;
struct acl_clause_rec *new_item_acr;
list_acr = get_acl_clause_rec(l_type);
/* Fix each item type */
TAILQ_FOREACH(le, &ale->al_head, l_list) {
if (le->l_acr->acr_stage != AS_NONE)
continue;
i_type = le->l_acr->acr_type;
new_item_acr = acl_list_item_fixup(i_type, l_type);
if (new_item_acr == NULL) {
char b[1024];
mg_log(LOG_ERR,
"list has mismatching item %s at line %d",
(*le->l_acr->acr_print)(&le->l_data, b, sizeof(b)),
conf_line);
exit(EX_DATAERR);
}
if (conf.c_debug || conf.c_acldebug) {
char b[1024];
mg_log(LOG_DEBUG, "item %s changing type from %s to %s",
(*le->l_acr->acr_print)(&le->l_data, b, sizeof(b)),
le->l_acr->acr_name, new_item_acr->acr_name);
}
/*
* Possible type changes:
* AC_EMAIL -> AC_FROM, AC_RCPT
* AC_REGEX -> AC_FROM_RE, AC_RCPT_RE, AC_DOMAIN_RE, ...
* No need for data modification
* AC_STRING -> AC_DNSRBL, AC_URLCHECK, AC_MACRO, AC_BODY, ...
* We get a string and we reinject it.
*/
if (le->l_acr->acr_type != new_item_acr->acr_type) {
char b[1024];
switch(le->l_acr->acr_type) {
case AC_EMAIL:
case AC_REGEX:
le->l_acr = new_item_acr;
break;
case AC_STRING:
string = strdup(le->l_data.string);
if (string == NULL) {
mg_log(LOG_ERR, "strdup failed: %s",
strerror(errno));
exit(EX_DATAERR);
}
(*le->l_acr->acr_free)(&le->l_data);
le->l_acr = new_item_acr;
(*le->l_acr->acr_add)(&le->l_data, string);
free(string);
break;
default:
(void)(*le->l_acr->acr_print)
(&le->l_data, b, sizeof(b));
mg_log(LOG_ERR, "cannot switch item %s "
"type from %s to %s", b,
le->l_acr->acr_name,
new_item_acr->acr_name);
exit(EX_DATAERR);
break;
}
}
}
ale->al_acr = list_acr;
if (conf.c_debug || conf.c_acldebug)
mg_log(LOG_INFO, "load list type %s, stage %s",
ale->al_acr->acr_name,
stage_string(ale->al_acr->acr_stage));
return;
}
void
all_list_setname(ale, name)
struct all_list_entry *ale;
char *name;
{
if (all_list_byname(name) != NULL) {
mg_log(LOG_ERR, "list \"%s\" defined twice at line %d",
name, conf_line - 1);
exit(EX_DATAERR);
}
if (conf.c_debug || conf.c_acldebug)
mg_log(LOG_DEBUG, "load list name \"%s\"", name);
strncpy(ale->al_name, name, sizeof(ale->al_name));
ale->al_name[sizeof(ale->al_name) - 1] = '\0';
return;
}
void
glist_init(void)
{
glist = all_list_get(AC_LIST, "");
return;
}
struct all_list_entry *
all_list_byname(name)
char *name;
{
struct all_list_entry *ale;
LIST_FOREACH(ale, &all_list_head, al_list) {
if (strcmp(ale->al_name, name) == 0)
break;
}
return ale;
}