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_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
95 TR_RP_CLIENT *rp = NULL;
99 if ((!jrp) || (!rc)) {
100 fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
102 *rc = TR_CFG_BAD_PARAMS;
106 if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
107 fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
112 memset(rp, 0, sizeof(TR_RP_CLIENT));
114 /* TBD parse filters and constraints */
116 if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
117 (!json_is_array(jgns))) {
118 fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration.\n");
120 *rc = TR_CFG_NOPARSE;
124 if (0 == json_array_size(jgns)) {
125 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has no GSS Names.\n");
126 *rc = TR_CFG_NOPARSE;
130 if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
131 fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
132 *rc = TR_CFG_NOPARSE;
136 for (i = 0; i < json_array_size(jgns); i++) {
137 if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
138 fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
147 static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
149 TR_RP_CLIENT *rp = NULL;
150 TR_CFG_RC rc = TR_CFG_SUCCESS;
153 if ((!tr) || (!tr->new_cfg) || (!jcfg))
154 return TR_CFG_BAD_PARAMS;
156 if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
157 (!json_is_array(jrps))) {
158 return TR_CFG_NOPARSE;
161 for (i = 0; i < json_array_size(jrps); i++) {
162 if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
163 json_array_get(jrps, i),
167 fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured: %s.\n", rp->gss_names[0]->buf);
168 rp->next = tr->new_cfg->rp_clients;
169 tr->new_cfg->rp_clients = rp;
174 static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
175 TR_AAA_SERVER *aaa = NULL;
177 if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
178 fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
179 *rc = TR_CFG_BAD_PARAMS;
183 if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
184 fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
189 memset(aaa, 0, sizeof(TR_AAA_SERVER));
191 /* TBD -- Handle IPv6 addresses */
192 inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
198 static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
200 TR_AAA_SERVER *aaa = NULL;
201 TR_AAA_SERVER *temp_aaa = NULL;
204 for (i = 0; i < json_array_size(jaaas); i++) {
205 if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
208 /* TBD -- IPv6 addresses */
209 // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
210 temp_aaa->next = aaa;
216 static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
220 *rc = TR_CFG_SUCCESS; /* presume success */
222 if ((!japcs) || (!rc)) {
223 fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
225 *rc = TR_CFG_BAD_PARAMS;
229 if (NULL == (apc = malloc(sizeof(TR_APC)))) {
230 fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
235 memset(apc, 0, sizeof(TR_APC));
237 /* TBD, deal with more than one APC. In the meantime, though... */
238 /* Only parse the first APC, because we only know how to deal with one, anyway. */
239 if (0 == json_array_size(japcs))
242 if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
243 fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
251 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
252 TR_IDP_REALM *idp = NULL;
254 json_t *jscfg = NULL;
255 json_t *jsrvrs = NULL;
256 json_t *japcs = NULL;
258 if ((!jidp) || (!rc)) {
259 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
261 *rc = TR_CFG_BAD_PARAMS;
265 if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
266 fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
271 memset(idp, 0, sizeof(TR_IDP_REALM));
273 if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
274 (!json_is_string(jrid)) ||
275 (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
276 (!json_is_string(jscfg)) ||
277 (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
278 (!json_is_array(jsrvrs)) ||
279 (NULL == (japcs = json_object_get(jidp, "apcs"))) ||
280 (!json_is_array(japcs))) {
281 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
283 *rc = TR_CFG_NOPARSE;
287 if (0 == strcmp(json_string_value(jscfg), "no")) {
288 idp->shared_config = 0;
290 idp->shared_config = 1;
293 if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
295 fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
300 if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
301 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
302 tr_free_name(idp->realm_id);
306 if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
307 fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
308 tr_free_name(idp->realm_id);
309 /* TBD -- free aaa_servers */;
317 static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
319 json_t *jidps = NULL;
320 TR_CFG_RC rc = TR_CFG_SUCCESS;
321 TR_IDP_REALM *idp = NULL;
324 if ((!tr) || (!tr->new_cfg) || (!jcfg))
325 return TR_CFG_BAD_PARAMS;
327 if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
328 (!json_is_array(jidps))) {
329 return TR_CFG_NOPARSE;
332 for (i = 0; i < json_array_size(jidps); i++) {
333 if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
334 json_array_get(jidps, i),
338 fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
339 idp->next = tr->new_cfg->idp_realms;
340 tr->new_cfg->idp_realms = idp;
345 static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
347 TR_IDP_REALM *idp = NULL;
348 TR_IDP_REALM *temp_idp = NULL;
353 (!json_is_array(jidps))) {
355 *rc = TR_CFG_BAD_PARAMS;
359 for (i = 0; i < json_array_size(jidps); i++) {
360 if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
361 tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
363 fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
364 (char *)json_string_value(json_array_get(jidps, i)));
368 temp_idp->comm_next = idp;
375 static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
377 TR_RP_REALM *rp = NULL;
378 TR_RP_REALM *temp_rp = NULL;
383 (!json_is_array(jrps))) {
385 *rc = TR_CFG_BAD_PARAMS;
389 for (i = (json_array_size(jrps)-1); i >= 0; i--) {
390 if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
391 fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
396 memset (temp_rp, 0, sizeof(TR_RP_REALM));
398 if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
399 fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
412 static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
413 TR_COMM *comm = NULL;
415 json_t *jtype = NULL;
416 json_t *japcs = NULL;
417 json_t *jidps = NULL;
420 if ((!jcomm) || (!rc)) {
421 fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
423 *rc = TR_CFG_BAD_PARAMS;
427 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
428 fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
433 memset(comm, 0, sizeof(TR_COMM));
435 if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
436 (!json_is_string(jid)) ||
437 (NULL == (jtype = json_object_get(jcomm, "type"))) ||
438 (!json_is_string(jtype)) ||
439 (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
440 (!json_is_array(japcs)) ||
441 (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
442 (!json_is_array(jidps)) ||
443 (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
444 (!json_is_array(jrps))) {
445 fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
447 *rc = TR_CFG_NOPARSE;
451 if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
453 fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
458 if (0 == strcmp(json_string_value(jtype), "apc")) {
459 comm->type = TR_COMM_APC;
460 } else if (0 == strcmp(json_string_value(jtype), "coi")) {
461 comm->type = TR_COMM_COI;
462 if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
463 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
464 tr_free_name(comm->id);
469 fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
470 tr_free_name(comm->id);
472 *rc = TR_CFG_NOPARSE;
476 comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
477 if (TR_CFG_SUCCESS != *rc) {
478 fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
479 tr_free_name(comm->id);
484 comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
485 if (TR_CFG_SUCCESS != *rc) {
486 fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
487 tr_free_name(comm->id);
488 /* TBD -- free idps? */;
496 static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
498 json_t *jcomms = NULL;
499 TR_CFG_RC rc = TR_CFG_SUCCESS;
500 TR_COMM *comm = NULL;
503 if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
504 fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
505 return TR_CFG_BAD_PARAMS;
508 if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
509 fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
512 memset (comm, 0, sizeof(TR_COMM));
514 if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
515 (!json_is_array(jcomms))) {
516 return TR_CFG_NOPARSE;
519 for (i = 0; i < json_array_size(jcomms); i++) {
520 if (NULL == (comm = tr_cfg_parse_one_comm(tr,
521 json_array_get(jcomms, i),
525 fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
526 comm->next = tr->new_cfg->comms;
527 tr->new_cfg->comms = comm;
532 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
534 /* If there is a partial/abandoned config lying around, free it */
536 tr_cfg_free(tr->new_cfg);
539 if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
542 memset(tr->new_cfg, 0, sizeof(TR_CFG));
544 if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
545 (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
546 (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
547 (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
548 tr_cfg_free(tr->new_cfg);
551 return TR_CFG_SUCCESS;
554 TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
557 TR_IDP_REALM *cfg_idp;
559 if ((!tr_cfg) || (!idp_id)) {
561 *rc = TR_CFG_BAD_PARAMS;
565 for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
566 if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
567 fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
571 /* if we didn't find one, return NULL */
575 TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
577 TR_RP_CLIENT *cfg_rp;
580 if ((!tr_cfg) || (!rp_gss)) {
582 *rc = TR_CFG_BAD_PARAMS;
586 for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
587 for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
588 if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
589 fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
594 /* if we didn't find one, return NULL */
598 json_t *tr_read_config (int n, struct dirent **cfg_files) {
607 fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
608 if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
609 fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
616 if (-1 == json_object_update(jcfg, temp)) {
617 fprintf(stderr, "tr_read_config: Error merging config information.\n");
623 // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
628 static int is_cfg_file(const struct dirent *dent) {
631 /* if the last four letters of the filename are .cfg, return true. */
632 if ((4 <= (n = strlen(dent->d_name))) &&
633 (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
637 /* otherwise, return false. */
641 int tr_find_config_files (struct dirent ***cfg_files) {
644 n = scandir(".", cfg_files, &is_cfg_file, 0);
648 fprintf(stderr, "tr_find_config: scandir error.\n");
653 fprintf (stderr, "tr_find_config: No config files found.\n");
659 fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);