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