Merge pull request #62 from painless-security/jennifer/report_incoming_ipaddr
[trust_router.git] / common / tr_filter_encoders.c
1 /*
2  * Copyright (c) 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 <talloc.h>
36 #include <jansson.h>
37
38 #include <tr_filter.h>
39 #include <tr_constraint_internal.h>
40
41 /* helper for below */
42 #define OBJECT_SET_OR_FAIL(jobj, key, val)     \
43 do {                                           \
44   if (val)                                     \
45     json_object_set_new((jobj),(key),(val));   \
46   else                                         \
47     goto cleanup;                              \
48 } while (0)
49
50 #define ARRAY_APPEND_OR_FAIL(jary, val)        \
51 do {                                           \
52   if (val)                                     \
53     json_array_append_new((jary),(val));       \
54   else                                         \
55     goto cleanup;                              \
56 } while (0)
57
58
59 typedef json_t *(ITEM_ENCODER_FUNC)(void *);
60
61 enum array_type {
62   ARRAY_TYPE_FSPEC,
63   ARRAY_TYPE_CONSTRAINT
64 };
65 /**
66  * Make an array of matches from a TR_FSPEC or TR_CONSTRAINT
67  *
68  * @param obj
69  * @param type
70  * @return
71  */
72 static json_t *tr_names_to_json_array(void *obj, enum array_type type)
73 {
74   json_t *jarray = json_array();
75   json_t *retval = NULL;
76   TR_FSPEC_ITER fspec_iter = {0};
77   TR_CONSTRAINT_ITER cons_iter = {0};
78   TR_NAME *this_match = NULL;
79
80   if (jarray == NULL)
81     goto cleanup;
82
83   switch(type) {
84     case ARRAY_TYPE_FSPEC:
85       this_match = tr_fspec_iter_first(&fspec_iter, (TR_FSPEC *)obj);
86       break;
87
88     case ARRAY_TYPE_CONSTRAINT:
89       this_match = tr_constraint_iter_first(&cons_iter, (TR_CONSTRAINT *)obj);
90       break;
91   }
92   while(this_match) {
93     ARRAY_APPEND_OR_FAIL(jarray, tr_name_to_json_string(this_match));
94     switch(type) {
95       case ARRAY_TYPE_FSPEC:
96         this_match = tr_fspec_iter_next(&fspec_iter);
97         break;
98
99       case ARRAY_TYPE_CONSTRAINT:
100         this_match = tr_constraint_iter_next(&cons_iter);
101         break;
102     }
103   }
104   /* success */
105   retval = jarray;
106   json_incref(retval);
107
108 cleanup:
109   if (jarray)
110     json_decref(jarray);
111
112   return retval;
113 }
114
115 static json_t *tr_fspec_to_json(TR_FSPEC *fspec)
116 {
117   json_t *fspec_json = NULL;
118   json_t *retval = NULL;
119
120   fspec_json = json_object();
121   if (fspec_json == NULL)
122     goto cleanup;
123
124   OBJECT_SET_OR_FAIL(fspec_json, "field",
125                      tr_name_to_json_string(fspec->field));
126   OBJECT_SET_OR_FAIL(fspec_json, "matches",
127                      tr_names_to_json_array(fspec, ARRAY_TYPE_FSPEC));
128
129   /* succeeded - set the return value and increment the reference count */
130   retval = fspec_json;
131   json_incref(retval);
132
133 cleanup:
134   if (fspec_json)
135     json_decref(fspec_json);
136   return retval;
137 }
138
139 static json_t *tr_fspecs_to_json_array(TR_FLINE *fline)
140 {
141   json_t *jarray = json_array();
142   json_t *retval = NULL;
143   TR_FLINE_ITER *iter = tr_fline_iter_new(NULL);
144   TR_FSPEC *this_fspec = NULL;
145
146   if ((jarray == NULL) || (iter == NULL))
147     goto cleanup;
148
149   for (this_fspec = tr_fline_iter_first(iter, fline);
150        this_fspec != NULL;
151        this_fspec = tr_fline_iter_next(iter)) {
152     ARRAY_APPEND_OR_FAIL(jarray, tr_fspec_to_json(this_fspec));
153   }
154   /* success */
155   retval = jarray;
156   json_incref(retval);
157
158 cleanup:
159   if (jarray)
160     json_decref(jarray);
161   if (iter)
162     tr_fline_iter_free(iter);
163
164   return retval;
165 }
166
167 static json_t *tr_fline_to_json(TR_FLINE *fline)
168 {
169   json_t *fline_json = NULL;
170   json_t *retval = NULL;
171
172   fline_json = json_object();
173   if (fline_json == NULL)
174     goto cleanup;
175
176   OBJECT_SET_OR_FAIL(fline_json, "action",
177                      json_string( (fline->action == TR_FILTER_ACTION_ACCEPT) ? "accept" : "reject"));
178   OBJECT_SET_OR_FAIL(fline_json, "specs",
179                      tr_fspecs_to_json_array(fline));
180   if (fline->realm_cons) {
181     OBJECT_SET_OR_FAIL(fline_json, "realm_constraints",
182                        tr_names_to_json_array(fline->realm_cons, ARRAY_TYPE_CONSTRAINT));
183   }
184   if (fline->domain_cons) {
185     OBJECT_SET_OR_FAIL(fline_json, "domain_constraints",
186                        tr_names_to_json_array(fline->domain_cons, ARRAY_TYPE_CONSTRAINT));
187   }
188
189   /* succeeded - set the return value and increment the reference count */
190   retval = fline_json;
191   json_incref(retval);
192
193 cleanup:
194   if (fline_json)
195     json_decref(fline_json);
196   return retval;
197 }
198
199 static json_t *tr_flines_to_json_array(TR_FILTER *filt)
200 {
201   json_t *jarray = json_array();
202   json_t *retval = NULL;
203   TR_FILTER_ITER *iter = tr_filter_iter_new(NULL);
204   TR_FLINE *this_fline = NULL;
205
206   if ((jarray == NULL) || (iter == NULL))
207     goto cleanup;
208
209   for(this_fline = tr_filter_iter_first(iter, filt);
210       this_fline != NULL;
211       this_fline = tr_filter_iter_next(iter)) {
212     ARRAY_APPEND_OR_FAIL(jarray, tr_fline_to_json(this_fline));
213   }
214   /* success */
215   retval = jarray;
216   json_incref(retval);
217
218 cleanup:
219   if (jarray)
220     json_decref(jarray);
221   if (iter)
222     tr_filter_iter_free(iter);
223
224   return retval;
225 }
226 json_t *tr_filter_set_to_json(TR_FILTER_SET *filter_set)
227 {
228   json_t *fset_json = NULL;
229   json_t *retval = NULL;
230   TR_FILTER *filt = NULL;
231   TR_FILTER_TYPE *filt_type = NULL;
232   TR_FILTER_TYPE types[] = {
233       TR_FILTER_TYPE_TID_INBOUND,
234       TR_FILTER_TYPE_TRP_INBOUND,
235       TR_FILTER_TYPE_TRP_OUTBOUND,
236       TR_FILTER_TYPE_UNKNOWN /* list terminator */
237   };
238
239   fset_json = json_object();
240   if (fset_json == NULL)
241     goto cleanup;
242
243   for (filt_type = types; *filt_type != TR_FILTER_TYPE_UNKNOWN; filt_type++) {
244     filt = tr_filter_set_get(filter_set, *filt_type);
245     if (filt) {
246       OBJECT_SET_OR_FAIL(fset_json, tr_filter_type_to_string(*filt_type),
247                          tr_flines_to_json_array(filt));
248     }
249   }
250
251   /* succeeded - set the return value and increment the reference count */
252   retval = fset_json;
253   json_incref(retval);
254
255 cleanup:
256   if (fset_json)
257     json_decref(fset_json);
258   return retval;
259 }
260