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 "libcli/ldap/ldap.h"
25 #include "auth/credentials/credentials.h"
26 #include "auth/gensec/gensec.h"
27 #include "param/param.h"
28 #include "smbd/service_stream.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "lib/ldb_wrap.h"
33 #define VALID_DN_SYNTAX(dn) do {\
35 return NT_STATUS_NO_MEMORY;\
36 } else if ( ! ldb_dn_validate(dn)) {\
37 result = LDAP_INVALID_DN_SYNTAX;\
38 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, &errstr);\
43 static int map_ldb_error(TALLOC_CTX
*mem_ctx
, int ldb_err
,
44 const char **errstring
)
52 case LDB_ERR_OPERATIONS_ERROR
:
53 err
= WERR_DS_OPERATIONS_ERROR
;
55 case LDB_ERR_PROTOCOL_ERROR
:
56 err
= WERR_DS_PROTOCOL_ERROR
;
58 case LDB_ERR_TIME_LIMIT_EXCEEDED
:
59 err
= WERR_DS_TIMELIMIT_EXCEEDED
;
61 case LDB_ERR_SIZE_LIMIT_EXCEEDED
:
62 err
= WERR_DS_SIZE_LIMIT_EXCEEDED
;
64 case LDB_ERR_COMPARE_FALSE
:
65 err
= WERR_DS_COMPARE_FALSE
;
67 case LDB_ERR_COMPARE_TRUE
:
68 err
= WERR_DS_COMPARE_TRUE
;
70 case LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
:
71 err
= WERR_DS_AUTH_METHOD_NOT_SUPPORTED
;
73 case LDB_ERR_STRONG_AUTH_REQUIRED
:
74 err
= WERR_DS_STRONG_AUTH_REQUIRED
;
76 case LDB_ERR_REFERRAL
:
77 err
= WERR_DS_REFERRAL
;
79 case LDB_ERR_ADMIN_LIMIT_EXCEEDED
:
80 err
= WERR_DS_ADMIN_LIMIT_EXCEEDED
;
82 case LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
:
83 err
= WERR_DS_UNAVAILABLE_CRIT_EXTENSION
;
85 case LDB_ERR_CONFIDENTIALITY_REQUIRED
:
86 err
= WERR_DS_CONFIDENTIALITY_REQUIRED
;
88 case LDB_ERR_SASL_BIND_IN_PROGRESS
:
91 case LDB_ERR_NO_SUCH_ATTRIBUTE
:
92 err
= WERR_DS_NO_ATTRIBUTE_OR_VALUE
;
94 case LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
:
95 err
= WERR_DS_ATTRIBUTE_TYPE_UNDEFINED
;
97 case LDB_ERR_INAPPROPRIATE_MATCHING
:
98 err
= WERR_DS_INAPPROPRIATE_MATCHING
;
100 case LDB_ERR_CONSTRAINT_VIOLATION
:
101 err
= WERR_DS_CONSTRAINT_VIOLATION
;
103 case LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
:
104 err
= WERR_DS_ATTRIBUTE_OR_VALUE_EXISTS
;
106 case LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
:
107 err
= WERR_DS_INVALID_ATTRIBUTE_SYNTAX
;
109 case LDB_ERR_NO_SUCH_OBJECT
:
110 err
= WERR_DS_NO_SUCH_OBJECT
;
112 case LDB_ERR_ALIAS_PROBLEM
:
113 err
= WERR_DS_ALIAS_PROBLEM
;
115 case LDB_ERR_INVALID_DN_SYNTAX
:
116 err
= WERR_DS_INVALID_DN_SYNTAX
;
118 case LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
:
119 err
= WERR_DS_ALIAS_DEREF_PROBLEM
;
121 case LDB_ERR_INAPPROPRIATE_AUTHENTICATION
:
122 err
= WERR_DS_INAPPROPRIATE_AUTH
;
124 case LDB_ERR_INVALID_CREDENTIALS
:
125 err
= WERR_ACCESS_DENIED
;
127 case LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
:
128 err
= WERR_DS_INSUFF_ACCESS_RIGHTS
;
133 case LDB_ERR_UNAVAILABLE
:
134 err
= WERR_DS_UNAVAILABLE
;
136 case LDB_ERR_UNWILLING_TO_PERFORM
:
137 err
= WERR_DS_UNWILLING_TO_PERFORM
;
139 case LDB_ERR_LOOP_DETECT
:
140 err
= WERR_DS_LOOP_DETECT
;
142 case LDB_ERR_NAMING_VIOLATION
:
143 err
= WERR_DS_NAMING_VIOLATION
;
145 case LDB_ERR_OBJECT_CLASS_VIOLATION
:
146 err
= WERR_DS_OBJ_CLASS_VIOLATION
;
148 case LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
:
149 err
= WERR_DS_CANT_ON_NON_LEAF
;
151 case LDB_ERR_NOT_ALLOWED_ON_RDN
:
152 err
= WERR_DS_CANT_ON_RDN
;
154 case LDB_ERR_ENTRY_ALREADY_EXISTS
:
155 err
= WERR_DS_OBJ_STRING_NAME_EXISTS
;
157 case LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
:
158 err
= WERR_DS_CANT_MOD_OBJ_CLASS
;
160 case LDB_ERR_AFFECTS_MULTIPLE_DSAS
:
161 err
= WERR_DS_AFFECTS_MULTIPLE_DSAS
;
164 err
= WERR_DS_GENERIC_ERROR
;
168 *errstring
= talloc_asprintf(mem_ctx
, "%08x: %s", W_ERROR_V(err
),
169 ldb_strerror(ldb_err
));
171 /* result is 1:1 for now */
174 /* create and execute a modify request */
175 static int ldb_mod_req_with_controls(struct ldb_context
*ldb
,
176 const struct ldb_message
*message
,
177 struct ldb_control
**controls
)
179 struct ldb_request
*req
;
182 ret
= ldb_msg_sanity_check(ldb
, message
);
183 if (ret
!= LDB_SUCCESS
) {
187 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
191 ldb_op_default_callback
,
194 if (ret
!= LDB_SUCCESS
) {
198 ret
= ldb_transaction_start(ldb
);
199 if (ret
!= LDB_SUCCESS
) {
203 ret
= ldb_request(ldb
, req
);
204 if (ret
== LDB_SUCCESS
) {
205 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
208 if (ret
== LDB_SUCCESS
) {
209 ret
= ldb_transaction_commit(ldb
);
212 ldb_transaction_cancel(ldb
);
220 connect to the sam database
222 NTSTATUS
ldapsrv_backend_Init(struct ldapsrv_connection
*conn
)
224 conn
->ldb
= ldb_wrap_connect(conn
,
225 conn
->connection
->event
.ctx
,
227 lp_sam_url(conn
->lp_ctx
),
229 samdb_credentials(conn
->connection
->event
.ctx
, conn
->lp_ctx
),
230 conn
->global_catalog
? LDB_FLG_RDONLY
: 0);
231 if (conn
->ldb
== NULL
) {
232 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
235 if (conn
->server_credentials
) {
236 char **sasl_mechs
= NULL
;
237 struct gensec_security_ops
**backends
= gensec_security_all();
238 struct gensec_security_ops
**ops
239 = gensec_use_kerberos_mechs(conn
, backends
, conn
->server_credentials
);
241 for (i
= 0; ops
&& ops
[i
]; i
++) {
242 if (!lp_parm_bool(conn
->lp_ctx
, NULL
, "gensec", ops
[i
]->name
, ops
[i
]->enabled
))
245 if (ops
[i
]->sasl_name
&& ops
[i
]->server_start
) {
246 char *sasl_name
= talloc_strdup(conn
, ops
[i
]->sasl_name
);
249 return NT_STATUS_NO_MEMORY
;
251 sasl_mechs
= talloc_realloc(conn
, sasl_mechs
, char *, j
+ 2);
253 return NT_STATUS_NO_MEMORY
;
255 sasl_mechs
[j
] = sasl_name
;
256 talloc_steal(sasl_mechs
, sasl_name
);
257 sasl_mechs
[j
+1] = NULL
;
261 talloc_unlink(conn
, ops
);
262 ldb_set_opaque(conn
->ldb
, "supportedSASLMechanims", sasl_mechs
);
268 struct ldapsrv_reply
*ldapsrv_init_reply(struct ldapsrv_call
*call
, uint8_t type
)
270 struct ldapsrv_reply
*reply
;
272 reply
= talloc(call
, struct ldapsrv_reply
);
276 reply
->msg
= talloc(reply
, struct ldap_message
);
277 if (reply
->msg
== NULL
) {
282 reply
->msg
->messageid
= call
->request
->messageid
;
283 reply
->msg
->type
= type
;
284 reply
->msg
->controls
= NULL
;
289 void ldapsrv_queue_reply(struct ldapsrv_call
*call
, struct ldapsrv_reply
*reply
)
291 DLIST_ADD_END(call
->replies
, reply
, struct ldapsrv_reply
*);
294 static NTSTATUS
ldapsrv_unwilling(struct ldapsrv_call
*call
, int error
)
296 struct ldapsrv_reply
*reply
;
297 struct ldap_ExtendedResponse
*r
;
299 DEBUG(10,("Unwilling type[%d] id[%d]\n", call
->request
->type
, call
->request
->messageid
));
301 reply
= ldapsrv_init_reply(call
, LDAP_TAG_ExtendedResponse
);
303 return NT_STATUS_NO_MEMORY
;
306 r
= &reply
->msg
->r
.ExtendedResponse
;
307 r
->response
.resultcode
= error
;
308 r
->response
.dn
= NULL
;
309 r
->response
.errormessage
= NULL
;
310 r
->response
.referral
= NULL
;
314 ldapsrv_queue_reply(call
, reply
);
318 static NTSTATUS
ldapsrv_SearchRequest(struct ldapsrv_call
*call
)
320 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
321 struct ldap_SearchResEntry
*ent
;
322 struct ldap_Result
*done
;
323 struct ldapsrv_reply
*ent_r
, *done_r
;
324 TALLOC_CTX
*local_ctx
;
325 struct ldb_context
*samdb
= talloc_get_type(call
->conn
->ldb
, struct ldb_context
);
326 struct ldb_dn
*basedn
;
327 struct ldb_result
*res
= NULL
;
328 struct ldb_request
*lreq
;
329 struct ldb_control
*search_control
;
330 struct ldb_search_options_control
*search_options
;
331 struct ldb_control
*extended_dn_control
;
332 struct ldb_extended_dn_control
*extended_dn_decoded
= NULL
;
333 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
334 const char **attrs
= NULL
;
335 const char *scope_str
, *errstr
= NULL
;
336 int success_limit
= 1;
340 int extended_type
= 1;
342 DEBUG(10, ("SearchRequest"));
343 DEBUGADD(10, (" basedn: %s", req
->basedn
));
344 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call
, req
->tree
)));
346 local_ctx
= talloc_new(call
);
347 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
349 basedn
= ldb_dn_new(local_ctx
, samdb
, req
->basedn
);
350 VALID_DN_SYNTAX(basedn
);
352 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req
->basedn
));
353 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call
, req
->tree
)));
355 switch (req
->scope
) {
356 case LDAP_SEARCH_SCOPE_BASE
:
358 scope
= LDB_SCOPE_BASE
;
361 case LDAP_SEARCH_SCOPE_SINGLE
:
363 scope
= LDB_SCOPE_ONELEVEL
;
366 case LDAP_SEARCH_SCOPE_SUB
:
368 scope
= LDB_SCOPE_SUBTREE
;
372 result
= LDAP_PROTOCOL_ERROR
;
373 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
,
375 errstr
= talloc_asprintf(local_ctx
,
376 "%s. Invalid scope", errstr
);
379 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str
));
381 if (req
->num_attributes
>= 1) {
382 attrs
= talloc_array(local_ctx
, const char *, req
->num_attributes
+1);
383 NT_STATUS_HAVE_NO_MEMORY(attrs
);
385 for (i
=0; i
< req
->num_attributes
; i
++) {
386 DEBUG(10,("SearchRequest: attrs: [%s]\n",req
->attributes
[i
]));
387 attrs
[i
] = req
->attributes
[i
];
392 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
393 scope_str
, req
->basedn
, ldb_filter_from_tree(call
, req
->tree
)));
395 res
= talloc_zero(local_ctx
, struct ldb_result
);
396 NT_STATUS_HAVE_NO_MEMORY(res
);
398 ldb_ret
= ldb_build_search_req_ex(&lreq
, samdb
, local_ctx
,
401 call
->request
->controls
,
402 res
, ldb_search_default_callback
,
405 if (ldb_ret
!= LDB_SUCCESS
) {
409 if (call
->conn
->global_catalog
) {
410 search_control
= ldb_request_get_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
412 search_options
= NULL
;
413 if (search_control
) {
414 search_options
= talloc_get_type(search_control
->data
, struct ldb_search_options_control
);
415 search_options
->search_options
|= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
417 search_options
= talloc(lreq
, struct ldb_search_options_control
);
418 NT_STATUS_HAVE_NO_MEMORY(search_options
);
419 search_options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
420 ldb_request_add_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
, false, search_options
);
424 extended_dn_control
= ldb_request_get_control(lreq
, LDB_CONTROL_EXTENDED_DN_OID
);
426 if (extended_dn_control
) {
427 if (extended_dn_control
->data
) {
428 extended_dn_decoded
= talloc_get_type(extended_dn_control
->data
, struct ldb_extended_dn_control
);
429 extended_type
= extended_dn_decoded
->type
;
435 ldb_set_timeout(samdb
, lreq
, req
->timelimit
);
437 ldb_ret
= ldb_request(samdb
, lreq
);
439 if (ldb_ret
!= LDB_SUCCESS
) {
443 ldb_ret
= ldb_wait(lreq
->handle
, LDB_WAIT_ALL
);
445 if (ldb_ret
== LDB_SUCCESS
) {
446 for (i
= 0; i
< res
->count
; i
++) {
447 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultEntry
);
448 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
450 /* Better to have the whole message kept here,
451 * than to find someone further up didn't put
452 * a value in the right spot in the talloc tree */
453 talloc_steal(ent_r
, res
->msgs
[i
]);
455 ent
= &ent_r
->msg
->r
.SearchResultEntry
;
456 ent
->dn
= ldb_dn_get_extended_linearized(ent_r
, res
->msgs
[i
]->dn
, extended_type
);
457 ent
->num_attributes
= 0;
458 ent
->attributes
= NULL
;
459 if (res
->msgs
[i
]->num_elements
== 0) {
462 ent
->num_attributes
= res
->msgs
[i
]->num_elements
;
463 ent
->attributes
= talloc_array(ent_r
, struct ldb_message_element
, ent
->num_attributes
);
464 NT_STATUS_HAVE_NO_MEMORY(ent
->attributes
);
465 for (j
=0; j
< ent
->num_attributes
; j
++) {
466 ent
->attributes
[j
].name
= res
->msgs
[i
]->elements
[j
].name
;
467 ent
->attributes
[j
].num_values
= 0;
468 ent
->attributes
[j
].values
= NULL
;
469 if (req
->attributesonly
&& (res
->msgs
[i
]->elements
[j
].num_values
== 0)) {
472 ent
->attributes
[j
].num_values
= res
->msgs
[i
]->elements
[j
].num_values
;
473 ent
->attributes
[j
].values
= res
->msgs
[i
]->elements
[j
].values
;
476 ldapsrv_queue_reply(call
, ent_r
);
481 done_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultDone
);
482 NT_STATUS_HAVE_NO_MEMORY(done_r
);
484 done
= &done_r
->msg
->r
.SearchResultDone
;
486 done
->referral
= NULL
;
489 } else if (ldb_ret
== LDB_SUCCESS
) {
490 if (res
->count
>= success_limit
) {
491 DEBUG(10,("SearchRequest: results: [%d]\n", res
->count
));
492 result
= LDAP_SUCCESS
;
496 done_r
->msg
->controls
= res
->controls
;
497 talloc_steal(done_r
, res
->controls
);
500 DEBUG(10,("SearchRequest: error\n"));
501 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
504 done
->resultcode
= result
;
505 done
->errormessage
= (errstr
?talloc_strdup(done_r
, errstr
):NULL
);
507 talloc_free(local_ctx
);
509 ldapsrv_queue_reply(call
, done_r
);
513 static NTSTATUS
ldapsrv_ModifyRequest(struct ldapsrv_call
*call
)
515 struct ldap_ModifyRequest
*req
= &call
->request
->r
.ModifyRequest
;
516 struct ldap_Result
*modify_result
;
517 struct ldapsrv_reply
*modify_reply
;
518 TALLOC_CTX
*local_ctx
;
519 struct ldb_context
*samdb
= call
->conn
->ldb
;
520 struct ldb_message
*msg
= NULL
;
522 const char *errstr
= NULL
;
523 int result
= LDAP_SUCCESS
;
527 DEBUG(10, ("ModifyRequest"));
528 DEBUGADD(10, (" dn: %s", req
->dn
));
530 local_ctx
= talloc_named(call
, 0, "ModifyRequest local memory context");
531 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
533 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
536 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req
->dn
));
538 msg
= talloc(local_ctx
, struct ldb_message
);
539 NT_STATUS_HAVE_NO_MEMORY(msg
);
542 msg
->num_elements
= 0;
543 msg
->elements
= NULL
;
545 if (req
->num_mods
> 0) {
546 msg
->num_elements
= req
->num_mods
;
547 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, req
->num_mods
);
548 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
550 for (i
=0; i
< msg
->num_elements
; i
++) {
551 msg
->elements
[i
].name
= discard_const_p(char, req
->mods
[i
].attrib
.name
);
552 msg
->elements
[i
].num_values
= 0;
553 msg
->elements
[i
].values
= NULL
;
555 switch (req
->mods
[i
].type
) {
557 result
= LDAP_PROTOCOL_ERROR
;
558 map_ldb_error(local_ctx
,
559 LDB_ERR_PROTOCOL_ERROR
, &errstr
);
560 errstr
= talloc_asprintf(local_ctx
,
561 "%s. Invalid LDAP_MODIFY_* type", errstr
);
563 case LDAP_MODIFY_ADD
:
564 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
566 case LDAP_MODIFY_DELETE
:
567 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
569 case LDAP_MODIFY_REPLACE
:
570 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
574 msg
->elements
[i
].num_values
= req
->mods
[i
].attrib
.num_values
;
575 if (msg
->elements
[i
].num_values
> 0) {
576 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
577 msg
->elements
[i
].num_values
);
578 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
580 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
581 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
582 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
589 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
590 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
592 if (result
== LDAP_SUCCESS
) {
593 ldb_ret
= ldb_mod_req_with_controls(samdb
, msg
, call
->request
->controls
);
594 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
597 modify_result
= &modify_reply
->msg
->r
.AddResponse
;
598 modify_result
->dn
= NULL
;
599 modify_result
->resultcode
= result
;
600 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
601 modify_result
->referral
= NULL
;
603 talloc_free(local_ctx
);
605 ldapsrv_queue_reply(call
, modify_reply
);
610 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
612 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
613 struct ldap_Result
*add_result
;
614 struct ldapsrv_reply
*add_reply
;
615 TALLOC_CTX
*local_ctx
;
616 struct ldb_context
*samdb
= call
->conn
->ldb
;
617 struct ldb_message
*msg
= NULL
;
619 const char *errstr
= NULL
;
620 int result
= LDAP_SUCCESS
;
624 DEBUG(10, ("AddRequest"));
625 DEBUGADD(10, (" dn: %s", req
->dn
));
627 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
628 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
630 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
633 DEBUG(10, ("AddRequest: dn: [%s]\n", req
->dn
));
635 msg
= talloc(local_ctx
, struct ldb_message
);
636 NT_STATUS_HAVE_NO_MEMORY(msg
);
639 msg
->num_elements
= 0;
640 msg
->elements
= NULL
;
642 if (req
->num_attributes
> 0) {
643 msg
->num_elements
= req
->num_attributes
;
644 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
645 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
647 for (i
=0; i
< msg
->num_elements
; i
++) {
648 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
649 msg
->elements
[i
].flags
= 0;
650 msg
->elements
[i
].num_values
= 0;
651 msg
->elements
[i
].values
= NULL
;
653 if (req
->attributes
[i
].num_values
> 0) {
654 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
655 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
656 msg
->elements
[i
].num_values
);
657 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
659 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
660 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
661 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
668 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
669 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
671 if (result
== LDAP_SUCCESS
) {
672 ldb_ret
= ldb_add(samdb
, msg
);
673 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
676 add_result
= &add_reply
->msg
->r
.AddResponse
;
677 add_result
->dn
= NULL
;
678 add_result
->resultcode
= result
;
679 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
680 add_result
->referral
= NULL
;
682 talloc_free(local_ctx
);
684 ldapsrv_queue_reply(call
, add_reply
);
689 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
691 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
692 struct ldap_Result
*del_result
;
693 struct ldapsrv_reply
*del_reply
;
694 TALLOC_CTX
*local_ctx
;
695 struct ldb_context
*samdb
= call
->conn
->ldb
;
697 const char *errstr
= NULL
;
698 int result
= LDAP_SUCCESS
;
701 DEBUG(10, ("DelRequest"));
702 DEBUGADD(10, (" dn: %s", req
->dn
));
704 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
705 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
707 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
710 DEBUG(10, ("DelRequest: dn: [%s]\n", req
->dn
));
713 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
714 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
716 if (result
== LDAP_SUCCESS
) {
717 ldb_ret
= ldb_delete(samdb
, dn
);
718 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
721 del_result
= &del_reply
->msg
->r
.DelResponse
;
722 del_result
->dn
= NULL
;
723 del_result
->resultcode
= result
;
724 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
725 del_result
->referral
= NULL
;
727 talloc_free(local_ctx
);
729 ldapsrv_queue_reply(call
, del_reply
);
733 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
735 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
736 struct ldap_Result
*modifydn
;
737 struct ldapsrv_reply
*modifydn_r
;
738 TALLOC_CTX
*local_ctx
;
739 struct ldb_context
*samdb
= call
->conn
->ldb
;
740 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
741 struct ldb_dn
*parentdn
= NULL
;
742 const char *errstr
= NULL
;
743 int result
= LDAP_SUCCESS
;
746 DEBUG(10, ("ModifyDNRequest"));
747 DEBUGADD(10, (" dn: %s", req
->dn
));
748 DEBUGADD(10, (" newrdn: %s", req
->newrdn
));
750 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
751 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
753 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
754 VALID_DN_SYNTAX(olddn
);
756 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
757 VALID_DN_SYNTAX(newrdn
);
759 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req
->dn
));
760 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req
->newrdn
));
762 if (ldb_dn_get_comp_num(newrdn
) != 1) {
763 result
= LDAP_INVALID_DN_SYNTAX
;
764 map_ldb_error(local_ctx
, LDB_ERR_INVALID_DN_SYNTAX
, &errstr
);
768 /* we can't handle the rename if we should not remove the old dn */
769 if (!req
->deleteolddn
) {
770 result
= LDAP_UNWILLING_TO_PERFORM
;
771 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, &errstr
);
772 errstr
= talloc_asprintf(local_ctx
,
773 "%s. Old RDN must be deleted", errstr
);
777 if (req
->newsuperior
) {
778 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
779 VALID_DN_SYNTAX(parentdn
);
780 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
782 if (ldb_dn_get_comp_num(parentdn
) < 1) {
783 result
= LDAP_AFFECTS_MULTIPLE_DSAS
;
784 map_ldb_error(local_ctx
, LDB_ERR_AFFECTS_MULTIPLE_DSAS
,
786 errstr
= talloc_asprintf(local_ctx
,
787 "%s. Error new Superior DN invalid", errstr
);
793 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
794 NT_STATUS_HAVE_NO_MEMORY(parentdn
);
797 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
804 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
805 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
807 if (result
== LDAP_SUCCESS
) {
808 ldb_ret
= ldb_rename(samdb
, olddn
, newdn
);
809 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
812 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
814 modifydn
->resultcode
= result
;
815 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
816 modifydn
->referral
= NULL
;
818 talloc_free(local_ctx
);
820 ldapsrv_queue_reply(call
, modifydn_r
);
824 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
826 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
827 struct ldap_Result
*compare
;
828 struct ldapsrv_reply
*compare_r
;
829 TALLOC_CTX
*local_ctx
;
830 struct ldb_context
*samdb
= call
->conn
->ldb
;
831 struct ldb_result
*res
= NULL
;
833 const char *attrs
[1];
834 const char *errstr
= NULL
;
835 const char *filter
= NULL
;
836 int result
= LDAP_SUCCESS
;
839 DEBUG(10, ("CompareRequest"));
840 DEBUGADD(10, (" dn: %s", req
->dn
));
842 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
843 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
845 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
848 DEBUG(10, ("CompareRequest: dn: [%s]\n", req
->dn
));
849 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
850 (int)req
->value
.length
, req
->value
.data
);
851 NT_STATUS_HAVE_NO_MEMORY(filter
);
853 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter
));
858 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
859 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
861 if (result
== LDAP_SUCCESS
) {
862 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
863 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
864 if (ldb_ret
!= LDB_SUCCESS
) {
865 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
866 DEBUG(10,("CompareRequest: error: %s\n", errstr
));
867 } else if (res
->count
== 0) {
868 DEBUG(10,("CompareRequest: doesn't matched\n"));
869 result
= LDAP_COMPARE_FALSE
;
871 } else if (res
->count
== 1) {
872 DEBUG(10,("CompareRequest: matched\n"));
873 result
= LDAP_COMPARE_TRUE
;
875 } else if (res
->count
> 1) {
877 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
878 errstr
= talloc_asprintf(local_ctx
,
879 "%s. Too many objects match!", errstr
);
880 DEBUG(10,("CompareRequest: %d results: %s\n", res
->count
, errstr
));
884 compare
= &compare_r
->msg
->r
.CompareResponse
;
886 compare
->resultcode
= result
;
887 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
888 compare
->referral
= NULL
;
890 talloc_free(local_ctx
);
892 ldapsrv_queue_reply(call
, compare_r
);
896 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
898 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
899 DEBUG(10, ("AbandonRequest\n"));
903 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
906 struct ldap_message
*msg
= call
->request
;
907 /* Check for undecoded critical extensions */
908 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
909 if (!msg
->controls_decoded
[i
] &&
910 msg
->controls
[i
]->critical
) {
911 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
912 msg
->controls
[i
]->oid
));
913 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
917 switch(call
->request
->type
) {
918 case LDAP_TAG_BindRequest
:
919 return ldapsrv_BindRequest(call
);
920 case LDAP_TAG_UnbindRequest
:
921 return ldapsrv_UnbindRequest(call
);
922 case LDAP_TAG_SearchRequest
:
923 return ldapsrv_SearchRequest(call
);
924 case LDAP_TAG_ModifyRequest
:
925 return ldapsrv_ModifyRequest(call
);
926 case LDAP_TAG_AddRequest
:
927 return ldapsrv_AddRequest(call
);
928 case LDAP_TAG_DelRequest
:
929 return ldapsrv_DelRequest(call
);
930 case LDAP_TAG_ModifyDNRequest
:
931 return ldapsrv_ModifyDNRequest(call
);
932 case LDAP_TAG_CompareRequest
:
933 return ldapsrv_CompareRequest(call
);
934 case LDAP_TAG_AbandonRequest
:
935 return ldapsrv_AbandonRequest(call
);
936 case LDAP_TAG_ExtendedRequest
:
937 return ldapsrv_ExtendedRequest(call
);
939 return ldapsrv_unwilling(call
, 2);