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/>.
23 #include "ldap_server/ldap_server.h"
24 #include "../lib/util/dlinklist.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "auth/common_auth.h"
28 #include "param/param.h"
29 #include "samba/service_stream.h"
30 #include "dsdb/gmsa/util.h"
31 #include "dsdb/samdb/samdb.h"
32 #include <ldb_errors.h>
33 #include <ldb_module.h>
35 #include "lib/tsocket/tsocket.h"
36 #include "libcli/ldap/ldap_proto.h"
37 #include "source4/auth/auth.h"
40 #define DBGC_CLASS DBGC_LDAPSRV
42 static int map_ldb_error(TALLOC_CTX
*mem_ctx
, int ldb_err
,
43 const char *add_err_string
, const char **errstring
)
47 /* Certain LDB modules need to return very special WERROR codes. Proof
48 * for them here and if they exist skip the rest of the mapping. */
49 if (add_err_string
!= NULL
) {
51 strtol(add_err_string
, &endptr
, 16);
52 if (endptr
!= add_err_string
) {
53 *errstring
= add_err_string
;
58 /* Otherwise we calculate here a generic, but appropriate WERROR. */
64 case LDB_ERR_OPERATIONS_ERROR
:
65 err
= WERR_DS_OPERATIONS_ERROR
;
67 case LDB_ERR_PROTOCOL_ERROR
:
68 err
= WERR_DS_PROTOCOL_ERROR
;
70 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
71 err
= WERR_DS_TIMELIMIT_EXCEEDED
;
73 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
74 err
= WERR_DS_SIZELIMIT_EXCEEDED
;
76 case LDB_ERR_COMPARE_FALSE
:
77 err
= WERR_DS_COMPARE_FALSE
;
79 case LDB_ERR_COMPARE_TRUE
:
80 err
= WERR_DS_COMPARE_TRUE
;
82 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
83 err
= WERR_DS_AUTH_METHOD_NOT_SUPPORTED
;
85 case LDB_ERR_STRONG_AUTH_REQUIRED
:
86 err
= WERR_DS_STRONG_AUTH_REQUIRED
;
88 case LDB_ERR_REFERRAL
:
89 err
= WERR_DS_REFERRAL
;
91 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
92 err
= WERR_DS_ADMIN_LIMIT_EXCEEDED
;
94 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
95 err
= WERR_DS_UNAVAILABLE_CRIT_EXTENSION
;
97 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
98 err
= WERR_DS_CONFIDENTIALITY_REQUIRED
;
100 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
103 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
104 err
= WERR_DS_NO_ATTRIBUTE_OR_VALUE
;
106 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
107 err
= WERR_DS_ATTRIBUTE_TYPE_UNDEFINED
;
109 case LDB_ERR_INAPPROPRIATE_MATCHING
:
110 err
= WERR_DS_INAPPROPRIATE_MATCHING
;
112 case LDB_ERR_CONSTRAINT_VIOLATION
:
113 err
= WERR_DS_CONSTRAINT_VIOLATION
;
115 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
116 err
= WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS
;
118 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
119 err
= WERR_DS_INVALID_ATTRIBUTE_SYNTAX
;
121 case LDB_ERR_NO_SUCH_OBJECT
:
122 err
= WERR_DS_NO_SUCH_OBJECT
;
124 case LDB_ERR_ALIAS_PROBLEM
:
125 err
= WERR_DS_ALIAS_PROBLEM
;
127 case LDB_ERR_INVALID_DN_SYNTAX
:
128 err
= WERR_DS_INVALID_DN_SYNTAX
;
130 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
131 err
= WERR_DS_ALIAS_DEREF_PROBLEM
;
133 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
134 err
= WERR_DS_INAPPROPRIATE_AUTH
;
136 case LDB_ERR_INVALID_CREDENTIALS
:
137 err
= WERR_ACCESS_DENIED
;
139 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
140 err
= WERR_DS_INSUFF_ACCESS_RIGHTS
;
145 case LDB_ERR_UNAVAILABLE
:
146 err
= WERR_DS_UNAVAILABLE
;
148 case LDB_ERR_UNWILLING_TO_PERFORM
:
149 err
= WERR_DS_UNWILLING_TO_PERFORM
;
151 case LDB_ERR_LOOP_DETECT
:
152 err
= WERR_DS_LOOP_DETECT
;
154 case LDB_ERR_NAMING_VIOLATION
:
155 err
= WERR_DS_NAMING_VIOLATION
;
157 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
158 err
= WERR_DS_OBJ_CLASS_VIOLATION
;
160 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
161 err
= WERR_DS_CANT_ON_NON_LEAF
;
163 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
164 err
= WERR_DS_CANT_ON_RDN
;
166 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
167 err
= WERR_DS_OBJ_STRING_NAME_EXISTS
;
169 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
170 err
= WERR_DS_CANT_MOD_OBJ_CLASS
;
172 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
173 err
= WERR_DS_AFFECTS_MULTIPLE_DSAS
;
176 err
= WERR_DS_GENERIC_ERROR
;
180 *errstring
= talloc_asprintf(mem_ctx
, "%08X: %s", W_ERROR_V(err
),
181 add_err_string
!= NULL
? add_err_string
: ldb_strerror(ldb_err
));
183 /* result is 1:1 for now */
188 connect to the sam database
190 int ldapsrv_backend_Init(struct ldapsrv_connection
*conn
,
193 bool using_tls
= conn
->sockets
.active
== conn
->sockets
.tls
;
194 bool using_seal
= conn
->gensec
!= NULL
&& gensec_have_feature(conn
->gensec
,
195 GENSEC_FEATURE_SEAL
);
196 struct dsdb_encrypted_connection_state
*opaque_connection_state
= NULL
;
198 int ret
= samdb_connect_url(conn
,
199 conn
->connection
->event
.ctx
,
202 conn
->global_catalog
? LDB_FLG_RDONLY
: 0,
204 conn
->connection
->remote_address
,
207 if (ret
!= LDB_SUCCESS
) {
212 * We can safely call ldb_set_opaque() on this ldb as we have
213 * set remote_address above which avoids the ldb handle cache
215 opaque_connection_state
= talloc_zero(conn
, struct dsdb_encrypted_connection_state
);
216 if (opaque_connection_state
== NULL
) {
217 return LDB_ERR_OPERATIONS_ERROR
;
219 opaque_connection_state
->using_encrypted_connection
= using_tls
|| using_seal
|| conn
->is_ldapi
;
220 ret
= ldb_set_opaque(conn
->ldb
,
221 DSDB_OPAQUE_ENCRYPTED_CONNECTION_STATE_NAME
,
222 opaque_connection_state
);
223 if (ret
!= LDB_SUCCESS
) {
224 DBG_ERR("ldb_set_opaque() failed to store our "
225 "encrypted connection state!\n");
229 if (conn
->server_credentials
) {
230 struct gensec_security
*gensec_security
= NULL
;
231 const char **sasl_mechs
= NULL
;
234 status
= samba_server_gensec_start(conn
,
235 conn
->connection
->event
.ctx
,
236 conn
->connection
->msg_ctx
,
238 conn
->server_credentials
,
241 if (!NT_STATUS_IS_OK(status
)) {
242 DBG_ERR("samba_server_gensec_start failed: %s\n",
244 return LDB_ERR_OPERATIONS_ERROR
;
247 /* ldb can have a different lifetime to conn, so we
248 need to ensure that sasl_mechs lives as long as the
250 sasl_mechs
= gensec_security_sasl_names(gensec_security
,
252 TALLOC_FREE(gensec_security
);
253 if (sasl_mechs
== NULL
) {
254 DBG_ERR("Failed to get sasl mechs!\n");
255 return LDB_ERR_OPERATIONS_ERROR
;
258 ldb_set_opaque(conn
->ldb
, "supportedSASLMechanisms", sasl_mechs
);
264 struct ldapsrv_reply
*ldapsrv_init_reply(struct ldapsrv_call
*call
, uint8_t type
)
266 struct ldapsrv_reply
*reply
;
268 reply
= talloc_zero(call
, struct ldapsrv_reply
);
272 reply
->msg
= talloc_zero(reply
, struct ldap_message
);
273 if (reply
->msg
== NULL
) {
278 reply
->msg
->messageid
= call
->request
->messageid
;
279 reply
->msg
->type
= type
;
280 reply
->msg
->controls
= NULL
;
286 * Encode a reply to an LDAP client as ASN.1, free the original memory
288 static NTSTATUS
ldapsrv_encode(TALLOC_CTX
*mem_ctx
,
289 struct ldapsrv_reply
*reply
)
291 bool bret
= ldap_encode(reply
->msg
,
292 samba_ldap_control_handlers(),
296 DBG_ERR("Failed to encode ldap reply of type %d: "
297 "ldap_encode() failed\n",
299 TALLOC_FREE(reply
->msg
);
300 return NT_STATUS_NO_MEMORY
;
303 TALLOC_FREE(reply
->msg
);
304 talloc_set_name_const(reply
->blob
.data
,
305 "Outgoing, encoded single LDAP reply");
311 * Queue a reply (encoding it also), even if it would exceed the
312 * limit. This allows the error packet with LDAP_SIZE_LIMIT_EXCEEDED
315 static NTSTATUS
ldapsrv_queue_reply_forced(struct ldapsrv_call
*call
,
316 struct ldapsrv_reply
*reply
)
318 NTSTATUS status
= ldapsrv_encode(call
, reply
);
320 if (NT_STATUS_IS_OK(status
)) {
321 DLIST_ADD_END(call
->replies
, reply
);
327 * Queue a reply (encoding it also) but check we do not send more than
328 * LDAP_SERVER_MAX_REPLY_SIZE of responses as a way to limit the
329 * amount of data a client can make us allocate.
331 NTSTATUS
ldapsrv_queue_reply(struct ldapsrv_call
*call
, struct ldapsrv_reply
*reply
)
333 NTSTATUS status
= ldapsrv_encode(call
, reply
);
335 if (!NT_STATUS_IS_OK(status
)) {
339 if (call
->reply_size
> call
->reply_size
+ reply
->blob
.length
340 || call
->reply_size
+ reply
->blob
.length
> LDAP_SERVER_MAX_REPLY_SIZE
) {
341 DBG_WARNING("Refusing to queue LDAP search response size "
342 "of more than %zu bytes\n",
343 LDAP_SERVER_MAX_REPLY_SIZE
);
344 TALLOC_FREE(reply
->blob
.data
);
345 return NT_STATUS_FILE_TOO_LARGE
;
348 call
->reply_size
+= reply
->blob
.length
;
350 DLIST_ADD_END(call
->replies
, reply
);
355 static NTSTATUS
ldapsrv_unwilling(struct ldapsrv_call
*call
, int error
)
357 struct ldapsrv_reply
*reply
;
358 struct ldap_ExtendedResponse
*r
;
360 DBG_DEBUG("type[%d] id[%d]\n", call
->request
->type
, call
->request
->messageid
);
362 reply
= ldapsrv_init_reply(call
, LDAP_TAG_ExtendedResponse
);
364 return NT_STATUS_NO_MEMORY
;
367 r
= &reply
->msg
->r
.ExtendedResponse
;
368 r
->response
.resultcode
= error
;
369 r
->response
.dn
= NULL
;
370 r
->response
.errormessage
= NULL
;
371 r
->response
.referral
= NULL
;
375 ldapsrv_queue_reply(call
, reply
);
379 static int ldapsrv_add_with_controls(struct ldapsrv_call
*call
,
380 const struct ldb_message
*message
,
381 struct ldb_control
**controls
,
382 struct ldb_result
*res
)
384 struct ldb_context
*ldb
= call
->conn
->ldb
;
385 struct ldb_request
*req
;
388 ret
= ldb_msg_sanity_check(ldb
, message
);
389 if (ret
!= LDB_SUCCESS
) {
393 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
397 ldb_modify_default_callback
,
400 if (ret
!= LDB_SUCCESS
) return ret
;
402 if (call
->conn
->global_catalog
) {
403 return ldb_error(ldb
, LDB_ERR_UNWILLING_TO_PERFORM
, "modify forbidden on global catalog port");
405 ldb_request_add_control(req
, DSDB_CONTROL_NO_GLOBAL_CATALOG
, false, NULL
);
407 ret
= ldb_transaction_start(ldb
);
408 if (ret
!= LDB_SUCCESS
) {
412 if (!call
->conn
->is_privileged
) {
413 ldb_req_mark_untrusted(req
);
416 LDB_REQ_SET_LOCATION(req
);
418 ret
= ldb_request(ldb
, req
);
419 if (ret
== LDB_SUCCESS
) {
420 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
423 if (ret
== LDB_SUCCESS
) {
424 ret
= ldb_transaction_commit(ldb
);
427 ldb_transaction_cancel(ldb
);
434 /* create and execute a modify request */
435 static int ldapsrv_mod_with_controls(struct ldapsrv_call
*call
,
436 const struct ldb_message
*message
,
437 struct ldb_control
**controls
,
438 struct ldb_result
*res
)
440 struct ldb_context
*ldb
= call
->conn
->ldb
;
441 struct ldb_request
*req
;
444 ret
= ldb_msg_sanity_check(ldb
, message
);
445 if (ret
!= LDB_SUCCESS
) {
449 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
453 ldb_modify_default_callback
,
456 if (ret
!= LDB_SUCCESS
) {
460 if (call
->conn
->global_catalog
) {
461 return ldb_error(ldb
, LDB_ERR_UNWILLING_TO_PERFORM
, "modify forbidden on global catalog port");
463 ldb_request_add_control(req
, DSDB_CONTROL_NO_GLOBAL_CATALOG
, false, NULL
);
465 ret
= ldb_transaction_start(ldb
);
466 if (ret
!= LDB_SUCCESS
) {
470 if (!call
->conn
->is_privileged
) {
471 ldb_req_mark_untrusted(req
);
474 LDB_REQ_SET_LOCATION(req
);
476 ret
= ldb_request(ldb
, req
);
477 if (ret
== LDB_SUCCESS
) {
478 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
481 if (ret
== LDB_SUCCESS
) {
482 ret
= ldb_transaction_commit(ldb
);
485 ldb_transaction_cancel(ldb
);
492 /* create and execute a delete request */
493 static int ldapsrv_del_with_controls(struct ldapsrv_call
*call
,
495 struct ldb_control
**controls
,
496 struct ldb_result
*res
)
498 struct ldb_context
*ldb
= call
->conn
->ldb
;
499 struct ldb_request
*req
;
502 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
506 ldb_modify_default_callback
,
509 if (ret
!= LDB_SUCCESS
) return ret
;
511 if (call
->conn
->global_catalog
) {
512 return ldb_error(ldb
, LDB_ERR_UNWILLING_TO_PERFORM
, "modify forbidden on global catalog port");
514 ldb_request_add_control(req
, DSDB_CONTROL_NO_GLOBAL_CATALOG
, false, NULL
);
516 ret
= ldb_transaction_start(ldb
);
517 if (ret
!= LDB_SUCCESS
) {
521 if (!call
->conn
->is_privileged
) {
522 ldb_req_mark_untrusted(req
);
525 LDB_REQ_SET_LOCATION(req
);
527 ret
= ldb_request(ldb
, req
);
528 if (ret
== LDB_SUCCESS
) {
529 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
532 if (ret
== LDB_SUCCESS
) {
533 ret
= ldb_transaction_commit(ldb
);
536 ldb_transaction_cancel(ldb
);
543 static int ldapsrv_rename_with_controls(struct ldapsrv_call
*call
,
544 struct ldb_dn
*olddn
,
545 struct ldb_dn
*newdn
,
546 struct ldb_control
**controls
,
547 struct ldb_result
*res
)
549 struct ldb_context
*ldb
= call
->conn
->ldb
;
550 struct ldb_request
*req
;
553 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
558 ldb_modify_default_callback
,
561 if (ret
!= LDB_SUCCESS
) return ret
;
563 if (call
->conn
->global_catalog
) {
564 return ldb_error(ldb
, LDB_ERR_UNWILLING_TO_PERFORM
, "modify forbidden on global catalog port");
566 ldb_request_add_control(req
, DSDB_CONTROL_NO_GLOBAL_CATALOG
, false, NULL
);
568 ret
= ldb_transaction_start(ldb
);
569 if (ret
!= LDB_SUCCESS
) {
573 if (!call
->conn
->is_privileged
) {
574 ldb_req_mark_untrusted(req
);
577 LDB_REQ_SET_LOCATION(req
);
579 ret
= ldb_request(ldb
, req
);
580 if (ret
== LDB_SUCCESS
) {
581 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
584 if (ret
== LDB_SUCCESS
) {
585 ret
= ldb_transaction_commit(ldb
);
588 ldb_transaction_cancel(ldb
);
597 struct ldapsrv_context
{
598 struct ldapsrv_call
*call
;
601 struct ldb_control
**controls
;
602 size_t count
; /* For notification only */
603 const struct gmsa_update
**updates
;
606 static int ldap_server_search_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
608 struct ldapsrv_context
*ctx
= talloc_get_type(req
->context
, struct ldapsrv_context
);
609 struct ldapsrv_call
*call
= ctx
->call
;
610 struct ldb_context
*ldb
= call
->conn
->ldb
;
612 struct ldapsrv_reply
*ent_r
= NULL
;
613 struct ldap_SearchResEntry
*ent
;
618 return ldb_request_done(req
, LDB_ERR_OPERATIONS_ERROR
);
620 if (ares
->error
!= LDB_SUCCESS
) {
621 return ldb_request_done(req
, ares
->error
);
624 switch (ares
->type
) {
625 case LDB_REPLY_ENTRY
:
627 struct ldb_message
*msg
= ares
->message
;
628 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultEntry
);
636 * Put the LDAP search response data under ent_r->msg
637 * so we can free that later once encoded
639 talloc_steal(ent_r
->msg
, msg
);
641 ent
= &ent_r
->msg
->r
.SearchResultEntry
;
642 ent
->dn
= ldb_dn_get_extended_linearized(ent_r
, msg
->dn
,
644 ent
->num_attributes
= 0;
645 ent
->attributes
= NULL
;
646 if (msg
->num_elements
== 0) {
649 ent
->num_attributes
= msg
->num_elements
;
650 ent
->attributes
= talloc_array(ent_r
, struct ldb_message_element
, ent
->num_attributes
);
651 if (ent
->attributes
== NULL
) {
655 for (j
=0; j
< ent
->num_attributes
; j
++) {
656 ent
->attributes
[j
].name
= msg
->elements
[j
].name
;
657 ent
->attributes
[j
].num_values
= 0;
658 ent
->attributes
[j
].values
= NULL
;
659 if (ctx
->attributesonly
&& (msg
->elements
[j
].num_values
== 0)) {
662 ent
->attributes
[j
].num_values
= msg
->elements
[j
].num_values
;
663 ent
->attributes
[j
].values
= msg
->elements
[j
].values
;
667 const struct ldb_control
668 *ctrl
= ldb_controls_get_control(
670 DSDB_CONTROL_GMSA_UPDATE_OID
);
673 const struct gmsa_update
**updates
= NULL
;
674 const size_t len
= talloc_array_length(
677 updates
= talloc_realloc(
680 const struct gmsa_update
*,
682 if (updates
!= NULL
) {
683 updates
[len
] = talloc_steal(updates
,
685 ctx
->updates
= updates
;
691 status
= ldapsrv_queue_reply(call
, ent_r
);
692 if (NT_STATUS_EQUAL(status
, NT_STATUS_FILE_TOO_LARGE
)) {
693 ret
= ldb_request_done(req
,
694 LDB_ERR_SIZE_LIMIT_EXCEEDED
);
695 ldb_asprintf_errstring(ldb
,
696 "LDAP search response size "
697 "limited to %zu bytes\n",
698 LDAP_SERVER_MAX_REPLY_SIZE
);
699 } else if (!NT_STATUS_IS_OK(status
)) {
700 ret
= ldb_request_done(req
,
707 case LDB_REPLY_REFERRAL
:
709 struct ldap_SearchResRef
*ent_ref
;
712 * TODO: This should be handled by the notification
715 if (call
->notification
.busy
) {
720 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultReference
);
726 * Put the LDAP referral data under ent_r->msg
727 * so we can free that later once encoded
729 talloc_steal(ent_r
->msg
, ares
->referral
);
731 ent_ref
= &ent_r
->msg
->r
.SearchResultReference
;
732 ent_ref
->referral
= ares
->referral
;
734 status
= ldapsrv_queue_reply(call
, ent_r
);
735 if (!NT_STATUS_IS_OK(status
)) {
736 ret
= LDB_ERR_OPERATIONS_ERROR
;
745 * We don't queue the reply for this one, we let that
748 ctx
->controls
= talloc_move(ctx
, &ares
->controls
);
751 return ldb_request_done(req
, LDB_SUCCESS
);
755 ret
= LDB_ERR_OPERATIONS_ERROR
;
763 static NTSTATUS
ldapsrv_SearchRequest(struct ldapsrv_call
*call
)
765 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
766 struct ldap_Result
*done
;
767 struct ldapsrv_reply
*done_r
;
768 TALLOC_CTX
*local_ctx
;
769 struct ldapsrv_context
*callback_ctx
= NULL
;
770 struct ldb_context
*samdb
= talloc_get_type(call
->conn
->ldb
, struct ldb_context
);
771 struct ldb_dn
*basedn
;
772 struct ldb_request
*lreq
;
773 struct ldb_control
*search_control
;
774 struct ldb_search_options_control
*search_options
;
775 struct ldb_control
*extended_dn_control
;
776 struct ldb_extended_dn_control
*extended_dn_decoded
= NULL
;
777 struct ldb_control
*notification_control
= NULL
;
778 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
779 const char **attrs
= NULL
;
780 const char *scope_str
, *errstr
= NULL
;
784 int extended_type
= 1;
787 * Warn for searches that are longer than 1/4 of the
788 * search_timeout, being 30sec by default
790 struct timeval start_time
= timeval_current();
791 struct timeval warning_time
792 = timeval_add(&start_time
,
793 call
->conn
->limits
.search_timeout
/ 4,
796 local_ctx
= talloc_new(call
);
797 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
799 basedn
= ldb_dn_new(local_ctx
, samdb
, req
->basedn
);
800 NT_STATUS_HAVE_NO_MEMORY(basedn
);
802 switch (req
->scope
) {
803 case LDAP_SEARCH_SCOPE_BASE
:
804 scope
= LDB_SCOPE_BASE
;
806 case LDAP_SEARCH_SCOPE_SINGLE
:
807 scope
= LDB_SCOPE_ONELEVEL
;
809 case LDAP_SEARCH_SCOPE_SUB
:
810 scope
= LDB_SCOPE_SUBTREE
;
813 result
= LDAP_PROTOCOL_ERROR
;
814 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
816 scope_str
= "<Invalid scope>";
817 errstr
= talloc_asprintf(local_ctx
,
818 "%s. Invalid scope", errstr
);
821 scope_str
= dsdb_search_scope_as_string(scope
);
823 DBG_DEBUG("scope: [%s]\n", scope_str
);
825 if (req
->num_attributes
>= 1) {
826 attrs
= talloc_array(local_ctx
, const char *, req
->num_attributes
+1);
827 NT_STATUS_HAVE_NO_MEMORY(attrs
);
829 for (i
=0; i
< req
->num_attributes
; i
++) {
830 DBG_DEBUG("attrs: [%s]\n",req
->attributes
[i
]);
831 attrs
[i
] = req
->attributes
[i
];
836 DBG_INFO("ldb_request %s dn=%s filter=%s\n",
837 scope_str
, req
->basedn
, ldb_filter_from_tree(call
, req
->tree
));
839 callback_ctx
= talloc_zero(local_ctx
, struct ldapsrv_context
);
840 NT_STATUS_HAVE_NO_MEMORY(callback_ctx
);
841 callback_ctx
->call
= call
;
842 callback_ctx
->extended_type
= extended_type
;
843 callback_ctx
->attributesonly
= req
->attributesonly
;
845 ldb_ret
= ldb_build_search_req_ex(&lreq
, samdb
, local_ctx
,
848 call
->request
->controls
,
850 ldap_server_search_callback
,
853 if (ldb_ret
!= LDB_SUCCESS
) {
857 if (call
->conn
->global_catalog
) {
858 search_control
= ldb_request_get_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
860 search_options
= NULL
;
861 if (search_control
) {
862 search_options
= talloc_get_type(search_control
->data
, struct ldb_search_options_control
);
863 search_options
->search_options
|= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
865 search_options
= talloc(lreq
, struct ldb_search_options_control
);
866 NT_STATUS_HAVE_NO_MEMORY(search_options
);
867 search_options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
868 ldb_request_add_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
, false, search_options
);
871 ldb_request_add_control(lreq
, DSDB_CONTROL_NO_GLOBAL_CATALOG
, false, NULL
);
874 extended_dn_control
= ldb_request_get_control(lreq
, LDB_CONTROL_EXTENDED_DN_OID
);
876 if (extended_dn_control
) {
877 if (extended_dn_control
->data
) {
878 extended_dn_decoded
= talloc_get_type(extended_dn_control
->data
, struct ldb_extended_dn_control
);
879 extended_type
= extended_dn_decoded
->type
;
883 callback_ctx
->extended_type
= extended_type
;
886 notification_control
= ldb_request_get_control(lreq
, LDB_CONTROL_NOTIFICATION_OID
);
887 if (notification_control
!= NULL
) {
888 const struct ldapsrv_call
*pc
= NULL
;
891 for (pc
= call
->conn
->pending_calls
; pc
!= NULL
; pc
= pc
->next
) {
895 if (count
>= call
->conn
->limits
.max_notifications
) {
896 DBG_DEBUG("error MaxNotificationPerConn\n");
897 result
= map_ldb_error(local_ctx
,
898 LDB_ERR_ADMIN_LIMIT_EXCEEDED
,
899 "MaxNotificationPerConn reached",
905 * For now we need to do periodic retries on our own.
906 * As the dsdb_notification module will return after each run.
908 call
->notification
.busy
= true;
912 const char *scheme
= NULL
;
913 switch (call
->conn
->referral_scheme
) {
914 case LDAP_REFERRAL_SCHEME_LDAPS
:
920 ldb_ret
= ldb_set_opaque(
922 LDAP_REFERRAL_SCHEME_OPAQUE
,
923 discard_const_p(char *, scheme
));
924 if (ldb_ret
!= LDB_SUCCESS
) {
930 time_t timeout
= call
->conn
->limits
.search_timeout
;
933 || (req
->timelimit
!= 0
934 && req
->timelimit
< timeout
))
936 timeout
= req
->timelimit
;
938 ldb_set_timeout(samdb
, lreq
, timeout
);
941 if (!call
->conn
->is_privileged
) {
942 ldb_req_mark_untrusted(lreq
);
945 LDB_REQ_SET_LOCATION(lreq
);
947 ldb_ret
= ldb_request(samdb
, lreq
);
949 if (ldb_ret
!= LDB_SUCCESS
) {
953 ldb_ret
= ldb_wait(lreq
->handle
, LDB_WAIT_ALL
);
955 if (ldb_ret
== LDB_SUCCESS
) {
957 const size_t len
= talloc_array_length(callback_ctx
->updates
);
959 for (n
= 0; n
< len
; ++n
) {
962 ret
= dsdb_update_gmsa_entry_keys(local_ctx
,
964 callback_ctx
->updates
[n
]);
966 /* Ignore the error. */
967 DBG_WARNING("Failed to update keys for Group "
968 "Managed Service Account: %s\n",
973 if (call
->notification
.busy
) {
974 /* Move/Add it to the end */
975 DLIST_DEMOTE(call
->conn
->pending_calls
, call
);
976 call
->notification
.generation
=
977 call
->conn
->service
->notification
.generation
;
979 if (callback_ctx
->count
!= 0) {
980 call
->notification
.generation
+= 1;
981 ldapsrv_notification_retry_setup(call
->conn
->service
,
985 talloc_free(local_ctx
);
993 * This looks like duplicated code - because it is - but
994 * otherwise the work in the parameters will be done
995 * regardless, this way the functions only execute when the
998 * The basedn is re-obtained as a string to escape it
1000 if ((req
->timelimit
== 0 || call
->conn
->limits
.search_timeout
< req
->timelimit
)
1001 && ldb_ret
== LDB_ERR_TIME_LIMIT_EXCEEDED
) {
1002 struct dom_sid_buf sid_buf
;
1003 DBG_WARNING("MaxQueryDuration(%d) timeout exceeded "
1004 "in SearchRequest by %s from %s filter: [%s] "
1007 call
->conn
->limits
.search_timeout
,
1008 dom_sid_str_buf(&call
->conn
->session_info
->security_token
->sids
[0],
1010 tsocket_address_string(call
->conn
->connection
->remote_address
,
1012 ldb_filter_from_tree(call
, req
->tree
),
1013 ldb_dn_get_extended_linearized(call
, basedn
, 1),
1015 for (i
=0; i
< req
->num_attributes
; i
++) {
1016 DBG_WARNING("MaxQueryDuration timeout exceeded attrs: [%s]\n",
1017 req
->attributes
[i
]);
1020 } else if (timeval_expired(&warning_time
)) {
1021 struct dom_sid_buf sid_buf
;
1022 DBG_NOTICE("Long LDAP Query: Duration was %.2fs, "
1023 "MaxQueryDuration(%d)/4 == %d "
1024 "in SearchRequest by %s from %s filter: [%s] "
1028 timeval_elapsed(&start_time
),
1029 call
->conn
->limits
.search_timeout
,
1030 call
->conn
->limits
.search_timeout
/ 4,
1031 dom_sid_str_buf(&call
->conn
->session_info
->security_token
->sids
[0],
1033 tsocket_address_string(call
->conn
->connection
->remote_address
,
1035 ldb_filter_from_tree(call
, req
->tree
),
1036 ldb_dn_get_extended_linearized(call
, basedn
, 1),
1038 ldb_strerror(ldb_ret
));
1039 for (i
=0; i
< req
->num_attributes
; i
++) {
1040 DBG_NOTICE("Long LDAP Query attrs: [%s]\n",
1041 req
->attributes
[i
]);
1044 struct dom_sid_buf sid_buf
;
1045 DBG_INFO("LDAP Query: Duration was %.2fs, "
1046 "SearchRequest by %s from %s filter: [%s] "
1050 timeval_elapsed(&start_time
),
1051 dom_sid_str_buf(&call
->conn
->session_info
->security_token
->sids
[0],
1053 tsocket_address_string(call
->conn
->connection
->remote_address
,
1055 ldb_filter_from_tree(call
, req
->tree
),
1056 ldb_dn_get_extended_linearized(call
, basedn
, 1),
1058 ldb_strerror(ldb_ret
));
1061 DLIST_REMOVE(call
->conn
->pending_calls
, call
);
1062 call
->notification
.busy
= false;
1064 done_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultDone
);
1065 NT_STATUS_HAVE_NO_MEMORY(done_r
);
1067 done
= &done_r
->msg
->r
.SearchResultDone
;
1069 done
->referral
= NULL
;
1072 } else if (ldb_ret
== LDB_SUCCESS
) {
1073 if (callback_ctx
->controls
) {
1074 done_r
->msg
->controls
= callback_ctx
->controls
;
1075 talloc_steal(done_r
->msg
, callback_ctx
->controls
);
1077 result
= LDB_SUCCESS
;
1079 DBG_DEBUG("error\n");
1080 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1084 done
->resultcode
= result
;
1085 done
->errormessage
= (errstr
?talloc_strdup(done_r
, errstr
):NULL
);
1087 talloc_free(local_ctx
);
1089 return ldapsrv_queue_reply_forced(call
, done_r
);
1092 static NTSTATUS
ldapsrv_ModifyRequest(struct ldapsrv_call
*call
)
1094 struct ldap_ModifyRequest
*req
= &call
->request
->r
.ModifyRequest
;
1095 struct ldap_Result
*modify_result
;
1096 struct ldapsrv_reply
*modify_reply
;
1097 TALLOC_CTX
*local_ctx
;
1098 struct ldb_context
*samdb
= call
->conn
->ldb
;
1099 struct ldb_message
*msg
= NULL
;
1101 const char *errstr
= NULL
;
1102 int result
= LDAP_SUCCESS
;
1105 struct ldb_result
*res
= NULL
;
1107 DBG_DEBUG("dn: %s\n", req
->dn
);
1109 local_ctx
= talloc_named(call
, 0, "ModifyRequest local memory context");
1110 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1112 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1113 NT_STATUS_HAVE_NO_MEMORY(dn
);
1115 DBG_DEBUG("dn: [%s]\n", req
->dn
);
1117 msg
= ldb_msg_new(local_ctx
);
1118 NT_STATUS_HAVE_NO_MEMORY(msg
);
1122 if (req
->num_mods
> 0) {
1123 msg
->num_elements
= req
->num_mods
;
1124 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, req
->num_mods
);
1125 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
1127 for (i
=0; i
< msg
->num_elements
; i
++) {
1128 msg
->elements
[i
].name
= discard_const_p(char, req
->mods
[i
].attrib
.name
);
1129 msg
->elements
[i
].num_values
= 0;
1130 msg
->elements
[i
].values
= NULL
;
1132 switch (req
->mods
[i
].type
) {
1134 result
= LDAP_PROTOCOL_ERROR
;
1135 map_ldb_error(local_ctx
,
1136 LDB_ERR_PROTOCOL_ERROR
, NULL
, &errstr
);
1137 errstr
= talloc_asprintf(local_ctx
,
1138 "%s. Invalid LDAP_MODIFY_* type", errstr
);
1140 case LDAP_MODIFY_ADD
:
1141 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
1143 case LDAP_MODIFY_DELETE
:
1144 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
1146 case LDAP_MODIFY_REPLACE
:
1147 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
1151 msg
->elements
[i
].num_values
= req
->mods
[i
].attrib
.num_values
;
1152 if (msg
->elements
[i
].num_values
> 0) {
1153 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
1154 msg
->elements
[i
].num_values
);
1155 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
1157 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
1158 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
1159 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
1166 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
1167 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
1169 if (result
== LDAP_SUCCESS
) {
1170 res
= talloc_zero(local_ctx
, struct ldb_result
);
1171 NT_STATUS_HAVE_NO_MEMORY(res
);
1172 ldb_ret
= ldapsrv_mod_with_controls(call
, msg
, call
->request
->controls
, res
);
1173 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1177 modify_result
= &modify_reply
->msg
->r
.ModifyResponse
;
1178 modify_result
->dn
= NULL
;
1179 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
1180 modify_result
->resultcode
= map_ldb_error(local_ctx
,
1183 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
1184 modify_result
->referral
= talloc_strdup(call
, *res
->refs
);
1186 modify_result
->resultcode
= result
;
1187 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
1188 modify_result
->referral
= NULL
;
1190 talloc_free(local_ctx
);
1192 return ldapsrv_queue_reply(call
, modify_reply
);
1196 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
1198 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
1199 struct ldap_Result
*add_result
;
1200 struct ldapsrv_reply
*add_reply
;
1201 TALLOC_CTX
*local_ctx
;
1202 struct ldb_context
*samdb
= call
->conn
->ldb
;
1203 struct ldb_message
*msg
= NULL
;
1205 const char *errstr
= NULL
;
1206 int result
= LDAP_SUCCESS
;
1209 struct ldb_result
*res
= NULL
;
1211 DBG_DEBUG("dn: %s\n", req
->dn
);
1213 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
1214 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1216 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1217 NT_STATUS_HAVE_NO_MEMORY(dn
);
1219 DBG_DEBUG("dn: [%s]\n", req
->dn
);
1221 msg
= talloc(local_ctx
, struct ldb_message
);
1222 NT_STATUS_HAVE_NO_MEMORY(msg
);
1225 msg
->num_elements
= 0;
1226 msg
->elements
= NULL
;
1228 if (req
->num_attributes
> 0) {
1229 msg
->num_elements
= req
->num_attributes
;
1230 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
1231 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
1233 for (i
=0; i
< msg
->num_elements
; i
++) {
1234 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
1235 msg
->elements
[i
].flags
= 0;
1236 msg
->elements
[i
].num_values
= 0;
1237 msg
->elements
[i
].values
= NULL
;
1239 if (req
->attributes
[i
].num_values
> 0) {
1240 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
1241 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
1242 msg
->elements
[i
].num_values
);
1243 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
1245 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
1246 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
1247 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
1253 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
1254 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
1256 if (result
== LDAP_SUCCESS
) {
1257 res
= talloc_zero(local_ctx
, struct ldb_result
);
1258 NT_STATUS_HAVE_NO_MEMORY(res
);
1259 ldb_ret
= ldapsrv_add_with_controls(call
, msg
, call
->request
->controls
, res
);
1260 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1264 add_result
= &add_reply
->msg
->r
.AddResponse
;
1265 add_result
->dn
= NULL
;
1266 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
1267 add_result
->resultcode
= map_ldb_error(local_ctx
,
1268 LDB_ERR_REFERRAL
, NULL
,
1270 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
1271 add_result
->referral
= talloc_strdup(call
, *res
->refs
);
1273 add_result
->resultcode
= result
;
1274 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
1275 add_result
->referral
= NULL
;
1277 talloc_free(local_ctx
);
1279 return ldapsrv_queue_reply(call
, add_reply
);
1283 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
1285 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
1286 struct ldap_Result
*del_result
;
1287 struct ldapsrv_reply
*del_reply
;
1288 TALLOC_CTX
*local_ctx
;
1289 struct ldb_context
*samdb
= call
->conn
->ldb
;
1291 const char *errstr
= NULL
;
1292 int result
= LDAP_SUCCESS
;
1294 struct ldb_result
*res
= NULL
;
1296 DBG_DEBUG("dn: %s\n", req
->dn
);
1298 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
1299 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1301 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1302 NT_STATUS_HAVE_NO_MEMORY(dn
);
1304 DBG_DEBUG("dn: [%s]\n", req
->dn
);
1306 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
1307 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
1309 if (result
== LDAP_SUCCESS
) {
1310 res
= talloc_zero(local_ctx
, struct ldb_result
);
1311 NT_STATUS_HAVE_NO_MEMORY(res
);
1312 ldb_ret
= ldapsrv_del_with_controls(call
, dn
, call
->request
->controls
, res
);
1313 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1317 del_result
= &del_reply
->msg
->r
.DelResponse
;
1318 del_result
->dn
= NULL
;
1319 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
1320 del_result
->resultcode
= map_ldb_error(local_ctx
,
1321 LDB_ERR_REFERRAL
, NULL
,
1323 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
1324 del_result
->referral
= talloc_strdup(call
, *res
->refs
);
1326 del_result
->resultcode
= result
;
1327 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
1328 del_result
->referral
= NULL
;
1331 talloc_free(local_ctx
);
1333 return ldapsrv_queue_reply(call
, del_reply
);
1336 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
1338 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
1339 struct ldap_Result
*modifydn
;
1340 struct ldapsrv_reply
*modifydn_r
;
1341 TALLOC_CTX
*local_ctx
;
1342 struct ldb_context
*samdb
= call
->conn
->ldb
;
1343 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
1344 struct ldb_dn
*parentdn
= NULL
;
1345 const char *errstr
= NULL
;
1346 int result
= LDAP_SUCCESS
;
1348 struct ldb_result
*res
= NULL
;
1350 DBG_DEBUG("dn: %s newrdn: %s\n",
1351 req
->dn
, req
->newrdn
);
1353 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
1354 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1356 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1357 NT_STATUS_HAVE_NO_MEMORY(olddn
);
1359 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
1360 NT_STATUS_HAVE_NO_MEMORY(newrdn
);
1362 DBG_DEBUG("olddn: [%s] newrdn: [%s]\n",
1363 req
->dn
, req
->newrdn
);
1365 if (ldb_dn_get_comp_num(newrdn
) == 0) {
1366 result
= LDAP_PROTOCOL_ERROR
;
1367 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
1372 if (ldb_dn_get_comp_num(newrdn
) > 1) {
1373 result
= LDAP_NAMING_VIOLATION
;
1374 map_ldb_error(local_ctx
, LDB_ERR_NAMING_VIOLATION
, NULL
,
1379 /* we can't handle the rename if we should not remove the old dn */
1380 if (!req
->deleteolddn
) {
1381 result
= LDAP_UNWILLING_TO_PERFORM
;
1382 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, NULL
,
1384 errstr
= talloc_asprintf(local_ctx
,
1385 "%s. Old RDN must be deleted", errstr
);
1389 if (req
->newsuperior
) {
1390 DBG_DEBUG("newsuperior: [%s]\n", req
->newsuperior
);
1391 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
1395 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
1398 result
= LDAP_NO_SUCH_OBJECT
;
1399 map_ldb_error(local_ctx
, LDB_ERR_NO_SUCH_OBJECT
, NULL
, &errstr
);
1403 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
1404 result
= LDAP_OTHER
;
1405 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
1411 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
1412 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
1414 if (result
== LDAP_SUCCESS
) {
1415 res
= talloc_zero(local_ctx
, struct ldb_result
);
1416 NT_STATUS_HAVE_NO_MEMORY(res
);
1417 ldb_ret
= ldapsrv_rename_with_controls(call
, olddn
, newdn
, call
->request
->controls
, res
);
1418 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1422 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
1423 modifydn
->dn
= NULL
;
1424 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
1425 modifydn
->resultcode
= map_ldb_error(local_ctx
,
1426 LDB_ERR_REFERRAL
, NULL
,
1428 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1429 modifydn
->referral
= talloc_strdup(call
, *res
->refs
);
1431 modifydn
->resultcode
= result
;
1432 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1433 modifydn
->referral
= NULL
;
1436 talloc_free(local_ctx
);
1438 return ldapsrv_queue_reply(call
, modifydn_r
);
1441 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
1443 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
1444 struct ldap_Result
*compare
;
1445 struct ldapsrv_reply
*compare_r
;
1446 TALLOC_CTX
*local_ctx
;
1447 struct ldb_context
*samdb
= call
->conn
->ldb
;
1448 struct ldb_result
*res
= NULL
;
1450 const char *attrs
[1];
1451 const char *errstr
= NULL
;
1452 const char *filter
= NULL
;
1453 int result
= LDAP_SUCCESS
;
1456 DBG_DEBUG("dn: %s\n", req
->dn
);
1458 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
1459 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1461 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1462 NT_STATUS_HAVE_NO_MEMORY(dn
);
1464 DBG_DEBUG("dn: [%s]\n", req
->dn
);
1465 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
1466 (int)req
->value
.length
, req
->value
.data
);
1467 NT_STATUS_HAVE_NO_MEMORY(filter
);
1469 DBG_DEBUG("attribute: [%s]\n", filter
);
1473 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
1474 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
1476 if (result
== LDAP_SUCCESS
) {
1477 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
1478 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
1479 if (ldb_ret
!= LDB_SUCCESS
) {
1480 result
= map_ldb_error(local_ctx
, ldb_ret
,
1481 ldb_errstring(samdb
), &errstr
);
1482 DBG_DEBUG("error: %s\n", errstr
);
1483 } else if (res
->count
== 0) {
1484 DBG_DEBUG("didn't match\n");
1485 result
= LDAP_COMPARE_FALSE
;
1487 } else if (res
->count
== 1) {
1488 DBG_DEBUG("matched\n");
1489 result
= LDAP_COMPARE_TRUE
;
1491 } else if (res
->count
> 1) {
1492 result
= LDAP_OTHER
;
1493 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
1494 errstr
= talloc_asprintf(local_ctx
,
1495 "%s. Too many objects match!", errstr
);
1496 DBG_DEBUG("%u results: %s\n", res
->count
, errstr
);
1500 compare
= &compare_r
->msg
->r
.CompareResponse
;
1502 compare
->resultcode
= result
;
1503 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
1504 compare
->referral
= NULL
;
1506 talloc_free(local_ctx
);
1508 return ldapsrv_queue_reply(call
, compare_r
);
1511 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
1513 struct ldap_AbandonRequest
*req
= &call
->request
->r
.AbandonRequest
;
1514 struct ldapsrv_call
*c
= NULL
;
1515 struct ldapsrv_call
*n
= NULL
;
1517 DBG_DEBUG("abandoned\n");
1519 for (c
= call
->conn
->pending_calls
; c
!= NULL
; c
= n
) {
1522 if (c
->request
->messageid
!= req
->messageid
) {
1526 DLIST_REMOVE(call
->conn
->pending_calls
, c
);
1530 return NT_STATUS_OK
;
1533 static NTSTATUS
ldapsrv_expired(struct ldapsrv_call
*call
)
1535 struct ldapsrv_reply
*reply
= NULL
;
1536 struct ldap_ExtendedResponse
*r
= NULL
;
1538 DBG_DEBUG("Sending connection expired message\n");
1540 reply
= ldapsrv_init_reply(call
, LDAP_TAG_ExtendedResponse
);
1541 if (reply
== NULL
) {
1542 return NT_STATUS_NO_MEMORY
;
1546 * According to RFC4511 section 4.4.1 this has a msgid of 0
1548 reply
->msg
->messageid
= 0;
1550 r
= &reply
->msg
->r
.ExtendedResponse
;
1551 r
->response
.resultcode
= LDB_ERR_UNAVAILABLE
;
1552 r
->response
.errormessage
= "The server has timed out this connection";
1553 r
->oid
= "1.3.6.1.4.1.1466.20036"; /* see rfc4511 section 4.4.1 */
1555 ldapsrv_queue_reply(call
, reply
);
1556 return NT_STATUS_OK
;
1559 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
1562 struct ldap_message
*msg
= call
->request
;
1563 struct ldapsrv_connection
*conn
= call
->conn
;
1567 expired
= timeval_expired(&conn
->limits
.expire_time
);
1569 status
= ldapsrv_expired(call
);
1570 if (!NT_STATUS_IS_OK(status
)) {
1573 return NT_STATUS_NETWORK_SESSION_EXPIRED
;
1576 /* Check for undecoded critical extensions */
1577 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
1578 if (!msg
->controls_decoded
[i
] &&
1579 msg
->controls
[i
]->critical
) {
1580 DBG_NOTICE("Critical extension %s is not known to this server\n",
1581 msg
->controls
[i
]->oid
);
1582 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
1586 if (call
->conn
->authz_logged
== false) {
1590 * We do not want to log anonymous access if the query
1591 * is just for the rootDSE, or it is a startTLS or a
1594 * A rootDSE search could also be done over
1595 * CLDAP anonymously for example, so these don't
1597 * Essentially we want to know about
1598 * access beyond that normally done prior to a
1602 switch(call
->request
->type
) {
1603 case LDAP_TAG_BindRequest
:
1604 case LDAP_TAG_UnbindRequest
:
1605 case LDAP_TAG_AbandonRequest
:
1608 case LDAP_TAG_ExtendedResponse
: {
1609 struct ldap_ExtendedRequest
*req
= &call
->request
->r
.ExtendedRequest
;
1610 if (strcmp(req
->oid
, LDB_EXTENDED_START_TLS_OID
) == 0) {
1615 case LDAP_TAG_SearchRequest
: {
1616 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
1617 if (req
->scope
== LDAP_SEARCH_SCOPE_BASE
) {
1618 if (req
->basedn
[0] == '\0') {
1629 const char *transport_protection
= AUTHZ_TRANSPORT_PROTECTION_NONE
;
1630 if (call
->conn
->sockets
.active
== call
->conn
->sockets
.tls
) {
1631 transport_protection
= AUTHZ_TRANSPORT_PROTECTION_TLS
;
1634 log_successful_authz_event(call
->conn
->connection
->msg_ctx
,
1635 call
->conn
->connection
->lp_ctx
,
1636 call
->conn
->connection
->remote_address
,
1637 call
->conn
->connection
->local_address
,
1640 transport_protection
,
1641 call
->conn
->session_info
,
1642 NULL
/* client_audit_info */,
1643 NULL
/* server_audit_info */);
1645 call
->conn
->authz_logged
= true;
1649 switch(call
->request
->type
) {
1650 case LDAP_TAG_BindRequest
:
1651 return ldapsrv_BindRequest(call
);
1652 case LDAP_TAG_UnbindRequest
:
1653 return ldapsrv_UnbindRequest(call
);
1654 case LDAP_TAG_SearchRequest
:
1655 return ldapsrv_SearchRequest(call
);
1656 case LDAP_TAG_ModifyRequest
:
1657 status
= ldapsrv_ModifyRequest(call
);
1659 case LDAP_TAG_AddRequest
:
1660 status
= ldapsrv_AddRequest(call
);
1662 case LDAP_TAG_DelRequest
:
1663 status
= ldapsrv_DelRequest(call
);
1665 case LDAP_TAG_ModifyDNRequest
:
1666 status
= ldapsrv_ModifyDNRequest(call
);
1668 case LDAP_TAG_CompareRequest
:
1669 return ldapsrv_CompareRequest(call
);
1670 case LDAP_TAG_AbandonRequest
:
1671 return ldapsrv_AbandonRequest(call
);
1672 case LDAP_TAG_ExtendedRequest
:
1673 status
= ldapsrv_ExtendedRequest(call
);
1676 return ldapsrv_unwilling(call
, LDAP_PROTOCOL_ERROR
);
1679 if (NT_STATUS_IS_OK(status
)) {
1680 ldapsrv_notification_retry_setup(call
->conn
->service
, true);