X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=common%2Ftr_config.c;h=ff92fd011391315966ead3a8ec2eee72aa377b17;hb=9159c30c092c747c957b69a6b69a348501bb6b2a;hp=7972cf8ad6138a30062e3ea7b9c0a5bbe4c938fd;hpb=903af103d029aab23f359cc662b7b3810ccc1ca1;p=trust_router.git diff --git a/common/tr_config.c b/common/tr_config.c index 7972cf8..ff92fd0 100644 --- a/common/tr_config.c +++ b/common/tr_config.c @@ -36,9 +36,12 @@ #include #include #include +#include #include #include +#include +#include void tr_print_config (FILE *stream, TR_CFG *cfg) { fprintf(stream, "tr_print_config: Not yet implemented.\n"); @@ -46,12 +49,11 @@ void tr_print_config (FILE *stream, TR_CFG *cfg) { } void tr_cfg_free (TR_CFG *cfg) { - /* TBD -- need to deallocate memory as part of dynamic config */ + talloc_free(cfg); return; } TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) { - if (!tr) return TR_CFG_BAD_PARAMS; @@ -62,120 +64,288 @@ TR_CFG_RC tr_apply_new_config (TR_INSTANCE *tr) { return TR_CFG_SUCCESS; } -static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) { +static TR_CFG_RC tr_cfg_parse_internal (TR_CFG *trc, json_t *jcfg) { json_t *jint = NULL; json_t *jmtd = NULL; + json_t *jtp = NULL; + json_t *jhname = NULL; - if ((!tr) || (!tr->new_cfg) || (!jcfg)) + if ((!trc) || (!jcfg)) return TR_CFG_BAD_PARAMS; - if (NULL == (tr->new_cfg->internal = malloc(sizeof(TR_CFG_INTERNAL)))) - return TR_CFG_NOMEM; + if (NULL == trc->internal) { + if (NULL == (trc->internal = talloc(trc, TR_CFG_INTERNAL))) + return TR_CFG_NOMEM; - memset(tr->new_cfg->internal, 0, sizeof(TR_CFG_INTERNAL)); + memset(trc->internal, 0, sizeof(TR_CFG_INTERNAL)); + } - if ((NULL != (jint = json_object_get(jcfg, "tr_internal"))) && - (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); + if (NULL != (jint = json_object_get(jcfg, "tr_internal"))) { + if (NULL != (jmtd = json_object_get(jint, "max_tree_depth"))) { + if (json_is_number(jmtd)) { + trc->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 { - fprintf(stderr,"tr_cfg_parse_internal: Parsing error, max_tree_depth is not a number.\n"); - return TR_CFG_NOPARSE; + /* If not configured, use the default */ + trc->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH; } - } else { - /* If not configured, use the default */ - tr->new_cfg->internal->max_tree_depth = TR_DEFAULT_MAX_TREE_DEPTH; + if (NULL != (jtp = json_object_get(jint, "tids_port"))) { + if (json_is_number(jtp)) { + trc->internal->tids_port = json_integer_value(jtp); + } else { + fprintf(stderr,"tr_cfg_parse_internal: Parsing error, port is not a number.\n"); + return TR_CFG_NOPARSE; + } + } else { + /* If not configured, use the default */ + trc->internal->tids_port = TR_DEFAULT_TIDS_PORT; + } + if (NULL != (jhname = json_object_get(jint, "hostname"))) { + if (json_is_string(jhname)) { + trc->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; + } + } + fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n"); + return TR_CFG_SUCCESS; } - fprintf(stderr, "tr_cfg_parse_internal: Internal config parsed.\n"); return TR_CFG_SUCCESS; } -static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc) +static TR_CONSTRAINT *tr_cfg_parse_one_constraint (TR_CFG *trc, char *ctype, json_t *jc, TR_CFG_RC *rc) { - TR_RP_CLIENT *rp = NULL; - json_t *jgns = NULL; - json_t *jfilt = NULL; - json_t *jfls = NULL; - json_t *jftype = NULL; - json_t *jfact = NULL; - json_t *jfspecs = NULL; - json_t *jffield = NULL; - json_t *jfrealm = NULL; - int i = 0; + TR_CONSTRAINT *cons; + int i; - if ((!jrp) || (!rc)) { - fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n"); - if (rc) - *rc = TR_CFG_BAD_PARAMS; + if ((!trc) || (!ctype) || (!jc) || (!rc) || + (!json_is_array(jc)) || + (0 >= json_array_size(jc)) || + (TR_MAX_CONST_MATCHES < json_array_size(jc)) || + (!json_is_string(json_array_get(jc, 0)))) { + fprintf(stderr, "tr_cfg_parse_one_constraint: config error.\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"); + if (NULL == (cons = talloc(trc, TR_CONSTRAINT))) { + fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (cons).\n"); *rc = TR_CFG_NOMEM; return NULL; } - - memset(rp, 0, sizeof(TR_RP_CLIENT)); - if ((NULL == (jfilt = json_object_get(jrp, "filter"))) || - (NULL == (jfls = json_object_get(jfilt, "filter_lines"))) || - (!json_is_array(jfls)) || - (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.\n"); - free(rp); + memset(cons, 0, sizeof(TR_CONSTRAINT)); + + if (NULL == (cons->type = tr_new_name(ctype))) { + fprintf(stderr, "tr_cfg_parse_one_constraint: Out of memory (type).\n"); + *rc = TR_CFG_NOMEM; + return NULL; + } + + for (i = 0; i < json_array_size(jc); i++) { + cons->matches[i] = tr_new_name((char *)(json_string_value(json_array_get(jc, i)))); + } + + return cons; +} + +static TR_FILTER *tr_cfg_parse_one_filter (TR_CFG *trc, 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; + json_t *jrc = NULL; + json_t *jdc = 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 (0 == json_array_size(jfls)) { - fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has no filter lines.\n"); + 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 ((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"); + 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; } + if (NULL == (filt = talloc(trc, TR_FILTER))) { + fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory.\n"); + *rc = TR_CFG_NOMEM; + return NULL; + } + + memset(filt, 0, sizeof(TR_FILTER)); - /* Right now, we only accept one type of filter, and we only care - * about one per rp_client. */ - if ((NULL == (jfact = json_object_get(json_array_get(jfls, 0), "action"))) || - (!json_is_string(jfact)) || - (strcmp(json_string_value(jfact), "accept"))) { - fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter action.\n"); + 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; - } + } - if ((NULL == (jfspecs = json_object_get(json_array_get(jfls, 0), "filter_specs"))) || - (!json_is_array(jfspecs)) || - (0 == json_array_size(jfspecs))) { - fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter specs.\n"); + /* 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; - } + } + + 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] = talloc(trc, TR_FLINE))) { + fprintf(stderr, "tr_cfg_parse_one_filter: Out of memory (fline).\n"); + *rc = TR_CFG_NOMEM; + tr_filter_free(filt); + return NULL; + } + + memset(filt->lines[i], 0, sizeof(TR_FLINE)); - if ((NULL == (jffield = json_object_get(json_array_get(jfspecs, 0), "field"))) || - (!json_is_string(jffield)) || - (strcmp(json_string_value(jffield), "rp_realm")) || - (NULL == (jfrealm = json_object_get(json_array_get(jfspecs, 0), "match"))) || - (!json_is_string(jfrealm))) { - fprintf(stderr, "tr_cfg_parse_one_rp_client: Error parsing RP client filter field and match.\n"); + 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; + } + + if ((NULL != (jrc = json_object_get(json_array_get(jfls, i), "realm_constraints"))) && + (json_is_array(jrc)) && + (0 != json_array_size(jrc)) && + (TR_MAX_CONST_MATCHES >= json_array_size(jrc))) { + + if (NULL == (filt->lines[i]->realm_cons = tr_cfg_parse_one_constraint(trc, "realm", jrc, rc))) { + fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing realm constraint"); + tr_filter_free(filt); + return NULL; + } + } + + if ((NULL != (jdc = json_object_get(json_array_get(jfls, i), "domain_constraints"))) && + (json_is_array(jdc)) && + (0 != json_array_size(jdc)) && + (TR_MAX_CONST_MATCHES >= json_array_size(jdc))) { + + if (NULL == (filt->lines[i]->domain_cons = tr_cfg_parse_one_constraint(trc, "domain", jdc, rc))) { + fprintf(stderr, "tr_cfg_parse_one_filter: Error parsing domain constraint"); + tr_filter_free(filt); return NULL; + } + } + + /*For each filter spec within the filter line... */ + for (j = 0; j lines[i]->specs[j] = talloc(trc, TR_FSPEC))) { + fprintf(stderr, "tr_cfg_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_cfg_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_CFG *trc, 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 ((!trc) || (!jrp) || (!rc)) { + fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n"); + if (rc) + *rc = TR_CFG_BAD_PARAMS; + return NULL; } - rp->rp_match = tr_new_name((char *)json_string_value(jfrealm)); + 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; + } - if (0 == json_array_size(jgns)) { - fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has no GSS Names.\n"); + /* 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; } @@ -186,9 +356,26 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, T return NULL; } + if (NULL == (rp = talloc(trc, TR_RP_CLIENT))) { + fprintf(stderr, "tr_cfg_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(trc, 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; } @@ -197,65 +384,65 @@ static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, T return rp; } -static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) { +static TR_CFG_RC tr_cfg_parse_rp_clients (TR_CFG *trc, 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)) + if ((!trc) || (!jcfg)) return TR_CFG_BAD_PARAMS; - if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) || - (!json_is_array(jrps))) { - return TR_CFG_NOPARSE; - } + if (NULL != (jrps = json_object_get(jcfg, "rp_clients"))) { + + if (!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; + for (i = 0; i < json_array_size(jrps); i++) { + if (NULL == (rp = tr_cfg_parse_one_rp_client(trc, + 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 = trc->rp_clients; + trc->rp_clients = rp; } - fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured -- first gss: %s, rp_realm: %s\n", rp->gss_names[0]->buf, rp->rp_match->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) { +static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server (TR_CFG *trc, json_t *jaddr, TR_CFG_RC *rc) { TR_AAA_SERVER *aaa = NULL; - if ((!tr) || (!tr->new_cfg) || (!jaddr) || (!json_is_string(jaddr))) { + if ((!trc) || (!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"); + if (NULL == (aaa = talloc(trc, TR_AAA_SERVER))) { + fprintf(stderr, "tr_cfg_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)); + aaa->hostname = tr_new_name((char *)(json_string_value(jaddr))); return aaa; } - -static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, TR_CFG_RC *rc) +static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_CFG *trc, 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))) { + if (NULL == (temp_aaa = tr_cfg_parse_one_aaa_server(trc, json_array_get(jaaas, i), rc))) { return NULL; } /* TBD -- IPv6 addresses */ @@ -266,20 +453,20 @@ static TR_AAA_SERVER *tr_cfg_parse_aaa_servers (TR_INSTANCE *tr, json_t *jaaas, return aaa; } -static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc) +static TR_APC *tr_cfg_parse_apcs (TR_CFG *trc, json_t *japcs, TR_CFG_RC *rc) { TR_APC *apc; *rc = TR_CFG_SUCCESS; /* presume success */ - if ((!japcs) || (!rc)) { + if ((!trc) || (!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)))) { + if (NULL == (apc = talloc(trc, TR_APC))) { fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n"); *rc = TR_CFG_NOMEM; return NULL; @@ -301,22 +488,22 @@ static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc) return apc; } -static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) { +static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_CFG *trc, 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)) { + if ((!trc) || (!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"); + if (NULL == (idp = talloc(trc, TR_IDP_REALM))) { + fprintf(stderr, "tr_cfg_parse_one_idp_realm: Out of memory.\n"); *rc = TR_CFG_NOMEM; return NULL; } @@ -348,7 +535,7 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, return NULL; } - if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(tr, jsrvrs, rc))) { + if (NULL == (idp->aaa_servers = tr_cfg_parse_aaa_servers(trc, 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); @@ -357,7 +544,7 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, if ((NULL != (japcs = json_object_get(jidp, "apcs"))) && (json_is_array(japcs))) { - if (NULL != (idp->apcs = tr_cfg_parse_apcs(tr, japcs, rc))) { + if (NULL == (idp->apcs = tr_cfg_parse_apcs(trc, 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 */; @@ -368,41 +555,71 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, return idp; } -static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg) +static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg) { - json_t *jidps = NULL; + json_t *jdss = NULL; TR_CFG_RC rc = TR_CFG_SUCCESS; - TR_IDP_REALM *idp = NULL; + TR_AAA_SERVER *ds = NULL; int i = 0; - if ((!tr) || (!tr->new_cfg) || (!jcfg)) + if ((!trc) || (!jcfg)) return TR_CFG_BAD_PARAMS; - if ((NULL == (jidps = json_object_get(jcfg, "idp_realms"))) || - (!json_is_array(jidps))) { - return TR_CFG_NOPARSE; - } + /* If there are default servers, store them */ + if ((NULL != (jdss = json_object_get(jcfg, "default_servers"))) && + (json_is_array(jdss)) && + (0 < json_array_size(jdss))) { - for (i = 0; i < json_array_size(jidps); i++) { - if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, - json_array_get(jidps, i), + for (i = 0; i < json_array_size(jdss); i++) { + if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc, + json_array_get(jdss, i), &rc))) { - return rc; + return rc; + } + fprintf(stderr, "tr_cfg_parse_default_servers: Default server configured.\n"); + ds->next = trc->default_servers; + trc->default_servers = ds; + } + } + + return rc; +} + +static TR_CFG_RC tr_cfg_parse_idp_realms (TR_CFG *trc, json_t *jcfg) +{ + json_t *jidps = NULL; + TR_CFG_RC rc = TR_CFG_SUCCESS; + TR_IDP_REALM *idp = NULL; + int i = 0; + + if ((!trc) || (!jcfg)) + return TR_CFG_BAD_PARAMS; + + /* If there are any IDP Realms, parse them */ + if ((NULL != (jidps = json_object_get(jcfg, "idp_realms"))) && + (json_is_array(jidps))) { + for (i = 0; i < json_array_size(jidps); i++) { + if (NULL == (idp = tr_cfg_parse_one_idp_realm(trc, + 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 = trc->idp_realms; + trc->idp_realms = idp; } - 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) +static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_CFG *trc, json_t *jidps, TR_CFG_RC *rc) { TR_IDP_REALM *idp = NULL; TR_IDP_REALM *temp_idp = NULL; int i = 0; - if ((!tr) || + if ((!trc) || (!jidps) || (!json_is_array(jidps))) { if (rc) @@ -411,7 +628,7 @@ static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_ } for (i = 0; i < json_array_size(jidps); i++) { - if (NULL == (temp_idp = (tr_cfg_find_idp(tr->new_cfg, + if (NULL == (temp_idp = (tr_cfg_find_idp(trc, tr_new_name((char *)json_string_value(json_array_get(jidps, i))), rc)))) { fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", @@ -426,13 +643,13 @@ static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_ return idp; } -static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc) +static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC *rc) { TR_RP_REALM *rp = NULL; TR_RP_REALM *temp_rp = NULL; int i = 0; - if ((!tr) || + if ((!trc) || (!jrps) || (!json_is_array(jrps))) { if (rc) @@ -441,7 +658,7 @@ static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG } for (i = (json_array_size(jrps)-1); i >= 0; i--) { - if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) { + if (NULL == (temp_rp = talloc(trc, TR_RP_REALM))) { fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n"); if (rc) *rc = TR_CFG_NOMEM; @@ -463,7 +680,7 @@ static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG return rp; } -static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) { +static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) { TR_COMM *comm = NULL; json_t *jid = NULL; json_t *jtype = NULL; @@ -471,15 +688,15 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC json_t *jidps = NULL; json_t *jrps = NULL; - if ((!jcomm) || (!rc)) { + if ((!trc) || (!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"); + if (NULL == (comm = talloc(trc, TR_COMM))) { + fprintf(stderr, "tr_cfg_parse_one_comm: Out of memory.\n"); *rc = TR_CFG_NOMEM; return NULL; } @@ -513,7 +730,7 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC 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))) { + if (NULL == (comm->apcs = tr_cfg_parse_apcs(trc, 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); @@ -527,7 +744,7 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC return NULL; } - comm->idp_realms = tr_cfg_parse_comm_idps(tr, jidps, rc); + comm->idp_realms = tr_cfg_parse_comm_idps(trc, 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); @@ -535,7 +752,7 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC return NULL; } - comm->rp_realms = tr_cfg_parse_comm_rps(tr, jrps, rc); + comm->rp_realms = tr_cfg_parse_comm_rps(trc, 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); @@ -547,61 +764,110 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC return comm; } -static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) +static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, 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"); + if ((!trc) || (!jcfg)) { + fprintf(stderr, "tr_cfg_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; + if (NULL != (jcomms = json_object_get(jcfg, "communities"))) { + if (!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(trc, + json_array_get(jcomms, i), + &rc))) { + return rc; + } + fprintf(stderr, "tr_cfg_parse_comms: Community configured: %s.\n", comm->id->buf); + comm->next = trc->comms; + trc->comms = comm; + } + } + return rc; +} + +TR_CFG_RC tr_cfg_validate (TR_CFG *trc) { + TR_CFG_RC rc = TR_CFG_SUCCESS; + + if (!trc) + return TR_CFG_BAD_PARAMS; + + if ((NULL == trc->internal)|| + (NULL == trc->internal->hostname)) { + fprintf(stderr, "tr_cfg_validate: Error: No internal configuration, or no hostname.\n"); + rc = TR_CFG_ERROR; } - memset (comm, 0, sizeof(TR_COMM)); - if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) || - (!json_is_array(jcomms))) { - return TR_CFG_NOPARSE; + if (NULL == trc->rp_clients) { + fprintf(stderr, "tr_cfg_validate: Error: No RP Clients configured\n"); + rc = TR_CFG_ERROR; } - 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; + if (NULL == trc->comms) { + fprintf(stderr, "tr_cfg_validate: Error: No Communities configured\n"); + rc = TR_CFG_ERROR; + } + + if ((NULL == trc->default_servers) && (NULL == trc->idp_realms)) { + fprintf(stderr, "tr_cfg_validate: Error: No default servers or IDPs configured.\n"); + rc = TR_CFG_ERROR; } + return rc; } -TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) { +TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, struct dirent **cfg_files) { + json_t *jcfg; + json_error_t rc; + int n; + + if ((!tr) || (!cfg_files)) + return TR_CFG_BAD_PARAMS; /* If there is a partial/abandoned config lying around, free it */ - if (tr->new_cfg) { + if (tr->new_cfg) tr_cfg_free(tr->new_cfg); - } - if (NULL == (tr->new_cfg = malloc(sizeof(TR_CFG)))) + if (NULL == (tr->new_cfg = talloc(NULL, 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); + /* Parse configuration information from each config file */ + while (n--) { + fprintf(stderr, "tr_read_config: Parsing %s.\n", cfg_files[n]->d_name); + if (NULL == (jcfg = json_load_file(cfg_files[n]->d_name, + JSON_DISABLE_EOF_CHECK, &rc))) { + fprintf (stderr, "tr_read_config: Error parsing config file %s.\n", + cfg_files[n]->d_name); + return TR_CFG_NOPARSE; + } + + if ((TR_CFG_SUCCESS != tr_cfg_parse_internal(tr->new_cfg, jcfg)) || + (TR_CFG_SUCCESS != tr_cfg_parse_rp_clients(tr->new_cfg, jcfg)) || + (TR_CFG_SUCCESS != tr_cfg_parse_idp_realms(tr->new_cfg, jcfg)) || + (TR_CFG_SUCCESS != tr_cfg_parse_default_servers(tr->new_cfg, jcfg)) || + (TR_CFG_SUCCESS != tr_cfg_parse_comms(tr->new_cfg, jcfg))) { + tr_cfg_free(tr->new_cfg); + return TR_CFG_ERROR; + } + } + + /* make sure we got a complete, consistent configuration */ + if (TR_CFG_SUCCESS != tr_cfg_validate(tr->new_cfg)) { + fprintf(stderr, "tr_parse_config: Error: INVALID CONFIGURATION, EXITING\n"); return TR_CFG_ERROR; } + return TR_CFG_SUCCESS; } @@ -649,6 +915,7 @@ TR_RP_CLIENT *tr_cfg_find_rp (TR_CFG *tr_cfg, TR_NAME *rp_gss, TR_CFG_RC *rc) return NULL; } +#if 0 json_t *tr_read_config (int n, struct dirent **cfg_files) { json_t *jcfg = NULL; json_t *temp = NULL; @@ -664,7 +931,6 @@ json_t *tr_read_config (int n, struct dirent **cfg_files) { return NULL; } - /* TBD -- instead of using json_object_update, iterate through files for non-overlap config? */ if (!jcfg) { jcfg = temp; }else { @@ -675,10 +941,11 @@ json_t *tr_read_config (int n, struct dirent **cfg_files) { } } - // fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0)); + fprintf(stderr, "tr_read_config: Merged configuration complete:\n%s\n", json_dumps(jcfg, 0)); return jcfg; } +#endif static int is_cfg_file(const struct dirent *dent) { int n;