2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2004
5 Copyright (C) Matthias Dieter Wallnöfer 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "ldap_server/ldap_server.h"
23 #include "../lib/util/dlinklist.h"
24 #include "auth/credentials/credentials.h"
25 #include "auth/gensec/gensec.h"
26 #include "param/param.h"
27 #include "smbd/service_stream.h"
28 #include "dsdb/samdb/samdb.h"
29 #include <ldb_errors.h>
30 #include <ldb_module.h>
33 static int map_ldb_error(TALLOC_CTX
*mem_ctx
, int ldb_err
,
34 const char *add_err_string
, const char **errstring
)
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
) {
42 strtol(add_err_string
, &endptr
, 16);
43 if (endptr
!= add_err_string
) {
44 *errstring
= add_err_string
;
49 /* Otherwise we calculate here a generic, but appropriate WERROR. */
55 case LDB_ERR_OPERATIONS_ERROR
:
56 err
= WERR_DS_OPERATIONS_ERROR
;
58 case LDB_ERR_PROTOCOL_ERROR
:
59 err
= WERR_DS_PROTOCOL_ERROR
;
61 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
62 err
= WERR_DS_TIMELIMIT_EXCEEDED
;
64 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
65 err
= WERR_DS_SIZELIMIT_EXCEEDED
;
67 case LDB_ERR_COMPARE_FALSE
:
68 err
= WERR_DS_COMPARE_FALSE
;
70 case LDB_ERR_COMPARE_TRUE
:
71 err
= WERR_DS_COMPARE_TRUE
;
73 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
74 err
= WERR_DS_AUTH_METHOD_NOT_SUPPORTED
;
76 case LDB_ERR_STRONG_AUTH_REQUIRED
:
77 err
= WERR_DS_STRONG_AUTH_REQUIRED
;
79 case LDB_ERR_REFERRAL
:
80 err
= WERR_DS_REFERRAL
;
82 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
83 err
= WERR_DS_ADMIN_LIMIT_EXCEEDED
;
85 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
86 err
= WERR_DS_UNAVAILABLE_CRIT_EXTENSION
;
88 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
89 err
= WERR_DS_CONFIDENTIALITY_REQUIRED
;
91 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
94 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
95 err
= WERR_DS_NO_ATTRIBUTE_OR_VALUE
;
97 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
98 err
= WERR_DS_ATTRIBUTE_TYPE_UNDEFINED
;
100 case LDB_ERR_INAPPROPRIATE_MATCHING
:
101 err
= WERR_DS_INAPPROPRIATE_MATCHING
;
103 case LDB_ERR_CONSTRAINT_VIOLATION
:
104 err
= WERR_DS_CONSTRAINT_VIOLATION
;
106 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
107 err
= WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS
;
109 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
110 err
= WERR_DS_INVALID_ATTRIBUTE_SYNTAX
;
112 case LDB_ERR_NO_SUCH_OBJECT
:
113 err
= WERR_DS_NO_SUCH_OBJECT
;
115 case LDB_ERR_ALIAS_PROBLEM
:
116 err
= WERR_DS_ALIAS_PROBLEM
;
118 case LDB_ERR_INVALID_DN_SYNTAX
:
119 err
= WERR_DS_INVALID_DN_SYNTAX
;
121 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
122 err
= WERR_DS_ALIAS_DEREF_PROBLEM
;
124 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
125 err
= WERR_DS_INAPPROPRIATE_AUTH
;
127 case LDB_ERR_INVALID_CREDENTIALS
:
128 err
= WERR_ACCESS_DENIED
;
130 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
131 err
= WERR_DS_INSUFF_ACCESS_RIGHTS
;
136 case LDB_ERR_UNAVAILABLE
:
137 err
= WERR_DS_UNAVAILABLE
;
139 case LDB_ERR_UNWILLING_TO_PERFORM
:
140 err
= WERR_DS_UNWILLING_TO_PERFORM
;
142 case LDB_ERR_LOOP_DETECT
:
143 err
= WERR_DS_LOOP_DETECT
;
145 case LDB_ERR_NAMING_VIOLATION
:
146 err
= WERR_DS_NAMING_VIOLATION
;
148 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
149 err
= WERR_DS_OBJ_CLASS_VIOLATION
;
151 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
152 err
= WERR_DS_CANT_ON_NON_LEAF
;
154 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
155 err
= WERR_DS_CANT_ON_RDN
;
157 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
158 err
= WERR_DS_OBJ_STRING_NAME_EXISTS
;
160 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
161 err
= WERR_DS_CANT_MOD_OBJ_CLASS
;
163 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
164 err
= WERR_DS_AFFECTS_MULTIPLE_DSAS
;
167 err
= WERR_DS_GENERIC_ERROR
;
171 *errstring
= talloc_asprintf(mem_ctx
, "%08X: %s", W_ERROR_V(err
),
172 ldb_strerror(ldb_err
));
173 if (add_err_string
!= NULL
) {
174 *errstring
= talloc_asprintf(mem_ctx
, "%s - %s", *errstring
,
178 /* result is 1:1 for now */
183 connect to the sam database
185 NTSTATUS
ldapsrv_backend_Init(struct ldapsrv_connection
*conn
)
187 conn
->ldb
= samdb_connect(conn
,
188 conn
->connection
->event
.ctx
,
191 conn
->global_catalog
? LDB_FLG_RDONLY
: 0);
192 if (conn
->ldb
== NULL
) {
193 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
196 if (conn
->server_credentials
) {
197 char **sasl_mechs
= NULL
;
198 struct gensec_security_ops
**backends
= gensec_security_all();
199 struct gensec_security_ops
**ops
200 = gensec_use_kerberos_mechs(conn
, backends
, conn
->server_credentials
);
201 unsigned int i
, j
= 0;
202 for (i
= 0; ops
&& ops
[i
]; i
++) {
203 if (!lpcfg_parm_bool(conn
->lp_ctx
, NULL
, "gensec", ops
[i
]->name
, ops
[i
]->enabled
))
206 if (ops
[i
]->sasl_name
&& ops
[i
]->server_start
) {
207 char *sasl_name
= talloc_strdup(conn
, ops
[i
]->sasl_name
);
210 return NT_STATUS_NO_MEMORY
;
212 sasl_mechs
= talloc_realloc(conn
, sasl_mechs
, char *, j
+ 2);
214 return NT_STATUS_NO_MEMORY
;
216 sasl_mechs
[j
] = sasl_name
;
217 talloc_steal(sasl_mechs
, sasl_name
);
218 sasl_mechs
[j
+1] = NULL
;
222 talloc_unlink(conn
, ops
);
224 /* ldb can have a different lifetime to conn, so we
225 need to ensure that sasl_mechs lives as long as the
227 talloc_steal(conn
->ldb
, sasl_mechs
);
229 ldb_set_opaque(conn
->ldb
, "supportedSASLMechanisms", sasl_mechs
);
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
,
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 ret
= ldb_transaction_start(ldb
);
309 if (ret
!= LDB_SUCCESS
) {
313 if (!call
->conn
->is_privileged
) {
314 ldb_req_mark_untrusted(req
);
317 LDB_REQ_SET_LOCATION(req
);
319 ret
= ldb_request(ldb
, req
);
320 if (ret
== LDB_SUCCESS
) {
321 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
324 if (ret
== LDB_SUCCESS
) {
325 ret
= ldb_transaction_commit(ldb
);
328 ldb_transaction_cancel(ldb
);
335 /* create and execute a modify request */
336 static int ldapsrv_mod_with_controls(struct ldapsrv_call
*call
,
337 const struct ldb_message
*message
,
338 struct ldb_control
**controls
,
341 struct ldb_context
*ldb
= call
->conn
->ldb
;
342 struct ldb_request
*req
;
345 ret
= ldb_msg_sanity_check(ldb
, message
);
346 if (ret
!= LDB_SUCCESS
) {
350 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
354 ldb_modify_default_callback
,
357 if (ret
!= LDB_SUCCESS
) {
361 ret
= ldb_transaction_start(ldb
);
362 if (ret
!= LDB_SUCCESS
) {
366 if (!call
->conn
->is_privileged
) {
367 ldb_req_mark_untrusted(req
);
370 LDB_REQ_SET_LOCATION(req
);
372 ret
= ldb_request(ldb
, req
);
373 if (ret
== LDB_SUCCESS
) {
374 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
377 if (ret
== LDB_SUCCESS
) {
378 ret
= ldb_transaction_commit(ldb
);
381 ldb_transaction_cancel(ldb
);
388 /* create and execute a delete request */
389 static int ldapsrv_del_with_controls(struct ldapsrv_call
*call
,
391 struct ldb_control
**controls
,
394 struct ldb_context
*ldb
= call
->conn
->ldb
;
395 struct ldb_request
*req
;
398 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
402 ldb_modify_default_callback
,
405 if (ret
!= LDB_SUCCESS
) return ret
;
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 static int ldapsrv_rename_with_controls(struct ldapsrv_call
*call
,
435 struct ldb_dn
*olddn
,
436 struct ldb_dn
*newdn
,
437 struct ldb_control
**controls
,
440 struct ldb_context
*ldb
= call
->conn
->ldb
;
441 struct ldb_request
*req
;
444 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
449 ldb_modify_default_callback
,
452 if (ret
!= LDB_SUCCESS
) return ret
;
454 ret
= ldb_transaction_start(ldb
);
455 if (ret
!= LDB_SUCCESS
) {
459 if (!call
->conn
->is_privileged
) {
460 ldb_req_mark_untrusted(req
);
463 LDB_REQ_SET_LOCATION(req
);
465 ret
= ldb_request(ldb
, req
);
466 if (ret
== LDB_SUCCESS
) {
467 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
470 if (ret
== LDB_SUCCESS
) {
471 ret
= ldb_transaction_commit(ldb
);
474 ldb_transaction_cancel(ldb
);
481 static NTSTATUS
ldapsrv_SearchRequest(struct ldapsrv_call
*call
)
483 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
484 struct ldap_SearchResEntry
*ent
;
485 struct ldap_Result
*done
;
486 struct ldapsrv_reply
*ent_r
, *done_r
;
487 TALLOC_CTX
*local_ctx
;
488 struct ldb_context
*samdb
= talloc_get_type(call
->conn
->ldb
, struct ldb_context
);
489 struct ldb_dn
*basedn
;
490 struct ldb_result
*res
= NULL
;
491 struct ldb_request
*lreq
;
492 struct ldb_control
*search_control
;
493 struct ldb_search_options_control
*search_options
;
494 struct ldb_control
*extended_dn_control
;
495 struct ldb_extended_dn_control
*extended_dn_decoded
= NULL
;
496 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
497 const char **attrs
= NULL
;
498 const char *scope_str
, *errstr
= NULL
;
499 int success_limit
= 1;
503 int extended_type
= 1;
505 DEBUG(10, ("SearchRequest"));
506 DEBUGADD(10, (" basedn: %s", req
->basedn
));
507 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call
, req
->tree
)));
509 local_ctx
= talloc_new(call
);
510 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
512 basedn
= ldb_dn_new(local_ctx
, samdb
, req
->basedn
);
513 NT_STATUS_HAVE_NO_MEMORY(basedn
);
515 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req
->basedn
));
516 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call
, req
->tree
)));
518 switch (req
->scope
) {
519 case LDAP_SEARCH_SCOPE_BASE
:
521 scope
= LDB_SCOPE_BASE
;
524 case LDAP_SEARCH_SCOPE_SINGLE
:
526 scope
= LDB_SCOPE_ONELEVEL
;
529 case LDAP_SEARCH_SCOPE_SUB
:
531 scope
= LDB_SCOPE_SUBTREE
;
535 result
= LDAP_PROTOCOL_ERROR
;
536 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
538 errstr
= talloc_asprintf(local_ctx
,
539 "%s. Invalid scope", errstr
);
542 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str
));
544 if (req
->num_attributes
>= 1) {
545 attrs
= talloc_array(local_ctx
, const char *, req
->num_attributes
+1);
546 NT_STATUS_HAVE_NO_MEMORY(attrs
);
548 for (i
=0; i
< req
->num_attributes
; i
++) {
549 DEBUG(10,("SearchRequest: attrs: [%s]\n",req
->attributes
[i
]));
550 attrs
[i
] = req
->attributes
[i
];
555 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
556 scope_str
, req
->basedn
, ldb_filter_from_tree(call
, req
->tree
)));
558 res
= talloc_zero(local_ctx
, struct ldb_result
);
559 NT_STATUS_HAVE_NO_MEMORY(res
);
561 ldb_ret
= ldb_build_search_req_ex(&lreq
, samdb
, local_ctx
,
564 call
->request
->controls
,
565 res
, ldb_search_default_callback
,
568 if (ldb_ret
!= LDB_SUCCESS
) {
572 if (call
->conn
->global_catalog
) {
573 search_control
= ldb_request_get_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
575 search_options
= NULL
;
576 if (search_control
) {
577 search_options
= talloc_get_type(search_control
->data
, struct ldb_search_options_control
);
578 search_options
->search_options
|= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
580 search_options
= talloc(lreq
, struct ldb_search_options_control
);
581 NT_STATUS_HAVE_NO_MEMORY(search_options
);
582 search_options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
583 ldb_request_add_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
, false, search_options
);
587 extended_dn_control
= ldb_request_get_control(lreq
, LDB_CONTROL_EXTENDED_DN_OID
);
589 if (extended_dn_control
) {
590 if (extended_dn_control
->data
) {
591 extended_dn_decoded
= talloc_get_type(extended_dn_control
->data
, struct ldb_extended_dn_control
);
592 extended_type
= extended_dn_decoded
->type
;
598 ldb_set_timeout(samdb
, lreq
, req
->timelimit
);
600 if (!call
->conn
->is_privileged
) {
601 ldb_req_mark_untrusted(lreq
);
604 LDB_REQ_SET_LOCATION(lreq
);
606 ldb_ret
= ldb_request(samdb
, lreq
);
608 if (ldb_ret
!= LDB_SUCCESS
) {
612 ldb_ret
= ldb_wait(lreq
->handle
, LDB_WAIT_ALL
);
614 if (ldb_ret
== LDB_SUCCESS
) {
615 for (i
= 0; i
< res
->count
; i
++) {
616 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultEntry
);
617 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
619 /* Better to have the whole message kept here,
620 * than to find someone further up didn't put
621 * a value in the right spot in the talloc tree */
622 talloc_steal(ent_r
, res
->msgs
[i
]);
624 ent
= &ent_r
->msg
->r
.SearchResultEntry
;
625 ent
->dn
= ldb_dn_get_extended_linearized(ent_r
, res
->msgs
[i
]->dn
, extended_type
);
626 ent
->num_attributes
= 0;
627 ent
->attributes
= NULL
;
628 if (res
->msgs
[i
]->num_elements
== 0) {
631 ent
->num_attributes
= res
->msgs
[i
]->num_elements
;
632 ent
->attributes
= talloc_array(ent_r
, struct ldb_message_element
, ent
->num_attributes
);
633 NT_STATUS_HAVE_NO_MEMORY(ent
->attributes
);
634 for (j
=0; j
< ent
->num_attributes
; j
++) {
635 ent
->attributes
[j
].name
= res
->msgs
[i
]->elements
[j
].name
;
636 ent
->attributes
[j
].num_values
= 0;
637 ent
->attributes
[j
].values
= NULL
;
638 if (req
->attributesonly
&& (res
->msgs
[i
]->elements
[j
].num_values
== 0)) {
641 ent
->attributes
[j
].num_values
= res
->msgs
[i
]->elements
[j
].num_values
;
642 ent
->attributes
[j
].values
= res
->msgs
[i
]->elements
[j
].values
;
645 ldapsrv_queue_reply(call
, ent_r
);
648 /* Send back referrals if they do exist (search operations) */
649 if (res
->refs
!= NULL
) {
651 struct ldap_SearchResRef
*ent_ref
;
653 for (ref
= res
->refs
; *ref
!= NULL
; ++ref
) {
654 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultReference
);
655 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
657 /* Better to have the whole referrals kept here,
658 * than to find someone further up didn't put
659 * a value in the right spot in the talloc tree
661 talloc_steal(ent_r
, *ref
);
663 ent_ref
= &ent_r
->msg
->r
.SearchResultReference
;
664 ent_ref
->referral
= *ref
;
666 ldapsrv_queue_reply(call
, ent_r
);
672 done_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultDone
);
673 NT_STATUS_HAVE_NO_MEMORY(done_r
);
675 done
= &done_r
->msg
->r
.SearchResultDone
;
677 done
->referral
= NULL
;
680 } else if (ldb_ret
== LDB_SUCCESS
) {
681 if (res
->count
>= success_limit
) {
682 DEBUG(10,("SearchRequest: results: [%d]\n", res
->count
));
683 result
= LDAP_SUCCESS
;
687 done_r
->msg
->controls
= res
->controls
;
688 talloc_steal(done_r
, res
->controls
);
691 DEBUG(10,("SearchRequest: error\n"));
692 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
696 done
->resultcode
= result
;
697 done
->errormessage
= (errstr
?talloc_strdup(done_r
, errstr
):NULL
);
699 talloc_free(local_ctx
);
701 ldapsrv_queue_reply(call
, done_r
);
705 static NTSTATUS
ldapsrv_ModifyRequest(struct ldapsrv_call
*call
)
707 struct ldap_ModifyRequest
*req
= &call
->request
->r
.ModifyRequest
;
708 struct ldap_Result
*modify_result
;
709 struct ldapsrv_reply
*modify_reply
;
710 TALLOC_CTX
*local_ctx
;
711 struct ldb_context
*samdb
= call
->conn
->ldb
;
712 struct ldb_message
*msg
= NULL
;
714 const char *errstr
= NULL
;
715 int result
= LDAP_SUCCESS
;
718 struct ldb_result
*res
= NULL
;
720 DEBUG(10, ("ModifyRequest"));
721 DEBUGADD(10, (" dn: %s\n", req
->dn
));
723 local_ctx
= talloc_named(call
, 0, "ModifyRequest local memory context");
724 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
726 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
727 NT_STATUS_HAVE_NO_MEMORY(dn
);
729 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req
->dn
));
731 msg
= talloc(local_ctx
, struct ldb_message
);
732 NT_STATUS_HAVE_NO_MEMORY(msg
);
735 msg
->num_elements
= 0;
736 msg
->elements
= NULL
;
738 if (req
->num_mods
> 0) {
739 msg
->num_elements
= req
->num_mods
;
740 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, req
->num_mods
);
741 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
743 for (i
=0; i
< msg
->num_elements
; i
++) {
744 msg
->elements
[i
].name
= discard_const_p(char, req
->mods
[i
].attrib
.name
);
745 msg
->elements
[i
].num_values
= 0;
746 msg
->elements
[i
].values
= NULL
;
748 switch (req
->mods
[i
].type
) {
750 result
= LDAP_PROTOCOL_ERROR
;
751 map_ldb_error(local_ctx
,
752 LDB_ERR_PROTOCOL_ERROR
, NULL
, &errstr
);
753 errstr
= talloc_asprintf(local_ctx
,
754 "%s. Invalid LDAP_MODIFY_* type", errstr
);
756 case LDAP_MODIFY_ADD
:
757 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
759 case LDAP_MODIFY_DELETE
:
760 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
762 case LDAP_MODIFY_REPLACE
:
763 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
767 msg
->elements
[i
].num_values
= req
->mods
[i
].attrib
.num_values
;
768 if (msg
->elements
[i
].num_values
> 0) {
769 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
770 msg
->elements
[i
].num_values
);
771 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
773 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
774 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
775 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
782 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
783 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
785 if (result
== LDAP_SUCCESS
) {
786 res
= talloc_zero(local_ctx
, struct ldb_result
);
787 NT_STATUS_HAVE_NO_MEMORY(res
);
788 ldb_ret
= ldapsrv_mod_with_controls(call
, msg
, call
->request
->controls
, res
);
789 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
793 modify_result
= &modify_reply
->msg
->r
.ModifyResponse
;
794 modify_result
->dn
= NULL
;
795 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
796 modify_result
->resultcode
= map_ldb_error(local_ctx
,
799 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
800 modify_result
->referral
= talloc_strdup(call
, *res
->refs
);
802 modify_result
->resultcode
= result
;
803 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
804 modify_result
->referral
= NULL
;
806 talloc_free(local_ctx
);
808 ldapsrv_queue_reply(call
, modify_reply
);
813 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
815 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
816 struct ldap_Result
*add_result
;
817 struct ldapsrv_reply
*add_reply
;
818 TALLOC_CTX
*local_ctx
;
819 struct ldb_context
*samdb
= call
->conn
->ldb
;
820 struct ldb_message
*msg
= NULL
;
822 const char *errstr
= NULL
;
823 int result
= LDAP_SUCCESS
;
826 struct ldb_result
*res
= NULL
;
828 DEBUG(10, ("AddRequest"));
829 DEBUGADD(10, (" dn: %s\n", req
->dn
));
831 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
832 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
834 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
835 NT_STATUS_HAVE_NO_MEMORY(dn
);
837 DEBUG(10, ("AddRequest: dn: [%s]\n", req
->dn
));
839 msg
= talloc(local_ctx
, struct ldb_message
);
840 NT_STATUS_HAVE_NO_MEMORY(msg
);
843 msg
->num_elements
= 0;
844 msg
->elements
= NULL
;
846 if (req
->num_attributes
> 0) {
847 msg
->num_elements
= req
->num_attributes
;
848 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
849 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
851 for (i
=0; i
< msg
->num_elements
; i
++) {
852 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
853 msg
->elements
[i
].flags
= 0;
854 msg
->elements
[i
].num_values
= 0;
855 msg
->elements
[i
].values
= NULL
;
857 if (req
->attributes
[i
].num_values
> 0) {
858 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
859 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
860 msg
->elements
[i
].num_values
);
861 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
863 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
864 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
865 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
871 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
872 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
874 if (result
== LDAP_SUCCESS
) {
875 res
= talloc_zero(local_ctx
, struct ldb_result
);
876 NT_STATUS_HAVE_NO_MEMORY(res
);
877 ldb_ret
= ldapsrv_add_with_controls(call
, msg
, call
->request
->controls
, res
);
878 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
882 add_result
= &add_reply
->msg
->r
.AddResponse
;
883 add_result
->dn
= NULL
;
884 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
885 add_result
->resultcode
= map_ldb_error(local_ctx
,
886 LDB_ERR_REFERRAL
, NULL
,
888 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
889 add_result
->referral
= talloc_strdup(call
, *res
->refs
);
891 add_result
->resultcode
= result
;
892 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
893 add_result
->referral
= NULL
;
895 talloc_free(local_ctx
);
897 ldapsrv_queue_reply(call
, add_reply
);
902 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
904 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
905 struct ldap_Result
*del_result
;
906 struct ldapsrv_reply
*del_reply
;
907 TALLOC_CTX
*local_ctx
;
908 struct ldb_context
*samdb
= call
->conn
->ldb
;
910 const char *errstr
= NULL
;
911 int result
= LDAP_SUCCESS
;
913 struct ldb_result
*res
= NULL
;
915 DEBUG(10, ("DelRequest"));
916 DEBUGADD(10, (" dn: %s\n", req
->dn
));
918 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
919 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
921 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
922 NT_STATUS_HAVE_NO_MEMORY(dn
);
924 DEBUG(10, ("DelRequest: dn: [%s]\n", req
->dn
));
926 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
927 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
929 if (result
== LDAP_SUCCESS
) {
930 res
= talloc_zero(local_ctx
, struct ldb_result
);
931 NT_STATUS_HAVE_NO_MEMORY(res
);
932 ldb_ret
= ldapsrv_del_with_controls(call
, dn
, call
->request
->controls
, res
);
933 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
937 del_result
= &del_reply
->msg
->r
.DelResponse
;
938 del_result
->dn
= NULL
;
939 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
940 del_result
->resultcode
= map_ldb_error(local_ctx
,
941 LDB_ERR_REFERRAL
, NULL
,
943 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
944 del_result
->referral
= talloc_strdup(call
, *res
->refs
);
946 del_result
->resultcode
= result
;
947 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
948 del_result
->referral
= NULL
;
951 talloc_free(local_ctx
);
953 ldapsrv_queue_reply(call
, del_reply
);
957 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
959 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
960 struct ldap_Result
*modifydn
;
961 struct ldapsrv_reply
*modifydn_r
;
962 TALLOC_CTX
*local_ctx
;
963 struct ldb_context
*samdb
= call
->conn
->ldb
;
964 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
965 struct ldb_dn
*parentdn
= NULL
;
966 const char *errstr
= NULL
;
967 int result
= LDAP_SUCCESS
;
969 struct ldb_result
*res
= NULL
;
971 DEBUG(10, ("ModifyDNRequest"));
972 DEBUGADD(10, (" dn: %s", req
->dn
));
973 DEBUGADD(10, (" newrdn: %s\n", req
->newrdn
));
975 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
976 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
978 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
979 NT_STATUS_HAVE_NO_MEMORY(olddn
);
981 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
982 NT_STATUS_HAVE_NO_MEMORY(newrdn
);
984 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req
->dn
));
985 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req
->newrdn
));
987 if (ldb_dn_get_comp_num(newrdn
) == 0) {
988 result
= LDAP_PROTOCOL_ERROR
;
989 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
994 if (ldb_dn_get_comp_num(newrdn
) > 1) {
995 result
= LDAP_NAMING_VIOLATION
;
996 map_ldb_error(local_ctx
, LDB_ERR_NAMING_VIOLATION
, NULL
,
1001 /* we can't handle the rename if we should not remove the old dn */
1002 if (!req
->deleteolddn
) {
1003 result
= LDAP_UNWILLING_TO_PERFORM
;
1004 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, NULL
,
1006 errstr
= talloc_asprintf(local_ctx
,
1007 "%s. Old RDN must be deleted", errstr
);
1011 if (req
->newsuperior
) {
1012 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
1013 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
1017 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
1020 result
= LDAP_NO_SUCH_OBJECT
;
1021 map_ldb_error(local_ctx
, LDB_ERR_NO_SUCH_OBJECT
, NULL
, &errstr
);
1025 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
1026 result
= LDAP_OTHER
;
1027 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
1033 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
1034 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
1036 if (result
== LDAP_SUCCESS
) {
1037 res
= talloc_zero(local_ctx
, struct ldb_result
);
1038 NT_STATUS_HAVE_NO_MEMORY(res
);
1039 ldb_ret
= ldapsrv_rename_with_controls(call
, olddn
, newdn
, call
->request
->controls
, res
);
1040 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
1044 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
1045 modifydn
->dn
= NULL
;
1046 if ((res
!= NULL
) && (res
->refs
!= NULL
)) {
1047 modifydn
->resultcode
= map_ldb_error(local_ctx
,
1048 LDB_ERR_REFERRAL
, NULL
,
1050 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1051 modifydn
->referral
= talloc_strdup(call
, *res
->refs
);
1053 modifydn
->resultcode
= result
;
1054 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1055 modifydn
->referral
= NULL
;
1058 talloc_free(local_ctx
);
1060 ldapsrv_queue_reply(call
, modifydn_r
);
1061 return NT_STATUS_OK
;
1064 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
1066 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
1067 struct ldap_Result
*compare
;
1068 struct ldapsrv_reply
*compare_r
;
1069 TALLOC_CTX
*local_ctx
;
1070 struct ldb_context
*samdb
= call
->conn
->ldb
;
1071 struct ldb_result
*res
= NULL
;
1073 const char *attrs
[1];
1074 const char *errstr
= NULL
;
1075 const char *filter
= NULL
;
1076 int result
= LDAP_SUCCESS
;
1079 DEBUG(10, ("CompareRequest"));
1080 DEBUGADD(10, (" dn: %s\n", req
->dn
));
1082 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
1083 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1085 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1086 NT_STATUS_HAVE_NO_MEMORY(dn
);
1088 DEBUG(10, ("CompareRequest: dn: [%s]\n", req
->dn
));
1089 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
1090 (int)req
->value
.length
, req
->value
.data
);
1091 NT_STATUS_HAVE_NO_MEMORY(filter
);
1093 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter
));
1097 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
1098 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
1100 if (result
== LDAP_SUCCESS
) {
1101 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
1102 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
1103 if (ldb_ret
!= LDB_SUCCESS
) {
1104 result
= map_ldb_error(local_ctx
, ldb_ret
,
1105 ldb_errstring(samdb
), &errstr
);
1106 DEBUG(10,("CompareRequest: error: %s\n", errstr
));
1107 } else if (res
->count
== 0) {
1108 DEBUG(10,("CompareRequest: doesn't matched\n"));
1109 result
= LDAP_COMPARE_FALSE
;
1111 } else if (res
->count
== 1) {
1112 DEBUG(10,("CompareRequest: matched\n"));
1113 result
= LDAP_COMPARE_TRUE
;
1115 } else if (res
->count
> 1) {
1116 result
= LDAP_OTHER
;
1117 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
1118 errstr
= talloc_asprintf(local_ctx
,
1119 "%s. Too many objects match!", errstr
);
1120 DEBUG(10,("CompareRequest: %d results: %s\n", res
->count
, errstr
));
1124 compare
= &compare_r
->msg
->r
.CompareResponse
;
1126 compare
->resultcode
= result
;
1127 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
1128 compare
->referral
= NULL
;
1130 talloc_free(local_ctx
);
1132 ldapsrv_queue_reply(call
, compare_r
);
1133 return NT_STATUS_OK
;
1136 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
1138 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
1139 DEBUG(10, ("AbandonRequest\n"));
1140 return NT_STATUS_OK
;
1143 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
1146 struct ldap_message
*msg
= call
->request
;
1147 /* Check for undecoded critical extensions */
1148 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
1149 if (!msg
->controls_decoded
[i
] &&
1150 msg
->controls
[i
]->critical
) {
1151 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1152 msg
->controls
[i
]->oid
));
1153 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
1157 switch(call
->request
->type
) {
1158 case LDAP_TAG_BindRequest
:
1159 return ldapsrv_BindRequest(call
);
1160 case LDAP_TAG_UnbindRequest
:
1161 return ldapsrv_UnbindRequest(call
);
1162 case LDAP_TAG_SearchRequest
:
1163 return ldapsrv_SearchRequest(call
);
1164 case LDAP_TAG_ModifyRequest
:
1165 return ldapsrv_ModifyRequest(call
);
1166 case LDAP_TAG_AddRequest
:
1167 return ldapsrv_AddRequest(call
);
1168 case LDAP_TAG_DelRequest
:
1169 return ldapsrv_DelRequest(call
);
1170 case LDAP_TAG_ModifyDNRequest
:
1171 return ldapsrv_ModifyDNRequest(call
);
1172 case LDAP_TAG_CompareRequest
:
1173 return ldapsrv_CompareRequest(call
);
1174 case LDAP_TAG_AbandonRequest
:
1175 return ldapsrv_AbandonRequest(call
);
1176 case LDAP_TAG_ExtendedRequest
:
1177 return ldapsrv_ExtendedRequest(call
);
1179 return ldapsrv_unwilling(call
, LDAP_PROTOCOL_ERROR
);