Refactor TR_FILTER using a GPtrArray of filter lines
[trust_router.git] / common / tr_config_rp_clients.c
1 /*
2  * Copyright (c) 2012-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 <stdlib.h>
36 #include <string.h>
37 #include <jansson.h>
38 #include <dirent.h>
39 #include <talloc.h>
40
41 #include <tr_cfgwatch.h>
42 #include <tr_comm.h>
43 #include <tr_config.h>
44 #include <tr_gss_names.h>
45 #include <tr_debug.h>
46 #include <tr_filter.h>
47 #include <trust_router/tr_constraint.h>
48 #include <tr_idp.h>
49 #include <tr.h>
50 #include <trust_router/trp.h>
51
52 #if JANSSON_VERSION_HEX < 0x020500
53 #include "jansson_iterators.h"
54 #endif
55
56
57 TR_CFG_RC tr_cfg_parse_gss_names(TALLOC_CTX *mem_ctx, json_t *jgss_names, TR_GSS_NAMES **gssn_out)
58 {
59   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
60   TR_GSS_NAMES *gn=NULL;
61   json_t *jname=NULL;
62   size_t ii=0;
63   TR_NAME *name=NULL;
64   TR_CFG_RC rc = TR_CFG_ERROR;
65
66   if (jgss_names==NULL) {
67     tr_err("tr_cfg_parse_gss_names: Bad parameters.");
68     rc=TR_CFG_BAD_PARAMS;
69     goto cleanup;
70   }
71
72   if (!json_is_array(jgss_names)) {
73     tr_err("tr_cfg_parse_gss_names: gss_names not an array.");
74     rc=TR_CFG_NOPARSE;
75     goto cleanup;
76   }
77
78   gn=tr_gss_names_new(tmp_ctx);
79   for (ii=0; ii<json_array_size(jgss_names); ii++) {
80     jname=json_array_get(jgss_names, ii);
81     if (!json_is_string(jname)) {
82       tr_err("tr_cfg_parse_gss_names: Encountered non-string gss name.");
83       rc=TR_CFG_NOPARSE;
84       goto cleanup;
85     }
86
87     name=tr_new_name(json_string_value(jname));
88     if (name==NULL) {
89       tr_err("tr_cfg_parse_gss_names: Out of memory allocating gss name.");
90       rc=TR_CFG_NOMEM;
91       goto cleanup;
92     }
93
94     if (tr_gss_names_add(gn, name)!=0) {
95       tr_free_name(name);
96       tr_err("tr_cfg_parse_gss_names: Unable to add gss name to RP client.");
97       rc=TR_CFG_ERROR;
98       goto cleanup;
99     }
100   }
101
102   *gssn_out = gn;
103   talloc_steal(mem_ctx, *gssn_out);
104   rc=TR_CFG_SUCCESS;
105
106 cleanup:
107   talloc_free(tmp_ctx);
108   return rc;
109 }
110
111 /* default filter accepts realm and *.realm */
112 static TR_FILTER_SET *tr_cfg_default_filters(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_CFG_RC *rc)
113 {
114   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
115   TR_FILTER *filt=NULL;
116   TR_FLINE *fline = NULL;
117   TR_FILTER_SET *filt_set=NULL;
118   TR_CONSTRAINT *cons=NULL;
119   TR_NAME *name=NULL;
120   TR_NAME *n_prefix=tr_new_name("*.");
121   TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
122   TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
123   TR_NAME *n_domain=tr_new_name("domain");
124   TR_NAME *n_realm=tr_new_name("realm");
125
126
127   if ((realm==NULL) || (rc==NULL)) {
128     tr_debug("tr_cfg_default_filters: invalid arguments.");
129     if (rc!=NULL)
130       *rc=TR_CFG_BAD_PARAMS;
131     goto cleanup;
132   }
133
134   if ((n_prefix==NULL) ||
135       (n_rp_realm_1==NULL) ||
136       (n_rp_realm_2==NULL) ||
137       (n_domain==NULL) ||
138       (n_realm==NULL)) {
139     tr_debug("tr_cfg_default_filters: unable to allocate names.");
140     *rc=TR_CFG_NOMEM;
141     goto cleanup;
142   }
143
144   filt=tr_filter_new(tmp_ctx);
145   if (filt==NULL) {
146     tr_debug("tr_cfg_default_filters: could not allocate filter.");
147     *rc=TR_CFG_NOMEM;
148     goto cleanup;
149   }
150   tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INBOUND);
151
152   fline = tr_fline_new(tmp_ctx);
153   if (fline==NULL) {
154     tr_debug("tr_cfg_default_filters: could not allocate filter line.");
155     *rc=TR_CFG_NOMEM;
156     goto cleanup;
157   }
158
159   fline->action=TR_FILTER_ACTION_ACCEPT;
160   fline->specs[0]=tr_fspec_new(fline);
161   fline->specs[0]->field=n_rp_realm_1;
162   n_rp_realm_1=NULL; /* we don't own this name any more */
163
164   name=tr_dup_name(realm);
165   if (name==NULL) {
166     tr_debug("tr_cfg_default_filters: could not allocate realm name.");
167     *rc=TR_CFG_NOMEM;
168     goto cleanup;
169   }
170   tr_fspec_add_match(fline->specs[0], name);
171   name=NULL; /* we no longer own the name */
172
173   /* now do the wildcard name */
174   fline->specs[1]=tr_fspec_new(fline);
175   fline->specs[1]->field=n_rp_realm_2;
176   n_rp_realm_2=NULL; /* we don't own this name any more */
177
178   if (NULL==(name=tr_name_cat(n_prefix, realm))) {
179     tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name.");
180     *rc=TR_CFG_NOMEM;
181     goto cleanup;
182   }
183
184   tr_fspec_add_match(fline->specs[1], name);
185   name=NULL; /* we no longer own the name */
186
187   /* domain constraint */
188   if (NULL==(cons=tr_constraint_new(fline))) {
189     tr_debug("tr_cfg_default_filters: could not allocate domain constraint.");
190     *rc=TR_CFG_NOMEM;
191     goto cleanup;
192   }
193
194   cons->type=n_domain;
195   n_domain=NULL; /* belongs to the constraint now */
196   name=tr_dup_name(realm);
197   if (name==NULL) {
198     tr_debug("tr_cfg_default_filters: could not allocate realm name for domain constraint.");
199     *rc=TR_CFG_NOMEM;
200     goto cleanup;
201   }
202   cons->matches[0]=name;
203   name=tr_name_cat(n_prefix, realm);
204   if (name==NULL) {
205     tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for domain constraint.");
206     *rc=TR_CFG_NOMEM;
207     goto cleanup;
208   }
209   cons->matches[1]=name;
210   name=NULL;
211   fline->domain_cons=cons;
212
213
214   /* realm constraint */
215   if (NULL==(cons=tr_constraint_new(fline))) {
216     tr_debug("tr_cfg_default_filters: could not allocate realm constraint.");
217     *rc=TR_CFG_NOMEM;
218     goto cleanup;
219   }
220
221   cons->type=n_realm;
222   n_realm=NULL; /* belongs to the constraint now */
223   name=tr_dup_name(realm);
224   if (name==NULL) {
225     tr_debug("tr_cfg_default_filters: could not allocate realm name for realm constraint.");
226     *rc=TR_CFG_NOMEM;
227     goto cleanup;
228   }
229   cons->matches[0]=name;
230   name=tr_name_cat(n_prefix, realm);
231   if (name==NULL) {
232     tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for realm constraint.");
233     *rc=TR_CFG_NOMEM;
234     goto cleanup;
235   }
236   cons->matches[1]=name;
237   name=NULL;
238   fline->realm_cons=cons;
239
240   /* put the fline in the filter */
241   if (NULL == tr_filter_add_line(filt, fline)) {
242     tr_debug("tr_cfg_default_filters: could not add line to filter.");
243     *rc = TR_CFG_NOMEM;
244     goto cleanup;
245   }
246
247   /* put the filter in a set */
248   filt_set=tr_filter_set_new(tmp_ctx);
249   if ((filt_set==NULL)||(0!=tr_filter_set_add(filt_set, filt))) {
250     tr_debug("tr_cfg_default_filters: could not allocate filter set.");
251     *rc=TR_CFG_NOMEM;
252     goto cleanup;
253   }
254   talloc_steal(mem_ctx, filt_set);
255
256 cleanup:
257   talloc_free(tmp_ctx);
258
259   if (*rc!=TR_CFG_SUCCESS)
260     filt=NULL;
261
262   if (n_prefix!=NULL)
263     tr_free_name(n_prefix);
264   if (n_rp_realm_1!=NULL)
265     tr_free_name(n_rp_realm_1);
266   if (n_rp_realm_2!=NULL)
267     tr_free_name(n_rp_realm_2);
268   if (n_realm!=NULL)
269     tr_free_name(n_realm);
270   if (n_domain!=NULL)
271     tr_free_name(n_domain);
272   if (name!=NULL)
273     tr_free_name(name);
274
275   return filt_set;
276 }
277
278 /* parses rp client */
279 static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
280 {
281   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
282   TR_RP_CLIENT *client=NULL;
283   TR_CFG_RC call_rc=TR_CFG_ERROR;
284   TR_FILTER_SET *new_filts=NULL;
285   TR_NAME *realm=NULL;
286   json_t *jfilt=NULL;
287   json_t *jrealm_id=NULL;
288
289   *rc=TR_CFG_ERROR; /* default to error if not set */
290
291   if ((!jrealm) || (!rc)) {
292     tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
293     if (rc)
294       *rc=TR_CFG_BAD_PARAMS;
295     goto cleanup;
296   }
297
298   if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
299     tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
300     *rc=TR_CFG_BAD_PARAMS;
301     goto cleanup;
302   }
303
304   tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
305   realm=tr_new_name(json_string_value(jrealm_id));
306   if (realm==NULL) {
307     tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
308     *rc=TR_CFG_NOMEM;
309     goto cleanup;
310   }
311
312   if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
313     tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
314     *rc=TR_CFG_NOMEM;
315     goto cleanup;
316   }
317
318   call_rc = tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &(client->gss_names));
319
320   if (call_rc!=TR_CFG_SUCCESS) {
321     tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
322     *rc=TR_CFG_NOPARSE;
323     goto cleanup;
324   }
325
326   /* parse filters */
327   jfilt=json_object_get(jrealm, "filters");
328   if (jfilt!=NULL) {
329     new_filts=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
330     if (call_rc!=TR_CFG_SUCCESS) {
331       tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
332       *rc=TR_CFG_NOPARSE;
333       goto cleanup;
334     }
335   } else {
336     tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
337     new_filts= tr_cfg_default_filters(tmp_ctx, realm, &call_rc);
338     if (call_rc!=TR_CFG_SUCCESS) {
339       tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
340       *rc=TR_CFG_NOPARSE;
341       goto cleanup;
342     }
343   }
344
345   tr_rp_client_set_filters(client, new_filts);
346   *rc=TR_CFG_SUCCESS;
347
348 cleanup:
349   if (realm!=NULL)
350     tr_free_name(realm);
351
352   if (*rc==TR_CFG_SUCCESS)
353     talloc_steal(mem_ctx, client);
354   else {
355     talloc_free(client);
356     client=NULL;
357   }
358
359   talloc_free(tmp_ctx);
360   return client;
361 }
362
363 /* Determine whether the realm is an RP realm */
364 static int tr_cfg_is_rp_realm(json_t *jrealm)
365 {
366   /* Check that we have a gss name. */
367   if (NULL != json_object_get(jrealm, "gss_names"))
368     return 1;
369   else
370     return 0;
371 }
372
373 /* Parse any rp clients in the j_realms object. Ignores other realms. */
374 TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
375 {
376   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
377   TR_RP_CLIENT *clients=NULL;
378   TR_RP_CLIENT *new_client=NULL;
379   json_t *this_jrealm=NULL;
380   int ii=0;
381
382   *rc=TR_CFG_ERROR;
383   if ((jrealms==NULL) || (!json_is_array(jrealms))) {
384     tr_err("tr_cfg_parse_rp_clients: realms not an array");
385     *rc=TR_CFG_BAD_PARAMS;
386     goto cleanup;
387   }
388
389   for (ii=0; ii<json_array_size(jrealms); ii++) {
390     this_jrealm=json_array_get(jrealms, ii);
391     if (tr_cfg_is_rp_realm(this_jrealm)) {
392       new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
393       if ((*rc)!=TR_CFG_SUCCESS) {
394         tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
395         *rc=TR_CFG_NOPARSE;
396         goto cleanup;
397       }
398       tr_rp_client_add(clients, new_client);
399     }
400   }
401
402   *rc=TR_CFG_SUCCESS;
403   talloc_steal(mem_ctx, clients);
404
405 cleanup:
406   talloc_free(tmp_ctx);
407   return clients;
408 }