Log incoming IP address when accepting a connection
[trust_router.git] / common / tr_config_filters.c
1 /*
2  * Copyright (c) 2012-2018, 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
35 #include <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39 #include <talloc.h>
40
41 #include <tr_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
54 #endif
55
56 static TR_CONSTRAINT *tr_cfg_parse_one_constraint(TALLOC_CTX *mem_ctx, char *ctype, json_t *jc, TR_CFG_RC *rc)
57 {
58   TR_CONSTRAINT *cons=NULL;
59   int i=0;
60
61   if ((!ctype) || (!jc) || (!rc) ||
62       (!json_is_array(jc)) ||
63       (0 >= json_array_size(jc)) ||
64       (TR_MAX_CONST_MATCHES < json_array_size(jc)) ||
65       (!json_is_string(json_array_get(jc, 0)))) {
66     tr_err("tr_cfg_parse_one_constraint: config error.");
67     *rc=TR_CFG_NOPARSE;
68     return NULL;
69   }
70
71   if (NULL==(cons=tr_constraint_new(mem_ctx))) {
72     tr_debug("tr_cfg_parse_one_constraint: Out of memory (cons).");
73     *rc=TR_CFG_NOMEM;
74     return NULL;
75   }
76
77   if (NULL==(cons->type=tr_new_name(ctype))) {
78     tr_err("tr_cfg_parse_one_constraint: Out of memory (type).");
79     *rc=TR_CFG_NOMEM;
80     tr_constraint_free(cons);
81     return NULL;
82   }
83
84   for (i=0; i < json_array_size(jc); i++) {
85     cons->matches[i]=tr_new_name(json_string_value(json_array_get(jc, i)));
86     if (cons->matches[i]==NULL) {
87       tr_err("tr_cfg_parse_one_constraint: Out of memory (match %d).", i+1);
88       *rc=TR_CFG_NOMEM;
89       tr_constraint_free(cons);
90       return NULL;
91     }
92   }
93
94   return cons;
95 }
96
97 static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR_FILTER_TYPE ftype, TR_CFG_RC *rc)
98 {
99   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
100   TR_FILTER *filt = NULL;
101   json_t *jfaction = NULL;
102   json_t *jfline = NULL;
103   json_t *jfspecs = NULL;
104   json_t *this_jfspec = NULL;
105   json_t *jfield = NULL;
106   json_t *jmatch = NULL;
107   json_t *jrc = NULL;
108   json_t *jdc = NULL;
109   json_t *this_jmatch = NULL;
110   TR_NAME *name = NULL;
111   size_t i = 0, j = 0, k = 0;
112
113   *rc = TR_CFG_ERROR;
114
115   if ((jfilt == NULL) || (rc == NULL)) {
116     tr_err("tr_cfg_parse_one_filter: null argument");
117     *rc = TR_CFG_BAD_PARAMS;
118     goto cleanup;
119   }
120
121   if (NULL == (filt = tr_filter_new(tmp_ctx))) {
122     tr_err("tr_cfg_parse_one_filter: Out of memory.");
123     *rc = TR_CFG_NOMEM;
124     goto cleanup;
125   }
126   tr_filter_set_type(filt, ftype);
127
128   /* make sure we have space to represent the filter */
129   if (json_array_size(jfilt) > TR_MAX_FILTER_LINES) {
130     tr_err("tr_cfg_parse_one_filter: Filter has too many lines, maximum of %d.", TR_MAX_FILTER_LINES);
131     *rc = TR_CFG_NOPARSE;
132     goto cleanup;
133   }
134
135   /* For each entry in the filter... */
136   json_array_foreach(jfilt, i, jfline) {
137     if ((NULL == (jfaction = json_object_get(jfline, "action"))) ||
138         (!json_is_string(jfaction))) {
139       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action.");
140       *rc = TR_CFG_NOPARSE;
141       goto cleanup;
142     }
143
144     if ((NULL == (jfspecs = json_object_get(jfline, "specs"))) ||
145         (!json_is_array(jfspecs)) ||
146         (0 == json_array_size(jfspecs))) {
147       tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs.");
148       *rc = TR_CFG_NOPARSE;
149       goto cleanup;
150     }
151
152     if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
153       tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS);
154       *rc = TR_CFG_NOPARSE;
155       goto cleanup;
156     }
157
158     if (NULL == (filt->lines[i] = tr_fline_new(filt))) {
159       tr_debug("tr_cfg_parse_one_filter: Out of memory allocating filter line %d.", i + 1);
160       *rc = TR_CFG_NOMEM;
161       goto cleanup;
162     }
163
164     if (!strcmp(json_string_value(jfaction), "accept")) {
165       filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
166     } else if (!strcmp(json_string_value(jfaction), "reject")) {
167       filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
168     } else {
169       tr_debug("tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.",
170                json_string_value(jfaction));
171       *rc = TR_CFG_NOPARSE;
172       goto cleanup;
173     }
174
175     if (NULL != (jrc = json_object_get(jfline, "realm_constraints"))) {
176       if (!json_is_array(jrc)) {
177         tr_err("tr_cfg_parse_one_filter: cannot parse realm_constraints, not an array.");
178         *rc = TR_CFG_NOPARSE;
179         goto cleanup;
180       } else if (json_array_size(jrc) > TR_MAX_CONST_MATCHES) {
181         tr_err("tr_cfg_parse_one_filter: realm_constraints has too many entries, maximum of %d.",
182                TR_MAX_CONST_MATCHES);
183         *rc = TR_CFG_NOPARSE;
184         goto cleanup;
185       } else if (json_array_size(jrc) > 0) {
186         /* ok we actually have entries to process */
187         if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(filt->lines[i], "realm", jrc, rc))) {
188           tr_debug("tr_cfg_parse_one_filter: Error parsing realm constraint");
189           *rc = TR_CFG_NOPARSE;
190           goto cleanup;
191         }
192       }
193     }
194
195     if (NULL != (jdc = json_object_get(jfline, "domain_constraints"))) {
196       if (!json_is_array(jdc)) {
197         tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array.");
198         *rc = TR_CFG_NOPARSE;
199         goto cleanup;
200       } else if (json_array_size(jdc) > TR_MAX_CONST_MATCHES) {
201         tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.",
202                TR_MAX_CONST_MATCHES);
203         *rc = TR_CFG_NOPARSE;
204         goto cleanup;
205       } else if (json_array_size(jdc) > 0) {
206         if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) {
207           tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint");
208           *rc = TR_CFG_NOPARSE;
209           goto cleanup;
210         }
211       }
212     }
213
214     /*For each filter spec within the filter line... */
215     json_array_foreach(jfspecs, j, this_jfspec) {
216       if ((NULL == (jfield = json_object_get(this_jfspec, "field"))) ||
217           (!json_is_string(jfield))) {
218         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing field for filer spec %d, filter line %d.", i,
219                  j);
220         *rc = TR_CFG_NOPARSE;
221         goto cleanup;
222       }
223
224       /* check that we have a match attribute */
225       if (NULL == (jmatch = json_object_get(this_jfspec, "match"))) {
226         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i,
227                  j);
228         *rc = TR_CFG_NOPARSE;
229         goto cleanup;
230       }
231
232       /* check that match is a string or an array */
233       if ((!json_is_string(jmatch)) && (!json_is_array(jmatch))) {
234         tr_debug(
235             "tr_cfg_parse_one_filter: Error parsing filter: match not a string or array for filter spec %d, filter line %d.",
236             i, j);
237         *rc = TR_CFG_NOPARSE;
238         goto cleanup;
239       }
240
241       /* allocate the filter spec */
242       if (NULL == (filt->lines[i]->specs[j] = tr_fspec_new(filt->lines[i]))) {
243         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
244         *rc = TR_CFG_NOMEM;
245         goto cleanup;
246       }
247
248       /* fill in the field */
249       if (NULL == (filt->lines[i]->specs[j]->field = tr_new_name(json_string_value(jfield)))) {
250         tr_debug("tr_cfg_parse_one_filter: Out of memory.");
251         *rc = TR_CFG_NOMEM;
252         goto cleanup;
253       }
254
255       /* fill in the matches */
256       if (json_is_string(jmatch)) {
257         if (NULL == (name = tr_new_name(json_string_value(jmatch)))) {
258           tr_debug("tr_cfg_parse_one_filter: Out of memory.");
259           *rc = TR_CFG_NOMEM;
260           goto cleanup;
261         }
262         tr_fspec_add_match(filt->lines[i]->specs[j], name);
263       } else {
264         /* jmatch is an array (we checked earlier) */
265         json_array_foreach(jmatch, k, this_jmatch) {
266           if (NULL == (name = tr_new_name(json_string_value(this_jmatch)))) {
267             tr_debug("tr_cfg_parse_one_filter: Out of memory.");
268             *rc = TR_CFG_NOMEM;
269             goto cleanup;
270           }
271           tr_fspec_add_match(filt->lines[i]->specs[j], name);
272         }
273       }
274       if (!tr_filter_validate_spec_field(ftype, filt->lines[i]->specs[j])){
275         tr_debug("tr_cfg_parse_one_filter: Invalid filter field \"%.*s\" for %s filter, spec %d, filter %d.",
276                  filt->lines[i]->specs[j]->field->len,
277                  filt->lines[i]->specs[j]->field->buf,
278                  tr_filter_type_to_string(filt->type),
279                  i, j);
280         *rc = TR_CFG_ERROR;
281         goto cleanup;
282       }
283     }
284   }
285
286   /* check that the filter is valid */
287   if (!tr_filter_validate(filt)) {
288     *rc = TR_CFG_ERROR;
289   } else {
290     *rc = TR_CFG_SUCCESS;
291     talloc_steal(mem_ctx, filt);
292   }
293
294 cleanup:
295   talloc_free(tmp_ctx);
296   if (*rc!=TR_CFG_SUCCESS)
297     filt=NULL;
298   return filt;
299 }
300
301 TR_FILTER_SET *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc)
302 {
303   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
304   json_t *jfilt;
305   const char *filt_label=NULL;
306   TR_FILTER *filt=NULL;
307   TR_FILTER_SET *filt_set=NULL;
308   TR_FILTER_TYPE filt_type=TR_FILTER_TYPE_UNKNOWN;
309
310   *rc=TR_CFG_ERROR;
311
312   /* no filters */
313   if (jfilts==NULL) {
314     *rc=TR_CFG_SUCCESS;
315     goto cleanup;
316   }
317
318   filt_set=tr_filter_set_new(tmp_ctx);
319   if (filt_set==NULL) {
320     tr_debug("tr_cfg_parse_filters: Unable to allocate filter set.");
321     *rc = TR_CFG_NOMEM;
322     goto cleanup;
323   }
324
325   json_object_foreach(jfilts, filt_label, jfilt) {
326     /* check that we got a filter */
327     if (jfilt == NULL) {
328       tr_debug("tr_cfg_parse_filters: Definition for %s filter is missing.", filt_label);
329       *rc = TR_CFG_NOPARSE;
330       goto cleanup;
331     }
332
333     /* check that we recognize the filter type */
334     filt_type=tr_filter_type_from_string(filt_label);
335     if (filt_type==TR_FILTER_TYPE_UNKNOWN) {
336       tr_debug("tr_cfg_parse_filters: Unrecognized filter (%s) defined.", filt_label);
337       *rc = TR_CFG_NOPARSE;
338       goto cleanup;
339     }
340
341     /* finally, parse the filter */
342     tr_debug("tr_cfg_parse_filters: Found %s filter.", filt_label);
343     filt = tr_cfg_parse_one_filter(tmp_ctx, jfilt, filt_type, rc);
344     tr_filter_set_add(filt_set, filt);
345     if (*rc != TR_CFG_SUCCESS) {
346       tr_debug("tr_cfg_parse_filters: Error parsing %s filter.", filt_label);
347       *rc = TR_CFG_NOPARSE;
348       goto cleanup;
349     }
350   }
351
352   *rc=TR_CFG_SUCCESS;
353
354 cleanup:
355   if (*rc==TR_CFG_SUCCESS)
356     talloc_steal(mem_ctx, filt_set);
357   else if (filt_set!=NULL) {
358     talloc_free(filt_set);
359     filt_set=NULL;
360   }
361
362   talloc_free(tmp_ctx);
363   return filt_set;
364 }