Support non-default TRP and TID ports
[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 <talloc.h>
39 #include <assert.h>
40
41 #include <tr_filter.h>
42 #include <trp_internal.h>
43 #include <tid_internal.h>
44 #include <tr_debug.h>
45 #include <tr_util.h>
46
47 /* Function types for handling filter fields generally. All target values
48  * are represented as strings in a TR_NAME.
49  */
50
51 /* CMP functions return values like strcmp: 0 on match, <0 on target<val, >0 on target>val */
52 typedef int (*TR_FILTER_FIELD_CMP)(TR_FILTER_TARGET *target, TR_NAME *val);
53 /* get functions return TR_NAME format of the field value. Caller must free it. */
54 typedef TR_NAME *(*TR_FILTER_FIELD_GET)(TR_FILTER_TARGET *target);
55
56 static TR_FILTER_TARGET *tr_filter_target_new(TALLOC_CTX *mem_ctx)
57 {
58   TR_FILTER_TARGET *target=talloc(mem_ctx, TR_FILTER_TARGET);
59   if (target) {
60     target->trp_inforec=NULL;
61     target->trp_upd=NULL;
62     target->tid_req=NULL;
63   }
64   return target;
65 }
66 void tr_filter_target_free(TR_FILTER_TARGET *target)
67 {
68   talloc_free(target);
69 }
70
71 /**
72  * Create a filter target for a TID request. Does not change the context of the request,
73  * so this is only valid until that is freed.
74  *
75  * @param mem_ctx talloc context for the object
76  * @param req TID request object
77  * @return pointer to a TR_FILTER_TARGET structure, or null on allocation failure
78  */
79 TR_FILTER_TARGET *tr_filter_target_tid_req(TALLOC_CTX *mem_ctx, TID_REQ *req)
80 {
81   TR_FILTER_TARGET *target=tr_filter_target_new(mem_ctx);
82   if (target)
83     target->tid_req=req; /* borrowed, not adding to our context */
84   return target;
85 }
86
87 /**
88  * Create a filter target for a TRP inforec. Does not change the context of the inforec or duplicate TR_NAMEs,
89  * so this is only valid until those are freed.
90  *
91  * @param mem_ctx talloc context for the object
92  * @param upd Update containing the TRP inforec
93  * @param inforec TRP inforec
94  * @return pointer to a TR_FILTER_TARGET structure, or null on allocation failure
95  */
96 TR_FILTER_TARGET *tr_filter_target_trp_inforec(TALLOC_CTX *mem_ctx, TRP_UPD *upd, TRP_INFOREC *inforec)
97 {
98   TR_FILTER_TARGET *target=tr_filter_target_new(mem_ctx);
99   if (target) {
100     target->trp_inforec = inforec; /* borrowed, not adding to our context */
101     target->trp_upd=upd;
102   }
103   return target;
104 }
105
106 /** Handler functions for TID RP_REALM field */
107 static int tr_ff_cmp_tid_rp_realm(TR_FILTER_TARGET *target, TR_NAME *val)
108 {
109   return tr_name_cmp(tid_req_get_rp_realm(target->tid_req), val);
110 }
111
112 static TR_NAME *tr_ff_get_tid_rp_realm(TR_FILTER_TARGET *target)
113 {
114   return tr_dup_name(tid_req_get_rp_realm(target->tid_req));
115 }
116
117 /** Handler functions for TRP info_type field */
118 static int tr_ff_cmp_trp_info_type(TR_FILTER_TARGET *target, TR_NAME *val)
119 {
120   TRP_INFOREC *inforec=target->trp_inforec;
121   char *valstr=NULL;
122   int val_type=0;
123
124   assert(val);
125   assert(inforec);
126
127   /* nothing matches unknown */
128   if (inforec->type==TRP_INFOREC_TYPE_UNKNOWN)
129     return 0;
130
131   valstr = tr_name_strdup(val); /* get this as an official null-terminated string */
132   val_type = trp_inforec_type_from_string(valstr);
133   free(valstr);
134
135   /* we do not define an ordering of info types */
136   return (val_type==inforec->type);
137 }
138
139 static TR_NAME *tr_ff_get_trp_info_type(TR_FILTER_TARGET *target)
140 {
141   TRP_INFOREC *inforec=target->trp_inforec;
142   return tr_new_name(trp_inforec_type_to_string(inforec->type));
143 }
144
145 /** Handlers for TRP realm field */
146 static int tr_ff_cmp_trp_realm(TR_FILTER_TARGET *target, TR_NAME *val)
147 {
148   return tr_name_cmp(trp_upd_get_realm(target->trp_upd), val);
149 }
150
151 static TR_NAME *tr_ff_get_trp_realm(TR_FILTER_TARGET *target)
152 {
153   return tr_dup_name(trp_upd_get_realm(target->trp_upd));
154 }
155
156 /** Handlers for TID realm field */
157 static int tr_ff_cmp_tid_realm(TR_FILTER_TARGET *target, TR_NAME *val)
158 {
159   return tr_name_cmp(tid_req_get_realm(target->tid_req), val);
160 }
161
162 static TR_NAME *tr_ff_get_tid_realm(TR_FILTER_TARGET *target)
163 {
164   return tr_dup_name(tid_req_get_realm(target->tid_req));
165 }
166
167 /** Handlers for TRP community field */
168 static int tr_ff_cmp_trp_comm(TR_FILTER_TARGET *target, TR_NAME *val)
169 {
170   return tr_name_cmp(trp_upd_get_comm(target->trp_upd), val);
171 }
172
173 static TR_NAME *tr_ff_get_trp_comm(TR_FILTER_TARGET *target)
174 {
175   return tr_dup_name(trp_upd_get_comm(target->trp_upd));
176 }
177
178 /** Handlers for TID community field */
179 static int tr_ff_cmp_tid_comm(TR_FILTER_TARGET *target, TR_NAME *val)
180 {
181   return tr_name_cmp(tid_req_get_comm(target->tid_req), val);
182 }
183
184 static TR_NAME *tr_ff_get_tid_comm(TR_FILTER_TARGET *target)
185 {
186   return tr_dup_name(tid_req_get_comm(target->tid_req));
187 }
188
189 /** Handlers for TRP community_type field */
190 static TR_NAME *tr_ff_get_trp_comm_type(TR_FILTER_TARGET *target)
191 {
192   TR_NAME *type=NULL;
193
194   switch(trp_inforec_get_comm_type(target->trp_inforec)) {
195     case TR_COMM_APC:
196       type=tr_new_name("apc");
197       break;
198     case TR_COMM_COI:
199       type=tr_new_name("coi");
200       break;
201     default:
202       type=NULL;
203       break; /* unknown types always fail */
204   }
205
206   return type;
207 }
208
209 static int tr_ff_cmp_trp_comm_type(TR_FILTER_TARGET *target, TR_NAME *val)
210 {
211   TR_NAME *type=tr_ff_get_trp_comm_type(target);
212   int retval=0;
213
214   if (type==NULL)
215     retval=1;
216   else {
217     retval = tr_name_cmp(val, type);
218     tr_free_name(type);
219   }
220   return retval;
221 }
222
223 /** Handlers for TRP realm_role field */
224 static TR_NAME *tr_ff_get_trp_realm_role(TR_FILTER_TARGET *target)
225 {
226   TR_NAME *type=NULL;
227
228   switch(trp_inforec_get_role(target->trp_inforec)) {
229     case TR_ROLE_IDP:
230       type=tr_new_name("idp");
231       break;
232     case TR_ROLE_RP:
233       type=tr_new_name("rp");
234       break;
235     default:
236       type=NULL;
237       break; /* unknown types always fail */
238   }
239
240   return type;
241 }
242
243 static int tr_ff_cmp_trp_realm_role(TR_FILTER_TARGET *target, TR_NAME *val)
244 {
245   TR_NAME *type=tr_ff_get_trp_realm_role(target);
246   int retval=0;
247
248   if (type==NULL)
249     retval=1;
250   else {
251     retval = tr_name_cmp(val, type);
252     tr_free_name(type);
253   }
254   return retval;
255 }
256
257 /** Handlers for TRP apc field */
258 /* TODO: Handle multiple APCs, not just the first */
259 static int tr_ff_cmp_trp_apc(TR_FILTER_TARGET *target, TR_NAME *val)
260 {
261   return tr_name_cmp(tr_apc_get_id(trp_inforec_get_apcs(target->trp_inforec)), val);
262 }
263
264 static TR_NAME *tr_ff_get_trp_apc(TR_FILTER_TARGET *target)
265 {
266   TR_APC *apc=trp_inforec_get_apcs(target->trp_inforec);
267   if (apc==NULL)
268     return NULL;
269
270   return tr_dup_name(tr_apc_get_id(apc));
271 }
272
273 /** Handlers for TRP owner_realm field */
274 static int tr_ff_cmp_trp_owner_realm(TR_FILTER_TARGET *target, TR_NAME *val)
275 {
276   return tr_name_cmp(trp_inforec_get_owner_realm(target->trp_inforec), val);
277 }
278
279 static TR_NAME *tr_ff_get_trp_owner_realm(TR_FILTER_TARGET *target)
280 {
281   return tr_dup_name(trp_inforec_get_owner_realm(target->trp_inforec));
282 }
283
284 /** Generic handlers for host:port fields*/
285 static TR_NAME *tr_ff_get_hostname_and_port(TR_NAME *hn, int port)
286 {
287   return tr_hostname_and_port_to_name(hn, port);
288 }
289
290 static int tr_ff_cmp_hostname_and_port(TR_NAME *hn, int port, int default_port, TR_NAME *val)
291 {
292   int cmp = -1;
293   TR_NAME *n = NULL;
294
295   /* allow a match without :port if the default port is in use */
296   if ((port == default_port) && (tr_name_cmp(hn, val) == 0))
297     return 0;
298
299   /* need to match with the :port */
300   n = tr_ff_get_hostname_and_port(hn, port);
301
302   if (n) {
303     cmp = tr_name_cmp(n, val);
304     tr_free_name(n);
305   }
306   return cmp;
307 }
308
309 /** Handlers for TRP trust_router field */
310 static int tr_ff_cmp_trp_trust_router(TR_FILTER_TARGET *target, TR_NAME *val)
311 {
312   return tr_ff_cmp_hostname_and_port(trp_inforec_get_trust_router(target->trp_inforec),
313                                      trp_inforec_get_trust_router_port(target->trp_inforec),
314                                      TRP_PORT,
315                                      val);
316 }
317
318 static TR_NAME *tr_ff_get_trp_trust_router(TR_FILTER_TARGET *target)
319 {
320   return tr_ff_get_hostname_and_port(trp_inforec_get_trust_router(target->trp_inforec),
321                                      trp_inforec_get_trust_router_port(target->trp_inforec));
322 }
323
324 /** Handlers for TRP next_hop field */
325 static int tr_ff_cmp_trp_next_hop(TR_FILTER_TARGET *target, TR_NAME *val)
326 {
327   return tr_ff_cmp_hostname_and_port(trp_inforec_get_next_hop(target->trp_inforec),
328                                      trp_inforec_get_next_hop_port(target->trp_inforec),
329                                      TID_PORT,
330                                      val);
331 }
332
333 static TR_NAME *tr_ff_get_trp_next_hop(TR_FILTER_TARGET *target)
334 {
335   return tr_ff_get_hostname_and_port(trp_inforec_get_next_hop(target->trp_inforec),
336                                      trp_inforec_get_next_hop_port(target->trp_inforec));
337 }
338
339 /** Handlers for TRP owner_contact field */
340 static int tr_ff_cmp_trp_owner_contact(TR_FILTER_TARGET *target, TR_NAME *val)
341 {
342   return tr_name_cmp(trp_inforec_get_owner_contact(target->trp_inforec), val);
343 }
344
345 static TR_NAME *tr_ff_get_trp_owner_contact(TR_FILTER_TARGET *target)
346 {
347   return tr_dup_name(trp_inforec_get_owner_contact(target->trp_inforec));
348 }
349
350 /** Handlers for TID req original_coi field */
351 static int tr_ff_cmp_tid_orig_coi(TR_FILTER_TARGET *target, TR_NAME *val)
352 {
353   return tr_name_cmp(tid_req_get_orig_coi(target->tid_req), val);
354 }
355
356 static TR_NAME *tr_ff_get_tid_orig_coi(TR_FILTER_TARGET *target)
357 {
358   return tr_dup_name(tid_req_get_orig_coi(target->tid_req));
359 }
360
361 /**
362  * Filter field handler table
363  */
364 struct tr_filter_field_entry {
365   TR_FILTER_TYPE filter_type;
366   const char *name;
367   TR_FILTER_FIELD_CMP cmp;
368   TR_FILTER_FIELD_GET get;
369 };
370 static struct tr_filter_field_entry tr_filter_field_table[] = {
371     /* realm */
372     {TR_FILTER_TYPE_TID_INBOUND, "realm", tr_ff_cmp_tid_realm, tr_ff_get_tid_realm},
373     {TR_FILTER_TYPE_TRP_INBOUND, "realm", tr_ff_cmp_trp_realm, tr_ff_get_trp_realm},
374     {TR_FILTER_TYPE_TRP_OUTBOUND, "realm", tr_ff_cmp_trp_realm, tr_ff_get_trp_realm},
375
376     /* community */
377     {TR_FILTER_TYPE_TID_INBOUND, "comm", tr_ff_cmp_tid_comm, tr_ff_get_tid_comm},
378     {TR_FILTER_TYPE_TRP_INBOUND, "comm", tr_ff_cmp_trp_comm, tr_ff_get_trp_comm},
379     {TR_FILTER_TYPE_TRP_OUTBOUND, "comm", tr_ff_cmp_trp_comm, tr_ff_get_trp_comm},
380
381     /* community type */
382     {TR_FILTER_TYPE_TRP_INBOUND, "comm_type", tr_ff_cmp_trp_comm_type, tr_ff_get_trp_comm_type},
383     {TR_FILTER_TYPE_TRP_OUTBOUND, "comm_type", tr_ff_cmp_trp_comm_type, tr_ff_get_trp_comm_type},
384
385     /* realm role */
386     {TR_FILTER_TYPE_TRP_INBOUND, "realm_role", tr_ff_cmp_trp_realm_role, tr_ff_get_trp_realm_role},
387     {TR_FILTER_TYPE_TRP_OUTBOUND, "realm_role", tr_ff_cmp_trp_realm_role, tr_ff_get_trp_realm_role},
388
389     /* apc */
390     {TR_FILTER_TYPE_TRP_INBOUND, "apc", tr_ff_cmp_trp_apc, tr_ff_get_trp_apc},
391     {TR_FILTER_TYPE_TRP_OUTBOUND, "apc", tr_ff_cmp_trp_apc, tr_ff_get_trp_apc},
392
393     /* trust_router */
394     {TR_FILTER_TYPE_TRP_INBOUND, "trust_router", tr_ff_cmp_trp_trust_router, tr_ff_get_trp_trust_router},
395     {TR_FILTER_TYPE_TRP_OUTBOUND, "trust_router", tr_ff_cmp_trp_trust_router, tr_ff_get_trp_trust_router},
396
397     /* next_hop */
398     {TR_FILTER_TYPE_TRP_INBOUND, "next_hop", tr_ff_cmp_trp_next_hop, tr_ff_get_trp_next_hop},
399     {TR_FILTER_TYPE_TRP_OUTBOUND, "next_hop", tr_ff_cmp_trp_next_hop, tr_ff_get_trp_next_hop},
400
401     /* owner_realm */
402     {TR_FILTER_TYPE_TRP_INBOUND, "owner_realm", tr_ff_cmp_trp_owner_realm, tr_ff_get_trp_owner_realm},
403     {TR_FILTER_TYPE_TRP_OUTBOUND, "owner_realm", tr_ff_cmp_trp_owner_realm, tr_ff_get_trp_owner_realm},
404
405     /* owner_contact */
406     {TR_FILTER_TYPE_TRP_INBOUND, "owner_contact", tr_ff_cmp_trp_owner_contact, tr_ff_get_trp_owner_contact},
407     {TR_FILTER_TYPE_TRP_OUTBOUND, "owner_contact", tr_ff_cmp_trp_owner_contact, tr_ff_get_trp_owner_contact},
408
409     /* rp_realm */
410     {TR_FILTER_TYPE_TID_INBOUND, "rp_realm", tr_ff_cmp_tid_rp_realm, tr_ff_get_tid_rp_realm},
411
412     /* original coi */
413     {TR_FILTER_TYPE_TID_INBOUND, "original_coi", tr_ff_cmp_tid_orig_coi, tr_ff_get_tid_orig_coi},
414
415     /* info_type */
416     {TR_FILTER_TYPE_TRP_INBOUND, "info_type", tr_ff_cmp_trp_info_type, tr_ff_get_trp_info_type},
417     {TR_FILTER_TYPE_TRP_OUTBOUND, "info_type", tr_ff_cmp_trp_info_type, tr_ff_get_trp_info_type},
418
419     /* Unknown */
420     {TR_FILTER_TYPE_UNKNOWN, NULL } /* This must be the final entry */
421 };
422
423 /* TODO: support TRP metric field (requires > < comparison instead of wildcard match) */
424
425 static struct tr_filter_field_entry *tr_filter_field_entry(TR_FILTER_TYPE filter_type, TR_NAME *field_name)
426 {
427   unsigned int ii;
428
429   for (ii=0; tr_filter_field_table[ii].filter_type!=TR_FILTER_TYPE_UNKNOWN; ii++) {
430     if ((tr_filter_field_table[ii].filter_type==filter_type)
431         && (tr_name_cmp_str(field_name, tr_filter_field_table[ii].name)==0)) {
432       return tr_filter_field_table+ii;
433     }
434   }
435   return NULL;
436 }
437
438 /**
439  * Apply a filter to a target record or TID request.
440  *
441  * If one of the filter lines matches, out_action is set to the applicable action. If constraints
442  * is not NULL, the constraints from the matching filter line will be added to the constraint set
443  * *constraints, or to a new one if *constraints is NULL. In this case, TR_FILTER_MATCH will be
444  * returned.
445  *
446  * If there is no match, returns TR_FILTER_NO_MATCH, out_action is undefined, and constraints
447  * will not be changed.
448  *
449  * @param target Record or request to which the filter is applied
450  * @param filt Filter to apply
451  * @param constraints Pointer to existing set of constraints (NULL if not tracking constraints)
452  * @param out_action Action to be carried out (output)
453  * @return TR_FILTER_MATCH or TR_FILTER_NO_MATCH
454  */
455 int tr_filter_apply(TR_FILTER_TARGET *target,
456                     TR_FILTER *filt,
457                     TR_CONSTRAINT_SET **constraints,
458                     TR_FILTER_ACTION *out_action)
459 {
460   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
461   TR_FILTER_ITER *filt_iter = tr_filter_iter_new(tmp_ctx);
462   TR_FLINE *this_fline = NULL;
463   TR_FLINE_ITER *fline_iter = tr_fline_iter_new(tmp_ctx);
464   TR_FSPEC *this_fspec = NULL;
465   int retval=TR_FILTER_NO_MATCH;
466
467   /* Default action is reject */
468   *out_action = TR_FILTER_ACTION_REJECT;
469
470   /* Validate filter */
471   if ((filt_iter == NULL) || (fline_iter == NULL) || (filt==NULL) || (filt->type==TR_FILTER_TYPE_UNKNOWN)) {
472     talloc_free(tmp_ctx);
473     return TR_FILTER_NO_MATCH;
474   }
475
476   /* Step through filter lines looking for a match. If a line matches, retval
477    * will be set to TR_FILTER_MATCH, so stop then. */
478   for (this_fline = tr_filter_iter_first(filt_iter, filt);
479        this_fline != NULL;
480        this_fline = tr_filter_iter_next(filt_iter)) {
481     /* Assume we are going to succeed. If any specs fail to match, we'll set
482      * this to TR_FILTER_NO_MATCH. */
483     retval=TR_FILTER_MATCH;
484     for (this_fspec = tr_fline_iter_first(fline_iter, this_fline);
485          this_fspec != NULL;
486          this_fspec = tr_fline_iter_next(fline_iter)) {
487       if (!tr_fspec_matches(this_fspec, filt->type, target)) {
488         retval=TR_FILTER_NO_MATCH; /* set this in case this is the last filter line */
489         break; /* give up on this filter line */
490       }
491     }
492
493     if (retval==TR_FILTER_MATCH)
494       break;
495
496   }
497
498   if (retval==TR_FILTER_MATCH) {
499     /* Matched line ii. Grab its action and constraints. */
500     *out_action = this_fline->action;
501     if (constraints!=NULL) {
502       /* if either constraint is missing, these are no-ops */
503       tr_constraint_add_to_set(constraints, this_fline->realm_cons);
504       tr_constraint_add_to_set(constraints, this_fline->domain_cons);
505     }
506   }
507
508   return retval;
509 }
510
511 void tr_fspec_free(TR_FSPEC *fspec)
512 {
513   talloc_free(fspec);
514 }
515
516 /**
517  * Helper for tr_fspec_destructor - calls tr_free_name on its first argument
518  *
519  * @param item void pointer to a TR_NAME
520  * @param cookie ignored
521  */
522 static void fspec_destruct_helper(void *item, void *cookie)
523 {
524   TR_NAME *name = (TR_NAME *) item;
525   tr_free_name(name);
526 }
527 static int tr_fspec_destructor(void *obj)
528 {
529   TR_FSPEC *fspec = talloc_get_type_abort(obj, TR_FSPEC);
530
531   if (fspec->field != NULL)
532     tr_free_name(fspec->field);
533
534   if (fspec->match)
535     tr_list_foreach(fspec->match, fspec_destruct_helper, NULL);
536
537   return 0;
538 }
539
540 TR_FSPEC *tr_fspec_new(TALLOC_CTX *mem_ctx)
541 {
542   TR_FSPEC *fspec = talloc(mem_ctx, TR_FSPEC);
543
544   if (fspec != NULL) {
545     fspec->field = NULL;
546     fspec->match = tr_list_new(fspec);
547     if (fspec->match == NULL) {
548       talloc_free(fspec);
549       return NULL;
550     }
551     talloc_set_destructor((void *)fspec, tr_fspec_destructor);
552   }
553   return fspec;
554 }
555
556 /* Helper function and cookie structure for finding a match. The helper is called
557  * for every item in the match list, even after a match is found. If a match is found,
558  * match should be pointed to the matching item. If this is not NULL, do not change it
559  * because a match has already been found. */
560 struct fspec_match_cookie { TR_NAME *name; TR_NAME *match;};
561 static void fspec_match_helper(void *item, void *data)
562 {
563   TR_NAME *this_name = (TR_NAME *) item;
564   struct fspec_match_cookie *cookie = (struct fspec_match_cookie *) data;
565   if (cookie->match == NULL) {
566     if (tr_name_prefix_wildcard_match(cookie->name, this_name))
567       cookie->match = this_name;
568   }
569 }
570 /* returns 1 if the spec matches */
571 int tr_fspec_matches(TR_FSPEC *fspec, TR_FILTER_TYPE ftype, TR_FILTER_TARGET *target)
572 {
573   struct tr_filter_field_entry *field=NULL;
574   struct fspec_match_cookie cookie = {0};
575
576   if (fspec==NULL)
577     return 0;
578
579   /* Look up how to handle the requested field */
580   field = tr_filter_field_entry(ftype, fspec->field);
581   if (field==NULL) {
582     tr_err("tr_fspec_matches: No entry to handle field %.*s for %*s filter.",
583            fspec->field->len, fspec->field->buf,
584            tr_filter_type_to_string(ftype));
585     return 0;
586   }
587
588   cookie.name = field->get(target);
589   if (cookie.name==NULL)
590     return 0; /* if there's no value, there's no match */
591
592   cookie.match = NULL;
593   tr_list_foreach(fspec->match,
594                   fspec_match_helper,
595                   &cookie);
596   if (cookie.match) {
597     tr_debug("tr_fspec_matches: Field %.*s value \"%.*s\" matches \"%.*s\" for %s filter.",
598              fspec->field->len, fspec->field->buf,
599              cookie.name->len, cookie.name->buf,
600              cookie.match->len, cookie.match->buf,
601              tr_filter_type_to_string(ftype));
602   } else {
603         tr_debug("tr_fspec_matches: Field %.*s value \"%.*s\" does not match for %s filter.",
604                  fspec->field->len, fspec->field->buf,
605                  cookie.name->len, cookie.name->buf,
606                  tr_filter_type_to_string(ftype));
607   }
608   return (cookie.match != NULL);
609 }
610
611 void tr_fline_free(TR_FLINE *fline)
612 {
613   talloc_free(fline);
614 }
615
616 TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
617 {
618   TR_FLINE *fl = talloc(mem_ctx, TR_FLINE);
619
620   if (fl != NULL) {
621     fl->action = TR_FILTER_ACTION_UNKNOWN;
622     fl->realm_cons = NULL;
623     fl->domain_cons = NULL;
624     fl->specs = tr_list_new(fl);
625     if (fl->specs == NULL) {
626       talloc_free(fl);
627       return NULL;
628     }
629   }
630   return fl;
631 }
632
633 TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
634 {
635   TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
636
637   if (f != NULL) {
638     f->type = TR_FILTER_TYPE_UNKNOWN;
639     f->lines = tr_list_new(f);
640     if (f->lines == NULL) {
641       talloc_free(f);
642       return NULL;
643     }
644   }
645   return f;
646 }
647
648 void tr_filter_free(TR_FILTER *filt)
649 {
650   talloc_free(filt);
651 }
652
653 void tr_filter_set_type(TR_FILTER *filt, TR_FILTER_TYPE type)
654 {
655   filt->type = type;
656 }
657
658 TR_FILTER_TYPE tr_filter_get_type(TR_FILTER *filt)
659 {
660   return filt->type;
661 }
662
663 /**
664  * Check that a filter is valid, i.e., can be processed.
665  *
666  * @param filt Filter to verify
667  * @return 1 if the filter is valid, 0 otherwise
668  */
669 int tr_filter_validate(TR_FILTER *filt)
670 {
671   TALLOC_CTX *tmp_ctx = talloc_new(NULL);
672   TR_FILTER_ITER *filt_iter = tr_filter_iter_new(tmp_ctx);
673   TR_FLINE *this_fline = NULL;
674   TR_FLINE_ITER *fline_iter = tr_fline_iter_new(tmp_ctx);
675   TR_FSPEC *this_fspec = NULL;
676   
677   if ((!filt) || (!filt_iter) || (!fline_iter)) {
678     talloc_free(tmp_ctx);
679     return 0;
680   }
681
682   /* check that we recognize the type */
683   switch(filt->type) {
684     case TR_FILTER_TYPE_TID_INBOUND:
685     case TR_FILTER_TYPE_TRP_INBOUND:
686     case TR_FILTER_TYPE_TRP_OUTBOUND:
687       break;
688
689     default:
690       talloc_free(tmp_ctx);
691       return 0; /* if we get here, either TR_FILTER_TYPE_UNKNOWN or an invalid value was found */
692   }
693   
694   for (this_fline = tr_filter_iter_first(filt_iter, filt);
695        this_fline != NULL;
696        this_fline = tr_filter_iter_next(filt_iter)) {
697     /* check that we recognize the action */
698     switch(this_fline->action) {
699       case TR_FILTER_ACTION_ACCEPT:
700       case TR_FILTER_ACTION_REJECT:
701         break;
702
703       default:
704         /* if we get here, either TR_FILTER_ACTION_UNKNOWN or an invalid value was found */
705         talloc_free(tmp_ctx);
706         return 0;
707     }
708
709     for (this_fspec = tr_fline_iter_first(fline_iter, this_fline);
710          this_fspec != NULL;
711          this_fspec = tr_fline_iter_next(fline_iter)) {
712       if (!tr_filter_validate_spec_field(filt->type, this_fspec)) {
713         talloc_free(tmp_ctx);
714         return 0;
715       }
716
717       /* check that at least one match is defined*/
718       if (tr_list_length(this_fspec->match) == 0) {
719         talloc_free(tmp_ctx);
720         return 0;
721       }
722     }
723   }
724
725   /* We ran the gauntlet. Success! */
726   talloc_free(tmp_ctx);
727   return 1;
728 }
729
730 int tr_filter_validate_spec_field(TR_FILTER_TYPE ftype, TR_FSPEC *fspec)
731 {
732   if ((fspec==NULL) || (tr_filter_field_entry(ftype, fspec->field)==NULL))
733     return 0; /* unknown field */
734
735   return 1;
736 }
737
738 /**
739  * Allocate a new filter set.
740  *
741  * @param mem_ctx Talloc context for the new set
742  * @return Pointer to new set, or null on error
743  */
744 TR_FILTER_SET *tr_filter_set_new(TALLOC_CTX *mem_ctx)
745 {
746   TR_FILTER_SET *set=talloc(mem_ctx, TR_FILTER_SET);
747   if (set!=NULL) {
748     set->next=NULL;
749     set->this=NULL;
750   }
751   return set;
752 }
753
754 /**
755  * Free a filter set
756  *
757  * @param fs Filter set to free
758  */
759 void tr_filter_set_free(TR_FILTER_SET *fs)
760 {
761   talloc_free(fs);
762 }
763
764 /**
765  * Find the tail of the filter set linked list.
766  *
767  * @param set Set to find tail of
768  * @return Last element in the list
769  */
770 static TR_FILTER_SET *tr_filter_set_tail(TR_FILTER_SET *set)
771 {
772   while (set->next)
773     set=set->next;
774   return set;
775 }
776
777 /**
778  * Add new filter to filter set.
779  *
780  * @param set Filter set
781  * @param new New filter to add
782  * @return 0 on success, nonzero on error
783  */
784 int tr_filter_set_add(TR_FILTER_SET *set, TR_FILTER *new)
785 {
786   TR_FILTER_SET *tail=NULL;
787
788   if (set->this==NULL)
789     tail=set;
790   else {
791     tail=tr_filter_set_tail(set);
792     tail->next=tr_filter_set_new(set);
793     if (tail->next==NULL)
794       return 1;
795     tail=tail->next;
796   }
797   tail->this=new;
798   talloc_steal(tail, new);
799   return 0;
800 }
801
802 /**
803  * Find a filter of a given type in the filter set. If there are multiple, returns the first one.
804  *
805  * @param set Filter set to search
806  * @param type Type of filter to find
807  * @return Borrowed pointer to the filter, or null if no filter of that type is found
808  */
809 TR_FILTER *tr_filter_set_get(TR_FILTER_SET *set, TR_FILTER_TYPE type)
810 {
811   TR_FILTER_SET *cur=set;
812   while(cur!=NULL) {
813     if ((cur->this != NULL) && (cur->this->type == type))
814       return cur->this;
815     cur=cur->next;
816   }
817   return NULL;
818 }
819
820 TR_FILTER_TYPE filter_type[]={TR_FILTER_TYPE_TID_INBOUND,
821                               TR_FILTER_TYPE_TRP_INBOUND,
822                               TR_FILTER_TYPE_TRP_OUTBOUND};
823 const char *filter_label[]={"tid_inbound",
824                             "trp_inbound",
825                             "trp_outbound"};
826 size_t num_filter_types=sizeof(filter_type)/sizeof(filter_type[0]);
827
828 const char *tr_filter_type_to_string(TR_FILTER_TYPE ftype)
829 {
830   size_t ii=0;
831
832   for (ii=0; ii<num_filter_types; ii++) {
833     if (ftype==filter_type[ii])
834       return filter_label[ii];
835   }
836   return "unknown";
837 }
838
839 TR_FILTER_TYPE tr_filter_type_from_string(const char *s)
840 {
841   size_t ii=0;
842
843   for(ii=0; ii<num_filter_types; ii++) {
844     if (0==strcmp(s, filter_label[ii]))
845       return filter_type[ii];
846   }
847   return TR_FILTER_TYPE_UNKNOWN;
848 }
849