-
-
/*
* Copyright (c) 2012, JANET(UK)
* All rights reserved.
*
*/
-#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <jansson.h>
+#include <dirent.h>
+
+#include <tr_config.h>
+#include <tr.h>
+#include <tr_filter.h>
+
+void tr_print_config (FILE *stream, TR_CFG *cfg) {
+ fprintf(stream, "tr_print_config: Not yet implemented.\n");
+ return;
+}
+
+void tr_cfg_free (TR_CFG *cfg) {
+ /* TBD -- need to deallocate memory as part of dynamic config */
+ return;
+}
+
+TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) {
+
+ if (!tr)
+ return TR_CFG_BAD_PARAMS;
+
+ if (tr->active_cfg)
+ tr_cfg_free(tr->active_cfg);
+
+ tr->active_cfg = tr->new_cfg;
+ return TR_CFG_SUCCESS;
+}
+
+static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
+ json_t *jint = NULL;
+ json_t *jmtd = NULL;
+ json_t *jhname = NULL;
+
+ if ((!tr) || (!tr->new_cfg) || (!jcfg))
+ return TR_CFG_BAD_PARAMS;
+
+ if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL))))
+ return TR_CFG_NOMEM;
+
+ memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL));
+
+ if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) {
+ if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) {
+ if (json_is_number(jmtd)) {
+ tr->new_cfg->internal->max_tree_depth = json_integer_value(jmtd);
+ } else {
+ fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n");
+ return TR_CFG_NOPARSE;
+ }
+ } else {
+ /* If not configured, use the default */
+ tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH;
+ }
+ if (NULL != (jhname = json_object_get(jint, "hostname"))) {
+ if (json_is_string(jhname)) {
+ tr->new_cfg->internal->hostname = json_string_value(jhname);
+ } else {
+ fprintf(stderr,"tr_cfg_parse_internal: Parsing error, hostname is not a string.\n");
+ return TR_CFG_NOPARSE;
+ }
+ }
+ else {
+ fprintf(stderr, "tr_cfg_parse_internal: Parsing error, hostname is not found.\n");
+ return TR_CFG_NOPARSE;
+ }
+ fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n");
+ return TR_CFG_SUCCESS;
+ }
+ else {
+ fprintf(stderr, "tr_cfg_parse_internal: Parsing error, tr_internal configuration section not found.\n");
+ return TR_CFG_NOPARSE;
+ }
+}
+
+static TR_FILTER *tr_cfg_parse_one_filter (TR_INSTANCE *tr, json_t *jfilt, TR_CFG_RC *rc)
+{
+ TR_FILTER *filt = NULL;
+ json_t *jftype = NULL;
+ json_t *jfls = NULL;
+ json_t *jfaction = NULL;
+ json_t *jfspecs = NULL;
+ json_t *jffield = NULL;
+ json_t *jfmatch = NULL;
+ int i = 0, j = 0;
+
+ if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
+ (!json_is_string(jftype))) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if ((NULL == (jfls = json_object_get(jfilt, "filter_lines"))) ||
+ (!json_is_array(jfls))) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if (TR_MAX_FILTER_LINES < json_array_size(jfls)) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_lines, maximimum of %d.\n", TR_MAX_FILTER_LINES);
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ /* TBD -- optionally parse realm and domain constraints */
+
+ if (NULL == (filt = malloc(sizeof(TR_FILTER)))) {
+ fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(filt, 0, sizeof(TR_FILTER));
+
+ if (!strcmp(json_string_value(jftype), "rp_permitted")) {
+ filt->type = TR_FILTER_TYPE_RP_PERMITTED;
+ }
+ else {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter type, unknown type '%s'.\n", json_string_value(jftype));
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ /* For each filter line... */
+ for (i = 0; i < json_array_size(jfls); i++) {
+
+ if ((NULL == (jfaction = json_object_get(json_array_get(jfls, i), "action"))) ||
+ (!json_is_string(jfaction))) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action.\n");
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ /* TBD -- parse constraints */
+
+ if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, i), "filter_specs"))) ||
+ (!json_is_array(jfspecs)) ||
+ (0 == json_array_size(jfspecs))) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter specs.\n");
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.\n", TR_MAX_FILTER_SPECS);
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ if (NULL == (filt->lines[i] = malloc(sizeof(TR_FLINE)))) {
+ fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ memset(filt->lines[i], 0, sizeof(TR_FLINE));
+
+ if (!strcmp(json_string_value(jfaction), "accept")) {
+ filt->lines[i]->action = TR_FILTER_ACTION_ACCEPT;
+ }
+ else if (!strcmp(json_string_value(jfaction), "reject")) {
+ filt->lines[i]->action = TR_FILTER_ACTION_REJECT;
+ }
+ else {
+ fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing filter action, unknown action' %s'.\n", json_string_value(jfaction));
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ /*For each filter spec within the filter line... */
+ for (j = 0; j <json_array_size(jfspecs); j++) {
+
+ if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, j), "field"))) ||
+ (!json_is_string(jffield)) ||
+ (NULL == (jfmatch = json_object_get(json_array_get(jfspecs, j), "match"))) ||
+ (!json_is_string(jfmatch))) {
+ fprintf (stderr, "tr_cfg_parse_one_filter: Error parsing filter field and match for filter spec %d, filter line %d.\n", i, j);
+ *rc = TR_CFG_NOPARSE;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ if (NULL == (filt->lines[i]->specs[j] = malloc(sizeof(TR_FSPEC)))) {
+ fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ tr_filter_free(filt);
+ return NULL;
+ }
+
+ memset(filt->lines[i]->specs[j], 0, sizeof(TR_FSPEC));
+
+ if ((NULL == (filt->lines[i]->specs[j]->field = tr_new_name((char *)json_string_value(jffield)))) ||
+ (NULL == (filt->lines[i]->specs[j]->match = tr_new_name((char *)json_string_value(jfmatch))))) {
+ fprintf(stderr, "tr_config_parse_one_filter: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ tr_filter_free(filt);
+ return NULL;
+ }
+ }
+ }
+ return filt;
+}
+
+static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc)
+{
+ TR_RP_CLIENT *rp = NULL;
+ json_t *jgns = NULL;
+ json_t *jfilt = NULL;
+ json_t *jftype = NULL;
+ int i = 0;
+
+ if ((!jrp) || (!rc)) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
-#include <trust_router.h>
+ if ((NULL == (jgns = json_object_get(jrp, "gss_names"))) ||
+ (!json_is_array(jgns))) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no GSS names.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
-int tr_read_config (FILE *cfg_file) {
- int rc = 0;
+ /* TBD -- Support more than one filter per RP client? */
+ if (NULL == (jfilt = json_object_get(jrp, "filter"))) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client configuration, no filter.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+ /* We only support rp_permitted filters for RP clients */
+ if ((NULL == (jftype = json_object_get(jfilt, "type"))) ||
+ (!json_is_string(jftype)) ||
+ (strcmp(json_string_value(jftype), "rp_permitted"))) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter type.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
+ fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(rp, 0, sizeof(TR_RP_CLIENT));
+
+ /* TBD -- support more than one filter entry per RP Client? */
+ if (NULL == (rp->filter = tr_cfg_parse_one_filter(tr, jfilt, rc))) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing filter.\n");
+ free(rp);
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ for (i = 0; i < json_array_size(jgns); i++) {
+ if (NULL == (rp->gss_names[i] = tr_new_name ((char *)json_string_value(json_array_get(jgns, i))))) {
+ fprintf(stderr, "tr_cfg_parse_one_rp_client: No memory for GSS Name.\n");
+ free(rp);
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+ }
+
+ return rp;
+}
+
+static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
+ json_t *jrps = NULL;
+ TR_RP_CLIENT *rp = NULL;
+ TR_CFG_RC rc = TR_CFG_SUCCESS;
+ int i = 0;
+
+ if ((!tr) || (!tr->new_cfg) || (!jcfg))
+ return TR_CFG_BAD_PARAMS;
+
+ if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
+ (!json_is_array(jrps))) {
+ return TR_CFG_NOPARSE;
+ }
+
+ for (i = 0; i < json_array_size(jrps); i++) {
+ if (NULL == (rp = tr_cfg_parse_one_rp_client(tr,
+ json_array_get(jrps, i),
+ &rc))) {
+ return rc;
+ }
+ fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s", rp->gss_names[0]->buf);
+ rp->next = tr->new_cfg->rp_clients;
+ tr->new_cfg->rp_clients = rp;
+ }
return rc;
}
+
+static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_INSTANCE *tr, json_t *jaddr, TR_CFG_RC *rc) {
+ TR_AAA_SERVER *aaa = NULL;
+
+ if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) {
+ fprintf(stderr, "tr_cfg_parse_one_aaa_server: Bad parameters.\n");
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ if (NULL == (aaa = malloc(sizeof(TR_AAA_SERVER)))) {
+ fprintf(stderr, "tr_config_parse_one_aaa_server: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(aaa, 0, sizeof(TR_AAA_SERVER));
+
+ /* TBD -- Handle IPv6 addresses */
+ inet_aton(json_string_value(jaddr), &(aaa->aaa_server_addr));
+
+ return aaa;
+}
+
+
+static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc)
+{
+ TR_AAA_SERVER *aaa = NULL;
+ TR_AAA_SERVER *temp_aaa = NULL;
+ int i = 0;
+
+ for (i = 0; i < json_array_size(jaaas); i++) {
+ if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(tr, json_array_get(jaaas, i), rc))) {
+ return NULL;
+ }
+ /* TBD -- IPv6 addresses */
+ // fprintf(stderr, "tr_cfg_parse_aaa_servers: Configuring AAA Server: ip_addr = %s.\n", inet_ntoa(temp_aaa->aaa_server_addr));
+ temp_aaa->next = aaa;
+ aaa = temp_aaa;
+ }
+ return aaa;
+}
+
+static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
+{
+ TR_APC *apc;
+
+ *rc = TR_CFG_SUCCESS; /* presume success */
+
+ if ((!japcs) || (!rc)) {
+ fprintf(stderr, "tr_cfg_parse_apcs: Bad parameters.\n");
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ if (NULL == (apc = malloc(sizeof(TR_APC)))) {
+ fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(apc, 0, sizeof(TR_APC));
+
+ /* TBD, deal with more than one APC. In the meantime, though... */
+ /* Only parse the first APC, because we only know how to deal with one, anyway. */
+ if (0 == json_array_size(japcs))
+ return NULL;
+
+ if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
+ fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ return apc;
+}
+
+static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
+ TR_IDP_REALM *idp = NULL;
+ json_t *jrid = NULL;
+ json_t *jscfg = NULL;
+ json_t *jsrvrs = NULL;
+ json_t *japcs = NULL;
+
+ if ((!jidp) || (!rc)) {
+ fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ if (NULL == (idp = malloc(sizeof(TR_IDP_REALM)))) {
+ fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(idp, 0, sizeof(TR_IDP_REALM));
+
+ if ((NULL == (jrid = json_object_get(jidp, "realm_id"))) ||
+ (!json_is_string(jrid)) ||
+ (NULL == (jscfg = json_object_get(jidp, "shared_config"))) ||
+ (!json_is_string(jscfg)) ||
+ (NULL == (jsrvrs = json_object_get(jidp, "aaa_servers"))) ||
+ (!json_is_array(jsrvrs))) {
+ fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
+ free(idp);
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if (0 == strcmp(json_string_value(jscfg), "no")) {
+ idp->shared_config = 0;
+ } else {
+ idp->shared_config = 1;
+ }
+
+ if (NULL == (idp->realm_id = tr_new_name((char *)json_string_value(jrid)))) {
+ free(idp);
+ fprintf(stderr, "tr_cfg_parse_one_idp_realm: No memory for realm id.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) {
+ fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse AAA servers for realm %s.\n", idp->realm_id->buf);
+ tr_free_name(idp->realm_id);
+ free(idp);
+ return NULL;
+ }
+
+ if ((NULL != (japcs = json_object_get(jidp, "apcs"))) &&
+ (json_is_array(japcs))) {
+ if (NULL == (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
+ fprintf(stderr, "tr_cfg_parse_one_idp_realm: Can't parse APCs for realm %s .\n", idp->realm_id->buf);
+ tr_free_name(idp->realm_id);
+ /* TBD -- free aaa_servers */;
+ free(idp);
+ return NULL;
+ }
+ }
+ return idp;
+}
+
+static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
+{
+ json_t *jidps = NULL;
+ TR_CFG_RC rc = TR_CFG_SUCCESS;
+ TR_IDP_REALM *idp = NULL;
+ int i = 0;
+
+ if ((!tr) || (!tr->new_cfg) || (!jcfg))
+ return TR_CFG_BAD_PARAMS;
+
+ if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) ||
+ (!json_is_array(jidps))) {
+ return TR_CFG_NOPARSE;
+ }
+
+ for (i = 0; i < json_array_size(jidps); i++) {
+ if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr,
+ json_array_get(jidps, i),
+ &rc))) {
+ return rc;
+ }
+ fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: %s.\n", idp->realm_id->buf);
+ idp->next = tr->new_cfg->idp_realms;
+ tr->new_cfg->idp_realms = idp;
+ }
+ return rc;
+}
+
+static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
+{
+ TR_IDP_REALM *idp = NULL;
+ TR_IDP_REALM *temp_idp = NULL;
+ int i = 0;
+
+ if ((!tr) ||
+ (!jidps) ||
+ (!json_is_array(jidps))) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ for (i = 0; i < json_array_size(jidps); i++) {
+ if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg,
+ tr_new_name((char *)json_string_value(json_array_get(jidps, i))),
+ rc)))) {
+ fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n",
+ (char *)json_string_value(json_array_get(jidps, i)));
+ return NULL;
+ }
+
+ temp_idp->comm_next = idp;
+ idp = temp_idp;
+ }
+
+ return idp;
+}
+
+static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
+{
+ TR_RP_REALM *rp = NULL;
+ TR_RP_REALM *temp_rp = NULL;
+ int i = 0;
+
+ if ((!tr) ||
+ (!jrps) ||
+ (!json_is_array(jrps))) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ for (i = (json_array_size(jrps)-1); i >= 0; i--) {
+ if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
+ fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
+ if (rc)
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+ memset (temp_rp, 0, sizeof(TR_RP_REALM));
+
+ if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
+ fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
+ if (rc)
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ temp_rp->next = rp;
+ rp = temp_rp;
+ }
+
+ return rp;
+}
+
+static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
+ TR_COMM *comm = NULL;
+ json_t *jid = NULL;
+ json_t *jtype = NULL;
+ json_t *japcs = NULL;
+ json_t *jidps = NULL;
+ json_t *jrps = NULL;
+
+ if ((!jcomm) || (!rc)) {
+ fprintf(stderr, "tr_cfg_parse_one_comm: Bad parameters.\n");
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
+ fprintf(stderr, "tr_config_parse_one_comm: Out of memory.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ memset(comm, 0, sizeof(TR_COMM));
+
+ if ((NULL == (jid = json_object_get(jcomm, "community_id"))) ||
+ (!json_is_string(jid)) ||
+ (NULL == (jtype = json_object_get(jcomm, "type"))) ||
+ (!json_is_string(jtype)) ||
+ (NULL == (japcs = json_object_get(jcomm, "apcs"))) ||
+ (!json_is_array(japcs)) ||
+ (NULL == (jidps = json_object_get(jcomm, "idp_realms"))) ||
+ (!json_is_array(jidps)) ||
+ (NULL == (jrps = json_object_get(jcomm, "rp_realms"))) ||
+ (!json_is_array(jrps))) {
+ fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
+ free(comm);
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
+ free(comm);
+ fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
+ *rc = TR_CFG_NOMEM;
+ return NULL;
+ }
+
+ if (0 == strcmp(json_string_value(jtype), "apc")) {
+ comm->type = TR_COMM_APC;
+ } else if (0 == strcmp(json_string_value(jtype), "coi")) {
+ comm->type = TR_COMM_COI;
+ if (NULL == (comm->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) {
+ fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse APCs for COI %s.\n", comm->id->buf);
+ tr_free_name(comm->id);
+ free(comm);
+ return NULL;
+ }
+ } else {
+ fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
+ tr_free_name(comm->id);
+ free(comm);
+ *rc = TR_CFG_NOPARSE;
+ return NULL;
+ }
+
+ comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc);
+ if (TR_CFG_SUCCESS != *rc) {
+ fprintf(stderr, "tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.\n", comm->id->buf);
+ tr_free_name(comm->id);
+ free(comm);
+ return NULL;
+ }
+
+ comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc);
+ if (TR_CFG_SUCCESS != *rc) {
+ fprintf(stderr, "tr_cfg_parse_comm: Can't parse RP realms for comm %s .\n", comm->id->buf);
+ tr_free_name(comm->id);
+ /* TBD -- free idps? */;
+ free(comm);
+ return NULL;
+ }
+
+ return comm;
+}
+
+static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg)
+{
+ json_t *jcomms = NULL;
+ TR_CFG_RC rc = TR_CFG_SUCCESS;
+ TR_COMM *comm = NULL;
+ int i = 0;
+
+ if ((!tr) || (!tr->new_cfg) || (!jcfg)) {
+ fprintf(stderr, "tr_config_parse_comms: Bad Parameters.\n");
+ return TR_CFG_BAD_PARAMS;
+ }
+
+ if (NULL == (comm = malloc(sizeof(TR_COMM)))) {
+ fprintf(stderr, "tr_cfg_parse_comms: Out of Memory\n");
+ return TR_CFG_NOMEM;
+ }
+ memset (comm, 0, sizeof(TR_COMM));
+
+ if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
+ (!json_is_array(jcomms))) {
+ return TR_CFG_NOPARSE;
+ }
+
+ for (i = 0; i < json_array_size(jcomms); i++) {
+ if (NULL == (comm = tr_cfg_parse_one_comm(tr,
+ json_array_get(jcomms, i),
+ &rc))) {
+ return rc;
+ }
+ fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf);
+ comm->next = tr->new_cfg->comms;
+ tr->new_cfg->comms = comm;
+ }
+ return rc;
+}
+
+TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
+
+ /* If there is a partial/abandoned config lying around, free it */
+ if (tr->new_cfg) {
+ tr_cfg_free(tr->new_cfg);
+ }
+
+ if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG))))
+ return TR_CFG_NOMEM;
+
+ memset(tr->new_cfg, 0, sizeof(TR_CFG));
+
+ if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr, jcfg)) ||
+ (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr, jcfg)) ||
+ (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr, jcfg)) ||
+ (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr, jcfg))) {
+ tr_cfg_free(tr->new_cfg);
+ return TR_CFG_ERROR;
+ }
+ return TR_CFG_SUCCESS;
+}
+
+TR_IDP_REALM *tr_cfg_find_idp (TR_CFG *tr_cfg, TR_NAME *idp_id, TR_CFG_RC *rc)
+{
+
+ TR_IDP_REALM *cfg_idp;
+
+ if ((!tr_cfg) || (!idp_id)) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ for (cfg_idp = tr_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
+ if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
+ fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
+ return cfg_idp;
+ }
+ }
+ /* if we didn't find one, return NULL */
+ return NULL;
+}
+
+TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc)
+{
+ TR_RP_CLIENT *cfg_rp;
+ int i;
+
+ if ((!tr_cfg) || (!rp_gss)) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return NULL;
+ }
+
+ for (cfg_rp = tr_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
+ for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
+ if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
+ fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
+ return cfg_rp;
+ }
+ }
+ }
+ /* if we didn't find one, return NULL */
+ return NULL;
+}
+
+json_t *tr_read_config (int n, struct dirent **cfg_files) {
+ json_t *jcfg = NULL;
+ json_t *temp = NULL;
+ json_error_t err;
+
+ if (!cfg_files)
+ return NULL;
+
+ while (n--) {
+ fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name);
+ if (NULL == (temp = json_load_file(cfg_files[n]->d_name, JSON_DISABLE_EOF_CHECK, &err))) {
+ fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", cfg_files[n]->d_name);
+ return NULL;
+ }
+
+ /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */
+ if (!jcfg) {
+ jcfg = temp;
+ }else {
+ if (-1 == json_object_update(jcfg, temp)) {
+ fprintf(stderr, "tr_read_config: Error merging config information.\n");
+ return NULL;
+ }
+ }
+ }
+
+ // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0));
+
+ return jcfg;
+}
+
+static int is_cfg_file(const struct dirent *dent) {
+ int n;
+
+ /* if the last four letters of the filename are .cfg, return true. */
+ if ((4 <= (n = strlen(dent->d_name))) &&
+ (0 == strcmp(&(dent->d_name[n-4]), ".cfg"))) {
+ return 1;
+ }
+
+ /* otherwise, return false. */
+ return 0;
+}
+
+int tr_find_config_files (struct dirent ***cfg_files) {
+ int n = 0, i = 0;
+
+ n = scandir(".", cfg_files, &is_cfg_file, 0);
+
+ if (n < 0) {
+ perror("scandir");
+ fprintf(stderr, "tr_find_config: scandir error.\n");
+ return 0;
+ }
+
+ if (n == 0) {
+ fprintf (stderr, "tr_find_config: No config files found.\n");
+ return 0;
+ }
+
+ i = n;
+ while(i--) {
+ fprintf(stderr, "tr_find_config: Config file found (%s).\n", (*cfg_files)[i]->d_name);
+ }
+
+ return n;
+}