r19832: better prototypes for the linearization functions:
[kamenim/samba.git] / source4 / lib / ldb / ldb_ildap / ldb_ildap.c
index c71f1be9ba2225da2ae62bfddcc434446d76d4c3..567fe4bd5f8f575612bc6d7f5426867b89ef49c6 100644 (file)
 #include "libcli/ldap/ldap.h"
 #include "libcli/ldap/ldap_client.h"
 #include "auth/auth.h"
+#include "auth/credentials/credentials.h"
 
 struct ildb_private {
        struct ldap_connection *ldap;
-       struct ldb_message *rootDSE;
        struct ldb_context *ldb;
 };
 
@@ -125,8 +125,7 @@ static int ildb_map_error(struct ildb_private *ildb, NTSTATUS status)
        if (NT_STATUS_IS_OK(status)) {
                return LDB_SUCCESS;
        }
-       talloc_free(ildb->ldb->err_string);
-       ildb->ldb->err_string = talloc_strdup(ildb, ldap_errstr(ildb->ldap, status));
+       ldb_set_errstring(ildb->ldb, ldap_errstr(ildb->ldap, status));
        if (NT_STATUS_IS_LDAP(status)) {
                return NT_STATUS_LDAP_CODE(status);
        }
@@ -247,14 +246,11 @@ static void ildb_callback(struct ldap_request *req)
 
                                status = ldap_check_response(req->conn, &msg->r.GeneralResult);
                                if (!NT_STATUS_IS_OK(status)) {
-                                       ldb_debug(ac->module->ldb, LDB_DEBUG_ERROR, "Error: %s\n" ,ldap_errstr(req->conn, status));
                                        handle->status = ildb_map_error(ildb, status);
                                        return;
                                }
                                
-                               if (msg->controls) {
-                                       ares->controls = talloc_steal(ares, msg->controls);
-                               }
+                               ares->controls = talloc_move(ares, &msg->controls);
                                if (msg->r.SearchResultDone.resultcode) {
                                        if (msg->r.SearchResultDone.errormessage) {
                                                ldb_set_errstring(ac->module->ldb, msg->r.SearchResultDone.errormessage);
@@ -277,13 +273,14 @@ static void ildb_callback(struct ldap_request *req)
 
                                search = &(msg->r.SearchResultEntry);
                
-                               ares->message->dn = ldb_dn_explode_or_special(ares->message, search->dn);
-                               if (ares->message->dn == NULL) {
+                               ares->message->dn = ldb_dn_new(ares->message, ac->module->ldb, search->dn);
+                               if ( ! ldb_dn_validate(ares->message->dn)) {
                                        handle->status = LDB_ERR_OPERATIONS_ERROR;
                                        return;
                                }
                                ares->message->num_elements = search->num_attributes;
-                               ares->message->elements = talloc_steal(ares->message, search->attributes);
+                               ares->message->elements = talloc_move(ares->message,
+                                                                     &search->attributes);
 
                                handle->status = LDB_SUCCESS;
                                handle->state = LDB_ASYNC_PENDING;
@@ -386,7 +383,6 @@ static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
-       ildb_ac->req = talloc_steal(ildb_ac, req);
        talloc_free(req->time_event);
        req->time_event = NULL;
        if (timeout) {
@@ -397,6 +393,7 @@ static int ildb_request_send(struct ldb_module *module, struct ldap_message *msg
 
        req->async.fn = ildb_callback;
        req->async.private_data = (void *)h;
+       ildb_ac->req = talloc_move(ildb_ac, &req);
 
        *handle = h;
        return LDB_SUCCESS;
@@ -453,14 +450,9 @@ static int ildb_search(struct ldb_module *module, struct ldb_request *req)
        msg->type = LDAP_TAG_SearchRequest;
 
        if (req->op.search.base == NULL) {
-               if (ildb->rootDSE != NULL) {
-                       msg->r.SearchRequest.basedn =
-                               talloc_strdup(msg, ldb_msg_find_string(ildb->rootDSE, "defaultNamingContext", ""));
-               } else {
-                       msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
-               }
+               msg->r.SearchRequest.basedn = talloc_strdup(msg, "");
        } else {
-               msg->r.SearchRequest.basedn  = ldb_dn_linearize(msg, req->op.search.base);
+               msg->r.SearchRequest.basedn  = ldb_dn_alloc_linearized(msg, req->op.search.base);
        }
        if (msg->r.SearchRequest.basedn == NULL) {
                ldb_set_errstring(module->ldb, "Unable to determine baseDN");
@@ -512,7 +504,7 @@ static int ildb_add(struct ldb_module *module, struct ldb_request *req)
 
        msg->type = LDAP_TAG_AddRequest;
 
-       msg->r.AddRequest.dn = ldb_dn_linearize(msg, req->op.add.message->dn);
+       msg->r.AddRequest.dn = ldb_dn_alloc_linearized(msg, req->op.add.message->dn);
        if (msg->r.AddRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -562,7 +554,7 @@ static int ildb_modify(struct ldb_module *module, struct ldb_request *req)
 
        msg->type = LDAP_TAG_ModifyRequest;
 
-       msg->r.ModifyRequest.dn = ldb_dn_linearize(msg, req->op.mod.message->dn);
+       msg->r.ModifyRequest.dn = ldb_dn_alloc_linearized(msg, req->op.mod.message->dn);
        if (msg->r.ModifyRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -610,7 +602,7 @@ static int ildb_delete(struct ldb_module *module, struct ldb_request *req)
 
        msg->type = LDAP_TAG_DelRequest;
        
-       msg->r.DelRequest.dn = ldb_dn_linearize(msg, req->op.del.dn);
+       msg->r.DelRequest.dn = ldb_dn_alloc_linearized(msg, req->op.del.dn);
        if (msg->r.DelRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -640,7 +632,7 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
        }
 
        msg->type = LDAP_TAG_ModifyDNRequest;
-       msg->r.ModifyDNRequest.dn = ldb_dn_linearize(msg, req->op.rename.olddn);
+       msg->r.ModifyDNRequest.dn = ldb_dn_alloc_linearized(msg, req->op.rename.olddn);
        if (msg->r.ModifyDNRequest.dn == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -648,16 +640,15 @@ static int ildb_rename(struct ldb_module *module, struct ldb_request *req)
 
        msg->r.ModifyDNRequest.newrdn = 
                talloc_asprintf(msg, "%s=%s",
-                               req->op.rename.newdn->components[0].name,
-                               ldb_dn_escape_value(msg, req->op.rename.newdn->components[0].value));
+                               ldb_dn_get_rdn_name(req->op.rename.newdn),
+                               ldb_dn_escape_value(msg, *ldb_dn_get_rdn_val(req->op.rename.newdn)));
        if (msg->r.ModifyDNRequest.newrdn == NULL) {
                talloc_free(msg);
                return LDB_ERR_OPERATIONS_ERROR;
        }
 
        msg->r.ModifyDNRequest.newsuperior =
-               ldb_dn_linearize(msg,
-                                ldb_dn_get_parent(msg, req->op.rename.newdn));
+               ldb_dn_alloc_linearized(msg, ldb_dn_get_parent(msg, req->op.rename.newdn));
        if (msg->r.ModifyDNRequest.newsuperior == NULL) {
                talloc_free(msg);
                return LDB_ERR_INVALID_DN_SYNTAX;
@@ -728,80 +719,6 @@ static int ildb_wait(struct ldb_handle *handle, enum ldb_wait_type type)
        return handle->status;
 }
 
-static int ildb_rootdse_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
-{
-       struct ildb_private *ildb;
-
-       if (!context || !ares) {
-               ldb_set_errstring(ldb, "NULL Context or Result in callback");
-               goto error;
-       }
-
-       ildb = talloc_get_type(context, struct ildb_private);
-
-       /* we are interested only in the single reply (rootdse) we receive here */
-       switch (ares->type) {
-       case LDB_REPLY_ENTRY:
-               if (ildb->rootDSE != NULL) {
-                       /* what ? more than one rootdse entry ?! */
-                       goto error;
-               }
-               ildb->rootDSE = talloc_steal(ildb, ares->message);
-               break;
-
-       case LDB_REPLY_REFERRAL:
-               goto error;
-
-       case LDB_REPLY_DONE:
-               break;
-       }
-       
-       talloc_free(ares);
-       return LDB_SUCCESS;
-
-error:
-       talloc_free(ares);
-       return LDB_ERR_OPERATIONS_ERROR;
-}
-
-/*
-  fetch the rootDSE for later use
-*/
-static int ildb_init(struct ldb_module *module)
-{
-       struct ildb_private *ildb = talloc_get_type(module->private_data, struct ildb_private);
-       struct ldb_request *req;
-       int ret;
-
-       req = talloc(ildb, struct ldb_request);
-       if (req == NULL) {
-               return LDB_ERR_OPERATIONS_ERROR;
-       }
-
-       ildb->rootDSE = NULL;
-
-       req->operation = LDB_SEARCH;
-       req->op.search.base = ldb_dn_new(req);
-       req->op.search.scope = LDB_SCOPE_BASE;
-       req->op.search.tree = ldb_parse_tree(req, "(objectClass=*)");
-       req->op.search.attrs = NULL;
-       req->controls = NULL;
-       req->context = ildb;
-       req->callback = ildb_rootdse_callback;
-       ldb_set_timeout(module->ldb, req, 60);
-
-       ret = ildb_search(module, req);
-       if (ret != LDB_SUCCESS) {
-               talloc_free(req);
-               return ret;
-       }
-
-       ret = ildb_wait(req->handle, LDB_WAIT_ALL);
-       
-       talloc_free(req);
-       return ret;
-}
-
 static const struct ldb_module_ops ildb_ops = {
        .name              = "ldap",
        .search            = ildb_search,
@@ -813,8 +730,7 @@ static const struct ldb_module_ops ildb_ops = {
        .start_transaction = ildb_start_trans,
        .end_transaction   = ildb_end_trans,
        .del_transaction   = ildb_del_trans,
-       .wait              = ildb_wait,
-       .init_context      = ildb_init
+       .wait              = ildb_wait
 };
 
 /*
@@ -834,10 +750,9 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
                goto failed;
        }
 
-       ildb->rootDSE = NULL;
        ildb->ldb     = ldb;
 
-       ildb->ldap = ldap_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
+       ildb->ldap = ldap4_new_connection(ildb, ldb_get_opaque(ldb, "EventContext"));
        if (!ildb->ldap) {
                ldb_oom(ldb);
                goto failed;
@@ -861,6 +776,7 @@ static int ildb_connect(struct ldb_context *ldb, const char *url,
                talloc_free(ildb);
                return -1;
        }
+       talloc_set_name_const(*module, "ldb_ildap backend");
        (*module)->ldb = ldb;
        (*module)->prev = (*module)->next = NULL;
        (*module)->private_data = ildb;