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 (!lp_parm_bool(conn
->lp_ctx
, NULL
, "gensec", ops
[i
]->name
, ops
[i
]->enabled
))
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 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
538 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
545 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
546 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
548 if (result
== LDAP_SUCCESS
) {
549 ldb_ret
= ldb_modify(samdb
, msg
);
550 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
553 modify_result
= &modify_reply
->msg
->r
.AddResponse
;
554 modify_result
->dn
= NULL
;
555 modify_result
->resultcode
= result
;
556 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
557 modify_result
->referral
= NULL
;
559 talloc_free(local_ctx
);
561 ldapsrv_queue_reply(call
, modify_reply
);
566 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
568 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
569 struct ldap_Result
*add_result
;
570 struct ldapsrv_reply
*add_reply
;
571 TALLOC_CTX
*local_ctx
;
572 struct ldb_context
*samdb
= call
->conn
->ldb
;
573 struct ldb_message
*msg
= NULL
;
575 const char *errstr
= NULL
;
576 int result
= LDAP_SUCCESS
;
580 DEBUG(10, ("AddRequest"));
581 DEBUGADD(10, (" dn: %s", req
->dn
));
583 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
584 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
586 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
589 DEBUG(10, ("AddRequest: dn: [%s]\n", req
->dn
));
591 msg
= talloc(local_ctx
, struct ldb_message
);
592 NT_STATUS_HAVE_NO_MEMORY(msg
);
595 msg
->num_elements
= 0;
596 msg
->elements
= NULL
;
598 if (req
->num_attributes
> 0) {
599 msg
->num_elements
= req
->num_attributes
;
600 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
601 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
603 for (i
=0; i
< msg
->num_elements
; i
++) {
604 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
605 msg
->elements
[i
].flags
= 0;
606 msg
->elements
[i
].num_values
= 0;
607 msg
->elements
[i
].values
= NULL
;
609 if (req
->attributes
[i
].num_values
> 0) {
610 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
611 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
612 msg
->elements
[i
].num_values
);
613 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
615 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
616 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
617 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
624 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
625 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
627 if (result
== LDAP_SUCCESS
) {
628 ldb_ret
= ldb_add(samdb
, msg
);
629 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
632 add_result
= &add_reply
->msg
->r
.AddResponse
;
633 add_result
->dn
= NULL
;
634 add_result
->resultcode
= result
;
635 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
636 add_result
->referral
= NULL
;
638 talloc_free(local_ctx
);
640 ldapsrv_queue_reply(call
, add_reply
);
645 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
647 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
648 struct ldap_Result
*del_result
;
649 struct ldapsrv_reply
*del_reply
;
650 TALLOC_CTX
*local_ctx
;
651 struct ldb_context
*samdb
= call
->conn
->ldb
;
653 const char *errstr
= NULL
;
654 int result
= LDAP_SUCCESS
;
657 DEBUG(10, ("DelRequest"));
658 DEBUGADD(10, (" dn: %s", req
->dn
));
660 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
661 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
663 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
666 DEBUG(10, ("DelRequest: dn: [%s]\n", req
->dn
));
669 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
670 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
672 if (result
== LDAP_SUCCESS
) {
673 ldb_ret
= ldb_delete(samdb
, dn
);
674 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
677 del_result
= &del_reply
->msg
->r
.DelResponse
;
678 del_result
->dn
= NULL
;
679 del_result
->resultcode
= result
;
680 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
681 del_result
->referral
= NULL
;
683 talloc_free(local_ctx
);
685 ldapsrv_queue_reply(call
, del_reply
);
689 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
691 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
692 struct ldap_Result
*modifydn
;
693 struct ldapsrv_reply
*modifydn_r
;
694 TALLOC_CTX
*local_ctx
;
695 struct ldb_context
*samdb
= call
->conn
->ldb
;
696 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
697 struct ldb_dn
*parentdn
= NULL
;
698 const char *errstr
= NULL
;
699 int result
= LDAP_SUCCESS
;
702 DEBUG(10, ("ModifyDNRequest"));
703 DEBUGADD(10, (" dn: %s", req
->dn
));
704 DEBUGADD(10, (" newrdn: %s", req
->newrdn
));
706 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
707 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
709 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
710 VALID_DN_SYNTAX(olddn
);
712 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
713 VALID_DN_SYNTAX(newrdn
);
715 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req
->dn
));
716 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req
->newrdn
));
718 if (ldb_dn_get_comp_num(newrdn
) != 1) {
719 result
= LDAP_INVALID_DN_SYNTAX
;
720 map_ldb_error(local_ctx
, LDB_ERR_INVALID_DN_SYNTAX
, &errstr
);
724 /* we can't handle the rename if we should not remove the old dn */
725 if (!req
->deleteolddn
) {
726 result
= LDAP_UNWILLING_TO_PERFORM
;
727 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, &errstr
);
728 errstr
= talloc_asprintf(local_ctx
,
729 "%s. Old RDN must be deleted", errstr
);
733 if (req
->newsuperior
) {
734 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
735 VALID_DN_SYNTAX(parentdn
);
736 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
738 if (ldb_dn_get_comp_num(parentdn
) < 1) {
739 result
= LDAP_AFFECTS_MULTIPLE_DSAS
;
740 map_ldb_error(local_ctx
, LDB_ERR_AFFECTS_MULTIPLE_DSAS
,
742 errstr
= talloc_asprintf(local_ctx
,
743 "%s. Error new Superior DN invalid", errstr
);
749 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
750 NT_STATUS_HAVE_NO_MEMORY(parentdn
);
753 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
760 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
761 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
763 if (result
== LDAP_SUCCESS
) {
764 ldb_ret
= ldb_rename(samdb
, olddn
, newdn
);
765 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
768 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
770 modifydn
->resultcode
= result
;
771 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
772 modifydn
->referral
= NULL
;
774 talloc_free(local_ctx
);
776 ldapsrv_queue_reply(call
, modifydn_r
);
780 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
782 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
783 struct ldap_Result
*compare
;
784 struct ldapsrv_reply
*compare_r
;
785 TALLOC_CTX
*local_ctx
;
786 struct ldb_context
*samdb
= call
->conn
->ldb
;
787 struct ldb_result
*res
= NULL
;
789 const char *attrs
[1];
790 const char *errstr
= NULL
;
791 const char *filter
= NULL
;
792 int result
= LDAP_SUCCESS
;
795 DEBUG(10, ("CompareRequest"));
796 DEBUGADD(10, (" dn: %s", req
->dn
));
798 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
799 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
801 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
804 DEBUG(10, ("CompareRequest: dn: [%s]\n", req
->dn
));
805 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
806 (int)req
->value
.length
, req
->value
.data
);
807 NT_STATUS_HAVE_NO_MEMORY(filter
);
809 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter
));
814 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
815 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
817 if (result
== LDAP_SUCCESS
) {
818 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
819 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
820 if (ldb_ret
!= LDB_SUCCESS
) {
821 result
= map_ldb_error(local_ctx
, ldb_ret
, &errstr
);
822 DEBUG(10,("CompareRequest: error: %s\n", errstr
));
823 } else if (res
->count
== 0) {
824 DEBUG(10,("CompareRequest: doesn't matched\n"));
825 result
= LDAP_COMPARE_FALSE
;
827 } else if (res
->count
== 1) {
828 DEBUG(10,("CompareRequest: matched\n"));
829 result
= LDAP_COMPARE_TRUE
;
831 } else if (res
->count
> 1) {
833 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, &errstr
);
834 errstr
= talloc_asprintf(local_ctx
,
835 "%s. Too many objects match!", errstr
);
836 DEBUG(10,("CompareRequest: %d results: %s\n", res
->count
, errstr
));
840 compare
= &compare_r
->msg
->r
.CompareResponse
;
842 compare
->resultcode
= result
;
843 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
844 compare
->referral
= NULL
;
846 talloc_free(local_ctx
);
848 ldapsrv_queue_reply(call
, compare_r
);
852 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
854 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
855 DEBUG(10, ("AbandonRequest\n"));
859 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
862 struct ldap_message
*msg
= call
->request
;
863 /* Check for undecoded critical extensions */
864 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
865 if (!msg
->controls_decoded
[i
] &&
866 msg
->controls
[i
]->critical
) {
867 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
868 msg
->controls
[i
]->oid
));
869 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
873 switch(call
->request
->type
) {
874 case LDAP_TAG_BindRequest
:
875 return ldapsrv_BindRequest(call
);
876 case LDAP_TAG_UnbindRequest
:
877 return ldapsrv_UnbindRequest(call
);
878 case LDAP_TAG_SearchRequest
:
879 return ldapsrv_SearchRequest(call
);
880 case LDAP_TAG_ModifyRequest
:
881 return ldapsrv_ModifyRequest(call
);
882 case LDAP_TAG_AddRequest
:
883 return ldapsrv_AddRequest(call
);
884 case LDAP_TAG_DelRequest
:
885 return ldapsrv_DelRequest(call
);
886 case LDAP_TAG_ModifyDNRequest
:
887 return ldapsrv_ModifyDNRequest(call
);
888 case LDAP_TAG_CompareRequest
:
889 return ldapsrv_CompareRequest(call
);
890 case LDAP_TAG_AbandonRequest
:
891 return ldapsrv_AbandonRequest(call
);
892 case LDAP_TAG_ExtendedRequest
:
893 return ldapsrv_ExtendedRequest(call
);
895 return ldapsrv_unwilling(call
, 2);