From 1a9957cf23b3635f60dd22485988fe47d7154f6c Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Tue, 2 Jun 2009 15:26:13 -0400 Subject: Turn sssd_mem_takeover into sssd_mem_attach The old function was not used anywhere, and this function uses better semantics, including not using void ** which gives strict aliasing problems. Also add a generic password destroy function --- server/util/memory.c | 51 ++++++++++++++++++++++++++++++++++----------------- server/util/util.h | 25 +++++++++++++++++-------- 2 files changed, 51 insertions(+), 25 deletions(-) (limited to 'server/util') diff --git a/server/util/memory.c b/server/util/memory.c index 87fefd2f..8ffad49a 100644 --- a/server/util/memory.c +++ b/server/util/memory.c @@ -1,30 +1,47 @@ #include "talloc.h" +#include "util/util.h" /* - * sssd_mem_takeover - * This function will take a non-talloc pointer and add it to a talloc + * sssd_mem_attach + * This function will take a non-talloc pointer and "attach" it to a talloc * memory context. It will accept a destructor for the original pointer * so that when the parent memory context is freed, the non-talloc * pointer will also be freed properly. */ -TALLOC_CTX *sssd_mem_takeover(TALLOC_CTX *mem_ctx, - void *ptr, - int (*destructor)(void **)) + +int password_destructor(void *memctx) +{ + char *password = (char *)memctx; + int i; + + /* zero out password */ + for (i = 0; password[i]; i++) password[i] = '\0'; + + return 0; +} + +static int mem_holder_destructor(void *ptr) +{ + struct mem_holder *h; + + h = talloc_get_type(ptr, struct mem_holder); + return h->fn(h->mem); +} + +void *sss_mem_attach(TALLOC_CTX *mem_ctx, + void *ptr, + void_destructor_fn_t *fn) { - TALLOC_CTX **handle; + struct mem_holder *h; - if (ptr == NULL) { - return NULL; - } + if (!ptr || !fn) return NULL; - handle = talloc_named_const(mem_ctx, sizeof(void *), - "sssd_mem_takeover destructor handle"); - if (handle == NULL) { - return NULL; - } + h = talloc(mem_ctx, struct mem_holder); + if (!h) return NULL; - *handle = ptr; - talloc_set_destructor(handle, destructor); + h->mem = ptr; + h->fn = fn; + talloc_set_destructor((TALLOC_CTX *)h, mem_holder_destructor); - return handle; + return h; } diff --git a/server/util/util.h b/server/util/util.h index 61f63fef..7acbfe61 100644 --- a/server/util/util.h +++ b/server/util/util.h @@ -52,11 +52,6 @@ void debug_fn(const char *format, ...); #define talloc_zfree(ptr) do { talloc_free(ptr); ptr = NULL; } while(0) #endif -struct main_context { - struct tevent_context *event_ctx; - struct confdb_ctx *confdb_ctx; -}; - #include "util/dlinklist.h" /* From debug.c */ @@ -64,6 +59,11 @@ void ldb_debug_messages(void *context, enum ldb_debug_level level, const char *fmt, va_list ap); /* from server.c */ +struct main_context { + struct tevent_context *event_ctx; + struct confdb_ctx *confdb_ctx; +}; + int server_setup(const char *name, int flags, const char *conf_entry, struct main_context **main_ctx); @@ -77,9 +77,18 @@ void CatchChild(void); void CatchChildLeaveStatus(void); /* from memory.c */ -TALLOC_CTX *sssd_mem_takeover(TALLOC_CTX *mem_ctx, - void *ptr, - int (*destructor)(void **)); +typedef int (void_destructor_fn_t)(void *); + +struct mem_holder { + void *mem; + void_destructor_fn_t *fn; +}; + +void *sss_mem_attach(TALLOC_CTX *mem_ctx, + void *ptr, + void_destructor_fn_t *fn); + +int password_destructor(void *memctx); /* from usertools.c */ char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid); -- cgit