Blame SOURCES/dhcp-resize_ldap_buffer.patch

2d964c
diff --git a/common/conflex.c b/common/conflex.c
2d964c
index c99732e..df33f18 100644
2d964c
--- a/common/conflex.c
2d964c
+++ b/common/conflex.c
2d964c
@@ -153,13 +153,19 @@ save_parse_state(struct parse *cfile) {
2d964c
 /*
2d964c
  * Return the parser to the previous saved state.
2d964c
  *
2d964c
- * You must call save_parse_state() before calling 
2d964c
- * restore_parse_state(), but you can call restore_parse_state() any
2d964c
- * number of times after that.
2d964c
+ * You must call save_parse_state() every time before calling
2d964c
+ * restore_parse_state().
2d964c
+ *
2d964c
+ * Note: When the read function callback is in use in ldap mode,
2d964c
+ * a call to get_char() may reallocate the buffer and will append
2d964c
+ * config data to the buffer until a state restore.
2d964c
+ * Do not restore to the (freed) pointer and size, but use new one.
2d964c
  */
2d964c
 isc_result_t
2d964c
 restore_parse_state(struct parse *cfile) {
2d964c
 	struct parse *saved_state;
2d964c
+	char *inbuf = cfile->inbuf;
2d964c
+	size_t size = cfile->bufsiz;
2d964c
 
2d964c
 	if (cfile->saved_state == NULL) {
2d964c
 		return DHCP_R_NOTYET;
2d964c
@@ -167,7 +173,11 @@ restore_parse_state(struct parse *cfile) {
2d964c
 
2d964c
 	saved_state = cfile->saved_state;
2d964c
 	memcpy(cfile, saved_state, sizeof(*cfile));
2d964c
-	cfile->saved_state = saved_state;
2d964c
+	dfree(cfile->saved_state, MDL);
2d964c
+	cfile->saved_state = NULL;
2d964c
+
2d964c
+	cfile->inbuf = inbuf;
2d964c
+	cfile->bufsiz = size;
2d964c
 	return ISC_R_SUCCESS;
2d964c
 }
2d964c
 
2d964c
diff --git a/server/ldap.c b/server/ldap.c
2d964c
index 5577e7a..9a8b33e 100644
2d964c
--- a/server/ldap.c
2d964c
+++ b/server/ldap.c
2d964c
@@ -80,12 +80,107 @@ typedef struct ldap_dn_node {
2d964c
 static ldap_dn_node *ldap_service_dn_head = NULL;
2d964c
 static ldap_dn_node *ldap_service_dn_tail = NULL;
2d964c
 
2d964c
+static int ldap_read_function (struct parse *cfile);
2d964c
+
2d964c
+static struct parse *
2d964c
+x_parser_init(const char *name)
2d964c
+{
2d964c
+  struct parse *cfile;
2d964c
+  isc_result_t res;
2d964c
+  char *inbuf;
2d964c
+
2d964c
+  inbuf = dmalloc (LDAP_BUFFER_SIZE, MDL);
2d964c
+  if (inbuf == NULL)
2d964c
+    return NULL;
2d964c
+
2d964c
+  cfile = (struct parse *) NULL;
2d964c
+  res = new_parse (&cfile, -1, inbuf, LDAP_BUFFER_SIZE, name, 0);
2d964c
+  if (res != ISC_R_SUCCESS)
2d964c
+    {
2d964c
+      dfree(inbuf, MDL);
2d964c
+      return NULL;
2d964c
+    }
2d964c
+  /* the buffer is still empty */
2d964c
+  cfile->bufsiz = LDAP_BUFFER_SIZE;
2d964c
+  cfile->buflen = cfile->bufix = 0;
2d964c
+  /* attach ldap read function */
2d964c
+  cfile->read_function = ldap_read_function;
2d964c
+  return cfile;
2d964c
+}
2d964c
+
2d964c
+static isc_result_t
2d964c
+x_parser_free(struct parse **cfile)
2d964c
+{
2d964c
+  if (cfile && *cfile)
2d964c
+    {
2d964c
+      if ((*cfile)->inbuf)
2d964c
+          dfree((*cfile)->inbuf, MDL);
2d964c
+      (*cfile)->inbuf = NULL;
2d964c
+      (*cfile)->bufsiz = 0;
2d964c
+      return end_parse(cfile);
2d964c
+    }
2d964c
+  return ISC_R_SUCCESS;
2d964c
+}
2d964c
+
2d964c
+static int
2d964c
+x_parser_resize(struct parse *cfile, size_t len)
2d964c
+{
2d964c
+  size_t size;
2d964c
+  char * temp;
2d964c
+
2d964c
+  /* grow by len rounded up at LDAP_BUFFER_SIZE */
2d964c
+  size = cfile->bufsiz + (len | (LDAP_BUFFER_SIZE-1)) + 1;
2d964c
+
2d964c
+  /* realloc would be better, but there isn't any */
2d964c
+  if ((temp = dmalloc (size, MDL)) != NULL)
2d964c
+    {
2d964c
+#if defined (DEBUG_LDAP)
2d964c
+      log_info ("Reallocated %s buffer from %zu to %zu",
2d964c
+                cfile->tlname, cfile->bufsiz, size);
2d964c
+#endif
2d964c
+      memcpy(temp, cfile->inbuf, cfile->bufsiz);
2d964c
+      dfree(cfile->inbuf, MDL);
2d964c
+      cfile->inbuf  = temp;
2d964c
+      cfile->bufsiz = size;
2d964c
+      return 1;
2d964c
+    }
2d964c
+
2d964c
+  /*
2d964c
+   * Hmm... what is worser, consider it as fatal error and
2d964c
+   * bail out completely or discard config data in hope it
2d964c
+   * is "only" an option in dynamic host lookup?
2d964c
+   */
2d964c
+  log_error("Unable to reallocated %s buffer from %zu to %zu",
2d964c
+            cfile->tlname, cfile->bufsiz, size);
2d964c
+  return 0;
2d964c
+}
2d964c
 
2d964c
 static char *
2d964c
-x_strncat(char *dst, const char *src, size_t dst_size)
2d964c
+x_parser_strcat(struct parse *cfile, const char *str)
2d964c
+{
2d964c
+  size_t cur = strlen(cfile->inbuf);
2d964c
+  size_t len = strlen(str);
2d964c
+  size_t cnt;
2d964c
+
2d964c
+  if (cur + len >= cfile->bufsiz && !x_parser_resize(cfile, len))
2d964c
+    return NULL;
2d964c
+
2d964c
+  cnt = cfile->bufsiz > cur ? cfile->bufsiz - cur - 1 : 0;
2d964c
+  return strncat(cfile->inbuf, str, cnt);
2d964c
+}
2d964c
+
2d964c
+static inline void
2d964c
+x_parser_reset(struct parse *cfile)
2d964c
+{
2d964c
+  cfile->inbuf[0] = '\0';
2d964c
+  cfile->bufix = cfile->buflen = 0;
2d964c
+}
2d964c
+
2d964c
+static inline size_t
2d964c
+x_parser_length(struct parse *cfile)
2d964c
 {
2d964c
-  size_t len = strlen(dst);
2d964c
-  return strncat(dst, src, dst_size > len ? dst_size - len - 1: 0);
2d964c
+  cfile->buflen = strlen(cfile->inbuf);
2d964c
+  return cfile->buflen;
2d964c
 }
2d964c
 
2d964c
 static void
2d964c
@@ -102,9 +197,9 @@ ldap_parse_class (struct ldap_config_stack *item, struct parse *cfile)
2d964c
       return;
2d964c
     }
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "class \"", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, "\" {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "class \"");
2d964c
+  x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+  x_parser_strcat (cfile, "\" {\n");
2d964c
 
2d964c
   item->close_brace = 1;
2d964c
   ldap_value_free_len (tempbv);
2d964c
@@ -136,11 +231,11 @@ ldap_parse_subclass (struct ldap_config_stack *item, struct parse *cfile)
2d964c
       return;
2d964c
     }
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "subclass ", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, classdata[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, " ", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, " {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "subclass ");
2d964c
+  x_parser_strcat (cfile, classdata[0]->bv_val);
2d964c
+  x_parser_strcat (cfile, " ");
2d964c
+  x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+  x_parser_strcat (cfile, " {\n");
2d964c
 
2d964c
   item->close_brace = 1;
2d964c
   ldap_value_free_len (tempbv);
2d964c
@@ -164,14 +259,14 @@ ldap_parse_host (struct ldap_config_stack *item, struct parse *cfile)
2d964c
 
2d964c
   hwaddr = ldap_get_values_len (ld, item->ldent, "dhcpHWAddress");
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "host ", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "host ");
2d964c
+  x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
 
2d964c
   if (hwaddr != NULL && hwaddr[0] != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, " {\nhardware ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, hwaddr[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, " {\nhardware ");
2d964c
+      x_parser_strcat (cfile, hwaddr[0]->bv_val);
2d964c
+      x_parser_strcat (cfile, ";\n");
2d964c
       ldap_value_free_len (hwaddr);
2d964c
     }
2d964c
 
2d964c
@@ -194,9 +289,9 @@ ldap_parse_shared_network (struct ldap_config_stack *item, struct parse *cfile)
2d964c
       return;
2d964c
     }
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "shared-network \"", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, "\" {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "shared-network \"");
2d964c
+  x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+  x_parser_strcat (cfile, "\" {\n");
2d964c
 
2d964c
   item->close_brace = 1;
2d964c
   ldap_value_free_len (tempbv);
2d964c
@@ -249,14 +344,14 @@ ldap_parse_subnet (struct ldap_config_stack *item, struct parse *cfile)
2d964c
       return;
2d964c
     }
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "subnet ", LDAP_BUFFER_SIZE);
2d964c
-  x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "subnet ");
2d964c
+  x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, " netmask ", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, " netmask ");
2d964c
   parse_netmask (strtol (netmaskstr[0]->bv_val, NULL, 10), netmaskbuf);
2d964c
-  x_strncat (cfile->inbuf, netmaskbuf, LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, netmaskbuf);
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, " {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, " {\n");
2d964c
 
2d964c
   ldap_value_free_len (tempbv);
2d964c
   ldap_value_free_len (netmaskstr);
2d964c
@@ -265,10 +360,10 @@ ldap_parse_subnet (struct ldap_config_stack *item, struct parse *cfile)
2d964c
     {
2d964c
       for (i=0; tempbv[i] != NULL; i++)
2d964c
         {
2d964c
-          x_strncat (cfile->inbuf, "range", LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, " ", LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, tempbv[i]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+          x_parser_strcat (cfile, "range");
2d964c
+          x_parser_strcat (cfile, " ");
2d964c
+          x_parser_strcat (cfile, tempbv[i]->bv_val);
2d964c
+          x_parser_strcat (cfile, ";\n");
2d964c
         }
2d964c
     }
2d964c
 
2d964c
@@ -282,17 +377,17 @@ ldap_parse_pool (struct ldap_config_stack *item, struct parse *cfile)
2d964c
   struct berval **tempbv;
2d964c
   int i;
2d964c
 
2d964c
-  x_strncat (cfile->inbuf, "pool {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "pool {\n");
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpRange")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "range", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "range");
2d964c
       for (i=0; tempbv[i] != NULL; i++)
2d964c
         {
2d964c
-          x_strncat (cfile->inbuf, " ", LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, tempbv[i]->bv_val, LDAP_BUFFER_SIZE);
2d964c
+          x_parser_strcat (cfile, " ");
2d964c
+          x_parser_strcat (cfile, tempbv[i]->bv_val);
2d964c
         }
2d964c
-      x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, ";\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
@@ -300,8 +395,8 @@ ldap_parse_pool (struct ldap_config_stack *item, struct parse *cfile)
2d964c
     {
2d964c
       for (i=0; tempbv[i] != NULL; i++)
2d964c
         {
2d964c
-          x_strncat (cfile->inbuf, tempbv[i]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+          x_parser_strcat (cfile, tempbv[i]->bv_val);
2d964c
+          x_parser_strcat (cfile, ";\n");
2d964c
         }
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
@@ -313,7 +408,7 @@ ldap_parse_pool (struct ldap_config_stack *item, struct parse *cfile)
2d964c
 static void
2d964c
 ldap_parse_group (struct ldap_config_stack *item, struct parse *cfile)
2d964c
 {
2d964c
-  x_strncat (cfile->inbuf, "group {\n", LDAP_BUFFER_SIZE);
2d964c
+  x_parser_strcat (cfile, "group {\n");
2d964c
   item->close_brace = 1;
2d964c
 }
2d964c
 
2d964c
@@ -325,25 +420,25 @@ ldap_parse_key (struct ldap_config_stack *item, struct parse *cfile)
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "key ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, " {\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "key ");
2d964c
+      x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+      x_parser_strcat (cfile, " {\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpKeyAlgorithm")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "algorithm ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "algorithm ");
2d964c
+      x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+      x_parser_strcat (cfile, ";\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpKeySecret")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "secret ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "secret ");
2d964c
+      x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+      x_parser_strcat (cfile, ";\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
@@ -361,18 +456,18 @@ ldap_parse_zone (struct ldap_config_stack *item, struct parse *cfile)
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "zone ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, " {\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "zone ");
2d964c
+      x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
+      x_parser_strcat (cfile, " {\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpDnsZoneServer")) != NULL)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "primary ", LDAP_BUFFER_SIZE);
2d964c
-      x_strncat (cfile->inbuf, tempbv[0]->bv_val, LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "primary ");
2d964c
+      x_parser_strcat (cfile, tempbv[0]->bv_val);
2d964c
 
2d964c
-      x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, ";\n");
2d964c
       ldap_value_free_len (tempbv);
2d964c
     }
2d964c
 
2d964c
@@ -400,9 +495,9 @@ ldap_parse_zone (struct ldap_config_stack *item, struct parse *cfile)
2d964c
           strncpy (keyCn, cnFindStart, len);
2d964c
           keyCn[len] = '\0';
2d964c
 
2d964c
-          x_strncat (cfile->inbuf, "key ", LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, keyCn, LDAP_BUFFER_SIZE);
2d964c
-          x_strncat (cfile->inbuf, ";\n", LDAP_BUFFER_SIZE);
2d964c
+          x_parser_strcat (cfile, "key ");
2d964c
+          x_parser_strcat (cfile, keyCn);
2d964c
+          x_parser_strcat (cfile, ";\n");
2d964c
 
2d964c
           dfree (keyCn, MDL);
2d964c
         }
2d964c
@@ -990,7 +1085,7 @@ next_ldap_entry (struct parse *cfile)
2d964c
 
2d964c
   if (ldap_stack != NULL && ldap_stack->close_brace)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "}\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "}\n");
2d964c
       ldap_stack->close_brace = 0;
2d964c
     }
2d964c
 
2d964c
@@ -1000,7 +1095,7 @@ next_ldap_entry (struct parse *cfile)
2d964c
     {
2d964c
       if (ldap_stack->close_brace)
2d964c
         {
2d964c
-          x_strncat (cfile->inbuf, "}\n", LDAP_BUFFER_SIZE);
2d964c
+          x_parser_strcat (cfile, "}\n");
2d964c
           ldap_stack->close_brace = 0;
2d964c
         }
2d964c
 
2d964c
@@ -1011,7 +1106,7 @@ next_ldap_entry (struct parse *cfile)
2d964c
 
2d964c
   if (ldap_stack != NULL && ldap_stack->close_brace)
2d964c
     {
2d964c
-      x_strncat (cfile->inbuf, "}\n", LDAP_BUFFER_SIZE);
2d964c
+      x_parser_strcat (cfile, "}\n");
2d964c
       ldap_stack->close_brace = 0;
2d964c
     }
2d964c
 }
2d964c
@@ -1067,13 +1162,13 @@ check_statement_end (const char *statement)
2d964c
 
2d964c
 
2d964c
 static isc_result_t
2d964c
-ldap_parse_entry_options (LDAPMessage *ent, char *buffer, size_t size,
2d964c
+ldap_parse_entry_options (LDAPMessage *ent, struct parse *cfile,
2d964c
                           int *lease_limit)
2d964c
 {
2d964c
   struct berval **tempbv;
2d964c
   int i;
2d964c
 
2d964c
-  if (ent == NULL || buffer == NULL || size == 0)
2d964c
+  if (ent == NULL || cfile == NULL)
2d964c
     return (ISC_R_FAILURE);
2d964c
 
2d964c
   if ((tempbv = ldap_get_values_len (ld, ent, "dhcpStatements")) != NULL)
2d964c
@@ -1087,16 +1182,16 @@ ldap_parse_entry_options (LDAPMessage *ent, char *buffer, size_t size,
2d964c
               continue;
2d964c
             }
2d964c
 
2d964c
-          x_strncat (buffer, tempbv[i]->bv_val, size);
2d964c
+          x_parser_strcat (cfile, tempbv[i]->bv_val);
2d964c
 
2d964c
           switch((int) check_statement_end (tempbv[i]->bv_val))
2d964c
             {
2d964c
               case '}':
2d964c
               case ';':
2d964c
-                x_strncat (buffer, "\n", size);
2d964c
+                x_parser_strcat (cfile, "\n");
2d964c
                 break;
2d964c
               default:
2d964c
-                x_strncat (buffer, ";\n", size);
2d964c
+                x_parser_strcat (cfile, ";\n");
2d964c
                 break;
2d964c
             }
2d964c
         }
2d964c
@@ -1107,15 +1202,15 @@ ldap_parse_entry_options (LDAPMessage *ent, char *buffer, size_t size,
2d964c
     {
2d964c
       for (i=0; tempbv[i] != NULL; i++)
2d964c
         {
2d964c
-          x_strncat (buffer, "option ", size);
2d964c
-          x_strncat (buffer, tempbv[i]->bv_val, size);
2d964c
+          x_parser_strcat (cfile, "option ");
2d964c
+          x_parser_strcat (cfile, tempbv[i]->bv_val);
2d964c
           switch ((int) check_statement_end (tempbv[i]->bv_val))
2d964c
             {
2d964c
               case ';':
2d964c
-                x_strncat (buffer, "\n", size);
2d964c
+                x_parser_strcat (cfile, "\n");
2d964c
                 break;
2d964c
               default:
2d964c
-                x_strncat (buffer, ";\n", size);
2d964c
+                x_parser_strcat (cfile, ";\n");
2d964c
                 break;
2d964c
             }
2d964c
         }
2d964c
@@ -1135,6 +1230,7 @@ ldap_generate_config_string (struct parse *cfile)
2d964c
   LDAPMessage * ent, * res;
2d964c
   int i, ignore, found;
2d964c
   int ret;
2d964c
+  size_t len = cfile->buflen;
2d964c
 
2d964c
   if (ld == NULL)
2d964c
     ldap_start ();
2d964c
@@ -1187,7 +1283,7 @@ ldap_generate_config_string (struct parse *cfile)
2d964c
       else
2d964c
         found = 0;
2d964c
 
2d964c
-      if (found && cfile->inbuf[0] == '\0')
2d964c
+      if (found && x_parser_length(cfile) <= len)
2d964c
         {
2d964c
           ignore = 1;
2d964c
           break;
2d964c
@@ -1202,8 +1298,7 @@ ldap_generate_config_string (struct parse *cfile)
2d964c
       return;
2d964c
     }
2d964c
 
2d964c
-  ldap_parse_entry_options(entry->ldent, cfile->inbuf,
2d964c
-                           LDAP_BUFFER_SIZE-1, NULL);
2d964c
+  ldap_parse_entry_options(entry->ldent, cfile, NULL);
2d964c
 
2d964c
   dn = ldap_get_dn (ld, entry->ldent);
2d964c
 
2d964c
@@ -1269,25 +1364,31 @@ ldap_write_debug (const void *buff, size_t size)
2d964c
 static int
2d964c
 ldap_read_function (struct parse *cfile)
2d964c
 {
2d964c
-  cfile->inbuf[0] = '\0';
2d964c
-  cfile->buflen = 0;
2d964c
- 
2d964c
-  while (ldap_stack != NULL && *cfile->inbuf == '\0')
2d964c
+  size_t len;
2d964c
+
2d964c
+  /* append when in saved state */
2d964c
+  if (cfile->saved_state == NULL)
2d964c
+    {
2d964c
+      cfile->inbuf[0] = '\0';
2d964c
+      cfile->bufix = 0;
2d964c
+      cfile->buflen = 0;
2d964c
+    }
2d964c
+  len = cfile->buflen;
2d964c
+
2d964c
+  while (ldap_stack != NULL && x_parser_length(cfile) <= len)
2d964c
     ldap_generate_config_string (cfile);
2d964c
 
2d964c
-  if (ldap_stack == NULL && *cfile->inbuf == '\0')
2d964c
+  if (x_parser_length(cfile) <= len && ldap_stack == NULL)
2d964c
     return (EOF);
2d964c
 
2d964c
-  cfile->bufix = 1;
2d964c
-  cfile->buflen = strlen (cfile->inbuf) - 1;
2d964c
-  if (cfile->buflen > 0)
2d964c
-    ldap_write_debug (cfile->inbuf, cfile->buflen);
2d964c
+  if (cfile->buflen > len)
2d964c
+    ldap_write_debug (cfile->inbuf + len, cfile->buflen - len);
2d964c
 
2d964c
 #if defined (DEBUG_LDAP)
2d964c
   log_info ("Sending config line '%s'", cfile->inbuf);
2d964c
 #endif
2d964c
 
2d964c
-  return (cfile->inbuf[0]);
2d964c
+  return (cfile->inbuf[cfile->bufix++]);
2d964c
 }
2d964c
 
2d964c
 
2d964c
@@ -1353,7 +1454,7 @@ ldap_read_config (void)
2d964c
 {
2d964c
   LDAPMessage * ldres, * hostres, * ent, * hostent;
2d964c
   char hfilter[1024], sfilter[1024], fqdn[257];
2d964c
-  char *buffer, *hostdn;
2d964c
+  char *hostdn;
2d964c
   ldap_dn_node *curr = NULL;
2d964c
   struct parse *cfile;
2d964c
   struct utsname unme;
2d964c
@@ -1362,20 +1463,18 @@ ldap_read_config (void)
2d964c
   int ret, cnt;
2d964c
   struct berval **tempbv = NULL;
2d964c
 
2d964c
+  cfile = x_parser_init("LDAP");
2d964c
+  if (cfile == NULL)
2d964c
+    return (ISC_R_NOMEMORY);
2d964c
+  
2d964c
   if (ld == NULL)
2d964c
     ldap_start ();
2d964c
   if (ld == NULL)
2d964c
-    return (ldap_server == NULL ? ISC_R_SUCCESS : ISC_R_FAILURE);
2d964c
- 
2d964c
-  buffer = dmalloc (LDAP_BUFFER_SIZE+1, MDL);
2d964c
-  if (buffer == NULL)
2d964c
-    return (ISC_R_FAILURE);
2d964c
+    {
2d964c
+      x_parser_free(&cfile);
2d964c
+      return (ldap_server == NULL ? ISC_R_SUCCESS : ISC_R_FAILURE);
2d964c
+    }
2d964c
 
2d964c
-  cfile = (struct parse *) NULL;
2d964c
-  res = new_parse (&cfile, -1, buffer, LDAP_BUFFER_SIZE, "LDAP", 0);
2d964c
-  if (res != ISC_R_SUCCESS)
2d964c
-    return (res);
2d964c
- 
2d964c
   uname (&unme);
2d964c
   if (ldap_dhcp_server_cn != NULL)
2d964c
     {
2d964c
@@ -1403,10 +1502,11 @@ ldap_read_config (void)
2d964c
                                 &hostres)) != LDAP_SUCCESS)
2d964c
     {
2d964c
       log_error ("Cannot find host LDAP entry %s %s",
2d964c
-		 ((ldap_dhcp_server_cn == NULL)?(unme.nodename):(ldap_dhcp_server_cn)), hfilter);
2d964c
+                 ((ldap_dhcp_server_cn == NULL)?(unme.nodename):(ldap_dhcp_server_cn)), hfilter);
2d964c
       if(NULL != hostres)
2d964c
         ldap_msgfree (hostres);
2d964c
       ldap_stop();
2d964c
+      x_parser_free(&cfile);
2d964c
       return (ISC_R_FAILURE);
2d964c
     }
2d964c
 
2d964c
@@ -1415,6 +1515,7 @@ ldap_read_config (void)
2d964c
       log_error ("Error: Cannot find LDAP entry matching %s", hfilter);
2d964c
       ldap_msgfree (hostres);
2d964c
       ldap_stop();
2d964c
+      x_parser_free(&cfile);
2d964c
       return (ISC_R_FAILURE);
2d964c
     }
2d964c
 
2d964c
@@ -1437,6 +1538,7 @@ ldap_read_config (void)
2d964c
         ldap_memfree (hostdn);
2d964c
       ldap_msgfree (hostres);
2d964c
       ldap_stop();
2d964c
+      x_parser_free(&cfile);
2d964c
       return (ISC_R_FAILURE);
2d964c
     }
2d964c
 
2d964c
@@ -1444,31 +1546,30 @@ ldap_read_config (void)
2d964c
   log_info ("LDAP: Parsing dhcpServer options '%s' ...", hostdn);
2d964c
 #endif
2d964c
 
2d964c
-  cfile->inbuf[0] = '\0';
2d964c
-  ldap_parse_entry_options(hostent, cfile->inbuf, LDAP_BUFFER_SIZE, NULL);
2d964c
-  cfile->buflen = strlen (cfile->inbuf);
2d964c
-  if(cfile->buflen > 0)
2d964c
+  res = ldap_parse_entry_options(hostent, cfile, NULL);
2d964c
+  if (res != ISC_R_SUCCESS)
2d964c
     {
2d964c
-      ldap_write_debug (cfile->inbuf, cfile->buflen);
2d964c
+      ldap_memfree (hostdn);
2d964c
+      ldap_stop();
2d964c
+      x_parser_free(&cfile);
2d964c
+      return res;
2d964c
+    }
2d964c
 
2d964c
+  if (x_parser_length(cfile) > 0)
2d964c
+    {
2d964c
       res = conf_file_subparse (cfile, root_group, ROOT_GROUP);
2d964c
       if (res != ISC_R_SUCCESS)
2d964c
         {
2d964c
           log_error ("LDAP: cannot parse dhcpServer entry '%s'", hostdn);
2d964c
           ldap_memfree (hostdn);
2d964c
           ldap_stop();
2d964c
+          x_parser_free(&cfile);
2d964c
           return res;
2d964c
         }
2d964c
-      cfile->inbuf[0] = '\0';
2d964c
+      x_parser_reset(cfile);
2d964c
     }
2d964c
   ldap_msgfree (hostres);
2d964c
 
2d964c
-  /*
2d964c
-  ** attach ldap (tree) read function now
2d964c
-  */
2d964c
-  cfile->bufix = cfile->buflen = 0;
2d964c
-  cfile->read_function = ldap_read_function;
2d964c
-
2d964c
   res = ISC_R_SUCCESS;
2d964c
   for (cnt=0; tempbv[cnt] != NULL; cnt++)
2d964c
     {
2d964c
@@ -1545,7 +1646,7 @@ ldap_read_config (void)
2d964c
         }
2d964c
     }
2d964c
 
2d964c
-  end_parse (&cfile);
2d964c
+  x_parser_free(&cfile);
2d964c
   ldap_close_debug_fd();
2d964c
 
2d964c
   ldap_memfree (hostdn);
2d964c
@@ -1593,17 +1694,18 @@ ldap_parse_options (LDAPMessage * ent, struct group *group,
2d964c
                          struct class **class)
2d964c
 {
2d964c
   int declaration, lease_limit;
2d964c
-  char option_buffer[8192];
2d964c
   enum dhcp_token token;
2d964c
   struct parse *cfile;
2d964c
   isc_result_t res;
2d964c
   const char *val;
2d964c
 
2d964c
   lease_limit = 0;
2d964c
-  *option_buffer = '\0';
2d964c
- 
2d964c
- /* This block of code will try to find the parent of the host, and
2d964c
-    if it is a group object, fetch the options and apply to the host. */
2d964c
+  cfile = x_parser_init(type == HOST_DECL ? "LDAP-HOST" : "LDAP-SUBCLASS");
2d964c
+  if (cfile == NULL)
2d964c
+    return (lease_limit);
2d964c
+
2d964c
+  /* This block of code will try to find the parent of the host, and
2d964c
+     if it is a group object, fetch the options and apply to the host. */
2d964c
   if (type == HOST_DECL) 
2d964c
     {
2d964c
       char *hostdn, *basedn, *temp1, *temp2, filter[1024];
2d964c
@@ -1642,13 +1744,11 @@ ldap_parse_options (LDAPMessage * ent, struct group *group,
2d964c
                 {
2d964c
                   if ((entry = ldap_first_entry (ld, groupdn)) != NULL)
2d964c
                     {
2d964c
-                      res = ldap_parse_entry_options (entry, option_buffer,
2d964c
-                                                      sizeof(option_buffer) - 1,
2d964c
-                                                      &lease_limit);
2d964c
+                      res = ldap_parse_entry_options (entry, cfile, &lease_limit);
2d964c
                       if (res != ISC_R_SUCCESS)
2d964c
                         {
2d964c
                           /* reset option buffer discarding any results */
2d964c
-                          *option_buffer = '\0';
2d964c
+                          x_parser_reset(cfile);
2d964c
                           lease_limit = 0;
2d964c
                         }
2d964c
                     }
2d964c
@@ -1659,24 +1759,18 @@ ldap_parse_options (LDAPMessage * ent, struct group *group,
2d964c
         }
2d964c
     }
2d964c
 
2d964c
-  res = ldap_parse_entry_options (ent, option_buffer, sizeof(option_buffer) - 1,
2d964c
-                                  &lease_limit);
2d964c
-  if (res != ISC_R_SUCCESS)
2d964c
-    return (lease_limit);
2d964c
-
2d964c
-  option_buffer[sizeof(option_buffer) - 1] = '\0';
2d964c
-  if (*option_buffer == '\0')
2d964c
-    return (lease_limit);
2d964c
-
2d964c
-  cfile = (struct parse *) NULL;
2d964c
-  res = new_parse (&cfile, -1, option_buffer, strlen (option_buffer), 
2d964c
-                   type == HOST_DECL ? "LDAP-HOST" : "LDAP-SUBCLASS", 0);
2d964c
+  res = ldap_parse_entry_options (ent, cfile, &lease_limit);
2d964c
   if (res != ISC_R_SUCCESS)
2d964c
-    return (lease_limit);
2d964c
+    {
2d964c
+      x_parser_free(&cfile);
2d964c
+      return (lease_limit);
2d964c
+    }
2d964c
 
2d964c
-#if defined (DEBUG_LDAP)
2d964c
-  log_info ("Sending the following options: '%s'", option_buffer);
2d964c
-#endif
2d964c
+  if (x_parser_length(cfile) == 0)
2d964c
+    {
2d964c
+      x_parser_free(&cfile);
2d964c
+      return (lease_limit);
2d964c
+    }
2d964c
 
2d964c
   declaration = 0;
2d964c
   do
2d964c
@@ -1687,7 +1781,7 @@ ldap_parse_options (LDAPMessage * ent, struct group *group,
2d964c
        declaration = parse_statement (cfile, group, type, host, declaration);
2d964c
     } while (1);
2d964c
 
2d964c
-  end_parse (&cfile);
2d964c
+  x_parser_free(&cfile);
2d964c
 
2d964c
   return (lease_limit);
2d964c
 }