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.
41 #include <tr_cfgwatch.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
50 #include <trust_router/trp.h>
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
56 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
58 TR_CONSTRAINT *cons=NULL;
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.");
71 if (NULL==(cons=tr_constraint_new(mem_ctx))) {
72 tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
77 if (NULL==(cons->type=tr_new_name(ctype))) {
78 tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
80 tr_constraint_free(cons);
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);
89 tr_constraint_free(cons);
97 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
99 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
100 TR_FILTER *filt = NULL;
101 json_t *jfaction = NULL;
102 json_t *jfline = NULL;
103 json_t *jfspecs = NULL;
104 json_t *this_jfspec = NULL;
105 json_t *jfield = NULL;
106 json_t *jmatch = NULL;
109 json_t *this_jmatch = NULL;
110 TR_NAME *name = NULL;
111 size_t i = 0, j = 0, k = 0;
115 if ((jfilt == NULL) || (rc == NULL)) {
116 tr_err("tr_cfg_parse_one_filter: null argument");
117 *rc = TR_CFG_BAD_PARAMS;
121 if (NULL == (filt = tr_filter_new(tmp_ctx))) {
122 tr_err("tr_cfg_parse_one_filter: Out of memory.");
126 tr_filter_set_type(filt, ftype);
128 /* make sure we have space to represent the filter */
129 if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
130 tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
131 *rc = TR_CFG_NOPARSE;
135 /* For each entry in the filter... */
136 json_array_foreach(jfilt, i, jfline) {
137 if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
138 (!json_is_string(jfaction))) {
139 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
140 *rc = TR_CFG_NOPARSE;
144 if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
145 (!json_is_array(jfspecs)) ||
146 (0 == json_array_size(jfspecs))) {
147 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
148 *rc = TR_CFG_NOPARSE;
152 if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
153 tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
154 *rc = TR_CFG_NOPARSE;
158 if (NULL == (filt->lines[i] = tr_fline_new(filt))) {
159 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
164 if (!strcmp(json_string_value(jfaction), "accept")) {
165 filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
166 } else if (!strcmp(json_string_value(jfaction), "reject")) {
167 filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
169 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
170 json_string_value(jfaction));
171 *rc = TR_CFG_NOPARSE;
175 if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
176 if (!json_is_array(jrc)) {
177 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
178 *rc = TR_CFG_NOPARSE;
180 } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
181 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
182 TR_MAX_CONST_MATCHES);
183 *rc = TR_CFG_NOPARSE;
185 } else if (json_array_size(jrc) > 0) {
186 /* ok we actually have entries to process */
187 if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
188 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
189 *rc = TR_CFG_NOPARSE;
195 if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
196 if (!json_is_array(jdc)) {
197 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
198 *rc = TR_CFG_NOPARSE;
200 } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
201 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
202 TR_MAX_CONST_MATCHES);
203 *rc = TR_CFG_NOPARSE;
205 } else if (json_array_size(jdc) > 0) {
206 if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
207 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
208 *rc = TR_CFG_NOPARSE;
214 /*For each filter spec within the filter line... */
215 json_array_foreach(jfspecs, j, this_jfspec) {
216 if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
217 (!json_is_string(jfield))) {
218 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
220 *rc = TR_CFG_NOPARSE;
224 /* check that we have a match attribute */
225 if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
226 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
228 *rc = TR_CFG_NOPARSE;
232 /* check that match is a string or an array */
233 if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
235 "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
237 *rc = TR_CFG_NOPARSE;
241 /* allocate the filter spec */
242 if (NULL == (filt->lines[i]->specs[j] = tr_fspec_new(filt->lines[i]))) {
243 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
248 /* fill in the field */
249 if (NULL == (filt->lines[i]->specs[j]->field = tr_new_name(json_string_value(jfield)))) {
250 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
255 /* fill in the matches */
256 if (json_is_string(jmatch)) {
257 if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
258 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
262 tr_fspec_add_match(filt->lines[i]->specs[j], name);
264 /* jmatch is an array (we checked earlier) */
265 json_array_foreach(jmatch, k, this_jmatch) {
266 if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
267 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
271 tr_fspec_add_match(filt->lines[i]->specs[j], name);
274 if (!tr_filter_validate_spec_field(ftype, filt->lines[i]->specs[j])){
275 tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
276 filt->lines[i]->specs[j]->field->len,
277 filt->lines[i]->specs[j]->field->buf,
278 tr_filter_type_to_string(filt->type),
286 /* check that the filter is valid */
287 if (!tr_filter_validate(filt)) {
290 *rc = TR_CFG_SUCCESS;
291 talloc_steal(mem_ctx, filt);
295 talloc_free(tmp_ctx);
296 if (*rc!=TR_CFG_SUCCESS)
301 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
303 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
305 const char *filt_label=NULL;
306 TR_FILTER *filt=NULL;
307 TR_FILTER_SET *filt_set=NULL;
308 TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
318 filt_set=tr_filter_set_new(tmp_ctx);
319 if (filt_set==NULL) {
320 tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
325 json_object_foreach(jfilts, filt_label, jfilt) {
326 /* check that we got a filter */
328 tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
329 *rc = TR_CFG_NOPARSE;
333 /* check that we recognize the filter type */
334 filt_type=tr_filter_type_from_string(filt_label);
335 if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
336 tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
337 *rc = TR_CFG_NOPARSE;
341 /* finally, parse the filter */
342 tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
343 filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
344 tr_filter_set_add(filt_set, filt);
345 if (*rc != TR_CFG_SUCCESS) {
346 tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
347 *rc = TR_CFG_NOPARSE;
355 if (*rc==TR_CFG_SUCCESS)
356 talloc_steal(mem_ctx, filt_set);
357 else if (filt_set!=NULL) {
358 talloc_free(filt_set);
362 talloc_free(tmp_ctx);