From 5acf3118945ed745825cf29d5353f39b5d0d72fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 2 Sep 2013 13:36:25 +0200
Subject: [PATCH 1/6] util: add sss_idmap_talloc[_free]

Remove code duplication.
---
 Makefile.am                      |  4 +++-
 src/providers/ad/ad_subdomains.c | 14 +++-----------
 src/providers/ipa/ipa_idmap.c    | 17 +++--------------
 src/providers/ldap/sdap_idmap.c  | 17 +++--------------
 src/responder/nss/nsssrv.c       | 13 ++-----------
 src/responder/pac/pacsrv.c       | 13 ++-----------
 src/util/util_sss_idmap.c        | 32 ++++++++++++++++++++++++++++++++
 src/util/util_sss_idmap.h        | 28 ++++++++++++++++++++++++++++
 8 files changed, 76 insertions(+), 62 deletions(-)
 create mode 100644 src/util/util_sss_idmap.c
 create mode 100644 src/util/util_sss_idmap.h

diff --git a/Makefile.am b/Makefile.am
index 4981ed94df8def239532dced88e95b7cc52e73e7..883ef86cfc7b211dfb5f0eb0a1c15b2c491971e3 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -404,6 +404,7 @@ dist_noinst_HEADERS = \
     src/util/auth_utils.h \
     src/util/authtok.h \
     src/util/util_safealign.h \
+    src/util/util_sss_idmap.h \
     src/monitor/monitor.h \
     src/monitor/monitor_interfaces.h \
     src/responder/common/responder.h \
@@ -566,7 +567,8 @@ libsss_util_la_SOURCES = \
     src/util/util_lock.c \
     src/util/util_errors.c \
     src/util/sss_ini.c \
-    src/util/io.c
+    src/util/io.c \
+    src/util/util_sss_idmap.c
 libsss_util_la_LIBADD = \
     $(SSSD_LIBS) \
     $(UNICODE_LIBS)
diff --git a/src/providers/ad/ad_subdomains.c b/src/providers/ad/ad_subdomains.c
index afd2031fe6be557f43555b6dd8b47731d9833585..876850888a8d58d7cb967bf39ecbcae1bf2d5f0c 100644
--- a/src/providers/ad/ad_subdomains.c
+++ b/src/providers/ad/ad_subdomains.c
@@ -24,6 +24,7 @@
 
 #include "providers/ldap/sdap_async.h"
 #include "providers/ad/ad_subdomains.h"
+#include "util/util_sss_idmap.h"
 #include <ctype.h>
 #include <ndr.h>
 #include <ndr/ndr_nbt.h>
@@ -777,16 +778,6 @@ struct bet_ops ad_subdomains_ops = {
     .finalize = NULL
 };
 
-static void *idmap_talloc(size_t size, void *pvt)
-{
-    return talloc_size(pvt, size);
-}
-
-static void idmap_free(void *ptr, void *pvt)
-{
-    talloc_free(ptr);
-}
-
 int ad_subdom_init(struct be_ctx *be_ctx,
                    struct ad_id_ctx *id_ctx,
                    const char *ad_domain,
@@ -825,7 +816,8 @@ int ad_subdom_init(struct be_ctx *be_ctx,
         DEBUG(SSSDBG_MINOR_FAILURE, ("Failed to add subdom offline callback"));
     }
 
-    err = sss_idmap_init(idmap_talloc, ctx, idmap_free, &ctx->idmap_ctx);
+    err = sss_idmap_init(sss_idmap_talloc, ctx, sss_idmap_talloc_free,
+                         &ctx->idmap_ctx);
     if (err != IDMAP_SUCCESS) {
         DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to initialize idmap context.\n"));
         return EFAULT;
diff --git a/src/providers/ipa/ipa_idmap.c b/src/providers/ipa/ipa_idmap.c
index c108ca75b84005c3d5957cd45ca89209d59f59ff..09772eb2294626f9cbf806185eb5bd7b1e4d833c 100644
--- a/src/providers/ipa/ipa_idmap.c
+++ b/src/providers/ipa/ipa_idmap.c
@@ -24,18 +24,7 @@
 #include "util/util.h"
 #include "providers/ldap/sdap_idmap.h"
 #include "providers/ipa/ipa_common.h"
-
-static void *
-ipa_idmap_talloc(size_t size, void *pvt)
-{
-    return talloc_size(pvt, size);
-}
-
-static void
-ipa_idmap_talloc_free(void *ptr, void *pvt)
-{
-    talloc_free(ptr);
-}
+#include "util/util_sss_idmap.h"
 
 errno_t ipa_idmap_find_new_domain(struct sdap_idmap_ctx *idmap_ctx,
                                   const char *dom_name,
@@ -168,8 +157,8 @@ errno_t ipa_idmap_init(TALLOC_CTX *mem_ctx,
     idmap_ctx->find_new_domain = ipa_idmap_find_new_domain;
 
     /* Initialize the map */
-    err = sss_idmap_init(ipa_idmap_talloc, idmap_ctx,
-                         ipa_idmap_talloc_free,
+    err = sss_idmap_init(sss_idmap_talloc, idmap_ctx,
+                         sss_idmap_talloc_free,
                          &idmap_ctx->map);
     if (err != IDMAP_SUCCESS) {
         DEBUG(SSSDBG_CRIT_FAILURE,
diff --git a/src/providers/ldap/sdap_idmap.c b/src/providers/ldap/sdap_idmap.c
index 0939c31e45a081a8494f4efa82ac1608314b82a3..6a76dc2c0d22061dca171be7fe5a96f4256a864c 100644
--- a/src/providers/ldap/sdap_idmap.c
+++ b/src/providers/ldap/sdap_idmap.c
@@ -24,18 +24,7 @@
 #include "util/dlinklist.h"
 #include "util/murmurhash3.h"
 #include "providers/ldap/sdap_idmap.h"
-
-static void *
-sdap_idmap_talloc(size_t size, void *pvt)
-{
-    return talloc_size(pvt, size);
-}
-
-static void
-sdap_idmap_talloc_free(void *ptr, void *pvt)
-{
-    talloc_free(ptr);
-}
+#include "util/util_sss_idmap.h"
 
 static errno_t
 sdap_idmap_add_configured_external_range(struct sdap_idmap_ctx *idmap_ctx)
@@ -172,8 +161,8 @@ sdap_idmap_init(TALLOC_CTX *mem_ctx,
     }
 
     /* Initialize the map */
-    err = sss_idmap_init(sdap_idmap_talloc, idmap_ctx,
-                         sdap_idmap_talloc_free,
+    err = sss_idmap_init(sss_idmap_talloc, idmap_ctx,
+                         sss_idmap_talloc_free,
                          &idmap_ctx->map);
     if (err != IDMAP_SUCCESS) {
         DEBUG(SSSDBG_CRIT_FAILURE,
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
index 253756d1bebb2ecc06740c5e6afff2799de92e1e..5f1d2e675f652c19c281263e0884d40e6b4b349e 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -47,6 +47,7 @@
 #include "providers/data_provider.h"
 #include "monitor/monitor_interfaces.h"
 #include "sbus/sbus_client.h"
+#include "util/util_sss_idmap.h"
 
 #define DEFAULT_PWFIELD "*"
 #define DEFAULT_NSS_FD_LIMIT 8192
@@ -413,16 +414,6 @@ static void nss_dp_reconnect_init(struct sbus_connection *conn,
     /* nss_shutdown(rctx); */
 }
 
-static void *idmap_talloc(size_t size, void *pvt)
-{
-    return talloc_size(pvt, size);
-}
-
-static void idmap_free(void *ptr, void *pvt)
-{
-    talloc_free(ptr);
-}
-
 int nss_process_init(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      struct confdb_ctx *cdb)
@@ -490,7 +481,7 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
                             nss_dp_reconnect_init, iter);
     }
 
-    err = sss_idmap_init(idmap_talloc, nctx, idmap_free,
+    err = sss_idmap_init(sss_idmap_talloc, nctx, sss_idmap_talloc_free,
                          &nctx->idmap_ctx);
     if (err != IDMAP_SUCCESS) {
         DEBUG(SSSDBG_FATAL_FAILURE, ("sss_idmap_init failed.\n"));
diff --git a/src/responder/pac/pacsrv.c b/src/responder/pac/pacsrv.c
index 22f87cb754f34efdf37035f9315b5f150f311061..a06d768bbc348f0a7f88428a6ce34d09bc3f5773 100644
--- a/src/responder/pac/pacsrv.c
+++ b/src/responder/pac/pacsrv.c
@@ -42,6 +42,7 @@
 #include "providers/data_provider.h"
 #include "monitor/monitor_interfaces.h"
 #include "sbus/sbus_client.h"
+#include "util/util_sss_idmap.h"
 
 #define SSS_PAC_PIPE_NAME "pac"
 #define DEFAULT_PAC_FD_LIMIT 8192
@@ -105,16 +106,6 @@ static void pac_dp_reconnect_init(struct sbus_connection *conn,
     /* nss_shutdown(rctx); */
 }
 
-static void *idmap_talloc(size_t size, void *pvt)
-{
-    return talloc_size(pvt, size);
-}
-
-static void idmap_free(void *ptr, void *pvt)
-{
-    talloc_free(ptr);
-}
-
 int pac_process_init(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      struct confdb_ctx *cdb)
@@ -186,7 +177,7 @@ int pac_process_init(TALLOC_CTX *mem_ctx,
                             pac_dp_reconnect_init, iter);
     }
 
-    err = sss_idmap_init(idmap_talloc, pac_ctx, idmap_free,
+    err = sss_idmap_init(sss_idmap_talloc, pac_ctx, sss_idmap_talloc_free,
                          &pac_ctx->idmap_ctx);
     if (err != IDMAP_SUCCESS) {
         DEBUG(SSSDBG_FATAL_FAILURE, ("sss_idmap_init failed.\n"));
diff --git a/src/util/util_sss_idmap.c b/src/util/util_sss_idmap.c
new file mode 100644
index 0000000000000000000000000000000000000000..4ce42507a35d17e68a65a6183eccc292d03e9fb4
--- /dev/null
+++ b/src/util/util_sss_idmap.c
@@ -0,0 +1,32 @@
+/*
+    Authors:
+        Pavel Březina <pbrezina@redhat.com>
+
+    Copyright (C) 2013 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <talloc.h>
+#include "util/util_sss_idmap.h"
+
+void *sss_idmap_talloc(size_t size, void *pvt)
+{
+    return talloc_size(pvt, size);
+}
+
+void sss_idmap_talloc_free(void *ptr, void *pvt)
+{
+    talloc_free(ptr);
+}
diff --git a/src/util/util_sss_idmap.h b/src/util/util_sss_idmap.h
new file mode 100644
index 0000000000000000000000000000000000000000..bde47271b37344e10d96a352da99e30958d279a9
--- /dev/null
+++ b/src/util/util_sss_idmap.h
@@ -0,0 +1,28 @@
+/*
+    Authors:
+        Pavel Březina <pbrezina@redhat.com>
+
+    Copyright (C) 2013 Red Hat
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __UTIL_SSS_IDMAP_H__
+#define __UTIL_SSS_IDMAP_H__
+
+void *sss_idmap_talloc(size_t size, void *pvt);
+
+void sss_idmap_talloc_free(void *ptr, void *pvt);
+
+#endif /* __UTIL_SSS_IDMAP_H__ */
-- 
1.7.11.7

