Move community/realm out of inforec and into update message.
[trust_router.git] / common / tr_config.c
index 22df406..4307183 100644 (file)
@@ -333,12 +333,11 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
   json_t *jfaction=NULL;
   json_t *jfspecs=NULL;
   json_t *jffield=NULL;
-  json_t *jfmatches=NULL;
   json_t *jfmatch=NULL;
   json_t *jrc=NULL;
   json_t *jdc=NULL;
   TR_NAME *name=NULL;
-  int i=0, j=0, k=0;
+  int i=0, j=0;
 
   *rc=TR_CFG_ERROR;
 
@@ -452,15 +451,15 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
       }
 
       /* check that we have a match attribute */
-      if (NULL==(jfmatches=json_object_get(json_array_get(jfspecs, j), "match"))) {
+      if (NULL==(jfmatch=json_object_get(json_array_get(jfspecs, j), "match"))) {
         tr_debug("tr_cfg_parse_one_filter: Error parsing filter: missing match for filer spec %d, filter line %d.", i, j);
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
 
-      /* check that match is an array */
-      if (!json_is_array(jfmatches)) {
-        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not an array for filter spec %d, filter line %d.", i, j);
+      /* check that match is a string */
+      if (!json_is_string(jfmatch)) {
+        tr_debug("tr_cfg_parse_one_filter: Error parsing filter: match not a string for filter spec %d, filter line %d.", i, j);
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
@@ -480,25 +479,12 @@ static TR_FILTER *tr_cfg_parse_one_filter(TALLOC_CTX *mem_ctx, json_t *jfilt, TR
       }
 
       /* fill in the matches */
-      for (k=0; k<json_array_size(jfmatches); k++) {
-        if (NULL==(jfmatch=json_array_get(jfmatches, k))) {
-          tr_debug("tr_cfg_parse_one_filter: Error parsing filter: unable to load match %d for filter spec %d, filter line %d.", k, i, j); 
-          *rc=TR_CFG_NOPARSE;
-          goto cleanup;
-        }
-        if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
-          tr_debug("tr_cfg_parse_one_filter: Out of memory.");
-          *rc=TR_CFG_NOMEM;
-          goto cleanup;
-        }
-        if (0!=tr_fspec_add_match(filt->lines[i]->specs[j], name)) {
-          tr_debug("tr_cfg_parse_one_filter: Could not add match %d to filter spec %d, filter line %d.", k, i, j);
-          tr_free_name(name);
-          *rc=TR_CFG_ERROR;
-          goto cleanup;
-        }
+      if (NULL==(name=tr_new_name(json_string_value(jfmatch)))) {
+        tr_debug("tr_cfg_parse_one_filter: Out of memory.");
+        *rc=TR_CFG_NOMEM;
+        goto cleanup;
       }
-
+      tr_fspec_set_match(filt->lines[i]->specs[j], name);
     }
   }
   *rc=TR_CFG_SUCCESS;
@@ -665,7 +651,7 @@ static TR_APC *tr_cfg_parse_apcs(TALLOC_CTX *mem_ctx, json_t *japcs, TR_CFG_RC *
       *rc=TR_CFG_NOPARSE;
       goto cleanup;
     }
-    apcs=tr_apc_add(apcs, new_apc);
+    tr_apc_add(apcs, new_apc);
   }
 
   talloc_steal(mem_ctx, apcs);
@@ -946,7 +932,7 @@ static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealm
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
-      realms=tr_idp_realm_add(realms, new_realm);
+      tr_idp_realm_add(realms, new_realm);
     } else if (tr_cfg_is_remote_realm(this_jrealm)) {
       new_realm=tr_cfg_parse_one_remote_realm(tmp_ctx, this_jrealm, rc);
       if ((*rc)!=TR_CFG_SUCCESS) {
@@ -954,7 +940,7 @@ static TR_IDP_REALM *tr_cfg_parse_idp_realms(TALLOC_CTX *mem_ctx, json_t *jrealm
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
-      realms=tr_idp_realm_add(realms, new_realm);
+      tr_idp_realm_add(realms, new_realm);
     }
   }
   
@@ -1066,7 +1052,8 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
   TR_CONSTRAINT *cons=NULL;
   TR_NAME *name=NULL;
   TR_NAME *n_prefix=tr_new_name("*.");
-  TR_NAME *n_rp_realm=tr_new_name("rp_realm");
+  TR_NAME *n_rp_realm_1=tr_new_name("rp_realm");
+  TR_NAME *n_rp_realm_2=tr_new_name("rp_realm");
   TR_NAME *n_domain=tr_new_name("domain");
   TR_NAME *n_realm=tr_new_name("realm");
   
@@ -1078,7 +1065,11 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
     goto cleanup;
   }
 
-  if ((n_prefix==NULL) || (n_rp_realm==NULL) || (n_domain==NULL) || (n_realm==NULL)) {
+  if ((n_prefix==NULL) ||
+      (n_rp_realm_1==NULL) ||
+      (n_rp_realm_2==NULL) ||
+      (n_domain==NULL) ||
+      (n_realm==NULL)) {
     tr_debug("tr_cfg_default_filter: unable to allocate names.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
@@ -1100,8 +1091,8 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
 
   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;
-  n_rp_realm=NULL; /* we don't own this name any more */
+  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) {
@@ -1109,24 +1100,21 @@ static TR_FILTER *tr_cfg_default_filter(TALLOC_CTX *mem_ctx, TR_NAME *realm, TR_
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
-  if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
-    tr_debug("tr_cfg_default_filter: could not add realm name to filter spec.");
-    *rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
+  tr_fspec_set_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_filter: could not allocate wildcard realm name.");
     *rc=TR_CFG_NOMEM;
     goto cleanup;
   }
 
-  if (0!=tr_fspec_add_match(filt->lines[0]->specs[0], name)) {
-    tr_debug("tr_cfg_default_filter: could not add wildcard realm name to filter spec.");
-    *rc=TR_CFG_NOMEM;
-    goto cleanup;
-  }
+  tr_fspec_set_match(filt->lines[0]->specs[1], name);
   name=NULL; /* we no longer own the name */
 
   /* domain constraint */
@@ -1191,8 +1179,10 @@ cleanup:
 
   if (n_prefix!=NULL)
     tr_free_name(n_prefix);
-  if (n_rp_realm!=NULL)
-    tr_free_name(n_rp_realm);
+  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)
@@ -1323,7 +1313,7 @@ static TR_RP_CLIENT *tr_cfg_parse_rp_clients(TALLOC_CTX *mem_ctx, json_t *jrealm
         *rc=TR_CFG_NOPARSE;
         goto cleanup;
       }
-      clients=tr_rp_client_add(clients, new_client);
+      tr_rp_client_add(clients, new_client);
     }
   }
   
@@ -1386,9 +1376,9 @@ static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_I
         /* fill in the community with info */
         comm->type=TR_COMM_APC; /* realms added this way are in APCs */
         comm->expiration_interval=TR_DEFAULT_APC_EXPIRATION_INTERVAL;
-        comm->id=tr_dup_name(apc->id);
+        tr_comm_set_id(comm, tr_dup_name(apc->id));
         tr_comm_add_idp_realm(comm, realm);
-        new_comms=tr_comm_add(new_comms, comm);
+        tr_comm_add(new_comms, comm);
       } else {
         /* add this realm to the comm */
         tr_comm_add_idp_realm(comm, realm);
@@ -1397,7 +1387,7 @@ static TR_COMM *tr_cfg_comm_idp_update(TALLOC_CTX *mem_ctx, TR_COMM *comms, TR_I
   }
 
   /* we successfully built a list, add it to the other list */
-  comms=tr_comm_add(comms, new_comms);
+  tr_comm_add(comms, new_comms);
   talloc_steal(mem_ctx, comms);
  cleanup:
   talloc_free(tmp_ctx);
@@ -1453,12 +1443,12 @@ 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) {
-      trc->idp_realms=tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
+      tr_idp_realm_add(trc->idp_realms, new_idp_realms); /* fixes talloc contexts except for head*/
       talloc_steal(trc, trc->idp_realms); /* make sure the head is in the right context */
     }
 
     if (new_rp_clients!=NULL) {
-      trc->rp_clients=tr_rp_client_add(trc->rp_clients, new_rp_clients); /* fixes talloc contexts */
+      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 */
     }
   }
@@ -1689,7 +1679,9 @@ static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_CFG *trc, json_t *jrps, TR_CFG_RC
   return rp;
 }
 
-static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc) {
+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;
@@ -1701,13 +1693,14 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
     tr_debug("tr_cfg_parse_one_comm: Bad parameters.");
     if (rc)
       *rc = TR_CFG_BAD_PARAMS;
-    return NULL;
+    goto cleanup;
   }
 
-  if (NULL == (comm = talloc_zero(trc, TR_COMM))) {
+  comm=tr_comm_new(tmp_ctx);
+  if (comm==NULL) {
     tr_crit("tr_cfg_parse_one_comm: Out of memory.");
     *rc = TR_CFG_NOMEM;
-    return NULL;
+    goto cleanup;
   }
 
 
@@ -1723,13 +1716,16 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
       (!json_is_array(jrps))) {
     tr_debug("tr_cfg_parse_one_comm: Error parsing Communities configuration.");
     *rc = TR_CFG_NOPARSE;
-    return NULL;
+    comm=NULL;
+    goto cleanup;
   }
 
-  if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
+  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;
-    return NULL;
+    comm=NULL;
+    goto cleanup;
   }
 
   if (0 == strcmp(json_string_value(jtype), "apc")) {
@@ -1737,29 +1733,33 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
   } 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.", comm->id->buf);
-      tr_free_name(comm->id);
-      return NULL;
+      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", comm->id->buf, json_string_value(jtype));
-    tr_free_name(comm->id);
+    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;
-    return NULL;
+    comm=NULL;
+    goto cleanup;
   }
 
   comm->idp_realms = tr_cfg_parse_comm_idps(trc, jidps, rc);
   if (TR_CFG_SUCCESS != *rc) {
-    tr_debug("tr_cfg_parse_one_comm: Can't parse IDP realms for comm %s.", comm->id->buf);
-    tr_free_name(comm->id);
-    return NULL;
+    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;
   }
 
   comm->rp_realms = tr_cfg_parse_comm_rps(trc, jrps, rc);
   if (TR_CFG_SUCCESS != *rc) {
-    tr_debug("tr_cfg_parse_comm: Can't parse RP realms for comm %s .", comm->id->buf);
-    tr_free_name(comm->id);
-    return NULL;
+    tr_debug("tr_cfg_parse_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) {
@@ -1768,7 +1768,8 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
     if (jexpire) {
        if (!json_is_integer(jexpire)) {
          fprintf(stderr, "tr_parse_comm: expirae_interval is not an integer\n");
-         return NULL;
+    comm=NULL;
+    goto cleanup;
        }
        comm->expiration_interval = json_integer_value(jexpire);
        if (comm->expiration_interval <= 10)
@@ -1777,7 +1778,11 @@ static TR_COMM *tr_cfg_parse_one_comm (TR_CFG *trc, json_t *jcomm, TR_CFG_RC *rc
        comm->expiration_interval = 129600;
     }
   }
-  
+
+cleanup:
+  if (comm!=NULL)
+    talloc_steal(mem_ctx, comm);
+  talloc_free(tmp_ctx);
   return comm;
 }
 
@@ -1799,12 +1804,14 @@ static TR_CFG_RC tr_cfg_parse_comms (TR_CFG *trc, json_t *jcfg)
     }
 
     for (i = 0; i < json_array_size(jcomms); i++) {
-      if (NULL == (comm = tr_cfg_parse_one_comm(trc, 
-                                               json_array_get(jcomms, i), 
-                                               &rc))) {
+      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.", comm->id->buf);
+      tr_debug("tr_cfg_parse_comms: Community configured: %s.",
+               tr_comm_get_id(comm)->buf);
       comm->next = trc->comms;
       trc->comms = comm;
     }