Reorder arguments to ldb_search() to match what is in Samba 4.
[kai/samba-autobuild/.git] / source3 / lib / ldb / common / ldb.c
index 7648abf795e27bfa7e73f64603ff061a556cee1b..495047f3a1ee0beb179925af6256f6338e078b9b 100644 (file)
@@ -11,7 +11,7 @@
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
-   version 2 of the License, or (at your option) any later version.
+   version 3 of the License, or (at your option) any later version.
 
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,8 +19,7 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with this library; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+   License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
 /*
@@ -52,7 +51,7 @@ struct ldb_context *ldb_init(void *mem_ctx)
        }
 
        ldb_set_utf8_default(ldb);
-       ldb_set_create_perms(ldb, 0666);
+       ldb_set_create_perms(ldb, 0600);
 
        return ldb;
 }
@@ -167,8 +166,8 @@ static const struct ldb_dn *ldb_set_default_basedn(struct ldb_context *ldb)
        }
 
        tmp_ctx = talloc_new(ldb);
-       ret = ldb_search(ldb, ldb_dn_new(tmp_ctx), LDB_SCOPE_BASE, 
-                        "(objectClass=*)", attrs, &res);
+       ret = ldb_search(ldb, ldb, &res, ldb_dn_new(tmp_ctx), LDB_SCOPE_BASE, 
+                        attrs, "(objectClass=*)");
        if (ret == LDB_SUCCESS) {
                if (res->count == 1) {
                        basedn = ldb_msg_find_attr_as_dn(ldb, res->msgs[0], "defaultNamingContext");
@@ -522,7 +521,7 @@ int ldb_request(struct ldb_context *ldb, struct ldb_request *req)
   Use talloc_free to free the ldb_message returned in 'res', if successful
 
 */
-static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
+int ldb_search_default_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
 {
        struct ldb_result *res;
        int n;
@@ -532,12 +531,13 @@ static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ld
                return LDB_ERR_OPERATIONS_ERROR;
        }       
 
-       res = *((struct ldb_result **)context);
+       res = talloc_get_type(context, struct ldb_result);
 
        if (!res || !ares) {
+               ldb_set_errstring(ldb, "NULL res or ares in callback");
                goto error;
        }
-       
+
        switch (ares->type) {
        case LDB_REPLY_ENTRY:
                res->msgs = talloc_realloc(res, res->msgs, struct ldb_message *, res->count + 2);
@@ -564,19 +564,17 @@ static int ldb_search_callback(struct ldb_context *ldb, void *context, struct ld
 
                res->refs[n] = talloc_move(res->refs, &ares->referral);
                res->refs[n + 1] = NULL;
+       case LDB_REPLY_EXTENDED:
        case LDB_REPLY_DONE:
-               /* Should do something here to detect if this never
-                * happens */
+               /* TODO: we should really support controls on entries and referrals too! */
+               res->controls = talloc_move(res, &ares->controls);
                break;          
        }
-       talloc_steal(res, ares->controls);
        talloc_free(ares);
        return LDB_SUCCESS;
 
 error:
        talloc_free(ares);
-       talloc_free(res);
-       *((struct ldb_result **)context) = NULL;
        return LDB_ERR_OPERATIONS_ERROR;
 }
 
@@ -747,29 +745,32 @@ 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, 
-              const struct ldb_dn *base,
-              enum ldb_scope scope,
-              const char *expression,
-              const char * const *attrs, 
-              struct ldb_result **res)
+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)
 {
        struct ldb_request *req;
        int ret;
+       struct ldb_result *res;
+
+       *_res = NULL;
 
-       *res = talloc_zero(ldb, struct ldb_result);
-       if (! *res) {
+       res = talloc_zero(mem_ctx, struct ldb_result);
+       if (!res) {
                return LDB_ERR_OPERATIONS_ERROR;
        }
-       
-       ret = ldb_build_search_req(&req, ldb, ldb,
+
+       ret = ldb_build_search_req(&req, ldb, mem_ctx,
                                        base?base:ldb_get_default_basedn(ldb),
                                        scope,
                                        expression,
                                        attrs,
                                        NULL,
                                        res,
-                                       ldb_search_callback);
+                                       ldb_search_default_callback);
 
        if (ret != LDB_SUCCESS) goto done;
 
@@ -785,10 +786,50 @@ int ldb_search(struct ldb_context *ldb,
 
 done:
        if (ret != LDB_SUCCESS) {
-               talloc_free(*res);
-               *res = NULL;
+               talloc_free(res);
+               res = NULL;
        }
 
+       *_res = res;
+       return ret;
+}
+
+/*
+ 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, ...)
+{
+       struct ldb_result *res;
+       char *expression;
+       va_list ap;
+       int ret;
+
+       res = NULL;
+       *result = NULL;
+
+       va_start(ap, exp_fmt);
+       expression = talloc_vasprintf(mem_ctx, exp_fmt, ap);
+       va_end(ap);
+
+       if ( ! expression) {
+               return LDB_ERR_OPERATIONS_ERROR;
+       }
+
+       ret = ldb_search(ldb, ldb, &res, base, scope, attrs, expression);
+
+       if (ret == LDB_SUCCESS) {
+               talloc_steal(mem_ctx, res);
+               *result = res;
+       } else {
+               talloc_free(res);
+       }
+
+       talloc_free(expression);
+
        return ret;
 }