bfdf399bee39dea249aaa852b8e18dead06f080f
[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 <assert.h>
36
37 #include <tr_filter.h>
38 #include <tr_debug.h>
39
40 #include <trust_router/tr_constraint.h>
41
42 /* Returns TRUE (1) if the the string (str) matchs the wildcard string (wc_str), FALSE (0) if not.
43  */
44 int tr_prefix_wildcard_match (const char *str, const char *wc_str) {
45   const char *wc_post = wc_str;
46   size_t len = 0;
47   size_t wc_len = 0;
48
49   if ((!str) || (!wc_str))
50     return 0;
51
52   len = strlen(str);
53   if (0 == (wc_len = strlen(wc_str)))
54     return 0;
55
56   /* TBD -- skip leading white space? */
57   if ('*' == wc_str[0]) {
58     wc_post = &(wc_str[1]);
59     wc_len--;
60   }else if (len != wc_len)
61     return 0;
62
63
64   if (wc_len > len)
65     return 0;
66   
67   if (0 == strcmp(&(str[len-wc_len]), wc_post)) {
68     return 1;
69   }
70   else
71     return 0;
72   }
73
74 TR_CONSTRAINT_SET *tr_constraint_set_from_fline (TR_FLINE *fline)
75 {
76   json_t *cset = NULL;
77
78   if (!fline)
79     return NULL;
80
81   if (fline->realm_cons)
82     tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->realm_cons);
83   if (fline->domain_cons)
84     tr_constraint_add_to_set((TR_CONSTRAINT_SET **)&cset, fline->domain_cons);
85   
86   return (TR_CONSTRAINT_SET *) cset;
87 }
88
89 /* A constraint set is represented in json as an array of constraint
90  * objects.  So, a constraint set (cset) that consists of one realm
91  * constraint and one domain constraint might look like:
92  *
93  *      {cset: [{domain: [a.com, b.co.uk]},
94  *              {realm: [c.net, d.org]}]}
95  */
96
97 void tr_constraint_add_to_set (TR_CONSTRAINT_SET **cset, TR_CONSTRAINT *cons)
98 {
99   json_t *jcons = NULL;
100   json_t *jmatches = NULL;
101   int i = 0;
102
103   if ((!cset) || (!cons))
104     return;
105
106   /* If we don't already have a json object, create one */
107   if (!(*cset))
108     *cset = (TR_CONSTRAINT_SET *) json_array();
109
110   /* Create a json object representing cons */
111   jmatches = json_array();
112   jcons = json_object();
113
114   for (i = 0; ((i < TR_MAX_CONST_MATCHES) && (NULL != cons->matches[i])); i++) {
115     json_array_append_new(jmatches, json_string(cons->matches[i]->buf));
116   }
117
118   json_object_set_new(jcons, cons->type->buf, jmatches);
119   
120   /* Add the created object to the cset object */
121   json_array_append_new((json_t *) *cset, jcons);
122
123
124  int tr_constraint_set_validate(TR_CONSTRAINT_SET *cset)
125 {
126   json_t *json = (json_t *) cset;
127   size_t i;
128   json_t *set_member;
129   if (!json_is_array(json)){
130     tr_debug("Constraint_set is not an array");
131     return 0;
132   }
133   json_array_foreach(json, i, set_member) {
134     json_t *value;
135     const char *key;
136     if (!json_is_object(set_member)) {
137       tr_debug("Constraint member at %zu is not an object\n", i);
138       return 0;
139     }
140     json_object_foreach( set_member, key, value) {
141       size_t inner_index;
142       json_t *inner_value;
143       if (!json_is_array(value)) {
144         tr_debug("Constraint type %s at index %zu in constraint set is not an array\n", key,
145                  i);
146         return 0;
147       }
148       json_array_foreach(value, inner_index, inner_value) {
149         if (!json_is_string(inner_value)) {
150           tr_debug("Constraint type %s at index %zu in constraint set has non-string element %zu\n",
151                    key, i, inner_index);
152           return 0;
153         }
154       }
155         }
156   }
157   return 1;
158 }
159
160
161 TR_CONSTRAINT_SET *tr_constraint_set_filter( TID_REQ *request,
162                                              TR_CONSTRAINT_SET *orig,
163                                              const char *constraint_type)
164 {
165   json_t *orig_cset = (json_t*) orig;
166   json_t  *new_cs = NULL;
167   size_t index;
168   json_t *set_member;
169   if (!tr_constraint_set_validate( (TR_CONSTRAINT_SET *) orig_cset)) {
170     tr_debug ("tr_constraint_set_filter: not a valid constraint set\n");
171     return NULL;
172   }
173   assert (new_cs = json_array());
174   json_array_foreach(orig_cset, index, set_member) {
175     if (json_object_get( set_member, constraint_type))
176       json_array_append(new_cs, set_member);
177   }
178   return (TR_CONSTRAINT_SET *) new_cs;
179 }
180
181 /**
182  * Within a given constraint object merge any overlapping domain or
183  * realm constraints.  For example ['*','*.net'] can be simplified to
184  * ['*']
185  */
186 static void merge_constraints(json_t *constraint, const char *key)
187 {
188   json_t *value_1, *value_2, *constraint_array;
189   size_t index_1, index_2;
190   /*
191    * Go through the loop pairwise linear, removing elements where one
192    * element is a subset of the other.  Always shrik the array from
193    * the end so that index_1 never becomes invalid (swapping if
194    * needed).
195    */
196   constraint_array = json_object_get(constraint, key);
197   if (NULL == constraint_array)
198       return;
199   json_array_foreach( constraint_array, index_1, value_1)
200     json_array_foreach( constraint_array, index_2, value_2) {
201     if (index_2 <= index_1)
202       continue;
203     if ( tr_prefix_wildcard_match( json_string_value(value_2),
204                                    json_string_value(value_1))) {
205       json_array_remove(constraint_array, index_2);
206       index_2--;
207     }else if (tr_prefix_wildcard_match( json_string_value(value_1),
208                                         json_string_value(value_2))) {
209       json_array_set(constraint_array, index_1, value_2);
210       json_array_remove(constraint_array, index_2);
211       index_2--;
212     }
213   }
214 }
215
216 /**
217  * Returns an array of constraint strings that is the intersection of
218  * all constraints in the constraint_set of type #type
219  */
220 static json_t *constraint_intersect_internal( TR_CONSTRAINT_SET *constraints,
221                                               const char *constraint_type)
222 {
223   json_t *constraint, *result = NULL;
224   size_t i;
225   json_array_foreach( (json_t *) constraints, i, constraint) {
226     merge_constraints( constraint, constraint_type);
227     if (NULL == result) {
228       result = json_object_get(constraint, constraint_type);
229       if (NULL != result)
230         result = json_copy(result);
231     }    else {
232       json_t *intersect, *value_1, *value_2;
233       size_t index_1, index_2;
234       intersect = json_object_get(constraint, constraint_type);
235     result_loop:
236       json_array_foreach(result, index_1, value_1) {
237         json_array_foreach(intersect, index_2, value_2) {
238           if (tr_prefix_wildcard_match( json_string_value(value_1),
239                                         json_string_value(value_2)))
240             goto result_acceptable;
241           else if (tr_prefix_wildcard_match(json_string_value( value_2),
242                                             json_string_value(value_1))) {
243             json_array_set(result, index_1, value_2);
244             goto result_acceptable;
245         }
246         }
247         json_array_remove(result, index_1);
248         if (index_1 == 0)
249           goto result_loop;
250         index_1--;
251       result_acceptable: continue;
252       }
253     }
254   }
255   return result;
256 }
257
258 /**
259  * Return the intersection of domain and realm constraints.
260  * Return is live until #request is freed.
261  */
262 TR_CONSTRAINT_SET *tr_constraint_set_intersect( TID_REQ *request,
263                                                 TR_CONSTRAINT_SET *input)
264 {
265   json_t *domain=NULL, *realm=NULL;
266   json_t *result = NULL, *result_array = NULL;
267   if (tr_constraint_set_validate(input)) {
268     domain = constraint_intersect_internal(input, "domain");
269     realm = constraint_intersect_internal(input, "realm");
270   }
271   assert(result = json_object());
272   if (domain)
273     json_object_set_new(result, "domain", domain);
274   if (realm)
275     json_object_set_new(result, "realm", realm);
276   assert(result_array = json_array());
277   json_array_append_new(result_array, result);
278   tid_req_cleanup_json( request, result_array);
279   return (TR_CONSTRAINT_SET *) result_array;
280 }