Underlying code to check RP GSS Name.
[trust_router.git] / common / tr_config.c
index afa80ea..3e79778 100644 (file)
@@ -46,7 +46,7 @@ void tr_print_config (FILE *stream, TR_CFG *cfg) {
 }
 
 void tr_cfg_free (TR_CFG *cfg) {
-  /* TBD */
+  /* TBD -- need to deallocate memory as part of dynamic config */
   return;
 }
 
@@ -90,10 +90,85 @@ static TR_CFG_RC tr_cfg_parse_internal (TR_INSTANCE *tr, json_t *jcfg) {
   return TR_CFG_SUCCESS;
 }
 
-static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, json_t *jcfg) {
-  //  json_t *jrpr = NULL;
+static TR_RP_CLIENT *tr_cfg_parse_one_rp_client (TR_INSTANCE *tr, json_t *jrp, TR_CFG_RC *rc) 
+{
+  TR_RP_CLIENT *rp = NULL;
+  json_t *jgns = NULL;
+  int i = 0;
+
+  if ((!jrp) || (!rc)) {
+    fprintf(stderr, "tr_cfg_parse_one_rp_realm: Bad parameters.\n");
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  if (NULL == (rp = malloc(sizeof(TR_RP_CLIENT)))) {
+    fprintf(stderr, "tr_config_parse_one_rp_realm: Out of memory.\n");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
   
-  return TR_CFG_SUCCESS;
+  memset(rp, 0, sizeof(TR_RP_CLIENT));
+        
+  /* TBD parse filters and constraints */
+
+  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.\n");
+    free(rp);
+    *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");
+    *rc = TR_CFG_NOPARSE;
+    return NULL;
+  }
+
+  if (TR_MAX_GSS_NAMES < json_array_size(jgns)) {
+    fprintf(stderr, "tr_cfg_parse_one_rp_client: RP Client has too many GSS Names.\n");
+    *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");
+      *rc = TR_CFG_NOMEM;
+      return NULL;
+    }
+  }
+  
+  return rp;
+}
+
+static TR_CFG_RC tr_cfg_parse_rp_clients (TR_INSTANCE *tr, 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))
+    return TR_CFG_BAD_PARAMS;
+
+  if ((NULL == (jrps = json_object_get(jcfg, "rp_clients"))) ||
+      (!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;
+    }
+    fprintf(stderr, "tr_cfg_parse_rp_clients: RP client configured: %s.\n", rp->gss_names[0]->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) {
@@ -138,11 +213,39 @@ 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 *apcs, TR_CFG_RC *rc) 
+static TR_APC *tr_cfg_parse_apcs (TR_INSTANCE *tr, json_t *japcs, TR_CFG_RC *rc)
 {
   TR_APC *apc;
 
-  return (apc = malloc(sizeof(TR_APC)));
+  *rc = TR_CFG_SUCCESS;                /* presume success */
+
+  if ((!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)))) {
+    fprintf (stderr, "tr_cfg_parse_apcs: Out of memory.\n");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  memset(apc, 0, sizeof(TR_APC));
+
+  /* TBD, deal with more than one APC.  In the meantime, though...                */
+  /* Only parse the first APC, because we only know how to deal with one, anyway. */
+  if (0 == json_array_size(japcs))
+    return NULL;
+
+  if (NULL == (apc->id = tr_new_name((char *)json_string_value(json_array_get(japcs, 0))))) {
+    fprintf(stderr, "tr_cfg_parse_apcs: No memory for APC name.\n");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  return apc;
 }
 
 static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp, TR_CFG_RC *rc) {
@@ -152,14 +255,15 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp,
   json_t *jsrvrs = NULL;
   json_t *japcs = NULL;
 
-  if ((!tr) || (!tr->new_cfg) || (!jidp)) {
+  if ((!jidp) || (!rc)) {
     fprintf(stderr, "tr_cfg_parse_one_idp_realm: Bad parameters.\n");
-    *rc = TR_CFG_BAD_PARAMS;
+    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: Our of memory.\n");
+    fprintf(stderr, "tr_config_parse_one_idp_realm: Out of memory.\n");
     *rc = TR_CFG_NOMEM;
     return NULL;
   }
@@ -174,7 +278,7 @@ static TR_IDP_REALM *tr_cfg_parse_one_idp_realm (TR_INSTANCE *tr, json_t *jidp,
       (!json_is_array(jsrvrs)) ||
       (NULL == (japcs = json_object_get(jidp, "apcs"))) ||
       (!json_is_array(japcs))) {
-    fprintf(stderr, "tr_cfg_parse_one_realm: Error parsing IDP realm configuration.\n");
+    fprintf(stderr, "tr_cfg_parse_one_idp_realm: Error parsing IDP realm configuration.\n");
     free(idp);
     *rc = TR_CFG_NOPARSE;
     return NULL;
@@ -226,20 +330,224 @@ static TR_CFG_RC tr_cfg_parse_idp_realms (TR_INSTANCE *tr, json_t *jcfg)
   }
 
   for (i = 0; i < json_array_size(jidps); i++) {
-    if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, json_array_get(jidps, i), &rc))) {
+    if (NULL == (idp = tr_cfg_parse_one_idp_realm(tr, 
+                                                 json_array_get(jidps, i), 
+                                                 &rc))) {
        return rc;
     }
-    fprintf(stderr, "tr_cfg_parse_idp_realms: IDP realm configured: realm_id = %s.\n", idp->realm_id->buf);
+    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_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, json_t *jcfg) {
-  
-  return TR_CFG_SUCCESS;
+static TR_IDP_REALM *tr_cfg_find_idp (TR_INSTANCE *tr, TR_NAME *idp_id, TR_CFG_RC *rc)
+{
+
+  TR_IDP_REALM *cfg_idp;
+
+  if ((!tr) || (!idp_id)) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (cfg_idp = tr->active_cfg->idp_realms; NULL != cfg_idp; cfg_idp = cfg_idp->next) {
+    if (!tr_name_cmp (idp_id, cfg_idp->realm_id)) {
+      fprintf(stderr, "tr_cfg_find_idp: Found %s.\n", idp_id->buf);
+      return cfg_idp;
+    }
+  }
+  /* if we didn't find one, return NULL */ 
+  return NULL;
+}
+
+static TR_IDP_REALM *tr_cfg_parse_comm_idps (TR_INSTANCE *tr, json_t *jidps, TR_CFG_RC *rc)
+{
+  TR_IDP_REALM *idp = NULL;
+  TR_IDP_REALM *temp_idp = NULL;
+  int i = 0;
+
+  if ((!tr) ||
+      (!jidps) ||
+      (!json_is_array(jidps))) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (i = 0; i < json_array_size(jidps); i++) {
+    if (NULL == (temp_idp = (tr_cfg_find_idp(tr, 
+                                            tr_new_name((char *)json_string_value(json_array_get(jidps, i))), 
+                                            rc)))) {
+      fprintf(stderr, "tr_cfg_parse_comm_idps: Unknown IDP %s.\n", 
+             (char *)json_string_value(json_array_get(jidps, i)));
+      return NULL;
+    }
+
+    temp_idp->comm_next = idp;
+    idp = temp_idp;
+  }
+
+  return idp;
+}
+
+static TR_RP_REALM *tr_cfg_parse_comm_rps (TR_INSTANCE *tr, json_t *jrps, TR_CFG_RC *rc)
+{
+  TR_RP_REALM *rp = NULL;
+  TR_RP_REALM *temp_rp = NULL;
+  int i = 0;
+
+  if ((!tr) ||
+      (!jrps) ||
+      (!json_is_array(jrps))) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (i = (json_array_size(jrps)-1); i >= 0; i++) {
+    if (NULL == (temp_rp = malloc(sizeof(TR_RP_REALM)))) {
+      fprintf(stderr, "tr_cfg_parse_comm_rps: Can't allocate memory for RP Realm.\n");
+      if (rc)
+       *rc = TR_CFG_NOMEM;
+      return NULL;
+    }
+    memset (temp_rp, 0, sizeof(TR_RP_REALM));
+
+    if (NULL == (temp_rp->realm_name = tr_new_name((char *)json_string_value(json_array_get(jrps, i))))) {
+      fprintf(stderr, "tr_cfg_parse_comm_rps: No memory for RP Realm Name.\n");
+      if (rc)
+       *rc = TR_CFG_NOMEM;
+      return NULL;
+    }
+
+    temp_rp->next = rp;
+    rp = temp_rp;
+  }
+
+  return rp;
+}
+
+static TR_COMM *tr_cfg_parse_one_comm (TR_INSTANCE *tr, json_t *jcomm, TR_CFG_RC *rc) {
+  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 ((!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");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  memset(comm, 0, sizeof(TR_COMM));
+
+  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))) {
+    fprintf(stderr, "tr_cfg_parse_one_comm: Error parsing Communities configuration.\n");
+    free(comm);
+    *rc = TR_CFG_NOPARSE;
+    return NULL;
+  }
+
+  if (NULL == (comm->id = tr_new_name((char *)json_string_value(jid)))) {
+    free(comm);
+    fprintf(stderr, "tr_cfg_parse_one_comm: No memory for community id.\n");
+    *rc = TR_CFG_NOMEM;
+    return NULL;
+  }
+
+  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(tr, 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);
+      return NULL;
+    }
+  } else {
+    fprintf(stderr, "tr_cfg_parse_one_comm: Invalid community type, comm = %s, type = %s\n", comm->id->buf, json_string_value(jtype));
+    tr_free_name(comm->id);
+    free(comm);
+    *rc = TR_CFG_NOPARSE;
+    return NULL;
+  }
+
+  comm->idp_realms = tr_cfg_parse_comm_idps(tr, 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);
+    free(comm);
+    return NULL;
+  }
+
+  comm->rp_realms = tr_cfg_parse_comm_rps(tr, 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);
+    /* TBD -- free idps? */;
+    free(comm);
+    return NULL;
+  }
+
+  return comm;
+}
+
+static TR_CFG_RC tr_cfg_parse_comms (TR_INSTANCE *tr, 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");
+    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;
+  }
+  memset (comm, 0, sizeof(TR_COMM));
+
+  if ((NULL == (jcomms = json_object_get(jcfg, "communities"))) ||
+      (!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(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;
+  }
+  return rc;
 }
 
 TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
@@ -264,6 +572,29 @@ TR_CFG_RC tr_parse_config (TR_INSTANCE *tr, json_t *jcfg) {
   return TR_CFG_SUCCESS;
 }
 
+TR_RP_CLIENT *tr_cfg_find_rp (TR_INSTANCE *tr, TR_NAME *rp_gss, TR_CFG_RC *rc)
+{
+  TR_RP_CLIENT *cfg_rp;
+  int i;
+
+  if ((!tr) || (!rp_gss)) {
+    if (rc)
+      *rc = TR_CFG_BAD_PARAMS;
+    return NULL;
+  }
+
+  for (cfg_rp = tr->active_cfg->rp_clients; NULL != cfg_rp; cfg_rp = cfg_rp->next) {
+    for (i = 0; i < TR_MAX_GSS_NAMES; i++) {
+      if (!tr_name_cmp (rp_gss, cfg_rp->gss_names[i])) {
+       fprintf(stderr, "tr_cfg_find_rp: Found %s.\n", rp_gss->buf);
+       return cfg_rp;
+      }
+    }
+  }
+  /* if we didn't find one, return NULL */ 
+  return NULL;
+}
+
 json_t *tr_read_config (int n, struct dirent **cfg_files) {
   json_t *jcfg = NULL;
   json_t *temp = NULL;