230545
From 9683a4d2524b870c4cee09259cb5eb7b8075a507 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
---
230545
 bin/sdb_tools/zone2ldap.c | 70 +++++++++++++++++++++++++--------------
230545
 1 file changed, 45 insertions(+), 25 deletions(-)
89a891
89a891
diff --git a/bin/sdb_tools/zone2ldap.c b/bin/sdb_tools/zone2ldap.c
230545
index d59936c..9ba73b8 100644
89a891
--- a/bin/sdb_tools/zone2ldap.c
89a891
+++ b/bin/sdb_tools/zone2ldap.c
230545
@@ -84,6 +84,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);
230545
@@ -120,6 +124,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 };
230545
@@ -391,6 +396,8 @@ main (int argc, char **argv)
89a891
 	    }
89a891
 
89a891
 	}
89a891
+
89a891
+        free_dc_list(dc_list);
89a891
     }
89a891
   else
89a891
     {
230545
@@ -446,12 +453,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
 
230545
@@ -473,6 +486,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
 
230545
@@ -533,12 +547,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
 
230545
@@ -554,9 +565,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");
230545
@@ -701,24 +721,16 @@ hostname_to_dn_list (char *hostname, char *zone, unsigned int flags)
89a891
 {
89a891
   char *tmp;
230545
   int i = 0;
230545
+  int 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);
230545
+	  hname= strdup(hostname);
230545
 	  last = strdup(sameZone);
89a891
       }else
230545
       {
230545
@@ -726,8 +738,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);
230545
@@ -735,8 +745,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
       }
230545
@@ -749,18 +758,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;
230545
       }
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;
230545
@@ -820,6 +832,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
-- 
230545
2.21.1
89a891