Hi,

this patch adds a special filter type for enumarations instead of using
'name=*'. I think this makes the code easier to read and understand,
allows the usage of a '*' in user or group names (although it might be
recommended to do without) and fixes trac ticket #676.

bye,
Sumit
From 7cacebfba9bfe41a2ad910ef19b690b4ab1be447 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sb...@redhat.com>
Date: Fri, 26 Nov 2010 10:46:11 +0100
Subject: [PATCH] Add a special filter type to handle enumerations

---
 src/providers/data_provider.h       |    1 +
 src/providers/data_provider_be.c    |    3 ++
 src/providers/ldap/ldap_id.c        |   23 ++++------------
 src/providers/proxy/proxy_id.c      |   48 +++++++++++-----------------------
 src/responder/common/responder_dp.c |    2 +-
 src/util/util.h                     |    2 +
 6 files changed, 29 insertions(+), 50 deletions(-)

diff --git a/src/providers/data_provider.h b/src/providers/data_provider.h
index 8574c11..e55b4ab 100644
--- a/src/providers/data_provider.h
+++ b/src/providers/data_provider.h
@@ -141,6 +141,7 @@
 
 #define BE_FILTER_NAME 1
 #define BE_FILTER_IDNUM 2
+#define BE_FILTER_ENUM 3
 
 #define BE_REQ_USER 0x0001
 #define BE_REQ_GROUP 0x0002
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 98c3f39..b4f3660 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -403,6 +403,9 @@ static int be_get_account_info(DBusMessage *message, struct 
sbus_connection *con
         } else if (strncmp(filter, "idnumber=", 9) == 0) {
             filter_type = BE_FILTER_IDNUM;
             filter_val = &filter[9];
+        } else if (strcmp(filter, ENUM_INDICATOR) == 0) {
+            filter_type = BE_FILTER_ENUM;
+            filter_val = NULL;
         } else {
             err_maj = DP_ERR_FATAL;
             err_min = EINVAL;
diff --git a/src/providers/ldap/ldap_id.c b/src/providers/ldap/ldap_id.c
index 1a9b2e0..9121a3e 100644
--- a/src/providers/ldap/ldap_id.c
+++ b/src/providers/ldap/ldap_id.c
@@ -210,13 +210,10 @@ static void users_get_done(struct tevent_req *subreq)
     }
 
     if (ret == ENOENT) {
-        if (strchr(state->name, '*')) {
-            /* it was an enumeration */
+        switch (state->filter_type) {
+        case BE_FILTER_ENUM:
             tevent_req_error(req, ret);
             return;
-        }
-
-        switch (state->filter_type) {
         case BE_FILTER_NAME:
             ret = sysdb_delete_user(state, state->sysdb,
                                     state->domain, state->name, 0);
@@ -442,13 +439,10 @@ static void groups_get_done(struct tevent_req *subreq)
     }
 
     if (ret == ENOENT) {
-        if (strchr(state->name, '*')) {
-            /* it was an enumeration */
+        switch (state->filter_type) {
+        case BE_FILTER_ENUM:
             tevent_req_error(req, ret);
             return;
-        }
-
-        switch (state->filter_type) {
         case BE_FILTER_NAME:
             ret = sysdb_delete_group(state, state->sysdb,
                                      state->domain, state->name, 0);
@@ -723,7 +717,7 @@ void sdap_account_info_handler(struct be_req *breq)
     case BE_REQ_USER: /* user */
 
         /* skip enumerations on demand */
-        if (strcmp(ar->filter_value, "*") == 0) {
+        if (ar->filter_type == BE_FILTER_ENUM) {
             return sdap_handler_done(breq, DP_ERR_OK, EOK, "Success");
         }
 
@@ -741,7 +735,7 @@ void sdap_account_info_handler(struct be_req *breq)
 
     case BE_REQ_GROUP: /* group */
 
-        if (strcmp(ar->filter_value, "*") == 0) {
+        if (ar->filter_type == BE_FILTER_ENUM) {
             return sdap_handler_done(breq, DP_ERR_OK, EOK, "Success");
         }
 
@@ -769,11 +763,6 @@ void sdap_account_info_handler(struct be_req *breq)
             err = "Invalid attr type";
             break;
         }
-        if (strchr(ar->filter_value, '*')) {
-            ret = EINVAL;
-            err = "Invalid filter value";
-            break;
-        }
         req = groups_by_user_send(breq, breq->be_ctx->ev, ctx,
                                   ar->filter_value);
         if (!req) ret = ENOMEM;
diff --git a/src/providers/proxy/proxy_id.c b/src/providers/proxy/proxy_id.c
index 7154138..4fd656f 100644
--- a/src/providers/proxy/proxy_id.c
+++ b/src/providers/proxy/proxy_id.c
@@ -1045,6 +1045,7 @@ void proxy_get_account_info(struct be_req *breq)
     uid_t uid;
     gid_t gid;
     int ret;
+    char *endptr;
 
     ar = talloc_get_type(breq->req_data, struct be_acct_req);
     ctx = talloc_get_type(breq->be_ctx->bet_info[BET_ID].pvt_bet_data,
@@ -1064,27 +1065,21 @@ void proxy_get_account_info(struct be_req *breq)
     switch (ar->entry_type & 0xFFF) {
     case BE_REQ_USER: /* user */
         switch (ar->filter_type) {
+        case BE_FILTER_ENUM:
+            ret = enum_users(breq, ctx, sysdb, domain);
+            break;
+
         case BE_FILTER_NAME:
-            if (strchr(ar->filter_value, '*')) {
-                ret = enum_users(breq, ctx, sysdb, domain);
-            } else {
-                ret = get_pw_name(breq, ctx, sysdb, domain, ar->filter_value);
-            }
+            ret = get_pw_name(breq, ctx, sysdb, domain, ar->filter_value);
             break;
 
         case BE_FILTER_IDNUM:
-            if (strchr(ar->filter_value, '*')) {
+            uid = (uid_t) strtouint32(ar->filter_value, &endptr, 0);
+            if (errno || *endptr || (ar->filter_value == endptr)) {
                 return proxy_reply(breq, DP_ERR_FATAL,
                                    EINVAL, "Invalid attr type");
-            } else {
-                char *endptr;
-                uid = (uid_t) strtouint32(ar->filter_value, &endptr, 0);
-                if (errno || *endptr || (ar->filter_value == endptr)) {
-                    return proxy_reply(breq, DP_ERR_FATAL,
-                                       EINVAL, "Invalid attr type");
-                }
-                ret = get_pw_uid(breq, ctx, sysdb, domain, uid);
             }
+            ret = get_pw_uid(breq, ctx, sysdb, domain, uid);
             break;
         default:
             return proxy_reply(breq, DP_ERR_FATAL,
@@ -1094,26 +1089,19 @@ void proxy_get_account_info(struct be_req *breq)
 
     case BE_REQ_GROUP: /* group */
         switch (ar->filter_type) {
+        case BE_FILTER_ENUM:
+            ret = enum_groups(breq, ctx, sysdb, domain);
+            break;
         case BE_FILTER_NAME:
-            if (strchr(ar->filter_value, '*')) {
-                ret = enum_groups(breq, ctx, sysdb, domain);
-            } else {
-                ret = get_gr_name(breq, ctx, sysdb, domain, ar->filter_value);
-            }
+            ret = get_gr_name(breq, ctx, sysdb, domain, ar->filter_value);
             break;
         case BE_FILTER_IDNUM:
-            if (strchr(ar->filter_value, '*')) {
+            gid = (gid_t) strtouint32(ar->filter_value, &endptr, 0);
+            if (errno || *endptr || (ar->filter_value == endptr)) {
                 return proxy_reply(breq, DP_ERR_FATAL,
                                    EINVAL, "Invalid attr type");
-            } else {
-                char *endptr;
-                gid = (gid_t) strtouint32(ar->filter_value, &endptr, 0);
-                if (errno || *endptr || (ar->filter_value == endptr)) {
-                    return proxy_reply(breq, DP_ERR_FATAL,
-                                       EINVAL, "Invalid attr type");
-                }
-                ret = get_gr_gid(breq, ctx, sysdb, domain, gid);
             }
+            ret = get_gr_gid(breq, ctx, sysdb, domain, gid);
             break;
         default:
             return proxy_reply(breq, DP_ERR_FATAL,
@@ -1126,10 +1114,6 @@ void proxy_get_account_info(struct be_req *breq)
             return proxy_reply(breq, DP_ERR_FATAL,
                                EINVAL, "Invalid filter type");
         }
-        if (strchr(ar->filter_value, '*')) {
-            return proxy_reply(breq, DP_ERR_FATAL,
-                               EINVAL, "Invalid filter value");
-        }
         if (ctx->ops.initgroups_dyn == NULL) {
             return proxy_reply(breq, DP_ERR_FATAL,
                                ENODEV, "Initgroups call not supported");
diff --git a/src/responder/common/responder_dp.c 
b/src/responder/common/responder_dp.c
index b2b5d40..8050e06 100644
--- a/src/responder/common/responder_dp.c
+++ b/src/responder/common/responder_dp.c
@@ -329,7 +329,7 @@ int sss_dp_send_acct_req(struct resp_ctx *rctx, TALLOC_CTX 
*callback_memctx,
         filter = talloc_asprintf(tmp_ctx, "idnumber=%u", opt_id);
         key.str = talloc_asprintf(tmp_ctx, "%...@%s", type, opt_id, domain);
     } else {
-        filter = talloc_strdup(tmp_ctx, "name=*");
+        filter = talloc_strdup(tmp_ctx, ENUM_INDICATOR);
         key.str = talloc_asprintf(tmp_ctx, "%...@%s", type, domain);
     }
     if (!filter || !key.str) {
diff --git a/src/util/util.h b/src/util/util.h
index b5da13c..12d3ff0 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -50,6 +50,8 @@ typedef int errno_t;
 
 #define _(STRING) gettext (STRING)
 
+#define ENUM_INDICATOR "*"
+
 extern const char *debug_prg_name;
 extern int debug_level;
 extern int debug_timestamps;
-- 
1.7.3.2

_______________________________________________
sssd-devel mailing list
sssd-devel@lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/sssd-devel

Reply via email to