Fix bug where one or two stanzas in a config file would be but not more.
authorlinus <linus@nordu.net>
Fri, 21 Jan 2011 12:09:00 +0000 (13:09 +0100)
committerlinus <linus@nordu.net>
Fri, 21 Jan 2011 12:09:00 +0000 (13:09 +0100)
Also restructure error handling in rs_context_create().

lib/conf.c
lib/radsec.c

index d42f2cd..8519922 100644 (file)
@@ -70,10 +70,8 @@ rs_context_read_config(struct rs_context *ctx, const char *config_file)
       if (!r)
        return rs_err_ctx_push_fl (ctx, RSE_NOMEM, __FILE__, __LINE__, NULL);
       memset (r, 0, sizeof(*r));
-      if (ctx->realms)
-       ctx->realms->next = r;
-      else
-       ctx->realms = r;
+      r->next = ctx->realms->next;
+      ctx->realms->next = r;
       cfg_config = cfg_getnsec (cfg, "config", i);
       r->name = strdup (cfg_title (cfg_config));
 
@@ -115,12 +113,12 @@ rs_context_read_config(struct rs_context *ctx, const char *config_file)
   return RSE_OK;
 }
 
-struct rs_realm
-*rs_conf_find_realm(struct rs_context *ctx, const char *name)
+struct rs_realm *
+rs_conf_find_realm(struct rs_context *ctx, const char *name)
 {
   struct rs_realm *r;
 
-  for (r = ctx->realms; r; r = r->next)
+  for (r = ctx->realms->next; r != ctx->realms; r = r->next)
     if (!strcmp (r->name, name))
        return r;
   return NULL;
index 40d14fc..bc06894 100644 (file)
 int
 rs_context_create(struct rs_context **ctx, const char *dict)
 {
+  int err = RSE_OK;
   struct rs_context *h;
+  char *buf1 = NULL, *buf2 = NULL;
+  char *dir, *fn;
+
+  assert (dict);
 
   if (ctx)
     *ctx = NULL;
   h = (struct rs_context *) malloc (sizeof(struct rs_context));
-  if (h)
+  if (!h)
+    return RSE_NOMEM;
+
+  /* Initialize freeradius dictionary.  */
+  buf1 = malloc (strlen (dict) + 1);
+  buf2 = malloc (strlen (dict) + 1);
+  if (!buf1 || !buf2)
+    {
+      err = RSE_NOMEM;
+      goto err_out;
+    }
+  strcpy (buf1, dict);
+  dir = dirname (buf1);
+  strcpy (buf2, dict);
+  fn = basename (buf2);
+  if (dict_init (dir, fn) < 0)
     {
-      char *buf1 = NULL, *buf2 = NULL;
-      char *dir, *fn;
-
-      buf1 = malloc (strlen (dict) + 1);
-      buf2 = malloc (strlen (dict) + 1);
-      if (!buf1 || !buf2)
-       {
-         free (h);
-         if (buf1)
-           free (buf1);
-         if (buf2)
-           free (buf2);
-         return RSE_NOMEM;
-       }
-      strcpy (buf1, dict);
-      dir = dirname (buf1);
-      strcpy (buf2, dict);
-      fn = basename (buf2);
-      if (dict_init (dir, fn) < 0)
-       {
-         free (h);
-         return RSE_SOME_ERROR;
-       }
-      free (buf1);
-      free (buf2);
+      err = RSE_SOME_ERROR;
+      goto err_out;
+    }
+  free (buf1);
+  free (buf2);
+
 #if defined RS_ENABLE_TLS
-      ssl_init ();
+  ssl_init ();
 #endif
 #if defined (DEBUG)
-      fr_log_fp = stderr;
-      fr_debug_flag = 1;
+  fr_log_fp = stderr;
+  fr_debug_flag = 1;
 #endif
-      debug_init ("libradsec");        /* radsecproxy compat, FIXME: remove */
-
-      memset (h, 0, sizeof(struct rs_context));
-      fr_randinit (&h->fr_randctx, 0);
-      fr_rand_seed (NULL, 0);
+  debug_init ("libradsec");    /* radsecproxy compat, FIXME: remove */
 
-      if (ctx)
-       *ctx = h;
+  memset (h, 0, sizeof(struct rs_context));
+  h->realms = malloc (sizeof (struct rs_realm));
+  if (!h->realms)
+    {
+      err = RSE_NOMEM;
+      goto err_out;
     }
-  return h ? RSE_OK : RSE_NOMEM;
+  memset (h->realms, 0, sizeof (struct rs_realm));
+  h->realms->next = h->realms;
+  fr_randinit (&h->fr_randctx, 0);
+  fr_rand_seed (NULL, 0);
+
+  if (ctx)
+    *ctx = h;
+
+  return RSE_OK;
+
+ err_out:
+  if (buf1)
+    free (buf1);
+  if (buf2)
+    free (buf2);
+  if (h)
+    free (h);
+  return err;
 }
 
 void rs_context_destroy(struct rs_context *ctx)