2e2c49
diff --git a/bin/sdb_tools/Makefile.in b/bin/sdb_tools/Makefile.in
230545
index 95ab742..5059a17 100644
2e2c49
--- a/bin/sdb_tools/Makefile.in
2e2c49
+++ b/bin/sdb_tools/Makefile.in
2e2c49
@@ -32,11 +32,11 @@ DEPLIBS =	${LWRESDEPLIBS} ${DNSDEPLIBS} ${BIND9DEPLIBS} \
2e2c49
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} \
2e2c49
 		${ISCCFGLIBS} ${ISCCCLIBS} ${ISCLIBS} ${DBDRIVER_LIBS} @LIBS@
2e2c49
 
2e2c49
-TARGETS =	zone2ldap@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@
230545
+TARGETS =	zone2ldap@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@ ldap2zone@EXEEXT@
2e2c49
 
2e2c49
-OBJS	=	zone2ldap.@O@ zonetodb.@O@ zone2sqlite.@O@
230545
+OBJS	=	zone2ldap.@O@ zonetodb.@O@ zone2sqlite.@O@ ldap2zone.@O@
2e2c49
 
2e2c49
-SRCS    =       zone2ldap.c zonetodb.c zone2sqlite.c
230545
+SRCS    =       zone2ldap.c zonetodb.c zone2sqlite.c ldap2zone.c
2e2c49
 
2e2c49
 MANPAGES =      zone2ldap.1
2e2c49
 
230545
@@ -47,6 +47,9 @@ EXT_CFLAGS =
230545
 zone2ldap@EXEEXT@: zone2ldap.@O@ ${DEPLIBS}
230545
 	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ zone2ldap.@O@ -lldap -llber ${LIBS}
2e2c49
 
2e2c49
+ldap2zone@EXEEXT@: ldap2zone.@O@ ${DEPLIBS}
2e2c49
+	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ ldap2zone.@O@ -lldap -llber ${LIBS}
2e2c49
+
230545
 zonetodb@EXEEXT@: zonetodb.@O@  ${DEPLIBS}
230545
 	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ zonetodb.@O@ -lpq ${LIBS}
2e2c49
 
230545
@@ -64,4 +67,5 @@ install:: ${TARGETS} installdirs
2e2c49
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2ldap@EXEEXT@ ${DESTDIR}${sbindir}
2e2c49
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zonetodb@EXEEXT@  ${DESTDIR}${sbindir}
2e2c49
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2sqlite@EXEEXT@ ${DESTDIR}${sbindir}
230545
+	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} ldap2zone@EXEEXT@ ${DESTDIR}${sbindir}
2e2c49
 	${INSTALL_DATA} ${srcdir}/zone2ldap.1 ${DESTDIR}${mandir}/man1/zone2ldap.1
2e2c49
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
230545
index e0e9207..d59936c 100644
2e2c49
--- a/bin/sdb_tools/zone2ldap.c
2e2c49
+++ b/bin/sdb_tools/zone2ldap.c
230545
@@ -73,7 +73,7 @@ void add_ldap_values (ldap_info * ldinfo);
230545
 void init_ldap_conn (void);
2e2c49
 
230545
 /* Ldap error checking */
230545
-void ldap_result_check (const char *msg, char *dn, int err);
230545
+void ldap_result_check (const char *msg, const char *dn, int err);
2e2c49
 
230545
 /* Put a hostname into a char ** array */
230545
 char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);
230545
@@ -82,7 +82,7 @@ char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);
2e2c49
 int get_attr_list_size (char **tmp);
2e2c49
 
2e2c49
 /* Get a DN */
2e2c49
-char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag);
2e2c49
+char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
2e2c49
 
2e2c49
 /* Add to RR list */
2e2c49
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
230545
@@ -104,11 +104,26 @@ void
2e2c49
 init_ldap_conn ();
2e2c49
 void usage();
2e2c49
 
2e2c49
-char *argzone, *ldapbase, *binddn, *bindpw = NULL;
2e2c49
-const char *ldapsystem = "localhost";
2e2c49
-static const char *objectClasses[] =
2e2c49
-  { "top", "dNSZone", NULL };
2e2c49
-static const char *topObjectClasses[] = { "top", NULL };
2e2c49
+static char *argzone, *ldapbase, *binddn, *bindpw = NULL;
2e2c49
+
2e2c49
+/* these are needed to placate gcc4's const-ness const-ernations : */
2e2c49
+static char localhost[] = "localhost";
2e2c49
+static char *ldapsystem=&(localhost[0]);
2e2c49
+/* dnszone schema class names: */
2e2c49
+static char topClass    [] ="top";
2e2c49
+static char dNSZoneClass[] ="dNSZone";
2e2c49
+static char objectClass [] ="objectClass";
2e2c49
+static char dcObjectClass[]="dcObject";
2e2c49
+/* dnszone schema attribute names: */
2e2c49
+static char relativeDomainName[]="relativeDomainName";
2e2c49
+static char dNSTTL            []="dNSTTL";
2e2c49
+static char zoneName          []="zoneName";
2e2c49
+static char dc                []="dc";
2e2c49
+static char sameZone          []="@";
2e2c49
+/* LDAPMod mod_values: */
2e2c49
+static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
2e2c49
+static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
2e2c49
+static char *dn_buffer      [64]={NULL};
2e2c49
 LDAP *conn;
2e2c49
 unsigned int debug = 0;
2e2c49
 
230545
@@ -120,7 +135,7 @@ static void
230545
 fatal(const char *msg) {
230545
   perror(msg);
230545
   if (conn != NULL)
230545
-    ldap_unbind_s(conn);
230545
+    ldap_unbind_ext_s(conn, NULL, NULL);
230545
   exit(1);
230545
 }
230545
 
230545
@@ -132,12 +147,13 @@ main (int argc, char **argv)
2e2c49
   isc_result_t result;
2e2c49
   char *basedn;
2e2c49
   ldap_info *tmp;
2e2c49
-  LDAPMod *base_attrs[2];
2e2c49
-  LDAPMod base;
2e2c49
+  LDAPMod *base_attrs[5];
2e2c49
+  LDAPMod base, dcBase, znBase, rdnBase;
2e2c49
   isc_buffer_t buff;
2e2c49
   char *zonefile=0L;
2e2c49
   char fullbasedn[1024];
230545
   char *ctmp;
230545
+  char *zn, *dcp[2], *znp[2], *rdn[2];
2e2c49
   dns_fixedname_t fixedzone, fixedname;
2e2c49
   dns_rdataset_t rdataset;
2e2c49
   char **dc_list;
230545
@@ -150,7 +166,7 @@ main (int argc, char **argv)
2e2c49
   extern char *optarg;
2e2c49
   extern int optind, opterr, optopt;
2e2c49
   int create_base = 0;
2e2c49
-  int topt;
2e2c49
+  int topt, dcn, zdn, znlen;
2e2c49
 
2e2c49
   if (argc < 2)
2e2c49
     {
230545
@@ -158,7 +174,7 @@ main (int argc, char **argv)
2e2c49
       exit (-1);
2e2c49
     }
2e2c49
 
2e2c49
-  while ((topt = getopt (argc, argv, "D:w:b:z:f:h:?dcv")) != -1)
2e2c49
+  while ((topt = getopt (argc, argv, "D:Ww:b:z:f:h:?dcv")) != -1)
2e2c49
     {
2e2c49
       switch (topt)
2e2c49
 	{
230545
@@ -181,6 +197,9 @@ main (int argc, char **argv)
2e2c49
 	  if (bindpw == NULL)
2e2c49
 	    fatal("strdup");
2e2c49
 	  break;
2e2c49
+	case 'W':
2e2c49
+	  bindpw = getpass("Enter LDAP Password: ");
2e2c49
+	  break;
2e2c49
 	case 'b':
2e2c49
 	  ldapbase = strdup (optarg);
2e2c49
 	  if (ldapbase == NULL)
230545
@@ -302,17 +321,51 @@ main (int argc, char **argv)
2e2c49
 	printf ("Creating base zone DN %s\n", argzone);
230545
 
2e2c49
       dc_list = hostname_to_dn_list (argzone, argzone, DNS_TOP);
2e2c49
-      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC);
2e2c49
+      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC, argzone);
2e2c49
+      if (debug)
2e2c49
+	printf ("base DN %s\n", basedn);
230545
 
230545
-      for (ctmp = &basedn[strlen (basedn)]; ctmp >= &basedn[0]; ctmp--)
2e2c49
+      for (ctmp = &basedn[strlen (basedn)], dcn=0; ctmp >= &basedn[0]; ctmp--)
2e2c49
 	{
230545
 	  if ((*ctmp == ',') || (ctmp == &basedn[0]))
2e2c49
 	    {
2e2c49
 	      base.mod_op = LDAP_MOD_ADD;
2e2c49
-	      base.mod_type = (char*)"objectClass";
2e2c49
+	      base.mod_type = objectClass;
230545
 	      base.mod_values = (char**)topObjectClasses;
2e2c49
 	      base_attrs[0] = (void*)&bas;;
2e2c49
-	      base_attrs[1] = NULL;
230545
+
2e2c49
+	      dcBase.mod_op = LDAP_MOD_ADD;
2e2c49
+	      dcBase.mod_type = dc;
2e2c49
+	      dcp[0]=dc_list[dcn];
2e2c49
+	      dcp[1]=0L;
2e2c49
+	      dcBase.mod_values=dcp;
2e2c49
+	      base_attrs[1] = (void*)&dcBase;
2e2c49
+
2e2c49
+	      znBase.mod_op = LDAP_MOD_ADD;
230545
+	      znBase.mod_type = zoneName;
2e2c49
+	      for( zdn = dcn, znlen = 0; zdn >= 0; zdn-- )
2e2c49
+		  znlen += strlen(dc_list[zdn])+1;
2e2c49
+	      znp[0] = (char*)malloc(znlen+1);
2e2c49
+	      znp[1] = 0L;
230545
+	      for( zdn = dcn, zn=znp[0]; zdn >= 0; zdn-- )
230545
+		  zn+=sprintf(zn,"%s%s",dc_list[zdn],
2e2c49
+			      ((zdn > 0) && (*(dc_list[zdn-1])!='.')) ? "." : ""
2e2c49
+		             );
2e2c49
+
2e2c49
+	      znBase.mod_values = znp;
2e2c49
+	      base_attrs[2] = (void*)&znBase;
2e2c49
+
2e2c49
+	      rdnBase.mod_op = LDAP_MOD_ADD;
2e2c49
+	      rdnBase.mod_type = relativeDomainName;
2e2c49
+	      rdn[0] = strdup(sameZone);
2e2c49
+	      rdn[1] = 0L;
2e2c49
+	      rdnBase.mod_values = rdn;
2e2c49
+	      base_attrs[3] = (void*)&rdnBase;
230545
+
2e2c49
+	      dcn++;
2e2c49
+
2e2c49
+	      base.mod_values = topObjectClasses;
230545
+	      base_attrs[4] = NULL;
230545
 
2e2c49
 	      if (ldapbase)
2e2c49
 		{
230545
@@ -329,6 +382,10 @@ main (int argc, char **argv)
2e2c49
 		  else
2e2c49
 		    sprintf (fullbasedn, "%s", ctmp);
2e2c49
 		}
2e2c49
+
2e2c49
+	      if( debug )
2e2c49
+		  printf("Full base dn: %s\n", fullbasedn);
2e2c49
+
2e2c49
 	      result = ldap_add_s (conn, fullbasedn, base_attrs);
230545
 	      ldap_result_check ("initial ldap_add_s", fullbasedn, result);
2e2c49
 	    }
230545
@@ -408,14 +465,14 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
2e2c49
   isc_result_check (result, "dns_rdata_totext");
2e2c49
   data[isc_buffer_usedlength (&buff)] = 0;
2e2c49
 
2e2c49
-  dc_list = hostname_to_dn_list (name, argzone, DNS_OBJECT);
2e2c49
+  dc_list = hostname_to_dn_list ((char*)name, argzone, DNS_OBJECT);
2e2c49
   len = (get_attr_list_size (dc_list) - 2);
2e2c49
-  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC);
2e2c49
+  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC, argzone);
2e2c49
 
2e2c49
   if (debug)
2e2c49
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
2e2c49
 
2e2c49
-  add_to_rr_list (dn, dc_list[len], type, data, ttl, DNS_OBJECT);
2e2c49
+  add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
2e2c49
 }
2e2c49
 
2e2c49
 
230545
@@ -455,7 +512,8 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
   int attrlist;
2e2c49
   char ldap_type_buffer[128];
2e2c49
   char charttl[64];
2e2c49
-
2e2c49
+  char *zn;
2e2c49
+  int znlen;
2e2c49
 
2e2c49
   if ((tmp = locate_by_dn (dn)) == NULL)
2e2c49
     {
230545
@@ -482,10 +540,10 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
 	    fatal("malloc");
2e2c49
 	}
2e2c49
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
2e2c49
-      tmp->attrs[0]->mod_type = (char*)"objectClass";
2e2c49
+      tmp->attrs[0]->mod_type = objectClass;
2e2c49
 
2e2c49
       if (flags == DNS_OBJECT)
2e2c49
-	tmp->attrs[0]->mod_values = (char**)objectClasses;
2e2c49
+	tmp->attrs[0]->mod_values = objectClasses;
2e2c49
       else
2e2c49
 	{
230545
 	  tmp->attrs[0]->mod_values = (char**)topObjectClasses;
230545
@@ -497,7 +555,7 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
 	}
2e2c49
 
2e2c49
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
2e2c49
-      tmp->attrs[1]->mod_type = (char*)"relativeDomainName";
2e2c49
+      tmp->attrs[1]->mod_type = relativeDomainName;
2e2c49
       tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
2e2c49
 
2e2c49
       if (tmp->attrs[1]->mod_values == (char **)NULL)
230545
@@ -526,7 +584,7 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
 	 fatal("strdup");
2e2c49
 
2e2c49
       tmp->attrs[3]->mod_op = LDAP_MOD_ADD;
2e2c49
-      tmp->attrs[3]->mod_type = (char*)"dNSTTL";
2e2c49
+      tmp->attrs[3]->mod_type = dNSTTL;
2e2c49
       tmp->attrs[3]->mod_values = (char **) calloc (sizeof (char *), 2);
2e2c49
 
2e2c49
       if (tmp->attrs[3]->mod_values == (char **)NULL)
230545
@@ -539,14 +597,25 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
       if (tmp->attrs[3]->mod_values[0] == NULL)
2e2c49
 	 fatal("strdup");
2e2c49
 
230545
+      znlen=strlen(gbl_zone);
230545
+      if ( gbl_zone[znlen-1] == '.' )
2e2c49
+      { /* ldapdb MUST search by relative zone name */
2e2c49
+	  zn = (char*)malloc(znlen);
230545
+	  memcpy(zn, gbl_zone, znlen-1);
230545
+	  zn[znlen-1]='\0';
2e2c49
+      }else
2e2c49
+      {
2e2c49
+	  zn = gbl_zone;
2e2c49
+      }
2e2c49
+
2e2c49
       tmp->attrs[4]->mod_op = LDAP_MOD_ADD;
2e2c49
-      tmp->attrs[4]->mod_type = (char*)"zoneName";
2e2c49
+      tmp->attrs[4]->mod_type = zoneName;
2e2c49
       tmp->attrs[4]->mod_values = (char **)calloc(sizeof(char *), 2);
2e2c49
 
2e2c49
       if (tmp->attrs[4]->mod_values == (char **)NULL)
2e2c49
 	 fatal("calloc");
2e2c49
 
2e2c49
-      tmp->attrs[4]->mod_values[0] = gbl_zone;
2e2c49
+      tmp->attrs[4]->mod_values[0] = zn;
2e2c49
       tmp->attrs[4]->mod_values[1] = NULL;
2e2c49
 
2e2c49
       tmp->attrs[5] = NULL;
230545
@@ -557,7 +626,7 @@ add_to_rr_list (char *dn, char *name, char *type,
2e2c49
   else
2e2c49
     {
2e2c49
 
2e2c49
-      for (i = 0; tmp->attrs[i] != NULL; i++)
2e2c49
+	for (i = 0; tmp->attrs[i] != NULL; i++)
2e2c49
 	{
2e2c49
 	  sprintf (ldap_type_buffer, "%sRecord", type);
2e2c49
 	  if (!strncmp
230545
@@ -631,44 +700,70 @@ char **
2e2c49
 hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
2e2c49
 {
2e2c49
   char *tmp;
2e2c49
-  static char *dn_buffer[64];
2e2c49
   int i = 0;
2e2c49
-  char *zname;
2e2c49
-  char *hnamebuff;
2e2c49
-
2e2c49
-  zname = strdup (hostname);
2e2c49
-  if (zname == NULL)
2e2c49
-	fatal("strdup");
2e2c49
-
2e2c49
-  if (flags == DNS_OBJECT)
2e2c49
-    {
2e2c49
-
2e2c49
-      if (strlen (zname) != strlen (zone))
2e2c49
-	{
2e2c49
-	  tmp = &zname[strlen (zname) - strlen (zone)];
2e2c49
-	  *--tmp = '\0';
2e2c49
-	  hnamebuff = strdup (zname);
2e2c49
-	  if (hnamebuff == NULL)
2e2c49
-		fatal("strdup");
2e2c49
-	  zname = ++tmp;
2e2c49
-	}
2e2c49
-      else
2e2c49
-	hnamebuff = (char*)"@";
2e2c49
-    }
2e2c49
-  else
2e2c49
-    {
2e2c49
-      zname = zone;
2e2c49
-      hnamebuff = NULL;
2e2c49
-    }
2e2c49
-
2e2c49
-  for (tmp = strrchr (zname, '.'); tmp != (char *) 0;
2e2c49
-       tmp = strrchr (zname, '.'))
2e2c49
-    {
2e2c49
-      *tmp++ = '\0';
2e2c49
-      dn_buffer[i++] = tmp;
2e2c49
-    }
2e2c49
-  dn_buffer[i++] = zname;
2e2c49
-  dn_buffer[i++] = hnamebuff;
2e2c49
+  char *hname=0L, *last=0L;
2e2c49
+  int hlen=strlen(hostname), zlen=(strlen(zone));
2e2c49
+
2e2c49
+/*  printf("hostname: %s zone: %s\n",hostname, zone); */
2e2c49
+  hname=0L;
2e2c49
+  if(flags == DNS_OBJECT)
2e2c49
+  {
2e2c49
+      if( (zone[ zlen - 1 ] == '.') && (hostname[hlen - 1] != '.') )
2e2c49
+      {
2e2c49
+	  hname=(char*)malloc(hlen + 1);
2e2c49
+	  hlen += 1;
2e2c49
+	  sprintf(hname, "%s.", hostname);
2e2c49
+	  hostname = hname;
2e2c49
+      }
2e2c49
+      if(strcmp(hostname, zone) == 0)
2e2c49
+      {
2e2c49
+	  if( hname == 0 )
2e2c49
+	      hname=strdup(hostname);
230545
+	  last = strdup(sameZone);
2e2c49
+      }else
230545
+      {
230545
+	  if(  (hlen < zlen)
2e2c49
+	     ||( strcmp( hostname + (hlen - zlen), zone ) != 0)
2e2c49
+	    )
2e2c49
+	  {
2e2c49
+	      if( hname != 0 )
2e2c49
+		  free(hname);
2e2c49
+	      hname=(char*)malloc( hlen + zlen + 1);
2e2c49
+	      if( *zone == '.' )
2e2c49
+		  sprintf(hname, "%s%s", hostname, zone);
2e2c49
+	      else
2e2c49
+		  sprintf(hname,"%s",zone);
2e2c49
+	  }else
2e2c49
+	  {
2e2c49
+	      if( hname == 0 )
2e2c49
+		  hname = strdup(hostname);
2e2c49
+	  }
2e2c49
+	  last = hname;
2e2c49
+      }
2e2c49
+  }else
2e2c49
+  { /* flags == DNS_TOP */
2e2c49
+      hname = strdup(zone);
2e2c49
+      last = hname;
2e2c49
+  }
2e2c49
+
2e2c49
+  for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
2e2c49
+       tmp = strrchr (hname, '.'))
2e2c49
+  {
2e2c49
+      if( *( tmp + 1 ) != '\0' )
2e2c49
+      {
2e2c49
+	  *tmp = '\0';
2e2c49
+	  dn_buffer[i++] = ++tmp;
2e2c49
+      }else
2e2c49
+      { /* trailing '.' ! */
2e2c49
+	  dn_buffer[i++] = strdup(".");
2e2c49
+	  *tmp = '\0';
2e2c49
+	  if( tmp == hname )
2e2c49
+	      break;
230545
+      }
2e2c49
+  }
2e2c49
+  if( ( last != hname ) && (tmp != hname) )
2e2c49
+      dn_buffer[i++] = hname;
2e2c49
+  dn_buffer[i++] = last;
2e2c49
   dn_buffer[i] = NULL;
2e2c49
 
2e2c49
   return dn_buffer;
230545
@@ -680,30 +775,38 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
2e2c49
  * exception of "@"/SOA. */
2e2c49
 
2e2c49
 char *
2e2c49
-build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag)
2e2c49
+build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
2e2c49
 {
2e2c49
   int size;
2e2c49
-  int x;
230545
-  static char dn[1024];
230545
-  char tmp[128];
2e2c49
+  int x, znlen;
230545
+  static char dn[DNS_NAME_MAXTEXT*3/2];
230545
+  char tmp[DNS_NAME_MAXTEXT*3/2];
2e2c49
+  char zn[DNS_NAME_MAXTEXT+1];
2e2c49
 
2e2c49
   bzero (tmp, sizeof (tmp));
2e2c49
   bzero (dn, sizeof (dn));
2e2c49
   size = get_attr_list_size (dc_list);
2e2c49
+  znlen = strlen(zone);
230545
+  if ( zone[znlen-1] == '.' )
2e2c49
+  { /* ldapdb MUST search by relative zone name */
2e2c49
+      memcpy(&(zn[0]),zone,znlen-1);
230545
+      zn[znlen-1]='\0';
2e2c49
+      zone = zn;
2e2c49
+  }
2e2c49
   for (x = size - 2; x > 0; x--)
2e2c49
     {
2e2c49
     if (flag == WI_SPEC)
2e2c49
     {
2e2c49
       if (x == (size - 2) && (strncmp (dc_list[x], "@", 1) == 0) && (ttl))
10d019
-	sprintf (tmp, "relativeDomainName=%s + dNSTTL=%u,", dc_list[x], ttl);
230545
+	snprintf (tmp, sizeof(tmp), "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
2e2c49
       else if (x == (size - 2))
2e2c49
-	      sprintf(tmp, "relativeDomainName=%s,",dc_list[x]);
230545
+	      snprintf(tmp, sizeof(tmp), "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
2e2c49
       else
230545
-	      sprintf(tmp,"dc=%s,", dc_list[x]);
230545
+	      snprintf(tmp, sizeof(tmp), "dc=%s,", dc_list[x]);
2e2c49
     }
230545
     else
2e2c49
     {
230545
-	    sprintf(tmp, "dc=%s,", dc_list[x]);
230545
+	    snprintf(tmp, sizeof(tmp), "dc=%s,", dc_list[x]);
230545
     }
230545
 
230545
 
230545
@@ -732,19 +835,18 @@ init_ldap_conn ()
2e2c49
     }
2e2c49
 
2e2c49
   result = ldap_simple_bind_s (conn, binddn, bindpw);
2e2c49
-  ldap_result_check ("ldap_simple_bind_s", (char*)"LDAP Bind", result);
230545
+  ldap_result_check ("ldap_simple_bind_s", "LDAP Bind", result);
2e2c49
 }
2e2c49
 
2e2c49
 /* Like isc_result_check, only for LDAP */
230545
 void
230545
-ldap_result_check (const char *msg, char *dn, int err)
230545
+ldap_result_check (const char *msg, const char *dn, int err)
230545
 {
230545
   if ((err != LDAP_SUCCESS) && (err != LDAP_ALREADY_EXISTS))
230545
     {
230545
-      fprintf(stderr, "Error while adding %s (%s):\n",
230545
-		      dn, msg);
230545
-      ldap_perror (conn, dn);
230545
-      ldap_unbind_s (conn);
230545
+      fprintf(stderr, "Error while adding %s (%s):\n%s",
230545
+		      dn, msg, ldap_err2string(err));
230545
+      ldap_unbind_ext_s (conn, NULL, NULL);
230545
       exit (-1);
2e2c49
     }
2e2c49
 }
230545
@@ -758,16 +860,15 @@ add_ldap_values (ldap_info * ldinfo)
2e2c49
   int result;
2e2c49
   char dnbuffer[1024];
2e2c49
 
2e2c49
-
2e2c49
   if (ldapbase != NULL)
2e2c49
     sprintf (dnbuffer, "%s,%s", ldinfo->dn, ldapbase);
2e2c49
   else
2e2c49
     sprintf (dnbuffer, "%s", ldinfo->dn);
2e2c49
 
2e2c49
   result = ldap_add_s (conn, dnbuffer, ldinfo->attrs);
2e2c49
-  ldap_result_check ("ldap_add_s", dnbuffer, result);
230545
-}
2e2c49
+    ldap_result_check ("ldap_add_s", dnbuffer, result);
230545
 
230545
+}
230545
 
2e2c49
 
2e2c49
 
230545
@@ -776,5 +877,5 @@ void
2e2c49
 usage ()
2e2c49
 {
2e2c49
   fprintf (stderr,
2e2c49
-	   "zone2ldap -D [BIND DN] -w [BIND PASSWORD] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
2e2c49
+	   "zone2ldap -D [BIND DN] [-w BIND PASSWORD | -W:prompt] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
2e2c49
 	   "[-c Create LDAP Base structure][-d Debug Output (lots !)] \n ");}