f79ca9
From 9683a4d2524b870c4cee09259cb5eb7b8075a507 Mon Sep 17 00:00:00 2001
57726f
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
57726f
Date: Tue, 18 Dec 2018 16:06:26 +0100
57726f
Subject: [PATCH] Make absolute hostname by dns API instead of strings
57726f
57726f
Duplicate all strings in dc_list. Free allocated memory on each record.
57726f
---
f79ca9
 bin/sdb_tools/zone2ldap.c | 70 +++++++++++++++++++++++++--------------
f79ca9
 1 file changed, 45 insertions(+), 25 deletions(-)
57726f
57726f
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
f79ca9
index d59936c..9ba73b8 100644
57726f
--- a/bin/sdb_tools/zone2ldap.c
57726f
+++ b/bin/sdb_tools/zone2ldap.c
f79ca9
@@ -84,6 +84,10 @@ int get_attr_list_size (char **tmp);
57726f
 /* Get a DN */
57726f
 char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
57726f
 
57726f
+/* Free a DN list */
57726f
+static void
57726f
+free_dc_list(char **dc_list);
57726f
+
57726f
 /* Add to RR list */
57726f
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
57726f
 		     unsigned int ttl, unsigned int flags);
f79ca9
@@ -120,6 +124,7 @@ static char dNSTTL            []="dNSTTL";
57726f
 static char zoneName          []="zoneName";
57726f
 static char dc                []="dc";
57726f
 static char sameZone          []="@";
57726f
+static char dot               []=".";
57726f
 /* LDAPMod mod_values: */
57726f
 static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
57726f
 static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
f79ca9
@@ -391,6 +396,8 @@ main (int argc, char **argv)
57726f
 	    }
57726f
 
57726f
 	}
57726f
+
57726f
+        free_dc_list(dc_list);
57726f
     }
57726f
   else
57726f
     {
f79ca9
@@ -446,12 +453,18 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
57726f
   char data[2048];
57726f
   char **dc_list;
57726f
   char *dn;
57726f
+  size_t argzone_len;
57726f
+  bool omit_dot;
57726f
 
57726f
   isc_buffer_t buff;
57726f
   isc_result_t result;
57726f
 
57726f
   isc_buffer_init (&buff, name, sizeof (name));
57726f
   result = dns_name_totext (dnsname, true, &buff);
57726f
+  argzone_len = strlen(argzone);
57726f
+  /* If argzone is absolute, output absolute name too */
57726f
+  omit_dot = (!(argzone_len > 0 && argzone[argzone_len-1] == '.'));
57726f
+  result = dns_name_totext (dnsname, omit_dot, &buff);
57726f
   isc_result_check (result, "dns_name_totext");
57726f
   name[isc_buffer_usedlength (&buff)] = 0;
57726f
 
f79ca9
@@ -473,6 +486,7 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
57726f
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
57726f
 
57726f
   add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
57726f
+  free_dc_list(dc_list);
57726f
 }
57726f
 
57726f
 
f79ca9
@@ -533,12 +547,9 @@ add_to_rr_list (char *dn, char *name, char *type,
57726f
       if (tmp->attrs == (LDAPMod **) NULL)
57726f
 	fatal("calloc");
57726f
 
57726f
-      for (i = 0; i < (int)flags; i++)
57726f
-	{
57726f
-	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
57726f
-	  if (tmp->attrs[i] == (LDAPMod *) NULL)
57726f
-	    fatal("malloc");
57726f
-	}
57726f
+      tmp->attrs[0] = (LDAPMod *) malloc (sizeof (LDAPMod));
57726f
+      if (tmp->attrs[0] == (LDAPMod *) NULL)
57726f
+	  fatal("malloc");
57726f
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
57726f
       tmp->attrs[0]->mod_type = objectClass;
57726f
 
f79ca9
@@ -554,9 +565,18 @@ add_to_rr_list (char *dn, char *name, char *type,
57726f
 	  return;
57726f
 	}
57726f
 
57726f
+      for (i = 1; i < (int)flags-1; i++)
57726f
+	{
57726f
+	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
57726f
+	  if (tmp->attrs[i] == (LDAPMod *) NULL)
57726f
+	    fatal("malloc");
57726f
+	}
57726f
+      tmp->attrs[i] = NULL;
57726f
+
57726f
+
57726f
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
57726f
       tmp->attrs[1]->mod_type = relativeDomainName;
57726f
-      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
57726f
+      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 3);
57726f
 
57726f
       if (tmp->attrs[1]->mod_values == (char **)NULL)
57726f
 	fatal("calloc");
f79ca9
@@ -701,24 +721,16 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
57726f
 {
57726f
   char *tmp;
f79ca9
   int i = 0;
f79ca9
+  int j = 0;
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
+	  hname= strdup(hostname);
f79ca9
 	  last = strdup(sameZone);
57726f
       }else
f79ca9
       {
f79ca9
@@ -726,8 +738,6 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
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);
f79ca9
@@ -735,8 +745,7 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
57726f
 		  sprintf(hname,"%s",zone);
57726f
 	  }else
57726f
 	  {
57726f
-	      if( hname == 0 )
57726f
-		  hname = strdup(hostname);
57726f
+	      hname = strdup(hostname);
57726f
 	  }
57726f
 	  last = hname;
57726f
       }
f79ca9
@@ -749,18 +758,21 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
57726f
   for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
57726f
        tmp = strrchr (hname, '.'))
57726f
   {
57726f
-      if( *( tmp + 1 ) != '\0' )
57726f
+      tmp[0] = '\0';
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
+	  dn_buffer[i++] = dot;
57726f
 	  if( tmp == hname )
57726f
 	      break;
f79ca9
       }
57726f
   }
57726f
+  for (j=0; j
57726f
+  {
57726f
+      dn_buffer[j] = strdup(dn_buffer[j]);
57726f
+  }
57726f
   if( ( last != hname ) && (tmp != hname) )
57726f
       dn_buffer[i++] = hname;
57726f
   dn_buffer[i++] = last;
f79ca9
@@ -820,6 +832,14 @@ build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
57726f
   return dn;
57726f
 }
57726f
 
57726f
+static void
57726f
+free_dc_list(char **dc_list)
57726f
+{
57726f
+    for (; *dc_list; dc_list++) {
57726f
+        free(*dc_list);
57726f
+	*dc_list=NULL;
57726f
+    }
57726f
+}
57726f
 
57726f
 /* Initialize LDAP Conn */
57726f
 void
57726f
-- 
f79ca9
2.21.1
57726f