lib: Convert [up|low]case.dat to C
[Samba.git] / source4 / ldap_server / ldap_backend.c
blobfd2b57936c5d6bd872b286246506662bd3d17de1
1 /*
2 Unix SMB/CIFS implementation.
3 LDAP server
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/>.
21 #include "includes.h"
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 "auth/gensec/gensec_internal.h" /* TODO: remove this */
27 #include "param/param.h"
28 #include "smbd/service_stream.h"
29 #include "dsdb/samdb/samdb.h"
30 #include <ldb_errors.h>
31 #include <ldb_module.h>
32 #include "ldb_wrap.h"
34 static int map_ldb_error(TALLOC_CTX *mem_ctx, int ldb_err,
35 const char *add_err_string, const char **errstring)
37 WERROR err;
39 /* Certain LDB modules need to return very special WERROR codes. Proof
40 * for them here and if they exist skip the rest of the mapping. */
41 if (add_err_string != NULL) {
42 char *endptr;
43 strtol(add_err_string, &endptr, 16);
44 if (endptr != add_err_string) {
45 *errstring = add_err_string;
46 return ldb_err;
50 /* Otherwise we calculate here a generic, but appropriate WERROR. */
52 switch (ldb_err) {
53 case LDB_SUCCESS:
54 err = WERR_OK;
55 break;
56 case LDB_ERR_OPERATIONS_ERROR:
57 err = WERR_DS_OPERATIONS_ERROR;
58 break;
59 case LDB_ERR_PROTOCOL_ERROR:
60 err = WERR_DS_PROTOCOL_ERROR;
61 break;
62 case LDB_ERR_TIME_LIMIT_EXCEEDED:
63 err = WERR_DS_TIMELIMIT_EXCEEDED;
64 break;
65 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
66 err = WERR_DS_SIZELIMIT_EXCEEDED;
67 break;
68 case LDB_ERR_COMPARE_FALSE:
69 err = WERR_DS_COMPARE_FALSE;
70 break;
71 case LDB_ERR_COMPARE_TRUE:
72 err = WERR_DS_COMPARE_TRUE;
73 break;
74 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
75 err = WERR_DS_AUTH_METHOD_NOT_SUPPORTED;
76 break;
77 case LDB_ERR_STRONG_AUTH_REQUIRED:
78 err = WERR_DS_STRONG_AUTH_REQUIRED;
79 break;
80 case LDB_ERR_REFERRAL:
81 err = WERR_DS_REFERRAL;
82 break;
83 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
84 err = WERR_DS_ADMIN_LIMIT_EXCEEDED;
85 break;
86 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
87 err = WERR_DS_UNAVAILABLE_CRIT_EXTENSION;
88 break;
89 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
90 err = WERR_DS_CONFIDENTIALITY_REQUIRED;
91 break;
92 case LDB_ERR_SASL_BIND_IN_PROGRESS:
93 err = WERR_DS_BUSY;
94 break;
95 case LDB_ERR_NO_SUCH_ATTRIBUTE:
96 err = WERR_DS_NO_ATTRIBUTE_OR_VALUE;
97 break;
98 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
99 err = WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
100 break;
101 case LDB_ERR_INAPPROPRIATE_MATCHING:
102 err = WERR_DS_INAPPROPRIATE_MATCHING;
103 break;
104 case LDB_ERR_CONSTRAINT_VIOLATION:
105 err = WERR_DS_CONSTRAINT_VIOLATION;
106 break;
107 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
108 err = WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS;
109 break;
110 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
111 err = WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
112 break;
113 case LDB_ERR_NO_SUCH_OBJECT:
114 err = WERR_DS_NO_SUCH_OBJECT;
115 break;
116 case LDB_ERR_ALIAS_PROBLEM:
117 err = WERR_DS_ALIAS_PROBLEM;
118 break;
119 case LDB_ERR_INVALID_DN_SYNTAX:
120 err = WERR_DS_INVALID_DN_SYNTAX;
121 break;
122 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
123 err = WERR_DS_ALIAS_DEREF_PROBLEM;
124 break;
125 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
126 err = WERR_DS_INAPPROPRIATE_AUTH;
127 break;
128 case LDB_ERR_INVALID_CREDENTIALS:
129 err = WERR_ACCESS_DENIED;
130 break;
131 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
132 err = WERR_DS_INSUFF_ACCESS_RIGHTS;
133 break;
134 case LDB_ERR_BUSY:
135 err = WERR_DS_BUSY;
136 break;
137 case LDB_ERR_UNAVAILABLE:
138 err = WERR_DS_UNAVAILABLE;
139 break;
140 case LDB_ERR_UNWILLING_TO_PERFORM:
141 err = WERR_DS_UNWILLING_TO_PERFORM;
142 break;
143 case LDB_ERR_LOOP_DETECT:
144 err = WERR_DS_LOOP_DETECT;
145 break;
146 case LDB_ERR_NAMING_VIOLATION:
147 err = WERR_DS_NAMING_VIOLATION;
148 break;
149 case LDB_ERR_OBJECT_CLASS_VIOLATION:
150 err = WERR_DS_OBJ_CLASS_VIOLATION;
151 break;
152 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
153 err = WERR_DS_CANT_ON_NON_LEAF;
154 break;
155 case LDB_ERR_NOT_ALLOWED_ON_RDN:
156 err = WERR_DS_CANT_ON_RDN;
157 break;
158 case LDB_ERR_ENTRY_ALREADY_EXISTS:
159 err = WERR_DS_OBJ_STRING_NAME_EXISTS;
160 break;
161 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
162 err = WERR_DS_CANT_MOD_OBJ_CLASS;
163 break;
164 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
165 err = WERR_DS_AFFECTS_MULTIPLE_DSAS;
166 break;
167 default:
168 err = WERR_DS_GENERIC_ERROR;
169 break;
172 *errstring = talloc_asprintf(mem_ctx, "%08X: %s", W_ERROR_V(err),
173 add_err_string != NULL ? add_err_string : ldb_strerror(ldb_err));
175 /* result is 1:1 for now */
176 return ldb_err;
180 connect to the sam database
182 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn)
184 conn->ldb = samdb_connect(conn,
185 conn->connection->event.ctx,
186 conn->lp_ctx,
187 conn->session_info,
188 conn->global_catalog ? LDB_FLG_RDONLY : 0);
189 if (conn->ldb == NULL) {
190 return NT_STATUS_INTERNAL_DB_CORRUPTION;
193 if (conn->server_credentials) {
194 char **sasl_mechs = NULL;
195 const struct gensec_security_ops * const *backends = gensec_security_all();
196 const struct gensec_security_ops **ops
197 = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
198 unsigned int i, j = 0;
199 for (i = 0; ops && ops[i]; i++) {
200 if (!lpcfg_parm_bool(conn->lp_ctx, NULL, "gensec", ops[i]->name, ops[i]->enabled))
201 continue;
203 if (ops[i]->sasl_name && ops[i]->server_start) {
204 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
206 if (!sasl_name) {
207 return NT_STATUS_NO_MEMORY;
209 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
210 if (!sasl_mechs) {
211 return NT_STATUS_NO_MEMORY;
213 sasl_mechs[j] = sasl_name;
214 talloc_steal(sasl_mechs, sasl_name);
215 sasl_mechs[j+1] = NULL;
216 j++;
219 talloc_unlink(conn, ops);
221 /* ldb can have a different lifetime to conn, so we
222 need to ensure that sasl_mechs lives as long as the
223 ldb does */
224 talloc_steal(conn->ldb, sasl_mechs);
226 ldb_set_opaque(conn->ldb, "supportedSASLMechanisms", sasl_mechs);
229 ldb_set_opaque(conn->ldb, "remoteAddress",
230 conn->connection->remote_address);
232 return NT_STATUS_OK;
235 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
237 struct ldapsrv_reply *reply;
239 reply = talloc(call, struct ldapsrv_reply);
240 if (!reply) {
241 return NULL;
243 reply->msg = talloc(reply, struct ldap_message);
244 if (reply->msg == NULL) {
245 talloc_free(reply);
246 return NULL;
249 reply->msg->messageid = call->request->messageid;
250 reply->msg->type = type;
251 reply->msg->controls = NULL;
253 return reply;
256 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
258 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
261 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
263 struct ldapsrv_reply *reply;
264 struct ldap_ExtendedResponse *r;
266 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
268 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
269 if (!reply) {
270 return NT_STATUS_NO_MEMORY;
273 r = &reply->msg->r.ExtendedResponse;
274 r->response.resultcode = error;
275 r->response.dn = NULL;
276 r->response.errormessage = NULL;
277 r->response.referral = NULL;
278 r->oid = NULL;
279 r->value = NULL;
281 ldapsrv_queue_reply(call, reply);
282 return NT_STATUS_OK;
285 static int ldapsrv_add_with_controls(struct ldapsrv_call *call,
286 const struct ldb_message *message,
287 struct ldb_control **controls,
288 struct ldb_result *res)
290 struct ldb_context *ldb = call->conn->ldb;
291 struct ldb_request *req;
292 int ret;
294 ret = ldb_msg_sanity_check(ldb, message);
295 if (ret != LDB_SUCCESS) {
296 return ret;
299 ret = ldb_build_add_req(&req, ldb, ldb,
300 message,
301 controls,
302 res,
303 ldb_modify_default_callback,
304 NULL);
306 if (ret != LDB_SUCCESS) return ret;
308 if (call->conn->global_catalog) {
309 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
311 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
313 ret = ldb_transaction_start(ldb);
314 if (ret != LDB_SUCCESS) {
315 return ret;
318 if (!call->conn->is_privileged) {
319 ldb_req_mark_untrusted(req);
322 LDB_REQ_SET_LOCATION(req);
324 ret = ldb_request(ldb, req);
325 if (ret == LDB_SUCCESS) {
326 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
329 if (ret == LDB_SUCCESS) {
330 ret = ldb_transaction_commit(ldb);
332 else {
333 ldb_transaction_cancel(ldb);
336 talloc_free(req);
337 return ret;
340 /* create and execute a modify request */
341 static int ldapsrv_mod_with_controls(struct ldapsrv_call *call,
342 const struct ldb_message *message,
343 struct ldb_control **controls,
344 struct ldb_result *res)
346 struct ldb_context *ldb = call->conn->ldb;
347 struct ldb_request *req;
348 int ret;
350 ret = ldb_msg_sanity_check(ldb, message);
351 if (ret != LDB_SUCCESS) {
352 return ret;
355 ret = ldb_build_mod_req(&req, ldb, ldb,
356 message,
357 controls,
358 res,
359 ldb_modify_default_callback,
360 NULL);
362 if (ret != LDB_SUCCESS) {
363 return ret;
366 if (call->conn->global_catalog) {
367 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
369 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
371 ret = ldb_transaction_start(ldb);
372 if (ret != LDB_SUCCESS) {
373 return ret;
376 if (!call->conn->is_privileged) {
377 ldb_req_mark_untrusted(req);
380 LDB_REQ_SET_LOCATION(req);
382 ret = ldb_request(ldb, req);
383 if (ret == LDB_SUCCESS) {
384 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
387 if (ret == LDB_SUCCESS) {
388 ret = ldb_transaction_commit(ldb);
390 else {
391 ldb_transaction_cancel(ldb);
394 talloc_free(req);
395 return ret;
398 /* create and execute a delete request */
399 static int ldapsrv_del_with_controls(struct ldapsrv_call *call,
400 struct ldb_dn *dn,
401 struct ldb_control **controls,
402 struct ldb_result *res)
404 struct ldb_context *ldb = call->conn->ldb;
405 struct ldb_request *req;
406 int ret;
408 ret = ldb_build_del_req(&req, ldb, ldb,
410 controls,
411 res,
412 ldb_modify_default_callback,
413 NULL);
415 if (ret != LDB_SUCCESS) return ret;
417 if (call->conn->global_catalog) {
418 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
420 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
422 ret = ldb_transaction_start(ldb);
423 if (ret != LDB_SUCCESS) {
424 return ret;
427 if (!call->conn->is_privileged) {
428 ldb_req_mark_untrusted(req);
431 LDB_REQ_SET_LOCATION(req);
433 ret = ldb_request(ldb, req);
434 if (ret == LDB_SUCCESS) {
435 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
438 if (ret == LDB_SUCCESS) {
439 ret = ldb_transaction_commit(ldb);
441 else {
442 ldb_transaction_cancel(ldb);
445 talloc_free(req);
446 return ret;
449 static int ldapsrv_rename_with_controls(struct ldapsrv_call *call,
450 struct ldb_dn *olddn,
451 struct ldb_dn *newdn,
452 struct ldb_control **controls,
453 struct ldb_result *res)
455 struct ldb_context *ldb = call->conn->ldb;
456 struct ldb_request *req;
457 int ret;
459 ret = ldb_build_rename_req(&req, ldb, ldb,
460 olddn,
461 newdn,
462 controls,
463 res,
464 ldb_modify_default_callback,
465 NULL);
467 if (ret != LDB_SUCCESS) return ret;
469 if (call->conn->global_catalog) {
470 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
472 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
474 ret = ldb_transaction_start(ldb);
475 if (ret != LDB_SUCCESS) {
476 return ret;
479 if (!call->conn->is_privileged) {
480 ldb_req_mark_untrusted(req);
483 LDB_REQ_SET_LOCATION(req);
485 ret = ldb_request(ldb, req);
486 if (ret == LDB_SUCCESS) {
487 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
490 if (ret == LDB_SUCCESS) {
491 ret = ldb_transaction_commit(ldb);
493 else {
494 ldb_transaction_cancel(ldb);
497 talloc_free(req);
498 return ret;
501 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
503 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
504 struct ldap_SearchResEntry *ent;
505 struct ldap_Result *done;
506 struct ldapsrv_reply *ent_r, *done_r;
507 TALLOC_CTX *local_ctx;
508 struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
509 struct ldb_dn *basedn;
510 struct ldb_result *res = NULL;
511 struct ldb_request *lreq;
512 struct ldb_control *search_control;
513 struct ldb_search_options_control *search_options;
514 struct ldb_control *extended_dn_control;
515 struct ldb_extended_dn_control *extended_dn_decoded = NULL;
516 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
517 const char **attrs = NULL;
518 const char *scope_str, *errstr = NULL;
519 int success_limit = 1;
520 int result = -1;
521 int ldb_ret = -1;
522 unsigned int i, j;
523 int extended_type = 1;
525 DEBUG(10, ("SearchRequest"));
526 DEBUGADD(10, (" basedn: %s", req->basedn));
527 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
529 local_ctx = talloc_new(call);
530 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
532 basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
533 NT_STATUS_HAVE_NO_MEMORY(basedn);
535 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
536 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
538 switch (req->scope) {
539 case LDAP_SEARCH_SCOPE_BASE:
540 scope_str = "BASE";
541 scope = LDB_SCOPE_BASE;
542 success_limit = 0;
543 break;
544 case LDAP_SEARCH_SCOPE_SINGLE:
545 scope_str = "ONE";
546 scope = LDB_SCOPE_ONELEVEL;
547 success_limit = 0;
548 break;
549 case LDAP_SEARCH_SCOPE_SUB:
550 scope_str = "SUB";
551 scope = LDB_SCOPE_SUBTREE;
552 success_limit = 0;
553 break;
554 default:
555 result = LDAP_PROTOCOL_ERROR;
556 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
557 &errstr);
558 errstr = talloc_asprintf(local_ctx,
559 "%s. Invalid scope", errstr);
560 goto reply;
562 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
564 if (req->num_attributes >= 1) {
565 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
566 NT_STATUS_HAVE_NO_MEMORY(attrs);
568 for (i=0; i < req->num_attributes; i++) {
569 DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
570 attrs[i] = req->attributes[i];
572 attrs[i] = NULL;
575 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
576 scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
578 res = talloc_zero(local_ctx, struct ldb_result);
579 NT_STATUS_HAVE_NO_MEMORY(res);
581 ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
582 basedn, scope,
583 req->tree, attrs,
584 call->request->controls,
585 res, ldb_search_default_callback,
586 NULL);
588 if (ldb_ret != LDB_SUCCESS) {
589 goto reply;
592 if (call->conn->global_catalog) {
593 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
595 search_options = NULL;
596 if (search_control) {
597 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
598 search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
599 } else {
600 search_options = talloc(lreq, struct ldb_search_options_control);
601 NT_STATUS_HAVE_NO_MEMORY(search_options);
602 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
603 ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
605 } else {
606 ldb_request_add_control(lreq, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
609 extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
611 if (extended_dn_control) {
612 if (extended_dn_control->data) {
613 extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
614 extended_type = extended_dn_decoded->type;
615 } else {
616 extended_type = 0;
620 ldb_set_timeout(samdb, lreq, req->timelimit);
622 if (!call->conn->is_privileged) {
623 ldb_req_mark_untrusted(lreq);
626 LDB_REQ_SET_LOCATION(lreq);
628 ldb_ret = ldb_request(samdb, lreq);
630 if (ldb_ret != LDB_SUCCESS) {
631 goto reply;
634 ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
636 if (ldb_ret == LDB_SUCCESS) {
637 for (i = 0; i < res->count; i++) {
638 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
639 NT_STATUS_HAVE_NO_MEMORY(ent_r);
641 /* Better to have the whole message kept here,
642 * than to find someone further up didn't put
643 * a value in the right spot in the talloc tree */
644 talloc_steal(ent_r, res->msgs[i]);
646 ent = &ent_r->msg->r.SearchResultEntry;
647 ent->dn = ldb_dn_get_extended_linearized(ent_r, res->msgs[i]->dn, extended_type);
648 ent->num_attributes = 0;
649 ent->attributes = NULL;
650 if (res->msgs[i]->num_elements == 0) {
651 goto queue_reply;
653 ent->num_attributes = res->msgs[i]->num_elements;
654 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
655 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
656 for (j=0; j < ent->num_attributes; j++) {
657 ent->attributes[j].name = res->msgs[i]->elements[j].name;
658 ent->attributes[j].num_values = 0;
659 ent->attributes[j].values = NULL;
660 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
661 continue;
663 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
664 ent->attributes[j].values = res->msgs[i]->elements[j].values;
666 queue_reply:
667 ldapsrv_queue_reply(call, ent_r);
670 /* Send back referrals if they do exist (search operations) */
671 if (res->refs != NULL) {
672 char **ref;
673 struct ldap_SearchResRef *ent_ref;
675 for (ref = res->refs; *ref != NULL; ++ref) {
676 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
677 NT_STATUS_HAVE_NO_MEMORY(ent_r);
679 /* Better to have the whole referrals kept here,
680 * than to find someone further up didn't put
681 * a value in the right spot in the talloc tree
683 talloc_steal(ent_r, *ref);
685 ent_ref = &ent_r->msg->r.SearchResultReference;
686 ent_ref->referral = *ref;
688 ldapsrv_queue_reply(call, ent_r);
693 reply:
694 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
695 NT_STATUS_HAVE_NO_MEMORY(done_r);
697 done = &done_r->msg->r.SearchResultDone;
698 done->dn = NULL;
699 done->referral = NULL;
701 if (result != -1) {
702 } else if (ldb_ret == LDB_SUCCESS) {
703 if (res->count >= success_limit) {
704 DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
705 result = LDAP_SUCCESS;
706 errstr = NULL;
708 if (res->controls) {
709 done_r->msg->controls = res->controls;
710 talloc_steal(done_r, res->controls);
712 } else {
713 DEBUG(10,("SearchRequest: error\n"));
714 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
715 &errstr);
718 done->resultcode = result;
719 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
721 talloc_free(local_ctx);
723 ldapsrv_queue_reply(call, done_r);
724 return NT_STATUS_OK;
727 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
729 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
730 struct ldap_Result *modify_result;
731 struct ldapsrv_reply *modify_reply;
732 TALLOC_CTX *local_ctx;
733 struct ldb_context *samdb = call->conn->ldb;
734 struct ldb_message *msg = NULL;
735 struct ldb_dn *dn;
736 const char *errstr = NULL;
737 int result = LDAP_SUCCESS;
738 int ldb_ret;
739 unsigned int i,j;
740 struct ldb_result *res = NULL;
742 DEBUG(10, ("ModifyRequest"));
743 DEBUGADD(10, (" dn: %s\n", req->dn));
745 local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
746 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
748 dn = ldb_dn_new(local_ctx, samdb, req->dn);
749 NT_STATUS_HAVE_NO_MEMORY(dn);
751 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
753 msg = talloc(local_ctx, struct ldb_message);
754 NT_STATUS_HAVE_NO_MEMORY(msg);
756 msg->dn = dn;
757 msg->num_elements = 0;
758 msg->elements = NULL;
760 if (req->num_mods > 0) {
761 msg->num_elements = req->num_mods;
762 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
763 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
765 for (i=0; i < msg->num_elements; i++) {
766 msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
767 msg->elements[i].num_values = 0;
768 msg->elements[i].values = NULL;
770 switch (req->mods[i].type) {
771 default:
772 result = LDAP_PROTOCOL_ERROR;
773 map_ldb_error(local_ctx,
774 LDB_ERR_PROTOCOL_ERROR, NULL, &errstr);
775 errstr = talloc_asprintf(local_ctx,
776 "%s. Invalid LDAP_MODIFY_* type", errstr);
777 goto reply;
778 case LDAP_MODIFY_ADD:
779 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
780 break;
781 case LDAP_MODIFY_DELETE:
782 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
783 break;
784 case LDAP_MODIFY_REPLACE:
785 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
786 break;
789 msg->elements[i].num_values = req->mods[i].attrib.num_values;
790 if (msg->elements[i].num_values > 0) {
791 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
792 msg->elements[i].num_values);
793 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
795 for (j=0; j < msg->elements[i].num_values; j++) {
796 msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
797 msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;
803 reply:
804 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
805 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
807 if (result == LDAP_SUCCESS) {
808 res = talloc_zero(local_ctx, struct ldb_result);
809 NT_STATUS_HAVE_NO_MEMORY(res);
810 ldb_ret = ldapsrv_mod_with_controls(call, msg, call->request->controls, res);
811 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
812 &errstr);
815 modify_result = &modify_reply->msg->r.ModifyResponse;
816 modify_result->dn = NULL;
817 if ((res != NULL) && (res->refs != NULL)) {
818 modify_result->resultcode = map_ldb_error(local_ctx,
819 LDB_ERR_REFERRAL,
820 NULL, &errstr);
821 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
822 modify_result->referral = talloc_strdup(call, *res->refs);
823 } else {
824 modify_result->resultcode = result;
825 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
826 modify_result->referral = NULL;
828 talloc_free(local_ctx);
830 ldapsrv_queue_reply(call, modify_reply);
831 return NT_STATUS_OK;
835 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
837 struct ldap_AddRequest *req = &call->request->r.AddRequest;
838 struct ldap_Result *add_result;
839 struct ldapsrv_reply *add_reply;
840 TALLOC_CTX *local_ctx;
841 struct ldb_context *samdb = call->conn->ldb;
842 struct ldb_message *msg = NULL;
843 struct ldb_dn *dn;
844 const char *errstr = NULL;
845 int result = LDAP_SUCCESS;
846 int ldb_ret;
847 unsigned int i,j;
848 struct ldb_result *res = NULL;
850 DEBUG(10, ("AddRequest"));
851 DEBUGADD(10, (" dn: %s\n", req->dn));
853 local_ctx = talloc_named(call, 0, "AddRequest local memory context");
854 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
856 dn = ldb_dn_new(local_ctx, samdb, req->dn);
857 NT_STATUS_HAVE_NO_MEMORY(dn);
859 DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
861 msg = talloc(local_ctx, struct ldb_message);
862 NT_STATUS_HAVE_NO_MEMORY(msg);
864 msg->dn = dn;
865 msg->num_elements = 0;
866 msg->elements = NULL;
868 if (req->num_attributes > 0) {
869 msg->num_elements = req->num_attributes;
870 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
871 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
873 for (i=0; i < msg->num_elements; i++) {
874 msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
875 msg->elements[i].flags = 0;
876 msg->elements[i].num_values = 0;
877 msg->elements[i].values = NULL;
879 if (req->attributes[i].num_values > 0) {
880 msg->elements[i].num_values = req->attributes[i].num_values;
881 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
882 msg->elements[i].num_values);
883 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
885 for (j=0; j < msg->elements[i].num_values; j++) {
886 msg->elements[i].values[j].length = req->attributes[i].values[j].length;
887 msg->elements[i].values[j].data = req->attributes[i].values[j].data;
893 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
894 NT_STATUS_HAVE_NO_MEMORY(add_reply);
896 if (result == LDAP_SUCCESS) {
897 res = talloc_zero(local_ctx, struct ldb_result);
898 NT_STATUS_HAVE_NO_MEMORY(res);
899 ldb_ret = ldapsrv_add_with_controls(call, msg, call->request->controls, res);
900 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
901 &errstr);
904 add_result = &add_reply->msg->r.AddResponse;
905 add_result->dn = NULL;
906 if ((res != NULL) && (res->refs != NULL)) {
907 add_result->resultcode = map_ldb_error(local_ctx,
908 LDB_ERR_REFERRAL, NULL,
909 &errstr);
910 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
911 add_result->referral = talloc_strdup(call, *res->refs);
912 } else {
913 add_result->resultcode = result;
914 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
915 add_result->referral = NULL;
917 talloc_free(local_ctx);
919 ldapsrv_queue_reply(call, add_reply);
920 return NT_STATUS_OK;
924 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
926 struct ldap_DelRequest *req = &call->request->r.DelRequest;
927 struct ldap_Result *del_result;
928 struct ldapsrv_reply *del_reply;
929 TALLOC_CTX *local_ctx;
930 struct ldb_context *samdb = call->conn->ldb;
931 struct ldb_dn *dn;
932 const char *errstr = NULL;
933 int result = LDAP_SUCCESS;
934 int ldb_ret;
935 struct ldb_result *res = NULL;
937 DEBUG(10, ("DelRequest"));
938 DEBUGADD(10, (" dn: %s\n", req->dn));
940 local_ctx = talloc_named(call, 0, "DelRequest local memory context");
941 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
943 dn = ldb_dn_new(local_ctx, samdb, req->dn);
944 NT_STATUS_HAVE_NO_MEMORY(dn);
946 DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
948 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
949 NT_STATUS_HAVE_NO_MEMORY(del_reply);
951 if (result == LDAP_SUCCESS) {
952 res = talloc_zero(local_ctx, struct ldb_result);
953 NT_STATUS_HAVE_NO_MEMORY(res);
954 ldb_ret = ldapsrv_del_with_controls(call, dn, call->request->controls, res);
955 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
956 &errstr);
959 del_result = &del_reply->msg->r.DelResponse;
960 del_result->dn = NULL;
961 if ((res != NULL) && (res->refs != NULL)) {
962 del_result->resultcode = map_ldb_error(local_ctx,
963 LDB_ERR_REFERRAL, NULL,
964 &errstr);
965 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
966 del_result->referral = talloc_strdup(call, *res->refs);
967 } else {
968 del_result->resultcode = result;
969 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
970 del_result->referral = NULL;
973 talloc_free(local_ctx);
975 ldapsrv_queue_reply(call, del_reply);
976 return NT_STATUS_OK;
979 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
981 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
982 struct ldap_Result *modifydn;
983 struct ldapsrv_reply *modifydn_r;
984 TALLOC_CTX *local_ctx;
985 struct ldb_context *samdb = call->conn->ldb;
986 struct ldb_dn *olddn, *newdn=NULL, *newrdn;
987 struct ldb_dn *parentdn = NULL;
988 const char *errstr = NULL;
989 int result = LDAP_SUCCESS;
990 int ldb_ret;
991 struct ldb_result *res = NULL;
993 DEBUG(10, ("ModifyDNRequest"));
994 DEBUGADD(10, (" dn: %s", req->dn));
995 DEBUGADD(10, (" newrdn: %s\n", req->newrdn));
997 local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
998 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1000 olddn = ldb_dn_new(local_ctx, samdb, req->dn);
1001 NT_STATUS_HAVE_NO_MEMORY(olddn);
1003 newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
1004 NT_STATUS_HAVE_NO_MEMORY(newrdn);
1006 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
1007 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
1009 if (ldb_dn_get_comp_num(newrdn) == 0) {
1010 result = LDAP_PROTOCOL_ERROR;
1011 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
1012 &errstr);
1013 goto reply;
1016 if (ldb_dn_get_comp_num(newrdn) > 1) {
1017 result = LDAP_NAMING_VIOLATION;
1018 map_ldb_error(local_ctx, LDB_ERR_NAMING_VIOLATION, NULL,
1019 &errstr);
1020 goto reply;
1023 /* we can't handle the rename if we should not remove the old dn */
1024 if (!req->deleteolddn) {
1025 result = LDAP_UNWILLING_TO_PERFORM;
1026 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, NULL,
1027 &errstr);
1028 errstr = talloc_asprintf(local_ctx,
1029 "%s. Old RDN must be deleted", errstr);
1030 goto reply;
1033 if (req->newsuperior) {
1034 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
1035 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
1038 if (!parentdn) {
1039 parentdn = ldb_dn_get_parent(local_ctx, olddn);
1041 if (!parentdn) {
1042 result = LDAP_NO_SUCH_OBJECT;
1043 map_ldb_error(local_ctx, LDB_ERR_NO_SUCH_OBJECT, NULL, &errstr);
1044 goto reply;
1047 if ( ! ldb_dn_add_child(parentdn, newrdn)) {
1048 result = LDAP_OTHER;
1049 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1050 goto reply;
1052 newdn = parentdn;
1054 reply:
1055 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
1056 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
1058 if (result == LDAP_SUCCESS) {
1059 res = talloc_zero(local_ctx, struct ldb_result);
1060 NT_STATUS_HAVE_NO_MEMORY(res);
1061 ldb_ret = ldapsrv_rename_with_controls(call, olddn, newdn, call->request->controls, res);
1062 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1063 &errstr);
1066 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
1067 modifydn->dn = NULL;
1068 if ((res != NULL) && (res->refs != NULL)) {
1069 modifydn->resultcode = map_ldb_error(local_ctx,
1070 LDB_ERR_REFERRAL, NULL,
1071 &errstr);;
1072 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1073 modifydn->referral = talloc_strdup(call, *res->refs);
1074 } else {
1075 modifydn->resultcode = result;
1076 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1077 modifydn->referral = NULL;
1080 talloc_free(local_ctx);
1082 ldapsrv_queue_reply(call, modifydn_r);
1083 return NT_STATUS_OK;
1086 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
1088 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
1089 struct ldap_Result *compare;
1090 struct ldapsrv_reply *compare_r;
1091 TALLOC_CTX *local_ctx;
1092 struct ldb_context *samdb = call->conn->ldb;
1093 struct ldb_result *res = NULL;
1094 struct ldb_dn *dn;
1095 const char *attrs[1];
1096 const char *errstr = NULL;
1097 const char *filter = NULL;
1098 int result = LDAP_SUCCESS;
1099 int ldb_ret;
1101 DEBUG(10, ("CompareRequest"));
1102 DEBUGADD(10, (" dn: %s\n", req->dn));
1104 local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
1105 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1107 dn = ldb_dn_new(local_ctx, samdb, req->dn);
1108 NT_STATUS_HAVE_NO_MEMORY(dn);
1110 DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
1111 filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute,
1112 (int)req->value.length, req->value.data);
1113 NT_STATUS_HAVE_NO_MEMORY(filter);
1115 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
1117 attrs[0] = NULL;
1119 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
1120 NT_STATUS_HAVE_NO_MEMORY(compare_r);
1122 if (result == LDAP_SUCCESS) {
1123 ldb_ret = ldb_search(samdb, local_ctx, &res,
1124 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
1125 if (ldb_ret != LDB_SUCCESS) {
1126 result = map_ldb_error(local_ctx, ldb_ret,
1127 ldb_errstring(samdb), &errstr);
1128 DEBUG(10,("CompareRequest: error: %s\n", errstr));
1129 } else if (res->count == 0) {
1130 DEBUG(10,("CompareRequest: doesn't matched\n"));
1131 result = LDAP_COMPARE_FALSE;
1132 errstr = NULL;
1133 } else if (res->count == 1) {
1134 DEBUG(10,("CompareRequest: matched\n"));
1135 result = LDAP_COMPARE_TRUE;
1136 errstr = NULL;
1137 } else if (res->count > 1) {
1138 result = LDAP_OTHER;
1139 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1140 errstr = talloc_asprintf(local_ctx,
1141 "%s. Too many objects match!", errstr);
1142 DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
1146 compare = &compare_r->msg->r.CompareResponse;
1147 compare->dn = NULL;
1148 compare->resultcode = result;
1149 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
1150 compare->referral = NULL;
1152 talloc_free(local_ctx);
1154 ldapsrv_queue_reply(call, compare_r);
1155 return NT_STATUS_OK;
1158 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
1160 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
1161 DEBUG(10, ("AbandonRequest\n"));
1162 return NT_STATUS_OK;
1165 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
1167 unsigned int i;
1168 struct ldap_message *msg = call->request;
1169 struct ldb_context *samdb = call->conn->ldb;
1170 NTSTATUS status;
1171 time_t *lastts;
1172 /* Check for undecoded critical extensions */
1173 for (i=0; msg->controls && msg->controls[i]; i++) {
1174 if (!msg->controls_decoded[i] &&
1175 msg->controls[i]->critical) {
1176 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1177 msg->controls[i]->oid));
1178 return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
1182 switch(call->request->type) {
1183 case LDAP_TAG_BindRequest:
1184 return ldapsrv_BindRequest(call);
1185 case LDAP_TAG_UnbindRequest:
1186 return ldapsrv_UnbindRequest(call);
1187 case LDAP_TAG_SearchRequest:
1188 return ldapsrv_SearchRequest(call);
1189 case LDAP_TAG_ModifyRequest:
1190 status = ldapsrv_ModifyRequest(call);
1191 break;
1192 case LDAP_TAG_AddRequest:
1193 status = ldapsrv_AddRequest(call);
1194 break;
1195 case LDAP_TAG_DelRequest:
1196 return ldapsrv_DelRequest(call);
1197 case LDAP_TAG_ModifyDNRequest:
1198 return ldapsrv_ModifyDNRequest(call);
1199 case LDAP_TAG_CompareRequest:
1200 return ldapsrv_CompareRequest(call);
1201 case LDAP_TAG_AbandonRequest:
1202 return ldapsrv_AbandonRequest(call);
1203 case LDAP_TAG_ExtendedRequest:
1204 return ldapsrv_ExtendedRequest(call);
1205 default:
1206 return ldapsrv_unwilling(call, LDAP_PROTOCOL_ERROR);
1209 if (NT_STATUS_IS_OK(status)) {
1210 lastts = (time_t *)ldb_get_opaque(samdb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME);
1211 if (lastts && !*lastts) {
1212 DEBUG(10, ("Schema update now was requested, "
1213 "fullfilling the request ts = %d\n",
1214 (int)*lastts));
1216 * Just requesting the schema will do the trick
1217 * as the delay for reload is experied, we will have a reload
1218 * from the schema as expected as we are not yet in a transaction!
1220 dsdb_get_schema(samdb, NULL);
1221 *lastts = time(NULL);
1222 ldb_set_opaque(samdb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME, lastts);
1225 return status;