2 * Copyright (c) 2012-2018, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 #include <tr_constraint_internal.h>
41 #include <tr_cfgwatch.h>
44 #if JANSSON_VERSION_HEX < 0x020500
45 #include "jansson_iterators.h"
48 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, const char *ctype, json_t *jc, TR_CFG_RC *rc)
50 TR_CONSTRAINT *cons=NULL;
54 tr_err("tr_cfg_parse_one_constraint: rc is null, cannot process constraint.");
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.");
67 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
68 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
73 if (NULL==(cons->type=tr_new_name(ctype))) {
74 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
76 tr_constraint_free(cons);
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);
84 tr_constraint_free(cons);
92 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
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;
106 json_t *this_jmatch = NULL;
107 TR_NAME *name = NULL;
108 size_t i = 0, j = 0, k = 0;
112 if ((jfilt == NULL) || (rc == NULL)) {
113 tr_err("tr_cfg_parse_one_filter: null argument");
114 *rc = TR_CFG_BAD_PARAMS;
118 if (NULL == (filt = tr_filter_new(tmp_ctx))) {
119 tr_err("tr_cfg_parse_one_filter: Out of memory.");
123 tr_filter_set_type(filt, ftype);
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;
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;
142 fline = tr_fline_new(tmp_ctx);
144 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i);
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'");
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'");
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;
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;
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;
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;
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;
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,
198 *rc = TR_CFG_NOPARSE;
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,
206 *rc = TR_CFG_NOPARSE;
210 /* check that match is a string or an array */
211 if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
213 "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
215 *rc = TR_CFG_NOPARSE;
219 /* allocate the filter spec */
220 if (NULL == (fspec = tr_fspec_new(fline))) {
221 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
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.");
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.");
240 tr_fspec_add_match(fspec, name);
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.");
249 tr_fspec_add_match(fspec, name);
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.",
256 tr_filter_type_to_string(filt->type),
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));
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));
274 tr_debug("tr_cfg_parse_one_filter: Added line %d to %s filter", i, tr_filter_type_to_string(filt->type));
277 /* check that the filter is valid */
278 if (!tr_filter_validate(filt)) {
281 *rc = TR_CFG_SUCCESS;
282 talloc_steal(mem_ctx, filt);
286 talloc_free(tmp_ctx);
287 if (*rc!=TR_CFG_SUCCESS)
292 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
294 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
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;
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.");
316 json_object_foreach(jfilts, filt_label, jfilt) {
317 /* check that we got a filter */
319 tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
320 *rc = TR_CFG_NOPARSE;
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;
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;
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;
350 if (*rc==TR_CFG_SUCCESS)
351 talloc_steal(mem_ctx, filt_set);
352 else if (filt_set!=NULL) {
353 talloc_free(filt_set);
357 talloc_free(tmp_ctx);