From 37542ee9bf271c650797e2d85a3ee402e8250e82 Mon Sep 17 00:00:00 2001
From: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
Date: Wed, 20 Jul 2016 18:08:40 +0300
Subject: [PATCH 06/15] NSS: Extract getbynam/id/sid search and reply parts

Extract the duplicate getbynam/id/sid search and reply parts into
functions to avoid maintenance cost and to prepare for insertion of
extra async steps.
---
 src/responder/nss/nsssrv_cmd.c | 299 +++++++++++++++++------------------------
 1 file changed, 120 insertions(+), 179 deletions(-)

diff --git a/src/responder/nss/nsssrv_cmd.c b/src/responder/nss/nsssrv_cmd.c
index 380038f..1b8f9cc 100644
--- a/src/responder/nss/nsssrv_cmd.c
+++ b/src/responder/nss/nsssrv_cmd.c
@@ -1484,6 +1484,65 @@ static int nss_cmd_getpwnam(struct cli_ctx *cctx)
     return nss_cmd_getbynam(SSS_NSS_GETPWNAM, cctx);
 }
 
+static int nss_cmd_getbynam_search_and_reply(struct nss_dom_ctx *dctx)
+{
+    errno_t ret;
+    const char *domname = dctx->domain->name;
+
+    DEBUG(SSSDBG_CONF_SETTINGS, "Requesting info for [%s] from [%s]\n",
+              dctx->cmdctx->name, domname?domname:"<ALL>");
+
+    dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
+
+    /* ok, find it ! */
+    switch (dctx->cmdctx->cmd) {
+    case SSS_NSS_GETPWNAM:
+        ret = nss_cmd_getpwnam_search(dctx);
+        if (ret == EOK) {
+            /* we have results to return */
+            ret = nss_cmd_getpw_send_reply(dctx, false);
+        } else if (ret == ENOENT
+                && dctx->rawname != NULL
+                && strchr(dctx->rawname, '@') != NULL) {
+            /* assume Kerberos principal */
+            ret = nss_cmd_assume_upn(dctx);
+        }
+        break;
+    case SSS_NSS_GETGRNAM:
+        ret = nss_cmd_getgrnam_search(dctx);
+        if (ret == EOK) {
+            /* we have results to return */
+            ret = nss_cmd_getgr_send_reply(dctx, false);
+        }
+        break;
+    case SSS_NSS_INITGR:
+        ret = nss_cmd_initgroups_search(dctx);
+        if (ret == EOK) {
+            /* we have results to return */
+            ret = nss_cmd_initgr_send_reply(dctx);
+        } else if (ret == ENOENT
+                && dctx->rawname != NULL
+                && strchr(dctx->rawname, '@') != NULL) {
+            /* assume Kerberos principal */
+            ret = nss_cmd_assume_upn(dctx);
+        }
+        break;
+    case SSS_NSS_GETSIDBYNAME:
+    case SSS_NSS_GETORIGBYNAME:
+        ret = nss_cmd_getsidby_search(dctx);
+        if (ret == EOK) {
+            ret = nss_cmd_getbysid_send_reply(dctx);
+        }
+        break;
+    default:
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
+              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
+        ret = EINVAL;
+    }
+
+    return ret;
+}
+
 static int nss_cmd_getbynam(enum sss_cli_command cmd, struct cli_ctx *cctx)
 {
 
@@ -1594,9 +1653,6 @@ static int nss_cmd_getbynam(enum sss_cli_command cmd, struct cli_ctx *cctx)
         goto done;
     }
 
-    DEBUG(SSSDBG_CONF_SETTINGS, "Requesting info for [%s] from [%s]\n",
-              cmdctx->name, domname?domname:"<ALL>");
-
     if (domname) {
         dctx->domain = responder_get_domain(cctx->rctx, domname);
         if (!dctx->domain) {
@@ -1619,53 +1675,7 @@ static int nss_cmd_getbynam(enum sss_cli_command cmd, struct cli_ctx *cctx)
         }
     }
 
-    dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
-
-    /* ok, find it ! */
-    switch (dctx->cmdctx->cmd) {
-    case SSS_NSS_GETPWNAM:
-        ret = nss_cmd_getpwnam_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getpw_send_reply(dctx, false);
-        } else if (ret == ENOENT
-                && dctx->rawname != NULL
-                && strchr(dctx->rawname, '@') != NULL) {
-            /* assume Kerberos principal */
-            ret = nss_cmd_assume_upn(dctx);
-        }
-        break;
-    case SSS_NSS_GETGRNAM:
-        ret = nss_cmd_getgrnam_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getgr_send_reply(dctx, false);
-        }
-        break;
-    case SSS_NSS_INITGR:
-        ret = nss_cmd_initgroups_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_initgr_send_reply(dctx);
-        } else if (ret == ENOENT
-                && dctx->rawname != NULL
-                && strchr(dctx->rawname, '@') != NULL) {
-            /* assume Kerberos principal */
-            ret = nss_cmd_assume_upn(dctx);
-        }
-        break;
-    case SSS_NSS_GETSIDBYNAME:
-    case SSS_NSS_GETORIGBYNAME:
-        ret = nss_cmd_getsidby_search(dctx);
-        if (ret == EOK) {
-            ret = nss_cmd_getbysid_send_reply(dctx);
-        }
-        break;
-    default:
-        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
-              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
-        ret = EINVAL;
-    }
+    ret = nss_cmd_getbynam_search_and_reply(dctx);
 
 done:
     return nss_cmd_done(cmdctx, ret);
@@ -1706,9 +1716,6 @@ static void nss_cmd_getbynam_done(struct tevent_req *req)
         /* Not fatal */
     }
 
-    DEBUG(SSSDBG_TRACE_FUNC, "Requesting info for [%s] from [%s]\n",
-              cmdctx->name, domname?domname:"<ALL>");
-
     if (domname) {
         dctx->domain = responder_get_domain(cctx->rctx, domname);
         if (dctx->domain == NULL) {
@@ -1721,53 +1728,7 @@ static void nss_cmd_getbynam_done(struct tevent_req *req)
         cmdctx->check_next = true;
     }
 
-    dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
-
-    /* ok, find it ! */
-    switch (dctx->cmdctx->cmd) {
-    case SSS_NSS_GETPWNAM:
-        ret = nss_cmd_getpwnam_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getpw_send_reply(dctx, false);
-        } else if (ret == ENOENT
-                    && dctx->rawname != NULL
-                    && strchr(dctx->rawname, '@') != NULL) {
-            /* assume Kerberos principal */
-            ret = nss_cmd_assume_upn(dctx);
-        }
-        break;
-    case SSS_NSS_GETGRNAM:
-        ret = nss_cmd_getgrnam_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getgr_send_reply(dctx, false);
-        }
-        break;
-    case SSS_NSS_INITGR:
-        ret = nss_cmd_initgroups_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_initgr_send_reply(dctx);
-        } else if (ret == ENOENT
-                    && dctx->rawname != NULL
-                    && strchr(dctx->rawname, '@') != NULL) {
-            /* assume Kerberos principal */
-            ret = nss_cmd_assume_upn(dctx);
-        }
-        break;
-    case SSS_NSS_GETSIDBYNAME:
-    case SSS_NSS_GETORIGBYNAME:
-        ret = nss_cmd_getsidby_search(dctx);
-        if (ret == EOK) {
-            ret = nss_cmd_getbysid_send_reply(dctx);
-        }
-        break;
-    default:
-        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
-              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
-        ret = EINVAL;
-    }
+    ret = nss_cmd_getbynam_search_and_reply(dctx);
 
 done:
     nss_cmd_done(cmdctx, ret);
@@ -1917,6 +1878,62 @@ static int nss_cmd_getpwuid(struct cli_ctx *cctx)
     return nss_cmd_getbyid(SSS_NSS_GETPWUID, cctx);
 }
 
+static int nss_cmd_getbyid_search_and_reply(struct nss_dom_ctx *dctx)
+{
+    int ret;
+
+    /* ok, find it ! */
+    switch(dctx->cmdctx->cmd) {
+    case SSS_NSS_GETPWUID:
+        ret = nss_cmd_getpwuid_search(dctx);
+        if (ret == EOK) {
+            /* we have results to return */
+            ret = nss_cmd_getpw_send_reply(dctx, true);
+        }
+        break;
+    case SSS_NSS_GETGRGID:
+        ret = nss_cmd_getgrgid_search(dctx);
+        if (ret == EOK) {
+            /* we have results to return */
+            ret = nss_cmd_getgr_send_reply(dctx, true);
+        }
+        break;
+    case SSS_NSS_GETNAMEBYSID:
+    case SSS_NSS_GETIDBYSID:
+        if (dctx->domain == NULL) {
+            ret = responder_get_domain_by_id(dctx->cmdctx->cctx->rctx,
+                                             dctx->cmdctx->secid,
+                                             &dctx->domain);
+            if (ret != EOK) {
+                DEBUG(SSSDBG_OP_FAILURE, "Cannot find domain for SID [%s].\n",
+                                          dctx->cmdctx->secid);
+                ret = ENOENT;
+                break;
+            }
+        }
+
+        dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
+
+        ret = nss_cmd_getbysid_search(dctx);
+        if (ret == EOK) {
+            ret = nss_cmd_getbysid_send_reply(dctx);
+        }
+        break;
+    case SSS_NSS_GETSIDBYID:
+        ret = nss_cmd_getsidby_search(dctx);
+        if (ret == EOK) {
+            ret = nss_cmd_getbysid_send_reply(dctx);
+        }
+        break;
+    default:
+        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
+              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
+        ret = EINVAL;
+    }
+
+    return ret;
+}
+
 static int nss_cmd_getbyid(enum sss_cli_command cmd, struct cli_ctx *cctx)
 {
     struct cli_protocol *pctx;
@@ -2027,33 +2044,7 @@ static int nss_cmd_getbyid(enum sss_cli_command cmd, struct cli_ctx *cctx)
         goto done;
     }
 
-    /* ok, find it ! */
-    switch(dctx->cmdctx->cmd) {
-    case SSS_NSS_GETPWUID:
-        ret = nss_cmd_getpwuid_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getpw_send_reply(dctx, true);
-        }
-        break;
-    case SSS_NSS_GETGRGID:
-        ret = nss_cmd_getgrgid_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getgr_send_reply(dctx, true);
-        }
-        break;
-    case SSS_NSS_GETSIDBYID:
-        ret = nss_cmd_getsidby_search(dctx);
-        if (ret == EOK) {
-            ret = nss_cmd_getbysid_send_reply(dctx);
-        }
-        break;
-    default:
-        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
-              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
-        ret = EINVAL;
-    }
+    ret = nss_cmd_getbyid_search_and_reply(dctx);
 
 done:
     return nss_cmd_done(cmdctx, ret);
@@ -2077,51 +2068,7 @@ static void nss_cmd_getbyid_done(struct tevent_req *req)
         /* Not fatal */
     }
 
-    /* ok, find it ! */
-    switch(dctx->cmdctx->cmd) {
-    case SSS_NSS_GETPWUID:
-        ret = nss_cmd_getpwuid_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getpw_send_reply(dctx, true);
-        }
-        break;
-    case SSS_NSS_GETGRGID:
-        ret = nss_cmd_getgrgid_search(dctx);
-        if (ret == EOK) {
-            /* we have results to return */
-            ret = nss_cmd_getgr_send_reply(dctx, true);
-        }
-        break;
-    case SSS_NSS_GETNAMEBYSID:
-    case SSS_NSS_GETIDBYSID:
-        ret = responder_get_domain_by_id(cmdctx->cctx->rctx, cmdctx->secid,
-                                         &dctx->domain);
-        if (ret != EOK) {
-            DEBUG(SSSDBG_OP_FAILURE, "Cannot find domain for SID [%s].\n",
-                                      cmdctx->secid);
-            ret = ENOENT;
-            goto done;
-        }
-
-        dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
-
-        ret = nss_cmd_getbysid_search(dctx);
-        if (ret == EOK) {
-            ret = nss_cmd_getbysid_send_reply(dctx);
-        }
-        break;
-    case SSS_NSS_GETSIDBYID:
-        ret = nss_cmd_getsidby_search(dctx);
-        if (ret == EOK) {
-            ret = nss_cmd_getbysid_send_reply(dctx);
-        }
-        break;
-    default:
-        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid command [%d][%s].\n",
-              dctx->cmdctx->cmd, sss_cmd2str(dctx->cmdctx->cmd));
-        ret = EINVAL;
-    }
+    ret = nss_cmd_getbyid_search_and_reply(dctx);
 
 done:
     nss_cmd_done(cmdctx, ret);
@@ -5317,13 +5264,7 @@ static int nss_cmd_getbysid(enum sss_cli_command cmd, struct cli_ctx *cctx)
     DEBUG(SSSDBG_CONF_SETTINGS, "Requesting info for [%s] from [%s]\n",
               cmdctx->secid, dctx->domain->name);
 
-    dctx->check_provider = NEED_CHECK_PROVIDER(dctx->domain->provider);
-
-    /* ok, find it ! */
-    ret = nss_cmd_getbysid_search(dctx);
-    if (ret == EOK) {
-        ret = nss_cmd_getbysid_send_reply(dctx);
-    }
+    ret = nss_cmd_getbyid_search_and_reply(dctx);
 
 done:
     return nss_cmd_done(cmdctx, ret);
-- 
2.8.1

