Use new tr_filter_apply() function for TID_REQ filtering
[trust_router.git] / common / tr_filter.c
1 /*
2  * Copyright (c) 2012, 2013, 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 <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <talloc.h>
40 #include <assert.h>
41
42 #include <tr_filter.h>
43 #include <trp_internal.h>
44 #include <tid_internal.h>
45
46 /* Function types for handling filter fields generally. All target values
47  * are represented as strings in a TR_NAME.
48  */
49 typedef int (*TR_FILTER_FIELD_CMP)(void *target, TR_NAME *val); /* returns 1 on match, 0 on no match */
50 typedef TR_NAME *(*TR_FILTER_FIELD_GET)(void *target); /* returns string form of the field value */
51
52 /* static handler prototypes */
53 static int tr_ff_cmp_tid_rp_realm(void *rp_req_arg, TR_NAME *val);
54 static TR_NAME *tr_ff_get_tid_rp_realm(void *rp_req_arg);
55 static int tr_ff_cmp_trp_info_type(void *inforec_arg, TR_NAME *val);
56 static TR_NAME *tr_ff_get_trp_info_type(void *inforec_arg);
57
58 /**
59  * Filter field handler table
60  */
61 struct tr_filter_field_entry {
62     TR_FILTER_TYPE filter_type;
63     const char *name;
64     TR_FILTER_FIELD_CMP cmp;
65     TR_FILTER_FIELD_GET get;
66 };
67 static struct tr_filter_field_entry tr_filter_field_table[] = {
68     {TR_FILTER_TYPE_TID_INBOUND, "rp_realm", tr_ff_cmp_tid_rp_realm, tr_ff_get_tid_rp_realm},
69     {TR_FILTER_TYPE_TRP_INBOUND, "info_type", tr_ff_cmp_trp_info_type, tr_ff_get_trp_info_type},
70     {TR_FILTER_TYPE_TRP_OUTBOUND, "info_type", tr_ff_cmp_trp_info_type, tr_ff_get_trp_info_type},
71     {TR_FILTER_TYPE_UNKNOWN, NULL } /* This must be the final entry */
72 };
73
74 static struct tr_filter_field_entry *tr_filter_field_entry(TR_FILTER_TYPE filter_type, TR_NAME *field_name)
75 {
76   unsigned int ii;
77
78   for (ii=0; tr_filter_field_table[ii].filter_type!=TR_FILTER_TYPE_UNKNOWN; ii++) {
79     if ((tr_filter_field_table[ii].filter_type==filter_type)
80         && (tr_name_cmp_str(field_name, tr_filter_field_table[ii].name)==0)) {
81       return tr_filter_field_table+ii;
82     }
83   }
84   return NULL;
85 }
86
87 static int tr_ff_cmp_tid_rp_realm(void *rp_req_arg, TR_NAME *val)
88 {
89   TID_REQ *req=talloc_get_type_abort(rp_req_arg, TID_REQ);
90   assert(req);
91   return 0==tr_name_cmp(val, req->rp_realm);
92 }
93
94 static TR_NAME *tr_ff_get_tid_rp_realm(void *rp_req_arg)
95 {
96   TID_REQ *req=talloc_get_type_abort(rp_req_arg, TID_REQ);
97   assert(req);
98   return tr_dup_name(req->rp_realm);
99 }
100
101 static int tr_ff_cmp_trp_info_type(void *inforec_arg, TR_NAME *val)
102 {
103   TRP_INFOREC *inforec=talloc_get_type_abort(inforec_arg, TRP_INFOREC);
104   char *valstr=NULL;
105   int val_type=0;
106
107   assert(val);
108   assert(inforec);
109
110   /* nothing matches unknown */
111   if (inforec->type==TRP_INFOREC_TYPE_UNKNOWN)
112     return 0;
113
114   valstr = tr_name_strdup(val); /* get this as an official null-terminated string */
115   val_type = trp_inforec_type_from_string(valstr);
116   free(valstr);
117
118   return (val_type==inforec->type);
119 }
120
121 static TR_NAME *tr_ff_get_trp_info_type(void *inforec_arg)
122 {
123   TRP_INFOREC *inforec=talloc_get_type_abort(inforec_arg, TRP_INFOREC);
124   return tr_new_name(trp_inforec_type_to_string(inforec->type));
125 }
126
127 /**
128  * Apply a filter to a target record or TID request.
129  *
130  * If one of the filter lines matches, out_action is set to the applicable action. If constraints
131  * is not NULL, the constraints from the matching filter line will be added to the constraint set
132  * *constraints, or to a new one if *constraints is NULL. In this case, TR_FILTER_MATCH will be
133  * returned.
134  *
135  * If there is no match, returns TR_FILTER_NO_MATCH, out_action is undefined, and constraints
136  * will not be changed.
137  *
138  * @param target Record or request to which the filter is applied
139  * @param filt Filter to apply
140  * @param constraints Pointer to existing set of constraints (NULL if not tracking constraints)
141  * @param out_action Action to be carried out (output)
142  * @return TR_FILTER_MATCH or TR_FILTER_NO_MATCH
143  */
144 int tr_filter_apply(void *target,
145                     TR_FILTER *filt,
146                     TR_CONSTRAINT_SET **constraints,
147                     TR_FILTER_ACTION *out_action)
148 {
149   unsigned int ii=0, jj=0;
150   int retval=TR_FILTER_NO_MATCH;
151
152   /* Default action is reject */
153   *out_action = TR_FILTER_ACTION_REJECT;
154
155   /* Validate filter */
156   if ((filt==NULL) || (filt->type==TR_FILTER_TYPE_UNKNOWN))
157     return TR_FILTER_NO_MATCH;
158
159   /* Step through filter lines looking for a match. If a line matches, retval
160    * will be set to TR_FILTER_MATCH, so stop then. */
161   for (ii=0, retval=TR_FILTER_NO_MATCH;
162        ii<TR_MAX_FILTER_LINES;
163        ii++) {
164     /* skip empty lines (these shouldn't really happen) */
165     if (filt->lines[ii]==NULL)
166       continue;
167
168     /* Assume we are going to succeed. If any specs fail to match, we'll set
169      * this to TR_FILTER_NO_MATCH. */
170     retval=TR_FILTER_MATCH;
171     for (jj=0; jj<TR_MAX_FILTER_SPECS; jj++) {
172       /* skip empty specs (these shouldn't really happen either) */
173       if (filt->lines[ii]->specs[jj]==NULL)
174         continue;
175
176       if (!tr_fspec_matches(filt->lines[ii]->specs[jj], filt->type, target)) {
177         retval=TR_FILTER_NO_MATCH; /* set this in case this is the last filter line */
178         break; /* give up on this filter line */
179       }
180     }
181
182     if (retval==TR_FILTER_MATCH)
183       break;
184   }
185
186   if (retval==TR_FILTER_MATCH) {
187     /* Matched line ii. Grab its action and constraints. */
188     *out_action = filt->lines[ii]->action;
189     if (constraints!=NULL) {
190       /* if either constraint is missing, these are no-ops */
191       tr_constraint_add_to_set(constraints, filt->lines[ii]->realm_cons);
192       tr_constraint_add_to_set(constraints, filt->lines[ii]->domain_cons);
193     }
194   }
195
196   return retval;
197 }
198
199 void tr_fspec_free(TR_FSPEC *fspec)
200 {
201   talloc_free(fspec);
202 }
203
204 static int tr_fspec_destructor(void *obj)
205 {
206   TR_FSPEC *fspec = talloc_get_type_abort(obj, TR_FSPEC);
207   size_t ii;
208
209   if (fspec->field != NULL)
210     tr_free_name(fspec->field);
211   for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
212     if (fspec->match[ii] != NULL)
213       tr_free_name(fspec->match[ii]);
214   }
215   return 0;
216 }
217
218 TR_FSPEC *tr_fspec_new(TALLOC_CTX *mem_ctx)
219 {
220   TR_FSPEC *fspec = talloc(mem_ctx, TR_FSPEC);
221   size_t ii=0;
222
223   if (fspec != NULL) {
224     fspec->field = NULL;
225     for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++)
226       fspec->match[ii] = NULL;
227
228     talloc_set_destructor((void *)fspec, tr_fspec_destructor);
229   }
230   return fspec;
231 }
232
233 void tr_fspec_add_match(TR_FSPEC *fspec, TR_NAME *match)
234 {
235   size_t ii;
236   for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
237     if (fspec->match[ii]==NULL) {
238       fspec->match[ii]=match;
239       break;
240     }
241   }
242   /* TODO: handle case that adding the match failed */
243 }
244
245 /* returns 1 if the spec matches */
246 int tr_fspec_matches(TR_FSPEC *fspec, TR_FILTER_TYPE ftype, void *target)
247 {
248   struct tr_filter_field_entry *field=NULL;
249   TR_NAME *name=NULL;
250   size_t ii=0;
251
252   if (fspec==NULL)
253     return 0;
254
255   /* Look up how to handle the requested field */
256   field = tr_filter_field_entry(ftype, fspec->field);
257   if (field==NULL)
258     return 0;
259
260   name=field->get(target);
261   for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
262     if (fspec->match[ii]!=NULL) {
263       if (tr_name_prefix_wildcard_match(name, fspec->match[ii]))
264         return 1;
265     }
266   }
267   return 0;
268 }
269
270 void tr_fline_free(TR_FLINE *fline)
271 {
272   talloc_free(fline);
273 }
274
275 TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
276 {
277   TR_FLINE *fl = talloc(mem_ctx, TR_FLINE);
278   int ii = 0;
279
280   if (fl != NULL) {
281     fl->action = TR_FILTER_ACTION_UNKNOWN;
282     fl->realm_cons = NULL;
283     fl->domain_cons = NULL;
284     for (ii = 0; ii < TR_MAX_FILTER_SPECS; ii++)
285       fl->specs[ii] = NULL;
286   }
287   return fl;
288 }
289
290 TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
291 {
292   TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
293   int ii = 0;
294
295   if (f != NULL) {
296     f->type = TR_FILTER_TYPE_UNKNOWN;
297     for (ii = 0; ii < TR_MAX_FILTER_LINES; ii++)
298       f->lines[ii] = NULL;
299   }
300   return f;
301 }
302
303 void tr_filter_free(TR_FILTER *filt)
304 {
305   talloc_free(filt);
306 }
307
308 void tr_filter_set_type(TR_FILTER *filt, TR_FILTER_TYPE type)
309 {
310   filt->type = type;
311 }
312
313 TR_FILTER_TYPE tr_filter_get_type(TR_FILTER *filt)
314 {
315   return filt->type;
316 }
317
318 /**
319  * Check that a filter is valid, i.e., can be processed.
320  *
321  * @param filt Filter to verify
322  * @return 1 if the filter is valid, 0 otherwise
323  */
324 int tr_filter_validate(TR_FILTER *filt)
325 {
326   size_t ii=0, jj=0, kk=0;
327
328   if (!filt)
329     return 0;
330
331   /* check that we recognize the type */
332   switch(filt->type) {
333     case TR_FILTER_TYPE_TID_INBOUND:
334     case TR_FILTER_TYPE_TRP_INBOUND:
335     case TR_FILTER_TYPE_TRP_OUTBOUND:
336       break;
337
338     default:
339       return 0; /* if we get here, either TR_FILTER_TYPE_UNKNOWN or an invalid value was found */
340   }
341   for (ii=0; ii<TR_MAX_FILTER_LINES; ii++) {
342     if (filt->lines[ii]==NULL)
343       continue; /* an empty filter line is valid */
344
345     /* check that we recognize the action */
346     switch(filt->lines[ii]->action) {
347       case TR_FILTER_ACTION_ACCEPT:
348       case TR_FILTER_ACTION_REJECT:
349         break;
350
351       default:
352         /* if we get here, either TR_FILTER_ACTION_UNKNOWN or an invalid value was found */
353         return 0;
354     }
355
356     for (jj=0; jj<TR_MAX_FILTER_SPECS; jj++) {
357       if (filt->lines[ii]->specs[jj]==NULL)
358         continue; /* an empty filter spec is valid */
359
360       if (!tr_filter_validate_spec_field(filt->type, filt->lines[ii]->specs[jj]))
361         return 0;
362
363       /* check that at least one match is non-null */
364       for (kk=0; kk<TR_MAX_FILTER_SPEC_MATCHES; kk++) {
365         if (filt->lines[ii]->specs[jj]->match[kk]!=NULL)
366           break;
367       }
368       if (kk==TR_MAX_FILTER_SPEC_MATCHES)
369         return 0;
370     }
371   }
372
373   /* We ran the gauntlet. Success! */
374   return 1;
375 }
376
377 int tr_filter_validate_spec_field(TR_FILTER_TYPE ftype, TR_FSPEC *fspec)
378 {
379   if ((fspec==NULL) || (tr_filter_field_entry(ftype, fspec->field)==NULL))
380     return 0; /* unknown field */
381
382   return 1;
383 }
384
385 /**
386  * Allocate a new filter set.
387  *
388  * @param mem_ctx Talloc context for the new set
389  * @return Pointer to new set, or null on error
390  */
391 TR_FILTER_SET *tr_filter_set_new(TALLOC_CTX *mem_ctx)
392 {
393   TR_FILTER_SET *set=talloc(mem_ctx, TR_FILTER_SET);
394   if (set!=NULL) {
395     set->next=NULL;
396     set->this=NULL;
397   }
398   return set;
399 }
400
401 /**
402  * Free a filter set
403  *
404  * @param fs Filter set to free
405  */
406 void tr_filter_set_free(TR_FILTER_SET *fs)
407 {
408   talloc_free(fs);
409 }
410
411 /**
412  * Find the tail of the filter set linked list.
413  *
414  * @param set Set to find tail of
415  * @return Last element in the list
416  */
417 static TR_FILTER_SET *tr_filter_set_tail(TR_FILTER_SET *set)
418 {
419   while (set->next)
420     set=set->next;
421   return set;
422 }
423
424 /**
425  * Add new filter to filter set.
426  *
427  * @param set Filter set
428  * @param new New filter to add
429  * @return 0 on success, nonzero on error
430  */
431 int tr_filter_set_add(TR_FILTER_SET *set, TR_FILTER *new)
432 {
433   TR_FILTER_SET *tail=NULL;
434
435   if (set->this==NULL)
436     tail=set;
437   else {
438     tail=tr_filter_set_tail(set);
439     tail->next=tr_filter_set_new(set);
440     if (tail->next==NULL)
441       return 1;
442     tail=tail->next;
443   }
444   tail->this=new;
445   talloc_steal(tail, new);
446   return 0;
447 }
448
449 /**
450  * Find a filter of a given type in the filter set. If there are multiple, returns the first one.
451  *
452  * @param set Filter set to search
453  * @param type Type of filter to find
454  * @return Borrowed pointer to the filter, or null if no filter of that type is found
455  */
456 TR_FILTER *tr_filter_set_get(TR_FILTER_SET *set, TR_FILTER_TYPE type)
457 {
458   TR_FILTER_SET *cur=set;
459   while(cur!=NULL) {
460     if ((cur->this != NULL) && (cur->this->type == type))
461       return cur->this;
462     cur=cur->next;
463   }
464   return NULL;
465 }
466
467 TR_FILTER_TYPE filter_type[]={TR_FILTER_TYPE_TID_INBOUND,
468                               TR_FILTER_TYPE_TRP_INBOUND,
469                               TR_FILTER_TYPE_TRP_OUTBOUND};
470 const char *filter_label[]={"tid_inbound",
471                             "trp_inbound",
472                             "trp_outbound"};
473 size_t num_filter_types=sizeof(filter_type)/sizeof(filter_type[0]);
474
475 const char *tr_filter_type_to_string(TR_FILTER_TYPE ftype)
476 {
477   size_t ii=0;
478
479   for (ii=0; ii<num_filter_types; ii++) {
480     if (ftype==filter_type[ii])
481       return filter_label[ii];
482   }
483   return "unknown";
484 }
485
486 TR_FILTER_TYPE tr_filter_type_from_string(const char *s)
487 {
488   size_t ii=0;
489
490   for(ii=0; ii<num_filter_types; ii++) {
491     if (0==strcmp(s, filter_label[ii]))
492       return filter_type[ii];
493   }
494   return TR_FILTER_TYPE_UNKNOWN;
495 }
496