Use TR_LIST for TR_FLINE's 'fspec' member
[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_cfgwatch.h>
41 #include <tr_debug.h>
42
43 #if JANSSON_VERSION_HEX < 0x020500
44 #include "jansson_iterators.h"
45 #endif
46
47 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
48 {
49   TR_CONSTRAINT *cons=NULL;
50   int i=0;
51
52   if ((!ctype) || (!jc) || (!rc) ||
53       (!json_is_array(jc)) ||
54       (0 >= json_array_size(jc)) ||
55       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
56       (!json_is_string(json_array_get(jc, 0)))) {
57     tr_err("tr_cfg_parse_one_constraint: config error.");
58     *rc=TR_CFG_NOPARSE;
59     return NULL;
60   }
61
62   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
63     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
64     *rc=TR_CFG_NOMEM;
65     return NULL;
66   }
67
68   if (NULL==(cons->type=tr_new_name(ctype))) {
69     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
70     *rc=TR_CFG_NOMEM;
71     tr_constraint_free(cons);
72     return NULL;
73   }
74
75   for (i=0; i < json_array_size(jc); i++) {
76     cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
77     if (cons->matches[i]==NULL) {
78       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
79       *rc=TR_CFG_NOMEM;
80       tr_constraint_free(cons);
81       return NULL;
82     }
83   }
84
85   return cons;
86 }
87
88 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
89 {
90   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
91   TR_FILTER *filt = NULL;
92   TR_FLINE *fline = NULL;
93   TR_FSPEC *fspec = NULL;
94   json_t *jfaction = NULL;
95   json_t *jfline = NULL;
96   json_t *jfspecs = NULL;
97   json_t *this_jfspec = NULL;
98   json_t *jfield = NULL;
99   json_t *jmatch = NULL;
100   json_t *jrc = NULL;
101   json_t *jdc = NULL;
102   json_t *this_jmatch = NULL;
103   TR_NAME *name = NULL;
104   size_t i = 0, j = 0, k = 0;
105
106   *rc = TR_CFG_ERROR;
107
108   if ((jfilt == NULL) || (rc == NULL)) {
109     tr_err("tr_cfg_parse_one_filter: null argument");
110     *rc = TR_CFG_BAD_PARAMS;
111     goto cleanup;
112   }
113
114   if (NULL == (filt = tr_filter_new(tmp_ctx))) {
115     tr_err("tr_cfg_parse_one_filter: Out of memory.");
116     *rc = TR_CFG_NOMEM;
117     goto cleanup;
118   }
119   tr_filter_set_type(filt, ftype);
120
121   /* For each entry in the filter... */
122   json_array_foreach(jfilt, i, jfline) {
123     if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
124         (!json_is_string(jfaction))) {
125       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
126       *rc = TR_CFG_NOPARSE;
127       goto cleanup;
128     }
129
130     if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
131         (!json_is_array(jfspecs)) ||
132         (0 == json_array_size(jfspecs))) {
133       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
134       *rc = TR_CFG_NOPARSE;
135       goto cleanup;
136     }
137
138     fline = tr_fline_new(tmp_ctx);
139     if (fline == NULL) {
140       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i);
141       *rc = TR_CFG_NOMEM;
142       goto cleanup;
143     }
144     if (!strcmp(json_string_value(jfaction), "accept")) {
145       fline->action = TR_FILTER_ACTION_ACCEPT;
146       tr_debug("tr_cfg_parse_one_filter: Filter action is 'accept'");
147
148     } else if (!strcmp(json_string_value(jfaction), "reject")) {
149       fline->action = TR_FILTER_ACTION_REJECT;
150       tr_debug("tr_cfg_parse_one_filter: Filter action is 'reject'");
151     } else {
152       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action '%s'.",
153                json_string_value(jfaction));
154       *rc = TR_CFG_NOPARSE;
155       goto cleanup;
156     }
157
158     if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
159       if (!json_is_array(jrc)) {
160         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
161         *rc = TR_CFG_NOPARSE;
162         goto cleanup;
163       } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
164         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
165                TR_MAX_CONST_MATCHES);
166         *rc = TR_CFG_NOPARSE;
167         goto cleanup;
168       } else if (json_array_size(jrc) > 0) {
169         /* ok we actually have entries to process */
170         if (NULL == (fline->realm_cons = tr_cfg_parse_one_constraint(fline, "realm", jrc, rc))) {
171           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
172           *rc = TR_CFG_NOPARSE;
173           goto cleanup;
174         }
175       }
176     }
177
178     if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
179       if (!json_is_array(jdc)) {
180         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
181         *rc = TR_CFG_NOPARSE;
182         goto cleanup;
183       } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
184         tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
185                TR_MAX_CONST_MATCHES);
186         *rc = TR_CFG_NOPARSE;
187         goto cleanup;
188       } else if (json_array_size(jdc) > 0) {
189         if (NULL == (fline->domain_cons = tr_cfg_parse_one_constraint(fline, "domain", jdc, rc))) {
190           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
191           *rc = TR_CFG_NOPARSE;
192           goto cleanup;
193         }
194       }
195     }
196
197     /*For each filter spec within the filter line... */
198     tr_debug("tr_cfg_parse_one_filter: Filter line has %d spec(s)", json_array_size(jfspecs));
199     json_array_foreach(jfspecs, j, this_jfspec) {
200       if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
201           (!json_is_string(jfield))) {
202         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
203                  j);
204         *rc = TR_CFG_NOPARSE;
205         goto cleanup;
206       }
207
208       /* check that we have a match attribute */
209       if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
210         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
211                  j);
212         *rc = TR_CFG_NOPARSE;
213         goto cleanup;
214       }
215
216       /* check that match is a string or an array */
217       if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
218         tr_debug(
219             "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
220             i, j);
221         *rc = TR_CFG_NOPARSE;
222         goto cleanup;
223       }
224
225       /* allocate the filter spec */
226       if (NULL == (fspec = tr_fspec_new(fline))) {
227         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
228         *rc = TR_CFG_NOMEM;
229         goto cleanup;
230       }
231
232       /* fill in the field */
233       if (NULL == (fspec->field = tr_new_name(json_string_value(jfield)))) {
234         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
235         *rc = TR_CFG_NOMEM;
236         goto cleanup;
237       }
238
239       /* fill in the matches */
240       if (json_is_string(jmatch)) {
241         if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
242           tr_debug("tr_cfg_parse_one_filter: Out of memory.");
243           *rc = TR_CFG_NOMEM;
244           goto cleanup;
245         }
246         tr_fspec_add_match(fspec, name);
247       } else {
248         /* jmatch is an array (we checked earlier) */
249         json_array_foreach(jmatch, k, this_jmatch) {
250           if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
251             tr_debug("tr_cfg_parse_one_filter: Out of memory.");
252             *rc = TR_CFG_NOMEM;
253             goto cleanup;
254           }
255           tr_fspec_add_match(fspec, name);
256         }
257       }
258       if (!tr_filter_validate_spec_field(ftype, fspec)) {
259         tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
260                  fspec->field->len,
261                  fspec->field->buf,
262                  tr_filter_type_to_string(filt->type),
263                  i, j);
264         *rc = TR_CFG_ERROR;
265         goto cleanup;
266       }
267
268       if (tr_fline_add_spec(fline, fspec) == NULL) {
269         tr_debug("tr_cfg_parse_one_filter: Unable to add spec %d to line %d of %s filter.",
270                  j, i, tr_filter_type_to_string(filt->type));
271       }
272     }
273
274     if (NULL == tr_filter_add_line(filt, fline)) {
275       tr_debug("tr_cfg_parse_one_filter: Error adding line %d for %s filter",
276                i, tr_filter_type_to_string(filt->type));
277       *rc = TR_CFG_NOMEM;
278       goto cleanup;
279     }
280     tr_debug("tr_cfg_parse_one_filter: Added line %d to %s filter", i, tr_filter_type_to_string(filt->type));
281   }
282
283   /* check that the filter is valid */
284   if (!tr_filter_validate(filt)) {
285     *rc = TR_CFG_ERROR;
286   } else {
287     *rc = TR_CFG_SUCCESS;
288     talloc_steal(mem_ctx, filt);
289   }
290
291 cleanup:
292   talloc_free(tmp_ctx);
293   if (*rc!=TR_CFG_SUCCESS)
294     filt=NULL;
295   return filt;
296 }
297
298 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
299 {
300   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
301   json_t *jfilt;
302   const char *filt_label=NULL;
303   TR_FILTER *filt=NULL;
304   TR_FILTER_SET *filt_set=NULL;
305   TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
306
307   *rc=TR_CFG_ERROR;
308
309   /* no filters */
310   if (jfilts==NULL) {
311     *rc=TR_CFG_SUCCESS;
312     goto cleanup;
313   }
314
315   filt_set=tr_filter_set_new(tmp_ctx);
316   if (filt_set==NULL) {
317     tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
318     *rc = TR_CFG_NOMEM;
319     goto cleanup;
320   }
321
322   json_object_foreach(jfilts, filt_label, jfilt) {
323     /* check that we got a filter */
324     if (jfilt == NULL) {
325       tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
326       *rc = TR_CFG_NOPARSE;
327       goto cleanup;
328     }
329
330     /* check that we recognize the filter type */
331     filt_type=tr_filter_type_from_string(filt_label);
332     if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
333       tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
334       *rc = TR_CFG_NOPARSE;
335       goto cleanup;
336     }
337
338     /* finally, parse the filter */
339     tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
340     filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
341     if (*rc != TR_CFG_SUCCESS) {
342       tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
343       *rc = TR_CFG_NOPARSE;
344       goto cleanup;
345     }
346     if (tr_filter_set_add(filt_set, filt) != 0) {
347       tr_debug("tr_cfg_parse_filters: Error adding %s filter to filter set.", filt_label);
348       *rc = TR_CFG_NOPARSE;
349       goto cleanup;
350     }
351   }
352
353   *rc=TR_CFG_SUCCESS;
354
355 cleanup:
356   if (*rc==TR_CFG_SUCCESS)
357     talloc_steal(mem_ctx, filt_set);
358   else if (filt_set!=NULL) {
359     talloc_free(filt_set);
360     filt_set=NULL;
361   }
362
363   talloc_free(tmp_ctx);
364   return filt_set;
365 }