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 */
176 connect to the sam database
178 NTSTATUS
ldapsrv_backend_Init(struct ldapsrv_connection
*conn
)
180 conn
->ldb
= ldb_wrap_connect(conn
,
181 conn
->connection
->event
.ctx
,
183 lp_sam_url(conn
->lp_ctx
),
185 samdb_credentials(conn
, conn
->connection
->event
.ctx
, conn
->lp_ctx
),
186 conn
->global_catalog
? LDB_FLG_RDONLY
: 0, NULL
);
187 if (conn
->ldb
== NULL
) {
188 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
191 if (conn
->server_credentials
) {
192 char **sasl_mechs
= NULL
;
193 struct gensec_security_ops
**backends
= gensec_security_all();
194 struct gensec_security_ops
**ops
195 = gensec_use_kerberos_mechs(conn
, backends
, conn
->server_credentials
);
197 for (i
= 0; ops
&& ops
[i
]; i
++) {
198 if (!gensec_security_ops_enabled(ops
[i
], conn
->lp_ctx
))
201 if (ops
[i
]->sasl_name
&& ops
[i
]->server_start
) {
202 char *sasl_name
= talloc_strdup(conn
, ops
[i
]->sasl_name
);
205 return NT_STATUS_NO_MEMORY
;
207 sasl_mechs
= talloc_realloc(conn
, sasl_mechs
, char *, j
+ 2);
209 return NT_STATUS_NO_MEMORY
;
211 sasl_mechs
[j
] = sasl_name
;
212 talloc_steal(sasl_mechs
, sasl_name
);
213 sasl_mechs
[j
+1] = NULL
;
217 talloc_unlink(conn
, ops
);
218 ldb_set_opaque(conn
->ldb
, "supportedSASLMechanims", sasl_mechs
);
224 struct ldapsrv_reply
*ldapsrv_init_reply(struct ldapsrv_call
*call
, uint8_t type
)
226 struct ldapsrv_reply
*reply
;
228 reply
= talloc(call
, struct ldapsrv_reply
);
232 reply
->msg
= talloc(reply
, struct ldap_message
);
233 if (reply
->msg
== NULL
) {
238 reply
->msg
->messageid
= call
->request
->messageid
;
239 reply
->msg
->type
= type
;
240 reply
->msg
->controls
= NULL
;
245 void ldapsrv_queue_reply(struct ldapsrv_call
*call
, struct ldapsrv_reply
*reply
)
247 DLIST_ADD_END(call
->replies
, reply
, struct ldapsrv_reply
*);
250 static NTSTATUS
ldapsrv_unwilling(struct ldapsrv_call
*call
, int error
)
252 struct ldapsrv_reply
*reply
;
253 struct ldap_ExtendedResponse
*r
;
255 DEBUG(10,("Unwilling type[%d] id[%d]\n", call
->request
->type
, call
->request
->messageid
));
257 reply
= ldapsrv_init_reply(call
, LDAP_TAG_ExtendedResponse
);
259 return NT_STATUS_NO_MEMORY
;
262 r
= &reply
->msg
->r
.ExtendedResponse
;
263 r
->response
.resultcode
= error
;
264 r
->response
.dn
= NULL
;
265 r
->response
.errormessage
= NULL
;
266 r
->response
.referral
= NULL
;
270 ldapsrv_queue_reply(call
, reply
);
274 static NTSTATUS
ldapsrv_SearchRequest(struct ldapsrv_call
*call
)
276 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
277 struct ldap_SearchResEntry
*ent
;
278 struct ldap_Result
*done
;
279 struct ldapsrv_reply
*ent_r
, *done_r
;
280 TALLOC_CTX
*local_ctx
;
281 struct ldb_context
*samdb
= talloc_get_type(call
->conn
->ldb
, struct ldb_context
);
282 struct ldb_dn
*basedn
;
283 struct ldb_result
*res
= NULL
;
284 struct ldb_request
*lreq
;
285 struct ldb_control
*search_control
;
286 struct ldb_search_options_control
*search_options
;
287 struct ldb_control
*extended_dn_control
;
288 struct ldb_extended_dn_control
*extended_dn_decoded
= NULL
;
289 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
290 const char **attrs
= NULL
;
291 const char *scope_str
, *errstr
= NULL
;
292 int success_limit
= 1;
296 int extended_type
= 1;
298 DEBUG(10, ("SearchRequest"));
299 DEBUGADD(10, (" basedn: %s", req
->basedn
));
300 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call
, req
->tree
)));
302 local_ctx
= talloc_new(call
);
303 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
305 basedn
= ldb_dn_new(local_ctx
, samdb
, req
->basedn
);
306 VALID_DN_SYNTAX(basedn
);
308 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req
->basedn
));
309 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call
, req
->tree
)));
311 switch (req
->scope
) {
312 case LDAP_SEARCH_SCOPE_BASE
:
314 scope
= LDB_SCOPE_BASE
;
317 case LDAP_SEARCH_SCOPE_SINGLE
:
319 scope
= LDB_SCOPE_ONELEVEL
;
322 case LDAP_SEARCH_SCOPE_SUB
:
324 scope
= LDB_SCOPE_SUBTREE
;
328 result
= LDAP_PROTOCOL_ERROR
;
329 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
,
331 errstr
= talloc_asprintf(local_ctx
,
332 "%s. Invalid scope", errstr
);
335 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str
));
337 if (req
->num_attributes
>= 1) {
338 attrs
= talloc_array(local_ctx
, const char *, req
->num_attributes
+1);
339 NT_STATUS_HAVE_NO_MEMORY(attrs
);
341 for (i
=0; i
< req
->num_attributes
; i
++) {
342 DEBUG(10,("SearchRequest: attrs: [%s]\n",req
->attributes
[i
]));
343 attrs
[i
] = req
->attributes
[i
];
348 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
349 scope_str
, req
->basedn
, ldb_filter_from_tree(call
, req
->tree
)));
351 res
= talloc_zero(local_ctx
, struct ldb_result
);
352 NT_STATUS_HAVE_NO_MEMORY(res
);
354 ldb_ret
= ldb_build_search_req_ex(&lreq
, samdb
, local_ctx
,
357 call
->request
->controls
,
358 res
, ldb_search_default_callback
,
361 if (ldb_ret
!= LDB_SUCCESS
) {
365 if (call
->conn
->global_catalog
) {
366 search_control
= ldb_request_get_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
368 search_options
= NULL
;
369 if (search_control
) {
370 search_options
= talloc_get_type(search_control
->data
, struct ldb_search_options_control
);
371 search_options
->search_options
|= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
373 search_options
= talloc(lreq
, struct ldb_search_options_control
);
374 NT_STATUS_HAVE_NO_MEMORY(search_options
);
375 search_options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
376 ldb_request_add_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
, false, search_options
);
380 extended_dn_control
= ldb_request_get_control(lreq
, LDB_CONTROL_EXTENDED_DN_OID
);
382 if (extended_dn_control
) {
383 if (extended_dn_control
->data
) {
384 extended_dn_decoded
= talloc_get_type(extended_dn_control
->data
, struct ldb_extended_dn_control
);
385 extended_type
= extended_dn_decoded
->type
;
391 ldb_set_timeout(samdb
, lreq
, req
->timelimit
);
393 ldb_ret
= ldb_request(samdb
, lreq
);
395 if (ldb_ret
!= LDB_SUCCESS
) {
399 ldb_ret
= ldb_wait(lreq
->handle
, LDB_WAIT_ALL
);
401 if (ldb_ret
== LDB_SUCCESS
) {
402 for (i
= 0; i
< res
->count
; i
++) {
403 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultEntry
);
404 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
406 /* Better to have the whole message kept here,
407 * than to find someone further up didn't put
408 * a value in the right spot in the talloc tree */
409 talloc_steal(ent_r
, res
->msgs
[i
]);
411 ent
= &ent_r
->msg
->r
.SearchResultEntry
;
412 ent
->dn
= ldb_dn_get_extended_linearized(ent_r
, res
->msgs
[i
]->dn
, extended_type
);
413 ent
->num_attributes
= 0;
414 ent
->attributes
= NULL
;
415 if (res
->msgs
[i
]->num_elements
== 0) {
418 ent
->num_attributes
= res
->msgs
[i
]->num_elements
;
419 ent
->attributes
= talloc_array(ent_r
, struct ldb_message_element
, ent
->num_attributes
);
420 NT_STATUS_HAVE_NO_MEMORY(ent
->attributes
);
421 for (j
=0; j
< ent
->num_attributes
; j
++) {
422 ent
->attributes
[j
].name
= res
->msgs
[i
]->elements
[j
].name
;
423 ent
->attributes
[j
].num_values
= 0;
424 ent
->attributes
[j
].values
= NULL
;
425 if (req
->attributesonly
&& (res
->msgs
[i
]->elements
[j
].num_values
== 0)) {
428 ent
->attributes
[j
].num_values
= res
->msgs
[i
]->elements
[j
].num_values
;
429 ent
->attributes
[j
].values
= res
->msgs
[i
]->elements
[j
].values
;
432 ldapsrv_queue_reply(call
, ent_r
);
437 done_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultDone
);
438 NT_STATUS_HAVE_NO_MEMORY(done_r
);
440 done
= &done_r
->msg
->r
.SearchResultDone
;
442 done
->referral
= NULL
;
445 } else if (ldb_ret
== LDB_SUCCESS
) {
446 if (res
->count
>= success_limit
) {
447 DEBUG(10,("SearchRequest: results: [%d]\n", res
->count
));
448 result
= LDAP_SUCCESS
;
452 done_r
->msg
->controls
= res
->controls
;
453 talloc_steal(done_r
, res
->controls
);
456 DEBUG(10,("SearchRequest: error\n"));
457 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
460 done
->resultcode
= result
;
461 done
->errormessage
= (errstr
?talloc_strdup(done_r
, errstr
):NULL
);
463 talloc_free(local_ctx
);
465 ldapsrv_queue_reply(call
, done_r
);
469 static NTSTATUS
ldapsrv_ModifyRequest(struct ldapsrv_call
*call
)
471 struct ldap_ModifyRequest
*req
= &call
->request
->r
.ModifyRequest
;
472 struct ldap_Result
*modify_result
;
473 struct ldapsrv_reply
*modify_reply
;
474 TALLOC_CTX
*local_ctx
;
475 struct ldb_context
*samdb
= call
->conn
->ldb
;
476 struct ldb_message
*msg
= NULL
;
478 const char *errstr
= NULL
;
479 int result
= LDAP_SUCCESS
;
483 DEBUG(10, ("ModifyRequest"));
484 DEBUGADD(10, (" dn: %s", req
->dn
));
486 local_ctx
= talloc_named(call
, 0, "ModifyRequest local memory context");
487 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
489 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
492 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req
->dn
));
494 msg
= talloc(local_ctx
, struct ldb_message
);
495 NT_STATUS_HAVE_NO_MEMORY(msg
);
498 msg
->num_elements
= 0;
499 msg
->elements
= NULL
;
501 if (req
->num_mods
> 0) {
502 msg
->num_elements
= req
->num_mods
;
503 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, req
->num_mods
);
504 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
506 for (i
=0; i
< msg
->num_elements
; i
++) {
507 msg
->elements
[i
].name
= discard_const_p(char, req
->mods
[i
].attrib
.name
);
508 msg
->elements
[i
].num_values
= 0;
509 msg
->elements
[i
].values
= NULL
;
511 switch (req
->mods
[i
].type
) {
513 result
= LDAP_PROTOCOL_ERROR
;
514 map_ldb_error(local_ctx
,
515 LDB_ERR_PROTOCOL_ERROR
, &errstr
);
516 errstr
= talloc_asprintf(local_ctx
,
517 "%s. Invalid LDAP_MODIFY_* type", errstr
);
519 case LDAP_MODIFY_ADD
:
520 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
522 case LDAP_MODIFY_DELETE
:
523 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
525 case LDAP_MODIFY_REPLACE
:
526 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
530 msg
->elements
[i
].num_values
= req
->mods
[i
].attrib
.num_values
;
531 if (msg
->elements
[i
].num_values
> 0) {
532 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
533 msg
->elements
[i
].num_values
);
534 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
536 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
537 if (!(req
->mods
[i
].attrib
.values
[j
].length
> 0)) {
540 map_ldb_error(local_ctx
,
541 LDB_ERR_OTHER
, &errstr
);
542 errstr
= talloc_asprintf(local_ctx
,
543 "%s. Empty attribute values not allowed", errstr
);
546 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
547 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
553 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
554 errstr
= talloc_asprintf(local_ctx
,
555 "%s. No mods are not allowed", errstr
);
560 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
561 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
563 if (result
== LDAP_SUCCESS
) {
564 ldb_ret
= ldb_modify(samdb
, msg
);
565 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
568 modify_result
= &modify_reply
->msg
->r
.AddResponse
;
569 modify_result
->dn
= NULL
;
570 modify_result
->resultcode
= result
;
571 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
572 modify_result
->referral
= NULL
;
574 talloc_free(local_ctx
);
576 ldapsrv_queue_reply(call
, modify_reply
);
581 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
583 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
584 struct ldap_Result
*add_result
;
585 struct ldapsrv_reply
*add_reply
;
586 TALLOC_CTX
*local_ctx
;
587 struct ldb_context
*samdb
= call
->conn
->ldb
;
588 struct ldb_message
*msg
= NULL
;
590 const char *errstr
= NULL
;
591 int result
= LDAP_SUCCESS
;
595 DEBUG(10, ("AddRequest"));
596 DEBUGADD(10, (" dn: %s", req
->dn
));
598 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
599 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
601 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
604 DEBUG(10, ("AddRequest: dn: [%s]\n", req
->dn
));
606 msg
= talloc(local_ctx
, struct ldb_message
);
607 NT_STATUS_HAVE_NO_MEMORY(msg
);
610 msg
->num_elements
= 0;
611 msg
->elements
= NULL
;
613 if (req
->num_attributes
> 0) {
614 msg
->num_elements
= req
->num_attributes
;
615 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
616 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
618 for (i
=0; i
< msg
->num_elements
; i
++) {
619 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
620 msg
->elements
[i
].flags
= 0;
621 msg
->elements
[i
].num_values
= 0;
622 msg
->elements
[i
].values
= NULL
;
624 if (req
->attributes
[i
].num_values
> 0) {
625 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
626 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
627 msg
->elements
[i
].num_values
);
628 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
630 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
631 if (!(req
->attributes
[i
].values
[j
].length
> 0)) {
633 map_ldb_error(local_ctx
,
634 LDB_ERR_OTHER
, &errstr
);
635 errstr
= talloc_asprintf(local_ctx
,
636 "%s. Empty attribute values not allowed", errstr
);
639 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
640 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
644 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
645 errstr
= talloc_asprintf(local_ctx
,
646 "%s. No attribute values are not allowed", errstr
);
652 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
653 errstr
= talloc_asprintf(local_ctx
,
654 "%s. No attributes are not allowed", errstr
);
659 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
660 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
662 if (result
== LDAP_SUCCESS
) {
663 ldb_ret
= ldb_add(samdb
, msg
);
664 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
667 add_result
= &add_reply
->msg
->r
.AddResponse
;
668 add_result
->dn
= NULL
;
669 add_result
->resultcode
= result
;
670 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
671 add_result
->referral
= NULL
;
673 talloc_free(local_ctx
);
675 ldapsrv_queue_reply(call
, add_reply
);
680 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
682 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
683 struct ldap_Result
*del_result
;
684 struct ldapsrv_reply
*del_reply
;
685 TALLOC_CTX
*local_ctx
;
686 struct ldb_context
*samdb
= call
->conn
->ldb
;
688 const char *errstr
= NULL
;
689 int result
= LDAP_SUCCESS
;
692 DEBUG(10, ("DelRequest"));
693 DEBUGADD(10, (" dn: %s", req
->dn
));
695 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
696 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
698 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
701 DEBUG(10, ("DelRequest: dn: [%s]\n", req
->dn
));
704 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
705 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
707 if (result
== LDAP_SUCCESS
) {
708 ldb_ret
= ldb_delete(samdb
, dn
);
709 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
712 del_result
= &del_reply
->msg
->r
.DelResponse
;
713 del_result
->dn
= NULL
;
714 del_result
->resultcode
= result
;
715 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
716 del_result
->referral
= NULL
;
718 talloc_free(local_ctx
);
720 ldapsrv_queue_reply(call
, del_reply
);
724 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
726 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
727 struct ldap_Result
*modifydn
;
728 struct ldapsrv_reply
*modifydn_r
;
729 TALLOC_CTX
*local_ctx
;
730 struct ldb_context
*samdb
= call
->conn
->ldb
;
731 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
732 struct ldb_dn
*parentdn
= NULL
;
733 const char *errstr
= NULL
;
734 int result
= LDAP_SUCCESS
;
737 DEBUG(10, ("ModifyDNRequrest"));
738 DEBUGADD(10, (" dn: %s", req
->dn
));
739 DEBUGADD(10, (" newrdn: %s", req
->newrdn
));
741 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
742 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
744 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
745 VALID_DN_SYNTAX(olddn
);
747 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
748 VALID_DN_SYNTAX(newrdn
);
750 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req
->dn
));
751 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req
->newrdn
));
753 /* we can't handle the rename if we should not remove the old dn */
754 if (!req
->deleteolddn
) {
755 result
= LDAP_UNWILLING_TO_PERFORM
;
756 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, &errstr
);
757 errstr
= talloc_asprintf(local_ctx
,
758 "%s. Old RDN must be deleted", errstr
);
762 if (req
->newsuperior
) {
763 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
764 VALID_DN_SYNTAX(parentdn
);
765 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
767 if (ldb_dn_get_comp_num(parentdn
) < 1) {
768 result
= LDAP_AFFECTS_MULTIPLE_DSAS
;
769 map_ldb_error(local_ctx
, LDB_ERR_AFFECTS_MULTIPLE_DSAS
,
771 errstr
= talloc_asprintf(local_ctx
,
772 "%s. Error new Superior DN invalid", errstr
);
778 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
779 NT_STATUS_HAVE_NO_MEMORY(parentdn
);
782 if ( ! ldb_dn_add_child_fmt(parentdn
,
784 ldb_dn_get_rdn_name(newrdn
),
785 (char *)ldb_dn_get_rdn_val(newrdn
)->data
)) {
792 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
793 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
795 if (result
== LDAP_SUCCESS
) {
796 ldb_ret
= ldb_rename(samdb
, olddn
, newdn
);
797 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
800 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
802 modifydn
->resultcode
= result
;
803 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
804 modifydn
->referral
= NULL
;
806 talloc_free(local_ctx
);
808 ldapsrv_queue_reply(call
, modifydn_r
);
812 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
814 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
815 struct ldap_Result
*compare
;
816 struct ldapsrv_reply
*compare_r
;
817 TALLOC_CTX
*local_ctx
;
818 struct ldb_context
*samdb
= call
->conn
->ldb
;
819 struct ldb_result
*res
= NULL
;
821 const char *attrs
[1];
822 const char *errstr
= NULL
;
823 const char *filter
= NULL
;
824 int result
= LDAP_SUCCESS
;
827 DEBUG(10, ("CompareRequest"));
828 DEBUGADD(10, (" dn: %s", req
->dn
));
830 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
831 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
833 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
836 DEBUG(10, ("CompareRequest: dn: [%s]\n", req
->dn
));
837 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
838 (int)req
->value
.length
, req
->value
.data
);
839 NT_STATUS_HAVE_NO_MEMORY(filter
);
841 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter
));
846 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
847 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
849 if (result
== LDAP_SUCCESS
) {
850 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
851 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
852 if (ldb_ret
!= LDB_SUCCESS
) {
853 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
854 DEBUG(10,("CompareRequest: error: %s\n", errstr
));
855 } else if (res
->count
== 0) {
856 DEBUG(10,("CompareRequest: doesn't matched\n"));
857 result
= LDAP_COMPARE_FALSE
;
859 } else if (res
->count
== 1) {
860 DEBUG(10,("CompareRequest: matched\n"));
861 result
= LDAP_COMPARE_TRUE
;
863 } else if (res
->count
> 1) {
865 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
866 errstr
= talloc_asprintf(local_ctx
,
867 "%s. Too many objects match!", errstr
);
868 DEBUG(10,("CompareRequest: %d results: %s\n", res
->count
, errstr
));
872 compare
= &compare_r
->msg
->r
.CompareResponse
;
874 compare
->resultcode
= result
;
875 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
876 compare
->referral
= NULL
;
878 talloc_free(local_ctx
);
880 ldapsrv_queue_reply(call
, compare_r
);
884 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
886 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
887 DEBUG(10, ("AbandonRequest\n"));
891 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
894 struct ldap_message
*msg
= call
->request
;
895 /* Check for undecoded critical extensions */
896 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
897 if (!msg
->controls_decoded
[i
] &&
898 msg
->controls
[i
]->critical
) {
899 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
900 msg
->controls
[i
]->oid
));
901 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
905 switch(call
->request
->type
) {
906 case LDAP_TAG_BindRequest
:
907 return ldapsrv_BindRequest(call
);
908 case LDAP_TAG_UnbindRequest
:
909 return ldapsrv_UnbindRequest(call
);
910 case LDAP_TAG_SearchRequest
:
911 return ldapsrv_SearchRequest(call
);
912 case LDAP_TAG_ModifyRequest
:
913 return ldapsrv_ModifyRequest(call
);
914 case LDAP_TAG_AddRequest
:
915 return ldapsrv_AddRequest(call
);
916 case LDAP_TAG_DelRequest
:
917 return ldapsrv_DelRequest(call
);
918 case LDAP_TAG_ModifyDNRequest
:
919 return ldapsrv_ModifyDNRequest(call
);
920 case LDAP_TAG_CompareRequest
:
921 return ldapsrv_CompareRequest(call
);
922 case LDAP_TAG_AbandonRequest
:
923 return ldapsrv_AbandonRequest(call
);
924 case LDAP_TAG_ExtendedRequest
:
925 return ldapsrv_ExtendedRequest(call
);
927 return ldapsrv_unwilling(call
, 2);