r7665: - added a ildap_*() interface to our internal ldap library. This
authorAndrew Tridgell <tridge@samba.org>
Fri, 17 Jun 2005 02:45:40 +0000 (02:45 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:18:19 +0000 (13:18 -0500)
  interface is very similar to the traditional ldap interface, and will
  be used as part of a ldb backend based on the current ldb_ldap backend

- fixed some allocation issues in ldb_msg.c

source/libcli/ldap/config.mk
source/libcli/ldap/ldap.h
source/libcli/ldap/ldap_client.c
source/libcli/ldap/ldap_ildap.c [new file with mode: 0644]
source/libcli/ldap/ldap_msg.c

index ca33581043032bb8c8dfc39331bd7e5ce4b58d15..210fb112d378b3e98347db90da5c1ee3470746a7 100644 (file)
@@ -5,6 +5,8 @@ ADD_OBJ_FILES = libcli/ldap/ldap.o \
                libcli/ldap/ldap_client.o \
                libcli/ldap/ldap_bind.o \
                libcli/ldap/ldap_msg.o \
-               libcli/ldap/ldap_ndr.o
+               libcli/ldap/ldap_ndr.o \
+               libcli/ldap/ldap_ildap.o
+REQUIRED_SUBSYSTEMS = LIBCLI_UTILS LIBBASIC LIBEVENTS GENSEC SOCKET RPC_NDR_SAMR
 # End SUBSYSTEM LIBCLI_LDAP
 #################################
index 072070f723bd5e32d81ae1de9ced01a9346ce27f..4f2dbc07871182d5d6182bd388355ddc7b38d516 100644 (file)
@@ -217,6 +217,7 @@ struct ldap_ExtendedResponse {
 };
 
 union ldap_Request {
+       struct ldap_Result              GeneralResult;
        struct ldap_BindRequest         BindRequest;
        struct ldap_BindResponse        BindResponse;
        struct ldap_UnbindRequest       UnbindRequest;
index f3a7f104d45814b610a225cfcd086b4744d49c48..7ad45f4eea80bf28d78419c15623284927eefa6e 100644 (file)
@@ -573,6 +573,8 @@ NTSTATUS ldap_result_n(struct ldap_request *req, int n, struct ldap_message **ms
 {
        *msg = NULL;
 
+       NT_STATUS_HAVE_NO_MEMORY(req);
+
        while (req->state != LDAP_REQUEST_DONE && n >= req->num_replies) {
                if (event_loop_once(req->conn->event.event_ctx) != 0) {
                        return NT_STATUS_UNEXPECTED_NETWORK_ERROR;
@@ -608,3 +610,26 @@ NTSTATUS ldap_result_one(struct ldap_request *req, struct ldap_message **msg, in
        }
        return status;
 }
+
+/*
+  a simple ldap transaction, for single result requests that only need a status code
+  this relies on single valued requests having the response type == request type + 1
+*/
+NTSTATUS ldap_transaction(struct ldap_connection *conn, struct ldap_message *msg)
+{
+       struct ldap_request *req = ldap_request_send(conn, msg);
+       struct ldap_message *res;
+       NTSTATUS status;
+       status = ldap_result_n(req, 0, &res);
+       if (!NT_STATUS_IS_OK(status)) {
+               talloc_free(req);
+               return status;
+       }
+       if (res->type != msg->type + 1) {
+               talloc_free(req);
+               return NT_STATUS_LDAP(LDAP_PROTOCOL_ERROR);
+       }
+       status = ldap_check_response(conn, &res->r.GeneralResult);
+       talloc_free(req);
+       return status;
+}
diff --git a/source/libcli/ldap/ldap_ildap.c b/source/libcli/ldap/ldap_ildap.c
new file mode 100644 (file)
index 0000000..cfcb79f
--- /dev/null
@@ -0,0 +1,209 @@
+/* 
+   Unix SMB/CIFS mplementation.
+
+   ildap api - an api similar to the traditional ldap api
+   
+   Copyright (C) Andrew Tridgell  2005
+    
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   
+*/
+
+#include "includes.h"
+#include "libcli/ldap/ldap.h"
+#include "libcli/ldap/ldap_client.h"
+
+/*
+  delete a record
+ */
+NTSTATUS ildap_delete(struct ldap_connection *conn, const char *dn)
+{
+       struct ldap_message *msg;
+       NTSTATUS status;
+
+       msg = new_ldap_message(conn);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       msg->type = LDAP_TAG_DelRequest;
+       msg->r.DelRequest.dn = dn;
+
+       status = ldap_transaction(conn, msg);
+
+       talloc_free(msg);
+
+       return status;
+}
+
+/*
+  add a record
+ */
+NTSTATUS ildap_add(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
+{
+       struct ldap_message *msg;
+       int n, i;
+       NTSTATUS status;
+
+       msg = new_ldap_message(conn);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       for (n=0;mods[n];n++) /* noop */ ;
+
+       msg->type = LDAP_TAG_AddRequest;
+       msg->r.AddRequest.dn = dn;
+       msg->r.AddRequest.num_attributes = n;
+       msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
+       if (msg->r.AddRequest.attributes == NULL) {
+               talloc_free(msg);
+               return NT_STATUS_NO_MEMORY;
+       }
+       for (i=0;i<n;i++) {
+               msg->r.AddRequest.attributes[i] = mods[i]->attrib;
+       }
+
+       status = ldap_transaction(conn, msg);
+
+       talloc_free(msg);
+
+       return status;
+}
+
+
+/*
+  modify a record
+ */
+NTSTATUS ildap_modify(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
+{
+       struct ldap_message *msg;
+       int n, i;
+       NTSTATUS status;
+
+       msg = new_ldap_message(conn);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       for (n=0;mods[n];n++) /* noop */ ;
+
+       msg->type = LDAP_TAG_ModifyRequest;
+       msg->r.ModifyRequest.dn = dn;
+       msg->r.ModifyRequest.num_mods = n;
+       msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
+       if (msg->r.ModifyRequest.mods == NULL) {
+               talloc_free(msg);
+               return NT_STATUS_NO_MEMORY;
+       }
+       for (i=0;i<n;i++) {
+               msg->r.ModifyRequest.mods[i] = *mods[i];
+       }
+
+       status = ldap_transaction(conn, msg);
+
+       talloc_free(msg);
+
+       return status;
+}
+
+
+/*
+  rename a record
+ */
+NTSTATUS ildap_rename(struct ldap_connection *conn, const char *dn, const char *newrdn, 
+                     const char *parentdn, BOOL deleteolddn)
+{
+       struct ldap_message *msg;
+       NTSTATUS status;
+
+       msg = new_ldap_message(conn);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       msg->type = LDAP_TAG_ModifyDNRequest;
+       msg->r.ModifyDNRequest.dn = dn;
+       msg->r.ModifyDNRequest.newrdn = newrdn;
+       msg->r.ModifyDNRequest.deleteolddn = deleteolddn;
+       msg->r.ModifyDNRequest.newsuperior = parentdn;
+
+       status = ldap_transaction(conn, msg);
+
+       talloc_free(msg);
+
+       return status;
+}
+
+
+/*
+  count the returned search entries
+*/
+int ildap_count_entries(struct ldap_connection *conn, struct ldap_message **res)
+{
+       int i;
+       for (i=0;res && res[i];i++) /* noop */ ;
+       return i;
+}
+
+
+/*
+  perform a ldap search
+*/
+NTSTATUS ildap_search(struct ldap_connection *conn, const char *basedn, 
+                     int scope, const char *expression, 
+                     const char * const *attrs, BOOL attributesonly, 
+                     struct ldap_message ***results)
+{
+       struct ldap_message *msg;
+       int n, i;
+       NTSTATUS status;
+       struct ldap_request *req;
+
+       *results = NULL;
+
+       msg = new_ldap_message(conn);
+       NT_STATUS_HAVE_NO_MEMORY(msg);
+
+       for (n=0;attrs && attrs[n];n++) /* noop */ ;
+       
+       msg->type = LDAP_TAG_SearchRequest;
+       msg->r.SearchRequest.basedn = basedn;
+       msg->r.SearchRequest.scope  = scope;
+       msg->r.SearchRequest.deref  = LDAP_DEREFERENCE_NEVER;
+       msg->r.SearchRequest.timelimit = 0;
+       msg->r.SearchRequest.sizelimit = 0;
+       msg->r.SearchRequest.attributesonly = attributesonly;
+       msg->r.SearchRequest.tree = ldb_parse_tree(msg, expression);
+       msg->r.SearchRequest.num_attributes = n;
+       msg->r.SearchRequest.attributes = attrs;
+
+       req = ldap_request_send(conn, msg);
+       talloc_steal(msg, req);
+       
+       for (i=n=0;True;i++) {
+               struct ldap_message *res;
+               status = ldap_result_n(req, i, &res);
+               if (!NT_STATUS_IS_OK(status)) break;
+               if (res->type != LDAP_TAG_SearchResultEntry) continue;
+               
+               (*results) = talloc_realloc(conn, *results, struct ldap_message *, n+2);
+               if (*results == NULL) {
+                       talloc_free(msg);
+                       return NT_STATUS_NO_MEMORY;
+               }
+               (*results)[n] = talloc_steal(*results, res);
+               (*results)[n+1] = NULL;
+               n++;
+       }
+
+       if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+               status = NT_STATUS_OK;
+       }
+
+       return status;
+}
index 5ac44a5226526cc6b5983c5cb838ee2ac0760bf4..901c42a62a70817f514acb0c404902d1b1808e65 100644 (file)
@@ -44,7 +44,7 @@ BOOL add_value_to_attrib(TALLOC_CTX *mem_ctx, struct ldb_val *value,
                return False;
 
        attrib->values[attrib->num_values] =
-               data_blob_talloc(mem_ctx, value->data, value->length);
+               data_blob_talloc(attrib->values, value->data, value->length);
        attrib->num_values += 1;
        return True;
 }
@@ -63,6 +63,8 @@ BOOL add_attrib_to_array_talloc(TALLOC_CTX *mem_ctx,
                return False;
 
        (*attribs)[*num_attribs] = *attrib;
+       talloc_steal(*attribs, attrib->values);
+       talloc_steal(*attribs, attrib->name);
        *num_attribs += 1;
        return True;
 }