Replace ldb_search() with ldb_search_exp_fmt(), like in Samba 4.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 21 Jan 2009 15:15:53 +0000 (16:15 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 21 Jan 2009 15:15:53 +0000 (16:15 +0100)
source3/groupdb/mapping_ldb.c
source3/lib/ldb/common/ldb.c
source3/lib/ldb/examples/ldbreader.c
source3/lib/ldb/include/ldb.h
source3/lib/ldb/modules/ldb_map.c
source3/lib/ldb/nssldb/ldb-grp.c
source3/lib/ldb/nssldb/ldb-pwd.c
source3/lib/ldb/tools/ad2oLschema.c
source3/lib/ldb/tools/ldbedit.c
source3/lib/ldb/tools/ldbtest.c

index 5c43af531f0809017f925e78fc1a3f792bb75492..af99b86c4501f06471f062db700943c8557e2658 100644 (file)
@@ -243,24 +243,16 @@ failed:
 static bool get_group_map_from_gid(gid_t gid, GROUP_MAP *map)
 {
        int ret;
-       char *expr;
        struct ldb_result *res=NULL;
 
-       expr = talloc_asprintf(ldb, "(&(gidNumber=%u)(objectClass=groupMap))", 
-                              (unsigned)gid);
-       if (expr == NULL) goto failed;
-
-       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, NULL, expr);
-       talloc_steal(expr, res);
+       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(&(gidNumber=%u)(objectClass=groupMap))", (unsigned)gid);
        if (ret != LDB_SUCCESS || res->count != 1) goto failed;
        
        if (!msg_to_group_map(res->msgs[0], map)) goto failed;
 
-       talloc_free(expr);
        return True;
 
 failed:
-       talloc_free(expr);
        return False;
 }
 
@@ -270,23 +262,16 @@ failed:
 static bool get_group_map_from_ntname(const char *name, GROUP_MAP *map)
 {
        int ret;
-       char *expr;
        struct ldb_result *res=NULL;
 
-       expr = talloc_asprintf(ldb, "(&(ntName=%s)(objectClass=groupMap))", name);
-       if (expr == NULL) goto failed;
-
-       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, NULL, expr);
-       talloc_steal(expr, res);
+       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, NULL, "(&(ntName=%s)(objectClass=groupMap))", name);
        if (ret != LDB_SUCCESS || res->count != 1) goto failed;
        
        if (!msg_to_group_map(res->msgs[0], map)) goto failed;
 
-       talloc_free(expr);
        return True;
 
 failed:
-       talloc_free(expr);
        return False;
 }
 
@@ -317,7 +302,6 @@ static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_
                               size_t *p_num_entries, bool unix_only)
 {
        int i, ret;
-       char *expr;
        fstring name;
        struct ldb_result *res = NULL;
        struct ldb_dn *basedn=NULL;
@@ -326,14 +310,6 @@ static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_
        tmp_ctx = talloc_new(ldb);
        if (tmp_ctx == NULL) goto failed;
 
-       if (sid_name_use == SID_NAME_UNKNOWN) {
-               expr = talloc_asprintf(tmp_ctx, "(&(objectClass=groupMap))");
-       } else {
-               expr = talloc_asprintf(tmp_ctx, "(&(sidNameUse=%u)(objectClass=groupMap))",
-                                      sid_name_use);
-       }
-       if (expr == NULL) goto failed;
-
        /* we do a subtree search on the domain */
        if (domsid != NULL) {
                sid_to_fstring(name, domsid);
@@ -341,7 +317,15 @@ static bool enum_group_mapping(const DOM_SID *domsid, enum lsa_SidType sid_name_
                if (basedn == NULL) goto failed;
        }
 
-       ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, expr);
+       if (sid_name_use == SID_NAME_UNKNOWN) {
+               ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, 
+                                                "(&(objectClass=groupMap))");
+       } else {
+               ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, 
+                                                "(&(sidNameUse=%u)(objectClass=groupMap))",
+                                                sid_name_use);
+       }
+
        talloc_steal(tmp_ctx, res);
        if (ret != LDB_SUCCESS) goto failed;
 
@@ -380,7 +364,6 @@ static NTSTATUS one_alias_membership(const DOM_SID *member,
                NULL
        };
        DOM_SID alias;
-       char *expr;
        int ret, i;
        struct ldb_result *res=NULL;
        fstring string_sid;
@@ -390,12 +373,7 @@ static NTSTATUS one_alias_membership(const DOM_SID *member,
                return NT_STATUS_INVALID_PARAMETER;
        }
 
-       expr = talloc_asprintf(ldb, "(&(member=%s)(objectClass=groupMap))", 
-                              string_sid);
-       if (expr == NULL) goto failed;
-
-       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, attrs, expr);
-       talloc_steal(expr, res);
+       ret = ldb_search(ldb, ldb, &res, NULL, LDB_SCOPE_SUBTREE, attrs, "(&(member=%s)(objectClass=groupMap))", string_sid);
        if (ret != LDB_SUCCESS) {
                goto failed;
        }
@@ -414,11 +392,9 @@ static NTSTATUS one_alias_membership(const DOM_SID *member,
                }
        }
 
-       talloc_free(expr);
        return NT_STATUS_OK;
 
 failed:
-       talloc_free(expr);
        return status;
 }
 
index 495047f3a1ee0beb179925af6256f6338e078b9b..6e28528dbf358f6f969d7018ddd2295e51d6631f 100644 (file)
@@ -745,7 +745,7 @@ int ldb_build_rename_req(struct ldb_request **ret_req,
   note that ldb_search() will automatically replace a NULL 'base' value with the 
   defaultNamingContext from the rootDSE if available.
 */
-int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
+static int _ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                           struct ldb_result **_res,
                           const struct ldb_dn *base,
                           enum ldb_scope scope,
@@ -799,7 +799,7 @@ done:
  takes a memory context where results are allocated
 */
 
-int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
+int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_result **result,
                         struct ldb_dn *base, enum ldb_scope scope, const char * const *attrs,
                         const char *exp_fmt, ...)
 {
@@ -819,7 +819,7 @@ int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, struct ldb_
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ret = ldb_search(ldb, ldb, &res, base, scope, attrs, expression);
+       ret = _ldb_search(ldb, ldb, &res, base, scope, attrs, expression);
 
        if (ret == LDB_SUCCESS) {
                talloc_steal(mem_ctx, res);
index 9ab21ee25f87628164883e03514377677da0dcda..35ea03b20e375a685c132fcc06689960245bb3b3 100644 (file)
@@ -56,7 +56,6 @@ static int vprintf_fn(void *private_data, const char *fmt, ...)
 int main(int argc, const char **argv)
 {
        struct ldb_context *ldb;
-       const char *expression = "(dn=*)";
        struct ldb_result *resultMsg;
        int i;
 
@@ -89,7 +88,7 @@ int main(int argc, const char **argv)
          confusing to start with. See RFC2254.
        */
        if (LDB_SUCCESS != ldb_search(ldb, ldb, &resultMsg, NULL, LDB_SCOPE_DEFAULT,
-                                     NULL, expression) ) {
+                                     NULL, "(dn=*)") ) {
                printf("Problem in search\n");
                exit(-1);
        }
index d98536c480c712f90fc78bc92a0eb4e925c01fa6..113652a1a7d8d85412a2121bf9e8080f17c6032f 100644 (file)
@@ -988,18 +988,6 @@ int ldb_build_rename_req(struct ldb_request **ret_req,
   \note use talloc_free() to free the ldb_result returned
 */
 int ldb_search(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
-                  struct ldb_result **_res,
-              const struct ldb_dn *base,
-              enum ldb_scope scope,
-              const char * const *attrs,
-              const char *expression);
-
-/*
- * a useful search function where you can easily define the expression and
- * that takes a memory context where results are allocated
-*/
-
-int ldb_search_exp_fmt(struct ldb_context *ldb, TALLOC_CTX *mem_ctx,
                       struct ldb_result **result, struct ldb_dn *base,
                       enum ldb_scope scope, const char * const *attrs,
                       const char *exp_fmt, ...);
index 54e1758c1ea19a6c26a88a3c6d49062729aa29b2..bda6cdcda5b7848b9f2d07d23d22b63fe9d19065 100644 (file)
@@ -1199,7 +1199,7 @@ static int map_init_dns(struct ldb_module *module, struct ldb_map_context *data,
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ret = ldb_search(module->ldb, dn, LDB_SCOPE_BASE, NULL, attrs, &res);
+       ret = ldb_search(module->ldb, module->ldb, &res, dn, LDB_SCOPE_BASE, attrs, NULL);
        talloc_free(dn);
        if (ret != LDB_SUCCESS) {
                return ret;
index 842a668d2f2c1eb29fdaa80d1322f5f1f49fcd89..82598e1e956ffebde60a958566d39e59dc8464f8 100644 (file)
@@ -151,7 +151,6 @@ NSS_STATUS _nss_ldb_getgrent_r(struct group *result_buf, char *buffer, size_t bu
 NSS_STATUS _nss_ldb_getgrnam_r(const char *name, struct group *result_buf, char *buffer, size_t buflen, int *errnop)
 {
        int ret;
-       char *filter;
        TALLOC_CTX *ctx;
        struct ldb_result *gr_res;
        struct ldb_result *mem_res;
@@ -167,21 +166,12 @@ NSS_STATUS _nss_ldb_getgrnam_r(const char *name, struct group *result_buf, char
                return NSS_STATUS_UNAVAIL;
        }
 
-       /* build the filter for this uid */
-       filter = talloc_asprintf(ctx, _LDB_NSS_GRNAM_FILTER, name);
-       if (filter == NULL) {
-               /* this is a fatal error */
-               *errnop = errno = ENOMEM;
-               ret = NSS_STATUS_UNAVAIL;
-               goto done;
-       }
-
        /* search the entry */
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &gr_res,
                         _ldb_nss_ctx->base,
                         LDB_SCOPE_SUBTREE,
                         _ldb_nss_gr_attrs,
-                        filter);
+                        _LDB_NSS_GRNAM_FILTER, name);
        if (ret != LDB_SUCCESS) {
                /* this is a fatal error */
                *errnop = errno = ENOENT;
@@ -242,7 +232,6 @@ done:
 NSS_STATUS _nss_ldb_getgrgid_r(gid_t gid, struct group *result_buf, char *buffer, size_t buflen, int *errnop)
 {
        int ret;
-       char *filter;
        TALLOC_CTX *ctx;
        struct ldb_result *gr_res;
        struct ldb_result *mem_res;
@@ -263,21 +252,12 @@ NSS_STATUS _nss_ldb_getgrgid_r(gid_t gid, struct group *result_buf, char *buffer
                return NSS_STATUS_UNAVAIL;
        }
 
-       /* build the filter for this uid */
-       filter = talloc_asprintf(ctx, _LDB_NSS_GRGID_FILTER, gid);
-       if (filter == NULL) {
-               /* this is a fatal error */
-               *errnop = errno = ENOMEM;
-               ret = NSS_STATUS_UNAVAIL;
-               goto done;
-       }
-
        /* search the entry */
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &gr_res,
                         _ldb_nss_ctx->base,
                         LDB_SCOPE_SUBTREE,
                         _ldb_nss_gr_attrs,
-                        filter);
+                        _LDB_NSS_GRGID_FILTER, gid);
        if (ret != LDB_SUCCESS) {
                /* this is a fatal error */
                *errnop = errno = ENOENT;
@@ -338,7 +318,6 @@ done:
 NSS_STATUS _nss_ldb_initgroups_dyn(const char *user, gid_t group, long int *start, long int *size, gid_t **groups, long int limit, int *errnop)
 {
        int ret;
-       char *filter;
        const char * attrs[] = { "uidNumber", "gidNumber", NULL };
        struct ldb_result *uid_res;
        struct ldb_result *mem_res;
@@ -354,21 +333,12 @@ NSS_STATUS _nss_ldb_initgroups_dyn(const char *user, gid_t group, long int *star
                return NSS_STATUS_UNAVAIL;
        }
 
-       /* build the filter for this name */
-       filter = talloc_asprintf(mem_res, _LDB_NSS_PWNAM_FILTER, user);
-       if (filter == NULL) {
-               /* this is a fatal error */
-               *errnop = errno = ENOENT;
-               ret = NSS_STATUS_UNAVAIL;
-               goto done;
-       }
-
        /* search the entry */
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &uid_res,
                         _ldb_nss_ctx->base,
                         LDB_SCOPE_SUBTREE,
                         attrs,
-                        filter);
+                        _LDB_NSS_PWNAM_FILTER, user);
        if (ret != LDB_SUCCESS) {
                /* this is a fatal error */
                *errnop = errno = ENOENT;
index 0af7af998187c209ace868350267260c1f7074b4..2e7ec456b336015744f4047c9f1fa0ef09878392 100644 (file)
@@ -113,7 +113,6 @@ NSS_STATUS _nss_ldb_getpwent_r(struct passwd *result_buf,
 NSS_STATUS _nss_ldb_getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
 {
        int ret;
-       char *filter;
        struct ldb_result *res;
 
        if (uid == 0) { /* we don't serve root uid by policy */
@@ -126,22 +125,12 @@ NSS_STATUS _nss_ldb_getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffe
                return ret;
        }
 
-       /* build the filter for this uid */
-       filter = talloc_asprintf(_ldb_nss_ctx, _LDB_NSS_PWUID_FILTER, uid);
-       if (filter == NULL) {
-               /* this is a fatal error */
-               *errnop = errno = ENOMEM;
-               ret = NSS_STATUS_UNAVAIL;
-               goto done;
-       }
-
        /* search the entry */
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res, 
                         _ldb_nss_ctx->base,
                         LDB_SCOPE_SUBTREE,
                         _ldb_nss_pw_attrs,
-                        filter
-                        );
+                        _LDB_NSS_PWUID_FILTER, uid);
        if (ret != LDB_SUCCESS) {
                /* this is a fatal error */
                *errnop = errno = ENOENT;
@@ -171,7 +160,6 @@ NSS_STATUS _nss_ldb_getpwuid_r(uid_t uid, struct passwd *result_buf, char *buffe
                                   res->msgs[0]);
 
 done:
-       talloc_free(filter);
        talloc_free(res);
        return ret;
 }
@@ -179,7 +167,6 @@ done:
 NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char *buffer, size_t buflen, int *errnop)
 {
        int ret;
-       char *filter;
        struct ldb_result *res;
 
        ret = _ldb_nss_init();
@@ -187,21 +174,12 @@ NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char
                return ret;
        }
 
-       /* build the filter for this name */
-       filter = talloc_asprintf(_ldb_nss_ctx, _LDB_NSS_PWNAM_FILTER, name);
-       if (filter == NULL) {
-               /* this is a fatal error */
-               *errnop = errno = ENOENT;
-               ret = NSS_STATUS_UNAVAIL;
-               goto done;
-       }
-
        /* search the entry */
        ret = ldb_search(_ldb_nss_ctx->ldb, _ldb_nss_ctx->ldb, &res,
                         _ldb_nss_ctx->base,
                         LDB_SCOPE_SUBTREE,
                         _ldb_nss_pw_attrs,
-                        filter);
+                        _LDB_NSS_PWNAM_FILTER, name);
        if (ret != LDB_SUCCESS) {
                /* this is a fatal error */
                *errnop = errno = ENOENT;
@@ -231,7 +209,6 @@ NSS_STATUS _nss_ldb_getpwnam_r(const char *name, struct passwd *result_buf, char
                                   res->msgs[0]);
 
 done:
-       talloc_free(filter);
        talloc_free(res);
        return ret;
 }
index 55406b29be47d8064569d69fa58fe3339e4fe9a0..0f05448a9e11f9f2e912676b91a3756a661ef605 100644 (file)
@@ -118,12 +118,10 @@ static int fetch_oc_recursive(struct ldb_context *ldb, struct ldb_dn *schemadn,
                struct ldb_result *res;
                const char *name = ldb_msg_find_attr_as_string(search_from->msgs[i], 
                                                               "lDAPDisplayname", NULL);
-               char *filter = talloc_asprintf(mem_ctx, "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))", 
-                                              name, name);
 
                ret = ldb_search(ldb, ldb, &res, schemadn, LDB_SCOPE_SUBTREE, 
-                                oc_attrs, filter);
-               talloc_free(filter);
+                                oc_attrs, "(&(&(objectClass=classSchema)(subClassOf=%s))(!(lDAPDisplayName=%s)))", 
+                                              name, name);
                if (ret != LDB_SUCCESS) {
                        printf("Search failed: %s\n", ldb_errstring(ldb));
                        return ret;
index dff7270e5d4b240df8178eae07bc3abd66c5a783..a33e6ae704267e137d1282a6f7d2bd0f9d7225fe 100644 (file)
@@ -306,7 +306,7 @@ int main(int argc, const char **argv)
                }
        }
 
-       ret = ldb_search(ldb, ldb, &result, basedn, options->scope, attrs, expression);
+       ret = ldb_search(ldb, ldb, &result, basedn, options->scope, attrs, "%s", expression);
        if (ret != LDB_SUCCESS) {
                printf("search failed - %s\n", ldb_errstring(ldb));
                exit(1);
index 637eb5a7ff23370d8b475b2384661ba1615175db..5e8ef1b21cc2ef7a19071f38efec5e0835db975d 100644 (file)
@@ -220,20 +220,18 @@ static void search_uid(struct ldb_context *ldb, struct ldb_dn *basedn, int nreco
 
        for (i=0;i<nsearches;i++) {
                int uid = (i * 700 + 17) % (nrecords * 2);
-               char *expr;
                struct ldb_result *res = NULL;
                int ret;
 
-               expr = talloc_asprintf(ldb, "(uid=TEST%d)", uid);
-               ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, expr);
+               ret = ldb_search(ldb, ldb, &res, basedn, LDB_SCOPE_SUBTREE, NULL, "(uid=TEST%d)", uid);
 
                if (ret != LDB_SUCCESS || (uid < nrecords && res->count != 1)) {
-                       printf("Failed to find %s - %s\n", expr, ldb_errstring(ldb));
+                       printf("Failed to find TEST%d - %s\n", uid, ldb_errstring(ldb));
                        exit(1);
                }
 
                if (uid >= nrecords && res->count > 0) {
-                       printf("Found %s !? - %d\n", expr, ret);
+                       printf("Found TEST%d !? - %d\n", uid, ret);
                        exit(1);
                }