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