[s3]zfsacl: Prevent calling POSIX ACL vfs methods on zfs share.
[Samba/gbeck.git] / source4 / ldap_server / ldap_backend.c
blob2adff2a1dfad5d5e9c43075dddc3fb4a2f6196ac
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) 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;\
40 } while(0)
42 static int map_ldb_error(struct ldb_context *ldb, int err, const char **errstring)
44 *errstring = ldb_errstring(ldb);
46 /* its 1:1 for now */
47 return err;
51 connect to the sam database
53 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn)
55 conn->ldb = ldb_wrap_connect(conn,
56 conn->connection->event.ctx,
57 conn->lp_ctx,
58 lp_sam_url(conn->lp_ctx),
59 conn->session_info,
60 samdb_credentials(conn, conn->connection->event.ctx, conn->lp_ctx),
61 conn->global_catalog ? LDB_FLG_RDONLY : 0, NULL);
62 if (conn->ldb == NULL) {
63 return NT_STATUS_INTERNAL_DB_CORRUPTION;
66 if (conn->server_credentials) {
67 char **sasl_mechs = NULL;
68 struct gensec_security_ops **backends = gensec_security_all();
69 struct gensec_security_ops **ops
70 = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
71 int i, j = 0;
72 for (i = 0; ops && ops[i]; i++) {
73 if (!gensec_security_ops_enabled(ops[i], conn->lp_ctx))
74 continue;
76 if (ops[i]->sasl_name && ops[i]->server_start) {
77 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
79 if (!sasl_name) {
80 return NT_STATUS_NO_MEMORY;
82 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
83 if (!sasl_mechs) {
84 return NT_STATUS_NO_MEMORY;
86 sasl_mechs[j] = sasl_name;
87 talloc_steal(sasl_mechs, sasl_name);
88 sasl_mechs[j+1] = NULL;
89 j++;
92 talloc_free(ops);
93 ldb_set_opaque(conn->ldb, "supportedSASLMechanims", sasl_mechs);
96 return NT_STATUS_OK;
99 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
101 struct ldapsrv_reply *reply;
103 reply = talloc(call, struct ldapsrv_reply);
104 if (!reply) {
105 return NULL;
107 reply->msg = talloc(reply, struct ldap_message);
108 if (reply->msg == NULL) {
109 talloc_free(reply);
110 return NULL;
113 reply->msg->messageid = call->request->messageid;
114 reply->msg->type = type;
115 reply->msg->controls = NULL;
117 return reply;
120 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
122 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
125 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
127 struct ldapsrv_reply *reply;
128 struct ldap_ExtendedResponse *r;
130 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
132 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
133 if (!reply) {
134 return NT_STATUS_NO_MEMORY;
137 r = &reply->msg->r.ExtendedResponse;
138 r->response.resultcode = error;
139 r->response.dn = NULL;
140 r->response.errormessage = NULL;
141 r->response.referral = NULL;
142 r->oid = NULL;
143 r->value = NULL;
145 ldapsrv_queue_reply(call, reply);
146 return NT_STATUS_OK;
149 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
151 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
152 struct ldap_SearchResEntry *ent;
153 struct ldap_Result *done;
154 struct ldapsrv_reply *ent_r, *done_r;
155 void *local_ctx;
156 struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
157 struct ldb_dn *basedn;
158 struct ldb_result *res = NULL;
159 struct ldb_request *lreq;
160 struct ldb_control *search_control;
161 struct ldb_search_options_control *search_options;
162 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
163 const char **attrs = NULL;
164 const char *scope_str, *errstr = NULL;
165 int success_limit = 1;
166 int result = -1;
167 int ldb_ret = -1;
168 int i, j;
170 DEBUG(10, ("SearchRequest"));
171 DEBUGADD(10, (" basedn: %s", req->basedn));
172 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
174 local_ctx = talloc_new(call);
175 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
177 basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
178 VALID_DN_SYNTAX(basedn);
180 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
181 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
183 switch (req->scope) {
184 case LDAP_SEARCH_SCOPE_BASE:
185 scope_str = "BASE";
186 scope = LDB_SCOPE_BASE;
187 success_limit = 0;
188 break;
189 case LDAP_SEARCH_SCOPE_SINGLE:
190 scope_str = "ONE";
191 scope = LDB_SCOPE_ONELEVEL;
192 success_limit = 0;
193 break;
194 case LDAP_SEARCH_SCOPE_SUB:
195 scope_str = "SUB";
196 scope = LDB_SCOPE_SUBTREE;
197 success_limit = 0;
198 break;
199 default:
200 result = LDAP_PROTOCOL_ERROR;
201 errstr = "Invalid scope";
202 goto reply;
204 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
206 if (req->num_attributes >= 1) {
207 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
208 NT_STATUS_HAVE_NO_MEMORY(attrs);
210 for (i=0; i < req->num_attributes; i++) {
211 DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
212 attrs[i] = req->attributes[i];
214 attrs[i] = NULL;
217 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
218 scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
220 res = talloc_zero(local_ctx, struct ldb_result);
221 NT_STATUS_HAVE_NO_MEMORY(res);
223 ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
224 basedn, scope,
225 req->tree, attrs,
226 call->request->controls,
227 res, ldb_search_default_callback,
228 NULL);
230 if (ldb_ret != LDB_SUCCESS) {
231 goto reply;
234 if (call->conn->global_catalog) {
235 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
237 search_options = NULL;
238 if (search_control) {
239 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
240 search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
241 } else {
242 search_options = talloc(lreq, struct ldb_search_options_control);
243 NT_STATUS_HAVE_NO_MEMORY(search_options);
244 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
245 ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
248 ldb_set_timeout(samdb, lreq, req->timelimit);
250 ldb_ret = ldb_request(samdb, lreq);
252 if (ldb_ret != LDB_SUCCESS) {
253 goto reply;
256 ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
258 if (ldb_ret == LDB_SUCCESS) {
259 for (i = 0; i < res->count; i++) {
260 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
261 NT_STATUS_HAVE_NO_MEMORY(ent_r);
263 /* Better to have the whole message kept here,
264 * than to find someone further up didn't put
265 * a value in the right spot in the talloc tree */
266 talloc_steal(ent_r, res->msgs[i]);
268 ent = &ent_r->msg->r.SearchResultEntry;
269 ent->dn = ldb_dn_alloc_linearized(ent_r, res->msgs[i]->dn);
270 ent->num_attributes = 0;
271 ent->attributes = NULL;
272 if (res->msgs[i]->num_elements == 0) {
273 goto queue_reply;
275 ent->num_attributes = res->msgs[i]->num_elements;
276 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
277 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
278 for (j=0; j < ent->num_attributes; j++) {
279 ent->attributes[j].name = talloc_steal(ent->attributes, res->msgs[i]->elements[j].name);
280 ent->attributes[j].num_values = 0;
281 ent->attributes[j].values = NULL;
282 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
283 continue;
285 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
286 ent->attributes[j].values = res->msgs[i]->elements[j].values;
287 talloc_steal(ent->attributes, res->msgs[i]->elements[j].values);
289 queue_reply:
290 ldapsrv_queue_reply(call, ent_r);
294 reply:
295 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
296 NT_STATUS_HAVE_NO_MEMORY(done_r);
298 done = &done_r->msg->r.SearchResultDone;
299 done->dn = NULL;
300 done->referral = NULL;
302 if (result != -1) {
303 } else if (ldb_ret == LDB_SUCCESS) {
304 if (res->count >= success_limit) {
305 DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
306 result = LDAP_SUCCESS;
307 errstr = NULL;
309 if (res->controls) {
310 done_r->msg->controls = res->controls;
311 talloc_steal(done_r, res->controls);
313 } else {
314 DEBUG(10,("SearchRequest: error\n"));
315 result = map_ldb_error(samdb, ldb_ret, &errstr);
318 done->resultcode = result;
319 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
321 talloc_free(local_ctx);
323 ldapsrv_queue_reply(call, done_r);
324 return NT_STATUS_OK;
327 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
329 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
330 struct ldap_Result *modify_result;
331 struct ldapsrv_reply *modify_reply;
332 void *local_ctx;
333 struct ldb_context *samdb = call->conn->ldb;
334 struct ldb_message *msg = NULL;
335 struct ldb_dn *dn;
336 const char *errstr = NULL;
337 int result = LDAP_SUCCESS;
338 int ldb_ret;
339 int i,j;
341 DEBUG(10, ("ModifyRequest"));
342 DEBUGADD(10, (" dn: %s", req->dn));
344 local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
345 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
347 dn = ldb_dn_new(local_ctx, samdb, req->dn);
348 VALID_DN_SYNTAX(dn);
350 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
352 msg = talloc(local_ctx, struct ldb_message);
353 NT_STATUS_HAVE_NO_MEMORY(msg);
355 msg->dn = dn;
356 msg->num_elements = 0;
357 msg->elements = NULL;
359 if (req->num_mods > 0) {
360 msg->num_elements = req->num_mods;
361 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
362 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
364 for (i=0; i < msg->num_elements; i++) {
365 msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
366 msg->elements[i].num_values = 0;
367 msg->elements[i].values = NULL;
369 switch (req->mods[i].type) {
370 default:
371 result = LDAP_PROTOCOL_ERROR;
372 errstr = "Invalid LDAP_MODIFY_* type";
373 goto reply;
374 case LDAP_MODIFY_ADD:
375 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
376 break;
377 case LDAP_MODIFY_DELETE:
378 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
379 break;
380 case LDAP_MODIFY_REPLACE:
381 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
382 break;
385 msg->elements[i].num_values = req->mods[i].attrib.num_values;
386 if (msg->elements[i].num_values > 0) {
387 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
388 msg->elements[i].num_values);
389 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
391 for (j=0; j < msg->elements[i].num_values; j++) {
392 if (!(req->mods[i].attrib.values[j].length > 0)) {
393 result = LDAP_OTHER;
394 errstr = "Empty attribute values are not allowed";
395 goto reply;
397 msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
398 msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;
402 } else {
403 result = LDAP_OTHER;
404 errstr = "No mods are not allowed";
405 goto reply;
408 reply:
409 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
410 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
412 if (result == LDAP_SUCCESS) {
413 ldb_ret = ldb_modify(samdb, msg);
414 result = map_ldb_error(samdb, ldb_ret, &errstr);
417 modify_result = &modify_reply->msg->r.AddResponse;
418 modify_result->dn = NULL;
419 modify_result->resultcode = result;
420 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
421 modify_result->referral = NULL;
423 talloc_free(local_ctx);
425 ldapsrv_queue_reply(call, modify_reply);
426 return NT_STATUS_OK;
430 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
432 struct ldap_AddRequest *req = &call->request->r.AddRequest;
433 struct ldap_Result *add_result;
434 struct ldapsrv_reply *add_reply;
435 void *local_ctx;
436 struct ldb_context *samdb = call->conn->ldb;
437 struct ldb_message *msg = NULL;
438 struct ldb_dn *dn;
439 const char *errstr = NULL;
440 int result = LDAP_SUCCESS;
441 int ldb_ret;
442 int i,j;
444 DEBUG(10, ("AddRequest"));
445 DEBUGADD(10, (" dn: %s", req->dn));
447 local_ctx = talloc_named(call, 0, "AddRequest local memory context");
448 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
450 dn = ldb_dn_new(local_ctx, samdb, req->dn);
451 VALID_DN_SYNTAX(dn);
453 DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
455 msg = talloc(local_ctx, struct ldb_message);
456 NT_STATUS_HAVE_NO_MEMORY(msg);
458 msg->dn = dn;
459 msg->num_elements = 0;
460 msg->elements = NULL;
462 if (req->num_attributes > 0) {
463 msg->num_elements = req->num_attributes;
464 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
465 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
467 for (i=0; i < msg->num_elements; i++) {
468 msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
469 msg->elements[i].flags = 0;
470 msg->elements[i].num_values = 0;
471 msg->elements[i].values = NULL;
473 if (req->attributes[i].num_values > 0) {
474 msg->elements[i].num_values = req->attributes[i].num_values;
475 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
476 msg->elements[i].num_values);
477 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
479 for (j=0; j < msg->elements[i].num_values; j++) {
480 if (!(req->attributes[i].values[j].length > 0)) {
481 result = LDAP_OTHER;
482 errstr = "Empty attribute values are not allowed";
483 goto reply;
485 msg->elements[i].values[j].length = req->attributes[i].values[j].length;
486 msg->elements[i].values[j].data = req->attributes[i].values[j].data;
488 } else {
489 result = LDAP_OTHER;
490 errstr = "No attribute values are not allowed";
491 goto reply;
494 } else {
495 result = LDAP_OTHER;
496 errstr = "No attributes are not allowed";
497 goto reply;
500 reply:
501 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
502 NT_STATUS_HAVE_NO_MEMORY(add_reply);
504 if (result == LDAP_SUCCESS) {
505 ldb_ret = ldb_add(samdb, msg);
506 result = map_ldb_error(samdb, ldb_ret, &errstr);
509 add_result = &add_reply->msg->r.AddResponse;
510 add_result->dn = NULL;
511 add_result->resultcode = result;
512 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
513 add_result->referral = NULL;
515 talloc_free(local_ctx);
517 ldapsrv_queue_reply(call, add_reply);
518 return NT_STATUS_OK;
522 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
524 struct ldap_DelRequest *req = &call->request->r.DelRequest;
525 struct ldap_Result *del_result;
526 struct ldapsrv_reply *del_reply;
527 void *local_ctx;
528 struct ldb_context *samdb = call->conn->ldb;
529 struct ldb_dn *dn;
530 const char *errstr = NULL;
531 int result = LDAP_SUCCESS;
532 int ldb_ret;
534 DEBUG(10, ("DelRequest"));
535 DEBUGADD(10, (" dn: %s", req->dn));
537 local_ctx = talloc_named(call, 0, "DelRequest local memory context");
538 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
540 dn = ldb_dn_new(local_ctx, samdb, req->dn);
541 VALID_DN_SYNTAX(dn);
543 DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
545 reply:
546 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
547 NT_STATUS_HAVE_NO_MEMORY(del_reply);
549 if (result == LDAP_SUCCESS) {
550 ldb_ret = ldb_delete(samdb, dn);
551 result = map_ldb_error(samdb, ldb_ret, &errstr);
554 del_result = &del_reply->msg->r.DelResponse;
555 del_result->dn = NULL;
556 del_result->resultcode = result;
557 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
558 del_result->referral = NULL;
560 talloc_free(local_ctx);
562 ldapsrv_queue_reply(call, del_reply);
563 return NT_STATUS_OK;
566 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
568 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
569 struct ldap_Result *modifydn;
570 struct ldapsrv_reply *modifydn_r;
571 void *local_ctx;
572 struct ldb_context *samdb = call->conn->ldb;
573 struct ldb_dn *olddn, *newdn=NULL, *newrdn;
574 struct ldb_dn *parentdn = NULL;
575 const char *errstr = NULL;
576 int result = LDAP_SUCCESS;
577 int ldb_ret;
579 DEBUG(10, ("ModifyDNRequrest"));
580 DEBUGADD(10, (" dn: %s", req->dn));
581 DEBUGADD(10, (" newrdn: %s", req->newrdn));
583 local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
584 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
586 olddn = ldb_dn_new(local_ctx, samdb, req->dn);
587 VALID_DN_SYNTAX(olddn);
589 newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
590 VALID_DN_SYNTAX(newrdn);
592 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
593 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
595 /* we can't handle the rename if we should not remove the old dn */
596 if (!req->deleteolddn) {
597 result = LDAP_UNWILLING_TO_PERFORM;
598 errstr = "Old RDN must be deleted";
599 goto reply;
602 if (req->newsuperior) {
603 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
604 VALID_DN_SYNTAX(parentdn);
605 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
607 if (ldb_dn_get_comp_num(parentdn) < 1) {
608 result = LDAP_AFFECTS_MULTIPLE_DSAS;
609 errstr = "Error new Superior DN invalid";
610 goto reply;
614 if (!parentdn) {
615 parentdn = ldb_dn_get_parent(local_ctx, olddn);
616 NT_STATUS_HAVE_NO_MEMORY(parentdn);
619 if ( ! ldb_dn_add_child_fmt(parentdn,
620 "%s=%s",
621 ldb_dn_get_rdn_name(newrdn),
622 (char *)ldb_dn_get_rdn_val(newrdn)->data)) {
623 result = LDAP_OTHER;
624 goto reply;
626 newdn = parentdn;
628 reply:
629 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
630 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
632 if (result == LDAP_SUCCESS) {
633 ldb_ret = ldb_rename(samdb, olddn, newdn);
634 result = map_ldb_error(samdb, ldb_ret, &errstr);
637 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
638 modifydn->dn = NULL;
639 modifydn->resultcode = result;
640 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
641 modifydn->referral = NULL;
643 talloc_free(local_ctx);
645 ldapsrv_queue_reply(call, modifydn_r);
646 return NT_STATUS_OK;
649 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
651 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
652 struct ldap_Result *compare;
653 struct ldapsrv_reply *compare_r;
654 void *local_ctx;
655 struct ldb_context *samdb = call->conn->ldb;
656 struct ldb_result *res = NULL;
657 struct ldb_dn *dn;
658 const char *attrs[1];
659 const char *errstr = NULL;
660 const char *filter = NULL;
661 int result = LDAP_SUCCESS;
662 int ldb_ret;
664 DEBUG(10, ("CompareRequest"));
665 DEBUGADD(10, (" dn: %s", req->dn));
667 local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
668 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
670 dn = ldb_dn_new(local_ctx, samdb, req->dn);
671 VALID_DN_SYNTAX(dn);
673 DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
674 filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute,
675 (int)req->value.length, req->value.data);
676 NT_STATUS_HAVE_NO_MEMORY(filter);
678 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
680 attrs[0] = NULL;
682 reply:
683 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
684 NT_STATUS_HAVE_NO_MEMORY(compare_r);
686 if (result == LDAP_SUCCESS) {
687 ldb_ret = ldb_search(samdb, local_ctx, &res,
688 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
689 if (ldb_ret != LDB_SUCCESS) {
690 result = map_ldb_error(samdb, ldb_ret, &errstr);
691 DEBUG(10,("CompareRequest: error: %s\n", errstr));
692 } else if (res->count == 0) {
693 DEBUG(10,("CompareRequest: doesn't matched\n"));
694 result = LDAP_COMPARE_FALSE;
695 errstr = NULL;
696 } else if (res->count == 1) {
697 DEBUG(10,("CompareRequest: matched\n"));
698 result = LDAP_COMPARE_TRUE;
699 errstr = NULL;
700 } else if (res->count > 1) {
701 result = LDAP_OTHER;
702 errstr = "too many objects match";
703 DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
707 compare = &compare_r->msg->r.CompareResponse;
708 compare->dn = NULL;
709 compare->resultcode = result;
710 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
711 compare->referral = NULL;
713 talloc_free(local_ctx);
715 ldapsrv_queue_reply(call, compare_r);
716 return NT_STATUS_OK;
719 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
721 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
722 DEBUG(10, ("AbandonRequest\n"));
723 return NT_STATUS_OK;
726 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
728 int i;
729 struct ldap_message *msg = call->request;
730 /* Check for undecoded critical extensions */
731 for (i=0; msg->controls && msg->controls[i]; i++) {
732 if (!msg->controls_decoded[i] &&
733 msg->controls[i]->critical) {
734 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
735 msg->controls[i]->oid));
736 return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
740 switch(call->request->type) {
741 case LDAP_TAG_BindRequest:
742 return ldapsrv_BindRequest(call);
743 case LDAP_TAG_UnbindRequest:
744 return ldapsrv_UnbindRequest(call);
745 case LDAP_TAG_SearchRequest:
746 return ldapsrv_SearchRequest(call);
747 case LDAP_TAG_ModifyRequest:
748 return ldapsrv_ModifyRequest(call);
749 case LDAP_TAG_AddRequest:
750 return ldapsrv_AddRequest(call);
751 case LDAP_TAG_DelRequest:
752 return ldapsrv_DelRequest(call);
753 case LDAP_TAG_ModifyDNRequest:
754 return ldapsrv_ModifyDNRequest(call);
755 case LDAP_TAG_CompareRequest:
756 return ldapsrv_CompareRequest(call);
757 case LDAP_TAG_AbandonRequest:
758 return ldapsrv_AbandonRequest(call);
759 case LDAP_TAG_ExtendedRequest:
760 return ldapsrv_ExtendedRequest(call);
761 default:
762 return ldapsrv_unwilling(call, 2);