+ filt=tr_filter_new(tmp_ctx);
+ if (filt==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate filter.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ tr_filter_set_type(filt, TR_FILTER_TYPE_TID_INBOUND);
+ filt->lines[0]=tr_fline_new(filt);
+ if (filt->lines[0]==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate filter line.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ filt->lines[0]->action=TR_FILTER_ACTION_ACCEPT;
+ filt->lines[0]->specs[0]=tr_fspec_new(filt->lines[0]);
+ filt->lines[0]->specs[0]->field=n_rp_realm_1;
+ n_rp_realm_1=NULL; /* we don't own this name any more */
+
+ name=tr_dup_name(realm);
+ if (name==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate realm name.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ tr_fspec_add_match(filt->lines[0]->specs[0], name);
+ name=NULL; /* we no longer own the name */
+
+ /* now do the wildcard name */
+ filt->lines[0]->specs[1]=tr_fspec_new(filt->lines[0]);
+ filt->lines[0]->specs[1]->field=n_rp_realm_2;
+ n_rp_realm_2=NULL; /* we don't own this name any more */
+
+ if (NULL==(name=tr_name_cat(n_prefix, realm))) {
+ tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ tr_fspec_add_match(filt->lines[0]->specs[1], name);
+ name=NULL; /* we no longer own the name */
+
+ /* domain constraint */
+ if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
+ tr_debug("tr_cfg_default_filters: could not allocate domain constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ cons->type=n_domain;
+ n_domain=NULL; /* belongs to the constraint now */
+ name=tr_dup_name(realm);
+ if (name==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate realm name for domain constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ cons->matches[0]=name;
+ name=tr_name_cat(n_prefix, realm);
+ if (name==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for domain constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ cons->matches[1]=name;
+ name=NULL;
+ filt->lines[0]->domain_cons=cons;
+
+
+ /* realm constraint */
+ if (NULL==(cons=tr_constraint_new(filt->lines[0]))) {
+ tr_debug("tr_cfg_default_filters: could not allocate realm constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ cons->type=n_realm;
+ n_realm=NULL; /* belongs to the constraint now */
+ name=tr_dup_name(realm);
+ if (name==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate realm name for realm constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ cons->matches[0]=name;
+ name=tr_name_cat(n_prefix, realm);
+ if (name==NULL) {
+ tr_debug("tr_cfg_default_filters: could not allocate wildcard realm name for realm constraint.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ cons->matches[1]=name;
+ name=NULL;
+ filt->lines[0]->realm_cons=cons;
+
+ /* put the filter in a set */
+ filt_set=tr_filter_set_new(tmp_ctx);
+ if ((filt_set==NULL)||(0!=tr_filter_set_add(filt_set, filt))) {
+ tr_debug("tr_cfg_default_filters: could not allocate filter set.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+ talloc_steal(mem_ctx, filt_set);
+
+cleanup:
+ talloc_free(tmp_ctx);
+
+ if (*rc!=TR_CFG_SUCCESS)
+ filt=NULL;
+
+ if (n_prefix!=NULL)
+ tr_free_name(n_prefix);
+ if (n_rp_realm_1!=NULL)
+ tr_free_name(n_rp_realm_1);
+ if (n_rp_realm_2!=NULL)
+ tr_free_name(n_rp_realm_2);
+ if (n_realm!=NULL)
+ tr_free_name(n_realm);
+ if (n_domain!=NULL)
+ tr_free_name(n_domain);
+ if (name!=NULL)
+ tr_free_name(name);
+
+ return filt_set;
+}
+
+/* parses rp client */
+static TR_RP_CLIENT *tr_cfg_parse_one_rp_client(TALLOC_CTX *mem_ctx, json_t *jrealm, TR_CFG_RC *rc)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ TR_RP_CLIENT *client=NULL;
+ TR_CFG_RC call_rc=TR_CFG_ERROR;
+ TR_FILTER_SET *new_filts=NULL;
+ TR_NAME *realm=NULL;
+ json_t *jfilt=NULL;
+ json_t *jrealm_id=NULL;
+
+ *rc=TR_CFG_ERROR; /* default to error if not set */
+
+ if ((!jrealm) || (!rc)) {
+ tr_err("tr_cfg_parse_one_rp_client: Bad parameters.");
+ if (rc)
+ *rc=TR_CFG_BAD_PARAMS;
+ goto cleanup;
+ }
+
+ if ((NULL==(jrealm_id=json_object_get(jrealm, "realm"))) || (!json_is_string(jrealm_id))) {
+ tr_debug("tr_cfg_parse_one_rp_client: no realm ID found.");
+ *rc=TR_CFG_BAD_PARAMS;
+ goto cleanup;
+ }
+
+ tr_debug("tr_cfg_parse_one_rp_client: realm_id=\"%s\"", json_string_value(jrealm_id));
+ realm=tr_new_name(json_string_value(jrealm_id));
+ if (realm==NULL) {
+ tr_err("tr_cfg_parse_one_rp_client: could not allocate realm ID.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ if (NULL==(client=tr_rp_client_new(tmp_ctx))) {
+ tr_err("tr_cfg_parse_one_rp_client: could not allocate rp client.");
+ *rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ client->gss_names=tr_cfg_parse_gss_names(client, json_object_get(jrealm, "gss_names"), &call_rc);
+
+ if (call_rc!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_one_rp_client: could not parse gss_names.");
+ *rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ /* parse filters */
+ jfilt=json_object_get(jrealm, "filters");
+ if (jfilt!=NULL) {
+ new_filts=tr_cfg_parse_filters(tmp_ctx, jfilt, &call_rc);
+ if (call_rc!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_one_rp_client: could not parse filters.");
+ *rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+ } else {
+ tr_debug("tr_cfg_parse_one_rp_client: no filters specified, using default filters.");
+ new_filts= tr_cfg_default_filters(tmp_ctx, realm, &call_rc);
+ if (call_rc!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_one_rp_client: could not set default filters.");
+ *rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+ }
+
+ tr_rp_client_set_filters(client, new_filts);
+ *rc=TR_CFG_SUCCESS;
+
+ cleanup:
+ if (realm!=NULL)
+ tr_free_name(realm);
+
+ if (*rc==TR_CFG_SUCCESS)
+ talloc_steal(mem_ctx, client);
+ else {
+ talloc_free(client);
+ client=NULL;
+ }
+
+ talloc_free(tmp_ctx);
+ return client;
+}
+
+ /* Determine whether the realm is an RP realm */
+static int tr_cfg_is_rp_realm(json_t *jrealm)
+{
+ /* Check that we have a gss name. */
+ if (NULL != json_object_get(jrealm, "gss_names"))
+ return 1;
+ else
+ return 0;
+}
+
+/* Parse any rp clients in the j_realms object. Ignores other realms. */
+static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealms, TR_CFG_RC *rc)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ TR_RP_CLIENT *clients=NULL;
+ TR_RP_CLIENT *new_client=NULL;
+ json_t *this_jrealm=NULL;
+ int ii=0;
+
+ *rc=TR_CFG_ERROR;
+ if ((jrealms==NULL) || (!json_is_array(jrealms))) {
+ tr_err("tr_cfg_parse_rp_clients: realms not an array");
+ *rc=TR_CFG_BAD_PARAMS;
+ goto cleanup;
+ }
+
+ for (ii=0; ii<json_array_size(jrealms); ii++) {
+ this_jrealm=json_array_get(jrealms, ii);
+ if (tr_cfg_is_rp_realm(this_jrealm)) {
+ new_client=tr_cfg_parse_one_rp_client(tmp_ctx, this_jrealm, rc);
+ if ((*rc)!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_rp_clients: error decoding realm entry %d", ii+1);
+ *rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+ tr_rp_client_add(clients, new_client);
+ }
+ }
+
+ *rc=TR_CFG_SUCCESS;
+ talloc_steal(mem_ctx, clients);
+
+cleanup:
+ talloc_free(tmp_ctx);
+ return clients;
+}
+
+/* takes a talloc context, but currently does not use it */
+static TR_NAME *tr_cfg_parse_org_name(TALLOC_CTX *mem_ctx, json_t *j_org, TR_CFG_RC *rc)
+{
+ TR_NAME *name=NULL;
+
+ if ((j_org==NULL) || (rc==NULL) || (!json_is_string(j_org))) {
+ tr_debug("tr_cfg_parse_org_name: Bad parameters.");
+ if (rc!=NULL)
+ *rc = TR_CFG_BAD_PARAMS; /* fill in return value if we can */
+ return NULL;
+ }
+
+ name=tr_new_name(json_string_value(j_org));
+ if (name==NULL)
+ *rc=TR_CFG_NOMEM;
+ else
+ *rc=TR_CFG_SUCCESS;
+ return name;
+}
+
+static TR_CFG_RC tr_cfg_parse_one_local_org(TR_CFG *trc, json_t *jlorg)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ TR_CFG_RC retval=TR_CFG_ERROR; /* our return code */
+ TR_CFG_RC rc=TR_CFG_ERROR; /* return code from functions we call */
+ TR_NAME *org_name=NULL;
+ json_t *j_org=NULL;
+ json_t *j_realms=NULL;
+ TR_IDP_REALM *new_idp_realms=NULL;
+ TR_RP_CLIENT *new_rp_clients=NULL;
+
+ tr_debug("tr_cfg_parse_one_local_org: parsing local organization");
+
+ /* get organization_name (optional) */
+ if (NULL==(j_org=json_object_get(jlorg, "organization_name"))) {
+ tr_debug("tr_cfg_parse_one_local_org: organization_name unspecified");
+ } else {
+ org_name=tr_cfg_parse_org_name(tmp_ctx, j_org, &rc);
+ if (rc==TR_CFG_SUCCESS) {
+ tr_debug("tr_cfg_parse_one_local_org: organization_name=\"%.*s\"",
+ org_name->len,
+ org_name->buf);
+ /* we don't actually do anything with this, but we could */
+ tr_free_name(org_name);
+ org_name=NULL;
+ }
+ }
+
+ /* Now get realms. Allow this to be missing; even though that is a pointless organization entry,
+ * it's harmless. Report a warning because that might be unintentional. */
+ if (NULL==(j_realms=json_object_get(jlorg, "realms"))) {
+ tr_warning("tr_cfg_parse_one_local_org: warning - no realms in this local organization");
+ } else {
+ /* Allocate in the tmp_ctx so these will be cleaned up if we do not complete successfully. */
+ new_idp_realms=tr_cfg_parse_idp_realms(tmp_ctx, j_realms, &rc);
+ if (rc!=TR_CFG_SUCCESS)
+ goto cleanup;
+
+ new_rp_clients=tr_cfg_parse_rp_clients(tmp_ctx, j_realms, &rc);
+ if (rc!=TR_CFG_SUCCESS)
+ goto cleanup;
+ }
+ retval=TR_CFG_SUCCESS;
+
+cleanup:
+ /* if we succeeded, link things to the configuration and move out of tmp context */
+ if (retval==TR_CFG_SUCCESS) {
+ if (new_idp_realms!=NULL) {
+ tr_idp_realm_add(trc->ctable->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
+ talloc_steal(trc, trc->ctable->idp_realms); /* make sure the head is in the right context */
+ }
+
+ if (new_rp_clients!=NULL) {
+ 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);
+ return rc;
+}
+
+/* Parse local organizations if present. Returns success if there are none. On failure, the configuration is unreliable. */
+static TR_CFG_RC tr_cfg_parse_local_orgs(TR_CFG *trc, json_t *jcfg)
+{
+ json_t *jlocorgs=NULL;
+ size_t ii=0;
+
+ jlocorgs=json_object_get(jcfg, "local_organizations");
+ if (jlocorgs==NULL)
+ return TR_CFG_SUCCESS;
+
+ if (!json_is_array(jlocorgs)) {
+ tr_err("tr_cfg_parse_local_orgs: local_organizations is not an array.");
+ return TR_CFG_NOPARSE;
+ }
+
+ for (ii=0; ii<json_array_size(jlocorgs); ii++) {
+ if (tr_cfg_parse_one_local_org(trc, json_array_get(jlocorgs, ii))!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_local_orgs: error parsing local_organization %d.", ii+1);
+ return TR_CFG_NOPARSE;
+ }
+ }
+
+ return TR_CFG_SUCCESS;
+}
+
+static TR_CFG_RC tr_cfg_parse_one_peer_org(TR_CFG *trc, json_t *jporg)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ json_t *jhost=NULL;
+ json_t *jport=NULL;
+ json_t *jgss=NULL;
+ json_t *jfilt=NULL;
+ TRP_PEER *new_peer=NULL;
+ TR_GSS_NAMES *names=NULL;
+ TR_FILTER_SET *filt_set=NULL;
+ TR_CFG_RC rc=TR_CFG_ERROR;
+
+ jhost=json_object_get(jporg, "hostname");
+ jport=json_object_get(jporg, "port");
+ jgss=json_object_get(jporg, "gss_names");
+ jfilt=json_object_get(jporg, "filters");
+
+ if ((jhost==NULL) || (!json_is_string(jhost))) {
+ tr_err("tr_cfg_parse_one_peer_org: hostname not specified or not a string.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ if ((jport!=NULL) && (!json_is_number(jport))) {
+ /* note that not specifying the port is allowed, but if set it must be a number */
+ tr_err("tr_cfg_parse_one_peer_org: port is not a number.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ if ((jgss==NULL) || (!json_is_array(jgss))) {
+ tr_err("tr_cfg_parse_one_peer_org: gss_names not specified or not an array.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ if ((jfilt!=NULL) && (!json_is_object(jfilt))) {
+ tr_err("tr_cfg_parse_one_peer_org: filters is not an object.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ new_peer=trp_peer_new(tmp_ctx);
+ if (new_peer==NULL) {
+ tr_err("tr_cfg_parse_one_peer_org: could not allocate new peer.");
+ rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ trp_peer_set_server(new_peer, json_string_value(jhost)); /* string is strdup'ed in _set_server() */
+ if (jport==NULL)
+ trp_peer_set_port(new_peer, TRP_PORT);
+ else
+ trp_peer_set_port(new_peer, json_integer_value(jport));
+
+ names=tr_cfg_parse_gss_names(tmp_ctx, jgss, &rc);
+ if (rc!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_one_peer_org: unable to parse gss names.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+ trp_peer_set_gss_names(new_peer, names);
+
+ if (jfilt) {
+ filt_set=tr_cfg_parse_filters(tmp_ctx, jfilt, &rc);
+ if (rc!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_one_peer_org: unable to parse filters.");
+ rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+ trp_peer_set_filters(new_peer, filt_set);
+ }
+
+ /* success! */
+ trp_ptable_add(trc->peers, new_peer);
+ rc=TR_CFG_SUCCESS;
+
+ cleanup:
+ talloc_free(tmp_ctx);
+ return rc;
+}
+
+/* Parse peer organizations, if present. Returns success if there are none. */
+static TR_CFG_RC tr_cfg_parse_peer_orgs(TR_CFG *trc, json_t *jcfg)
+{
+ json_t *jpeerorgs=NULL;
+ int ii=0;
+
+ jpeerorgs=json_object_get(jcfg, "peer_organizations");
+ if (jpeerorgs==NULL)
+ return TR_CFG_SUCCESS;
+
+ if (!json_is_array(jpeerorgs)) {
+ tr_err("tr_cfg_parse_peer_orgs: peer_organizations is not an array.");
+ return TR_CFG_NOPARSE;
+ }
+
+ for (ii=0; ii<json_array_size(jpeerorgs); ii++) {
+ if (tr_cfg_parse_one_peer_org(trc, json_array_get(jpeerorgs, ii))!=TR_CFG_SUCCESS) {
+ tr_err("tr_cfg_parse_peer_orgs: error parsing peer_organization %d.", ii+1);
+ return TR_CFG_NOPARSE;
+ }
+ }
+
+ return TR_CFG_SUCCESS;
+}
+
+static TR_CFG_RC tr_cfg_parse_default_servers (TR_CFG *trc, json_t *jcfg)
+{
+ json_t *jdss = NULL;
+ TR_CFG_RC rc = TR_CFG_SUCCESS;
+ TR_AAA_SERVER *ds = NULL;
+ int i = 0;
+
+ if ((!trc) || (!jcfg))
+ return TR_CFG_BAD_PARAMS;
+
+ /* 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(jdss); i++) {
+ if (NULL == (ds = tr_cfg_parse_one_aaa_server(trc,
+ json_array_get(jdss, i),
+ &rc))) {
+ return rc;
+ }
+ tr_debug("tr_cfg_parse_default_servers: Default server configured: %s", ds->hostname->buf);
+ ds->next = trc->default_servers;
+ trc->default_servers = ds;
+ }
+ }
+
+ tr_debug("tr_cfg_parse_default_servers: Finished (rc=%d)", rc);
+ return rc;
+}
+
+static void tr_cfg_parse_comm_idps(TR_CFG *trc, json_t *jidps, TR_COMM *comm, TR_CFG_RC *rc)
+{
+ TR_IDP_REALM *found_idp=NULL;
+ json_t *jidp_name=NULL;
+ TR_NAME *idp_name=NULL;
+ size_t ii = 0;
+
+ if ((!trc) ||
+ (!jidps) ||
+ (!json_is_array(jidps))) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return;
+ }
+
+ json_array_foreach(jidps, ii, jidp_name) {
+ idp_name=tr_new_name(json_string_value(jidp_name));
+ if (idp_name==NULL) {
+ *rc = TR_CFG_NOMEM;
+ return;
+ }
+ found_idp=tr_cfg_find_idp(trc, idp_name, rc);
+ tr_free_name(idp_name);
+
+ if ((found_idp==NULL) || (*rc!=TR_CFG_SUCCESS)) {
+ tr_debug("tr_cfg_parse_comm_idps: Unknown IDP %s.", json_string_value(jidp_name));
+ *rc=TR_CFG_ERROR;
+ return;
+ }
+ tr_comm_add_idp_realm(trc->ctable, comm, found_idp, 0, NULL, NULL); /* no provenance, never expires */
+ }
+
+ *rc=TR_CFG_SUCCESS;
+ return;
+}
+
+static void tr_cfg_parse_comm_rps(TR_CFG *trc, json_t *jrps, TR_COMM *comm, TR_CFG_RC *rc)
+{
+ TR_RP_REALM *found_rp=NULL;
+ TR_RP_REALM *new_rp=NULL;
+ TR_NAME *rp_name=NULL;
+ const char *s=NULL;
+ int ii=0;
+
+ if ((!trc) ||
+ (!jrps) ||
+ (!json_is_array(jrps))) {
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ return;
+ }
+
+ for (ii=0; ii<json_array_size(jrps); ii++) {
+ /* get the RP name as a string */
+ s=json_string_value(json_array_get(jrps, ii));
+ if (s==NULL) {
+ tr_notice("tr_cfg_parse_comm_rps: null RP found in community %.*s, ignoring.",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ continue;
+ }
+
+ /* convert string to TR_NAME */
+ rp_name=tr_new_name(s);
+ if (rp_name==NULL) {
+ tr_err("tr_cfg_parse_comm_rps: unable to allocate RP name for %s in community %.*s.",
+ s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
+
+ /* see if we already have this RP in this community */
+ found_rp=tr_comm_find_rp(trc->ctable, comm, rp_name);
+ if (found_rp!=NULL) {
+ tr_notice("tr_cfg_parse_comm_rps: RP %s repeated in community %.*s.",
+ s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ tr_free_name(rp_name);
+ continue;
+ }
+
+ /* Add the RP to the community, first see if we have the RP in any community */
+ found_rp=tr_rp_realm_lookup(trc->ctable->rp_realms, rp_name);
+ if (found_rp!=NULL) {
+ tr_debug("tr_cfg_parse_comm_rps: RP realm %s already exists.", s);
+ new_rp=found_rp; /* use it rather than creating a new realm record */
+ } else {
+ new_rp=tr_rp_realm_new(NULL);
+ if (new_rp==NULL) {
+ tr_err("tr_cfg_parse_comm_rps: unable to allocate RP record for %s in community %.*s.",
+ s, tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
+ tr_debug("tr_cfg_parse_comm_rps: setting name to %s", rp_name->buf);
+ tr_rp_realm_set_id(new_rp, rp_name);
+ rp_name=NULL; /* rp_name no longer belongs to us */
+ tr_rp_realm_add(trc->ctable->rp_realms, new_rp);
+ talloc_steal(trc->ctable, trc->ctable->rp_realms); /* make sure head is in the right context */
+ }
+ tr_comm_add_rp_realm(trc->ctable, comm, new_rp, 0, NULL, NULL);
+ }
+}
+
+static TR_COMM *tr_cfg_parse_one_comm (TALLOC_CTX *mem_ctx, TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ 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 ((!trc) || (!jcomm) || (!rc)) {
+ tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
+ if (rc)
+ *rc = TR_CFG_BAD_PARAMS;
+ goto cleanup;
+ }
+
+ comm=tr_comm_new(tmp_ctx);
+ if (comm==NULL) {
+ tr_crit("tr_cfg_parse_one_comm: Out of memory.");
+ *rc = TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+
+ 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))) {
+ tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
+ *rc = TR_CFG_NOPARSE;
+ comm=NULL;
+ goto cleanup;
+ }
+
+ tr_comm_set_id(comm, tr_new_name(json_string_value(jid)));
+ if (NULL == tr_comm_get_id(comm)) {
+ tr_debug("tr_cfg_parse_one_comm: No memory for community id.");
+ *rc = TR_CFG_NOMEM;
+ comm=NULL;
+ goto cleanup;
+ }
+
+ 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(trc, japcs, rc))) {
+ tr_debug("tr_cfg_parse_one_comm: Can't parse APCs for COI %s.",
+ tr_comm_get_id(comm)->buf);
+ comm=NULL;
+ goto cleanup;
+ }
+ } else {
+ tr_debug("tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s",
+ tr_comm_get_id(comm)->buf, json_string_value(jtype));
+ *rc = TR_CFG_NOPARSE;
+ comm=NULL;
+ goto cleanup;
+ }
+
+ tr_cfg_parse_comm_idps(trc, jidps, comm, rc);
+ if (TR_CFG_SUCCESS != *rc) {
+ tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.",
+ tr_comm_get_id(comm)->buf);
+ comm=NULL;
+ goto cleanup;
+ }
+
+ tr_cfg_parse_comm_rps(trc, jrps, comm, rc);
+ if (TR_CFG_SUCCESS != *rc) {
+ tr_debug("tr_cfg_parse_one_comm: Can't parse RP realms for comm %s .",
+ tr_comm_get_id(comm)->buf);
+ comm=NULL;
+ goto cleanup;
+ }
+
+ if (TR_COMM_APC == comm->type) {
+ json_t *jexpire = json_object_get(jcomm, "expiration_interval");
+ comm->expiration_interval = 43200; /*30 days*/
+ if (jexpire) {
+ if (!json_is_integer(jexpire)) {
+ tr_err("tr_parse_one_comm: expiration_interval is not an integer for comm %.*s",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ comm=NULL;
+ goto cleanup;
+ }
+ comm->expiration_interval = json_integer_value(jexpire);
+ if (comm->expiration_interval <= 10) {
+ comm->expiration_interval = 11; /* Freeradius waits 10 minutes between successful TR queries*/
+ tr_notice(
+ "tr_parse_one_comm: expiration interval for %.*s less than minimum of 11 minutes; using 11 minutes instead.",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
+ if (comm->expiration_interval > 129600) {
+ /* > 90 days*/
+ comm->expiration_interval = 129600;
+ tr_notice(
+ "tr_parse_one_comm: expiration interval for %.*s exceeds maximum of 90 days; using 90 days instead.",
+ tr_comm_get_id(comm)->len, tr_comm_get_id(comm)->buf);
+ }
+ }
+ }
+
+cleanup:
+ if (comm!=NULL)
+ talloc_steal(mem_ctx, comm);
+ talloc_free(tmp_ctx);
+ return comm;
+}
+
+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 ((!trc) || (!jcfg)) {
+ tr_debug("tr_cfg_parse_comms: Bad Parameters.");
+ return TR_CFG_BAD_PARAMS;
+ }
+
+ 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(NULL, /* TODO: use a talloc context */
+ trc,
+ json_array_get(jcomms, i),
+ &rc))) {
+ return rc;
+ }
+ tr_debug("tr_cfg_parse_comms: Community configured: %s.",
+ tr_comm_get_id(comm)->buf);
+
+ tr_comm_table_add_comm(trc->ctable, comm);
+ }
+ }
+ tr_debug("tr_cfg_parse_comms: Finished (rc=%d)", rc);
+ 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)) {
+ tr_debug("tr_cfg_validate: Error: No internal configuration, or no hostname.");
+ rc = TR_CFG_ERROR;
+ }
+
+ if (NULL == trc->rp_clients) {
+ tr_debug("tr_cfg_validate: Error: No RP Clients configured");
+ rc = TR_CFG_ERROR;
+ }
+
+ if (0==tr_comm_table_size(trc->ctable)) {
+ tr_debug("tr_cfg_validate: Error: No Communities configured");
+ rc = TR_CFG_ERROR;
+ }
+
+ if ((NULL == trc->default_servers) && (NULL == trc->ctable->idp_realms)) {
+ tr_debug("tr_cfg_validate: Error: No default servers or IDPs configured.");
+ rc = TR_CFG_ERROR;
+ }
+
+ return rc;
+}
+
+static void tr_cfg_log_json_error(const char *label, json_error_t *rc)
+{
+ tr_debug("%s: JSON parse error on line %d: %s",
+ label,
+ rc->line,
+ rc->text);
+}
+
+/**
+ * Parse a config file and return its JSON structure. Also emits a serial number to the log
+ * if one is present.
+ *
+ * @param file_with_path The file (with path!) to parse
+ * @return Pointer to the result of parsing, or null on error
+ */
+static json_t *tr_cfg_parse_one_config_file(const char *file_with_path)
+{
+ json_t *jcfg=NULL;
+ json_t *jser=NULL;
+ json_error_t rc;
+
+ if (NULL==(jcfg=json_load_file(file_with_path,
+ JSON_DISABLE_EOF_CHECK|JSON_REJECT_DUPLICATES, &rc))) {
+ tr_debug("tr_cfg_parse_one_config_file: Error parsing config file %s.",
+ file_with_path);
+ tr_cfg_log_json_error("tr_cfg_parse_one_config_file", &rc);
+ return NULL;
+ }
+
+ // Look for serial number and log it if it exists (borrowed reference, so no need to free it later)
+ if (NULL!=(jser=json_object_get(jcfg, "serial_number"))) {
+ if (json_is_number(jser)) {
+ tr_notice("tr_parse_one_config_file: Attempting to load revision %" JSON_INTEGER_FORMAT " of '%s'.",
+ json_integer_value(jser),
+ file_with_path);
+ }
+ }
+
+ return jcfg;
+}
+
+/**
+ * Helper to free an array returned by tr_cfg_parse_config_files
+ * @param n_jcfgs
+ * @param jcfgs
+ */
+static void tr_cfg_parse_free_jcfgs(unsigned int n_jcfgs, json_t **jcfgs)
+{
+ int ii=0;
+ for (ii=0; ii<n_jcfgs; ii++)
+ json_decref(jcfgs[ii]);
+ talloc_free(jcfgs);
+}
+
+/**
+ * Parse a list of configuration files. Returns an array of JSON objects, free this with
+ * tr_cfg_parse_free_jcfgs(), a helper function
+ *
+ * @param config_dir
+ * @param n_files
+ * @param cfg_files
+ * @return
+ */
+static json_t **tr_cfg_parse_config_files(TALLOC_CTX *mem_ctx, unsigned int n_files, char **files_with_paths)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ unsigned int ii=0;
+ json_t **jcfgs=NULL;
+
+ /* first allocate the jcfgs */
+ jcfgs=talloc_array(NULL, json_t *, n_files);
+ if (jcfgs==NULL) {
+ tr_crit("tr_parse_config_files: cannot allocate JSON structure array");
+ goto cleanup;
+ }
+ for (ii=0; ii<n_files; ii++) {
+ jcfgs[ii]=tr_cfg_parse_one_config_file(files_with_paths[ii]);
+ if (jcfgs[ii]==NULL) {
+ tr_err("tr_parse_config: Error parsing JSON in %s", files_with_paths[ii]);
+ tr_cfg_parse_free_jcfgs(ii, jcfgs); /* frees the JSON objects and the jcfgs array */
+ jcfgs=NULL;
+ goto cleanup;
+ }
+ }
+cleanup:
+ if (jcfgs)
+ talloc_steal(mem_ctx, jcfgs); /* give this to the caller's context if we succeeded */
+ talloc_free(tmp_ctx);
+ return jcfgs;
+}
+
+/* define a type for config parse functions */
+typedef TR_CFG_RC (TR_CFG_PARSE_FN)(TR_CFG *, json_t *);
+/**
+ * Helper function to parse a collection of JSON structures using a generic parse function.
+ *
+ * @param cfg Config structure to receive results
+ * @param parse_fn Function to apply
+ * @param n_jcfg Number of JSON structures in the array
+ * @param jcfgs Pointer to an array of decoded JSON structures
+ * @param key Key to extract from each jcfg before parsing, or NULL to use the object itself
+ * @return TR_CFG_SUCCESS on success, _FAIL or an error code on failure
+ */
+static TR_CFG_RC tr_cfg_parse_helper(TR_CFG *cfg,
+ TR_CFG_PARSE_FN parse_fn,
+ unsigned int n_jcfg,
+ json_t **jcfgs,
+ const char *key)
+{
+ size_t ii=0;
+ json_t *this_jcfg=NULL;
+ TR_CFG_RC ret=TR_CFG_ERROR;
+
+ if ((cfg==NULL) || (jcfgs==NULL) || (parse_fn==NULL))
+ return TR_CFG_ERROR;
+
+ for (ii=0; ii<n_jcfg; ii++) {
+ if (key)
+ this_jcfg = json_object_get(jcfgs[ii], key);
+ else
+ this_jcfg = jcfgs[ii];
+
+ /* do not try to parse a missing jcfg */
+ if (this_jcfg == NULL)
+ continue;
+
+ ret=parse_fn(cfg, this_jcfg);
+ if (ret!=TR_CFG_SUCCESS)
+ break;
+ }
+ return ret;
+}
+
+
+/**
+ * Reads configuration files in config_dir ("" or "./" will use the current directory).
+ *
+ * @param cfg_mgr Configuration manager
+ * @param n_files Number of entries in cfg_files
+ * @param files_with_paths Array of filenames with path to load
+ * @return TR_CFG_SUCCESS on success, TR_CFG_ERROR or a more specific error on failure
+ */
+TR_CFG_RC tr_parse_config(TR_CFG_MGR *cfg_mgr, unsigned int n_files, char **files_with_paths)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ json_t **jcfgs=NULL;
+ TR_CFG_RC cfg_rc=TR_CFG_ERROR;
+
+ if ((!cfg_mgr) || (!files_with_paths)) {
+ cfg_rc=TR_CFG_BAD_PARAMS;
+ goto cleanup;
+ }
+
+ /* get a fresh config to fill in, freeing old one if needed */
+ if (cfg_mgr->new != NULL)
+ tr_cfg_free(cfg_mgr->new);
+ cfg_mgr->new=tr_cfg_new(tmp_ctx); /* belongs to the temporary context for now */
+ if (cfg_mgr->new == NULL) {
+ cfg_rc=TR_CFG_NOMEM;
+ goto cleanup;
+ }
+
+ /* first parse the json */
+ jcfgs=tr_cfg_parse_config_files(tmp_ctx, n_files, files_with_paths);
+ if (jcfgs==NULL) {
+ cfg_rc=TR_CFG_NOPARSE;
+ goto cleanup;
+ }
+
+ cfg_mgr->new->peers=trp_ptable_new(cfg_mgr); /* not sure why this isn't in cfg_mgr->new's context */
+
+ /* now run through the parsers on the JSON */
+ if ((TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_internal, n_files, jcfgs, "tr_internal"))) ||
+ (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_local_orgs, n_files, jcfgs, NULL))) ||
+ (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_peer_orgs, n_files, jcfgs, NULL))) ||
+ (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_default_servers, n_files, jcfgs,
+ NULL))) ||
+ (TR_CFG_SUCCESS != (cfg_rc= tr_cfg_parse_helper(cfg_mgr->new, tr_cfg_parse_comms, n_files, jcfgs, NULL))))
+ goto cleanup; /* cfg_rc was set above */
+
+ /* make sure we got a complete, consistent configuration */
+ if (TR_CFG_SUCCESS != tr_cfg_validate(cfg_mgr->new)) {
+ tr_err("tr_parse_config: Error: INVALID CONFIGURATION");
+ cfg_rc=TR_CFG_ERROR;
+ goto cleanup;
+ }
+
+ /* success! */
+ talloc_steal(cfg_mgr, cfg_mgr->new); /* hand this over to the cfg_mgr context */
+ cfg_rc=TR_CFG_SUCCESS;
+
+cleanup:
+ if (jcfgs!=NULL)
+ tr_cfg_parse_free_jcfgs(n_files, jcfgs);
+ talloc_free(tmp_ctx);
+ return cfg_rc;
+}
+
+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->ctable->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
+ if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
+ tr_debug("tr_cfg_find_idp: Found %s.", 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;
+
+ 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) {
+ if (tr_gss_names_matches(cfg_rp->gss_names, rp_gss)) {
+ tr_debug("tr_cfg_find_rp: Found %s.", rp_gss->buf);
+ return cfg_rp;
+ }
+ }
+ /* if we didn't find one, return NULL */
+ return NULL;
+}
+
+static int is_cfg_file(const struct dirent *dent) {
+ int n;
+
+ /* Only accept filenames ending in ".cfg" and starting with a character
+ * other than an ASCII '.' */
+
+ /* filename must be at least 4 characters long to be acceptable */
+ n=strlen(dent->d_name);
+ if (n < 4) {
+ return 0;
+ }
+
+ /* filename must not start with '.' */
+ if ('.' == dent->d_name[0]) {
+ return 0;
+ }
+
+ /* If the above passed and the last four characters of the filename are .cfg, accept.
+ * (n.b., assumes an earlier test checked that the name is >= 4 chars long.) */
+ if (0 == strcmp(&(dent->d_name[n-4]), ".cfg")) {
+ return 1;
+ }
+
+ /* otherwise, return false. */
+ return 0;
+}
+
+/* Find configuration files in a particular directory. Returns the
+ * number of entries found, 0 if none are found, or <0 for some
+ * errors. If n>=0, the cfg_files parameter will contain a newly
+ * allocated array of pointers to struct dirent entries, as returned
+ * by scandir(). These can be freed with tr_free_config_file_list().
+ */
+int tr_find_config_files(const char *config_dir, struct dirent ***cfg_files) {
+ int n = 0;
+
+ n = scandir(config_dir, cfg_files, is_cfg_file, alphasort);
+
+ if (n < 0) {
+ perror("scandir");
+ tr_debug("tr_find_config: scandir error trying to scan %s.", config_dir);
+ }
+
+ return n;
+}
+
+/* Free memory allocated for configuration file list returned from tr_find_config_files().
+ * This can be called regardless of the return value of tr_find_config_values(). */
+void tr_free_config_file_list(int n, struct dirent ***cfg_files) {
+ int ii;
+
+ /* if n < 0, then scandir did not allocate anything because it failed */
+ if((n>=0) && (*cfg_files != NULL)) {
+ for(ii=0; ii<n; ii++) {
+ free((*cfg_files)[ii]);
+ }
+ free(*cfg_files); /* safe even if n==0 */
+ *cfg_files=NULL; /* this will help prevent accidentally freeing twice */