From 3dc1bbb6fa4353f0bb2cc2b9417900a6d4695240 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 9 Nov 2015 12:44:13 +0100
Subject: [PATCH 04/34] SUDO: simplify error handling

This patch removes state->error and uses only ret instead since
state->error was only duplication anyway.
---
 src/providers/ldap/sdap_async_sudo.c   | 41 ++++++++++------------------------
 src/providers/ldap/sdap_sudo.c         | 22 +++++++++---------
 src/providers/ldap/sdap_sudo.h         |  9 +++-----
 src/providers/ldap/sdap_sudo_refresh.c | 39 +++++++++++++-------------------
 4 files changed, 41 insertions(+), 70 deletions(-)

diff --git a/src/providers/ldap/sdap_async_sudo.c b/src/providers/ldap/sdap_async_sudo.c
index 24ee353fcfb3887197e0d1c98ec273e0e9003af0..fad936c398579070347340d3446ad959ba9d83ce 100644
--- a/src/providers/ldap/sdap_async_sudo.c
+++ b/src/providers/ldap/sdap_async_sudo.c
@@ -46,7 +46,6 @@ struct sdap_sudo_refresh_state {
     const char *sysdb_filter;   /* delete */
 
     int dp_error;
-    int error;
     char *highest_usn;
     size_t num_rules;
 };
@@ -131,8 +130,7 @@ struct tevent_req *sdap_sudo_refresh_send(TALLOC_CTX *mem_ctx,
     state->domain = be_ctx->domain;
     state->ldap_filter = talloc_strdup(state, ldap_filter);
     state->sysdb_filter = talloc_strdup(state, sysdb_filter);
-    state->dp_error = DP_ERR_OK;
-    state->error = EOK;
+    state->dp_error = DP_ERR_FATAL;
     state->highest_usn = NULL;
 
     if (state->ldap_filter == NULL) {
@@ -165,7 +163,6 @@ immediately:
 int sdap_sudo_refresh_recv(TALLOC_CTX *mem_ctx,
                            struct tevent_req *req,
                            int *dp_error,
-                           int *error,
                            char **usn,
                            size_t *num_rules)
 {
@@ -176,7 +173,6 @@ int sdap_sudo_refresh_recv(TALLOC_CTX *mem_ctx,
     TEVENT_REQ_RETURN_ON_ERROR(req);
 
     *dp_error = state->dp_error;
-    *error = state->error;
 
     if (usn != NULL && state->highest_usn != NULL) {
         *usn = talloc_steal(mem_ctx, state->highest_usn);
@@ -202,18 +198,14 @@ static int sdap_sudo_refresh_retry(struct tevent_req *req)
         if (state->sdap_op == NULL) {
             DEBUG(SSSDBG_CRIT_FAILURE, "sdap_id_op_create() failed\n");
             state->dp_error = DP_ERR_FATAL;
-            state->error = EIO;
-            return EIO;
+            return ENOMEM;
         }
     }
 
     subreq = sdap_id_op_connect_send(state->sdap_op, state, &ret);
     if (subreq == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "sdap_id_op_connect_send() failed: %d(%s)\n", ret, strerror(ret));
-        talloc_zfree(state->sdap_op);
-        state->dp_error = DP_ERR_FATAL;
-        state->error = ret;
+        DEBUG(SSSDBG_CRIT_FAILURE, "sdap_id_op_connect_send() failed: "
+                                   "%d(%s)\n", ret, strerror(ret));
         return ret;
     }
 
@@ -235,16 +227,12 @@ static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq)
     ret = sdap_id_op_connect_recv(subreq, &dp_error);
     talloc_zfree(subreq);
 
-    if (dp_error == DP_ERR_OFFLINE) {
-        talloc_zfree(state->sdap_op);
-        state->dp_error = DP_ERR_OFFLINE;
-        state->error = EAGAIN;
-        tevent_req_done(req);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "SUDO LDAP connection failed "
+                                   "[%d]: %s\n", ret, strerror(ret));
+        state->dp_error = dp_error;
+        tevent_req_error(req, ret);
         return;
-    } else if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE,
-              "SUDO LDAP connection failed - %s\n", strerror(ret));
-        goto fail;
     }
 
     DEBUG(SSSDBG_TRACE_FUNC, "SUDO LDAP connection successful\n");
@@ -254,18 +242,14 @@ static void sdap_sudo_refresh_connect_done(struct tevent_req *subreq)
                                          sdap_id_op_handle(state->sdap_op),
                                          state->ldap_filter);
     if (subreq == NULL) {
-        ret = EFAULT;
-        goto fail;
+        state->dp_error = DP_ERR_FATAL;
+        tevent_req_error(req, ENOMEM);
+        return;
     }
 
     tevent_req_set_callback(subreq, sdap_sudo_refresh_load_done, req);
 
     return;
-
-fail:
-    state->dp_error = DP_ERR_FATAL;
-    state->error = ret;
-    tevent_req_error(req, ret);
 }
 
 static struct tevent_req * sdap_sudo_load_sudoers_send(TALLOC_CTX *mem_ctx,
@@ -517,7 +501,6 @@ done:
         }
     }
 
-    state->error = ret;
     if (ret == EOK) {
         state->dp_error = DP_ERR_OK;
         tevent_req_done(req);
diff --git a/src/providers/ldap/sdap_sudo.c b/src/providers/ldap/sdap_sudo.c
index 9286f5090ae89801c5d3889771872549709a2a95..e759878f342f5e7639f723ab94a1b10559716bf0 100644
--- a/src/providers/ldap/sdap_sudo.c
+++ b/src/providers/ldap/sdap_sudo.c
@@ -135,8 +135,8 @@ static void sdap_sudo_reply(struct tevent_req *req)
 {
     struct be_req *be_req = NULL;
     struct be_sudo_req *sudo_req = NULL;
-    int dp_error = DP_ERR_OK;
-    int error = EOK;
+    int dp_error;
+    bool deleted;
     int ret;
 
     be_req = tevent_req_callback_data(req, struct be_req);
@@ -144,24 +144,24 @@ static void sdap_sudo_reply(struct tevent_req *req)
 
     switch (sudo_req->type) {
     case BE_REQ_SUDO_FULL:
-        ret = sdap_sudo_full_refresh_recv(req, &dp_error, &error);
+        ret = sdap_sudo_full_refresh_recv(req, &dp_error);
         break;
     case BE_REQ_SUDO_RULES:
-        ret = sdap_sudo_rules_refresh_recv(req, &dp_error, &error);
+        ret = sdap_sudo_rules_refresh_recv(req, &dp_error, &deleted);
+        if (ret == EOK && deleted == true) {
+            ret = ENOENT;
+        }
         break;
     default:
         DEBUG(SSSDBG_CRIT_FAILURE, "Invalid request type: %d\n",
                                     sudo_req->type);
-        ret = EINVAL;
+        dp_error = DP_ERR_FATAL;
+        ret = ERR_INTERNAL;
+        break;
     }
 
     talloc_zfree(req);
-    if (ret != EOK) {
-        sdap_handler_done(be_req, DP_ERR_FATAL, ret, strerror(ret));
-        return;
-    }
-
-    sdap_handler_done(be_req, dp_error, error, strerror(error));
+    sdap_handler_done(be_req, dp_error, ret, strerror(ret));
 }
 
 void sdap_sudo_handler(struct be_req *be_req)
diff --git a/src/providers/ldap/sdap_sudo.h b/src/providers/ldap/sdap_sudo.h
index 466f7f0a4bc2f99b190da53b6deebea0ffa379f3..38e1b88176f7b475c7a3fa42fd6b383110d37277 100644
--- a/src/providers/ldap/sdap_sudo.h
+++ b/src/providers/ldap/sdap_sudo.h
@@ -55,7 +55,6 @@ struct tevent_req *sdap_sudo_refresh_send(TALLOC_CTX *mem_ctx,
 int sdap_sudo_refresh_recv(TALLOC_CTX *mem_ctx,
                            struct tevent_req *req,
                            int *dp_error,
-                           int *error,
                            char **usn,
                            size_t *num_rules);
 
@@ -63,15 +62,13 @@ struct tevent_req *sdap_sudo_full_refresh_send(TALLOC_CTX *mem_ctx,
                                                struct sdap_sudo_ctx *sudo_ctx);
 
 int sdap_sudo_full_refresh_recv(struct tevent_req *req,
-                                int *dp_error,
-                                int *error);
+                                int *dp_error);
 
 struct tevent_req *sdap_sudo_smart_refresh_send(TALLOC_CTX *mem_ctx,
                                                 struct sdap_sudo_ctx *sudo_ctx);
 
 int sdap_sudo_smart_refresh_recv(struct tevent_req *req,
-                                 int *dp_error,
-                                 int *error);
+                                 int *dp_error);
 
 struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx,
                                                 struct sdap_sudo_ctx *sudo_ctx,
@@ -82,7 +79,7 @@ struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx,
 
 int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
                                  int *dp_error,
-                                 int *error);
+                                 bool *deleted);
 
 errno_t
 sdap_sudo_ptask_setup(struct be_ctx *be_ctx, struct sdap_sudo_ctx *sudo_ctx);
diff --git a/src/providers/ldap/sdap_sudo_refresh.c b/src/providers/ldap/sdap_sudo_refresh.c
index b350569baa68043d979bfa21f86d5af76e404f26..8e6ac54604ec28b758072881d8100a38a10f9ef2 100644
--- a/src/providers/ldap/sdap_sudo_refresh.c
+++ b/src/providers/ldap/sdap_sudo_refresh.c
@@ -195,7 +195,6 @@ struct sdap_sudo_full_refresh_state {
     struct sysdb_ctx *sysdb;
     struct sss_domain_info *domain;
     int dp_error;
-    int error;
 };
 
 static void sdap_sudo_full_refresh_done(struct tevent_req *subreq);
@@ -289,9 +288,9 @@ static void sdap_sudo_full_refresh_done(struct tevent_req *subreq)
     state = tevent_req_data(req, struct sdap_sudo_full_refresh_state);
 
     ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error,
-                                 &state->error, &highest_usn, NULL);
+                                 &highest_usn, NULL);
     talloc_zfree(subreq);
-    if (ret != EOK || state->dp_error != DP_ERR_OK || state->error != EOK) {
+    if (ret != EOK || state->dp_error != DP_ERR_OK) {
         goto done;
     }
 
@@ -326,8 +325,7 @@ done:
 }
 
 int sdap_sudo_full_refresh_recv(struct tevent_req *req,
-                                int *dp_error,
-                                int *error)
+                                int *dp_error)
 {
     struct sdap_sudo_full_refresh_state *state = NULL;
     state = tevent_req_data(req, struct sdap_sudo_full_refresh_state);
@@ -335,7 +333,6 @@ int sdap_sudo_full_refresh_recv(struct tevent_req *req,
     TEVENT_REQ_RETURN_ON_ERROR(req);
 
     *dp_error = state->dp_error;
-    *error = state->error;
 
     return EOK;
 }
@@ -444,15 +441,14 @@ static void sdap_sudo_smart_refresh_done(struct tevent_req *subreq)
     struct sdap_sudo_smart_refresh_state *state = NULL;
     char *highest_usn = NULL;
     int dp_error;
-    int error;
     int ret;
 
     req = tevent_req_callback_data(subreq, struct tevent_req);
     state = tevent_req_data(req, struct sdap_sudo_smart_refresh_state);
 
-    ret = sdap_sudo_refresh_recv(state, subreq, &dp_error, &error,
+    ret = sdap_sudo_refresh_recv(state, subreq, &dp_error,
                                  &highest_usn, NULL);
-    if (ret != EOK || dp_error != DP_ERR_OK || error != EOK) {
+    if (ret != EOK || dp_error != DP_ERR_OK) {
         goto done;
     }
 
@@ -473,15 +469,14 @@ done:
 }
 
 int sdap_sudo_smart_refresh_recv(struct tevent_req *req,
-                                 int *dp_error,
-                                 int *error)
+                                 int *dp_error)
 {
     struct sdap_sudo_smart_refresh_state *state = NULL;
     state = tevent_req_data(req, struct sdap_sudo_smart_refresh_state);
 
     TEVENT_REQ_RETURN_ON_ERROR(req);
 
-    return sdap_sudo_refresh_recv(state, state->subreq, dp_error, error,
+    return sdap_sudo_refresh_recv(state, state->subreq, dp_error,
                                   NULL, NULL);
 }
 
@@ -489,7 +484,7 @@ struct sdap_sudo_rules_refresh_state {
     struct sdap_id_ctx *id_ctx;
     size_t num_rules;
     int dp_error;
-    int error;
+    bool deleted;
 };
 
 static void sdap_sudo_rules_refresh_done(struct tevent_req *subreq);
@@ -615,10 +610,10 @@ static void sdap_sudo_rules_refresh_done(struct tevent_req *subreq)
     req = tevent_req_callback_data(subreq, struct tevent_req);
     state = tevent_req_data(req, struct sdap_sudo_rules_refresh_state);
 
-    ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error, &state->error,
+    ret = sdap_sudo_refresh_recv(state, subreq, &state->dp_error,
                                  &highest_usn, &downloaded_rules_num);
     talloc_zfree(subreq);
-    if (ret != EOK || state->dp_error != DP_ERR_OK || state->error != EOK) {
+    if (ret != EOK || state->dp_error != DP_ERR_OK) {
         goto done;
     }
 
@@ -627,9 +622,7 @@ static void sdap_sudo_rules_refresh_done(struct tevent_req *subreq)
         sdap_sudo_set_usn(state->id_ctx->srv_opts, highest_usn);
     }
 
-    if (downloaded_rules_num != state->num_rules) {
-        state->error = ENOENT;
-    }
+    state->deleted = downloaded_rules_num != state->num_rules ? true : false;
 
 done:
     if (ret != EOK) {
@@ -642,7 +635,7 @@ done:
 
 int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
                                  int *dp_error,
-                                 int *error)
+                                 bool *deleted)
 {
     struct sdap_sudo_rules_refresh_state *state = NULL;
     state = tevent_req_data(req, struct sdap_sudo_rules_refresh_state);
@@ -650,7 +643,7 @@ int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
     TEVENT_REQ_RETURN_ON_ERROR(req);
 
     *dp_error = state->dp_error;
-    *error = state->error;
+    *deleted = state->deleted;
 
     return EOK;
 }
@@ -672,9 +665,8 @@ static errno_t
 sdap_sudo_ptask_full_refresh_recv(struct tevent_req *req)
 {
     int dp_error;
-    int error;
 
-    return sdap_sudo_full_refresh_recv(req, &dp_error, &error);
+    return sdap_sudo_full_refresh_recv(req, &dp_error);
 }
 
 static struct tevent_req *
@@ -694,9 +686,8 @@ static errno_t
 sdap_sudo_ptask_smart_refresh_recv(struct tevent_req *req)
 {
     int dp_error;
-    int error;
 
-    return sdap_sudo_smart_refresh_recv(req, &dp_error, &error);
+    return sdap_sudo_smart_refresh_recv(req, &dp_error);
 }
 
 errno_t
-- 
2.1.0

