From d0af7f68ca4ace27d2de6252df443290364d49c1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 19 Oct 2012 11:43:47 +0200
Subject: [PATCH 2/4] change responder contexts hierarchy

https://fedorahosted.org/sssd/ticket/1575

The hierarchy is now:
main_ctx -> responder_ctx -> specific_ctx

where specific_ctx is one of sudo, pam, etc.
---
 src/responder/autofs/autofssrv.c | 33 ++++++++++++++++++---------------
 src/responder/nss/nsssrv.c       | 33 ++++++++++++++++++---------------
 src/responder/pac/pacsrv.c       | 21 ++++++++++++---------
 src/responder/pam/pamsrv.c       | 18 ++++++++++--------
 src/responder/ssh/sshsrv.c       | 21 ++++++++++++---------
 src/responder/sudo/sudosrv.c     | 21 ++++++++++++---------
 6 files changed, 82 insertions(+), 65 deletions(-)

diff --git a/src/responder/autofs/autofssrv.c b/src/responder/autofs/autofssrv.c
index 9b0708629d31d917f2e12872114a072dabdff394..a4dc1b22d9d5c37d065728052336137447a8f92e 100644
--- a/src/responder/autofs/autofssrv.c
+++ b/src/responder/autofs/autofssrv.c
@@ -106,6 +106,7 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
                     struct tevent_context *ev,
                     struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *autofs_cmds;
     struct autofs_ctx *autofs_ctx;
     struct be_conn *iter;
@@ -113,20 +114,8 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
     int hret;
     int max_retries;
 
-    autofs_ctx = talloc_zero(mem_ctx, struct autofs_ctx);
-    if (!autofs_ctx) {
-        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing autofs_ctx\n"));
-        return ENOMEM;
-    }
-
-    ret = autofs_get_config(autofs_ctx, cdb);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_FATAL_FAILURE, ("Cannot read autofs configuration\n"));
-        goto fail;
-    }
-
     autofs_cmds = get_autofs_cmds();
-    ret = sss_process_init(autofs_ctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            autofs_cmds,
                            SSS_AUTOFS_SOCKET_NAME, NULL,
                            CONFDB_AUTOFS_CONF_ENTRY,
@@ -135,10 +124,24 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_autofs_interface,
                            "autofs",
                            &autofs_dp_interface,
-                           &autofs_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto fail;
     }
+
+    autofs_ctx = talloc_zero(rctx, struct autofs_ctx);
+    if (!autofs_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing autofs_ctx\n"));
+        return ENOMEM;
+    }
+
+    ret = autofs_get_config(autofs_ctx, cdb);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("Cannot read autofs configuration\n"));
+        goto fail;
+    }
+
+    autofs_ctx->rctx = rctx;
     autofs_ctx->rctx->pvt_ctx = autofs_ctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -170,7 +173,7 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(autofs_ctx);
+    talloc_free(rctx);
     return ret;
 }
 
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
index 764737db023cbdf9d52781342785091d0e9d4e16..f725009964f42d9e886897e65815dc12a0dfc7b1 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -326,6 +326,7 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *nss_cmds;
     struct be_conn *iter;
     struct nss_ctx *nctx;
@@ -334,21 +335,9 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
     int hret;
     int fd_limit;
 
-    nctx = talloc_zero(mem_ctx, struct nss_ctx);
-    if (!nctx) {
-        DEBUG(0, ("fatal error initializing nss_ctx\n"));
-        return ENOMEM;
-    }
-
-    ret = sss_ncache_init(nctx, &nctx->ncache);
-    if (ret != EOK) {
-        DEBUG(0, ("fatal error initializing negative cache\n"));
-        goto fail;
-    }
-
     nss_cmds = get_nss_cmds();
 
-    ret = sss_process_init(nctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            nss_cmds,
                            SSS_NSS_SOCKET_NAME, NULL,
                            CONFDB_NSS_CONF_ENTRY,
@@ -356,10 +345,24 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
                            NSS_SBUS_SERVICE_VERSION,
                            &monitor_nss_interface,
                            "NSS", &nss_dp_interface,
-                           &nctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto fail;
     }
+
+    nctx = talloc_zero(mem_ctx, struct nss_ctx);
+    if (!nctx) {
+        DEBUG(0, ("fatal error initializing nss_ctx\n"));
+        return ENOMEM;
+    }
+
+    ret = sss_ncache_init(rctx, &nctx->ncache);
+    if (ret != EOK) {
+        DEBUG(0, ("fatal error initializing negative cache\n"));
+        goto fail;
+    }
+
+    nctx->rctx = rctx;
     nctx->rctx->pvt_ctx = nctx;
 
     ret = nss_get_config(nctx, cdb);
@@ -444,7 +447,7 @@ int nss_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(nctx);
+    talloc_free(rctx);
     return ret;
 }
 
diff --git a/src/responder/pac/pacsrv.c b/src/responder/pac/pacsrv.c
index c6cac0942a266447ce46e974abc7c382941bce69..fe9e10f535ce2ddff5d581e2318b0486d915ee61 100644
--- a/src/responder/pac/pacsrv.c
+++ b/src/responder/pac/pacsrv.c
@@ -119,6 +119,7 @@ int pac_process_init(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *pac_cmds;
     struct be_conn *iter;
     struct pac_ctx *pac_ctx;
@@ -127,15 +128,9 @@ int pac_process_init(TALLOC_CTX *mem_ctx,
     int fd_limit;
     char *uid_str;
 
-    pac_ctx = talloc_zero(mem_ctx, struct pac_ctx);
-    if (!pac_ctx) {
-        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing pac_ctx\n"));
-        return ENOMEM;
-    }
-
     pac_cmds = get_pac_cmds();
 
-    ret = sss_process_init(pac_ctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            pac_cmds,
                            SSS_PAC_SOCKET_NAME, NULL,
                            CONFDB_PAC_CONF_ENTRY,
@@ -143,10 +138,18 @@ int pac_process_init(TALLOC_CTX *mem_ctx,
                            PAC_SBUS_SERVICE_VERSION,
                            &monitor_pac_interface,
                            "PAC", &pac_dp_interface,
-                           &pac_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto fail;
     }
+
+    pac_ctx = talloc_zero(rctx, struct pac_ctx);
+    if (!pac_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing pac_ctx\n"));
+        return ENOMEM;
+    }
+
+    pac_ctx->rctx = rctx;
     pac_ctx->rctx->pvt_ctx = pac_ctx;
 
 
@@ -207,7 +210,7 @@ int pac_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(pac_ctx);
+    talloc_free(rctx);
     return ret;
 }
 
diff --git a/src/responder/pam/pamsrv.c b/src/responder/pam/pamsrv.c
index 4a549526edd91c87583756dc030bfd509a1a7a4e..736a908b2c7afae11e17aa16b76c2c1deffcd151 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -106,6 +106,7 @@ static int pam_process_init(TALLOC_CTX *mem_ctx,
                             struct tevent_context *ev,
                             struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *pam_cmds;
     struct be_conn *iter;
     struct pam_ctx *pctx;
@@ -113,13 +114,8 @@ static int pam_process_init(TALLOC_CTX *mem_ctx,
     int id_timeout;
     int fd_limit;
 
-    pctx = talloc_zero(mem_ctx, struct pam_ctx);
-    if (!pctx) {
-        return ENOMEM;
-    }
-
     pam_cmds = get_pam_cmds();
-    ret = sss_process_init(pctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            pam_cmds,
                            SSS_PAM_SOCKET_NAME,
                            SSS_PAM_PRIV_SOCKET_NAME,
@@ -128,11 +124,17 @@ static int pam_process_init(TALLOC_CTX *mem_ctx,
                            SSS_PAM_SBUS_SERVICE_VERSION,
                            &monitor_pam_interface,
                            "PAM", &pam_dp_interface,
-                           &pctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto done;
     }
 
+    pctx = talloc_zero(mem_ctx, struct pam_ctx);
+    if (!pctx) {
+        return ENOMEM;
+    }
+
+    pctx->rctx = rctx;
     pctx->rctx->pvt_ctx = pctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -202,7 +204,7 @@ static int pam_process_init(TALLOC_CTX *mem_ctx,
 
 done:
     if (ret != EOK) {
-        talloc_free(pctx);
+        talloc_free(rctx);
     }
     return ret;
 }
diff --git a/src/responder/ssh/sshsrv.c b/src/responder/ssh/sshsrv.c
index 2cc8a8287bee42deea4da248e0e8033beec1c038..5e7833f90ddda9a5220a901f8b6771b592fa517b 100644
--- a/src/responder/ssh/sshsrv.c
+++ b/src/responder/ssh/sshsrv.c
@@ -84,20 +84,15 @@ int ssh_process_init(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *ssh_cmds;
     struct ssh_ctx *ssh_ctx;
     struct be_conn *iter;
     int ret;
     int max_retries;
 
-    ssh_ctx = talloc_zero(mem_ctx, struct ssh_ctx);
-    if (!ssh_ctx) {
-        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing ssh_ctx\n"));
-        return ENOMEM;
-    }
-
     ssh_cmds = get_ssh_cmds();
-    ret = sss_process_init(ssh_ctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            ssh_cmds,
                            SSS_SSH_SOCKET_NAME, NULL,
                            CONFDB_SSH_CONF_ENTRY,
@@ -106,10 +101,18 @@ int ssh_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_ssh_interface,
                            "SSH",
                            &ssh_dp_interface,
-                           &ssh_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto fail;
     }
+
+    ssh_ctx = talloc_zero(rctx, struct ssh_ctx);
+    if (!ssh_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing ssh_ctx\n"));
+        return ENOMEM;
+    }
+
+    ssh_ctx->rctx = rctx;
     ssh_ctx->rctx->pvt_ctx = ssh_ctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -157,7 +160,7 @@ int ssh_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(ssh_ctx);
+    talloc_free(rctx);
     return ret;
 }
 
diff --git a/src/responder/sudo/sudosrv.c b/src/responder/sudo/sudosrv.c
index c6fc2d3db6681f57f4f45b8483e1c0d9249a8041..44105d11a0066498e7fe5605a4a66cabd4e7070d 100644
--- a/src/responder/sudo/sudosrv.c
+++ b/src/responder/sudo/sudosrv.c
@@ -85,20 +85,15 @@ int sudo_process_init(TALLOC_CTX *mem_ctx,
                       struct tevent_context *ev,
                       struct confdb_ctx *cdb)
 {
+    struct resp_ctx *rctx;
     struct sss_cmd_table *sudo_cmds;
     struct sudo_ctx *sudo_ctx;
     struct be_conn *iter;
     int ret;
     int max_retries;
 
-    sudo_ctx = talloc_zero(mem_ctx, struct sudo_ctx);
-    if (!sudo_ctx) {
-        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing sudo_ctx\n"));
-        return ENOMEM;
-    }
-
     sudo_cmds = get_sudo_cmds();
-    ret = sss_process_init(sudo_ctx, ev, cdb,
+    ret = sss_process_init(mem_ctx, ev, cdb,
                            sudo_cmds,
                            SSS_SUDO_SOCKET_NAME, NULL,
                            CONFDB_SUDO_CONF_ENTRY,
@@ -107,10 +102,18 @@ int sudo_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_sudo_interface,
                            "SUDO",
                            &sudo_dp_interface,
-                           &sudo_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
         goto fail;
     }
+
+    sudo_ctx = talloc_zero(rctx, struct sudo_ctx);
+    if (!sudo_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing sudo_ctx\n"));
+        return ENOMEM;
+    }
+
+    sudo_ctx->rctx = rctx;
     sudo_ctx->rctx->pvt_ctx = sudo_ctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -147,7 +150,7 @@ int sudo_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(sudo_ctx);
+    talloc_free(rctx);
     return ret;
 }
 
-- 
1.7.11.4

