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