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