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