From 4ac7861e54e3e92256c5afbd22ef1a76251c8c95 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/3] 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 | 35 ++++++++++++++++++++---------------
 src/responder/nss/nsssrv.c       | 35 ++++++++++++++++++++---------------
 src/responder/pac/pacsrv.c       | 23 ++++++++++++++---------
 src/responder/pam/pamsrv.c       | 20 ++++++++++++--------
 src/responder/ssh/sshsrv.c       | 23 ++++++++++++++---------
 src/responder/sudo/sudosrv.c     | 23 ++++++++++++++---------
 6 files changed, 94 insertions(+), 65 deletions(-)

diff --git a/src/responder/autofs/autofssrv.c b/src/responder/autofs/autofssrv.c
index ef90207fec17bf0700af40ca678483d3157f74f9..ea4c049467a4ee46c79a6c6df7db65f2d97ab894 100644
--- a/src/responder/autofs/autofssrv.c
+++ b/src/responder/autofs/autofssrv.c
@@ -128,6 +128,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;
@@ -135,20 +136,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,
@@ -157,10 +146,26 @@ autofs_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_autofs_interface,
                            "autofs",
                            &autofs_dp_interface,
-                           &autofs_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    autofs_ctx = talloc_zero(rctx, struct autofs_ctx);
+    if (!autofs_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing autofs_ctx\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+
+    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 */
@@ -193,7 +198,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 e5334129d677cf581637c79305f56df8916e855c..709912c21bcc5e0f57551d281a7bad72115e72b3 100644
--- a/src/responder/nss/nsssrv.c
+++ b/src/responder/nss/nsssrv.c
@@ -395,6 +395,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;
@@ -403,21 +404,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,
@@ -425,10 +414,26 @@ 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) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    nctx = talloc_zero(rctx, struct nss_ctx);
+    if (!nctx) {
+        DEBUG(0, ("fatal error initializing nss_ctx\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+
+    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);
@@ -513,7 +518,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 29447c4713bb2a713e90e89fc28a724e852e7aca..9bc276631085d4858f07cc4a5b05e9c65fa47c70 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,20 @@ 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) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    pac_ctx = talloc_zero(rctx, struct pac_ctx);
+    if (!pac_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing pac_ctx\n"));
+        ret = ENOMEM;
         goto fail;
     }
+
+    pac_ctx->rctx = rctx;
     pac_ctx->rctx->pvt_ctx = pac_ctx;
 
 
@@ -207,7 +212,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 e5bb9009b16693d0f7947a5a4c5ac93abdb4231d..c71ef07ca32af67ad08043ac70df1c0474b2721c 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -107,6 +107,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;
@@ -114,13 +115,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,
@@ -129,11 +125,19 @@ 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) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    pctx = talloc_zero(rctx, struct pam_ctx);
+    if (!pctx) {
+        ret = ENOMEM;
         goto done;
     }
 
+    pctx->rctx = rctx;
     pctx->rctx->pvt_ctx = pctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -203,7 +207,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 fe6956aba1746684695b379a3324eeefec133e84..8a66f2239ac370218ec48d4cfc003d40dc1b7aec 100644
--- a/src/responder/ssh/sshsrv.c
+++ b/src/responder/ssh/sshsrv.c
@@ -85,20 +85,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,
@@ -107,10 +102,20 @@ int ssh_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_ssh_interface,
                            "SSH",
                            &ssh_dp_interface,
-                           &ssh_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    ssh_ctx = talloc_zero(rctx, struct ssh_ctx);
+    if (!ssh_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing ssh_ctx\n"));
+        ret = ENOMEM;
         goto fail;
     }
+
+    ssh_ctx->rctx = rctx;
     ssh_ctx->rctx->pvt_ctx = ssh_ctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -158,7 +163,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 4081760106d9ceb612d3531ade0a89abf22022e4..a6344a99a3dfb8cb453c2318a5740cc520a66a0c 100644
--- a/src/responder/sudo/sudosrv.c
+++ b/src/responder/sudo/sudosrv.c
@@ -86,20 +86,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,
@@ -108,10 +103,20 @@ int sudo_process_init(TALLOC_CTX *mem_ctx,
                            &monitor_sudo_interface,
                            "SUDO",
                            &sudo_dp_interface,
-                           &sudo_ctx->rctx);
+                           &rctx);
     if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("sss_process_init() failed\n"));
+        return ret;
+    }
+
+    sudo_ctx = talloc_zero(rctx, struct sudo_ctx);
+    if (!sudo_ctx) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing sudo_ctx\n"));
+        ret = ENOMEM;
         goto fail;
     }
+
+    sudo_ctx->rctx = rctx;
     sudo_ctx->rctx->pvt_ctx = sudo_ctx;
 
     /* Enable automatic reconnection to the Data Provider */
@@ -148,7 +153,7 @@ int sudo_process_init(TALLOC_CTX *mem_ctx,
     return EOK;
 
 fail:
-    talloc_free(sudo_ctx);
+    talloc_free(rctx);
     return ret;
 }
 
-- 
1.7.11.7

