Parse RP clients from new-style configuration files.
[trust_router.git] / common / tr_constraint.c
1 /*
2  * Copyright (c) 2012-2014, 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 #include <jansson.h>
35 #include "jansson_iterators.h"
36 #include <assert.h>
37 #include <talloc.h>
38
39 #include <tr_filter.h>
40 #include <tr_debug.h>
41
42 #include <trust_router/tr_constraint.h>
43 #include <tid_internal.h>
44
45
46 static int tr_constraint_destructor(void *obj)
47 {
48   TR_CONSTRAINT *cons=talloc_get_type_abort(obj, TR_CONSTRAINT);
49   int ii=0;
50
51   if (cons->type!=NULL)
52     tr_free_name(cons->type);
53   for (ii=0; ii<TR_MAX_CONST_MATCHES; ii++) {
54     if (cons->matches[ii]!=NULL)
55       tr_free_name(cons->matches[ii]);
56   }
57   return 0;
58 }
59
60 TR_CONSTRAINT *tr_constraint_new(TALLOC_CTX *mem_ctx)
61 {
62   TR_CONSTRAINT *cons=talloc(mem_ctx, TR_CONSTRAINT);
63   int ii=0;
64
65   if (cons!=NULL) {
66     cons->type=NULL;
67     for (ii=0; ii<TR_MAX_CONST_MATCHES; ii++)
68       cons->matches[ii]=NULL;
69     talloc_set_destructor((void *)cons, tr_constraint_destructor);
70   }
71   return cons;
72 }
73
74 void tr_constraint_free(TR_CONSTRAINT *cons)
75 {
76   talloc_free(cons);
77 }
78
79 /* Returns TRUE (1) if the the string (str) matchs the wildcard string (wc_str), FALSE (0) if not.
80  */
81 int tr_prefix_wildcard_match (const char *str, const char *wc_str) {
82   const char *wc_post = wc_str;
83   size_t len = 0;
84   size_t wc_len = 0;
85
86   if ((!str) || (!wc_str))
87     return 0;
88
89   len = strlen(str);
90   if (0 == (wc_len = strlen(wc_str)))
91     return 0;
92
93   /* TBD -- skip leading white space? */
94   if ('*' == wc_str[0]) {
95     wc_post = &(wc_str[1]);
96     wc_len--;
97   }else if (len != wc_len)
98     return 0;
99
100
101   if (wc_len > len)
102     return 0;
103   
104   if (0 == strcmp(&(str[len-wc_len]), wc_post)) {
105     return 1;
106   }
107   else
108     return 0;
109   }
110
111 TR_CONSTRAINT_SET *tr_constraint_set_from_fline (TR_FLINE *fline)
112 {
113   json_t *cset = NULL;
114
115   if (!fline)
116     return NULL;
117
118   if (fline->realm_cons)
119     tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->realm_cons);
120   if (fline->domain_cons)
121     tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->domain_cons);
122   
123   return (TR_CONSTRAINT_SET *) cset;
124 }
125
126 /* A constraint set is represented in json as an array of constraint
127  * objects.  So, a constraint set (cset) that consists of one realm
128  * constraint and one domain constraint might look like:
129  *
130  *      {cset: [{domain: [a.com, b.co.uk]},
131  *              {realm: [c.net, d.org]}]}
132  */
133
134 void tr_constraint_add_to_set (TR_CONSTRAINT_SET **cset, TR_CONSTRAINT *cons)
135 {
136   json_t *jcons = NULL;
137   json_t *jmatches = NULL;
138   int i = 0;
139
140   if ((!cset) || (!cons))
141     return;
142
143   /* If we don't already have a json object, create one */
144   if (!(*cset))
145     *cset = (TR_CONSTRAINT_SET *) json_array();
146
147   /* Create a json object representing cons */
148   jmatches = json_array();
149   jcons = json_object();
150
151   for (i = 0; ((i < TR_MAX_CONST_MATCHES) && (NULL != cons->matches[i])); i++) {
152     json_array_append_new(jmatches, json_string(cons->matches[i]->buf));
153   }
154
155   json_object_set_new(jcons, cons->type->buf, jmatches);
156   
157   /* Add the created object to the cset object */
158   json_array_append_new((json_t *) *cset, jcons);
159
160
161  int tr_constraint_set_validate(TR_CONSTRAINT_SET *cset)
162 {
163   json_t *json = (json_t *) cset;
164   size_t i;
165   json_t *set_member;
166   if (!json_is_array(json)){
167     tr_debug("Constraint_set is not an array");
168     return 0;
169   }
170   json_array_foreach(json, i, set_member) {
171     json_t *value;
172     const char *key;
173     if (!json_is_object(set_member)) {
174       tr_debug("Constraint member at %zu is not an object\n", i);
175       return 0;
176     }
177     json_object_foreach( set_member, key, value) {
178       size_t inner_index;
179       json_t *inner_value;
180       if (!json_is_array(value)) {
181         tr_debug("Constraint type %s at index %zu in constraint set is not an array\n", key,
182                  i);
183         return 0;
184       }
185       json_array_foreach(value, inner_index, inner_value) {
186         if (!json_is_string(inner_value)) {
187           tr_debug("Constraint type %s at index %zu in constraint set has non-string element %zu\n",
188                    key, i, inner_index);
189           return 0;
190         }
191       }
192         }
193   }
194   return 1;
195 }
196
197
198 TR_CONSTRAINT_SET *tr_constraint_set_filter( TID_REQ *request,
199                                              TR_CONSTRAINT_SET *orig,
200                                              const char *constraint_type)
201 {
202   json_t *orig_cset = (json_t*) orig;
203   json_t  *new_cs = NULL;
204   size_t index;
205   json_t *set_member;
206   if (!tr_constraint_set_validate( (TR_CONSTRAINT_SET *) orig_cset)) {
207     tr_debug ("tr_constraint_set_filter: not a valid constraint set\n");
208     return NULL;
209   }
210   assert (new_cs = json_array());
211   json_array_foreach(orig_cset, index, set_member) {
212     if (json_object_get( set_member, constraint_type))
213       json_array_append(new_cs, set_member);
214   }
215   return (TR_CONSTRAINT_SET *) new_cs;
216 }
217
218 /**
219  * Within a given constraint object merge any overlapping domain or
220  * realm constraints.  For example ['*','*.net'] can be simplified to
221  * ['*']
222  */
223 static void merge_constraints(json_t *constraint, const char *key)
224 {
225   json_t *value_1, *value_2, *constraint_array;
226   size_t index_1, index_2;
227   /*
228    * Go through the loop pairwise linear, removing elements where one
229    * element is a subset of the other.  Always shrik the array from
230    * the end so that index_1 never becomes invalid (swapping if
231    * needed).
232    */
233   constraint_array = json_object_get(constraint, key);
234   if (NULL == constraint_array)
235       return;
236   json_array_foreach( constraint_array, index_1, value_1)
237     json_array_foreach( constraint_array, index_2, value_2) {
238     if (index_2 <= index_1)
239       continue;
240     if ( tr_prefix_wildcard_match( json_string_value(value_2),
241                                    json_string_value(value_1))) {
242       json_array_remove(constraint_array, index_2);
243       index_2--;
244     }else if (tr_prefix_wildcard_match( json_string_value(value_1),
245                                         json_string_value(value_2))) {
246       json_array_set(constraint_array, index_1, value_2);
247       json_array_remove(constraint_array, index_2);
248       index_2--;
249     }
250   }
251 }
252
253 /**
254  * Returns an array of constraint strings that is the intersection of
255  * all constraints in the constraint_set of type #type
256  */
257 static json_t *constraint_intersect_internal( TR_CONSTRAINT_SET *constraints,
258                                               const char *constraint_type)
259 {
260   json_t *constraint, *result = NULL;
261   size_t i;
262   json_array_foreach( (json_t *) constraints, i, constraint) {
263     merge_constraints( constraint, constraint_type);
264     if (NULL == result) {
265       result = json_object_get(constraint, constraint_type);
266       if (NULL != result)
267         result = json_copy(result);
268     }    else {
269       json_t *intersect, *value_1, *value_2;
270       size_t index_1, index_2;
271       intersect = json_object_get(constraint, constraint_type);
272       /*If an element of the constraint set doesn't have a particular
273        * constraint type, we ignore that element of the constraint set.
274        * However, if no element of the constraint set has a particular
275        *     constraint type we return empty (no access) rather than universal
276        * access.*/
277       if (!intersect)
278         continue;
279     result_loop:
280       json_array_foreach(result, index_1, value_1) {
281         json_array_foreach(intersect, index_2, value_2) {
282           if (tr_prefix_wildcard_match( json_string_value(value_1),
283                                         json_string_value(value_2)))
284             goto result_acceptable;
285           else if (tr_prefix_wildcard_match(json_string_value( value_2),
286                                             json_string_value(value_1))) {
287             json_array_set(result, index_1, value_2);
288             goto result_acceptable;
289         }
290         }
291         json_array_remove(result, index_1);
292         if (index_1 == 0)
293           goto result_loop;
294         index_1--;
295       result_acceptable: continue;
296       }
297     }
298   }
299   return result;
300 }
301
302 /**
303  * Return the intersection of domain and realm constraints.
304  * Return is live until #request is freed.
305  */
306 TR_CONSTRAINT_SET *tr_constraint_set_intersect( TID_REQ *request,
307                                                 TR_CONSTRAINT_SET *input)
308 {
309   json_t *domain=NULL, *realm=NULL;
310   json_t *result = NULL, *result_array = NULL;
311   if (tr_constraint_set_validate(input)) {
312     domain = constraint_intersect_internal(input, "domain");
313     realm = constraint_intersect_internal(input, "realm");
314   }
315   assert(result = json_object());
316   if (domain)
317     json_object_set_new(result, "domain", domain);
318   if (realm)
319     json_object_set_new(result, "realm", realm);
320   assert(result_array = json_array());
321   json_array_append_new(result_array, result);
322   tid_req_cleanup_json( request, result_array);
323   return (TR_CONSTRAINT_SET *) result_array;
324 }
325
326
327 int tr_constraint_set_get_match_strings(
328                                         TID_REQ *request,
329                                         TR_CONSTRAINT_SET *constraints,
330                                         const char *constraint_type,
331                                         tr_const_string **output,
332                                         size_t *output_len)
333 {
334   json_t *cset = (json_t *) constraints;
335   json_t *member, *matches, *value;;
336   size_t index, array_size;
337   assert (output && output_len);
338   *output = NULL;
339   *output_len = 0;
340   if (json_array_size(cset) != 1) {
341     tr_debug("Constraint set for get_match_strings has more than one member\n");
342     return -1;
343   }
344   member = json_array_get(cset, 0);
345   matches = json_object_get(member, constraint_type);
346   if (!matches)
347     return -1;
348   array_size = json_array_size(matches);
349   if (array_size == 0)
350     return -1;
351   *output = talloc_array_ptrtype(request, *output, array_size);
352   json_array_foreach( matches, index, value)
353     (*output)[index] = json_string_value(value);
354   *output_len = array_size;
355   return 0;
356 }