-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHostFilter.c
207 lines (167 loc) · 5.1 KB
/
HostFilter.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
/*
Copyright (C) <2010-2011> Karl Hiramoto <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include "nfq-web-filter-config.h"
#endif
#include <fnmatch.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Filter.h"
#include "FilterType.h"
#include "HttpReq.h"
#include "nfq_wf_private.h"
#include "HttpConn.h"
#include "PrivData.h"
/**
* @ingroup FilterObject
* @defgroup HostFilter Domain Filter Object
* @{
*/
struct HostFilter
{
FILTER_OBJECT_COMMON
char *host; /**< Hostname or Domain to filter */
};
#if 0
/** To clone any private data*/
static int HostFilter_clone(struct Filter *dst, struct Filter *src)
{
struct HostFilter *sf = (struct HostFilter *) src; /* filter object */
struct HostFilter *df = (struct HostFilter *) dst; /* filter object */
if (df->host) {
free(df->host);
df->host = NULL;
}
if (sf->host) {
df->host = strdup(sf->host);
}
return 0;
}
static int HostFilter_constructor(struct Filter *fobj)
{
return 0;
}
#endif
static int HostFilter_destructor(struct Filter *fobj)
{
struct HostFilter *fo = (struct HostFilter *) fobj; /* filter object */
if (fo->host) {
free(fo->host);
fo->host = NULL;
}
return 0;
}
#define HOST_STR "host"
// FIXME for quick prototype use char * later use real XML
static int HostFilter_load_from_xml(struct Filter *fobj, xmlNode *node)
{
struct HostFilter *fo = (struct HostFilter *) fobj; /* filter object */
xmlChar *prop = NULL;
DBG(5, "Loading XML config\n");
prop = xmlGetProp(node, BAD_CAST HOST_STR);
if (!prop) {
ERROR(" filter/host objects MUST have '%s' XML props \n", HOST_STR);
return -1;
}
fo->host = strdup((char*) prop);
xmlFree(prop);
if (!fo->host)
return -1;
DBG(2, "Loaded Host Filter object ID=%d host='%s'\n",
Filter_getFilterId(fobj),
fo->host);
return 0;
}
//Note saving to the private con data will be faster, when
// there are multiple requests per connection and the private data lookup
// is faster than the fnmatch()
#define PRIV_CON_DATA 1
#ifdef PRIV_CON_DATA
static int HostFilter_start_req(struct Filter *fobj, struct HttpReq *req)
{
struct HostFilter *fo = (struct HostFilter *) fobj; /* Host filter object */
struct HttpConn *con = req->con;
void *data;
if (con->cur_request > 1) {
DBG(7, "multiple requests to same host filter_id=%d\n", Filter_getObjId(fobj));
return 0;
}
data = PrivData_newData(con->priv_data, Filter_getObjId(fobj), sizeof(int), free);
if (!data) {
ERROR_FATAL("Missing private data id=%d\n", Filter_getObjId(fobj));
return -1;
}
if (!req->host)
ERROR_FATAL("Host NULL, BUG\n");
if (fnmatch(fo->host, req->host, FNM_CASEFOLD))
(*(int *)data) = 0; // no match
else
(*(int *)data) = 1; // match
return 0; // OK
}
#endif
static int HostFilter_matches_req(struct Filter *fobj, struct HttpReq *req)
{
#ifdef PRIV_CON_DATA
struct HttpConn *con = req->con;
void *data;
// every request in the same connection will be to the same host so use saved value.
data = PrivData_getData(con->priv_data, Filter_getObjId(fobj));
if (!data) {
WARN("Missing private data with id=%d\n", Filter_getObjId(fobj));
return 0;
}
// return saved match
return (*(int *)data);
#else
struct HostFilter *fo = (struct HostFilter *) fobj; /* Host filter object */
DBG(5, "check if req host='%s' contains = '%s'\n", req->host, fo->host);
if (!req->host)
ERROR_FATAL("Host NULL, BUG\n");
if (!fnmatch(fo->host, req->host, FNM_CASEFOLD))
return 1;
return 0;
#endif
}
static struct Object_ops obj_ops = {
.obj_type = "filter/host",
.obj_size = sizeof(struct HostFilter),
};
static struct Filter_ops HostFilter_obj_ops = {
.ops = &obj_ops,
.foo_destructor = HostFilter_destructor,
/* .foo_constructor = HostFilter_constructor, */
.foo_load_from_xml = HostFilter_load_from_xml,
#if PRIV_CON_DATA
.foo_request_start = HostFilter_start_req,
#endif
.foo_matches_req = HostFilter_matches_req,
};
/**
* Initialization function to register this filter type.
*/
static void __init HostFilter_init(void)
{
DBG(5, "init Host/Domain filter\n");
FilterType_register(&HostFilter_obj_ops);
}
/** @} */