2 * Copyright (c) 2012, 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.
40 #include <tr_config.h>
43 void tr_print_config (FILE *stream, TR_CFG *cfg) {
44 fprintf(stream, "tr_print_config: Not yet implemented.\n");
48 void tr_cfg_free (TR_CFG *cfg) {
49 /* TBD -- need to deallocate memory as part of dynamic config */
53 TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
56 return TR_CFG_BAD_PARAMS;
59 tr_cfg_free(tr->active_cfg);
61 tr->active_cfg = tr->new_cfg;
62 return TR_CFG_SUCCESS;
65 static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
69 if ((!tr) || (!tr->new_cfg) || (!jcfg))
70 return TR_CFG_BAD_PARAMS;
72 if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
75 memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
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);
82 fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
83 return TR_CFG_NOPARSE;
86 /* If not configured, use the default */
87 tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
89 fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
90 return TR_CFG_SUCCESS;
93 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
94 // json_t *jrpr = NULL;
96 return TR_CFG_SUCCESS;
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;
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;
108 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
109 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
114 memset(aaa, 0, sizeof(TR_AAA_SERVER));
116 /* TBD -- Handle IPv6 addresses */
117 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
123 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
125 TR_AAA_SERVER *aaa = NULL;
126 TR_AAA_SERVER *temp_aaa = NULL;
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))) {
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;
141 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
145 *rc = TR_CFG_SUCCESS; /* presume success */
147 if ((!japcs) || (!rc)) {
148 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
150 *rc = TR_CFG_BAD_PARAMS;
154 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
155 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
160 memset(apc, 0, sizeof(TR_APC));
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))
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");
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;
179 json_t *jscfg = NULL;
180 json_t *jsrvrs = NULL;
181 json_t *japcs = NULL;
183 if ((!jidp) || (!rc)) {
184 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
186 *rc = TR_CFG_BAD_PARAMS;
190 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
191 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
196 memset(idp, 0, sizeof(TR_IDP_REALM));
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");
208 *rc = TR_CFG_NOPARSE;
212 if (0 == strcmp(json_string_value(jscfg), "no")) {
213 idp->shared_config = 0;
215 idp->shared_config = 1;
218 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
220 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
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);
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 */;
242 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
244 json_t *jidps = NULL;
245 TR_CFG_RC rc = TR_CFG_SUCCESS;
246 TR_IDP_REALM *idp = NULL;
249 if ((!tr) || (!tr->new_cfg) || (!jcfg))
250 return TR_CFG_BAD_PARAMS;
252 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
253 (!json_is_array(jidps))) {
254 return TR_CFG_NOPARSE;
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),
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;
270 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *idps, TR_CFG_RC *rc)
274 return (idp = malloc(sizeof(TR_IDP_REALM)));
277 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *rps, TR_CFG_RC *rc)
281 return (rp = malloc(sizeof(TR_RP_REALM)));
284 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
285 TR_COMM *comm = NULL;
287 json_t *jtype = NULL;
288 json_t *japcs = NULL;
289 json_t *jidps = NULL;
292 if ((!jcomm) || (!rc)) {
293 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
295 *rc = TR_CFG_BAD_PARAMS;
299 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
300 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
305 memset(comm, 0, sizeof(TR_COMM));
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");
319 *rc = TR_CFG_NOPARSE;
323 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
325 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
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);
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);
344 *rc = TR_CFG_NOPARSE;
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);
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? */;
368 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
370 json_t *jcomms = NULL;
371 TR_CFG_RC rc = TR_CFG_SUCCESS;
372 TR_COMM *comm = NULL;
375 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
376 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
377 return TR_CFG_BAD_PARAMS;
380 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
381 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
384 memset (comm, 0, sizeof(TR_COMM));
386 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
387 (!json_is_array(jcomms))) {
388 return TR_CFG_NOPARSE;
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),
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;
404 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
406 /* If there is a partial/abandoned config lying around, free it */
408 tr_cfg_free(tr->new_cfg);
411 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
414 memset(tr->new_cfg, 0, sizeof(TR_CFG));
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);
423 return TR_CFG_SUCCESS;
426 json_t *tr_read_config (int n, struct dirent **cfg_files) {
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);
444 if (-1 == json_object_update(jcfg, temp)) {
445 fprintf(stderr, "tr_read_config: Error merging config information.\n");
451 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
456 static int is_cfg_file(const struct dirent *dent) {
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"))) {
465 /* otherwise, return false. */
469 int tr_find_config_files (struct dirent ***cfg_files) {
472 n = scandir(".", cfg_files, &is_cfg_file, 0);
476 fprintf(stderr, "tr_find_config: scandir error.\n");
481 fprintf (stderr, "tr_find_config: No config files found.\n");
487 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);