09333c43738838806a1edc4cb8b2ece0ba77d2dd
[trust_router.git] / common / tr_config.c
1 /*
2  * Copyright (c) 2012, 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
40 #include <tr_config.h>
41 #include <tr.h>
42
43 void tr_print_config (FILE *stream, TR_CFG *cfg) {
44   fprintf(stream, "tr_print_config: Not yet implemented.\n");
45   return;
46 }
47
48 void tr_cfg_free (TR_CFG *cfg) {
49   /* TBD -- need to deallocate memory as part of dynamic config */
50   return;
51 }
52
53 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
54
55   if (!tr)
56     return TR_CFG_BAD_PARAMS;
57
58   if (tr->active_cfg)
59     tr_cfg_free(tr->active_cfg);
60
61   tr->active_cfg = tr->new_cfg;
62   return TR_CFG_SUCCESS;
63 }
64
65 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
66   json_t *jint = NULL;
67   json_t *jmtd = NULL;
68
69   if ((!tr) || (!tr->new_cfg) || (!jcfg))
70     return TR_CFG_BAD_PARAMS;
71
72   if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
73     return TR_CFG_NOMEM;
74
75   memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
76
77   if ((NULL != (jint = json_object_get(jcfg, "tr_internal"))) &&
78       (NULL != (jmtd = json_object_get(jint, "max_tree_depth")))) {
79     if (json_is_number(jmtd)) {
80       tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
81     } else {
82       fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
83       return TR_CFG_NOPARSE;
84     }
85   } else {
86     /* If not configured, use the default */
87     tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
88   }
89   fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
90   return TR_CFG_SUCCESS;
91 }
92
93 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
94   //  json_t *jrpr = NULL;
95   
96   return TR_CFG_SUCCESS;
97 }
98
99 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
100   TR_AAA_SERVER *aaa = NULL;
101
102   if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
103     fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
104     *rc = TR_CFG_BAD_PARAMS;
105     return NULL;
106   }
107
108   if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
109     fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
110     *rc = TR_CFG_NOMEM;
111     return NULL;
112   }
113
114   memset(aaa, 0, sizeof(TR_AAA_SERVER));
115
116   /* TBD -- Handle IPv6 addresses */
117   inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
118
119   return aaa;
120 }
121
122
123 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) 
124 {
125   TR_AAA_SERVER *aaa = NULL;
126   TR_AAA_SERVER *temp_aaa = NULL;
127   int i = 0;
128
129   for (i = 0; i < json_array_size(jaaas); i++) {
130     if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
131       return NULL;
132     }
133     /* TBD -- IPv6 addresses */
134     //    fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
135     temp_aaa->next = aaa;
136     aaa = temp_aaa;
137   }
138   return aaa;
139 }
140
141 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
142 {
143   TR_APC *apc;
144
145   *rc = TR_CFG_SUCCESS;         /* presume success */
146
147   if ((!japcs) || (!rc)) {
148     fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
149     if (rc) 
150       *rc = TR_CFG_BAD_PARAMS;
151     return NULL;
152   }
153
154   if (NULL == (apc = malloc(sizeof(TR_APC)))) {
155     fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
156     *rc = TR_CFG_NOMEM;
157     return NULL;
158   }
159
160   memset(apc, 0, sizeof(TR_APC));
161
162   /* TBD, deal with more than one APC.  In the meantime, though...                */
163   /* Only parse the first APC, because we only know how to deal with one, anyway. */
164   if (0 == json_array_size(japcs))
165     return NULL;
166
167   if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
168     fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
169     *rc = TR_CFG_NOMEM;
170     return NULL;
171   }
172
173   return apc;
174 }
175
176 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
177   TR_IDP_REALM *idp = NULL;
178   json_t *jrid = NULL;
179   json_t *jscfg = NULL;
180   json_t *jsrvrs = NULL;
181   json_t *japcs = NULL;
182
183   if ((!jidp) || (!rc)) {
184     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
185     if (rc)
186       *rc = TR_CFG_BAD_PARAMS;
187     return NULL;
188   }
189
190   if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
191     fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
192     *rc = TR_CFG_NOMEM;
193     return NULL;
194   }
195
196   memset(idp, 0, sizeof(TR_IDP_REALM));
197
198   if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
199       (!json_is_string(jrid)) ||
200       (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
201       (!json_is_string(jscfg)) ||
202       (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
203       (!json_is_array(jsrvrs)) ||
204       (NULL == (japcs = json_object_get(jidp, "apcs"))) ||
205       (!json_is_array(japcs))) {
206     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
207     free(idp);
208     *rc = TR_CFG_NOPARSE;
209     return NULL;
210   }
211
212   if (0 == strcmp(json_string_value(jscfg), "no")) {
213     idp->shared_config = 0;
214   } else {
215     idp->shared_config = 1;
216   }
217
218   if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
219     free(idp);
220     fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
221     *rc = TR_CFG_NOMEM;
222     return NULL;
223   }
224
225   if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
226     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
227     tr_free_name(idp->realm_id);
228     free(idp);
229     return NULL;
230   }
231   if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
232     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
233     tr_free_name(idp->realm_id);
234     /* TBD -- free aaa_servers */;
235     free(idp);
236     return NULL;
237   }
238
239 return idp;
240 }
241
242 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) 
243 {
244   json_t *jidps = NULL;
245   TR_CFG_RC rc = TR_CFG_SUCCESS;
246   TR_IDP_REALM *idp = NULL;
247   int i = 0;
248
249   if ((!tr) || (!tr->new_cfg) || (!jcfg))
250     return TR_CFG_BAD_PARAMS;
251
252   if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
253       (!json_is_array(jidps))) {
254     return TR_CFG_NOPARSE;
255   }
256
257   for (i = 0; i < json_array_size(jidps); i++) {
258     if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
259                                                   json_array_get(jidps, i), 
260                                                   &rc))) {
261        return rc;
262     }
263     fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
264     idp->next = tr->new_cfg->idp_realms;
265     tr->new_cfg->idp_realms = idp;
266   }
267   return rc;
268 }
269
270 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *idps, TR_CFG_RC *rc)
271 {
272   TR_IDP_REALM *idp;
273
274   return (idp = malloc(sizeof(TR_IDP_REALM)));
275 }
276
277 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *rps, TR_CFG_RC *rc)
278 {
279   TR_RP_REALM *rp;
280
281   return (rp = malloc(sizeof(TR_RP_REALM)));
282 }
283
284 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
285   TR_COMM *comm = NULL;
286   json_t *jid = NULL;
287   json_t *jtype = NULL;
288   json_t *japcs = NULL;
289   json_t *jidps = NULL;
290   json_t *jrps = NULL;
291
292   if ((!jcomm) || (!rc)) {
293     fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
294     if (rc)
295       *rc = TR_CFG_BAD_PARAMS;
296     return NULL;
297   }
298
299   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
300     fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
301     *rc = TR_CFG_NOMEM;
302     return NULL;
303   }
304
305   memset(comm, 0, sizeof(TR_COMM));
306
307   if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
308       (!json_is_string(jid)) ||
309       (NULL == (jtype = json_object_get(jcomm, "type"))) ||
310       (!json_is_string(jtype)) ||
311       (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
312       (!json_is_array(japcs)) ||
313       (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
314       (!json_is_array(jidps)) ||
315       (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
316       (!json_is_array(jrps))) {
317     fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
318     free(comm);
319     *rc = TR_CFG_NOPARSE;
320     return NULL;
321   }
322
323   if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
324     free(comm);
325     fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
326     *rc = TR_CFG_NOMEM;
327     return NULL;
328   }
329
330   if (0 == strcmp(json_string_value(jtype), "apc")) {
331     comm->type = TR_COMM_APC;
332   } else if (0 == strcmp(json_string_value(jtype), "coi")) {
333     comm->type = TR_COMM_COI;
334     if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
335       fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
336       tr_free_name(comm->id);
337       free(comm);
338       return NULL;
339     }
340   } else {
341     fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
342     tr_free_name(comm->id);
343     free(comm);
344     *rc = TR_CFG_NOPARSE;
345     return NULL;
346   }
347
348   comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
349   if (TR_CFG_SUCCESS != *rc) {
350     fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
351     tr_free_name(comm->id);
352     free(comm);
353     return NULL;
354   }
355
356   comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
357   if (TR_CFG_SUCCESS != *rc) {
358     fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
359     tr_free_name(comm->id);
360     /* TBD -- free idps? */;
361     free(comm);
362     return NULL;
363   }
364
365   return comm;
366 }
367
368 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) 
369 {
370   json_t *jcomms = NULL;
371   TR_CFG_RC rc = TR_CFG_SUCCESS;
372   TR_COMM *comm = NULL;
373   int i = 0;
374
375   if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
376     fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
377     return TR_CFG_BAD_PARAMS;
378   }
379
380   if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
381     fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
382     return TR_CFG_NOMEM;
383   }
384   memset (comm, 0, sizeof(TR_COMM));
385
386   if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
387       (!json_is_array(jcomms))) {
388     return TR_CFG_NOPARSE;
389   }
390
391   for (i = 0; i < json_array_size(jcomms); i++) {
392     if (NULL == (comm = tr_cfg_parse_one_comm(tr, 
393                                               json_array_get(jcomms, i), 
394                                               &rc))) {
395        return rc;
396     }
397     fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
398     comm->next = tr->new_cfg->comms;
399     tr->new_cfg->comms = comm;
400   }
401   return rc;
402 }
403
404 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
405
406   /* If there is a partial/abandoned config lying around, free it */
407   if (tr->new_cfg) {
408     tr_cfg_free(tr->new_cfg);
409   }
410   
411   if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
412     return TR_CFG_NOMEM;
413
414   memset(tr->new_cfg, 0, sizeof(TR_CFG));
415
416   if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
417       (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
418       (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
419       (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
420     tr_cfg_free(tr->new_cfg);
421     return TR_CFG_ERROR;
422   }
423   return TR_CFG_SUCCESS;
424 }
425
426 json_t *tr_read_config (int n, struct dirent **cfg_files) {
427   json_t *jcfg = NULL;
428   json_t *temp = NULL;
429   json_error_t err;
430
431   if (!cfg_files)
432     return NULL;
433
434   while (n--) {
435     fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
436     if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
437       fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
438       return NULL;
439     }
440
441     if (!jcfg) {
442       jcfg = temp;
443     }else {
444       if (-1 == json_object_update(jcfg, temp)) {
445         fprintf(stderr, "tr_read_config: Error merging config information.\n");
446         return NULL;
447       }
448     }
449   }
450
451   //  fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
452
453   return jcfg;
454 }
455
456 static int is_cfg_file(const struct dirent *dent) {
457   int n;
458
459   /* if the last four letters of the filename are .cfg, return true. */
460   if ((4 <= (n = strlen(dent->d_name))) &&
461       (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
462     return 1;
463   }
464
465   /* otherwise, return false. */
466   return 0;
467 }
468
469 int tr_find_config_files (struct dirent ***cfg_files) {
470   int n = 0, i = 0;
471   
472   n = scandir(".", cfg_files, &is_cfg_file, 0);
473
474   if (n < 0) {
475     perror("scandir");
476     fprintf(stderr, "tr_find_config: scandir error.\n");
477     return 0;
478   }
479
480   if (n == 0) {
481     fprintf (stderr, "tr_find_config: No config files found.\n");
482     return 0;
483   }
484
485   i = n;
486   while(i--) {
487     fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
488   }
489     
490   return n;
491 }