2 * Copyright (c) 2012-2014, 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.
35 #include "jansson_iterators.h"
39 #include <tr_filter.h>
42 #include <trust_router/tr_constraint.h>
44 /* Returns TRUE (1) if the the string (str) matchs the wildcard string (wc_str), FALSE (0) if not.
46 int tr_prefix_wildcard_match (const char *str, const char *wc_str) {
47 const char *wc_post = wc_str;
51 if ((!str) || (!wc_str))
55 if (0 == (wc_len = strlen(wc_str)))
58 /* TBD -- skip leading white space? */
59 if ('*' == wc_str[0]) {
60 wc_post = &(wc_str[1]);
62 }else if (len != wc_len)
69 if (0 == strcmp(&(str[len-wc_len]), wc_post)) {
76 TR_CONSTRAINT_SET *tr_constraint_set_from_fline (TR_FLINE *fline)
83 if (fline->realm_cons)
84 tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->realm_cons);
85 if (fline->domain_cons)
86 tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->domain_cons);
88 return (TR_CONSTRAINT_SET *) cset;
91 /* A constraint set is represented in json as an array of constraint
92 * objects. So, a constraint set (cset) that consists of one realm
93 * constraint and one domain constraint might look like:
95 * {cset: [{domain: [a.com, b.co.uk]},
96 * {realm: [c.net, d.org]}]}
99 void tr_constraint_add_to_set (TR_CONSTRAINT_SET **cset, TR_CONSTRAINT *cons)
101 json_t *jcons = NULL;
102 json_t *jmatches = NULL;
105 if ((!cset) || (!cons))
108 /* If we don't already have a json object, create one */
110 *cset = (TR_CONSTRAINT_SET *) json_array();
112 /* Create a json object representing cons */
113 jmatches = json_array();
114 jcons = json_object();
116 for (i = 0; ((i < TR_MAX_CONST_MATCHES) && (NULL != cons->matches[i])); i++) {
117 json_array_append_new(jmatches, json_string(cons->matches[i]->buf));
120 json_object_set_new(jcons, cons->type->buf, jmatches);
122 /* Add the created object to the cset object */
123 json_array_append_new((json_t *) *cset, jcons);
126 int tr_constraint_set_validate(TR_CONSTRAINT_SET *cset)
128 json_t *json = (json_t *) cset;
131 if (!json_is_array(json)){
132 tr_debug("Constraint_set is not an array");
135 json_array_foreach(json, i, set_member) {
138 if (!json_is_object(set_member)) {
139 tr_debug("Constraint member at %zu is not an object\n", i);
142 json_object_foreach( set_member, key, value) {
145 if (!json_is_array(value)) {
146 tr_debug("Constraint type %s at index %zu in constraint set is not an array\n", key,
150 json_array_foreach(value, inner_index, inner_value) {
151 if (!json_is_string(inner_value)) {
152 tr_debug("Constraint type %s at index %zu in constraint set has non-string element %zu\n",
153 key, i, inner_index);
163 TR_CONSTRAINT_SET *tr_constraint_set_filter( TID_REQ *request,
164 TR_CONSTRAINT_SET *orig,
165 const char *constraint_type)
167 json_t *orig_cset = (json_t*) orig;
168 json_t *new_cs = NULL;
171 if (!tr_constraint_set_validate( (TR_CONSTRAINT_SET *) orig_cset)) {
172 tr_debug ("tr_constraint_set_filter: not a valid constraint set\n");
175 assert (new_cs = json_array());
176 json_array_foreach(orig_cset, index, set_member) {
177 if (json_object_get( set_member, constraint_type))
178 json_array_append(new_cs, set_member);
180 return (TR_CONSTRAINT_SET *) new_cs;
184 * Within a given constraint object merge any overlapping domain or
185 * realm constraints. For example ['*','*.net'] can be simplified to
188 static void merge_constraints(json_t *constraint, const char *key)
190 json_t *value_1, *value_2, *constraint_array;
191 size_t index_1, index_2;
193 * Go through the loop pairwise linear, removing elements where one
194 * element is a subset of the other. Always shrik the array from
195 * the end so that index_1 never becomes invalid (swapping if
198 constraint_array = json_object_get(constraint, key);
199 if (NULL == constraint_array)
201 json_array_foreach( constraint_array, index_1, value_1)
202 json_array_foreach( constraint_array, index_2, value_2) {
203 if (index_2 <= index_1)
205 if ( tr_prefix_wildcard_match( json_string_value(value_2),
206 json_string_value(value_1))) {
207 json_array_remove(constraint_array, index_2);
209 }else if (tr_prefix_wildcard_match( json_string_value(value_1),
210 json_string_value(value_2))) {
211 json_array_set(constraint_array, index_1, value_2);
212 json_array_remove(constraint_array, index_2);
219 * Returns an array of constraint strings that is the intersection of
220 * all constraints in the constraint_set of type #type
222 static json_t *constraint_intersect_internal( TR_CONSTRAINT_SET *constraints,
223 const char *constraint_type)
225 json_t *constraint, *result = NULL;
227 json_array_foreach( (json_t *) constraints, i, constraint) {
228 merge_constraints( constraint, constraint_type);
229 if (NULL == result) {
230 result = json_object_get(constraint, constraint_type);
232 result = json_copy(result);
234 json_t *intersect, *value_1, *value_2;
235 size_t index_1, index_2;
236 intersect = json_object_get(constraint, constraint_type);
238 json_array_foreach(result, index_1, value_1) {
239 json_array_foreach(intersect, index_2, value_2) {
240 if (tr_prefix_wildcard_match( json_string_value(value_1),
241 json_string_value(value_2)))
242 goto result_acceptable;
243 else if (tr_prefix_wildcard_match(json_string_value( value_2),
244 json_string_value(value_1))) {
245 json_array_set(result, index_1, value_2);
246 goto result_acceptable;
249 json_array_remove(result, index_1);
253 result_acceptable: continue;
261 * Return the intersection of domain and realm constraints.
262 * Return is live until #request is freed.
264 TR_CONSTRAINT_SET *tr_constraint_set_intersect( TID_REQ *request,
265 TR_CONSTRAINT_SET *input)
267 json_t *domain=NULL, *realm=NULL;
268 json_t *result = NULL, *result_array = NULL;
269 if (tr_constraint_set_validate(input)) {
270 domain = constraint_intersect_internal(input, "domain");
271 realm = constraint_intersect_internal(input, "realm");
273 assert(result = json_object());
275 json_object_set_new(result, "domain", domain);
277 json_object_set_new(result, "realm", realm);
278 assert(result_array = json_array());
279 json_array_append_new(result_array, result);
280 tid_req_cleanup_json( request, result_array);
281 return (TR_CONSTRAINT_SET *) result_array;
285 int tr_constraint_set_get_match_strings(
287 TR_CONSTRAINT_SET *constraints,
288 const char *constraint_type,
289 tr_const_string **output,
292 json_t *cset = (json_t *) constraints;
293 json_t *member, *matches, *value;;
294 size_t index, array_size;
295 assert (output && output_len);
298 if (json_array_size(cset) != 1) {
299 tr_debug("Constraint set for get_match_strings has more than one member\n");
302 member = json_array_get(cset, 0);
303 matches = json_object_get(member, constraint_type);
306 array_size = json_array_size(matches);
309 *output = talloc_array_ptrtype(request, *output, array_size);
310 json_array_foreach( matches, index, value)
311 (*output)[index] = json_string_value(value);
312 *output_len = array_size;