Clean up formatting, no functional changes.
[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
40 #include <tr_filter.h>
41
42
43 int tr_filter_process_rp_permitted(TR_NAME *rp_realm,
44                                    TR_FILTER *rpp_filter,
45                                    TR_CONSTRAINT_SET *in_constraints,
46                                    TR_CONSTRAINT_SET **out_constraints,
47                                    int *out_action)
48 {
49   int i = 0, j = 0;
50
51   *out_action = TR_FILTER_ACTION_REJECT;
52   *out_constraints = NULL;
53
54   /* If this isn't a valid rp_permitted filter, return no match. */
55   if ((!rpp_filter) ||
56       (TR_FILTER_TYPE_RP_PERMITTED != rpp_filter->type)) {
57     return TR_FILTER_NO_MATCH;
58   }
59
60   /* Check if there is a match for this filter. */
61   for (i = 0; i < TR_MAX_FILTER_LINES; i++) {
62     for (j = 0; j < TR_MAX_FILTER_SPECS; j++) {
63
64       if ((rpp_filter->lines[i]) &&
65           (rpp_filter->lines[i]->specs[j]) &&
66           (tr_fspec_matches(rpp_filter->lines[i]->specs[j], rp_realm))) {
67         *out_action = rpp_filter->lines[i]->action;
68         *out_constraints = in_constraints;
69         if (rpp_filter->lines[i]->realm_cons)
70           tr_constraint_add_to_set(out_constraints,
71                                    rpp_filter->lines[i]->realm_cons);
72         if (rpp_filter->lines[i]->domain_cons)
73           tr_constraint_add_to_set(out_constraints,
74                                    rpp_filter->lines[i]->domain_cons);
75
76         return TR_FILTER_MATCH;
77       }
78     }
79   }
80   /* If there is no match, indicate that. */
81   return TR_FILTER_NO_MATCH;
82 }
83
84 void tr_fspec_free(TR_FSPEC *fspec)
85 {
86   talloc_free(fspec);
87 }
88
89 static int tr_fspec_destructor(void *obj)
90 {
91   TR_FSPEC *fspec = talloc_get_type_abort(obj, TR_FSPEC);
92
93   if (fspec->field != NULL)
94     tr_free_name(fspec->field);
95   if (fspec->match != NULL)
96     tr_free_name(fspec->match);
97   return 0;
98 }
99
100 TR_FSPEC *tr_fspec_new(TALLOC_CTX *mem_ctx)
101 {
102   TR_FSPEC *fspec = talloc(mem_ctx, TR_FSPEC);
103
104   if (fspec != NULL) {
105     fspec->field = NULL;
106     fspec->match = NULL;
107     talloc_set_destructor((void *) fspec, tr_fspec_destructor);
108   }
109   return fspec;
110 }
111
112 void tr_fspec_set_match(TR_FSPEC *fspec, TR_NAME *match)
113 {
114   if (fspec->match != NULL)
115     tr_free_name(fspec->match);
116   fspec->match = match;
117 }
118
119 /* returns 1 if the spec matches */
120 int tr_fspec_matches(TR_FSPEC *fspec, TR_NAME *name)
121 {
122   return ((fspec->match != NULL) &&
123           (0 != tr_prefix_wildcard_match(name->buf, fspec->match->buf)));
124 }
125
126 void tr_fline_free(TR_FLINE *fline)
127 {
128   talloc_free(fline);
129 }
130
131 TR_FLINE *tr_fline_new(TALLOC_CTX *mem_ctx)
132 {
133   TR_FLINE *fl = talloc(mem_ctx, TR_FLINE);
134   int ii = 0;
135
136   if (fl != NULL) {
137     fl->action = TR_FILTER_ACTION_UNKNOWN;
138     fl->realm_cons = NULL;
139     fl->domain_cons = NULL;
140     for (ii = 0; ii < TR_MAX_FILTER_SPECS; ii++)
141       fl->specs[ii] = NULL;
142   }
143   return fl;
144 }
145
146 TR_FILTER *tr_filter_new(TALLOC_CTX *mem_ctx)
147 {
148   TR_FILTER *f = talloc(mem_ctx, TR_FILTER);
149   int ii = 0;
150
151   if (f != NULL) {
152     f->type = TR_FILTER_TYPE_UNKNOWN;
153     for (ii = 0; ii < TR_MAX_FILTER_LINES; ii++)
154       f->lines[ii] = NULL;
155   }
156   return f;
157 }
158
159 void tr_filter_free(TR_FILTER *filt)
160 {
161   talloc_free(filt);
162 }
163
164 void tr_filter_set_type(TR_FILTER *filt, TR_FILTER_TYPE type)
165 {
166   filt->type = type;
167 }
168
169 TR_FILTER_TYPE tr_filter_get_type(TR_FILTER *filt)
170 {
171   return filt->type;
172 }