Merge pull request #62 from painless-security/jennifer/report_incoming_ipaddr
[trust_router.git] / common / tr_config_filters.c
1 /*
2  * Copyright (c) 2012-2018, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <talloc.h>
39
40 #include <tr_constraint_internal.h>
41 #include <tr_cfgwatch.h>
42 #include <tr_debug.h>
43
44 #if JANSSON_VERSION_HEX < 0x020500
45 #include "jansson_iterators.h"
46 #endif
47
48 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, const char *ctype, json_t *jc, TR_CFG_RC *rc)
49 {
50   TR_CONSTRAINT *cons=NULL;
51   size_t i=0;
52
53   if (!rc) {
54     tr_err("tr_cfg_parse_one_constraint: rc is null, cannot process constraint.");
55     return NULL;
56   }
57
58   if ((!ctype) || (!jc) ||
59       (!json_is_array(jc)) ||
60       (0 >= json_array_size(jc)) ||
61       (!json_is_string(json_array_get(jc, 0)))) {
62     tr_err("tr_cfg_parse_one_constraint: config error.");
63     *rc=TR_CFG_NOPARSE;
64     return NULL;
65   }
66
67   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
68     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
69     *rc=TR_CFG_NOMEM;
70     return NULL;
71   }
72
73   if (NULL==(cons->type=tr_new_name(ctype))) {
74     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
75     *rc=TR_CFG_NOMEM;
76     tr_constraint_free(cons);
77     return NULL;
78   }
79
80   for (i=0; i < json_array_size(jc); i++) {
81     if (NULL == tr_constraint_add_match(cons, tr_new_name(json_string_value(json_array_get(jc, i))))) {
82       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i);
83       *rc=TR_CFG_NOMEM;
84       tr_constraint_free(cons);
85       return NULL;
86     }
87   }
88
89   return cons;
90 }
91
92 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
93 {
94   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
95   TR_FILTER *filt = NULL;
96   TR_FLINE *fline = NULL;
97   TR_FSPEC *fspec = NULL;
98   json_t *jfaction = NULL;
99   json_t *jfline = NULL;
100   json_t *jfspecs = NULL;
101   json_t *this_jfspec = NULL;
102   json_t *jfield = NULL;
103   json_t *jmatch = NULL;
104   json_t *jrc = NULL;
105   json_t *jdc = NULL;
106   json_t *this_jmatch = NULL;
107   TR_NAME *name = NULL;
108   size_t i = 0, j = 0, k = 0;
109
110   *rc = TR_CFG_ERROR;
111
112   if ((jfilt == NULL) || (rc == NULL)) {
113     tr_err("tr_cfg_parse_one_filter: null argument");
114     *rc = TR_CFG_BAD_PARAMS;
115     goto cleanup;
116   }
117
118   if (NULL == (filt = tr_filter_new(tmp_ctx))) {
119     tr_err("tr_cfg_parse_one_filter: Out of memory.");
120     *rc = TR_CFG_NOMEM;
121     goto cleanup;
122   }
123   tr_filter_set_type(filt, ftype);
124
125   /* For each entry in the filter... */
126   json_array_foreach(jfilt, i, jfline) {
127     if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
128         (!json_is_string(jfaction))) {
129       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
130       *rc = TR_CFG_NOPARSE;
131       goto cleanup;
132     }
133
134     if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
135         (!json_is_array(jfspecs)) ||
136         (0 == json_array_size(jfspecs))) {
137       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
138       *rc = TR_CFG_NOPARSE;
139       goto cleanup;
140     }
141
142     fline = tr_fline_new(tmp_ctx);
143     if (fline == NULL) {
144       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i);
145       *rc = TR_CFG_NOMEM;
146       goto cleanup;
147     }
148     if (!strcmp(json_string_value(jfaction), "accept")) {
149       fline->action = TR_FILTER_ACTION_ACCEPT;
150       tr_debug("tr_cfg_parse_one_filter: Filter action is 'accept'");
151
152     } else if (!strcmp(json_string_value(jfaction), "reject")) {
153       fline->action = TR_FILTER_ACTION_REJECT;
154       tr_debug("tr_cfg_parse_one_filter: Filter action is 'reject'");
155     } else {
156       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action '%s'.",
157                json_string_value(jfaction));
158       *rc = TR_CFG_NOPARSE;
159       goto cleanup;
160     }
161
162     if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
163       if (!json_is_array(jrc)) {
164         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
165         *rc = TR_CFG_NOPARSE;
166         goto cleanup;
167       } else if (json_array_size(jrc) > 0) {
168         /* ok we actually have entries to process */
169         if (NULL == (fline->realm_cons = tr_cfg_parse_one_constraint(fline, "realm", jrc, rc))) {
170           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
171           *rc = TR_CFG_NOPARSE;
172           goto cleanup;
173         }
174       }
175     }
176
177     if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
178       if (!json_is_array(jdc)) {
179         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
180         *rc = TR_CFG_NOPARSE;
181         goto cleanup;
182       } else if (json_array_size(jdc) > 0) {
183         if (NULL == (fline->domain_cons = tr_cfg_parse_one_constraint(fline, "domain", jdc, rc))) {
184           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
185           *rc = TR_CFG_NOPARSE;
186           goto cleanup;
187         }
188       }
189     }
190
191     /*For each filter spec within the filter line... */
192     tr_debug("tr_cfg_parse_one_filter: Filter line has %d spec(s)", json_array_size(jfspecs));
193     json_array_foreach(jfspecs, j, this_jfspec) {
194       if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
195           (!json_is_string(jfield))) {
196         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
197                  j);
198         *rc = TR_CFG_NOPARSE;
199         goto cleanup;
200       }
201
202       /* check that we have a match attribute */
203       if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
204         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
205                  j);
206         *rc = TR_CFG_NOPARSE;
207         goto cleanup;
208       }
209
210       /* check that match is a string or an array */
211       if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
212         tr_debug(
213             "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
214             i, j);
215         *rc = TR_CFG_NOPARSE;
216         goto cleanup;
217       }
218
219       /* allocate the filter spec */
220       if (NULL == (fspec = tr_fspec_new(fline))) {
221         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
222         *rc = TR_CFG_NOMEM;
223         goto cleanup;
224       }
225
226       /* fill in the field */
227       if (NULL == (fspec->field = tr_new_name(json_string_value(jfield)))) {
228         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
229         *rc = TR_CFG_NOMEM;
230         goto cleanup;
231       }
232
233       /* fill in the matches */
234       if (json_is_string(jmatch)) {
235         if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
236           tr_debug("tr_cfg_parse_one_filter: Out of memory.");
237           *rc = TR_CFG_NOMEM;
238           goto cleanup;
239         }
240         tr_fspec_add_match(fspec, name);
241       } else {
242         /* jmatch is an array (we checked earlier) */
243         json_array_foreach(jmatch, k, this_jmatch) {
244           if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
245             tr_debug("tr_cfg_parse_one_filter: Out of memory.");
246             *rc = TR_CFG_NOMEM;
247             goto cleanup;
248           }
249           tr_fspec_add_match(fspec, name);
250         }
251       }
252       if (!tr_filter_validate_spec_field(ftype, fspec)) {
253         tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
254                  fspec->field->len,
255                  fspec->field->buf,
256                  tr_filter_type_to_string(filt->type),
257                  i, j);
258         *rc = TR_CFG_ERROR;
259         goto cleanup;
260       }
261
262       if (tr_fline_add_spec(fline, fspec) == NULL) {
263         tr_debug("tr_cfg_parse_one_filter: Unable to add spec %d to line %d of %s filter.",
264                  j, i, tr_filter_type_to_string(filt->type));
265       }
266     }
267
268     if (NULL == tr_filter_add_line(filt, fline)) {
269       tr_debug("tr_cfg_parse_one_filter: Error adding line %d for %s filter",
270                i, tr_filter_type_to_string(filt->type));
271       *rc = TR_CFG_NOMEM;
272       goto cleanup;
273     }
274     tr_debug("tr_cfg_parse_one_filter: Added line %d to %s filter", i, tr_filter_type_to_string(filt->type));
275   }
276
277   /* check that the filter is valid */
278   if (!tr_filter_validate(filt)) {
279     *rc = TR_CFG_ERROR;
280   } else {
281     *rc = TR_CFG_SUCCESS;
282     talloc_steal(mem_ctx, filt);
283   }
284
285 cleanup:
286   talloc_free(tmp_ctx);
287   if (*rc!=TR_CFG_SUCCESS)
288     filt=NULL;
289   return filt;
290 }
291
292 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
293 {
294   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
295   json_t *jfilt;
296   const char *filt_label=NULL;
297   TR_FILTER *filt=NULL;
298   TR_FILTER_SET *filt_set=NULL;
299   TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
300
301   *rc=TR_CFG_ERROR;
302
303   /* no filters */
304   if (jfilts==NULL) {
305     *rc=TR_CFG_SUCCESS;
306     goto cleanup;
307   }
308
309   filt_set=tr_filter_set_new(tmp_ctx);
310   if (filt_set==NULL) {
311     tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
312     *rc = TR_CFG_NOMEM;
313     goto cleanup;
314   }
315
316   json_object_foreach(jfilts, filt_label, jfilt) {
317     /* check that we got a filter */
318     if (jfilt == NULL) {
319       tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
320       *rc = TR_CFG_NOPARSE;
321       goto cleanup;
322     }
323
324     /* check that we recognize the filter type */
325     filt_type=tr_filter_type_from_string(filt_label);
326     if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
327       tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
328       *rc = TR_CFG_NOPARSE;
329       goto cleanup;
330     }
331
332     /* finally, parse the filter */
333     tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
334     filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
335     if (*rc != TR_CFG_SUCCESS) {
336       tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
337       *rc = TR_CFG_NOPARSE;
338       goto cleanup;
339     }
340     if (tr_filter_set_add(filt_set, filt) != 0) {
341       tr_debug("tr_cfg_parse_filters: Error adding %s filter to filter set.", filt_label);
342       *rc = TR_CFG_NOPARSE;
343       goto cleanup;
344     }
345   }
346
347   *rc=TR_CFG_SUCCESS;
348
349 cleanup:
350   if (*rc==TR_CFG_SUCCESS)
351     talloc_steal(mem_ctx, filt_set);
352   else if (filt_set!=NULL) {
353     talloc_free(filt_set);
354     filt_set=NULL;
355   }
356
357   talloc_free(tmp_ctx);
358   return filt_set;
359 }