Blob Blame History Raw
From 9683a4d2524b870c4cee09259cb5eb7b8075a507 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20Men=C5=A1=C3=ADk?= <pemensik@redhat.com>
Date: Tue, 18 Dec 2018 16:06:26 +0100
Subject: [PATCH] Make absolute hostname by dns API instead of strings

Duplicate all strings in dc_list. Free allocated memory on each record.
---
 bin/sdb_tools/zone2ldap.c | 70 +++++++++++++++++++++++++--------------
 1 file changed, 45 insertions(+), 25 deletions(-)

diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
index d59936c..9ba73b8 100644
--- a/bin/sdb_tools/zone2ldap.c
+++ b/bin/sdb_tools/zone2ldap.c
@@ -84,6 +84,10 @@ int get_attr_list_size (char **tmp);
 /* Get a DN */
 char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone);
 
+/* Free a DN list */
+static void
+free_dc_list(char **dc_list);
+
 /* Add to RR list */
 void add_to_rr_list (char *dn, char *name, char *type, char *data,
 		     unsigned int ttl, unsigned int flags);
@@ -120,6 +124,7 @@ static char dNSTTL            []="dNSTTL";
 static char zoneName          []="zoneName";
 static char dc                []="dc";
 static char sameZone          []="@";
+static char dot               []=".";
 /* LDAPMod mod_values: */
 static char *objectClasses    []= { &(topClass[0]), &(dNSZoneClass[0]), NULL };
 static char *topObjectClasses []= { &(topClass[0]), &(dcObjectClass[0]), &(dNSZoneClass[0]), NULL };
@@ -391,6 +396,8 @@ main (int argc, char **argv)
 	    }
 
 	}
+
+        free_dc_list(dc_list);
     }
   else
     {
@@ -446,12 +453,18 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
   char data[2048];
   char **dc_list;
   char *dn;
+  size_t argzone_len;
+  bool omit_dot;
 
   isc_buffer_t buff;
   isc_result_t result;
 
   isc_buffer_init (&buff, name, sizeof (name));
   result = dns_name_totext (dnsname, true, &buff);
+  argzone_len = strlen(argzone);
+  /* If argzone is absolute, output absolute name too */
+  omit_dot = (!(argzone_len > 0 && argzone[argzone_len-1] == '.'));
+  result = dns_name_totext (dnsname, omit_dot, &buff);
   isc_result_check (result, "dns_name_totext");
   name[isc_buffer_usedlength (&buff)] = 0;
 
@@ -473,6 +486,7 @@ generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl)
     printf ("Adding %s (%s %s) to run queue list.\n", dn, type, data);
 
   add_to_rr_list (dn, dc_list[len], (char*)type, (char*)data, ttl, DNS_OBJECT);
+  free_dc_list(dc_list);
 }
 
 
@@ -533,12 +547,9 @@ add_to_rr_list (char *dn, char *name, char *type,
       if (tmp->attrs == (LDAPMod **) NULL)
 	fatal("calloc");
 
-      for (i = 0; i < (int)flags; i++)
-	{
-	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
-	  if (tmp->attrs[i] == (LDAPMod *) NULL)
-	    fatal("malloc");
-	}
+      tmp->attrs[0] = (LDAPMod *) malloc (sizeof (LDAPMod));
+      if (tmp->attrs[0] == (LDAPMod *) NULL)
+	  fatal("malloc");
       tmp->attrs[0]->mod_op = LDAP_MOD_ADD;
       tmp->attrs[0]->mod_type = objectClass;
 
@@ -554,9 +565,18 @@ add_to_rr_list (char *dn, char *name, char *type,
 	  return;
 	}
 
+      for (i = 1; i < (int)flags-1; i++)
+	{
+	  tmp->attrs[i] = (LDAPMod *) malloc (sizeof (LDAPMod));
+	  if (tmp->attrs[i] == (LDAPMod *) NULL)
+	    fatal("malloc");
+	}
+      tmp->attrs[i] = NULL;
+
+
       tmp->attrs[1]->mod_op = LDAP_MOD_ADD;
       tmp->attrs[1]->mod_type = relativeDomainName;
-      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 2);
+      tmp->attrs[1]->mod_values = (char **) calloc (sizeof (char *), 3);
 
       if (tmp->attrs[1]->mod_values == (char **)NULL)
 	fatal("calloc");
@@ -701,24 +721,16 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
 {
   char *tmp;
   int i = 0;
+  int j = 0;
   char *hname=0L, *last=0L;
   int hlen=strlen(hostname), zlen=(strlen(zone));
 
 /*  printf("hostname: %s zone: %s\n",hostname, zone); */
-  hname=0L;
   if(flags == DNS_OBJECT)
   {
-      if( (zone[ zlen - 1 ] == '.') && (hostname[hlen - 1] != '.') )
-      {
-	  hname=(char*)malloc(hlen + 1);
-	  hlen += 1;
-	  sprintf(hname, "%s.", hostname);
-	  hostname = hname;
-      }
       if(strcmp(hostname, zone) == 0)
       {
-	  if( hname == 0 )
-	      hname=strdup(hostname);
+	  hname= strdup(hostname);
 	  last = strdup(sameZone);
       }else
       {
@@ -726,8 +738,6 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
 	     ||( strcmp( hostname + (hlen - zlen), zone ) != 0)
 	    )
 	  {
-	      if( hname != 0 )
-		  free(hname);
 	      hname=(char*)malloc( hlen + zlen + 1);
 	      if( *zone == '.' )
 		  sprintf(hname, "%s%s", hostname, zone);
@@ -735,8 +745,7 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
 		  sprintf(hname,"%s",zone);
 	  }else
 	  {
-	      if( hname == 0 )
-		  hname = strdup(hostname);
+	      hname = strdup(hostname);
 	  }
 	  last = hname;
       }
@@ -749,18 +758,21 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
   for (tmp = strrchr (hname, '.'); tmp != (char *) 0;
        tmp = strrchr (hname, '.'))
   {
-      if( *( tmp + 1 ) != '\0' )
+      tmp[0] = '\0';
+      if( tmp[1] != '\0' )
       {
-	  *tmp = '\0';
 	  dn_buffer[i++] = ++tmp;
       }else
       { /* trailing '.' ! */
-	  dn_buffer[i++] = strdup(".");
-	  *tmp = '\0';
+	  dn_buffer[i++] = dot;
 	  if( tmp == hname )
 	      break;
       }
   }
+  for (j=0; j<i; j++)
+  {
+      dn_buffer[j] = strdup(dn_buffer[j]);
+  }
   if( ( last != hname ) && (tmp != hname) )
       dn_buffer[i++] = hname;
   dn_buffer[i++] = last;
@@ -820,6 +832,14 @@ build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag, char *zone)
   return dn;
 }
 
+static void
+free_dc_list(char **dc_list)
+{
+    for (; *dc_list; dc_list++) {
+        free(*dc_list);
+	*dc_list=NULL;
+    }
+}
 
 /* Initialize LDAP Conn */
 void
-- 
2.21.1