X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=common%2Ftr_idp.c;h=50a9330d534669b29a377189022b00034896b0f3;hb=3feea37388d58efac91a7e10b85043bb52c8bf19;hp=0c2a0c6e6d8442c60781a0a2d0dfe23556bb42fc;hpb=f4fa9a7584189324cff981ccf965802b4c69ddda;p=trust_router.git diff --git a/common/tr_idp.c b/common/tr_idp.c index 0c2a0c6..50a9330 100644 --- a/common/tr_idp.c +++ b/common/tr_idp.c @@ -35,9 +35,10 @@ #include #include -#include +#include #include #include +#include static int tr_aaa_server_destructor(void *obj) { @@ -51,6 +52,7 @@ TR_AAA_SERVER *tr_aaa_server_new(TALLOC_CTX *mem_ctx, TR_NAME *hostname) { TR_AAA_SERVER *aaa=talloc(mem_ctx, TR_AAA_SERVER); if (aaa!=NULL) { + aaa->next=NULL; aaa->hostname=hostname; talloc_set_destructor((void *)aaa, tr_aaa_server_destructor); } @@ -62,7 +64,33 @@ void tr_aaa_server_free(TR_AAA_SERVER *aaa) talloc_free(aaa); } -TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm) +TR_AAA_SERVER_ITER *tr_aaa_server_iter_new(TALLOC_CTX *mem_ctx) +{ + return talloc(mem_ctx, TR_AAA_SERVER_ITER); +} + +void tr_aaa_server_iter_free(TR_AAA_SERVER_ITER *iter) +{ + talloc_free(iter); +} + +TR_AAA_SERVER *tr_aaa_server_iter_first(TR_AAA_SERVER_ITER *iter, TR_AAA_SERVER *aaa) +{ + iter->this=aaa; + return iter->this; +} + +TR_AAA_SERVER *tr_aaa_server_iter_next(TR_AAA_SERVER_ITER *iter) +{ + if (iter->this!=NULL) { + iter->this=iter->this->next; + } + return iter->this; +} + + +/* fills in shared if pointer not null */ +TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_realm_name, TR_NAME *comm, int *shared_out) { TR_IDP_REALM *idp = NULL; @@ -72,9 +100,11 @@ TR_AAA_SERVER *tr_idp_aaa_server_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_r break; } } - if (idp) + if (idp) { + if (shared_out!=NULL) + *shared_out=idp->shared_config; return idp->aaa_servers; - else + } else return NULL; } @@ -109,12 +139,7 @@ TR_IDP_REALM *tr_idp_realm_new(TALLOC_CTX *mem_ctx) idp->aaa_servers=NULL; idp->apcs=NULL; idp->origin=TR_REALM_LOCAL; - idp->peer=NULL; - idp->expiry=talloc(idp, struct timespec); - if (idp->expiry==NULL) { - talloc_free(idp); - return NULL; - } + idp->refcount=0; talloc_set_destructor((void *)idp, tr_idp_realm_destructor); } return idp; @@ -148,6 +173,36 @@ void tr_idp_realm_set_id(TR_IDP_REALM *idp, TR_NAME *id) idp->realm_id=id; } +void tr_idp_realm_set_apcs(TR_IDP_REALM *idp, TR_APC *apc) +{ + if (idp->apcs!=NULL) + tr_apc_free(idp->apcs); + idp->apcs=apc; + talloc_steal(idp, apc); +} + +TR_APC *tr_idp_realm_get_apcs(TR_IDP_REALM *idp) +{ + return idp->apcs; +} + +TR_IDP_REALM *tr_idp_realm_lookup(TR_IDP_REALM *idp_realms, TR_NAME *idp_name) +{ + TR_IDP_REALM *idp = NULL; + + if (!idp_name) { + tr_debug("tr_idp_realm_lookup: Bad parameters."); + return NULL; + } + + for (idp=idp_realms; NULL!=idp; idp=idp->next) { + if (0==tr_name_cmp(tr_idp_realm_get_id(idp), idp_name)) + return idp; + } + return NULL; +} + + static TR_IDP_REALM *tr_idp_realm_tail(TR_IDP_REALM *idp) { if (idp==NULL) @@ -173,7 +228,38 @@ TR_IDP_REALM *tr_idp_realm_add_func(TR_IDP_REALM *head, TR_IDP_REALM *new) return head; } -static int tr_idp_realm_apc_count(TR_IDP_REALM *idp) +/* use the macro */ +TR_IDP_REALM *tr_idp_realm_remove_func(TR_IDP_REALM *head, TR_IDP_REALM *remove) +{ + TALLOC_CTX *list_ctx=talloc_parent(head); + TR_IDP_REALM *this=NULL; + + if (head==NULL) + return NULL; + + if (head==remove) { + /* if we're removing the head, put the next element (if present) into the context + * the list head was in. */ + head=head->next; + if (head!=NULL) { + talloc_steal(list_ctx, head); + /* now put all the other elements in the context of the list head */ + for (this=head->next; this!=NULL; this=this->next) + talloc_steal(head, this); + } + } else { + /* not removing the head; no need to play with contexts */ + for (this=head; this->next!=NULL; this=this->next) { + if (this->next==remove) { + this->next=remove->next; + break; + } + } + } + return head; +} + +int tr_idp_realm_apc_count(TR_IDP_REALM *idp) { int ii=0; TR_APC *apc=idp->apcs; @@ -184,7 +270,7 @@ static int tr_idp_realm_apc_count(TR_IDP_REALM *idp) return ii; } -static int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp) +int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp) { int ii=0; TR_AAA_SERVER *aaa=idp->aaa_servers; @@ -195,91 +281,45 @@ static int tr_idp_realm_aaa_server_count(TR_IDP_REALM *idp) return ii; } -static char *tr_aaa_server_to_str(TALLOC_CTX *mem_ctx, TR_AAA_SERVER *aaa) +void tr_idp_realm_incref(TR_IDP_REALM *realm) { - return talloc_strndup(mem_ctx, aaa->hostname->buf, aaa->hostname->len); + realm->refcount++; } -char *tr_idp_realm_to_str(TALLOC_CTX *mem_ctx, TR_IDP_REALM *idp) +void tr_idp_realm_decref(TR_IDP_REALM *realm) { - TALLOC_CTX *tmp_ctx=talloc_new(NULL); - char **s_aaa=NULL, *aaa_servers=NULL; - char **s_apc=NULL, *apcs=NULL; - int ii=0, aaa_servers_strlen=0, apcs_strlen=0; - int n_aaa_servers=tr_idp_realm_aaa_server_count(idp); - int n_apcs=tr_idp_realm_apc_count(idp); - TR_AAA_SERVER *aaa=NULL; - TR_APC *apc=NULL; - char *result=NULL; - - /* get the AAA servers */ - if (n_aaa_servers<=0) - aaa_servers=talloc_strdup(tmp_ctx, ""); - else { - s_aaa=talloc_array(tmp_ctx, char *, n_aaa_servers); - for (aaa=idp->aaa_servers,ii=0; aaa!=NULL; aaa=aaa->next,ii++) { - s_aaa[ii]=tr_aaa_server_to_str(s_aaa, aaa); - aaa_servers_strlen+=strlen(s_aaa[ii]); - } + if (realm->refcount>0) + realm->refcount--; +} - /* add space for comma-space separators */ - aaa_servers_strlen+=2*(n_aaa_servers-1); +/* remove any with zero refcount + * Call via macro. */ +TR_IDP_REALM *tr_idp_realm_sweep_func(TR_IDP_REALM *head) +{ + TR_IDP_REALM *idp=NULL; + TR_IDP_REALM *old_next=NULL; - aaa_servers=talloc_array(tmp_ctx, char, aaa_servers_strlen+1); - aaa_servers[0]='\0'; - for (ii=0; iiapcs,ii=0; apc!=NULL; apc=apc->next,ii++) { - s_apc[ii]=tr_apc_to_str(s_apc, apc); - apcs_strlen+=strlen(s_apc[ii]); - } + while ((head!=NULL) && (head->refcount==0)) { + idp=head; /* keep a pointer so we can remove it */ + tr_idp_realm_remove(head, idp); /* use this to get talloc contexts right */ + tr_idp_realm_free(idp); + } - /* add space for comma-space separators */ - apcs_strlen+=2*(n_apcs-1); + if (head==NULL) + return NULL; - apcs=talloc_array(tmp_ctx, char, apcs_strlen+1); - apcs[0]='\0'; - for (ii=0; iinext!=NULL); idp=idp->next) { + if (idp->next->refcount==0) { + old_next=idp->next; + tr_idp_realm_remove(head, idp->next); /* changes idp->next, may make it NULL */ + tr_idp_realm_free(old_next); } - talloc_free(s_apc); } - result=talloc_asprintf(mem_ctx, - "IDP realm: \"%.*s\"\n" - " shared: %s\n" - " local: %s\n" - " AAA servers: %s\n" - " APCs: %s\n", - idp->realm_id->len, idp->realm_id->buf, - (idp->shared_config)?"yes":"no", - (idp->origin==TR_REALM_LOCAL)?"yes":"no", - aaa_servers, - apcs); - talloc_free(tmp_ctx); - return result; -} - -void tr_idp_realm_incref(TR_IDP_REALM *realm) -{ - realm->refcount++; + return head; } -void tr_idp_realm_decref(TR_IDP_REALM *realm) -{ - if (realm->refcount>0) - realm->refcount--; -}