2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Matthias Dieter Wallnöfer 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "ldap_server/ldap_server.h"
23 #include "../lib/util/dlinklist.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 {\
34 return NT_STATUS_NO_MEMORY;\
35 } else if ( ! ldb_dn_validate(dn)) {\
36 result = LDAP_INVALID_DN_SYNTAX;\
37 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, &errstr);\
42 static int map_ldb_error(TALLOC_CTX *mem_ctx, int ldb_err,
43 const char **errstring)
51 case LDB_ERR_OPERATIONS_ERROR:
52 err = WERR_DS_OPERATIONS_ERROR;
54 case LDB_ERR_PROTOCOL_ERROR:
55 err = WERR_DS_PROTOCOL_ERROR;
57 case LDB_ERR_TIME_LIMIT_EXCEEDED:
58 err = WERR_DS_TIMELIMIT_EXCEEDED;
60 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
61 err = WERR_DS_SIZE_LIMIT_EXCEEDED;
63 case LDB_ERR_COMPARE_FALSE:
64 err = WERR_DS_COMPARE_FALSE;
66 case LDB_ERR_COMPARE_TRUE:
67 err = WERR_DS_COMPARE_TRUE;
69 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
70 err = WERR_DS_AUTH_METHOD_NOT_SUPPORTED;
72 case LDB_ERR_STRONG_AUTH_REQUIRED:
73 err = WERR_DS_STRONG_AUTH_REQUIRED;
75 case LDB_ERR_REFERRAL:
76 err = WERR_DS_REFERRAL;
78 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
79 err = WERR_DS_ADMIN_LIMIT_EXCEEDED;
81 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
82 err = WERR_DS_UNAVAILABLE_CRIT_EXTENSION;
84 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
85 err = WERR_DS_CONFIDENTIALITY_REQUIRED;
87 case LDB_ERR_SASL_BIND_IN_PROGRESS:
90 case LDB_ERR_NO_SUCH_ATTRIBUTE:
91 err = WERR_DS_NO_ATTRIBUTE_OR_VALUE;
93 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
94 err = WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
96 case LDB_ERR_INAPPROPRIATE_MATCHING:
97 err = WERR_DS_INAPPROPRIATE_MATCHING;
99 case LDB_ERR_CONSTRAINT_VIOLATION:
100 err = WERR_DS_CONSTRAINT_VIOLATION;
102 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
103 err = WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS;
105 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
106 err = WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
108 case LDB_ERR_NO_SUCH_OBJECT:
109 err = WERR_DS_NO_SUCH_OBJECT;
111 case LDB_ERR_ALIAS_PROBLEM:
112 err = WERR_DS_ALIAS_PROBLEM;
114 case LDB_ERR_INVALID_DN_SYNTAX:
115 err = WERR_DS_INVALID_DN_SYNTAX;
117 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
118 err = WERR_DS_ALIAS_DEREF_PROBLEM;
120 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
121 err = WERR_DS_INAPPROPRIATE_AUTH;
123 case LDB_ERR_INVALID_CREDENTIALS:
124 err = WERR_ACCESS_DENIED;
126 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
127 err = WERR_DS_INSUFF_ACCESS_RIGHTS;
132 case LDB_ERR_UNAVAILABLE:
133 err = WERR_DS_UNAVAILABLE;
135 case LDB_ERR_UNWILLING_TO_PERFORM:
136 err = WERR_DS_UNWILLING_TO_PERFORM;
138 case LDB_ERR_LOOP_DETECT:
139 err = WERR_DS_LOOP_DETECT;
141 case LDB_ERR_NAMING_VIOLATION:
142 err = WERR_DS_NAMING_VIOLATION;
144 case LDB_ERR_OBJECT_CLASS_VIOLATION:
145 err = WERR_DS_OBJ_CLASS_VIOLATION;
147 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
148 err = WERR_DS_CANT_ON_NON_LEAF;
150 case LDB_ERR_NOT_ALLOWED_ON_RDN:
151 err = WERR_DS_CANT_ON_RDN;
153 case LDB_ERR_ENTRY_ALREADY_EXISTS:
154 err = WERR_DS_OBJ_STRING_NAME_EXISTS;
156 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
157 err = WERR_DS_CANT_MOD_OBJ_CLASS;
159 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
160 err = WERR_DS_AFFECTS_MULTIPLE_DSAS;
163 err = WERR_DS_GENERIC_ERROR;
167 *errstring = talloc_asprintf(mem_ctx, "%08x: %s", W_ERROR_V(err),
168 ldb_strerror(ldb_err));
170 /* result is 1:1 for now */
173 /* create and execute a modify request */
174 static int ldb_mod_req_with_controls(struct ldb_context *ldb,
175 const struct ldb_message *message,
176 struct ldb_control **controls,
179 struct ldb_request *req;
182 ret = ldb_msg_sanity_check(ldb, message);
183 if (ret != LDB_SUCCESS) {
187 ret = ldb_build_mod_req(&req, ldb, ldb,
191 ldb_modify_default_callback,
194 if (ret != LDB_SUCCESS) {
198 ret = ldb_transaction_start(ldb);
199 if (ret != LDB_SUCCESS) {
203 ret = ldb_request(ldb, req);
204 if (ret == LDB_SUCCESS) {
205 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
208 if (ret == LDB_SUCCESS) {
209 ret = ldb_transaction_commit(ldb);
212 ldb_transaction_cancel(ldb);
220 connect to the sam database
222 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn)
224 conn->ldb = ldb_wrap_connect(conn,
225 conn->connection->event.ctx,
227 lp_sam_url(conn->lp_ctx),
229 samdb_credentials(conn->connection->event.ctx, conn->lp_ctx),
230 conn->global_catalog ? LDB_FLG_RDONLY : 0);
231 if (conn->ldb == NULL) {
232 return NT_STATUS_INTERNAL_DB_CORRUPTION;
235 if (conn->server_credentials) {
236 char **sasl_mechs = NULL;
237 struct gensec_security_ops **backends = gensec_security_all();
238 struct gensec_security_ops **ops
239 = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
240 unsigned int i, j = 0;
241 for (i = 0; ops && ops[i]; i++) {
242 if (!lp_parm_bool(conn->lp_ctx, NULL, "gensec", ops[i]->name, ops[i]->enabled))
245 if (ops[i]->sasl_name && ops[i]->server_start) {
246 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
249 return NT_STATUS_NO_MEMORY;
251 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
253 return NT_STATUS_NO_MEMORY;
255 sasl_mechs[j] = sasl_name;
256 talloc_steal(sasl_mechs, sasl_name);
257 sasl_mechs[j+1] = NULL;
261 talloc_unlink(conn, ops);
262 ldb_set_opaque(conn->ldb, "supportedSASLMechanims", sasl_mechs);
268 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
270 struct ldapsrv_reply *reply;
272 reply = talloc(call, struct ldapsrv_reply);
276 reply->msg = talloc(reply, struct ldap_message);
277 if (reply->msg == NULL) {
282 reply->msg->messageid = call->request->messageid;
283 reply->msg->type = type;
284 reply->msg->controls = NULL;
289 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
291 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
294 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
296 struct ldapsrv_reply *reply;
297 struct ldap_ExtendedResponse *r;
299 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
301 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
303 return NT_STATUS_NO_MEMORY;
306 r = &reply->msg->r.ExtendedResponse;
307 r->response.resultcode = error;
308 r->response.dn = NULL;
309 r->response.errormessage = NULL;
310 r->response.referral = NULL;
314 ldapsrv_queue_reply(call, reply);
318 int ldb_add_with_context(struct ldb_context *ldb,
319 const struct ldb_message *message,
322 struct ldb_request *req;
325 ret = ldb_msg_sanity_check(ldb, message);
326 if (ret != LDB_SUCCESS) {
330 ret = ldb_build_add_req(&req, ldb, ldb,
334 ldb_modify_default_callback,
337 if (ret != LDB_SUCCESS) return ret;
339 ret = ldb_transaction_start(ldb);
340 if (ret != LDB_SUCCESS) {
344 ret = ldb_request(ldb, req);
345 if (ret == LDB_SUCCESS) {
346 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
349 if (ret == LDB_SUCCESS) {
350 ret = ldb_transaction_commit(ldb);
353 ldb_transaction_cancel(ldb);
360 int ldb_delete_with_context(struct ldb_context *ldb,
364 struct ldb_request *req;
367 ret = ldb_build_del_req(&req, ldb, ldb,
371 ldb_modify_default_callback,
374 if (ret != LDB_SUCCESS) return ret;
376 ret = ldb_transaction_start(ldb);
377 if (ret != LDB_SUCCESS) {
381 ret = ldb_request(ldb, req);
382 if (ret == LDB_SUCCESS) {
383 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
386 if (ret == LDB_SUCCESS) {
387 ret = ldb_transaction_commit(ldb);
390 ldb_transaction_cancel(ldb);
397 int ldb_rename_with_context(struct ldb_context *ldb,
398 struct ldb_dn *olddn,
399 struct ldb_dn *newdn,
402 struct ldb_request *req;
405 ret = ldb_build_rename_req(&req, ldb, ldb,
410 ldb_modify_default_callback,
413 if (ret != LDB_SUCCESS) return ret;
415 ret = ldb_transaction_start(ldb);
416 if (ret != LDB_SUCCESS) {
420 ret = ldb_request(ldb, req);
421 if (ret == LDB_SUCCESS) {
422 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
425 if (ret == LDB_SUCCESS) {
426 ret = ldb_transaction_commit(ldb);
429 ldb_transaction_cancel(ldb);
436 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
438 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
439 struct ldap_SearchResEntry *ent;
440 struct ldap_Result *done;
441 struct ldapsrv_reply *ent_r, *done_r;
442 TALLOC_CTX *local_ctx;
443 struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
444 struct ldb_dn *basedn;
445 struct ldb_result *res = NULL;
446 struct ldb_request *lreq;
447 struct ldb_control *search_control;
448 struct ldb_search_options_control *search_options;
449 struct ldb_control *extended_dn_control;
450 struct ldb_extended_dn_control *extended_dn_decoded = NULL;
451 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
452 const char **attrs = NULL;
453 const char *scope_str, *errstr = NULL;
454 int success_limit = 1;
458 int extended_type = 1;
460 DEBUG(10, ("SearchRequest"));
461 DEBUGADD(10, (" basedn: %s", req->basedn));
462 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
464 local_ctx = talloc_new(call);
465 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
467 basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
468 VALID_DN_SYNTAX(basedn);
470 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
471 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
473 switch (req->scope) {
474 case LDAP_SEARCH_SCOPE_BASE:
476 scope = LDB_SCOPE_BASE;
479 case LDAP_SEARCH_SCOPE_SINGLE:
481 scope = LDB_SCOPE_ONELEVEL;
484 case LDAP_SEARCH_SCOPE_SUB:
486 scope = LDB_SCOPE_SUBTREE;
490 result = LDAP_PROTOCOL_ERROR;
491 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR,
493 errstr = talloc_asprintf(local_ctx,
494 "%s. Invalid scope", errstr);
497 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
499 if (req->num_attributes >= 1) {
500 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
501 NT_STATUS_HAVE_NO_MEMORY(attrs);
503 for (i=0; i < req->num_attributes; i++) {
504 DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
505 attrs[i] = req->attributes[i];
510 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
511 scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
513 res = talloc_zero(local_ctx, struct ldb_result);
514 NT_STATUS_HAVE_NO_MEMORY(res);
516 ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
519 call->request->controls,
520 res, ldb_search_default_callback,
523 if (ldb_ret != LDB_SUCCESS) {
527 if (call->conn->global_catalog) {
528 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
530 search_options = NULL;
531 if (search_control) {
532 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
533 search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
535 search_options = talloc(lreq, struct ldb_search_options_control);
536 NT_STATUS_HAVE_NO_MEMORY(search_options);
537 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
538 ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
542 extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
544 if (extended_dn_control) {
545 if (extended_dn_control->data) {
546 extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
547 extended_type = extended_dn_decoded->type;
553 ldb_set_timeout(samdb, lreq, req->timelimit);
555 ldb_ret = ldb_request(samdb, lreq);
557 if (ldb_ret != LDB_SUCCESS) {
561 ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
563 if (ldb_ret == LDB_SUCCESS) {
564 for (i = 0; i < res->count; i++) {
565 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
566 NT_STATUS_HAVE_NO_MEMORY(ent_r);
568 /* Better to have the whole message kept here,
569 * than to find someone further up didn't put
570 * a value in the right spot in the talloc tree */
571 talloc_steal(ent_r, res->msgs[i]);
573 ent = &ent_r->msg->r.SearchResultEntry;
574 ent->dn = ldb_dn_get_extended_linearized(ent_r, res->msgs[i]->dn, extended_type);
575 ent->num_attributes = 0;
576 ent->attributes = NULL;
577 if (res->msgs[i]->num_elements == 0) {
580 ent->num_attributes = res->msgs[i]->num_elements;
581 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
582 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
583 for (j=0; j < ent->num_attributes; j++) {
584 ent->attributes[j].name = res->msgs[i]->elements[j].name;
585 ent->attributes[j].num_values = 0;
586 ent->attributes[j].values = NULL;
587 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
590 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
591 ent->attributes[j].values = res->msgs[i]->elements[j].values;
594 ldapsrv_queue_reply(call, ent_r);
597 /* Send back referrals if they do exist (search operations) */
598 if (res->refs != NULL) {
600 struct ldap_SearchResRef *ent_ref;
602 for (ref = res->refs; *ref != NULL; ++ref) {
603 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
604 NT_STATUS_HAVE_NO_MEMORY(ent_r);
606 /* Better to have the whole referrals kept here,
607 * than to find someone further up didn't put
608 * a value in the right spot in the talloc tree
610 talloc_steal(ent_r, *ref);
612 ent_ref = &ent_r->msg->r.SearchResultReference;
613 ent_ref->referral = *ref;
615 ldapsrv_queue_reply(call, ent_r);
621 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
622 NT_STATUS_HAVE_NO_MEMORY(done_r);
624 done = &done_r->msg->r.SearchResultDone;
626 done->referral = NULL;
629 } else if (ldb_ret == LDB_SUCCESS) {
630 if (res->count >= success_limit) {
631 DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
632 result = LDAP_SUCCESS;
636 done_r->msg->controls = res->controls;
637 talloc_steal(done_r, res->controls);
640 DEBUG(10,("SearchRequest: error\n"));
641 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
644 done->resultcode = result;
645 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
647 talloc_free(local_ctx);
649 ldapsrv_queue_reply(call, done_r);
653 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
655 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
656 struct ldap_Result *modify_result;
657 struct ldapsrv_reply *modify_reply;
658 TALLOC_CTX *local_ctx;
659 struct ldb_context *samdb = call->conn->ldb;
660 struct ldb_message *msg = NULL;
662 const char *errstr = NULL;
663 int result = LDAP_SUCCESS;
666 struct ldb_result *res = NULL;
668 DEBUG(10, ("ModifyRequest"));
669 DEBUGADD(10, (" dn: %s", req->dn));
671 local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
672 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
674 dn = ldb_dn_new(local_ctx, samdb, req->dn);
677 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
679 msg = talloc(local_ctx, struct ldb_message);
680 NT_STATUS_HAVE_NO_MEMORY(msg);
683 msg->num_elements = 0;
684 msg->elements = NULL;
686 if (req->num_mods > 0) {
687 msg->num_elements = req->num_mods;
688 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
689 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
691 for (i=0; i < msg->num_elements; i++) {
692 msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
693 msg->elements[i].num_values = 0;
694 msg->elements[i].values = NULL;
696 switch (req->mods[i].type) {
698 result = LDAP_PROTOCOL_ERROR;
699 map_ldb_error(local_ctx,
700 LDB_ERR_PROTOCOL_ERROR, &errstr);
701 errstr = talloc_asprintf(local_ctx,
702 "%s. Invalid LDAP_MODIFY_* type", errstr);
704 case LDAP_MODIFY_ADD:
705 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
707 case LDAP_MODIFY_DELETE:
708 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
710 case LDAP_MODIFY_REPLACE:
711 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
715 msg->elements[i].num_values = req->mods[i].attrib.num_values;
716 if (msg->elements[i].num_values > 0) {
717 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
718 msg->elements[i].num_values);
719 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
721 for (j=0; j < msg->elements[i].num_values; j++) {
722 msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
723 msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;
730 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
731 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
733 if (result == LDAP_SUCCESS) {
734 res = talloc_zero(local_ctx, struct ldb_result);
735 NT_STATUS_HAVE_NO_MEMORY(res);
736 ldb_ret = ldb_mod_req_with_controls(samdb, msg, call->request->controls, res);
737 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
740 modify_result = &modify_reply->msg->r.AddResponse;
741 modify_result->dn = NULL;
743 if (res->refs != NULL) {
744 modify_result->resultcode = map_ldb_error(local_ctx, LDB_ERR_REFERRAL, &errstr);
745 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
746 modify_result->referral = talloc_strdup(call, *res->refs);
748 modify_result->resultcode = result;
749 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
750 modify_result->referral = NULL;
752 talloc_free(local_ctx);
754 ldapsrv_queue_reply(call, modify_reply);
759 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
761 struct ldap_AddRequest *req = &call->request->r.AddRequest;
762 struct ldap_Result *add_result;
763 struct ldapsrv_reply *add_reply;
764 TALLOC_CTX *local_ctx;
765 struct ldb_context *samdb = call->conn->ldb;
766 struct ldb_message *msg = NULL;
768 const char *errstr = NULL;
769 int result = LDAP_SUCCESS;
772 struct ldb_result *res = NULL;
774 DEBUG(10, ("AddRequest"));
775 DEBUGADD(10, (" dn: %s", req->dn));
777 local_ctx = talloc_named(call, 0, "AddRequest local memory context");
778 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
780 dn = ldb_dn_new(local_ctx, samdb, req->dn);
783 DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
785 msg = talloc(local_ctx, struct ldb_message);
786 NT_STATUS_HAVE_NO_MEMORY(msg);
789 msg->num_elements = 0;
790 msg->elements = NULL;
792 if (req->num_attributes > 0) {
793 msg->num_elements = req->num_attributes;
794 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
795 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
797 for (i=0; i < msg->num_elements; i++) {
798 msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
799 msg->elements[i].flags = 0;
800 msg->elements[i].num_values = 0;
801 msg->elements[i].values = NULL;
803 if (req->attributes[i].num_values > 0) {
804 msg->elements[i].num_values = req->attributes[i].num_values;
805 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
806 msg->elements[i].num_values);
807 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
809 for (j=0; j < msg->elements[i].num_values; j++) {
810 msg->elements[i].values[j].length = req->attributes[i].values[j].length;
811 msg->elements[i].values[j].data = req->attributes[i].values[j].data;
818 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
819 NT_STATUS_HAVE_NO_MEMORY(add_reply);
821 if (result == LDAP_SUCCESS) {
822 res = talloc_zero(local_ctx, struct ldb_result);
823 NT_STATUS_HAVE_NO_MEMORY(res);
824 ldb_ret = ldb_add_with_context(samdb, msg, res);
825 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
828 add_result = &add_reply->msg->r.AddResponse;
829 add_result->dn = NULL;
830 if (res->refs != NULL) {
831 add_result->resultcode = map_ldb_error(local_ctx, LDB_ERR_REFERRAL, &errstr);
832 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
833 add_result->referral = talloc_strdup(call, *res->refs);
835 add_result->resultcode = result;
836 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
837 add_result->referral = NULL;
839 talloc_free(local_ctx);
841 ldapsrv_queue_reply(call, add_reply);
846 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
848 struct ldap_DelRequest *req = &call->request->r.DelRequest;
849 struct ldap_Result *del_result;
850 struct ldapsrv_reply *del_reply;
851 TALLOC_CTX *local_ctx;
852 struct ldb_context *samdb = call->conn->ldb;
854 const char *errstr = NULL;
855 int result = LDAP_SUCCESS;
857 struct ldb_result *res = NULL;
859 DEBUG(10, ("DelRequest"));
860 DEBUGADD(10, (" dn: %s", req->dn));
862 local_ctx = talloc_named(call, 0, "DelRequest local memory context");
863 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
865 dn = ldb_dn_new(local_ctx, samdb, req->dn);
868 DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
871 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
872 NT_STATUS_HAVE_NO_MEMORY(del_reply);
874 if (result == LDAP_SUCCESS) {
875 res = talloc_zero(local_ctx, struct ldb_result);
876 NT_STATUS_HAVE_NO_MEMORY(res);
877 ldb_ret = ldb_delete_with_context(samdb, dn, res);
878 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
881 del_result = &del_reply->msg->r.DelResponse;
882 del_result->dn = NULL;
883 if (res->refs != NULL) {
884 del_result->resultcode = map_ldb_error(local_ctx, LDB_ERR_REFERRAL, &errstr);
885 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
886 del_result->referral = talloc_strdup(call, *res->refs);
888 del_result->resultcode = result;
889 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
890 del_result->referral = NULL;
893 talloc_free(local_ctx);
895 ldapsrv_queue_reply(call, del_reply);
899 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
901 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
902 struct ldap_Result *modifydn;
903 struct ldapsrv_reply *modifydn_r;
904 TALLOC_CTX *local_ctx;
905 struct ldb_context *samdb = call->conn->ldb;
906 struct ldb_dn *olddn, *newdn=NULL, *newrdn;
907 struct ldb_dn *parentdn = NULL;
908 const char *errstr = NULL;
909 int result = LDAP_SUCCESS;
911 struct ldb_result *res = NULL;
913 DEBUG(10, ("ModifyDNRequest"));
914 DEBUGADD(10, (" dn: %s", req->dn));
915 DEBUGADD(10, (" newrdn: %s", req->newrdn));
917 local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
918 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
920 olddn = ldb_dn_new(local_ctx, samdb, req->dn);
921 VALID_DN_SYNTAX(olddn);
923 newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
924 VALID_DN_SYNTAX(newrdn);
926 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
927 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
929 if (ldb_dn_get_comp_num(newrdn) != 1) {
930 result = LDAP_INVALID_DN_SYNTAX;
931 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, &errstr);
935 /* we can't handle the rename if we should not remove the old dn */
936 if (!req->deleteolddn) {
937 result = LDAP_UNWILLING_TO_PERFORM;
938 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, &errstr);
939 errstr = talloc_asprintf(local_ctx,
940 "%s. Old RDN must be deleted", errstr);
944 if (req->newsuperior) {
945 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
946 VALID_DN_SYNTAX(parentdn);
947 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
949 if (ldb_dn_get_comp_num(parentdn) < 1) {
950 result = LDAP_AFFECTS_MULTIPLE_DSAS;
951 map_ldb_error(local_ctx, LDB_ERR_AFFECTS_MULTIPLE_DSAS,
953 errstr = talloc_asprintf(local_ctx,
954 "%s. Error new Superior DN invalid", errstr);
960 parentdn = ldb_dn_get_parent(local_ctx, olddn);
961 NT_STATUS_HAVE_NO_MEMORY(parentdn);
964 if ( ! ldb_dn_add_child(parentdn, newrdn)) {
971 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
972 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
974 if (result == LDAP_SUCCESS) {
975 res = talloc_zero(local_ctx, struct ldb_result);
976 NT_STATUS_HAVE_NO_MEMORY(res);
977 ldb_ret = ldb_rename_with_context(samdb, olddn, newdn, res);
978 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
981 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
983 if (res->refs != NULL) {
984 modifydn->resultcode = map_ldb_error(local_ctx, LDB_ERR_REFERRAL, &errstr);;
985 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
986 modifydn->referral = talloc_strdup(call, *res->refs);
988 modifydn->resultcode = result;
989 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
990 modifydn->referral = NULL;
993 talloc_free(local_ctx);
995 ldapsrv_queue_reply(call, modifydn_r);
999 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
1001 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
1002 struct ldap_Result *compare;
1003 struct ldapsrv_reply *compare_r;
1004 TALLOC_CTX *local_ctx;
1005 struct ldb_context *samdb = call->conn->ldb;
1006 struct ldb_result *res = NULL;
1008 const char *attrs[1];
1009 const char *errstr = NULL;
1010 const char *filter = NULL;
1011 int result = LDAP_SUCCESS;
1014 DEBUG(10, ("CompareRequest"));
1015 DEBUGADD(10, (" dn: %s", req->dn));
1017 local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
1018 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1020 dn = ldb_dn_new(local_ctx, samdb, req->dn);
1021 VALID_DN_SYNTAX(dn);
1023 DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
1024 filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute,
1025 (int)req->value.length, req->value.data);
1026 NT_STATUS_HAVE_NO_MEMORY(filter);
1028 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
1033 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
1034 NT_STATUS_HAVE_NO_MEMORY(compare_r);
1036 if (result == LDAP_SUCCESS) {
1037 ldb_ret = ldb_search(samdb, local_ctx, &res,
1038 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
1039 if (ldb_ret != LDB_SUCCESS) {
1040 result = map_ldb_error(local_ctx, ldb_ret, &errstr);
1041 DEBUG(10,("CompareRequest: error: %s\n", errstr));
1042 } else if (res->count == 0) {
1043 DEBUG(10,("CompareRequest: doesn't matched\n"));
1044 result = LDAP_COMPARE_FALSE;
1046 } else if (res->count == 1) {
1047 DEBUG(10,("CompareRequest: matched\n"));
1048 result = LDAP_COMPARE_TRUE;
1050 } else if (res->count > 1) {
1051 result = LDAP_OTHER;
1052 map_ldb_error(local_ctx, LDB_ERR_OTHER, &errstr);
1053 errstr = talloc_asprintf(local_ctx,
1054 "%s. Too many objects match!", errstr);
1055 DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
1059 compare = &compare_r->msg->r.CompareResponse;
1061 compare->resultcode = result;
1062 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
1063 compare->referral = NULL;
1065 talloc_free(local_ctx);
1067 ldapsrv_queue_reply(call, compare_r);
1068 return NT_STATUS_OK;
1071 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
1073 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
1074 DEBUG(10, ("AbandonRequest\n"));
1075 return NT_STATUS_OK;
1078 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
1081 struct ldap_message *msg = call->request;
1082 /* Check for undecoded critical extensions */
1083 for (i=0; msg->controls && msg->controls[i]; i++) {
1084 if (!msg->controls_decoded[i] &&
1085 msg->controls[i]->critical) {
1086 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1087 msg->controls[i]->oid));
1088 return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
1092 switch(call->request->type) {
1093 case LDAP_TAG_BindRequest:
1094 return ldapsrv_BindRequest(call);
1095 case LDAP_TAG_UnbindRequest:
1096 return ldapsrv_UnbindRequest(call);
1097 case LDAP_TAG_SearchRequest:
1098 return ldapsrv_SearchRequest(call);
1099 case LDAP_TAG_ModifyRequest:
1100 return ldapsrv_ModifyRequest(call);
1101 case LDAP_TAG_AddRequest:
1102 return ldapsrv_AddRequest(call);
1103 case LDAP_TAG_DelRequest:
1104 return ldapsrv_DelRequest(call);
1105 case LDAP_TAG_ModifyDNRequest:
1106 return ldapsrv_ModifyDNRequest(call);
1107 case LDAP_TAG_CompareRequest:
1108 return ldapsrv_CompareRequest(call);
1109 case LDAP_TAG_AbandonRequest:
1110 return ldapsrv_AbandonRequest(call);
1111 case LDAP_TAG_ExtendedRequest:
1112 return ldapsrv_ExtendedRequest(call);
1114 return ldapsrv_unwilling(call, 2);