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