From 8ea4b1d336e83aa48eae5b3445a8381886e7fad2 Mon Sep 17 00:00:00 2001 From: Jennifer Richards Date: Fri, 19 Aug 2016 16:41:10 -0400 Subject: [PATCH] Parse RP clients from new-style configuration files. --- common/cfg_test/cfg_test.c | 61 +++++++++- common/tr_config.c | 272 +++++++++++++++++++++++++++++++++++++-------- common/tr_constraint.c | 2 +- common/tr_rp.c | 79 ++++++++++++- include/tr_rp.h | 8 ++ 5 files changed, 370 insertions(+), 52 deletions(-) diff --git a/common/cfg_test/cfg_test.c b/common/cfg_test/cfg_test.c index 896b8ac..be2085b 100644 --- a/common/cfg_test/cfg_test.c +++ b/common/cfg_test/cfg_test.c @@ -65,6 +65,23 @@ static int verify_idp_cfg(TR_CFG *cfg) return 0; } +static int verify_rp_cfg(TR_CFG *cfg) +{ + int ii=0; + TR_NAME *name=NULL; + + assert(cfg!=NULL); + assert(cfg->rp_clients!=NULL); + assert(cfg->rp_clients->next==NULL); + assert(cfg->rp_clients->comm_next==NULL); + for (ii=1; iirp_clients->gss_names[ii]==NULL); + assert(cfg->rp_clients->gss_names[0]!=NULL); + name=tr_new_name("gss@example.com"); + assert(tr_name_cmp(name, cfg->rp_clients->gss_names[0])==0); + return 0; +} + int main(void) { TALLOC_CTX *mem_ctx=talloc_new(NULL); @@ -96,14 +113,56 @@ int main(void) break; } - printf("Verifying parse results... "); + printf("Verifying IDP parse results... "); if (verify_idp_cfg(cfg)!=0) { printf("Error!\n"); exit(-1); } printf("success!\n"); + printf("Verifying RP parse results... "); + if (verify_rp_cfg(cfg)!=0) { + printf("Error!\n"); + exit(-1); + } + printf("success!\n"); + talloc_report_full(mem_ctx, stderr); tr_cfg_free(cfg); + + printf("Cleared configuration for next test.\n\n"); + + cfg=tr_cfg_new(mem_ctx); + + printf("Parsing rp.cfg.\n"); + rc=tr_cfg_parse_one_config_file(cfg, "rp.cfg"); + switch(rc) { + case TR_CFG_SUCCESS: + tr_debug("main: TR_CFG_SUCCESS"); + break; + case TR_CFG_ERROR: + tr_debug("main: TR_CFG_ERROR"); + break; + case TR_CFG_BAD_PARAMS: + tr_debug("main: TR_CFG_BAD_PARAMS"); + break; + case TR_CFG_NOPARSE: + tr_debug("main: TR_CFG_NOPARSE"); + break; + case TR_CFG_NOMEM: + tr_debug("main: TR_CFG_NOMEM"); + break; + } + +#if 0 + printf("Verifying RP parse results... "); + if (verify_rp_cfg(cfg)!=0) { + printf("Error!\n"); + exit(-1); + } + printf("success!\n"); +#endif + + talloc_free(mem_ctx); return 0; } diff --git a/common/tr_config.c b/common/tr_config.c index cb956c1..bfe8fe8 100644 --- a/common/tr_config.c +++ b/common/tr_config.c @@ -362,7 +362,7 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR goto cleanup; } - if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "filter_specs"))) || + if ((NULL==(jfspecs=json_object_get(json_array_get(jfilt, i), "specs"))) || (!json_is_array(jfspecs)) || (0==json_array_size(jfspecs))) { tr_debug("tr_cfg_parse_one_filter: Error parsing filter specs."); @@ -371,7 +371,7 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR } if (TR_MAX_FILTER_SPECS < json_array_size(jfspecs)) { - tr_debug("tr_cfg_parse_one_filter: Filter has too many filter_specs, maximimum of %d.", TR_MAX_FILTER_SPECS); + tr_debug("tr_cfg_parse_one_filter: Filter has too many specs, maximimum of %d.", TR_MAX_FILTER_SPECS); *rc=TR_CFG_NOPARSE; goto cleanup; } @@ -413,52 +413,51 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR } } } - } - if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) { - if (!json_is_array(jdc)) { - tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array."); - *rc=TR_CFG_NOPARSE; - goto cleanup; - } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) { - tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.", - TR_MAX_CONST_MATCHES); - *rc=TR_CFG_NOPARSE; - goto cleanup; - } else if (json_array_size(jdc)>0) { - if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) { - tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint"); + if (NULL!=(jdc=json_object_get(json_array_get(jfilt, i), "domain_constraints"))) { + if (!json_is_array(jdc)) { + tr_err("tr_cfg_parse_one_filter: cannot parse domain_constraints, not an array."); *rc=TR_CFG_NOPARSE; goto cleanup; + } else if (json_array_size(jdc)>TR_MAX_CONST_MATCHES) { + tr_err("tr_cfg_parse_one_filter: domain_constraints has too many entries, maximum of %d.", + TR_MAX_CONST_MATCHES); + *rc=TR_CFG_NOPARSE; + goto cleanup; + } else if (json_array_size(jdc)>0) { + if (NULL==(filt->lines[i]->domain_cons=tr_cfg_parse_one_constraint(filt->lines[i], "domain", jdc, rc))) { + tr_debug("tr_cfg_parse_one_filter: Error parsing domain constraint"); + *rc=TR_CFG_NOPARSE; + goto cleanup; + } } } - } - /*For each filter spec within the filter line... */ - for (j=0; j lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) { - tr_debug("tr_cfg_parse_one_filter: Out of memory."); - *rc=TR_CFG_NOMEM; - goto cleanup; - } + if (NULL==(filt->lines[i]->specs[j]=tr_fspec_new(filt->lines[i]))) { + tr_debug("tr_cfg_parse_one_filter: Out of memory."); + *rc=TR_CFG_NOMEM; + goto cleanup; + } - if ((NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) || + if ((NULL==(filt->lines[i]->specs[j]->field=tr_new_name(json_string_value(jffield)))) || (NULL==(filt->lines[i]->specs[j]->match=tr_new_name(json_string_value(jfmatch))))) { - tr_debug("tr_cfg_parse_one_filter: Out of memory."); - *rc=TR_CFG_NOMEM; - goto cleanup; + tr_debug("tr_cfg_parse_one_filter: Out of memory."); + *rc=TR_CFG_NOMEM; + goto cleanup; + } } } - *rc=TR_CFG_SUCCESS; talloc_steal(mem_ctx, filt); @@ -467,6 +466,49 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR return filt; } +static TR_FILTER *tr_cfg_parse_filters(TALLOC_CTX *mem_ctx, json_t *jfilts, TR_CFG_RC *rc) +{ + TALLOC_CTX *tmp_ctx=talloc_new(NULL); + json_t *jfilt; + TR_FILTER *filt=NULL; + int ii=0; + + *rc=TR_CFG_ERROR; + + /* no filters */ + if (jfilts==NULL) { + *rc=TR_CFG_SUCCESS; + goto cleanup; + } + + jfilt=json_object_get(jfilts, "tid_inbound"); + if (jfilt!=NULL) { + filt=tr_cfg_parse_one_filter(tmp_ctx, jfilt, TR_FILTER_TYPE_TID_INCOMING, rc); + if (*rc!=TR_CFG_SUCCESS) { + tr_debug("tr_cfg_parse_filters: Error parsing tid_inbound filter."); + *rc=TR_CFG_NOPARSE; + goto cleanup; + } + } else { + tr_debug("tr_cfg_parse_filters: Unknown filter types in filter block."); + *rc=TR_CFG_NOPARSE; + goto cleanup; + } + + *rc=TR_CFG_SUCCESS; + + cleanup: + if (*rc==TR_CFG_SUCCESS) + talloc_steal(mem_ctx, filt); + else { + talloc_free(filt); + filt=NULL; + } + + talloc_free(tmp_ctx); + return filt; +} + static TR_AAA_SERVER *tr_cfg_parse_one_aaa_server(TALLOC_CTX *mem_ctx, json_t *jaddr, TR_CFG_RC *rc) { TR_AAA_SERVER *aaa = NULL; @@ -659,10 +701,6 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm(TALLOC_CTX *mem_ctx, json_t *jre { TALLOC_CTX *tmp_ctx=talloc_new(NULL); TR_IDP_REALM *realm=NULL; - json_t *jremote=NULL; - json_t *jscfg=NULL; - json_t *jsrvrs=NULL; - json_t *japcs=NULL; TR_CFG_RC call_rc=TR_CFG_ERROR; *rc=TR_CFG_ERROR; /* default to error if not set */ @@ -761,6 +799,143 @@ cleanup: return realms; } +static TR_CFG_RC tr_cfg_parse_gss_names(TR_RP_CLIENT *client, json_t *jgss_names) +{ + json_t *jname=NULL; + int ii=0; + TR_NAME *name=NULL; + + if ((client==NULL) || (jgss_names==NULL)) { + tr_err("tr_cfg_parse_gss_names: Bad parameters."); + return TR_CFG_BAD_PARAMS; + } + + if (!json_is_array(jgss_names)) { + tr_err("tr_cfg_parse_gss_names: gss_names not an array."); + return TR_CFG_NOPARSE; + } + + for (ii=0; iiidp_realms); /* make sure the head is in the right context */ trc->comms=tr_cfg_comm_idp_update(trc, trc->comms, new_idp_realms, &rc); /* put realm info in community table */ } -#if 0 - if (new_rp_realms!=NULL) - trc->rp_realms=tr_rp_realm_add(trc->rp_realms, new_rp_realms); /* fixes talloc contexts */ -#endif + + if (new_rp_clients!=NULL) { + trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */ + talloc_steal(trc, trc->rp_clients); /* make sure head is in the right context */ + } } talloc_free(tmp_ctx); diff --git a/common/tr_constraint.c b/common/tr_constraint.c index 2a14947..44a60ba 100644 --- a/common/tr_constraint.c +++ b/common/tr_constraint.c @@ -64,7 +64,7 @@ TR_CONSTRAINT *tr_constraint_new(TALLOC_CTX *mem_ctx) if (cons!=NULL) { cons->type=NULL; - for (ii=0; ii<matches[ii]=NULL; talloc_set_destructor((void *)cons, tr_constraint_destructor); } diff --git a/common/tr_rp.c b/common/tr_rp.c index da11dc1..a132596 100644 --- a/common/tr_rp.c +++ b/common/tr_rp.c @@ -40,7 +40,84 @@ #include #include -TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name) { +static int tr_rp_client_destructor(void *obj) +{ + TR_RP_CLIENT *client=talloc_get_type_abort(obj, TR_RP_CLIENT); + int ii=0; + + for (ii=0; iigss_names[ii]!=NULL) + tr_free_name(client->gss_names[ii]); + } + return 0; +} + +TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx) +{ + TR_RP_CLIENT *client=talloc(mem_ctx, TR_RP_CLIENT); + int ii=0; + + if (client!=NULL) { + client->next=NULL; + client->comm_next=NULL; + for (ii=0; iigss_names[ii]=NULL; + client->filter=NULL; + talloc_set_destructor((void *)client, tr_rp_client_destructor); + } + return client; +} + +void tr_rp_client_free(TR_RP_CLIENT *client) +{ + talloc_free(client); +} + +static TR_RP_CLIENT *tr_rp_client_tail(TR_RP_CLIENT *client) +{ + while (client!=NULL) + client=client->next; + return client; +} + +TR_RP_CLIENT *tr_rp_client_add(TR_RP_CLIENT *clients, TR_RP_CLIENT *new) +{ + if (clients==NULL) + clients=new; + else { + tr_rp_client_tail(clients)->next=new; + while (new!=NULL) { + talloc_steal(clients, new); /* put it in the right context */ + new=new->next; + } + } + return clients; +} + + +int tr_rp_client_add_gss_name(TR_RP_CLIENT *rp_client, TR_NAME *gss_name) +{ + int ii=0; + for (ii=0; iigss_names[ii]) { + rp_client->gss_names[ii]=gss_name; + return 0; /* success */ + } + } + return -1; /* error */ +} + +int tr_rp_client_set_filter(TR_RP_CLIENT *client, TR_FILTER *filt) +{ + if (client->filter!=NULL) + tr_filter_free(client->filter); + client->filter=filt; + talloc_steal(client, filt); + return 0; /* success */ +} + +TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name) +{ TR_RP_CLIENT *rp = NULL; int i = 0; diff --git a/include/tr_rp.h b/include/tr_rp.h index ed74344..c991ff7 100644 --- a/include/tr_rp.h +++ b/include/tr_rp.h @@ -35,6 +35,8 @@ #ifndef TR_RP_H #define TR_RP_H +#include + #include #define TR_MAX_GSS_NAMES 5 @@ -53,6 +55,12 @@ typedef struct tr_rp_realm { } TR_RP_REALM; /* prototypes */ +TR_RP_CLIENT *tr_rp_client_new(TALLOC_CTX *mem_ctx); +void tr_rp_client_free(TR_RP_CLIENT *client); +TR_RP_CLIENT *tr_rp_client_add(TR_RP_CLIENT *clients, TR_RP_CLIENT *new); +int tr_rp_client_add_gss_name(TR_RP_CLIENT *client, TR_NAME *name); +int tr_rp_client_set_filter(TR_RP_CLIENT *client, TR_FILTER *filt); + TR_RP_CLIENT *tr_rp_client_lookup(TR_RP_CLIENT *rp_clients, TR_NAME *gss_name); TR_RP_REALM *tr_rp_realm_add(TR_RP_REALM *head, TR_RP_REALM *new); #endif -- 2.1.4