b9f8ff
From 9683a4d2524b870c4cee09259cb5eb7b8075a507 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
---
b9f8ff
 bin/sdb_tools/zone2ldap.c | 70 +++++++++++++++++++++++++--------------
b9f8ff
 1 file changed, 45 insertions(+), 25 deletions(-)
e55890
e55890
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
b9f8ff
index d59936c..9ba73b8 100644
e55890
--- a/bin/sdb_tools/zone2ldap.c
e55890
+++ b/bin/sdb_tools/zone2ldap.c
b9f8ff
@@ -84,6 +84,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);
b9f8ff
@@ -120,6 +124,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 };
b9f8ff
@@ -391,6 +396,8 @@ main (int argc, char **argv)
e55890
 	    }
e55890
 
e55890
 	}
e55890
+
e55890
+        free_dc_list(dc_list);
e55890
     }
e55890
   else
e55890
     {
b9f8ff
@@ -446,12 +453,18 @@ 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;
27025e
+  bool omit_dot;
e55890
 
e55890
   isc_buffer_t buff;
e55890
   isc_result_t result;
e55890
 
e55890
   isc_buffer_init (&buff, name, sizeof (name));
27025e
   result = dns_name_totext (dnsname, true, &buff);
e55890
+  argzone_len = strlen(argzone);
e55890
+  /* If argzone is absolute, output absolute name too */
27025e
+  omit_dot = (!(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
 
b9f8ff
@@ -473,6 +486,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
 
b9f8ff
@@ -533,12 +547,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
 
b9f8ff
@@ -554,9 +565,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");
b9f8ff
@@ -701,24 +721,16 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
e55890
 {
e55890
   char *tmp;
b9f8ff
   int i = 0;
b9f8ff
+  int 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);
b9f8ff
+	  hname= strdup(hostname);
b9f8ff
 	  last = strdup(sameZone);
e55890
       }else
b9f8ff
       {
b9f8ff
@@ -726,8 +738,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);
b9f8ff
@@ -735,8 +745,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
       }
b9f8ff
@@ -749,18 +758,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;
b9f8ff
       }
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;
b9f8ff
@@ -820,6 +832,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
-- 
b9f8ff
2.21.1
e55890