2 * Copyright (c) 2012, 2013, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
42 #include <tr_filter.h>
43 #include <trp_internal.h>
44 #include <tid_internal.h>
46 /* Function types for handling filter fields generally. All target values
47 * are represented as strings in a TR_NAME.
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 */
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);
59 * Filter field handler table
61 struct tr_filter_field_entry {
62 TR_FILTER_TYPE filter_type;
64 TR_FILTER_FIELD_CMP cmp;
65 TR_FILTER_FIELD_GET get;
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 */
74 static struct tr_filter_field_entry *tr_filter_field_entry(TR_FILTER_TYPE filter_type, TR_NAME *field_name)
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;
87 static int tr_ff_cmp_tid_rp_realm(void *rp_req_arg, TR_NAME *val)
89 TID_REQ *req=talloc_get_type_abort(rp_req_arg, TID_REQ);
91 return 0==tr_name_cmp(val, req->rp_realm);
94 static TR_NAME *tr_ff_get_tid_rp_realm(void *rp_req_arg)
96 TID_REQ *req=talloc_get_type_abort(rp_req_arg, TID_REQ);
98 return tr_dup_name(req->rp_realm);
101 static int tr_ff_cmp_trp_info_type(void *inforec_arg, TR_NAME *val)
103 TRP_INFOREC *inforec=talloc_get_type_abort(inforec_arg, TRP_INFOREC);
110 /* nothing matches unknown */
111 if (inforec->type==TRP_INFOREC_TYPE_UNKNOWN)
114 valstr = tr_name_strdup(val); /* get this as an official null-terminated string */
115 val_type = trp_inforec_type_from_string(valstr);
118 return (val_type==inforec->type);
121 static TR_NAME *tr_ff_get_trp_info_type(void *inforec_arg)
123 TRP_INFOREC *inforec=talloc_get_type_abort(inforec_arg, TRP_INFOREC);
124 return tr_new_name(trp_inforec_type_to_string(inforec->type));
128 * Apply a filter to a target record or TID request.
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
135 * If there is no match, returns TR_FILTER_NO_MATCH, out_action is undefined, and constraints
136 * will not be changed.
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
144 int tr_filter_apply(void *target,
146 TR_CONSTRAINT_SET **constraints,
147 TR_FILTER_ACTION *out_action)
149 unsigned int ii=0, jj=0;
150 int retval=TR_FILTER_NO_MATCH;
152 /* Default action is reject */
153 *out_action = TR_FILTER_ACTION_REJECT;
155 /* Validate filter */
156 if ((filt==NULL) || (filt->type==TR_FILTER_TYPE_UNKNOWN))
157 return TR_FILTER_NO_MATCH;
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) && (retval==TR_FILTER_NO_MATCH);
164 /* skip empty lines (these shouldn't really happen) */
165 if (filt->lines[ii]==NULL)
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)
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 */
183 if (retval==TR_FILTER_MATCH) {
184 /* Matched line ii. Grab its action and constraints. */
185 *out_action = filt->lines[ii]->action;
186 if (constraints!=NULL) {
187 /* if either constraint is missing, these are no-ops */
188 tr_constraint_add_to_set(constraints, filt->lines[ii]->realm_cons);
189 tr_constraint_add_to_set(constraints, filt->lines[ii]->domain_cons);
196 int tr_filter_process_rp_permitted(TR_NAME *rp_realm,
197 TR_FILTER *rpp_filter,
198 TR_CONSTRAINT_SET *in_constraints,
199 TR_CONSTRAINT_SET **out_constraints,
200 TR_FILTER_ACTION *out_action)
204 *out_action = TR_FILTER_ACTION_REJECT;
205 *out_constraints = NULL;
207 /* If this isn't a valid rp_permitted filter, return no match. */
209 (TR_FILTER_TYPE_TID_INBOUND != rpp_filter->type)) {
210 return TR_FILTER_NO_MATCH;
213 /* Check if there is a match for this filter. */
214 for (i = 0; i < TR_MAX_FILTER_LINES; i++) {
215 for (j = 0; j < TR_MAX_FILTER_SPECS; j++) {
217 if ((rpp_filter->lines[i]) &&
218 (rpp_filter->lines[i]->specs[j]) &&
219 (tr_fspec_matches(rpp_filter->lines[i]->specs[j], 0, rp_realm))) { /* todo: fix or remove */
220 *out_action = rpp_filter->lines[i]->action;
221 *out_constraints = in_constraints;
222 if (rpp_filter->lines[i]->realm_cons)
223 tr_constraint_add_to_set(out_constraints,
224 rpp_filter->lines[i]->realm_cons);
225 if (rpp_filter->lines[i]->domain_cons)
226 tr_constraint_add_to_set(out_constraints,
227 rpp_filter->lines[i]->domain_cons);
229 return TR_FILTER_MATCH;
233 /* If there is no match, indicate that. */
234 return TR_FILTER_NO_MATCH;
237 void tr_fspec_free(TR_FSPEC *fspec)
242 static int tr_fspec_destructor(void *obj)
244 TR_FSPEC *fspec = talloc_get_type_abort(obj, TR_FSPEC);
247 if (fspec->field != NULL)
248 tr_free_name(fspec->field);
249 for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
250 if (fspec->match[ii] != NULL)
251 tr_free_name(fspec->match[ii]);
256 TR_FSPEC *tr_fspec_new(TALLOC_CTX *mem_ctx)
258 TR_FSPEC *fspec = talloc(mem_ctx, TR_FSPEC);
263 for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++)
264 fspec->match[ii] = NULL;
266 talloc_set_destructor((void *)fspec, tr_fspec_destructor);
271 void tr_fspec_add_match(TR_FSPEC *fspec, TR_NAME *match)
274 for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
275 if (fspec->match[ii]==NULL) {
276 fspec->match[ii]=match;
280 /* TODO: handle case that adding the match failed */
283 /* returns 1 if the spec matches */
284 int tr_fspec_matches(TR_FSPEC *fspec, TR_FILTER_TYPE ftype, void *target)
286 struct tr_filter_field_entry *field=NULL;
293 /* Look up how to handle the requested field */
294 field = tr_filter_field_entry(ftype, fspec->field);
298 name=field->get(target);
299 for (ii=0; ii<TR_MAX_FILTER_SPEC_MATCHES; ii++) {
300 if (fspec->match[ii]!=NULL) {
301 if (tr_name_prefix_wildcard_match(name, fspec->match[ii]))
308 void tr_fline_free(TR_FLINE *fline)
313 TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
315 TR_FLINE *fl = talloc(mem_ctx, TR_FLINE);
319 fl->action = TR_FILTER_ACTION_UNKNOWN;
320 fl->realm_cons = NULL;
321 fl->domain_cons = NULL;
322 for (ii = 0; ii < TR_MAX_FILTER_SPECS; ii++)
323 fl->specs[ii] = NULL;
328 TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
330 TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
334 f->type = TR_FILTER_TYPE_UNKNOWN;
335 for (ii = 0; ii < TR_MAX_FILTER_LINES; ii++)
341 void tr_filter_free(TR_FILTER *filt)
346 void tr_filter_set_type(TR_FILTER *filt, TR_FILTER_TYPE type)
351 TR_FILTER_TYPE tr_filter_get_type(TR_FILTER *filt)
357 * Check that a filter is valid, i.e., can be processed.
359 * @param filt Filter to verify
360 * @return 1 if the filter is valid, 0 otherwise
362 int tr_filter_validate(TR_FILTER *filt)
364 size_t ii=0, jj=0, kk=0;
369 /* check that we recognize the type */
371 case TR_FILTER_TYPE_TID_INBOUND:
372 case TR_FILTER_TYPE_TRP_INBOUND:
373 case TR_FILTER_TYPE_TRP_OUTBOUND:
377 return 0; /* if we get here, either TR_FILTER_TYPE_UNKNOWN or an invalid value was found */
379 for (ii=0; ii<TR_MAX_FILTER_LINES; ii++) {
380 if (filt->lines[ii]==NULL)
381 continue; /* an empty filter line is valid */
383 /* check that we recognize the action */
384 switch(filt->lines[ii]->action) {
385 case TR_FILTER_ACTION_ACCEPT:
386 case TR_FILTER_ACTION_REJECT:
390 /* if we get here, either TR_FILTER_ACTION_UNKNOWN or an invalid value was found */
394 for (jj=0; jj<TR_MAX_FILTER_SPECS; jj++) {
395 if (filt->lines[ii]->specs[jj]==NULL)
396 continue; /* an empty filter spec is valid */
398 if (!tr_filter_validate_spec_field(filt->type, filt->lines[ii]->specs[jj]))
401 /* check that at least one match is non-null */
402 for (kk=0; kk<TR_MAX_FILTER_SPEC_MATCHES; kk++) {
403 if (filt->lines[ii]->specs[jj]->match[kk]!=NULL)
406 if (kk==TR_MAX_FILTER_SPEC_MATCHES)
411 /* We ran the gauntlet. Success! */
415 int tr_filter_validate_spec_field(TR_FILTER_TYPE ftype, TR_FSPEC *fspec)
417 if ((fspec==NULL) || (tr_filter_field_entry(ftype, fspec->field)==NULL))
418 return 0; /* unknown field */