Tomas Hozza 3fed71
diff --git a/bin/sdb_tools/Makefile.in b/bin/sdb_tools/Makefile.in
Tomas Hozza 3fed71
index 95ab742..6069f09 100644
Tomas Hozza 3fed71
--- a/bin/sdb_tools/Makefile.in
Tomas Hozza 3fed71
+++ b/bin/sdb_tools/Makefile.in
Tomas Hozza 3fed71
@@ -32,11 +32,11 @@ DEPLIBS =	${LWRESDEPLIBS} ${DNSDEPLIBS} ${BIND9DEPLIBS} \
jvdias 207484
 LIBS =		${LWRESLIBS} ${DNSLIBS} ${BIND9LIBS} \
jvdias 207484
 		${ISCCFGLIBS} ${ISCCCLIBS} ${ISCLIBS} ${DBDRIVER_LIBS} @LIBS@
jvdias 207484
 
Tomas Hozza 4fa997
-TARGETS =	zone2ldap@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@
Tomas Hozza 4fa997
+TARGETS =	zone2ldap@EXEEXT@ ldap2zone@EXEEXT@ zonetodb@EXEEXT@ zone2sqlite@EXEEXT@
jvdias 207484
 
Tomas Hozza 4fa997
-OBJS	=	zone2ldap.@O@ zonetodb.@O@ zone2sqlite.@O@
Tomas Hozza 4fa997
+OBJS	=	zone2ldap.@O@ ldap2zone.@O@ zonetodb.@O@ zone2sqlite.@O@
jvdias 207484
 
Tomas Hozza 4fa997
-SRCS    =       zone2ldap.c zonetodb.c zone2sqlite.c
Tomas Hozza 4fa997
+SRCS    =       zone2ldap.c ldap2zone.c zonetodb.c zone2sqlite.c
jvdias 207484
 
jvdias 207484
 MANPAGES =      zone2ldap.1
jvdias 207484
 
Tomas Hozza 3fed71
@@ -53,6 +53,9 @@ zonetodb@EXEEXT@: zonetodb.@O@  ${DEPLIBS}
Tomas Hozza 4fa997
 zone2sqlite@EXEEXT@: zone2sqlite.@O@  ${DEPLIBS}
Tomas Hozza 4fa997
 	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ zone2sqlite.@O@ -lsqlite3 -lssl ${LIBS}
jvdias 207484
 
Adam Tkac 1e57f4
+ldap2zone@EXEEXT@: ldap2zone.@O@ ${DEPLIBS}
Adam Tkac 5ca594
+	${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${ALL_CFLAGS} ${LDFLAGS} -o $@ ldap2zone.@O@ -lldap -llber ${LIBS}
jvdias 207484
+
jvdias 207484
 clean distclean manclean maintainer-clean::
jvdias 207484
 	rm -f ${TARGETS} ${OBJS}
jvdias 207484
 
Tomas Hozza 4fa997
@@ -62,6 +65,7 @@ installdirs:
jvdias 207484
 
jvdias 207484
 install:: ${TARGETS} installdirs
Adam Tkac 5ca594
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2ldap@EXEEXT@ ${DESTDIR}${sbindir}
Adam Tkac 5ca594
+	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} ldap2zone@EXEEXT@ ${DESTDIR}${sbindir}
Adam Tkac 5ca594
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zonetodb@EXEEXT@  ${DESTDIR}${sbindir}
Tomas Hozza 4fa997
 	${LIBTOOL_MODE_INSTALL} ${INSTALL_PROGRAM} zone2sqlite@EXEEXT@ ${DESTDIR}${sbindir}
jvdias 207484
 	${INSTALL_DATA} ${srcdir}/zone2ldap.1 ${DESTDIR}${mandir}/man1/zone2ldap.1
Tomas Hozza 3fed71
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
2a4663
index aa2c711..76186b5 100644
Tomas Hozza 3fed71
--- a/bin/sdb_tools/zone2ldap.c
Tomas Hozza 3fed71
+++ b/bin/sdb_tools/zone2ldap.c
2a4663
@@ -66,6 +66,9 @@ ldap_info;
jvdias 207484
 /* usage Info */
jvdias 207484
 void usage (void);
jvdias 207484
 
jvdias 207484
+/* Check for existence of (and possibly add) containing dNSZone objects */
jvdias 207484
+int lookup_dns_zones( ldap_info *ldinfo);
jvdias 207484
+
jvdias 207484
 /* Add to the ldap dit */
jvdias 207484
 void add_ldap_values (ldap_info * ldinfo);
jvdias 207484
 
2a4663
@@ -82,7 +85,7 @@ char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);
jvdias 207484
 int get_attr_list_size (char **tmp);
jvdias 207484
 
jvdias 207484
 /* Get a DN */
jvdias 207484
-char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag);
jvdias 207484
+char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
jvdias 207484
 
jvdias 207484
 /* Add to RR list */
jvdias 207484
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
2a4663
@@ -104,11 +107,27 @@ void
jvdias 207484
 init_ldap_conn ();
jvdias 207484
 void usage();
jvdias 207484
 
jvdias 207484
-char *argzone, *ldapbase, *binddn, *bindpw = NULL;
jvdias 207484
-const char *ldapsystem = "localhost";
jvdias 207484
-static const char *objectClasses[] =
jvdias 207484
-  { "top", "dNSZone", NULL };
jvdias 207484
-static const char *topObjectClasses[] = { "top", NULL };
jvdias 207484
+static char *argzone, *ldapbase, *binddn, *bindpw = NULL;
jvdias 207484
+
jvdias 207484
+/* these are needed to placate gcc4's const-ness const-ernations : */
jvdias 207484
+static char localhost[] = "localhost";
jvdias 207484
+static char *ldapsystem=&(localhost[0]);
jvdias 207484
+/* dnszone schema class names: */
jvdias 207484
+static char topClass    [] ="top";
jvdias 207484
+static char dNSZoneClass[] ="dNSZone";
jvdias 207484
+static char objectClass [] ="objectClass";
jvdias 207484
+static char dcObjectClass[]="dcObject";
jvdias 207484
+/* dnszone schema attribute names: */
jvdias 207484
+static char relativeDomainName[]="relativeDomainName";
jvdias 207484
+static char dNSTTL            []="dNSTTL";
jvdias 207484
+static char zoneName          []="zoneName";
jvdias 207484
+static char dc                []="dc";
jvdias 207484
+static char sameZone          []="@";
jvdias 207484
+/* LDAPMod mod_values: */
jvdias 207484
+static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
jvdias 207484
+static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
jvdias 207484
+static char *dn_buffer      [64]={NULL};
jvdias 207484
+
jvdias 207484
 LDAP *conn;
jvdias 207484
 unsigned int debug = 0;
jvdias 207484
 
2a4663
@@ -132,12 +151,12 @@ main (int argc, char **argv)
jvdias 207484
   isc_result_t result;
jvdias 207484
   char *basedn;
jvdias 207484
   ldap_info *tmp;
jvdias 207484
-  LDAPMod *base_attrs[2];
jvdias 207484
-  LDAPMod base;
jvdias 207484
+  LDAPMod *base_attrs[5];
jvdias 207484
+  LDAPMod base, dcBase, znBase, rdnBase;
jvdias 207484
   isc_buffer_t buff;
jvdias 207484
   char *zonefile=0L;
jvdias 207484
   char fullbasedn[1024];
jvdias 207484
-  char *ctmp;
jvdias 207484
+  char *ctmp, *zn, *dcp[2], *znp[2], *rdn[2];
jvdias 207484
   dns_fixedname_t fixedzone, fixedname;
jvdias 207484
   dns_rdataset_t rdataset;
jvdias 207484
   char **dc_list;
2a4663
@@ -150,7 +169,7 @@ main (int argc, char **argv)
jvdias 207484
   extern char *optarg;
jvdias 207484
   extern int optind, opterr, optopt;
jvdias 207484
   int create_base = 0;
jvdias 207484
-  int topt;
jvdias 207484
+  int topt, dcn, zdn, znlen;
jvdias 207484
 
Tomas Hozza 3fed71
   if (argc < 2)
jvdias 207484
     {
2a4663
@@ -158,7 +177,7 @@ main (int argc, char **argv)
jvdias 207484
       exit (-1);
jvdias 207484
     }
jvdias 207484
 
Tomas Hozza 3fed71
-  while ((topt = getopt (argc, argv, "D:w:b:z:f:h:?dcv")) != -1)
Tomas Hozza 3fed71
+  while ((topt = getopt (argc, argv, "D:Ww:b:z:f:h:?dcv")) != -1)
jvdias 207484
     {
jvdias 207484
       switch (topt)
jvdias 207484
 	{
2a4663
@@ -181,6 +200,9 @@ main (int argc, char **argv)
Tomas Hozza 3fed71
 	  if (bindpw == NULL)
Tomas Hozza 3fed71
 	    fatal("strdup");
jvdias 207484
 	  break;
jvdias 207484
+	case 'W':
jvdias 207484
+	  bindpw = getpass("Enter LDAP Password: ");
jvdias 207484
+	  break;
jvdias 207484
 	case 'b':
Tomas Hozza 3fed71
 	  ldapbase = strdup (optarg);
Tomas Hozza 3fed71
 	  if (ldapbase == NULL)
2a4663
@@ -300,27 +322,62 @@ main (int argc, char **argv)
jvdias 207484
     {
jvdias 207484
       if (debug)
jvdias 207484
 	printf ("Creating base zone DN %s\n", argzone);
jvdias 207484
-
jvdias 207484
+      
jvdias 207484
       dc_list = hostname_to_dn_list (argzone, argzone, DNS_TOP);
jvdias 207484
-      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC);
jvdias 207484
 
jvdias 207484
-      for (ctmp = &basedn[strlen (basedn)]; ctmp >= &basedn[0]; ctmp--)
jvdias 207484
+      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC, argzone);
jvdias 207484
+      if (debug)
jvdias 207484
+	printf ("base DN %s\n", basedn);
jvdias 207484
+
jvdias 207484
+      for (ctmp = &basedn[strlen (basedn)], dcn=0; ctmp >= &basedn[0]; ctmp--)
jvdias 207484
 	{
jvdias 207484
-	  if ((*ctmp == ',') || (ctmp == &basedn[0]))
jvdias 207484
+	    if ((*ctmp == ',') || (ctmp == &basedn[0]))
jvdias 207484
 	    {
jvdias 207484
+
jvdias 207484
 	      base.mod_op = LDAP_MOD_ADD;
jvdias 207484
-	      base.mod_type = (char*)"objectClass";
jvdias 207484
-	      base.mod_values = (char**)topObjectClasses;
jvdias 207484
+	      base.mod_type = objectClass;
jvdias 207484
+	      base.mod_values = topObjectClasses;
jvdias 207484
 	      base_attrs[0] = (void*)&bas;;
jvdias 207484
-	      base_attrs[1] = NULL;
jvdias 207484
-
jvdias 207484
+	     
jvdias 207484
+	      dcBase.mod_op = LDAP_MOD_ADD;
jvdias 207484
+	      dcBase.mod_type = dc;
jvdias 207484
+	      dcp[0]=dc_list[dcn];
jvdias 207484
+	      dcp[1]=0L;
jvdias 207484
+	      dcBase.mod_values=dcp;
jvdias 207484
+	      base_attrs[1] = (void*)&dcBase;
jvdias 207484
+
jvdias 207484
+	      znBase.mod_op = LDAP_MOD_ADD;
jvdias 207484
+	      znBase.mod_type = zoneName;	      
jvdias 207484
+	      for( zdn = dcn, znlen = 0; zdn >= 0; zdn-- )
jvdias 207484
+		  znlen += strlen(dc_list[zdn])+1;
jvdias 207484
+	      znp[0] = (char*)malloc(znlen+1);
jvdias 207484
+	      znp[1] = 0L;
jvdias 207484
+	      for( zdn = dcn, zn=znp[0]; zdn >= 0; zdn-- )		  
jvdias 207484
+		  zn+=sprintf(zn,"%s%s",dc_list[zdn], 
jvdias 207484
+			      ((zdn > 0) && (*(dc_list[zdn-1])!='.')) ? "." : ""
jvdias 207484
+		             );
jvdias 207484
+
jvdias 207484
+	      znBase.mod_values = znp;
jvdias 207484
+	      base_attrs[2] = (void*)&znBase;
jvdias 207484
+
jvdias 207484
+	      rdnBase.mod_op = LDAP_MOD_ADD;
jvdias 207484
+	      rdnBase.mod_type = relativeDomainName;
jvdias 207484
+	      rdn[0] = strdup(sameZone);
jvdias 207484
+	      rdn[1] = 0L;
jvdias 207484
+	      rdnBase.mod_values = rdn;
jvdias 207484
+	      base_attrs[3] = (void*)&rdnBase;
jvdias 207484
+	      
jvdias 207484
+	      dcn++;
jvdias 207484
+
jvdias 207484
+	      base.mod_values = topObjectClasses;
jvdias 207484
+	      base_attrs[4] = NULL;	      
jvdias 207484
+	      
jvdias 207484
 	      if (ldapbase)
jvdias 207484
 		{
jvdias 207484
 		  if (ctmp != &basedn[0])
jvdias 207484
 		    sprintf (fullbasedn, "%s,%s", ctmp + 1, ldapbase);
jvdias 207484
 		  else
jvdias 207484
-		    sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);
jvdias 207484
-
jvdias 207484
+		    sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);		  
jvdias 207484
 		}
jvdias 207484
 	      else
jvdias 207484
 		{
2a4663
@@ -329,8 +386,13 @@ main (int argc, char **argv)
jvdias 207484
 		  else
jvdias 207484
 		    sprintf (fullbasedn, "%s", ctmp);
jvdias 207484
 		}
jvdias 207484
+
jvdias 207484
+	      if( debug )
jvdias 207484
+		  printf("Full base dn: %s\n", fullbasedn);
jvdias 207484
+
jvdias 207484
 	      result = ldap_add_s (conn, fullbasedn, base_attrs);
jvdias 207484
 	      ldap_result_check ("intial ldap_add_s", fullbasedn, result);
jvdias 207484
+
jvdias 207484
 	    }
jvdias 207484
 
jvdias 207484
 	}
2a4663
@@ -408,14 +470,14 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
jvdias 207484
   isc_result_check (result, "dns_rdata_totext");
jvdias 207484
   data[isc_buffer_usedlength (&buff)] = 0;
jvdias 207484
 
jvdias 207484
-  dc_list = hostname_to_dn_list (name, argzone, DNS_OBJECT);
jvdias 207484
+  dc_list = hostname_to_dn_list ((char*)name, argzone, DNS_OBJECT);
jvdias 207484
   len = (get_attr_list_size (dc_list) - 2);
jvdias 207484
-  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC);
jvdias 207484
+  dn = build_dn_from_dc_list (dc_list, ttl, WI_SPEC, argzone);
jvdias 207484
 
jvdias 207484
   if (debug)
jvdias 207484
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
jvdias 207484
 
jvdias 207484
-  add_to_rr_list (dn, dc_list[len], type, data, ttl, DNS_OBJECT);
jvdias 207484
+  add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
jvdias 207484
 }
jvdias 207484
 
jvdias 207484
 
2a4663
@@ -455,7 +517,8 @@ add_to_rr_list (char *dn, char *name, char *type,
jvdias 207484
   int attrlist;
jvdias 207484
   char ldap_type_buffer[128];
jvdias 207484
   char charttl[64];
jvdias 207484
-
jvdias 207484
+  char *zn;
jvdias 207484
+  int znlen;
jvdias 207484
 
jvdias 207484
   if ((tmp = locate_by_dn (dn)) == NULL)
jvdias 207484
     {
2a4663
@@ -482,13 +545,13 @@ add_to_rr_list (char *dn, char *name, char *type,
Tomas Hozza 3fed71
 	    fatal("malloc");
jvdias 207484
 	}
jvdias 207484
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
jvdias 207484
-      tmp->attrs[0]->mod_type = (char*)"objectClass";
jvdias 207484
+      tmp->attrs[0]->mod_type = objectClass;
jvdias 207484
 
jvdias 207484
       if (flags == DNS_OBJECT)
jvdias 207484
-	tmp->attrs[0]->mod_values = (char**)objectClasses;
jvdias 207484
+	tmp->attrs[0]->mod_values = objectClasses;
jvdias 207484
       else
jvdias 207484
 	{
jvdias 207484
-	  tmp->attrs[0]->mod_values = (char**)topObjectClasses;
jvdias 207484
+	  tmp->attrs[0]->mod_values =topObjectClasses;
jvdias 207484
 	  tmp->attrs[1] = NULL;
jvdias 207484
 	  tmp->attrcnt = 2;
jvdias 207484
 	  tmp->next = ldap_info_base;
2a4663
@@ -497,7 +560,7 @@ add_to_rr_list (char *dn, char *name, char *type,
jvdias 207484
 	}
jvdias 207484
 
jvdias 207484
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
jvdias 207484
-      tmp->attrs[1]->mod_type = (char*)"relativeDomainName";
jvdias 207484
+      tmp->attrs[1]->mod_type = relativeDomainName;
jvdias 207484
       tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
jvdias 207484
 
jvdias 207484
       if (tmp->attrs[1]->mod_values == (char **)NULL)
2a4663
@@ -526,7 +589,7 @@ add_to_rr_list (char *dn, char *name, char *type,
Tomas Hozza 3fed71
 	 fatal("strdup");
jvdias 207484
 
jvdias 207484
       tmp->attrs[3]->mod_op = LDAP_MOD_ADD;
jvdias 207484
-      tmp->attrs[3]->mod_type = (char*)"dNSTTL";
jvdias 207484
+      tmp->attrs[3]->mod_type = dNSTTL;
jvdias 207484
       tmp->attrs[3]->mod_values = (char **) calloc (sizeof (char *), 2);
jvdias 207484
 
jvdias 207484
       if (tmp->attrs[3]->mod_values == (char **)NULL)
2a4663
@@ -539,14 +602,25 @@ add_to_rr_list (char *dn, char *name, char *type,
Tomas Hozza 3fed71
       if (tmp->attrs[3]->mod_values[0] == NULL)
Tomas Hozza 3fed71
 	 fatal("strdup");
jvdias 207484
 
jvdias 207484
+      znlen=strlen(gbl_zone);      
jvdias 207484
+      if ( *(gbl_zone + (znlen-1)) == '.' )
jvdias 207484
+      { /* ldapdb MUST search by relative zone name */
jvdias 207484
+	  zn = (char*)malloc(znlen);
jvdias 207484
+	  strncpy(zn,gbl_zone,znlen-1);
jvdias 207484
+	  *(zn + (znlen-1))='\0';	  
jvdias 207484
+      }else
jvdias 207484
+      {
jvdias 207484
+	  zn = gbl_zone;
jvdias 207484
+      }
jvdias 207484
+
jvdias 207484
       tmp->attrs[4]->mod_op = LDAP_MOD_ADD;
jvdias 207484
-      tmp->attrs[4]->mod_type = (char*)"zoneName";
jvdias 207484
+      tmp->attrs[4]->mod_type = zoneName;
jvdias 207484
       tmp->attrs[4]->mod_values = (char **)calloc(sizeof(char *), 2);
Tomas Hozza 3fed71
 
Tomas Hozza 3fed71
       if (tmp->attrs[4]->mod_values == (char **)NULL)
Tomas Hozza 3fed71
 	 fatal("calloc");
Tomas Hozza 3fed71
 
jvdias 207484
-      tmp->attrs[4]->mod_values[0] = gbl_zone;
jvdias 207484
+      tmp->attrs[4]->mod_values[0] = zn;
jvdias 207484
       tmp->attrs[4]->mod_values[1] = NULL;
jvdias 207484
 
jvdias 207484
       tmp->attrs[5] = NULL;
2a4663
@@ -557,7 +631,7 @@ add_to_rr_list (char *dn, char *name, char *type,
jvdias 207484
   else
jvdias 207484
     {
jvdias 207484
 
jvdias 207484
-      for (i = 0; tmp->attrs[i] != NULL; i++)
jvdias 207484
+	for (i = 0; tmp->attrs[i] != NULL; i++)
jvdias 207484
 	{
jvdias 207484
 	  sprintf (ldap_type_buffer, "%sRecord", type);
jvdias 207484
 	  if (!strncmp
2a4663
@@ -631,44 +705,70 @@ char **
jvdias 207484
 hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
jvdias 207484
 {
jvdias 207484
   char *tmp;
jvdias 207484
-  static char *dn_buffer[64];
jvdias 207484
   int i = 0;
jvdias 207484
-  char *zname;
jvdias 207484
-  char *hnamebuff;
jvdias 207484
-
jvdias 207484
-  zname = strdup (hostname);
Tomas Hozza 3fed71
-  if (zname == NULL)
Tomas Hozza 3fed71
-	fatal("strdup");
jvdias 207484
-
jvdias 207484
-  if (flags == DNS_OBJECT)
jvdias 207484
-    {
Tomas Hozza 3fed71
-
jvdias 207484
-      if (strlen (zname) != strlen (zone))
jvdias 207484
-	{
jvdias 207484
-	  tmp = &zname[strlen (zname) - strlen (zone)];
jvdias 207484
-	  *--tmp = '\0';
jvdias 207484
-	  hnamebuff = strdup (zname);
Tomas Hozza 3fed71
-	  if (hnamebuff == NULL)
Tomas Hozza 3fed71
-		fatal("strdup");
jvdias 207484
-	  zname = ++tmp;
jvdias 207484
-	}
jvdias 207484
-      else
jvdias 207484
-	hnamebuff = (char*)"@";
jvdias 207484
-    }
jvdias 207484
-  else
jvdias 207484
-    {
jvdias 207484
-      zname = zone;
jvdias 207484
-      hnamebuff = NULL;
jvdias 207484
-    }
jvdias 207484
-
jvdias 207484
-  for (tmp = strrchr (zname, '.'); tmp != (char *) 0;
jvdias 207484
-       tmp = strrchr (zname, '.'))
jvdias 207484
-    {
jvdias 207484
-      *tmp++ = '\0';
jvdias 207484
-      dn_buffer[i++] = tmp;
jvdias 207484
-    }
jvdias 207484
-  dn_buffer[i++] = zname;
jvdias 207484
-  dn_buffer[i++] = hnamebuff;
Tomas Hozza 3fed71
+  char *hname=0L, *last=0L;
Tomas Hozza 3fed71
+  int hlen=strlen(hostname), zlen=(strlen(zone));
Tomas Hozza 3fed71
+
jvdias 207484
+/*  printf("hostname: %s zone: %s\n",hostname, zone); */
jvdias 207484
+  hname=0L;
jvdias 207484
+  if(flags == DNS_OBJECT)
jvdias 207484
+  {
jvdias 207484
+      if( (zone[ zlen - 1 ] == '.') && (hostname[hlen - 1] != '.') )
jvdias 207484
+      {
jvdias 207484
+	  hname=(char*)malloc(hlen + 1);
jvdias 207484
+	  hlen += 1;
jvdias 207484
+	  sprintf(hname, "%s.", hostname);
jvdias 207484
+	  hostname = hname;
jvdias 207484
+      }
jvdias 207484
+      if(strcmp(hostname, zone) == 0)
jvdias 207484
+      {
jvdias 207484
+	  if( hname == 0 )
jvdias 207484
+	      hname=strdup(hostname);
jvdias 207484
+  	  last = strdup(sameZone);
jvdias 207484
+      }else
jvdias 207484
+      {	   
jvdias 207484
+	  if(  (hlen < zlen) 
jvdias 207484
+	     ||( strcmp( hostname + (hlen - zlen), zone ) != 0)
jvdias 207484
+	    )
jvdias 207484
+	  {
jvdias 207484
+	      if( hname != 0 )
jvdias 207484
+		  free(hname);
jvdias 207484
+	      hname=(char*)malloc( hlen + zlen + 1);
jvdias 207484
+	      if( *zone == '.' )
jvdias 207484
+		  sprintf(hname, "%s%s", hostname, zone);
jvdias 207484
+	      else
jvdias 207484
+		  sprintf(hname,"%s",zone);
jvdias 207484
+	  }else
jvdias 207484
+	  {
jvdias 207484
+	      if( hname == 0 )
jvdias 207484
+		  hname = strdup(hostname);
jvdias 207484
+	  }
jvdias 207484
+	  last = hname;
jvdias 207484
+      }
jvdias 207484
+  }else
jvdias 207484
+  { /* flags == DNS_TOP */
jvdias 207484
+      hname = strdup(zone);
jvdias 207484
+      last = hname;
jvdias 207484
+  }
jvdias 207484
+
jvdias 207484
+  for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
jvdias 207484
+       tmp = strrchr (hname, '.'))
jvdias 207484
+  {
jvdias 207484
+      if( *( tmp + 1 ) != '\0' )
jvdias 207484
+      {
jvdias 207484
+	  *tmp = '\0';
jvdias 207484
+	  dn_buffer[i++] = ++tmp;
jvdias 207484
+      }else
jvdias 207484
+      { /* trailing '.' ! */
jvdias 207484
+	  dn_buffer[i++] = strdup(".");
jvdias 207484
+	  *tmp = '\0';
jvdias 207484
+	  if( tmp == hname )
jvdias 207484
+	      break;
jvdias 207484
+      }	  
jvdias 207484
+  }
jvdias 207484
+  if( ( last != hname ) && (tmp != hname) )
jvdias 207484
+      dn_buffer[i++] = hname;
jvdias 207484
+  dn_buffer[i++] = last;
jvdias 207484
   dn_buffer[i] = NULL;
jvdias 207484
 
Tomas Hozza 3fed71
   return dn_buffer;
2a4663
@@ -680,24 +780,32 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
jvdias 207484
  * exception of "@"/SOA. */
jvdias 207484
 
jvdias 207484
 char *
jvdias 207484
-build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag)
jvdias 207484
+build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
jvdias 207484
 {
jvdias 207484
   int size;
jvdias 207484
-  int x;
jvdias 207484
+  int x, znlen;
jvdias 207484
   static char dn[1024];
jvdias 207484
   char tmp[128];
jvdias 207484
+  char zn[DNS_NAME_MAXTEXT+1];
jvdias 207484
 
jvdias 207484
   bzero (tmp, sizeof (tmp));
jvdias 207484
   bzero (dn, sizeof (dn));
jvdias 207484
   size = get_attr_list_size (dc_list);
jvdias 207484
+  znlen = strlen(zone);
jvdias 207484
+  if ( *(zone + (znlen-1)) == '.' )
jvdias 207484
+  { /* ldapdb MUST search by relative zone name */
jvdias 207484
+      memcpy(&(zn[0]),zone,znlen-1);
jvdias 207484
+      *(zn + (znlen-1))='\0';
jvdias 207484
+      zone = zn;
jvdias 207484
+  }
jvdias 207484
   for (x = size - 2; x > 0; x--)
jvdias 207484
     {
jvdias 207484
     if (flag == WI_SPEC)
jvdias 207484
     {
jvdias 207484
       if (x == (size - 2) && (strncmp (dc_list[x], "@", 1) == 0) && (ttl))
2a4663
-	sprintf (tmp, "relativeDomainName=%s + dNSTTL=%u,", dc_list[x], ttl);
jvdias 207484
+	sprintf (tmp, "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
jvdias 207484
       else if (x == (size - 2))
jvdias 207484
-	      sprintf(tmp, "relativeDomainName=%s,",dc_list[x]);
jvdias 207484
+	      sprintf(tmp, "zoneName=%s + relativeDomainName=%s,", zone, dc_list[x]);
jvdias 207484
       else
jvdias 207484
 	      sprintf(tmp,"dc=%s,", dc_list[x]);
jvdias 207484
     }
2a4663
@@ -723,6 +831,7 @@ void
jvdias 207484
 init_ldap_conn ()
jvdias 207484
 {
jvdias 207484
   int result;
jvdias 207484
+  char ldb_tag[]="LDAP Bind";
jvdias 207484
   conn = ldap_open (ldapsystem, LDAP_PORT);
jvdias 207484
   if (conn == NULL)
jvdias 207484
     {
2a4663
@@ -732,7 +841,7 @@ init_ldap_conn ()
jvdias 207484
     }
jvdias 207484
 
jvdias 207484
   result = ldap_simple_bind_s (conn, binddn, bindpw);
jvdias 207484
-  ldap_result_check ("ldap_simple_bind_s", (char*)"LDAP Bind", result);
jvdias 207484
+  ldap_result_check ("ldap_simple_bind_s", ldb_tag , result);
jvdias 207484
 }
jvdias 207484
 
jvdias 207484
 /* Like isc_result_check, only for LDAP */
2a4663
@@ -749,8 +858,6 @@ ldap_result_check (const char *msg, char *dn, int err)
jvdias 207484
     }
jvdias 207484
 }
jvdias 207484
 
jvdias 207484
-
jvdias 207484
-
jvdias 207484
 /* For running the ldap_info run queue. */
jvdias 207484
 void
jvdias 207484
 add_ldap_values (ldap_info * ldinfo)
2a4663
@@ -758,14 +865,14 @@ add_ldap_values (ldap_info * ldinfo)
jvdias 207484
   int result;
jvdias 207484
   char dnbuffer[1024];
jvdias 207484
 
jvdias 207484
-
jvdias 207484
   if (ldapbase != NULL)
jvdias 207484
     sprintf (dnbuffer, "%s,%s", ldinfo->dn, ldapbase);
jvdias 207484
   else
jvdias 207484
     sprintf (dnbuffer, "%s", ldinfo->dn);
jvdias 207484
 
jvdias 207484
   result = ldap_add_s (conn, dnbuffer, ldinfo->attrs);
jvdias 207484
-  ldap_result_check ("ldap_add_s", dnbuffer, result);
jvdias 207484
+    ldap_result_check ("ldap_add_s", dnbuffer, result);
jvdias 207484
+
jvdias 207484
 }
jvdias 207484
 
jvdias 207484
 
2a4663
@@ -776,5 +883,5 @@ void
jvdias 207484
 usage ()
jvdias 207484
 {
jvdias 207484
   fprintf (stderr,
Tomas Hozza 3fed71
-	   "zone2ldap -D [BIND DN] -w [BIND PASSWORD] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
Tomas Hozza 3fed71
+	   "zone2ldap -D [BIND DN] [-w BIND PASSWORD | -W:prompt] -b [BASE DN] -z [ZONE] -f [ZONE FILE] -h [LDAP HOST] "
Tomas Hozza 3fed71
 	   "[-c Create LDAP Base structure][-d Debug Output (lots !)] \n ");}