From 1be39d7f31003b391ae080c5e5f54dd54d4619bb Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Mon, 16 Jul 2012 11:51:45 -0400
Subject: [PATCH 1/4] LOCATOR: Convert Kerberos locator to talloc

---
 Makefile.am                                |    2 +
 src/krb5_plugin/sssd_krb5_locator_plugin.c |   56 ++++++++++++----------------
 2 files changed, 25 insertions(+), 33 deletions(-)

diff --git a/Makefile.am b/Makefile.am
index 3c66b6cfe0715395aeb06039ad1bea4cb9298fab..2f7143a25f13f7bf4c8dd86b9c3613cfa8222cc4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1465,8 +1465,10 @@ sssd_krb5_locator_plugin_la_SOURCES = \
     src/util/atomic_io.c
 sssd_krb5_locator_plugin_la_CFLAGS = \
     $(AM_CFLAGS) \
+    $(TALLOC_CFLAGS) \
     $(KRB5_CFLAGS)
 sssd_krb5_locator_plugin_la_LDFLAGS = \
+    $(TALLOC_LIBS) \
     -avoid-version \
     -module
 endif
diff --git a/src/krb5_plugin/sssd_krb5_locator_plugin.c b/src/krb5_plugin/sssd_krb5_locator_plugin.c
index 402fd03e7e45644c65581c7c6f14fe8d7bf2e894..c33068bb9906ed8662faf6b0b5e86f4fcc3a0115 100644
--- a/src/krb5_plugin/sssd_krb5_locator_plugin.c
+++ b/src/krb5_plugin/sssd_krb5_locator_plugin.c
@@ -32,6 +32,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <ctype.h>
+#include <talloc.h>
 
 #include "util/sss_krb5.h"
 #include <krb5/locate_plugin.h>
@@ -65,19 +66,18 @@ void debug_fn(const char *format, ...)
 {
     va_list ap;
     char *s = NULL;
-    int ret;
 
     va_start(ap, format);
-
-    ret = vasprintf(&s, format, ap);
+    s = talloc_vasprintf(NULL, format, ap);
     va_end(ap);
-    if (ret < 0) {
+
+    if (!s) {
         /* ENOMEM */
         return;
     }
 
     fprintf(stderr, DEBUG_KEY "%s", s);
-    free(s);
+    talloc_free(s);
 }
 
 static int get_krb5info(const char *realm, struct sssd_ctx *ctx,
@@ -105,22 +105,12 @@ static int get_krb5info(const char *realm, struct sssd_ctx *ctx,
             return EINVAL;
     }
 
-
-    len = strlen(realm) + strlen(name_tmpl);
-
-    krb5info_name = calloc(1, len + 1);
+    krb5info_name = talloc_asprintf(ctx, name_tmpl, realm);
     if (krb5info_name == NULL) {
-        PLUGIN_DEBUG(("malloc failed.\n"));
-        return ENOMEM;
-    }
-
-    ret = snprintf(krb5info_name, len, name_tmpl, realm);
-    if (ret < 0) {
-        PLUGIN_DEBUG(("snprintf failed.\n"));
+        PLUGIN_DEBUG(("asprintf failed.\n"));
         ret = EINVAL;
         goto done;
     }
-    krb5info_name[len] = '\0';
 
     fd = open(krb5info_name, O_RDONLY);
     if (fd == -1) {
@@ -184,8 +174,8 @@ static int get_krb5info(const char *realm, struct sssd_ctx *ctx,
 
     switch (svc) {
         case locate_service_kdc:
-            free(ctx->kdc_addr);
-            ctx->kdc_addr = strdup((char *) buf);
+            talloc_zfree(ctx->kdc_addr);
+            ctx->kdc_addr = talloc_strdup(ctx, (char *) buf);
             if (ctx->kdc_addr == NULL) {
                 PLUGIN_DEBUG(("strdup failed.\n"));
                 ret = ENOMEM;
@@ -194,8 +184,8 @@ static int get_krb5info(const char *realm, struct sssd_ctx *ctx,
             ctx->kdc_port = (uint16_t) port;
             break;
         case locate_service_kpasswd:
-            free(ctx->kpasswd_addr);
-            ctx->kpasswd_addr = strdup((char *) buf);
+            talloc_zfree(ctx->kpasswd_addr);
+            ctx->kpasswd_addr = talloc_strdup(ctx, (char *) buf);
             if (ctx->kpasswd_addr == NULL) {
                 PLUGIN_DEBUG(("strdup failed.\n"));
                 ret = ENOMEM;
@@ -211,7 +201,7 @@ static int get_krb5info(const char *realm, struct sssd_ctx *ctx,
 
     ret = 0;
 done:
-    free(krb5info_name);
+    talloc_free(krb5info_name);
     return ret;
 }
 
@@ -221,7 +211,7 @@ krb5_error_code sssd_krb5_locator_init(krb5_context context,
     struct sssd_ctx *ctx;
     const char *dummy;
 
-    ctx = calloc(1,sizeof(struct sssd_ctx));
+    ctx = talloc_zero(NULL, struct sssd_ctx);
     if (ctx == NULL) return KRB5_PLUGIN_NO_HANDLE;
 
     dummy = getenv(SSSD_KRB5_LOCATOR_DEBUG);
@@ -246,11 +236,7 @@ void sssd_krb5_locator_close(void *private_data)
     ctx = (struct sssd_ctx *) private_data;
     PLUGIN_DEBUG(("sssd_krb5_locator_close called\n"));
 
-    free(ctx->kdc_addr);
-    free(ctx->kpasswd_addr);
-    free(ctx->sssd_realm);
-    free(ctx);
-    private_data = NULL;
+    talloc_zfree(private_data);
 
     return;
 }
@@ -272,11 +258,11 @@ krb5_error_code sssd_krb5_locator_lookup(void *private_data,
     char port_str[PORT_STR_SIZE];
 
     if (private_data == NULL) return KRB5_PLUGIN_NO_HANDLE;
-    ctx = (struct sssd_ctx *) private_data;
+    ctx = talloc_get_type_abort(private_data, struct sssd_ctx);
 
     if (ctx->sssd_realm == NULL || strcmp(ctx->sssd_realm, realm) != 0) {
-        free(ctx->sssd_realm);
-        ctx->sssd_realm = strdup(realm);
+        talloc_zfree(ctx->sssd_realm);
+        ctx->sssd_realm = talloc_strdup(ctx, realm);
         if (ctx->sssd_realm == NULL) {
             PLUGIN_DEBUG(("strdup failed.\n"));
             return KRB5_PLUGIN_NO_HANDLE;
@@ -294,8 +280,12 @@ krb5_error_code sssd_krb5_locator_lookup(void *private_data,
             if (ret != EOK) {
                 PLUGIN_DEBUG(("reading kpasswd address failed, "
                               "using kdc address.\n"));
-                free(ctx->kpasswd_addr);
-                ctx->kpasswd_addr = strdup(ctx->kdc_addr);
+                talloc_zfree(ctx->kpasswd_addr);
+                ctx->kpasswd_addr = talloc_strdup(ctx, ctx->kdc_addr);
+                if (ctx->kpasswd_addr == NULL) {
+                    PLUGIN_DEBUG(("strdup failed.\n"));
+                    return KRB5_PLUGIN_NO_HANDLE;
+                }
                 ctx->kpasswd_port = 0;
             }
         }
-- 
1.7.10.4

