From 74e84edcc341a80f15d5431a42122a3eae4d995c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 9 May 2012 14:07:13 +0200
Subject: [PATCH 25/29] sudo ldap provider: add smart refresh API

---
 src/providers/ldap/sdap_sudo.c |  119 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 119 insertions(+), 0 deletions(-)

diff --git a/src/providers/ldap/sdap_sudo.c b/src/providers/ldap/sdap_sudo.c
index 0a3a5805f8b3132756c0d8cd4596b5631b1ebaac..c4b219776b182974b37018d3afe0472d14ef1fb0 100644
--- a/src/providers/ldap/sdap_sudo.c
+++ b/src/providers/ldap/sdap_sudo.c
@@ -55,6 +55,21 @@ static int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
                                         int *dp_error,
                                         int *error);
 
+struct sdap_sudo_smart_refresh_state {
+    struct tevent_req *subreq;
+    struct sdap_id_ctx *id_ctx;
+    struct sysdb_ctx *sysdb;
+};
+
+static struct tevent_req *sdap_sudo_smart_refresh_send(TALLOC_CTX *mem_ctx,
+                                                      struct sdap_id_ctx *id_ctx);
+
+static void sdap_sudo_smart_refresh_done(struct tevent_req *subreq);
+
+static int sdap_sudo_smart_refresh_recv(struct tevent_req *req,
+                                        int *dp_error,
+                                        int *error);
+
 static void sdap_sudo_periodical_full_refresh_done(struct tevent_req *req);
 
 static void
@@ -437,6 +452,110 @@ static int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
     return sdap_sudo_refresh_recv(req, req, dp_error, error, NULL);
 }
 
+/* issue smart refresh of sudo rules */
+static struct tevent_req *sdap_sudo_smart_refresh_send(TALLOC_CTX *mem_ctx,
+                                                       struct sdap_id_ctx *id_ctx)
+{
+    struct tevent_req *req = NULL;
+    struct tevent_req *subreq = NULL;
+    struct sdap_attr_map *map = id_ctx->opts->sudorule_map;
+    struct sdap_server_opts *srv_opts = id_ctx->srv_opts;
+    struct sdap_sudo_smart_refresh_state *state = NULL;
+    char *ldap_filter = NULL;
+
+    if (srv_opts == NULL || srv_opts->max_sudo_value == 0) {
+        /* Perform full refresh */
+        DEBUG(SSSDBG_TRACE_FUNC, ("Unknown USN, performing full refresh instead"
+                                  " of smart refresh of sudo rules!\n"));
+        return sdap_sudo_full_refresh_send(mem_ctx, id_ctx);
+    }
+
+    req = tevent_req_create(mem_ctx, &state, struct sdap_sudo_smart_refresh_state);
+    if (req == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n"));
+        goto fail;
+    }
+
+    state->id_ctx = id_ctx;
+    state->sysdb = id_ctx->be->sysdb;
+
+    /* Download all rules from LDAP that are newer than usn */
+    ldap_filter = talloc_asprintf(req, "(&(objectclass=%s)(%s>=%s)(!(%s=%s)))",
+                                  map[SDAP_OC_SUDORULE].name,
+                                  map[SDAP_AT_SUDO_USN].name,
+                                  srv_opts->max_sudo_value,
+                                  map[SDAP_AT_SUDO_USN].name,
+                                  srv_opts->max_sudo_value);
+    if (ldap_filter == NULL) {
+        goto fail;
+    }
+
+    /* Do not remove any rules that are already in the sysdb
+     * sysdb_filter = NULL; */
+
+    DEBUG(SSSDBG_TRACE_FUNC, ("Issuing a smart refresh of sudo rules "
+                              "(USN >= %s)\n", srv_opts->max_sudo_value));
+
+    subreq = sdap_sudo_refresh_send(req, id_ctx->be, id_ctx->opts,
+                                    id_ctx->conn_cache,
+                                    ldap_filter, NULL);
+    if (subreq == NULL) {
+        goto fail;
+    }
+
+    state->subreq = subreq;
+    tevent_req_set_callback(subreq, sdap_sudo_smart_refresh_done, req);
+
+    /* free filters with the request */
+    talloc_steal(subreq, ldap_filter);
+
+    return req;
+
+fail:
+    talloc_zfree(req);
+    return NULL;
+}
+
+static int sdap_sudo_smart_refresh_recv(struct tevent_req *req,
+                                        int *dp_error,
+                                        int *error)
+{
+    struct sdap_sudo_full_refresh_state *state = NULL;
+    state = tevent_req_data(req, struct sdap_sudo_full_refresh_state);
+
+    TEVENT_REQ_RETURN_ON_ERROR(req);
+
+    return sdap_sudo_refresh_recv(state, state->subreq, dp_error, error, NULL);
+}
+
+static void sdap_sudo_smart_refresh_done(struct tevent_req *subreq)
+{
+    struct tevent_req *req = NULL;
+    struct sdap_sudo_full_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_full_refresh_state);
+
+    ret = sdap_sudo_refresh_recv(state, subreq, &dp_error, &error, &highest_usn);
+    if (ret != EOK || dp_error != DP_ERR_OK || error != EOK) {
+        tevent_req_error(req, ret);
+        return;
+    }
+
+    DEBUG(SSSDBG_TRACE_FUNC, ("Successful smart refresh of sudo rules\n"));
+
+    /* set highest usn */
+    if (highest_usn != NULL) {
+        sdap_sudo_set_usn(state->id_ctx->srv_opts, highest_usn);
+    }
+
+    tevent_req_done(req);
+}
+
 static void sdap_sudo_periodical_full_refresh_done(struct tevent_req *req)
 {
     struct tevent_req *subreq = NULL; /* req from sdap_sudo_full_refresh_send() */
-- 
1.7.6.5

