Merge pull request #62 from painless-security/jennifer/report_incoming_ipaddr
[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 #if JANSSON_VERSION_HEX < 0x020500
36 #include "jansson_iterators.h"
37 #endif
38 #include <assert.h>
39 #include <talloc.h>
40
41 #include <tr_filter.h>
42 #include <tid_internal.h>
43 #include <tr_debug.h>
44 #include <tr_constraint_internal.h>
45
46 /**
47  * Helper for tr_constraint_destructor - calls tr_free_name on its first argument
48  *
49  * @param item void pointer to a TR_NAME
50  * @param cookie ignored
51  */
52 static void constraint_destruct_helper(void *item, void *cookie)
53 {
54   TR_NAME *name = (TR_NAME *) item;
55   tr_free_name(name);
56 }
57 static int tr_constraint_destructor(void *obj)
58 {
59   TR_CONSTRAINT *cons = talloc_get_type_abort(obj, TR_CONSTRAINT);
60
61   if (cons->type)
62     tr_free_name(cons->type);
63
64   if (cons->matches)
65     tr_list_foreach(cons->matches, constraint_destruct_helper, NULL);
66
67   return 0;
68 }
69
70 TR_CONSTRAINT *tr_constraint_new(TALLOC_CTX *mem_ctx)
71 {
72   TR_CONSTRAINT *cons = talloc(mem_ctx, TR_CONSTRAINT);
73
74   if (cons != NULL) {
75     cons->type = NULL;
76     cons->matches = tr_list_new(cons);
77     if (cons->matches == NULL) {
78       talloc_free(cons);
79       return NULL;
80     }
81     talloc_set_destructor((void *) cons, tr_constraint_destructor);
82   }
83   return cons;
84 }
85
86 void tr_constraint_free(TR_CONSTRAINT *cons)
87 {
88   talloc_free(cons);
89 }
90
91 /**
92  * Helper for tr_constraint_dup - duplicates a TR_NAME and adds it as a TR_CONSTRAINT match
93  *
94  * No return value. If this succeeds, it will have added a new entry to the TR_CONSTRAINT
95  * match list. Check the length of that - you won't be able to tell whether the allocation
96  * of the duplicate TR_NAME or the addition to the list failed, but either of those is probably
97  * due to a memory allocation failure, in which case the system is probably crashing anyway.
98  *
99  * @param item void pointer to a TR_NAME to add as a match
100  * @param cookie void pointer to a TR_CONSTRAINT to add the match to
101  */
102 static void cons_dup_helper(void *item, void *cookie)
103 {
104   TR_CONSTRAINT *new_cons = talloc_get_type_abort(cookie, TR_CONSTRAINT);
105   TR_NAME *new_name = tr_dup_name((TR_NAME *) item);
106   if (new_name) {
107     /* check that new_name is added, free if it fails */
108     if (tr_constraint_add_match(new_cons, new_name) == NULL)
109       tr_free_name(new_name);
110   }
111 }
112 /**
113  * Duplicate a TR_CONSTRAINT
114  *
115  * @param mem_ctx talloc context for the result
116  * @param cons TR_CONSTRAINT to duplicate
117  * @return pointer to the new TR_CONSTRAINT, or NULL on error
118  */
119 TR_CONSTRAINT *tr_constraint_dup(TALLOC_CTX *mem_ctx, TR_CONSTRAINT *cons)
120 {
121   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
122   TR_CONSTRAINT *new = NULL;
123
124   if (cons == NULL)
125     goto cleanup;
126
127   new = tr_constraint_new(tmp_ctx);
128   if (new == NULL)
129     goto cleanup;
130
131   new->type = tr_dup_name(cons->type);
132   if (new->type == NULL) {
133     new = NULL;
134     goto cleanup;
135   }
136
137   tr_list_foreach(cons->matches, cons_dup_helper, new); /* copies matches to new->matches */
138   /* check that we were successful - if we were, then the lists will be the same length */
139   if (tr_list_length(new->matches) != tr_list_length(cons->matches)) {
140     new = NULL;
141     goto cleanup; /* at least one dup or add failed */
142   }
143
144   /* success */
145   talloc_steal(mem_ctx, new);
146
147 cleanup:
148   talloc_free(tmp_ctx);
149   return new;
150 }
151
152 /* Returns TRUE (1) if the the string (str) matches the wildcard string (wc_str), FALSE (0) if not.
153  * Allows for a single '*' as the wildcard character if it is the first character. Leading white
154  * space is significant.
155  */
156 int tr_prefix_wildcard_match(const char *str, const char *wc_str)
157 {
158   const char *wc_post = wc_str;
159   size_t len = 0;
160   size_t wc_len = 0;
161
162   if ((!str) || (!wc_str))
163     return 0;
164
165   len = strlen(str);
166   if (0 == (wc_len = strlen(wc_str)))
167     return 0;
168
169   /* TBD -- skip leading white space? */
170   if ('*' == wc_str[0]) {
171     wc_post = &(wc_str[1]);
172     wc_len--;
173   } else if (len != wc_len)
174     return 0;
175
176
177   if (wc_len > len)
178     return 0;
179
180   if (0 == strcmp(&(str[len - wc_len]), wc_post)) {
181     return 1;
182   } else
183     return 0;
184 }
185
186 /* This combines the two constraints in a filter line (TR_FLINE) into a single
187  * set with two constraints. */
188 TR_CONSTRAINT_SET *tr_constraint_set_from_fline(TR_FLINE *fline)
189 {
190   json_t *cset = NULL;
191
192   if (!fline)
193     return NULL;
194
195   if (fline->realm_cons)
196     tr_constraint_add_to_set((TR_CONSTRAINT_SET **) &cset, fline->realm_cons);
197   if (fline->domain_cons)
198     tr_constraint_add_to_set((TR_CONSTRAINT_SET **) &cset, fline->domain_cons);
199
200   return (TR_CONSTRAINT_SET *) cset;
201 }
202
203 /* A constraint set is represented in json as an array of constraint
204  * objects.  So, a constraint set (cset) that consists of one realm
205  * constraint and one domain constraint might look like:
206  *
207  *      {cset: [{domain: [a.com, b.co.uk]},
208  *              {realm: [c.net, d.org]}]}
209  *
210  * This routine takes a TR_CONSTRAINT, converts it to its JSON representation,
211  * and adds that to the TR_CONSTRAINT_SET.
212  */
213 void tr_constraint_add_to_set(TR_CONSTRAINT_SET **cset, TR_CONSTRAINT *cons)
214 {
215   json_t *jcons = NULL;
216   json_t *jmatches = NULL;
217   TR_NAME *this_match = NULL;
218   TR_CONSTRAINT_ITER iter = {0};
219
220   if ((!cset) || (!cons))
221     return;
222
223   /* If we don't already have a json object, create one */
224   if (!(*cset))
225     *cset = (TR_CONSTRAINT_SET *) json_array();
226
227   /* Create a json object representing cons */
228   jmatches = json_array();
229   jcons = json_object();
230
231   for (this_match = tr_constraint_iter_first(&iter, cons);
232        this_match != NULL;
233        this_match = tr_constraint_iter_next(&iter))
234   {
235     json_array_append_new(jmatches, tr_name_to_json_string(this_match));
236   }
237
238   json_object_set_new(jcons, cons->type->buf, jmatches);
239
240   /* Add the created object to the cset object */
241   json_array_append_new((json_t *) *cset, jcons);
242 }
243
244 /* Test whether a JSON object has a valid structure
245  * to represent a constraint set.
246  */
247 int tr_constraint_set_validate(TR_CONSTRAINT_SET *cset) {
248   json_t *json = (json_t *) cset;
249   size_t i;
250   json_t *set_member;
251   if (!json_is_array(json)) {
252     tr_debug("Constraint_set is not an array");
253     return 0;
254   }
255   json_array_foreach(json, i, set_member) {
256     json_t *value;
257     const char *key;
258     if (!json_is_object(set_member)) {
259       tr_debug("Constraint member at %zu is not an object\n", i);
260       return 0;
261     }
262     json_object_foreach(set_member, key, value) {
263       size_t inner_index;
264       json_t *inner_value;
265       if (!json_is_array(value)) {
266         tr_debug("Constraint type %s at index %zu in constraint set is not an array\n", key,
267                  i);
268         return 0;
269       }
270       json_array_foreach(value, inner_index, inner_value) {
271         if (!json_is_string(inner_value)) {
272           tr_debug("Constraint type %s at index %zu in constraint set has non-string element %zu\n",
273                    key, i, inner_index);
274           return 0;
275         }
276       }
277     }
278   }
279   return 1;
280 }
281
282 /**
283  * Create a new constraint set containing all constraints from #orig
284  * with constraint_type #constraint_type and no others.  This constraint set is
285  * live until #request is freed.
286  */
287 TR_CONSTRAINT_SET *tr_constraint_set_filter(TID_REQ *request,
288                                             TR_CONSTRAINT_SET *orig,
289                                             const char *constraint_type)
290 {
291   json_t *orig_cset = (json_t *) orig;
292   json_t *new_cs = NULL;
293   size_t index;
294   json_t *set_member;
295   if (!tr_constraint_set_validate((TR_CONSTRAINT_SET *) orig_cset)) {
296     tr_debug ("tr_constraint_set_filter: not a valid constraint set\n");
297     return NULL;
298   }
299   new_cs = json_array();
300   assert(new_cs);
301   json_array_foreach(orig_cset, index, set_member) {
302     if (json_object_get(set_member, constraint_type))
303       json_array_append(new_cs, set_member);
304   }
305   tid_req_cleanup_json(request, new_cs);
306   return (TR_CONSTRAINT_SET *) new_cs;
307 }
308
309 /**
310  * Within a given constraint object merge any overlapping domain or
311  * realm constraints.  For example ['*','*.net'] can be simplified to
312  * ['*']
313  */
314 static void merge_constraints(json_t *constraint, const char *key)
315 {
316   json_t *value_1, *value_2, *constraint_array;
317   size_t index_1, index_2;
318   /*
319    * Go through the loop pairwise linear, removing elements where one
320    * element is a subset of the other.  Always shrik the array from
321    * the end so that index_1 never becomes invalid (swapping if
322    * needed).
323    */
324   constraint_array = json_object_get(constraint, key);
325   if (NULL == constraint_array)
326     return;
327   json_array_foreach(constraint_array, index_1, value_1)json_array_foreach(constraint_array, index_2, value_2) {
328       if (index_2 <= index_1)
329         continue;
330       if (tr_prefix_wildcard_match(json_string_value(value_2),
331                                    json_string_value(value_1))) {
332         json_array_remove(constraint_array, index_2);
333         index_2--;
334       } else if (tr_prefix_wildcard_match(json_string_value(value_1),
335                                           json_string_value(value_2))) {
336         json_array_set(constraint_array, index_1, value_2);
337         json_array_remove(constraint_array, index_2);
338         index_2--;
339       }
340     }
341 }
342
343 /**
344  * Returns an array of constraint strings that is the intersection of
345  * all constraints in the constraint_set of type #type
346  */
347 static json_t *constraint_intersect_internal(TR_CONSTRAINT_SET *constraints,
348                                              const char *constraint_type)
349 {
350   json_t *constraint, *result = NULL;
351   size_t i;
352   json_array_foreach((json_t *) constraints, i, constraint) {
353     merge_constraints(constraint, constraint_type);
354     if (NULL == result) {
355       result = json_object_get(constraint, constraint_type);
356       if (NULL != result)
357         result = json_copy(result);
358     } else {
359       json_t *intersect, *value_1, *value_2;
360       size_t index_1, index_2;
361       intersect = json_object_get(constraint, constraint_type);
362       /*If an element of the constraint set doesn't have a particular
363        * constraint type, we ignore that element of the constraint set.
364        * However, if no element of the constraint set has a particular
365        *     constraint type we return empty (no access) rather than universal
366        * access.*/
367       if (!intersect)
368         continue;
369       result_loop:
370       json_array_foreach(result, index_1, value_1) {
371         json_array_foreach(intersect, index_2, value_2) {
372           if (tr_prefix_wildcard_match(json_string_value(value_1),
373                                        json_string_value(value_2)))
374             goto result_acceptable;
375           else if (tr_prefix_wildcard_match(json_string_value(value_2),
376                                             json_string_value(value_1))) {
377             json_array_set(result, index_1, value_2);
378             goto result_acceptable;
379           }
380         }
381         json_array_remove(result, index_1);
382         if (index_1 == 0)
383           goto result_loop;
384         index_1--;
385         result_acceptable:
386         continue;
387       }
388     }
389   }
390   return result;
391 }
392
393 /**
394  * Return the intersection of domain and realm constraints.
395  * Return is live until #request is freed.
396  */
397 TR_CONSTRAINT_SET *tr_constraint_set_intersect(TID_REQ *request,
398                                                TR_CONSTRAINT_SET *input)
399 {
400   json_t *domain = NULL, *realm = NULL;
401   json_t *result = NULL, *result_array = NULL;
402   if (tr_constraint_set_validate(input)) {
403     domain = constraint_intersect_internal(input, "domain");
404     realm = constraint_intersect_internal(input, "realm");
405   }
406   result = json_object();
407   assert(result);
408   if (domain)
409     json_object_set_new(result, "domain", domain);
410   if (realm)
411     json_object_set_new(result, "realm", realm);
412   result_array = json_array();
413   assert(result_array);
414   json_array_append_new(result_array, result);
415   tid_req_cleanup_json(request, result_array);
416   return (TR_CONSTRAINT_SET *) result_array;
417 }
418
419 /** Get the set of wildcard strings that matches a fully intersected
420  * constraint set.  Requires that the constraint set only have one
421  * constraint in it, but the constraint may have multiple matches for
422  * a given type.  Returns true on success false on failure.  The
423  * output is live as long as the request is live.
424  */
425 int tr_constraint_set_get_match_strings(TID_REQ *request,
426                                         TR_CONSTRAINT_SET *constraints,
427                                         const char *constraint_type,
428                                         tr_const_string **output,
429                                         size_t *output_len)
430 {
431   json_t *cset = (json_t *) constraints;
432   json_t *member, *matches, *value;;
433   size_t index, array_size;
434   assert (output && output_len);
435   *output = NULL;
436   *output_len = 0;
437   if (json_array_size(cset) != 1) {
438     tr_debug("Constraint set for get_match_strings has more than one member\n");
439     return -1;
440   }
441   member = json_array_get(cset, 0);
442   matches = json_object_get(member, constraint_type);
443   if (!matches)
444     return -1;
445   array_size = json_array_size(matches);
446   if (array_size == 0)
447     return -1;
448   *output = talloc_array_ptrtype(request, *output, array_size);
449   json_array_foreach(matches, index, value)(*output)[index] = json_string_value(value);
450   *output_len = array_size;
451   return 0;
452 }