2 Unix SMB/CIFS mplementation.
4 ildap api - an api similar to the traditional ldap api
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "libcli/ldap/ldap.h"
26 #include "libcli/ldap/ldap_client.h"
31 NTSTATUS ildap_delete(struct ldap_connection *conn, const char *dn)
33 struct ldap_message *msg;
36 msg = new_ldap_message(conn);
37 NT_STATUS_HAVE_NO_MEMORY(msg);
39 msg->type = LDAP_TAG_DelRequest;
40 msg->r.DelRequest.dn = dn;
42 status = ldap_transaction(conn, msg);
52 NTSTATUS ildap_add(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
54 struct ldap_message *msg;
58 msg = new_ldap_message(conn);
59 NT_STATUS_HAVE_NO_MEMORY(msg);
61 for (n=0;mods[n];n++) /* noop */ ;
63 msg->type = LDAP_TAG_AddRequest;
64 msg->r.AddRequest.dn = dn;
65 msg->r.AddRequest.num_attributes = n;
66 msg->r.AddRequest.attributes = talloc_array(msg, struct ldb_message_element, n);
67 if (msg->r.AddRequest.attributes == NULL) {
69 return NT_STATUS_NO_MEMORY;
72 msg->r.AddRequest.attributes[i] = mods[i]->attrib;
75 status = ldap_transaction(conn, msg);
86 NTSTATUS ildap_modify(struct ldap_connection *conn, const char *dn, struct ldap_mod **mods)
88 struct ldap_message *msg;
92 msg = new_ldap_message(conn);
93 NT_STATUS_HAVE_NO_MEMORY(msg);
95 for (n=0;mods[n];n++) /* noop */ ;
97 msg->type = LDAP_TAG_ModifyRequest;
98 msg->r.ModifyRequest.dn = dn;
99 msg->r.ModifyRequest.num_mods = n;
100 msg->r.ModifyRequest.mods = talloc_array(msg, struct ldap_mod, n);
101 if (msg->r.ModifyRequest.mods == NULL) {
103 return NT_STATUS_NO_MEMORY;
106 msg->r.ModifyRequest.mods[i] = *mods[i];
109 status = ldap_transaction(conn, msg);
120 NTSTATUS ildap_rename(struct ldap_connection *conn, const char *dn, const char *newrdn,
121 const char *parentdn, BOOL deleteolddn)
123 struct ldap_message *msg;
126 msg = new_ldap_message(conn);
127 NT_STATUS_HAVE_NO_MEMORY(msg);
129 msg->type = LDAP_TAG_ModifyDNRequest;
130 msg->r.ModifyDNRequest.dn = dn;
131 msg->r.ModifyDNRequest.newrdn = newrdn;
132 msg->r.ModifyDNRequest.deleteolddn = deleteolddn;
133 msg->r.ModifyDNRequest.newsuperior = parentdn;
135 status = ldap_transaction(conn, msg);
144 count the returned search entries
146 int ildap_count_entries(struct ldap_connection *conn, struct ldap_message **res)
149 for (i=0;res && res[i];i++) /* noop */ ;
155 perform a ldap search
157 NTSTATUS ildap_search_bytree(struct ldap_connection *conn, const char *basedn,
158 int scope, struct ldb_parse_tree *tree,
159 const char * const *attrs, BOOL attributesonly,
160 struct ldap_message ***results)
162 struct ldap_message *msg;
165 struct ldap_request *req;
169 msg = new_ldap_message(conn);
170 NT_STATUS_HAVE_NO_MEMORY(msg);
172 for (n=0;attrs && attrs[n];n++) /* noop */ ;
174 msg->type = LDAP_TAG_SearchRequest;
175 msg->r.SearchRequest.basedn = basedn;
176 msg->r.SearchRequest.scope = scope;
177 msg->r.SearchRequest.deref = LDAP_DEREFERENCE_NEVER;
178 msg->r.SearchRequest.timelimit = 0;
179 msg->r.SearchRequest.sizelimit = 0;
180 msg->r.SearchRequest.attributesonly = attributesonly;
181 msg->r.SearchRequest.tree = tree;
182 msg->r.SearchRequest.num_attributes = n;
183 msg->r.SearchRequest.attributes = discard_const(attrs);
185 req = ldap_request_send(conn, msg);
186 talloc_steal(msg, req);
188 for (i=n=0;True;i++) {
189 struct ldap_message *res;
190 status = ldap_result_n(req, i, &res);
191 if (!NT_STATUS_IS_OK(status)) break;
193 if (res->type == LDAP_TAG_SearchResultDone) {
194 status = ldap_check_response(conn, &res->r.GeneralResult);
198 if (res->type != LDAP_TAG_SearchResultEntry) continue;
200 (*results) = talloc_realloc(conn, *results, struct ldap_message *, n+2);
201 if (*results == NULL) {
203 return NT_STATUS_NO_MEMORY;
205 (*results)[n] = talloc_steal(*results, res);
206 (*results)[n+1] = NULL;
210 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
211 status = NT_STATUS_OK;
218 perform a ldap search
220 NTSTATUS ildap_search(struct ldap_connection *conn, const char *basedn,
221 int scope, const char *expression,
222 const char * const *attrs, BOOL attributesonly,
223 struct ldap_message ***results)
225 struct ldb_parse_tree *tree = ldb_parse_tree(conn, expression);
227 status = ildap_search_bytree(conn, basedn, scope, tree, attrs,
228 attributesonly, results);