LDB ASYNC: misc changes
[Samba/ekacnet.git] / source4 / ldap_server / ldap_backend.c
blobffbef3d92fe9dbb0a9b117dbb028bd566be4009d
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP server
4 Copyright (C) Stefan Metzmacher 2004
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "ldap_server/ldap_server.h"
22 #include "lib/util/dlinklist.h"
23 #include "libcli/ldap/ldap.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "param/param.h"
27 #include "smbd/service_stream.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "lib/ldb_wrap.h"
32 #define VALID_DN_SYNTAX(dn,i) do {\
33 if (!(dn)) {\
34 return NT_STATUS_NO_MEMORY;\
35 } else if ( ! ldb_dn_validate(dn)) {\
36 result = LDAP_INVALID_DN_SYNTAX;\
37 errstr = "Invalid DN format";\
38 goto reply;\
39 } else if (ldb_dn_get_comp_num(dn) < (i)) {\
40 result = LDAP_INVALID_DN_SYNTAX;\
41 errstr = "Invalid DN (" #i " components needed for '" #dn "')";\
42 goto reply;\
44 } while(0)
46 static int map_ldb_error(struct ldb_context *ldb, int err, const char **errstring)
48 *errstring = ldb_errstring(ldb);
50 /* its 1:1 for now */
51 return err;
55 connect to the sam database
57 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn)
59 conn->ldb = ldb_wrap_connect(conn,
60 conn->connection->event.ctx,
61 conn->lp_ctx,
62 lp_sam_url(conn->lp_ctx),
63 conn->session_info,
64 samdb_credentials(conn, conn->connection->event.ctx, conn->lp_ctx),
65 conn->global_catalog ? LDB_FLG_RDONLY : 0, NULL);
66 if (conn->ldb == NULL) {
67 return NT_STATUS_INTERNAL_DB_CORRUPTION;
70 if (conn->server_credentials) {
71 char **sasl_mechs = NULL;
72 struct gensec_security_ops **backends = gensec_security_all();
73 struct gensec_security_ops **ops
74 = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
75 int i, j = 0;
76 for (i = 0; ops && ops[i]; i++) {
77 if (ops[i]->sasl_name && ops[i]->server_start) {
78 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
80 if (!sasl_name) {
81 return NT_STATUS_NO_MEMORY;
83 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
84 if (!sasl_mechs) {
85 return NT_STATUS_NO_MEMORY;
87 sasl_mechs[j] = sasl_name;
88 talloc_steal(sasl_mechs, sasl_name);
89 sasl_mechs[j+1] = NULL;
90 j++;
93 talloc_free(ops);
94 ldb_set_opaque(conn->ldb, "supportedSASLMechanims", sasl_mechs);
97 return NT_STATUS_OK;
100 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
102 struct ldapsrv_reply *reply;
104 reply = talloc(call, struct ldapsrv_reply);
105 if (!reply) {
106 return NULL;
108 reply->msg = talloc(reply, struct ldap_message);
109 if (reply->msg == NULL) {
110 talloc_free(reply);
111 return NULL;
114 reply->msg->messageid = call->request->messageid;
115 reply->msg->type = type;
116 reply->msg->controls = NULL;
118 return reply;
121 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
123 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
126 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
128 struct ldapsrv_reply *reply;
129 struct ldap_ExtendedResponse *r;
131 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
133 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
134 if (!reply) {
135 return NT_STATUS_NO_MEMORY;
138 r = &reply->msg->r.ExtendedResponse;
139 r->response.resultcode = error;
140 r->response.dn = NULL;
141 r->response.errormessage = NULL;
142 r->response.referral = NULL;
143 r->oid = NULL;
144 r->value = NULL;
146 ldapsrv_queue_reply(call, reply);
147 return NT_STATUS_OK;
150 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
152 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
153 struct ldap_SearchResEntry *ent;
154 struct ldap_Result *done;
155 struct ldapsrv_reply *ent_r, *done_r;
156 void *local_ctx;
157 struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
158 struct ldb_dn *basedn;
159 struct ldb_result *res = NULL;
160 struct ldb_request *lreq;
161 struct ldb_control *search_control;
162 struct ldb_search_options_control *search_options;
163 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
164 const char **attrs = NULL;
165 const char *scope_str, *errstr = NULL;
166 int success_limit = 1;
167 int result = -1;
168 int ldb_ret = -1;
169 int i, j;
171 DEBUG(10, ("SearchRequest"));
172 DEBUGADD(10, (" basedn: %s", req->basedn));
173 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
175 local_ctx = talloc_new(call);
176 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
178 basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
179 VALID_DN_SYNTAX(basedn, 0);
181 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
182 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
184 switch (req->scope) {
185 case LDAP_SEARCH_SCOPE_BASE:
186 scope_str = "BASE";
187 scope = LDB_SCOPE_BASE;
188 success_limit = 0;
189 break;
190 case LDAP_SEARCH_SCOPE_SINGLE:
191 scope_str = "ONE";
192 scope = LDB_SCOPE_ONELEVEL;
193 success_limit = 0;
194 break;
195 case LDAP_SEARCH_SCOPE_SUB:
196 scope_str = "SUB";
197 scope = LDB_SCOPE_SUBTREE;
198 success_limit = 0;
199 break;
200 default:
201 result = LDAP_PROTOCOL_ERROR;
202 errstr = "Invalid scope";
203 goto reply;
205 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
207 if (req->num_attributes >= 1) {
208 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
209 NT_STATUS_HAVE_NO_MEMORY(attrs);
211 for (i=0; i < req->num_attributes; i++) {
212 DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
213 attrs[i] = req->attributes[i];
215 attrs[i] = NULL;
218 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
219 scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
221 res = talloc_zero(local_ctx, struct ldb_result);
222 NT_STATUS_HAVE_NO_MEMORY(res);
224 ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
225 basedn, scope,
226 req->tree, attrs,
227 call->request->controls,
228 res, ldb_search_default_callback,
229 NULL);
231 if (ldb_ret != LDB_SUCCESS) {
232 goto reply;
235 if (call->conn->global_catalog) {
236 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
238 search_options = NULL;
239 if (search_control) {
240 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
241 search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
242 } else {
243 search_options = talloc(lreq, struct ldb_search_options_control);
244 NT_STATUS_HAVE_NO_MEMORY(search_options);
245 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
246 ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
249 ldb_set_timeout(samdb, lreq, req->timelimit);
251 ldb_ret = ldb_request(samdb, lreq);
253 if (ldb_ret != LDB_SUCCESS) {
254 goto reply;
257 ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
259 if (ldb_ret == LDB_SUCCESS) {
260 for (i = 0; i < res->count; i++) {
261 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
262 NT_STATUS_HAVE_NO_MEMORY(ent_r);
264 /* Better to have the whole message kept here,
265 * than to find someone further up didn't put
266 * a value in the right spot in the talloc tree */
267 talloc_steal(ent_r, res->msgs[i]);
269 ent = &ent_r->msg->r.SearchResultEntry;
270 ent->dn = ldb_dn_alloc_linearized(ent_r, res->msgs[i]->dn);
271 ent->num_attributes = 0;
272 ent->attributes = NULL;
273 if (res->msgs[i]->num_elements == 0) {
274 goto queue_reply;
276 ent->num_attributes = res->msgs[i]->num_elements;
277 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
278 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
279 for (j=0; j < ent->num_attributes; j++) {
280 ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name);
281 ent->attributes[j].num_values = 0;
282 ent->attributes[j].values = NULL;
283 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
284 continue;
286 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
287 ent->attributes[j].values = res->msgs[i]->elements[j].values;
288 talloc_steal(ent->attributes, res->msgs[i]->elements[j].values);
290 queue_reply:
291 ldapsrv_queue_reply(call, ent_r);
295 reply:
296 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
297 NT_STATUS_HAVE_NO_MEMORY(done_r);
299 done = &done_r->msg->r.SearchResultDone;
300 done->dn = NULL;
301 done->referral = NULL;
303 if (result != -1) {
304 } else if (ldb_ret == LDB_SUCCESS) {
305 if (res->count >= success_limit) {
306 DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
307 result = LDAP_SUCCESS;
308 errstr = NULL;
310 if (res->controls) {
311 done_r->msg->controls = res->controls;
312 talloc_steal(done_r, res->controls);
314 } else {
315 DEBUG(10,("SearchRequest: error\n"));
316 result = map_ldb_error(samdb, ldb_ret, &errstr);
319 done->resultcode = result;
320 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
322 talloc_free(local_ctx);
324 ldapsrv_queue_reply(call, done_r);
325 return NT_STATUS_OK;
328 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
330 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
331 struct ldap_Result *modify_result;
332 struct ldapsrv_reply *modify_reply;
333 void *local_ctx;
334 struct ldb_context *samdb = call->conn->ldb;
335 struct ldb_message *msg = NULL;
336 struct ldb_dn *dn;
337 const char *errstr = NULL;
338 int result = LDAP_SUCCESS;
339 int ldb_ret;
340 int i,j;
342 DEBUG(10, ("ModifyRequest"));
343 DEBUGADD(10, (" dn: %s", req->dn));
345 local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
346 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
348 dn = ldb_dn_new(local_ctx, samdb, req->dn);
349 VALID_DN_SYNTAX(dn, 0);
351 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
353 msg = talloc(local_ctx, struct ldb_message);
354 NT_STATUS_HAVE_NO_MEMORY(msg);
356 msg->dn = dn;
357 msg->num_elements = 0;
358 msg->elements = NULL;
360 if (req->num_mods > 0) {
361 msg->num_elements = req->num_mods;
362 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
363 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
365 for (i=0; i < msg->num_elements; i++) {
366 msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
367 msg->elements[i].num_values = 0;
368 msg->elements[i].values = NULL;
370 switch (req->mods[i].type) {
371 default:
372 result = LDAP_PROTOCOL_ERROR;
373 errstr = "Invalid LDAP_MODIFY_* type";
374 goto reply;
375 case LDAP_MODIFY_ADD:
376 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
377 break;
378 case LDAP_MODIFY_DELETE:
379 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
380 break;
381 case LDAP_MODIFY_REPLACE:
382 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
383 break;
386 msg->elements[i].num_values = req->mods[i].attrib.num_values;
387 if (msg->elements[i].num_values > 0) {
388 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
389 msg->elements[i].num_values);
390 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
392 for (j=0; j < msg->elements[i].num_values; j++) {
393 if (!(req->mods[i].attrib.values[j].length > 0)) {
394 result = LDAP_OTHER;
395 errstr = "Empty attribute values are not allowed";
396 goto reply;
398 msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
399 msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;
403 } else {
404 result = LDAP_OTHER;
405 errstr = "No mods are not allowed";
406 goto reply;
409 reply:
410 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
411 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
413 if (result == LDAP_SUCCESS) {
414 ldb_ret = ldb_modify(samdb, msg);
415 result = map_ldb_error(samdb, ldb_ret, &errstr);
418 modify_result = &modify_reply->msg->r.AddResponse;
419 modify_result->dn = NULL;
420 modify_result->resultcode = result;
421 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
422 modify_result->referral = NULL;
424 talloc_free(local_ctx);
426 ldapsrv_queue_reply(call, modify_reply);
427 return NT_STATUS_OK;
431 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
433 struct ldap_AddRequest *req = &call->request->r.AddRequest;
434 struct ldap_Result *add_result;
435 struct ldapsrv_reply *add_reply;
436 void *local_ctx;
437 struct ldb_context *samdb = call->conn->ldb;
438 struct ldb_message *msg = NULL;
439 struct ldb_dn *dn;
440 const char *errstr = NULL;
441 int result = LDAP_SUCCESS;
442 int ldb_ret;
443 int i,j;
445 DEBUG(10, ("AddRequest"));
446 DEBUGADD(10, (" dn: %s", req->dn));
448 local_ctx = talloc_named(call, 0, "AddRequest local memory context");
449 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
451 dn = ldb_dn_new(local_ctx, samdb, req->dn);
452 VALID_DN_SYNTAX(dn,1);
454 DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
456 msg = talloc(local_ctx, struct ldb_message);
457 NT_STATUS_HAVE_NO_MEMORY(msg);
459 msg->dn = dn;
460 msg->num_elements = 0;
461 msg->elements = NULL;
463 if (req->num_attributes > 0) {
464 msg->num_elements = req->num_attributes;
465 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
466 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
468 for (i=0; i < msg->num_elements; i++) {
469 msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
470 msg->elements[i].flags = 0;
471 msg->elements[i].num_values = 0;
472 msg->elements[i].values = NULL;
474 if (req->attributes[i].num_values > 0) {
475 msg->elements[i].num_values = req->attributes[i].num_values;
476 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
477 msg->elements[i].num_values);
478 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
480 for (j=0; j < msg->elements[i].num_values; j++) {
481 if (!(req->attributes[i].values[j].length > 0)) {
482 result = LDAP_OTHER;
483 errstr = "Empty attribute values are not allowed";
484 goto reply;
486 msg->elements[i].values[j].length = req->attributes[i].values[j].length;
487 msg->elements[i].values[j].data = req->attributes[i].values[j].data;
489 } else {
490 result = LDAP_OTHER;
491 errstr = "No attribute values are not allowed";
492 goto reply;
495 } else {
496 result = LDAP_OTHER;
497 errstr = "No attributes are not allowed";
498 goto reply;
501 reply:
502 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
503 NT_STATUS_HAVE_NO_MEMORY(add_reply);
505 if (result == LDAP_SUCCESS) {
506 ldb_ret = ldb_add(samdb, msg);
507 result = map_ldb_error(samdb, ldb_ret, &errstr);
510 add_result = &add_reply->msg->r.AddResponse;
511 add_result->dn = NULL;
512 add_result->resultcode = result;
513 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
514 add_result->referral = NULL;
516 talloc_free(local_ctx);
518 ldapsrv_queue_reply(call, add_reply);
519 return NT_STATUS_OK;
523 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
525 struct ldap_DelRequest *req = &call->request->r.DelRequest;
526 struct ldap_Result *del_result;
527 struct ldapsrv_reply *del_reply;
528 void *local_ctx;
529 struct ldb_context *samdb = call->conn->ldb;
530 struct ldb_dn *dn;
531 const char *errstr = NULL;
532 int result = LDAP_SUCCESS;
533 int ldb_ret;
535 DEBUG(10, ("DelRequest"));
536 DEBUGADD(10, (" dn: %s", req->dn));
538 local_ctx = talloc_named(call, 0, "DelRequest local memory context");
539 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
541 dn = ldb_dn_new(local_ctx, samdb, req->dn);
542 VALID_DN_SYNTAX(dn,1);
544 DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
546 reply:
547 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
548 NT_STATUS_HAVE_NO_MEMORY(del_reply);
550 if (result == LDAP_SUCCESS) {
551 ldb_ret = ldb_delete(samdb, dn);
552 result = map_ldb_error(samdb, ldb_ret, &errstr);
555 del_result = &del_reply->msg->r.DelResponse;
556 del_result->dn = NULL;
557 del_result->resultcode = result;
558 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
559 del_result->referral = NULL;
561 talloc_free(local_ctx);
563 ldapsrv_queue_reply(call, del_reply);
564 return NT_STATUS_OK;
567 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
569 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
570 struct ldap_Result *modifydn;
571 struct ldapsrv_reply *modifydn_r;
572 void *local_ctx;
573 struct ldb_context *samdb = call->conn->ldb;
574 struct ldb_dn *olddn, *newdn=NULL, *newrdn;
575 struct ldb_dn *parentdn = NULL;
576 const char *errstr = NULL;
577 int result = LDAP_SUCCESS;
578 int ldb_ret;
580 DEBUG(10, ("ModifyDNRequrest"));
581 DEBUGADD(10, (" dn: %s", req->dn));
582 DEBUGADD(10, (" newrdn: %s", req->newrdn));
584 local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
585 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
587 olddn = ldb_dn_new(local_ctx, samdb, req->dn);
588 VALID_DN_SYNTAX(olddn, 2);
590 newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
591 VALID_DN_SYNTAX(newrdn, 1);
593 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
594 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
596 /* we can't handle the rename if we should not remove the old dn */
597 if (!req->deleteolddn) {
598 result = LDAP_UNWILLING_TO_PERFORM;
599 errstr = "Old RDN must be deleted";
600 goto reply;
603 if (req->newsuperior) {
604 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
605 VALID_DN_SYNTAX(parentdn, 0);
606 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
608 if (ldb_dn_get_comp_num(parentdn) < 1) {
609 result = LDAP_AFFECTS_MULTIPLE_DSAS;
610 errstr = "Error new Superior DN invalid";
611 goto reply;
615 if (!parentdn) {
616 parentdn = ldb_dn_get_parent(local_ctx, olddn);
617 NT_STATUS_HAVE_NO_MEMORY(parentdn);
620 if ( ! ldb_dn_add_child_fmt(parentdn,
621 "%s=%s",
622 ldb_dn_get_rdn_name(newrdn),
623 (char *)ldb_dn_get_rdn_val(newrdn)->data)) {
624 result = LDAP_OTHER;
625 goto reply;
627 newdn = parentdn;
629 reply:
630 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
631 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
633 if (result == LDAP_SUCCESS) {
634 ldb_ret = ldb_rename(samdb, olddn, newdn);
635 result = map_ldb_error(samdb, ldb_ret, &errstr);
638 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
639 modifydn->dn = NULL;
640 modifydn->resultcode = result;
641 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
642 modifydn->referral = NULL;
644 talloc_free(local_ctx);
646 ldapsrv_queue_reply(call, modifydn_r);
647 return NT_STATUS_OK;
650 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
652 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
653 struct ldap_Result *compare;
654 struct ldapsrv_reply *compare_r;
655 void *local_ctx;
656 struct ldb_context *samdb = call->conn->ldb;
657 struct ldb_result *res = NULL;
658 struct ldb_dn *dn;
659 const char *attrs[1];
660 const char *errstr = NULL;
661 const char *filter = NULL;
662 int result = LDAP_SUCCESS;
663 int ldb_ret;
665 DEBUG(10, ("CompareRequest"));
666 DEBUGADD(10, (" dn: %s", req->dn));
668 local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
669 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
671 dn = ldb_dn_new(local_ctx, samdb, req->dn);
672 VALID_DN_SYNTAX(dn, 1);
674 DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
675 filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute,
676 (int)req->value.length, req->value.data);
677 NT_STATUS_HAVE_NO_MEMORY(filter);
679 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
681 attrs[0] = NULL;
683 reply:
684 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
685 NT_STATUS_HAVE_NO_MEMORY(compare_r);
687 if (result == LDAP_SUCCESS) {
688 ldb_ret = ldb_search(samdb, local_ctx, &res,
689 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
690 if (ldb_ret != LDB_SUCCESS) {
691 result = map_ldb_error(samdb, ldb_ret, &errstr);
692 DEBUG(10,("CompareRequest: error: %s\n", errstr));
693 } else if (res->count == 0) {
694 DEBUG(10,("CompareRequest: doesn't matched\n"));
695 result = LDAP_COMPARE_FALSE;
696 errstr = NULL;
697 } else if (res->count == 1) {
698 DEBUG(10,("CompareRequest: matched\n"));
699 result = LDAP_COMPARE_TRUE;
700 errstr = NULL;
701 } else if (res->count > 1) {
702 result = LDAP_OTHER;
703 errstr = "too many objects match";
704 DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
708 compare = &compare_r->msg->r.CompareResponse;
709 compare->dn = NULL;
710 compare->resultcode = result;
711 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
712 compare->referral = NULL;
714 talloc_free(local_ctx);
716 ldapsrv_queue_reply(call, compare_r);
717 return NT_STATUS_OK;
720 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
722 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
723 DEBUG(10, ("AbandonRequest\n"));
724 return NT_STATUS_OK;
727 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
729 int i;
730 struct ldap_message *msg = call->request;
731 /* Check for undecoded critical extensions */
732 for (i=0; msg->controls && msg->controls[i]; i++) {
733 if (!msg->controls_decoded[i] &&
734 msg->controls[i]->critical) {
735 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
736 msg->controls[i]->oid));
737 return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
741 switch(call->request->type) {
742 case LDAP_TAG_BindRequest:
743 return ldapsrv_BindRequest(call);
744 case LDAP_TAG_UnbindRequest:
745 return ldapsrv_UnbindRequest(call);
746 case LDAP_TAG_SearchRequest:
747 return ldapsrv_SearchRequest(call);
748 case LDAP_TAG_ModifyRequest:
749 return ldapsrv_ModifyRequest(call);
750 case LDAP_TAG_AddRequest:
751 return ldapsrv_AddRequest(call);
752 case LDAP_TAG_DelRequest:
753 return ldapsrv_DelRequest(call);
754 case LDAP_TAG_ModifyDNRequest:
755 return ldapsrv_ModifyDNRequest(call);
756 case LDAP_TAG_CompareRequest:
757 return ldapsrv_CompareRequest(call);
758 case LDAP_TAG_AbandonRequest:
759 return ldapsrv_AbandonRequest(call);
760 case LDAP_TAG_ExtendedRequest:
761 return ldapsrv_ExtendedRequest(call);
762 default:
763 return ldapsrv_unwilling(call, 2);