Blame SOURCES/bind-9.11-zone2ldap.patch

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