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
|
#include "mfrules.h"
#include "alloc.h"
#include "byte.h"
#include "case.h"
#include "cdbread.h"
#include "close.h"
#include "open.h"
#include "str.h"
#include "stralloc.h"
/* return -9: problems reading cdb */
/* return -1: key matches; data not */
/* return 0: no key */
/* return 1: key matches without data */
/* return 2: key and data match */
stralloc key = {0};
static struct cdb cdb;
static int mffind(char *mf)
{
char *x;
char *data;
unsigned int datalen;
int plus = 0;
int dlen;
int len;
int mflen;
int delta;
switch (cdb_find(&cdb, key.s, key.len)) {
case -1: return -9;
case 0: return 0;
}
datalen = cdb_datalen(&cdb);
data = alloc(datalen);
if (!data) return -9;
if (!datalen) return 1;
mflen = str_len(mf);
if (cdb_read(&cdb, data, datalen, cdb_datapos(&cdb)) == -1) {
alloc_free(data);
return -9;
}
x = data;
dlen = datalen - 1; /* trailing separator */
while (dlen > 0) {
plus = byte_rchr(data, dlen, '+');
x = data + plus + 1;
len = dlen - plus;
delta = (mflen > len) ? mflen - len : 0;
if (!byte_diff(x, len, mf + delta)) {
alloc_free(data);
return 2;
}
dlen = plus - 1;
}
alloc_free(data);
return -1;
}
int mfsearch(char *ip, char *host, char *info, char *mf)
{
int r;
if (info) {
if (!stralloc_copys(&key, info)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
if (!stralloc_cats(&key, "@")) return -9;
if (!stralloc_cats(&key, ip)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
if (host) {
if (!stralloc_copys(&key, info)) return -9;
if (!stralloc_cats(&key, "@=")) return -9;
if (!stralloc_cats(&key, host)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
}
if (!stralloc_copys(&key, ip)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
if (host) {
if (!stralloc_copys(&key, "=")) return -9;
if (!stralloc_cats(&key, host)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
if (!stralloc_copys(&key, ip)) return -9; /* IPv6 */
while (key.len > 0) {
if (ip[key.len - 1] == ':') {
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
--key.len;
}
if (!stralloc_copys(&key, ip)) return -9; /* IPv4 */
while (key.len > 0) {
if (ip[key.len - 1] == '.') {
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
--key.len;
}
if (host) {
while (*host) {
if (*host == '.') {
if (!stralloc_copys(&key, "=")) return -9;
if (!stralloc_cats(&key, host)) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
++host;
}
if (!stralloc_copys(&key, "=")) return -9;
r = mffind(mf);
if (r < -1 || r > 0) return r;
}
key.len = 0;
/* return mffind(mf); */
return -1;
}
int mfrules(int fd, char *ip, char *host, char *info, char *mf)
{
int r;
cdb_init(&cdb, fd);
case_lowers(mf);
r = mfsearch(ip, host, info, mf);
cdb_free(&cdb);
close(fd);
return r;
}
|