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 TR_FLINE *fline = NULL;
102 TR_FSPEC *fspec = NULL;
103 json_t *jfaction = NULL;
104 json_t *jfline = NULL;
105 json_t *jfspecs = NULL;
106 json_t *this_jfspec = NULL;
107 json_t *jfield = NULL;
108 json_t *jmatch = NULL;
111 json_t *this_jmatch = NULL;
112 TR_NAME *name = NULL;
113 size_t i = 0, j = 0, k = 0;
117 if ((jfilt == NULL) || (rc == NULL)) {
118 tr_err("tr_cfg_parse_one_filter: null argument");
119 *rc = TR_CFG_BAD_PARAMS;
123 if (NULL == (filt = tr_filter_new(tmp_ctx))) {
124 tr_err("tr_cfg_parse_one_filter: Out of memory.");
128 tr_filter_set_type(filt, ftype);
130 /* For each entry in the filter... */
131 json_array_foreach(jfilt, i, jfline) {
132 if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
133 (!json_is_string(jfaction))) {
134 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
135 *rc = TR_CFG_NOPARSE;
139 if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
140 (!json_is_array(jfspecs)) ||
141 (0 == json_array_size(jfspecs))) {
142 tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
143 *rc = TR_CFG_NOPARSE;
147 fline = tr_fline_new(tmp_ctx);
149 tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
154 if (!strcmp(json_string_value(jfaction), "accept")) {
155 fline->action = TR_FILTER_ACTION_ACCEPT;
156 } else if (!strcmp(json_string_value(jfaction), "reject")) {
157 fline->action = TR_FILTER_ACTION_REJECT;
159 tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
160 json_string_value(jfaction));
161 *rc = TR_CFG_NOPARSE;
165 if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
166 if (!json_is_array(jrc)) {
167 tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
168 *rc = TR_CFG_NOPARSE;
170 } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
171 tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
172 TR_MAX_CONST_MATCHES);
173 *rc = TR_CFG_NOPARSE;
175 } else if (json_array_size(jrc) > 0) {
176 /* ok we actually have entries to process */
177 if (NULL == (fline->realm_cons = tr_cfg_parse_one_constraint(fline, "realm", jrc, rc))) {
178 tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
179 *rc = TR_CFG_NOPARSE;
185 if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
186 if (!json_is_array(jdc)) {
187 tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
188 *rc = TR_CFG_NOPARSE;
190 } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
191 tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
192 TR_MAX_CONST_MATCHES);
193 *rc = TR_CFG_NOPARSE;
195 } else if (json_array_size(jdc) > 0) {
196 if (NULL == (fline->domain_cons = tr_cfg_parse_one_constraint(fline, "domain", jdc, rc))) {
197 tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
198 *rc = TR_CFG_NOPARSE;
204 /*For each filter spec within the filter line... */
205 json_array_foreach(jfspecs, j, this_jfspec) {
206 if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
207 (!json_is_string(jfield))) {
208 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
210 *rc = TR_CFG_NOPARSE;
214 /* check that we have a match attribute */
215 if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
216 tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
218 *rc = TR_CFG_NOPARSE;
222 /* check that match is a string or an array */
223 if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
225 "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
227 *rc = TR_CFG_NOPARSE;
231 /* allocate the filter spec */
232 if (NULL == (fspec = tr_fspec_new(fline))) {
233 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
238 /* fill in the field */
239 if (NULL == (fspec->field = tr_new_name(json_string_value(jfield)))) {
240 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
245 /* fill in the matches */
246 if (json_is_string(jmatch)) {
247 if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
248 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
252 tr_fspec_add_match(fspec, name);
254 /* jmatch is an array (we checked earlier) */
255 json_array_foreach(jmatch, k, this_jmatch) {
256 if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
257 tr_debug("tr_cfg_parse_one_filter: Out of memory.");
261 tr_fspec_add_match(fspec, name);
264 if (!tr_filter_validate_spec_field(ftype, fspec)){
265 tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
268 tr_filter_type_to_string(filt->type),
274 if(tr_fline_add_spec(fline, fspec) == NULL) {
275 tr_debug("tr_cfg_parse_one_filter: Unable to add spec %d to line %d of %s filter.",
276 j, i, tr_filter_type_to_string(filt->type));
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, tr_filter_type_to_string(filt->type));
288 /* check that the filter is valid */
289 if (!tr_filter_validate(filt)) {
292 *rc = TR_CFG_SUCCESS;
293 talloc_steal(mem_ctx, filt);
297 talloc_free(tmp_ctx);
298 if (*rc!=TR_CFG_SUCCESS)
303 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
305 TALLOC_CTX *tmp_ctx=talloc_new(NULL);
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;
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.");
327 json_object_foreach(jfilts, filt_label, jfilt) {
328 /* check that we got a filter */
330 tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
331 *rc = TR_CFG_NOPARSE;
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;
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;
357 if (*rc==TR_CFG_SUCCESS)
358 talloc_steal(mem_ctx, filt_set);
359 else if (filt_set!=NULL) {
360 talloc_free(filt_set);
364 talloc_free(tmp_ctx);