From ecddd3947789fb3ff2852277e33c9734d2bf9928 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Mon, 23 Apr 2012 16:33:30 +0200
Subject: [PATCH 10/24] sudo ldap provider: provide API for refresh of
 specific rules

---
 src/providers/ldap/sdap_sudo.c |   77 ++++++++++++++++++++++++++++++++++++++++
 src/providers/ldap/sdap_sudo.h |    3 +-
 2 files changed, 79 insertions(+), 1 deletions(-)

diff --git a/src/providers/ldap/sdap_sudo.c b/src/providers/ldap/sdap_sudo.c
index 1e116064d7a1ba43321262f94fa34c7116836a0d..9f3e94b9edff18f7b81bf785b8486dc1817b6991 100644
--- a/src/providers/ldap/sdap_sudo.c
+++ b/src/providers/ldap/sdap_sudo.c
@@ -316,3 +316,80 @@ static void sdap_sudo_full_refresh_done(struct tevent_req *subreq)
 
     tevent_req_done(req);
 }
+
+/* issue refresh of specific sudo rules */
+static struct tevent_req *sdap_sudo_rules_refresh_send(TALLOC_CTX *mem_ctx,
+                                                       struct be_ctx *be_ctx,
+                                                       struct sdap_options *opts,
+                                                       struct sdap_id_conn_cache *conn_cache,
+                                                       char **rules)
+{
+    struct tevent_req *req = NULL;
+    TALLOC_CTX *tmp_ctx = NULL;
+    char *ldap_filter = NULL;
+    char *sysdb_filter = NULL;
+    int i;
+
+    if (rules == NULL) {
+        return NULL;
+    }
+
+    tmp_ctx = talloc_new(NULL);
+    if (tmp_ctx == NULL) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed\n"));
+        return NULL;
+    }
+
+    /* Download only selected rules from LDAP */
+    /* Remove all selected rules from cache */
+    for (i = 0; rules[i] != NULL; i++) {
+        ldap_filter = talloc_asprintf_append_buffer(ldap_filter, "(%s=%s)",
+                                     opts->sudorule_map[SDAP_AT_SUDO_NAME].name,
+                                     rules[i]);
+        if (ldap_filter == NULL) {
+            goto done;
+        }
+
+        sysdb_filter = talloc_asprintf_append_buffer(sysdb_filter, "(%s=%s)",
+                                                     SYSDB_SUDO_CACHE_AT_CN,
+                                                     rules[i]);
+        if (sysdb_filter == NULL) {
+            goto done;
+        }
+    }
+
+    ldap_filter = talloc_asprintf(tmp_ctx, "(&"SDAP_SUDO_FILTER_CLASS"(|%s))",
+                                  opts->sudorule_map[SDAP_OC_SUDORULE].name,
+                                  ldap_filter);
+    if (ldap_filter == NULL) {
+        goto done;
+    }
+
+    sysdb_filter = talloc_asprintf(tmp_ctx, "(&(%s=%s)(|%s))",
+                                   SYSDB_OBJECTCLASS, SYSDB_SUDO_CACHE_AT_OC,
+                                   ldap_filter);
+    if (sysdb_filter == NULL) {
+        goto done;
+    }
+
+    req = sdap_sudo_refresh_send(mem_ctx, be_ctx, opts, conn_cache,
+                                 ldap_filter, sysdb_filter);
+    if (req == NULL) {
+        goto done;
+    }
+
+    /* free filters with the request */
+    talloc_steal(req, ldap_filter);
+    talloc_steal(req, sysdb_filter);
+
+done:
+    talloc_free(tmp_ctx);
+    return req;
+}
+
+static int sdap_sudo_rules_refresh_recv(struct tevent_req *req,
+                                        int *dp_error,
+                                        int *error)
+{
+    return sdap_sudo_refresh_recv(req, dp_error, error);
+}
diff --git a/src/providers/ldap/sdap_sudo.h b/src/providers/ldap/sdap_sudo.h
index f119730a5269a0e02b15013a80305dbf3fa5b2c8..7be69841070dc2de32b79c2cbc9cef9176283a2e 100644
--- a/src/providers/ldap/sdap_sudo.h
+++ b/src/providers/ldap/sdap_sudo.h
@@ -62,7 +62,8 @@ int sdap_sudo_timer_recv(TALLOC_CTX *mem_ctx,
 
 /* (&(objectClass=sudoRole)(|(cn=defaults)(sudoUser=ALL)%s)) */
 #define SDAP_SUDO_FILTER_USER "(&(objectClass=%s)(|(%s=%s)(%s=ALL)%s))"
-#define SDAP_SUDO_FILTER_ALL  "(objectClass=%s)"
+#define SDAP_SUDO_FILTER_CLASS "(objectClass=%s)"
+#define SDAP_SUDO_FILTER_ALL   SDAP_SUDO_FILTER_CLASS
 #define SDAP_SUDO_FILTER_DEFAULTS  "(&(objectClass=%s)(%s=%s))"
 #define SDAP_SUDO_DEFAULTS    "defaults"
 
-- 
1.7.6.5

