3ce7d3
From 738d12594972ad816e8cff9821f760aa0682fd08 Mon Sep 17 00:00:00 2001
3ce7d3
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
3ce7d3
Date: Tue, 18 Dec 2018 16:06:26 +0100
3ce7d3
Subject: [PATCH] Make absolute hostname by dns API instead of strings
3ce7d3
3ce7d3
Duplicate all strings in dc_list. Free allocated memory on each record.
3ce7d3
---
3ce7d3
 bin/sdb_tools/zone2ldap.c | 72 +++++++++++++++++++++++++++++------------------
3ce7d3
 1 file changed, 45 insertions(+), 27 deletions(-)
3ce7d3
3ce7d3
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
3ce7d3
index acf160b..cc482dc 100644
3ce7d3
--- a/bin/sdb_tools/zone2ldap.c
3ce7d3
+++ b/bin/sdb_tools/zone2ldap.c
3ce7d3
@@ -87,6 +87,10 @@ int get_attr_list_size (char **tmp);
3ce7d3
 /* Get a DN */
3ce7d3
 char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
3ce7d3
 
3ce7d3
+/* Free a DN list */
3ce7d3
+static void
3ce7d3
+free_dc_list(char **dc_list);
3ce7d3
+
3ce7d3
 /* Add to RR list */
3ce7d3
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
3ce7d3
 		     unsigned int ttl, unsigned int flags);
3ce7d3
@@ -123,6 +127,7 @@ static char dNSTTL            []="dNSTTL";
3ce7d3
 static char zoneName          []="zoneName";
3ce7d3
 static char dc                []="dc";
3ce7d3
 static char sameZone          []="@";
3ce7d3
+static char dot               []=".";
3ce7d3
 /* LDAPMod mod_values: */
3ce7d3
 static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
3ce7d3
 static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
3ce7d3
@@ -396,6 +401,8 @@ main (int argc, char **argv)
3ce7d3
 	    }
3ce7d3
 
3ce7d3
 	}
3ce7d3
+
3ce7d3
+        free_dc_list(dc_list);
3ce7d3
     }
3ce7d3
   else
3ce7d3
     {
3ce7d3
@@ -451,12 +458,17 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
3ce7d3
   char data[2048];
3ce7d3
   char **dc_list;
3ce7d3
   char *dn;
3ce7d3
+  size_t argzone_len;
3ce7d3
+  isc_boolean_t omit_dot;
3ce7d3
 
3ce7d3
   isc_buffer_t buff;
3ce7d3
   isc_result_t result;
3ce7d3
 
3ce7d3
   isc_buffer_init (&buff, name, sizeof (name));
3ce7d3
-  result = dns_name_totext (dnsname, ISC_TRUE, &buff);
3ce7d3
+  argzone_len = strlen(argzone);
3ce7d3
+  /* If argzone is absolute, output absolute name too */
3ce7d3
+  omit_dot = ISC_TF(!(argzone_len > 0 && argzone[argzone_len-1] == '.'));
3ce7d3
+  result = dns_name_totext (dnsname, omit_dot, &buff);
3ce7d3
   isc_result_check (result, "dns_name_totext");
3ce7d3
   name[isc_buffer_usedlength (&buff)] = 0;
3ce7d3
 
3ce7d3
@@ -478,6 +490,7 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
3ce7d3
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
3ce7d3
 
3ce7d3
   add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
3ce7d3
+  free_dc_list(dc_list);
3ce7d3
 }
3ce7d3
 
3ce7d3
 
3ce7d3
@@ -538,12 +551,9 @@ add_to_rr_list (char *dn, char *name, char *type,
3ce7d3
       if (tmp->attrs == (LDAPMod **) NULL)
3ce7d3
 	fatal("calloc");
3ce7d3
 
3ce7d3
-      for (i = 0; i < (int)flags; i++)
3ce7d3
-	{
3ce7d3
-	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
3ce7d3
-	  if (tmp->attrs[i] == (LDAPMod *) NULL)
3ce7d3
-	    fatal("malloc");
3ce7d3
-	}
3ce7d3
+      tmp->attrs[0] = (LDAPMod *) malloc (sizeof (LDAPMod));
3ce7d3
+      if (tmp->attrs[0] == (LDAPMod *) NULL)
3ce7d3
+	  fatal("malloc");
3ce7d3
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
3ce7d3
       tmp->attrs[0]->mod_type = objectClass;
3ce7d3
 
3ce7d3
@@ -559,9 +569,18 @@ add_to_rr_list (char *dn, char *name, char *type,
3ce7d3
 	  return;
3ce7d3
 	}
3ce7d3
 
3ce7d3
+      for (i = 1; i < (int)flags-1; i++)
3ce7d3
+	{
3ce7d3
+	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
3ce7d3
+	  if (tmp->attrs[i] == (LDAPMod *) NULL)
3ce7d3
+	    fatal("malloc");
3ce7d3
+	}
3ce7d3
+      tmp->attrs[i] = NULL;
3ce7d3
+
3ce7d3
+
3ce7d3
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
3ce7d3
       tmp->attrs[1]->mod_type = relativeDomainName;
3ce7d3
-      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
3ce7d3
+      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 3);
3ce7d3
 
3ce7d3
       if (tmp->attrs[1]->mod_values == (char **)NULL)
3ce7d3
 	fatal("calloc");
3ce7d3
@@ -705,25 +724,16 @@ char **
3ce7d3
 hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
3ce7d3
 {
3ce7d3
   char *tmp;
3ce7d3
-  int i = 0;
3ce7d3
+  int i = 0, j = 0;
3ce7d3
   char *hname=0L, *last=0L;
3ce7d3
   int hlen=strlen(hostname), zlen=(strlen(zone));
3ce7d3
 
3ce7d3
 /*  printf("hostname: %s zone: %s\n",hostname, zone); */
3ce7d3
-  hname=0L;
3ce7d3
   if(flags == DNS_OBJECT)
3ce7d3
   {
3ce7d3
-      if( (zone[ zlen - 1 ] == '.') && (hostname[hlen - 1] != '.') )
3ce7d3
-      {
3ce7d3
-	  hname=(char*)malloc(hlen + 1);
3ce7d3
-	  hlen += 1;
3ce7d3
-	  sprintf(hname, "%s.", hostname);
3ce7d3
-	  hostname = hname;
3ce7d3
-      }
3ce7d3
       if(strcmp(hostname, zone) == 0)
3ce7d3
       {
3ce7d3
-	  if( hname == 0 )
3ce7d3
-	      hname=strdup(hostname);
3ce7d3
+	  hname=strdup(hostname);
3ce7d3
   	  last = strdup(sameZone);
3ce7d3
       }else
3ce7d3
       {	   
3ce7d3
@@ -731,8 +741,6 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
3ce7d3
 	     ||( strcmp( hostname + (hlen - zlen), zone ) != 0)
3ce7d3
 	    )
3ce7d3
 	  {
3ce7d3
-	      if( hname != 0 )
3ce7d3
-		  free(hname);
3ce7d3
 	      hname=(char*)malloc( hlen + zlen + 1);
3ce7d3
 	      if( *zone == '.' )
3ce7d3
 		  sprintf(hname, "%s%s", hostname, zone);
3ce7d3
@@ -740,8 +748,7 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
3ce7d3
 		  sprintf(hname,"%s",zone);
3ce7d3
 	  }else
3ce7d3
 	  {
3ce7d3
-	      if( hname == 0 )
3ce7d3
-		  hname = strdup(hostname);
3ce7d3
+	      hname = strdup(hostname);
3ce7d3
 	  }
3ce7d3
 	  last = hname;
3ce7d3
       }
3ce7d3
@@ -754,18 +761,21 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
3ce7d3
   for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
3ce7d3
        tmp = strrchr (hname, '.'))
3ce7d3
   {
3ce7d3
-      if( *( tmp + 1 ) != '\0' )
3ce7d3
+      tmp[0] = '\0';
3ce7d3
+      if( tmp[1] != '\0' )
3ce7d3
       {
3ce7d3
-	  *tmp = '\0';
3ce7d3
 	  dn_buffer[i++] = ++tmp;
3ce7d3
       }else
3ce7d3
       { /* trailing '.' ! */
3ce7d3
-	  dn_buffer[i++] = strdup(".");
3ce7d3
-	  *tmp = '\0';
3ce7d3
+	  dn_buffer[i++] = dot;
3ce7d3
 	  if( tmp == hname )
3ce7d3
 	      break;
3ce7d3
       }	  
3ce7d3
   }
3ce7d3
+  for (j=0; j
3ce7d3
+  {
3ce7d3
+      dn_buffer[j] = strdup(dn_buffer[j]);
3ce7d3
+  }
3ce7d3
   if( ( last != hname ) && (tmp != hname) )
3ce7d3
       dn_buffer[i++] = hname;
3ce7d3
   dn_buffer[i++] = last;
3ce7d3
@@ -825,6 +835,14 @@ build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
3ce7d3
   return dn;
3ce7d3
 }
3ce7d3
 
3ce7d3
+static void
3ce7d3
+free_dc_list(char **dc_list)
3ce7d3
+{
3ce7d3
+    for (; *dc_list; dc_list++) {
3ce7d3
+        free(*dc_list);
3ce7d3
+	*dc_list=NULL;
3ce7d3
+    }
3ce7d3
+}
3ce7d3
 
3ce7d3
 /* Initialize LDAP Conn */
3ce7d3
 void
3ce7d3
-- 
3ce7d3
2.14.5
3ce7d3