s3-net: pass down struct net_context to the dns update calls.
[Samba/id10ts.git] / source4 / ldap_server / ldap_backend.c
blobf415e9bf62a8c2dfebb6a2467ea611d6f8aefa67
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 "param/param.h"
27 #include "smbd/service_stream.h"
28 #include "dsdb/samdb/samdb.h"
29 #include <ldb_errors.h>
30 #include <ldb_module.h>
31 #include "ldb_wrap.h"
33 static int map_ldb_error(TALLOC_CTX *mem_ctx, int ldb_err,
34 const char *add_err_string, const char **errstring)
36 WERROR err;
38 /* Certain LDB modules need to return very special WERROR codes. Proof
39 * for them here and if they exist skip the rest of the mapping. */
40 if (add_err_string != NULL) {
41 char *endptr;
42 strtol(add_err_string, &endptr, 16);
43 if (endptr != add_err_string) {
44 *errstring = add_err_string;
45 return ldb_err;
49 /* Otherwise we calculate here a generic, but appropriate WERROR. */
51 switch (ldb_err) {
52 case LDB_SUCCESS:
53 err = WERR_OK;
54 break;
55 case LDB_ERR_OPERATIONS_ERROR:
56 err = WERR_DS_OPERATIONS_ERROR;
57 break;
58 case LDB_ERR_PROTOCOL_ERROR:
59 err = WERR_DS_PROTOCOL_ERROR;
60 break;
61 case LDB_ERR_TIME_LIMIT_EXCEEDED:
62 err = WERR_DS_TIMELIMIT_EXCEEDED;
63 break;
64 case LDB_ERR_SIZE_LIMIT_EXCEEDED:
65 err = WERR_DS_SIZELIMIT_EXCEEDED;
66 break;
67 case LDB_ERR_COMPARE_FALSE:
68 err = WERR_DS_COMPARE_FALSE;
69 break;
70 case LDB_ERR_COMPARE_TRUE:
71 err = WERR_DS_COMPARE_TRUE;
72 break;
73 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED:
74 err = WERR_DS_AUTH_METHOD_NOT_SUPPORTED;
75 break;
76 case LDB_ERR_STRONG_AUTH_REQUIRED:
77 err = WERR_DS_STRONG_AUTH_REQUIRED;
78 break;
79 case LDB_ERR_REFERRAL:
80 err = WERR_DS_REFERRAL;
81 break;
82 case LDB_ERR_ADMIN_LIMIT_EXCEEDED:
83 err = WERR_DS_ADMIN_LIMIT_EXCEEDED;
84 break;
85 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION:
86 err = WERR_DS_UNAVAILABLE_CRIT_EXTENSION;
87 break;
88 case LDB_ERR_CONFIDENTIALITY_REQUIRED:
89 err = WERR_DS_CONFIDENTIALITY_REQUIRED;
90 break;
91 case LDB_ERR_SASL_BIND_IN_PROGRESS:
92 err = WERR_DS_BUSY;
93 break;
94 case LDB_ERR_NO_SUCH_ATTRIBUTE:
95 err = WERR_DS_NO_ATTRIBUTE_OR_VALUE;
96 break;
97 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE:
98 err = WERR_DS_ATTRIBUTE_TYPE_UNDEFINED;
99 break;
100 case LDB_ERR_INAPPROPRIATE_MATCHING:
101 err = WERR_DS_INAPPROPRIATE_MATCHING;
102 break;
103 case LDB_ERR_CONSTRAINT_VIOLATION:
104 err = WERR_DS_CONSTRAINT_VIOLATION;
105 break;
106 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS:
107 err = WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS;
108 break;
109 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX:
110 err = WERR_DS_INVALID_ATTRIBUTE_SYNTAX;
111 break;
112 case LDB_ERR_NO_SUCH_OBJECT:
113 err = WERR_DS_NO_SUCH_OBJECT;
114 break;
115 case LDB_ERR_ALIAS_PROBLEM:
116 err = WERR_DS_ALIAS_PROBLEM;
117 break;
118 case LDB_ERR_INVALID_DN_SYNTAX:
119 err = WERR_DS_INVALID_DN_SYNTAX;
120 break;
121 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM:
122 err = WERR_DS_ALIAS_DEREF_PROBLEM;
123 break;
124 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION:
125 err = WERR_DS_INAPPROPRIATE_AUTH;
126 break;
127 case LDB_ERR_INVALID_CREDENTIALS:
128 err = WERR_ACCESS_DENIED;
129 break;
130 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS:
131 err = WERR_DS_INSUFF_ACCESS_RIGHTS;
132 break;
133 case LDB_ERR_BUSY:
134 err = WERR_DS_BUSY;
135 break;
136 case LDB_ERR_UNAVAILABLE:
137 err = WERR_DS_UNAVAILABLE;
138 break;
139 case LDB_ERR_UNWILLING_TO_PERFORM:
140 err = WERR_DS_UNWILLING_TO_PERFORM;
141 break;
142 case LDB_ERR_LOOP_DETECT:
143 err = WERR_DS_LOOP_DETECT;
144 break;
145 case LDB_ERR_NAMING_VIOLATION:
146 err = WERR_DS_NAMING_VIOLATION;
147 break;
148 case LDB_ERR_OBJECT_CLASS_VIOLATION:
149 err = WERR_DS_OBJ_CLASS_VIOLATION;
150 break;
151 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF:
152 err = WERR_DS_CANT_ON_NON_LEAF;
153 break;
154 case LDB_ERR_NOT_ALLOWED_ON_RDN:
155 err = WERR_DS_CANT_ON_RDN;
156 break;
157 case LDB_ERR_ENTRY_ALREADY_EXISTS:
158 err = WERR_DS_OBJ_STRING_NAME_EXISTS;
159 break;
160 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED:
161 err = WERR_DS_CANT_MOD_OBJ_CLASS;
162 break;
163 case LDB_ERR_AFFECTS_MULTIPLE_DSAS:
164 err = WERR_DS_AFFECTS_MULTIPLE_DSAS;
165 break;
166 default:
167 err = WERR_DS_GENERIC_ERROR;
168 break;
171 *errstring = talloc_asprintf(mem_ctx, "%08X: %s", W_ERROR_V(err),
172 add_err_string != NULL ? add_err_string : ldb_strerror(ldb_err));
174 /* result is 1:1 for now */
175 return ldb_err;
179 connect to the sam database
181 NTSTATUS ldapsrv_backend_Init(struct ldapsrv_connection *conn)
183 conn->ldb = samdb_connect(conn,
184 conn->connection->event.ctx,
185 conn->lp_ctx,
186 conn->session_info,
187 conn->global_catalog ? LDB_FLG_RDONLY : 0);
188 if (conn->ldb == NULL) {
189 return NT_STATUS_INTERNAL_DB_CORRUPTION;
192 if (conn->server_credentials) {
193 char **sasl_mechs = NULL;
194 struct gensec_security_ops **backends = gensec_security_all();
195 struct gensec_security_ops **ops
196 = gensec_use_kerberos_mechs(conn, backends, conn->server_credentials);
197 unsigned int i, j = 0;
198 for (i = 0; ops && ops[i]; i++) {
199 if (!lpcfg_parm_bool(conn->lp_ctx, NULL, "gensec", ops[i]->name, ops[i]->enabled))
200 continue;
202 if (ops[i]->sasl_name && ops[i]->server_start) {
203 char *sasl_name = talloc_strdup(conn, ops[i]->sasl_name);
205 if (!sasl_name) {
206 return NT_STATUS_NO_MEMORY;
208 sasl_mechs = talloc_realloc(conn, sasl_mechs, char *, j + 2);
209 if (!sasl_mechs) {
210 return NT_STATUS_NO_MEMORY;
212 sasl_mechs[j] = sasl_name;
213 talloc_steal(sasl_mechs, sasl_name);
214 sasl_mechs[j+1] = NULL;
215 j++;
218 talloc_unlink(conn, ops);
220 /* ldb can have a different lifetime to conn, so we
221 need to ensure that sasl_mechs lives as long as the
222 ldb does */
223 talloc_steal(conn->ldb, sasl_mechs);
225 ldb_set_opaque(conn->ldb, "supportedSASLMechanisms", sasl_mechs);
228 return NT_STATUS_OK;
231 struct ldapsrv_reply *ldapsrv_init_reply(struct ldapsrv_call *call, uint8_t type)
233 struct ldapsrv_reply *reply;
235 reply = talloc(call, struct ldapsrv_reply);
236 if (!reply) {
237 return NULL;
239 reply->msg = talloc(reply, struct ldap_message);
240 if (reply->msg == NULL) {
241 talloc_free(reply);
242 return NULL;
245 reply->msg->messageid = call->request->messageid;
246 reply->msg->type = type;
247 reply->msg->controls = NULL;
249 return reply;
252 void ldapsrv_queue_reply(struct ldapsrv_call *call, struct ldapsrv_reply *reply)
254 DLIST_ADD_END(call->replies, reply, struct ldapsrv_reply *);
257 static NTSTATUS ldapsrv_unwilling(struct ldapsrv_call *call, int error)
259 struct ldapsrv_reply *reply;
260 struct ldap_ExtendedResponse *r;
262 DEBUG(10,("Unwilling type[%d] id[%d]\n", call->request->type, call->request->messageid));
264 reply = ldapsrv_init_reply(call, LDAP_TAG_ExtendedResponse);
265 if (!reply) {
266 return NT_STATUS_NO_MEMORY;
269 r = &reply->msg->r.ExtendedResponse;
270 r->response.resultcode = error;
271 r->response.dn = NULL;
272 r->response.errormessage = NULL;
273 r->response.referral = NULL;
274 r->oid = NULL;
275 r->value = NULL;
277 ldapsrv_queue_reply(call, reply);
278 return NT_STATUS_OK;
281 static int ldapsrv_add_with_controls(struct ldapsrv_call *call,
282 const struct ldb_message *message,
283 struct ldb_control **controls,
284 void *context)
286 struct ldb_context *ldb = call->conn->ldb;
287 struct ldb_request *req;
288 int ret;
290 ret = ldb_msg_sanity_check(ldb, message);
291 if (ret != LDB_SUCCESS) {
292 return ret;
295 ret = ldb_build_add_req(&req, ldb, ldb,
296 message,
297 controls,
298 context,
299 ldb_modify_default_callback,
300 NULL);
302 if (ret != LDB_SUCCESS) return ret;
304 if (call->conn->global_catalog) {
305 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
307 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
309 ret = ldb_transaction_start(ldb);
310 if (ret != LDB_SUCCESS) {
311 return ret;
314 if (!call->conn->is_privileged) {
315 ldb_req_mark_untrusted(req);
318 LDB_REQ_SET_LOCATION(req);
320 ret = ldb_request(ldb, req);
321 if (ret == LDB_SUCCESS) {
322 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
325 if (ret == LDB_SUCCESS) {
326 ret = ldb_transaction_commit(ldb);
328 else {
329 ldb_transaction_cancel(ldb);
332 talloc_free(req);
333 return ret;
336 /* create and execute a modify request */
337 static int ldapsrv_mod_with_controls(struct ldapsrv_call *call,
338 const struct ldb_message *message,
339 struct ldb_control **controls,
340 void *context)
342 struct ldb_context *ldb = call->conn->ldb;
343 struct ldb_request *req;
344 int ret;
346 ret = ldb_msg_sanity_check(ldb, message);
347 if (ret != LDB_SUCCESS) {
348 return ret;
351 ret = ldb_build_mod_req(&req, ldb, ldb,
352 message,
353 controls,
354 context,
355 ldb_modify_default_callback,
356 NULL);
358 if (ret != LDB_SUCCESS) {
359 return ret;
362 if (call->conn->global_catalog) {
363 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
365 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
367 ret = ldb_transaction_start(ldb);
368 if (ret != LDB_SUCCESS) {
369 return ret;
372 if (!call->conn->is_privileged) {
373 ldb_req_mark_untrusted(req);
376 LDB_REQ_SET_LOCATION(req);
378 ret = ldb_request(ldb, req);
379 if (ret == LDB_SUCCESS) {
380 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
383 if (ret == LDB_SUCCESS) {
384 ret = ldb_transaction_commit(ldb);
386 else {
387 ldb_transaction_cancel(ldb);
390 talloc_free(req);
391 return ret;
394 /* create and execute a delete request */
395 static int ldapsrv_del_with_controls(struct ldapsrv_call *call,
396 struct ldb_dn *dn,
397 struct ldb_control **controls,
398 void *context)
400 struct ldb_context *ldb = call->conn->ldb;
401 struct ldb_request *req;
402 int ret;
404 ret = ldb_build_del_req(&req, ldb, ldb,
406 controls,
407 context,
408 ldb_modify_default_callback,
409 NULL);
411 if (ret != LDB_SUCCESS) return ret;
413 if (call->conn->global_catalog) {
414 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
416 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
418 ret = ldb_transaction_start(ldb);
419 if (ret != LDB_SUCCESS) {
420 return ret;
423 if (!call->conn->is_privileged) {
424 ldb_req_mark_untrusted(req);
427 LDB_REQ_SET_LOCATION(req);
429 ret = ldb_request(ldb, req);
430 if (ret == LDB_SUCCESS) {
431 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
434 if (ret == LDB_SUCCESS) {
435 ret = ldb_transaction_commit(ldb);
437 else {
438 ldb_transaction_cancel(ldb);
441 talloc_free(req);
442 return ret;
445 static int ldapsrv_rename_with_controls(struct ldapsrv_call *call,
446 struct ldb_dn *olddn,
447 struct ldb_dn *newdn,
448 struct ldb_control **controls,
449 void *context)
451 struct ldb_context *ldb = call->conn->ldb;
452 struct ldb_request *req;
453 int ret;
455 ret = ldb_build_rename_req(&req, ldb, ldb,
456 olddn,
457 newdn,
458 NULL,
459 context,
460 ldb_modify_default_callback,
461 NULL);
463 if (ret != LDB_SUCCESS) return ret;
465 if (call->conn->global_catalog) {
466 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM, "modify forbidden on global catalog port");
468 ldb_request_add_control(req, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
470 ret = ldb_transaction_start(ldb);
471 if (ret != LDB_SUCCESS) {
472 return ret;
475 if (!call->conn->is_privileged) {
476 ldb_req_mark_untrusted(req);
479 LDB_REQ_SET_LOCATION(req);
481 ret = ldb_request(ldb, req);
482 if (ret == LDB_SUCCESS) {
483 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
486 if (ret == LDB_SUCCESS) {
487 ret = ldb_transaction_commit(ldb);
489 else {
490 ldb_transaction_cancel(ldb);
493 talloc_free(req);
494 return ret;
497 static NTSTATUS ldapsrv_SearchRequest(struct ldapsrv_call *call)
499 struct ldap_SearchRequest *req = &call->request->r.SearchRequest;
500 struct ldap_SearchResEntry *ent;
501 struct ldap_Result *done;
502 struct ldapsrv_reply *ent_r, *done_r;
503 TALLOC_CTX *local_ctx;
504 struct ldb_context *samdb = talloc_get_type(call->conn->ldb, struct ldb_context);
505 struct ldb_dn *basedn;
506 struct ldb_result *res = NULL;
507 struct ldb_request *lreq;
508 struct ldb_control *search_control;
509 struct ldb_search_options_control *search_options;
510 struct ldb_control *extended_dn_control;
511 struct ldb_extended_dn_control *extended_dn_decoded = NULL;
512 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
513 const char **attrs = NULL;
514 const char *scope_str, *errstr = NULL;
515 int success_limit = 1;
516 int result = -1;
517 int ldb_ret = -1;
518 unsigned int i, j;
519 int extended_type = 1;
521 DEBUG(10, ("SearchRequest"));
522 DEBUGADD(10, (" basedn: %s", req->basedn));
523 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call, req->tree)));
525 local_ctx = talloc_new(call);
526 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
528 basedn = ldb_dn_new(local_ctx, samdb, req->basedn);
529 NT_STATUS_HAVE_NO_MEMORY(basedn);
531 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req->basedn));
532 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call, req->tree)));
534 switch (req->scope) {
535 case LDAP_SEARCH_SCOPE_BASE:
536 scope_str = "BASE";
537 scope = LDB_SCOPE_BASE;
538 success_limit = 0;
539 break;
540 case LDAP_SEARCH_SCOPE_SINGLE:
541 scope_str = "ONE";
542 scope = LDB_SCOPE_ONELEVEL;
543 success_limit = 0;
544 break;
545 case LDAP_SEARCH_SCOPE_SUB:
546 scope_str = "SUB";
547 scope = LDB_SCOPE_SUBTREE;
548 success_limit = 0;
549 break;
550 default:
551 result = LDAP_PROTOCOL_ERROR;
552 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
553 &errstr);
554 errstr = talloc_asprintf(local_ctx,
555 "%s. Invalid scope", errstr);
556 goto reply;
558 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str));
560 if (req->num_attributes >= 1) {
561 attrs = talloc_array(local_ctx, const char *, req->num_attributes+1);
562 NT_STATUS_HAVE_NO_MEMORY(attrs);
564 for (i=0; i < req->num_attributes; i++) {
565 DEBUG(10,("SearchRequest: attrs: [%s]\n",req->attributes[i]));
566 attrs[i] = req->attributes[i];
568 attrs[i] = NULL;
571 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
572 scope_str, req->basedn, ldb_filter_from_tree(call, req->tree)));
574 res = talloc_zero(local_ctx, struct ldb_result);
575 NT_STATUS_HAVE_NO_MEMORY(res);
577 ldb_ret = ldb_build_search_req_ex(&lreq, samdb, local_ctx,
578 basedn, scope,
579 req->tree, attrs,
580 call->request->controls,
581 res, ldb_search_default_callback,
582 NULL);
584 if (ldb_ret != LDB_SUCCESS) {
585 goto reply;
588 if (call->conn->global_catalog) {
589 search_control = ldb_request_get_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID);
591 search_options = NULL;
592 if (search_control) {
593 search_options = talloc_get_type(search_control->data, struct ldb_search_options_control);
594 search_options->search_options |= LDB_SEARCH_OPTION_PHANTOM_ROOT;
595 } else {
596 search_options = talloc(lreq, struct ldb_search_options_control);
597 NT_STATUS_HAVE_NO_MEMORY(search_options);
598 search_options->search_options = LDB_SEARCH_OPTION_PHANTOM_ROOT;
599 ldb_request_add_control(lreq, LDB_CONTROL_SEARCH_OPTIONS_OID, false, search_options);
601 } else {
602 ldb_request_add_control(lreq, DSDB_CONTROL_NO_GLOBAL_CATALOG, false, NULL);
605 extended_dn_control = ldb_request_get_control(lreq, LDB_CONTROL_EXTENDED_DN_OID);
607 if (extended_dn_control) {
608 if (extended_dn_control->data) {
609 extended_dn_decoded = talloc_get_type(extended_dn_control->data, struct ldb_extended_dn_control);
610 extended_type = extended_dn_decoded->type;
611 } else {
612 extended_type = 0;
616 ldb_set_timeout(samdb, lreq, req->timelimit);
618 if (!call->conn->is_privileged) {
619 ldb_req_mark_untrusted(lreq);
622 LDB_REQ_SET_LOCATION(lreq);
624 ldb_ret = ldb_request(samdb, lreq);
626 if (ldb_ret != LDB_SUCCESS) {
627 goto reply;
630 ldb_ret = ldb_wait(lreq->handle, LDB_WAIT_ALL);
632 if (ldb_ret == LDB_SUCCESS) {
633 for (i = 0; i < res->count; i++) {
634 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultEntry);
635 NT_STATUS_HAVE_NO_MEMORY(ent_r);
637 /* Better to have the whole message kept here,
638 * than to find someone further up didn't put
639 * a value in the right spot in the talloc tree */
640 talloc_steal(ent_r, res->msgs[i]);
642 ent = &ent_r->msg->r.SearchResultEntry;
643 ent->dn = ldb_dn_get_extended_linearized(ent_r, res->msgs[i]->dn, extended_type);
644 ent->num_attributes = 0;
645 ent->attributes = NULL;
646 if (res->msgs[i]->num_elements == 0) {
647 goto queue_reply;
649 ent->num_attributes = res->msgs[i]->num_elements;
650 ent->attributes = talloc_array(ent_r, struct ldb_message_element, ent->num_attributes);
651 NT_STATUS_HAVE_NO_MEMORY(ent->attributes);
652 for (j=0; j < ent->num_attributes; j++) {
653 ent->attributes[j].name = res->msgs[i]->elements[j].name;
654 ent->attributes[j].num_values = 0;
655 ent->attributes[j].values = NULL;
656 if (req->attributesonly && (res->msgs[i]->elements[j].num_values == 0)) {
657 continue;
659 ent->attributes[j].num_values = res->msgs[i]->elements[j].num_values;
660 ent->attributes[j].values = res->msgs[i]->elements[j].values;
662 queue_reply:
663 ldapsrv_queue_reply(call, ent_r);
666 /* Send back referrals if they do exist (search operations) */
667 if (res->refs != NULL) {
668 char **ref;
669 struct ldap_SearchResRef *ent_ref;
671 for (ref = res->refs; *ref != NULL; ++ref) {
672 ent_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultReference);
673 NT_STATUS_HAVE_NO_MEMORY(ent_r);
675 /* Better to have the whole referrals kept here,
676 * than to find someone further up didn't put
677 * a value in the right spot in the talloc tree
679 talloc_steal(ent_r, *ref);
681 ent_ref = &ent_r->msg->r.SearchResultReference;
682 ent_ref->referral = *ref;
684 ldapsrv_queue_reply(call, ent_r);
689 reply:
690 done_r = ldapsrv_init_reply(call, LDAP_TAG_SearchResultDone);
691 NT_STATUS_HAVE_NO_MEMORY(done_r);
693 done = &done_r->msg->r.SearchResultDone;
694 done->dn = NULL;
695 done->referral = NULL;
697 if (result != -1) {
698 } else if (ldb_ret == LDB_SUCCESS) {
699 if (res->count >= success_limit) {
700 DEBUG(10,("SearchRequest: results: [%d]\n", res->count));
701 result = LDAP_SUCCESS;
702 errstr = NULL;
704 if (res->controls) {
705 done_r->msg->controls = res->controls;
706 talloc_steal(done_r, res->controls);
708 } else {
709 DEBUG(10,("SearchRequest: error\n"));
710 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
711 &errstr);
714 done->resultcode = result;
715 done->errormessage = (errstr?talloc_strdup(done_r, errstr):NULL);
717 talloc_free(local_ctx);
719 ldapsrv_queue_reply(call, done_r);
720 return NT_STATUS_OK;
723 static NTSTATUS ldapsrv_ModifyRequest(struct ldapsrv_call *call)
725 struct ldap_ModifyRequest *req = &call->request->r.ModifyRequest;
726 struct ldap_Result *modify_result;
727 struct ldapsrv_reply *modify_reply;
728 TALLOC_CTX *local_ctx;
729 struct ldb_context *samdb = call->conn->ldb;
730 struct ldb_message *msg = NULL;
731 struct ldb_dn *dn;
732 const char *errstr = NULL;
733 int result = LDAP_SUCCESS;
734 int ldb_ret;
735 unsigned int i,j;
736 struct ldb_result *res = NULL;
738 DEBUG(10, ("ModifyRequest"));
739 DEBUGADD(10, (" dn: %s\n", req->dn));
741 local_ctx = talloc_named(call, 0, "ModifyRequest local memory context");
742 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
744 dn = ldb_dn_new(local_ctx, samdb, req->dn);
745 NT_STATUS_HAVE_NO_MEMORY(dn);
747 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req->dn));
749 msg = talloc(local_ctx, struct ldb_message);
750 NT_STATUS_HAVE_NO_MEMORY(msg);
752 msg->dn = dn;
753 msg->num_elements = 0;
754 msg->elements = NULL;
756 if (req->num_mods > 0) {
757 msg->num_elements = req->num_mods;
758 msg->elements = talloc_array(msg, struct ldb_message_element, req->num_mods);
759 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
761 for (i=0; i < msg->num_elements; i++) {
762 msg->elements[i].name = discard_const_p(char, req->mods[i].attrib.name);
763 msg->elements[i].num_values = 0;
764 msg->elements[i].values = NULL;
766 switch (req->mods[i].type) {
767 default:
768 result = LDAP_PROTOCOL_ERROR;
769 map_ldb_error(local_ctx,
770 LDB_ERR_PROTOCOL_ERROR, NULL, &errstr);
771 errstr = talloc_asprintf(local_ctx,
772 "%s. Invalid LDAP_MODIFY_* type", errstr);
773 goto reply;
774 case LDAP_MODIFY_ADD:
775 msg->elements[i].flags = LDB_FLAG_MOD_ADD;
776 break;
777 case LDAP_MODIFY_DELETE:
778 msg->elements[i].flags = LDB_FLAG_MOD_DELETE;
779 break;
780 case LDAP_MODIFY_REPLACE:
781 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
782 break;
785 msg->elements[i].num_values = req->mods[i].attrib.num_values;
786 if (msg->elements[i].num_values > 0) {
787 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
788 msg->elements[i].num_values);
789 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
791 for (j=0; j < msg->elements[i].num_values; j++) {
792 msg->elements[i].values[j].length = req->mods[i].attrib.values[j].length;
793 msg->elements[i].values[j].data = req->mods[i].attrib.values[j].data;
799 reply:
800 modify_reply = ldapsrv_init_reply(call, LDAP_TAG_ModifyResponse);
801 NT_STATUS_HAVE_NO_MEMORY(modify_reply);
803 if (result == LDAP_SUCCESS) {
804 res = talloc_zero(local_ctx, struct ldb_result);
805 NT_STATUS_HAVE_NO_MEMORY(res);
806 ldb_ret = ldapsrv_mod_with_controls(call, msg, call->request->controls, res);
807 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
808 &errstr);
811 modify_result = &modify_reply->msg->r.ModifyResponse;
812 modify_result->dn = NULL;
813 if ((res != NULL) && (res->refs != NULL)) {
814 modify_result->resultcode = map_ldb_error(local_ctx,
815 LDB_ERR_REFERRAL,
816 NULL, &errstr);
817 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
818 modify_result->referral = talloc_strdup(call, *res->refs);
819 } else {
820 modify_result->resultcode = result;
821 modify_result->errormessage = (errstr?talloc_strdup(modify_reply, errstr):NULL);
822 modify_result->referral = NULL;
824 talloc_free(local_ctx);
826 ldapsrv_queue_reply(call, modify_reply);
827 return NT_STATUS_OK;
831 static NTSTATUS ldapsrv_AddRequest(struct ldapsrv_call *call)
833 struct ldap_AddRequest *req = &call->request->r.AddRequest;
834 struct ldap_Result *add_result;
835 struct ldapsrv_reply *add_reply;
836 TALLOC_CTX *local_ctx;
837 struct ldb_context *samdb = call->conn->ldb;
838 struct ldb_message *msg = NULL;
839 struct ldb_dn *dn;
840 const char *errstr = NULL;
841 int result = LDAP_SUCCESS;
842 int ldb_ret;
843 unsigned int i,j;
844 struct ldb_result *res = NULL;
846 DEBUG(10, ("AddRequest"));
847 DEBUGADD(10, (" dn: %s\n", req->dn));
849 local_ctx = talloc_named(call, 0, "AddRequest local memory context");
850 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
852 dn = ldb_dn_new(local_ctx, samdb, req->dn);
853 NT_STATUS_HAVE_NO_MEMORY(dn);
855 DEBUG(10, ("AddRequest: dn: [%s]\n", req->dn));
857 msg = talloc(local_ctx, struct ldb_message);
858 NT_STATUS_HAVE_NO_MEMORY(msg);
860 msg->dn = dn;
861 msg->num_elements = 0;
862 msg->elements = NULL;
864 if (req->num_attributes > 0) {
865 msg->num_elements = req->num_attributes;
866 msg->elements = talloc_array(msg, struct ldb_message_element, msg->num_elements);
867 NT_STATUS_HAVE_NO_MEMORY(msg->elements);
869 for (i=0; i < msg->num_elements; i++) {
870 msg->elements[i].name = discard_const_p(char, req->attributes[i].name);
871 msg->elements[i].flags = 0;
872 msg->elements[i].num_values = 0;
873 msg->elements[i].values = NULL;
875 if (req->attributes[i].num_values > 0) {
876 msg->elements[i].num_values = req->attributes[i].num_values;
877 msg->elements[i].values = talloc_array(msg->elements, struct ldb_val,
878 msg->elements[i].num_values);
879 NT_STATUS_HAVE_NO_MEMORY(msg->elements[i].values);
881 for (j=0; j < msg->elements[i].num_values; j++) {
882 msg->elements[i].values[j].length = req->attributes[i].values[j].length;
883 msg->elements[i].values[j].data = req->attributes[i].values[j].data;
889 add_reply = ldapsrv_init_reply(call, LDAP_TAG_AddResponse);
890 NT_STATUS_HAVE_NO_MEMORY(add_reply);
892 if (result == LDAP_SUCCESS) {
893 res = talloc_zero(local_ctx, struct ldb_result);
894 NT_STATUS_HAVE_NO_MEMORY(res);
895 ldb_ret = ldapsrv_add_with_controls(call, msg, call->request->controls, res);
896 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
897 &errstr);
900 add_result = &add_reply->msg->r.AddResponse;
901 add_result->dn = NULL;
902 if ((res != NULL) && (res->refs != NULL)) {
903 add_result->resultcode = map_ldb_error(local_ctx,
904 LDB_ERR_REFERRAL, NULL,
905 &errstr);
906 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
907 add_result->referral = talloc_strdup(call, *res->refs);
908 } else {
909 add_result->resultcode = result;
910 add_result->errormessage = (errstr?talloc_strdup(add_reply,errstr):NULL);
911 add_result->referral = NULL;
913 talloc_free(local_ctx);
915 ldapsrv_queue_reply(call, add_reply);
916 return NT_STATUS_OK;
920 static NTSTATUS ldapsrv_DelRequest(struct ldapsrv_call *call)
922 struct ldap_DelRequest *req = &call->request->r.DelRequest;
923 struct ldap_Result *del_result;
924 struct ldapsrv_reply *del_reply;
925 TALLOC_CTX *local_ctx;
926 struct ldb_context *samdb = call->conn->ldb;
927 struct ldb_dn *dn;
928 const char *errstr = NULL;
929 int result = LDAP_SUCCESS;
930 int ldb_ret;
931 struct ldb_result *res = NULL;
933 DEBUG(10, ("DelRequest"));
934 DEBUGADD(10, (" dn: %s\n", req->dn));
936 local_ctx = talloc_named(call, 0, "DelRequest local memory context");
937 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
939 dn = ldb_dn_new(local_ctx, samdb, req->dn);
940 NT_STATUS_HAVE_NO_MEMORY(dn);
942 DEBUG(10, ("DelRequest: dn: [%s]\n", req->dn));
944 del_reply = ldapsrv_init_reply(call, LDAP_TAG_DelResponse);
945 NT_STATUS_HAVE_NO_MEMORY(del_reply);
947 if (result == LDAP_SUCCESS) {
948 res = talloc_zero(local_ctx, struct ldb_result);
949 NT_STATUS_HAVE_NO_MEMORY(res);
950 ldb_ret = ldapsrv_del_with_controls(call, dn, call->request->controls, res);
951 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
952 &errstr);
955 del_result = &del_reply->msg->r.DelResponse;
956 del_result->dn = NULL;
957 if ((res != NULL) && (res->refs != NULL)) {
958 del_result->resultcode = map_ldb_error(local_ctx,
959 LDB_ERR_REFERRAL, NULL,
960 &errstr);
961 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
962 del_result->referral = talloc_strdup(call, *res->refs);
963 } else {
964 del_result->resultcode = result;
965 del_result->errormessage = (errstr?talloc_strdup(del_reply,errstr):NULL);
966 del_result->referral = NULL;
969 talloc_free(local_ctx);
971 ldapsrv_queue_reply(call, del_reply);
972 return NT_STATUS_OK;
975 static NTSTATUS ldapsrv_ModifyDNRequest(struct ldapsrv_call *call)
977 struct ldap_ModifyDNRequest *req = &call->request->r.ModifyDNRequest;
978 struct ldap_Result *modifydn;
979 struct ldapsrv_reply *modifydn_r;
980 TALLOC_CTX *local_ctx;
981 struct ldb_context *samdb = call->conn->ldb;
982 struct ldb_dn *olddn, *newdn=NULL, *newrdn;
983 struct ldb_dn *parentdn = NULL;
984 const char *errstr = NULL;
985 int result = LDAP_SUCCESS;
986 int ldb_ret;
987 struct ldb_result *res = NULL;
989 DEBUG(10, ("ModifyDNRequest"));
990 DEBUGADD(10, (" dn: %s", req->dn));
991 DEBUGADD(10, (" newrdn: %s\n", req->newrdn));
993 local_ctx = talloc_named(call, 0, "ModifyDNRequest local memory context");
994 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
996 olddn = ldb_dn_new(local_ctx, samdb, req->dn);
997 NT_STATUS_HAVE_NO_MEMORY(olddn);
999 newrdn = ldb_dn_new(local_ctx, samdb, req->newrdn);
1000 NT_STATUS_HAVE_NO_MEMORY(newrdn);
1002 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req->dn));
1003 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req->newrdn));
1005 if (ldb_dn_get_comp_num(newrdn) == 0) {
1006 result = LDAP_PROTOCOL_ERROR;
1007 map_ldb_error(local_ctx, LDB_ERR_PROTOCOL_ERROR, NULL,
1008 &errstr);
1009 goto reply;
1012 if (ldb_dn_get_comp_num(newrdn) > 1) {
1013 result = LDAP_NAMING_VIOLATION;
1014 map_ldb_error(local_ctx, LDB_ERR_NAMING_VIOLATION, NULL,
1015 &errstr);
1016 goto reply;
1019 /* we can't handle the rename if we should not remove the old dn */
1020 if (!req->deleteolddn) {
1021 result = LDAP_UNWILLING_TO_PERFORM;
1022 map_ldb_error(local_ctx, LDB_ERR_UNWILLING_TO_PERFORM, NULL,
1023 &errstr);
1024 errstr = talloc_asprintf(local_ctx,
1025 "%s. Old RDN must be deleted", errstr);
1026 goto reply;
1029 if (req->newsuperior) {
1030 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req->newsuperior));
1031 parentdn = ldb_dn_new(local_ctx, samdb, req->newsuperior);
1034 if (!parentdn) {
1035 parentdn = ldb_dn_get_parent(local_ctx, olddn);
1037 if (!parentdn) {
1038 result = LDAP_NO_SUCH_OBJECT;
1039 map_ldb_error(local_ctx, LDB_ERR_NO_SUCH_OBJECT, NULL, &errstr);
1040 goto reply;
1043 if ( ! ldb_dn_add_child(parentdn, newrdn)) {
1044 result = LDAP_OTHER;
1045 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1046 goto reply;
1048 newdn = parentdn;
1050 reply:
1051 modifydn_r = ldapsrv_init_reply(call, LDAP_TAG_ModifyDNResponse);
1052 NT_STATUS_HAVE_NO_MEMORY(modifydn_r);
1054 if (result == LDAP_SUCCESS) {
1055 res = talloc_zero(local_ctx, struct ldb_result);
1056 NT_STATUS_HAVE_NO_MEMORY(res);
1057 ldb_ret = ldapsrv_rename_with_controls(call, olddn, newdn, call->request->controls, res);
1058 result = map_ldb_error(local_ctx, ldb_ret, ldb_errstring(samdb),
1059 &errstr);
1062 modifydn = &modifydn_r->msg->r.ModifyDNResponse;
1063 modifydn->dn = NULL;
1064 if ((res != NULL) && (res->refs != NULL)) {
1065 modifydn->resultcode = map_ldb_error(local_ctx,
1066 LDB_ERR_REFERRAL, NULL,
1067 &errstr);;
1068 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1069 modifydn->referral = talloc_strdup(call, *res->refs);
1070 } else {
1071 modifydn->resultcode = result;
1072 modifydn->errormessage = (errstr?talloc_strdup(modifydn_r,errstr):NULL);
1073 modifydn->referral = NULL;
1076 talloc_free(local_ctx);
1078 ldapsrv_queue_reply(call, modifydn_r);
1079 return NT_STATUS_OK;
1082 static NTSTATUS ldapsrv_CompareRequest(struct ldapsrv_call *call)
1084 struct ldap_CompareRequest *req = &call->request->r.CompareRequest;
1085 struct ldap_Result *compare;
1086 struct ldapsrv_reply *compare_r;
1087 TALLOC_CTX *local_ctx;
1088 struct ldb_context *samdb = call->conn->ldb;
1089 struct ldb_result *res = NULL;
1090 struct ldb_dn *dn;
1091 const char *attrs[1];
1092 const char *errstr = NULL;
1093 const char *filter = NULL;
1094 int result = LDAP_SUCCESS;
1095 int ldb_ret;
1097 DEBUG(10, ("CompareRequest"));
1098 DEBUGADD(10, (" dn: %s\n", req->dn));
1100 local_ctx = talloc_named(call, 0, "CompareRequest local_memory_context");
1101 NT_STATUS_HAVE_NO_MEMORY(local_ctx);
1103 dn = ldb_dn_new(local_ctx, samdb, req->dn);
1104 NT_STATUS_HAVE_NO_MEMORY(dn);
1106 DEBUG(10, ("CompareRequest: dn: [%s]\n", req->dn));
1107 filter = talloc_asprintf(local_ctx, "(%s=%*s)", req->attribute,
1108 (int)req->value.length, req->value.data);
1109 NT_STATUS_HAVE_NO_MEMORY(filter);
1111 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter));
1113 attrs[0] = NULL;
1115 compare_r = ldapsrv_init_reply(call, LDAP_TAG_CompareResponse);
1116 NT_STATUS_HAVE_NO_MEMORY(compare_r);
1118 if (result == LDAP_SUCCESS) {
1119 ldb_ret = ldb_search(samdb, local_ctx, &res,
1120 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
1121 if (ldb_ret != LDB_SUCCESS) {
1122 result = map_ldb_error(local_ctx, ldb_ret,
1123 ldb_errstring(samdb), &errstr);
1124 DEBUG(10,("CompareRequest: error: %s\n", errstr));
1125 } else if (res->count == 0) {
1126 DEBUG(10,("CompareRequest: doesn't matched\n"));
1127 result = LDAP_COMPARE_FALSE;
1128 errstr = NULL;
1129 } else if (res->count == 1) {
1130 DEBUG(10,("CompareRequest: matched\n"));
1131 result = LDAP_COMPARE_TRUE;
1132 errstr = NULL;
1133 } else if (res->count > 1) {
1134 result = LDAP_OTHER;
1135 map_ldb_error(local_ctx, LDB_ERR_OTHER, NULL, &errstr);
1136 errstr = talloc_asprintf(local_ctx,
1137 "%s. Too many objects match!", errstr);
1138 DEBUG(10,("CompareRequest: %d results: %s\n", res->count, errstr));
1142 compare = &compare_r->msg->r.CompareResponse;
1143 compare->dn = NULL;
1144 compare->resultcode = result;
1145 compare->errormessage = (errstr?talloc_strdup(compare_r,errstr):NULL);
1146 compare->referral = NULL;
1148 talloc_free(local_ctx);
1150 ldapsrv_queue_reply(call, compare_r);
1151 return NT_STATUS_OK;
1154 static NTSTATUS ldapsrv_AbandonRequest(struct ldapsrv_call *call)
1156 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
1157 DEBUG(10, ("AbandonRequest\n"));
1158 return NT_STATUS_OK;
1161 NTSTATUS ldapsrv_do_call(struct ldapsrv_call *call)
1163 unsigned int i;
1164 struct ldap_message *msg = call->request;
1165 struct ldb_context *samdb = call->conn->ldb;
1166 NTSTATUS status;
1167 time_t *lastts;
1168 /* Check for undecoded critical extensions */
1169 for (i=0; msg->controls && msg->controls[i]; i++) {
1170 if (!msg->controls_decoded[i] &&
1171 msg->controls[i]->critical) {
1172 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1173 msg->controls[i]->oid));
1174 return ldapsrv_unwilling(call, LDAP_UNAVAILABLE_CRITICAL_EXTENSION);
1178 switch(call->request->type) {
1179 case LDAP_TAG_BindRequest:
1180 return ldapsrv_BindRequest(call);
1181 case LDAP_TAG_UnbindRequest:
1182 return ldapsrv_UnbindRequest(call);
1183 case LDAP_TAG_SearchRequest:
1184 return ldapsrv_SearchRequest(call);
1185 case LDAP_TAG_ModifyRequest:
1186 status = ldapsrv_ModifyRequest(call);
1187 break;
1188 case LDAP_TAG_AddRequest:
1189 status = ldapsrv_AddRequest(call);
1190 break;
1191 case LDAP_TAG_DelRequest:
1192 return ldapsrv_DelRequest(call);
1193 case LDAP_TAG_ModifyDNRequest:
1194 return ldapsrv_ModifyDNRequest(call);
1195 case LDAP_TAG_CompareRequest:
1196 return ldapsrv_CompareRequest(call);
1197 case LDAP_TAG_AbandonRequest:
1198 return ldapsrv_AbandonRequest(call);
1199 case LDAP_TAG_ExtendedRequest:
1200 return ldapsrv_ExtendedRequest(call);
1201 default:
1202 return ldapsrv_unwilling(call, LDAP_PROTOCOL_ERROR);
1205 if (NT_STATUS_IS_OK(status)) {
1206 lastts = (time_t *)ldb_get_opaque(samdb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME);
1207 if (lastts && !*lastts) {
1208 DEBUG(10, ("Schema update now was requested, "
1209 "fullfilling the request ts = %d\n",
1210 (int)*lastts));
1212 * Just requesting the schema will do the trick
1213 * as the delay for reload is experied, we will have a reload
1214 * from the schema as expected as we are not yet in a transaction!
1216 dsdb_get_schema(samdb, NULL);
1217 *lastts = time(NULL);
1218 ldb_set_opaque(samdb, DSDB_OPAQUE_LAST_SCHEMA_UPDATE_MSG_OPAQUE_NAME, lastts);
1221 return status;