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 "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>
34 static int map_ldb_error(TALLOC_CTX
*mem_ctx
, int ldb_err
,
35 const char *add_err_string
, const char **errstring
)
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
) {
43 strtol(add_err_string
, &endptr
, 16);
44 if (endptr
!= add_err_string
) {
45 *errstring
= add_err_string
;
50 /* Otherwise we calculate here a generic, but appropriate WERROR. */
56 case LDB_ERR_OPERATIONS_ERROR
:
57 err
= WERR_DS_OPERATIONS_ERROR
;
59 case LDB_ERR_PROTOCOL_ERROR
:
60 err
= WERR_DS_PROTOCOL_ERROR
;
62 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
63 err
= WERR_DS_TIMELIMIT_EXCEEDED
;
65 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
66 err
= WERR_DS_SIZELIMIT_EXCEEDED
;
68 case LDB_ERR_COMPARE_FALSE
:
69 err
= WERR_DS_COMPARE_FALSE
;
71 case LDB_ERR_COMPARE_TRUE
:
72 err
= WERR_DS_COMPARE_TRUE
;
74 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
75 err
= WERR_DS_AUTH_METHOD_NOT_SUPPORTED
;
77 case LDB_ERR_STRONG_AUTH_REQUIRED
:
78 err
= WERR_DS_STRONG_AUTH_REQUIRED
;
80 case LDB_ERR_REFERRAL
:
81 err
= WERR_DS_REFERRAL
;
83 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
84 err
= WERR_DS_ADMIN_LIMIT_EXCEEDED
;
86 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
87 err
= WERR_DS_UNAVAILABLE_CRIT_EXTENSION
;
89 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
90 err
= WERR_DS_CONFIDENTIALITY_REQUIRED
;
92 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
95 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
96 err
= WERR_DS_NO_ATTRIBUTE_OR_VALUE
;
98 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
99 err
= WERR_DS_ATTRIBUTE_TYPE_UNDEFINED
;
101 case LDB_ERR_INAPPROPRIATE_MATCHING
:
102 err
= WERR_DS_INAPPROPRIATE_MATCHING
;
104 case LDB_ERR_CONSTRAINT_VIOLATION
:
105 err
= WERR_DS_CONSTRAINT_VIOLATION
;
107 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
108 err
= WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS
;
110 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
111 err
= WERR_DS_INVALID_ATTRIBUTE_SYNTAX
;
113 case LDB_ERR_NO_SUCH_OBJECT
:
114 err
= WERR_DS_NO_SUCH_OBJECT
;
116 case LDB_ERR_ALIAS_PROBLEM
:
117 err
= WERR_DS_ALIAS_PROBLEM
;
119 case LDB_ERR_INVALID_DN_SYNTAX
:
120 err
= WERR_DS_INVALID_DN_SYNTAX
;
122 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
123 err
= WERR_DS_ALIAS_DEREF_PROBLEM
;
125 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
126 err
= WERR_DS_INAPPROPRIATE_AUTH
;
128 case LDB_ERR_INVALID_CREDENTIALS
:
129 err
= WERR_ACCESS_DENIED
;
131 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
132 err
= WERR_DS_INSUFF_ACCESS_RIGHTS
;
137 case LDB_ERR_UNAVAILABLE
:
138 err
= WERR_DS_UNAVAILABLE
;
140 case LDB_ERR_UNWILLING_TO_PERFORM
:
141 err
= WERR_DS_UNWILLING_TO_PERFORM
;
143 case LDB_ERR_LOOP_DETECT
:
144 err
= WERR_DS_LOOP_DETECT
;
146 case LDB_ERR_NAMING_VIOLATION
:
147 err
= WERR_DS_NAMING_VIOLATION
;
149 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
150 err
= WERR_DS_OBJ_CLASS_VIOLATION
;
152 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
153 err
= WERR_DS_CANT_ON_NON_LEAF
;
155 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
156 err
= WERR_DS_CANT_ON_RDN
;
158 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
159 err
= WERR_DS_OBJ_STRING_NAME_EXISTS
;
161 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
162 err
= WERR_DS_CANT_MOD_OBJ_CLASS
;
164 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
165 err
= WERR_DS_AFFECTS_MULTIPLE_DSAS
;
168 err
= WERR_DS_GENERIC_ERROR
;
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 */
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
,
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
))
203 if (ops
[i
]->sasl_name
&& ops
[i
]->server_start
) {
204 char *sasl_name
= talloc_strdup(conn
, ops
[i
]->sasl_name
);
207 return NT_STATUS_NO_MEMORY
;
209 sasl_mechs
= talloc_realloc(conn
, sasl_mechs
, char *, j
+ 2);
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
;
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
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
);
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
);
243 reply
->msg
= talloc(reply
, struct ldap_message
);
244 if (reply
->msg
== NULL
) {
249 reply
->msg
->messageid
= call
->request
->messageid
;
250 reply
->msg
->type
= type
;
251 reply
->msg
->controls
= NULL
;
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
);
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
;
281 ldapsrv_queue_reply(call
, reply
);
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
;
294 ret
= ldb_msg_sanity_check(ldb
, message
);
295 if (ret
!= LDB_SUCCESS
) {
299 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
303 ldb_modify_default_callback
,
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
) {
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
);
333 ldb_transaction_cancel(ldb
);
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
;
350 ret
= ldb_msg_sanity_check(ldb
, message
);
351 if (ret
!= LDB_SUCCESS
) {
355 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
359 ldb_modify_default_callback
,
362 if (ret
!= LDB_SUCCESS
) {
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
) {
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
);
391 ldb_transaction_cancel(ldb
);
398 /* create and execute a delete request */
399 static int ldapsrv_del_with_controls(struct ldapsrv_call
*call
,
401 struct ldb_control
**controls
,
402 struct ldb_result
*res
)
404 struct ldb_context
*ldb
= call
->conn
->ldb
;
405 struct ldb_request
*req
;
408 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
412 ldb_modify_default_callback
,
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
) {
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
);
442 ldb_transaction_cancel(ldb
);
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
;
459 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
464 ldb_modify_default_callback
,
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
) {
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
);
494 ldb_transaction_cancel(ldb
);
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;
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
:
541 scope
= LDB_SCOPE_BASE
;
544 case LDAP_SEARCH_SCOPE_SINGLE
:
546 scope
= LDB_SCOPE_ONELEVEL
;
549 case LDAP_SEARCH_SCOPE_SUB
:
551 scope
= LDB_SCOPE_SUBTREE
;
555 result
= LDAP_PROTOCOL_ERROR
;
556 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
558 errstr
= talloc_asprintf(local_ctx
,
559 "%s. Invalid scope", errstr
);
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
];
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
,
584 call
->request
->controls
,
585 res
, ldb_search_default_callback
,
588 if (ldb_ret
!= LDB_SUCCESS
) {
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
;
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
);
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
;
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
) {
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) {
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)) {
663 ent
->attributes
[j
].num_values
= res
->msgs
[i
]->elements
[j
].num_values
;
664 ent
->attributes
[j
].values
= res
->msgs
[i
]->elements
[j
].values
;
667 ldapsrv_queue_reply(call
, ent_r
);
670 /* Send back referrals if they do exist (search operations) */
671 if (res
->refs
!= NULL
) {
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
);
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
;
699 done
->referral
= NULL
;
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
;
709 done_r
->msg
->controls
= res
->controls
;
710 talloc_steal(done_r
, res
->controls
);
713 DEBUG(10,("SearchRequest: error\n"));
714 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
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
);
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
;
736 const char *errstr
= NULL
;
737 int result
= LDAP_SUCCESS
;
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
);
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
) {
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
);
778 case LDAP_MODIFY_ADD
:
779 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
781 case LDAP_MODIFY_DELETE
:
782 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
784 case LDAP_MODIFY_REPLACE
:
785 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
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
;
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
),
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
,
821 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
822 modify_result
->referral
= talloc_strdup(call
, *res
->refs
);
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
);
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
;
844 const char *errstr
= NULL
;
845 int result
= LDAP_SUCCESS
;
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
);
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
),
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
,
910 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
911 add_result
->referral
= talloc_strdup(call
, *res
->refs
);
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
);
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
;
932 const char *errstr
= NULL
;
933 int result
= LDAP_SUCCESS
;
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
),
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
,
965 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
966 del_result
->referral
= talloc_strdup(call
, *res
->refs
);
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
);
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
;
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
,
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
,
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
,
1028 errstr
= talloc_asprintf(local_ctx
,
1029 "%s. Old RDN must be deleted", errstr
);
1033 if (req
->newsuperior
) {
1034 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
1035 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
1039 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
1042 result
= LDAP_NO_SUCH_OBJECT
;
1043 map_ldb_error(local_ctx
, LDB_ERR_NO_SUCH_OBJECT
, NULL
, &errstr
);
1047 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
1048 result
= LDAP_OTHER
;
1049 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
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
),
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
,
1072 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1073 modifydn
->referral
= talloc_strdup(call
, *res
->refs
);
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
;
1095 const char *attrs
[1];
1096 const char *errstr
= NULL
;
1097 const char *filter
= NULL
;
1098 int result
= LDAP_SUCCESS
;
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
));
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
;
1133 } else if (res
->count
== 1) {
1134 DEBUG(10,("CompareRequest: matched\n"));
1135 result
= LDAP_COMPARE_TRUE
;
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
;
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
)
1168 struct ldap_message
*msg
= call
->request
;
1169 struct ldb_context
*samdb
= call
->conn
->ldb
;
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
);
1192 case LDAP_TAG_AddRequest
:
1193 status
= ldapsrv_AddRequest(call
);
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
);
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",
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
);