Fix segfault when sorting LDAP replies on the client.
[ira/wip.git] / source / lib / ldb / tools / ldbsearch.c
index 181d857efc6a7f6bb5168ff9a5450bee5f64920e..dba0549b4445e15c8e0f2a37cf97cc2ca4e7cb18 100644 (file)
@@ -10,7 +10,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
@@ -18,8 +18,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/>.
 */
 
 /*
@@ -32,9 +31,8 @@
  *  Author: Andrew Tridgell
  */
 
-#include "includes.h"
-#include "ldb/include/includes.h"
-#include "ldb/tools/cmdline.h"
+#include "ldb_includes.h"
+#include "tools/cmdline.h"
 
 static void usage(void)
 {
@@ -54,8 +52,7 @@ static int do_compare_msg(struct ldb_message **el1,
                          struct ldb_message **el2,
                          void *opaque)
 {
-       struct ldb_context *ldb = talloc_get_type(opaque, struct ldb_context);
-       return ldb_dn_compare(ldb, (*el1)->dn, (*el2)->dn);
+       return ldb_dn_compare((*el1)->dn, (*el2)->dn);
 }
 
 struct search_context {
@@ -81,7 +78,7 @@ static int store_message(struct ldb_message *msg, struct search_context *sctx) {
                return -1;
        }
 
-       sctx->store[sctx->num_stored] = talloc_move(sctx->store, msg);
+       sctx->store[sctx->num_stored] = talloc_move(sctx->store, &msg);
        sctx->num_stored++;
        sctx->store[sctx->num_stored] = NULL;
 
@@ -96,7 +93,7 @@ static int store_referral(char *referral, struct search_context *sctx) {
                return -1;
        }
 
-       sctx->refs_store[sctx->refs] = talloc_move(sctx->refs_store, referral);
+       sctx->refs_store[sctx->refs] = talloc_move(sctx->refs_store, &referral);
        sctx->refs++;
        sctx->refs_store[sctx->refs] = NULL;
 
@@ -185,7 +182,7 @@ static int search_callback(struct ldb_context *ldb, void *context, struct ldb_re
 }
 
 static int do_search(struct ldb_context *ldb,
-                    const struct ldb_dn *basedn,
+                    struct ldb_dn *basedn,
                     struct ldb_cmdline *options,
                     const char *expression,
                     const char * const *attrs)
@@ -203,8 +200,11 @@ static int do_search(struct ldb_context *ldb,
        sctx->sort = options->sorted;
        sctx->num_stored = 0;
        sctx->store = NULL;
-       sctx->req_ctrls = parse_controls(ldb, options->controls);
-       if (options->controls != NULL &&  sctx->req_ctrls== NULL) return -1;
+       sctx->req_ctrls = ldb_parse_control_strings(ldb, sctx, (const char **)options->controls);
+       if (options->controls != NULL &&  sctx->req_ctrls== NULL) {
+               printf("parsing controls failed: %s\n", ldb_errstring(ldb));
+               return -1;
+       }
        sctx->entries = 0;
        sctx->refs = 0;
 
@@ -215,7 +215,7 @@ static int do_search(struct ldb_context *ldb,
        req->operation = LDB_SEARCH;
        req->op.search.base = basedn;
        req->op.search.scope = options->scope;
-       req->op.search.tree = ldb_parse_tree(ldb, expression);
+       req->op.search.tree = ldb_parse_tree(req, expression);
        if (req->op.search.tree == NULL) return -1;
        req->op.search.attrs = attrs;
        req->controls = sctx->req_ctrls;
@@ -241,10 +241,10 @@ again:
        if (sctx->pending)
                goto again;
 
-       if (sctx->sort && sctx->num_stored != 0) {
+       if (sctx->sort && (sctx->num_stored != 0 || sctx->refs != 0)) {
                int i;
 
-               ldb_qsort(sctx->store, ret, sizeof(struct ldb_message *),
+               ldb_qsort(sctx->store, sctx->num_stored, sizeof(struct ldb_message *),
                          ldb, (ldb_qsort_cmp_fn_t)do_compare_msg);
 
                if (ret != 0) {
@@ -298,8 +298,8 @@ int main(int argc, const char **argv)
        }
 
        if (options->basedn != NULL) {
-               basedn = ldb_dn_explode(ldb, options->basedn);
-               if (basedn == NULL) {
+               basedn = ldb_dn_new(ldb, ldb, options->basedn);
+               if ( ! ldb_dn_validate(basedn)) {
                        fprintf(stderr, "Invalid Base DN format\n");
                        exit(1);
                }