a2a915
diff --git a/bin/sdb_tools/Makefile.in b/bin/sdb_tools/Makefile.in
a2a915
index 95ab742..6069f09 100644
a2a915
--- a/bin/sdb_tools/Makefile.in
a2a915
+++ b/bin/sdb_tools/Makefile.in
a2a915
@@ -32,11 +32,11 @@ DEPLIBS =	${LWRESDEPLIBS} ${DNSDEPLIBS} ${BIND9DEPLIBS} \
a2a915
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} \
a2a915
 		${ISCCFGLIBS} ${ISCCCLIBS} ${ISCLIBS} ${DBDRIVER_LIBS} @LIBS@
a2a915
 
a2a915
-TARGETS =	zone2ldap@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@
a2a915
+TARGETS =	zone2ldap@EXEEXT@ ldap2zone@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@
a2a915
 
a2a915
-OBJS	=	zone2ldap.@O@ zonetodb.@O@ zone2sqlite.@O@
a2a915
+OBJS	=	zone2ldap.@O@ ldap2zone.@O@ zonetodb.@O@ zone2sqlite.@O@
a2a915
 
a2a915
-SRCS    =       zone2ldap.c zonetodb.c zone2sqlite.c
a2a915
+SRCS    =       zone2ldap.c ldap2zone.c zonetodb.c zone2sqlite.c
a2a915
 
a2a915
 MANPAGES =      zone2ldap.1
a2a915
 
a2a915
@@ -53,6 +53,9 @@ zonetodb@EXEEXT@: zonetodb.@O@  ${DEPLIBS}
a2a915
 zone2sqlite@EXEEXT@: zone2sqlite.@O@  ${DEPLIBS}
a2a915
 	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ zone2sqlite.@O@ -lsqlite3 -lssl ${LIBS}
a2a915
 
a2a915
+ldap2zone@EXEEXT@: ldap2zone.@O@ ${DEPLIBS}
a2a915
+	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ ldap2zone.@O@ -lldap -llber ${LIBS}
a2a915
+
a2a915
 clean distclean manclean maintainer-clean::
a2a915
 	rm -f ${TARGETS} ${OBJS}
a2a915
 
a2a915
@@ -62,6 +65,7 @@ installdirs:
a2a915
 
a2a915
 install:: ${TARGETS} installdirs
a2a915
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2ldap@EXEEXT@ ${DESTDIR}${sbindir}
a2a915
+	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} ldap2zone@EXEEXT@ ${DESTDIR}${sbindir}
a2a915
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zonetodb@EXEEXT@  ${DESTDIR}${sbindir}
a2a915
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2sqlite@EXEEXT@ ${DESTDIR}${sbindir}
a2a915
 	${INSTALL_DATA} ${srcdir}/zone2ldap.1 ${DESTDIR}${mandir}/man1/zone2ldap.1
a2a915
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
a2a915
index 23dd873..d56bc56 100644
a2a915
--- a/bin/sdb_tools/zone2ldap.c
a2a915
+++ b/bin/sdb_tools/zone2ldap.c
a2a915
@@ -65,6 +66,9 @@ ldap_info;
a2a915
 /* usage Info */
a2a915
 void usage (void);
a2a915
 
a2a915
+/* Check for existence of (and possibly add) containing dNSZone objects */
a2a915
+int lookup_dns_zones( ldap_info *ldinfo);
a2a915
+
a2a915
 /* Add to the ldap dit */
a2a915
 void add_ldap_values (ldap_info * ldinfo);
a2a915
 
a2a915
@@ -81,7 +85,7 @@ char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);
a2a915
 int get_attr_list_size (char **tmp);
a2a915
 
a2a915
 /* Get a DN */
a2a915
-char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag);
a2a915
+char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
a2a915
 
a2a915
 /* Add to RR list */
a2a915
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
a2a915
@@ -103,11 +107,27 @@ void
a2a915
 init_ldap_conn ();
a2a915
 void usage();
a2a915
 
a2a915
-char *argzone, *ldapbase, *binddn, *bindpw = NULL;
a2a915
-const char *ldapsystem = "localhost";
a2a915
-static const char *objectClasses[] =
a2a915
-  { "top", "dNSZone", NULL };
a2a915
-static const char *topObjectClasses[] = { "top", NULL };
a2a915
+static char *argzone, *ldapbase, *binddn, *bindpw = NULL;
a2a915
+
a2a915
+/* these are needed to placate gcc4's const-ness const-ernations : */
a2a915
+static char localhost[] = "localhost";
a2a915
+static char *ldapsystem=&(localhost[0]);
a2a915
+/* dnszone schema class names: */
a2a915
+static char topClass    [] ="top";
a2a915
+static char dNSZoneClass[] ="dNSZone";
a2a915
+static char objectClass [] ="objectClass";
a2a915
+static char dcObjectClass[]="dcObject";
a2a915
+/* dnszone schema attribute names: */
a2a915
+static char relativeDomainName[]="relativeDomainName";
a2a915
+static char dNSTTL            []="dNSTTL";
a2a915
+static char zoneName          []="zoneName";
a2a915
+static char dc                []="dc";
a2a915
+static char sameZone          []="@";
a2a915
+/* LDAPMod mod_values: */
a2a915
+static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
a2a915
+static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
a2a915
+static char *dn_buffer      [64]={NULL};
a2a915
+
a2a915
 LDAP *conn;
a2a915
 unsigned int debug = 0;
a2a915
 
a2a915
@@ -131,12 +151,12 @@ main (int argc, char **argv)
a2a915
   isc_result_t result;
a2a915
   char *basedn;
a2a915
   ldap_info *tmp;
a2a915
-  LDAPMod *base_attrs[2];
a2a915
-  LDAPMod base;
a2a915
+  LDAPMod *base_attrs[5];
a2a915
+  LDAPMod base, dcBase, znBase, rdnBase;
a2a915
   isc_buffer_t buff;
a2a915
   char *zonefile=0L;
a2a915
   char fullbasedn[1024];
a2a915
-  char *ctmp;
a2a915
+  char *ctmp, *zn, *dcp[2], *znp[2], *rdn[2];
a2a915
   dns_fixedname_t fixedzone, fixedname;
a2a915
   dns_rdataset_t rdataset;
a2a915
   char **dc_list;
a2a915
@@ -149,7 +169,7 @@ main (int argc, char **argv)
a2a915
   extern char *optarg;
a2a915
   extern int optind, opterr, optopt;
a2a915
   int create_base = 0;
a2a915
-  int topt;
a2a915
+  int topt, dcn, zdn, znlen;
a2a915
 
a2a915
   if (argc < 2)
a2a915
     {
a2a915
@@ -157,7 +177,7 @@ main (int argc, char **argv)
a2a915
       exit (-1);
a2a915
     }
a2a915
 
a2a915
-  while ((topt = getopt (argc, argv, "D:w:b:z:f:h:?dcv")) != -1)
a2a915
+  while ((topt = getopt (argc, argv, "D:Ww:b:z:f:h:?dcv")) != -1)
a2a915
     {
a2a915
       switch (topt)
a2a915
 	{
a2a915
@@ -180,6 +200,9 @@ main (int argc, char **argv)
a2a915
 	  if (bindpw == NULL)
a2a915
 	    fatal("strdup");
a2a915
 	  break;
a2a915
+	case 'W':
a2a915
+	  bindpw = getpass("Enter LDAP Password: ");
a2a915
+	  break;
a2a915
 	case 'b':
a2a915
 	  ldapbase = strdup (optarg);
a2a915
 	  if (ldapbase == NULL)
a2a915
@@ -301,27 +324,62 @@ main (int argc, char **argv)
a2a915
     {
a2a915
       if (debug)
a2a915
 	printf ("Creating base zone DN %s\n", argzone);
a2a915
-
a2a915
+      
a2a915
       dc_list = hostname_to_dn_list (argzone, argzone, DNS_TOP);
a2a915
-      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC);
a2a915
 
a2a915
-      for (ctmp = &basedn[strlen (basedn)]; ctmp >= &basedn[0]; ctmp--)
a2a915
+      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC, argzone);
a2a915
+      if (debug)
a2a915
+	printf ("base DN %s\n", basedn);
a2a915
+
a2a915
+      for (ctmp = &basedn[strlen (basedn)], dcn=0; ctmp >= &basedn[0]; ctmp--)
a2a915
 	{
a2a915
-	  if ((*ctmp == ',') || (ctmp == &basedn[0]))
a2a915
+	    if ((*ctmp == ',') || (ctmp == &basedn[0]))
a2a915
 	    {
a2a915
+
a2a915
 	      base.mod_op = LDAP_MOD_ADD;
a2a915
-	      base.mod_type = (char*)"objectClass";
a2a915
-	      base.mod_values = (char**)topObjectClasses;
a2a915
+	      base.mod_type = objectClass;
a2a915
+	      base.mod_values = topObjectClasses;
a2a915
 	      base_attrs[0] = (void*)&bas;;
a2a915
-	      base_attrs[1] = NULL;
a2a915
-
a2a915
+	     
a2a915
+	      dcBase.mod_op = LDAP_MOD_ADD;
a2a915
+	      dcBase.mod_type = dc;
a2a915
+	      dcp[0]=dc_list[dcn];
a2a915
+	      dcp[1]=0L;
a2a915
+	      dcBase.mod_values=dcp;
a2a915
+	      base_attrs[1] = (void*)&dcBase;
a2a915
+
a2a915
+	      znBase.mod_op = LDAP_MOD_ADD;
a2a915
+	      znBase.mod_type = zoneName;	      
a2a915
+	      for( zdn = dcn, znlen = 0; zdn >= 0; zdn-- )
a2a915
+		  znlen += strlen(dc_list[zdn])+1;
a2a915
+	      znp[0] = (char*)malloc(znlen+1);
a2a915
+	      znp[1] = 0L;
a2a915
+	      for( zdn = dcn, zn=znp[0]; zdn >= 0; zdn-- )		  
a2a915
+		  zn+=sprintf(zn,"%s%s",dc_list[zdn], 
a2a915
+			      ((zdn > 0) && (*(dc_list[zdn-1])!='.')) ? "." : ""
a2a915
+		             );
a2a915
+
a2a915
+	      znBase.mod_values = znp;
a2a915
+	      base_attrs[2] = (void*)&znBase;
a2a915
+
a2a915
+	      rdnBase.mod_op = LDAP_MOD_ADD;
a2a915
+	      rdnBase.mod_type = relativeDomainName;
a2a915
+	      rdn[0] = strdup(sameZone);
a2a915
+	      rdn[1] = 0L;
a2a915
+	      rdnBase.mod_values = rdn;
a2a915
+	      base_attrs[3] = (void*)&rdnBase;
a2a915
+	      
a2a915
+	      dcn++;
a2a915
+
a2a915
+	      base.mod_values = topObjectClasses;
a2a915
+	      base_attrs[4] = NULL;	      
a2a915
+	      
a2a915
 	      if (ldapbase)
a2a915
 		{
a2a915
 		  if (ctmp != &basedn[0])
a2a915
 		    sprintf (fullbasedn, "%s,%s", ctmp + 1, ldapbase);
a2a915
 		  else
a2a915
-		    sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);
a2a915
-
a2a915
+		    sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);		  
a2a915
 		}
a2a915
 	      else
a2a915
 		{
a2a915
@@ -330,8 +388,13 @@ main (int argc, char **argv)
a2a915
 		  else
a2a915
 		    sprintf (fullbasedn, "%s", ctmp);
a2a915
 		}
a2a915
+
a2a915
+	      if( debug )
a2a915
+		  printf("Full base dn: %s\n", fullbasedn);
a2a915
+
a2a915
 	      result = ldap_add_s (conn, fullbasedn, base_attrs);
a2a915
 	      ldap_result_check ("intial ldap_add_s", fullbasedn, result);
a2a915
+
a2a915
 	    }
a2a915
 
a2a915
 	}
a2a915
@@ -409,14 +472,14 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
a2a915
   isc_result_check (result, "dns_rdata_totext");
a2a915
   data[isc_buffer_usedlength (&buff)] = 0;
a2a915
 
a2a915
-  dc_list = hostname_to_dn_list (name, argzone, DNS_OBJECT);
a2a915
+  dc_list = hostname_to_dn_list ((char*)name, argzone, DNS_OBJECT);
a2a915
   len = (get_attr_list_size (dc_list) - 2);
a2a915
-  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC);
a2a915
+  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC, argzone);
a2a915
 
a2a915
   if (debug)
a2a915
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
a2a915
 
a2a915
-  add_to_rr_list (dn, dc_list[len], type, data, ttl, DNS_OBJECT);
a2a915
+  add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
a2a915
 }
a2a915
 
a2a915
 
a2a915
@@ -456,7 +519,8 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
   int attrlist;
a2a915
   char ldap_type_buffer[128];
a2a915
   char charttl[64];
a2a915
-
a2a915
+  char *zn;
a2a915
+  int znlen;
a2a915
 
a2a915
   if ((tmp = locate_by_dn (dn)) == NULL)
a2a915
     {
a2a915
@@ -483,13 +547,13 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
 	    fatal("malloc");
a2a915
 	}
a2a915
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
a2a915
-      tmp->attrs[0]->mod_type = (char*)"objectClass";
a2a915
+      tmp->attrs[0]->mod_type = objectClass;
a2a915
 
a2a915
       if (flags == DNS_OBJECT)
a2a915
-	tmp->attrs[0]->mod_values = (char**)objectClasses;
a2a915
+	tmp->attrs[0]->mod_values = objectClasses;
a2a915
       else
a2a915
 	{
a2a915
-	  tmp->attrs[0]->mod_values = (char**)topObjectClasses;
a2a915
+	  tmp->attrs[0]->mod_values =topObjectClasses;
a2a915
 	  tmp->attrs[1] = NULL;
a2a915
 	  tmp->attrcnt = 2;
a2a915
 	  tmp->next = ldap_info_base;
a2a915
@@ -498,7 +562,7 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
 	}
a2a915
 
a2a915
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
a2a915
-      tmp->attrs[1]->mod_type = (char*)"relativeDomainName";
a2a915
+      tmp->attrs[1]->mod_type = relativeDomainName;
a2a915
       tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
a2a915
 
a2a915
       if (tmp->attrs[1]->mod_values == (char **)NULL)
a2a915
@@ -527,7 +591,7 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
 	 fatal("strdup");
a2a915
 
a2a915
       tmp->attrs[3]->mod_op = LDAP_MOD_ADD;
a2a915
-      tmp->attrs[3]->mod_type = (char*)"dNSTTL";
a2a915
+      tmp->attrs[3]->mod_type = dNSTTL;
a2a915
       tmp->attrs[3]->mod_values = (char **) calloc (sizeof (char *), 2);
a2a915
 
a2a915
       if (tmp->attrs[3]->mod_values == (char **)NULL)
a2a915
@@ -540,14 +604,25 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
       if (tmp->attrs[3]->mod_values[0] == NULL)
a2a915
 	 fatal("strdup");
a2a915
 
a2a915
+      znlen=strlen(gbl_zone);      
a2a915
+      if ( *(gbl_zone + (znlen-1)) == '.' )
a2a915
+      { /* ldapdb MUST search by relative zone name */
a2a915
+	  zn = (char*)malloc(znlen);
a2a915
+	  strncpy(zn,gbl_zone,znlen-1);
a2a915
+	  *(zn + (znlen-1))='\0';	  
a2a915
+      }else
a2a915
+      {
a2a915
+	  zn = gbl_zone;
a2a915
+      }
a2a915
+
a2a915
       tmp->attrs[4]->mod_op = LDAP_MOD_ADD;
a2a915
-      tmp->attrs[4]->mod_type = (char*)"zoneName";
a2a915
+      tmp->attrs[4]->mod_type = zoneName;
a2a915
       tmp->attrs[4]->mod_values = (char **)calloc(sizeof(char *), 2);
a2a915
 
a2a915
       if (tmp->attrs[4]->mod_values == (char **)NULL)
a2a915
 	 fatal("calloc");
a2a915
 
a2a915
-      tmp->attrs[4]->mod_values[0] = gbl_zone;
a2a915
+      tmp->attrs[4]->mod_values[0] = zn;
a2a915
       tmp->attrs[4]->mod_values[1] = NULL;
a2a915
 
a2a915
       tmp->attrs[5] = NULL;
a2a915
@@ -558,7 +633,7 @@ add_to_rr_list (char *dn, char *name, char *type,
a2a915
   else
a2a915
     {
a2a915
 
a2a915
-      for (i = 0; tmp->attrs[i] != NULL; i++)
a2a915
+	for (i = 0; tmp->attrs[i] != NULL; i++)
a2a915
 	{
a2a915
 	  sprintf (ldap_type_buffer, "%sRecord", type);
a2a915
 	  if (!strncmp
a2a915
@@ -632,44 +707,70 @@ char **
a2a915
 hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
a2a915
 {
a2a915
   char *tmp;
a2a915
-  static char *dn_buffer[64];
a2a915
   int i = 0;
a2a915
-  char *zname;
a2a915
-  char *hnamebuff;
a2a915
-
a2a915
-  zname = strdup (hostname);
a2a915
-  if (zname == NULL)
a2a915
-	fatal("strdup");
a2a915
-
a2a915
-  if (flags == DNS_OBJECT)
a2a915
-    {
a2a915
-
a2a915
-      if (strlen (zname) != strlen (zone))
a2a915
-	{
a2a915
-	  tmp = &zname[strlen (zname) - strlen (zone)];
a2a915
-	  *--tmp = '\0';
a2a915
-	  hnamebuff = strdup (zname);
a2a915
-	  if (hnamebuff == NULL)
a2a915
-		fatal("strdup");
a2a915
-	  zname = ++tmp;
a2a915
-	}
a2a915
-      else
a2a915
-	hnamebuff = (char*)"@";
a2a915
-    }
a2a915
-  else
a2a915
-    {
a2a915
-      zname = zone;
a2a915
-      hnamebuff = NULL;
a2a915
-    }
a2a915
-
a2a915
-  for (tmp = strrchr (zname, '.'); tmp != (char *) 0;
a2a915
-       tmp = strrchr (zname, '.'))
a2a915
-    {
a2a915
-      *tmp++ = '\0';
a2a915
-      dn_buffer[i++] = tmp;
a2a915
-    }
a2a915
-  dn_buffer[i++] = zname;
a2a915
-  dn_buffer[i++] = hnamebuff;
a2a915
+  char *hname=0L, *last=0L;
a2a915
+  int hlen=strlen(hostname), zlen=(strlen(zone));
a2a915
+
a2a915
+/*  printf("hostname: %s zone: %s\n",hostname, zone); */
a2a915
+  hname=0L;
a2a915
+  if(flags == DNS_OBJECT)
a2a915
+  {
a2a915
+      if( (zone[ zlen - 1 ] == '.') && (hostname[hlen - 1] != '.') )
a2a915
+      {
a2a915
+	  hname=(char*)malloc(hlen + 1);
a2a915
+	  hlen += 1;
a2a915
+	  sprintf(hname, "%s.", hostname);
a2a915
+	  hostname = hname;
a2a915
+      }
a2a915
+      if(strcmp(hostname, zone) == 0)
a2a915
+      {
a2a915
+	  if( hname == 0 )
a2a915
+	      hname=strdup(hostname);
a2a915
+  	  last = strdup(sameZone);
a2a915
+      }else
a2a915
+      {	   
a2a915
+	  if(  (hlen < zlen) 
a2a915
+	     ||( strcmp( hostname + (hlen - zlen), zone ) != 0)
a2a915
+	    )
a2a915
+	  {
a2a915
+	      if( hname != 0 )
a2a915
+		  free(hname);
a2a915
+	      hname=(char*)malloc( hlen + zlen + 1);
a2a915
+	      if( *zone == '.' )
a2a915
+		  sprintf(hname, "%s%s", hostname, zone);
a2a915
+	      else
a2a915
+		  sprintf(hname,"%s",zone);
a2a915
+	  }else
a2a915
+	  {
a2a915
+	      if( hname == 0 )
a2a915
+		  hname = strdup(hostname);
a2a915
+	  }
a2a915
+	  last = hname;
a2a915
+      }
a2a915
+  }else
a2a915
+  { /* flags == DNS_TOP */
a2a915
+      hname = strdup(zone);
a2a915
+      last = hname;
a2a915
+  }
a2a915
+
a2a915
+  for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
a2a915
+       tmp = strrchr (hname, '.'))
a2a915
+  {
a2a915
+      if( *( tmp + 1 ) != '\0' )
a2a915
+      {
a2a915
+	  *tmp = '\0';
a2a915
+	  dn_buffer[i++] = ++tmp;
a2a915
+      }else
a2a915
+      { /* trailing '.' ! */
a2a915
+	  dn_buffer[i++] = strdup(".");
a2a915
+	  *tmp = '\0';
a2a915
+	  if( tmp == hname )
a2a915
+	      break;
a2a915
+      }	  
a2a915
+  }
a2a915
+  if( ( last != hname ) && (tmp != hname) )
a2a915
+      dn_buffer[i++] = hname;
a2a915
+  dn_buffer[i++] = last;
a2a915
   dn_buffer[i] = NULL;
a2a915
 
a2a915
   return dn_buffer;
a2a915
@@ -681,24 +782,32 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
a2a915
  * exception of "@"/SOA. */
a2a915
 
a2a915
 char *
a2a915
-build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag)
a2a915
+build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
a2a915
 {
a2a915
   int size;
a2a915
-  int x;
a2a915
+  int x, znlen;
a2a915
   static char dn[1024];
a2a915
   char tmp[128];
a2a915
+  char zn[DNS_NAME_MAXTEXT+1];
a2a915
 
a2a915
   bzero (tmp, sizeof (tmp));
a2a915
   bzero (dn, sizeof (dn));
a2a915
   size = get_attr_list_size (dc_list);
a2a915
+  znlen = strlen(zone);
a2a915
+  if ( *(zone + (znlen-1)) == '.' )
a2a915
+  { /* ldapdb MUST search by relative zone name */
a2a915
+      memcpy(&(zn[0]),zone,znlen-1);
a2a915
+      *(zn + (znlen-1))='\0';
a2a915
+      zone = zn;
a2a915
+  }
a2a915
   for (x = size - 2; x > 0; x--)
a2a915
     {
a2a915
     if (flag == WI_SPEC)
a2a915
     {
a2a915
       if (x == (size - 2) && (strncmp (dc_list[x], "@", 1) == 0) && (ttl))
a2a915
-	sprintf (tmp, "relativeDomainName=%s + dNSTTL=%d,", dc_list[x], ttl);
a2a915
+	sprintf (tmp, "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
a2a915
       else if (x == (size - 2))
a2a915
-	      sprintf(tmp, "relativeDomainName=%s,",dc_list[x]);
a2a915
+	      sprintf(tmp, "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
a2a915
       else
a2a915
 	      sprintf(tmp,"dc=%s,", dc_list[x]);
a2a915
     }
a2a915
@@ -724,6 +833,7 @@ void
a2a915
 init_ldap_conn ()
a2a915
 {
a2a915
   int result;
a2a915
+  char ldb_tag[]="LDAP Bind";
a2a915
   conn = ldap_open (ldapsystem, LDAP_PORT);
a2a915
   if (conn == NULL)
a2a915
     {
a2a915
@@ -733,7 +843,7 @@ init_ldap_conn ()
a2a915
     }
a2a915
 
a2a915
   result = ldap_simple_bind_s (conn, binddn, bindpw);
a2a915
-  ldap_result_check ("ldap_simple_bind_s", (char*)"LDAP Bind", result);
a2a915
+  ldap_result_check ("ldap_simple_bind_s", ldb_tag , result);
a2a915
 }
a2a915
 
a2a915
 /* Like isc_result_check, only for LDAP */
a2a915
@@ -750,8 +860,6 @@ ldap_result_check (const char *msg, char *dn, int err)
a2a915
     }
a2a915
 }
a2a915
 
a2a915
-
a2a915
-
a2a915
 /* For running the ldap_info run queue. */
a2a915
 void
a2a915
 add_ldap_values (ldap_info * ldinfo)
a2a915
@@ -759,14 +867,14 @@ add_ldap_values (ldap_info * ldinfo)
a2a915
   int result;
a2a915
   char dnbuffer[1024];
a2a915
 
a2a915
-
a2a915
   if (ldapbase != NULL)
a2a915
     sprintf (dnbuffer, "%s,%s", ldinfo->dn, ldapbase);
a2a915
   else
a2a915
     sprintf (dnbuffer, "%s", ldinfo->dn);
a2a915
 
a2a915
   result = ldap_add_s (conn, dnbuffer, ldinfo->attrs);
a2a915
-  ldap_result_check ("ldap_add_s", dnbuffer, result);
a2a915
+    ldap_result_check ("ldap_add_s", dnbuffer, result);
a2a915
+
a2a915
 }
a2a915
 
a2a915
 
a2a915
@@ -777,5 +885,5 @@ void
a2a915
 usage ()
a2a915
 {
a2a915
   fprintf (stderr,
a2a915
-	   "zone2ldap -D [BIND DN] -w [BIND PASSWORD] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
a2a915
+	   "zone2ldap -D [BIND DN] [-w BIND PASSWORD | -W:prompt] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
a2a915
 	   "[-c Create LDAP Base structure][-d Debug Output (lots !)] \n ");}