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 "lib/ldb/include/ldb_errors.h"
30 #include "lib/ldb_wrap.h"
32 #define VALID_DN_SYNTAX(dn) do {\
34 return NT_STATUS_NO_MEMORY;\
35 } else if ( ! ldb_dn_validate(dn)) {\
36 result = LDAP_INVALID_DN_SYNTAX;\
37 map_ldb_error(local_ctx, LDB_ERR_INVALID_DN_SYNTAX, NULL,\
43 static int map_ldb_error(TALLOC_CTX
*mem_ctx
, int ldb_err
,
44 const char *add_err_string
, 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
));
170 if (add_err_string
!= NULL
) {
171 *errstring
= talloc_asprintf(mem_ctx
, "%s - %s", *errstring
,
175 /* result is 1:1 for now */
178 /* create and execute a modify request */
179 static int ldb_mod_req_with_controls(struct ldb_context
*ldb
,
180 const struct ldb_message
*message
,
181 struct ldb_control
**controls
,
184 struct ldb_request
*req
;
187 ret
= ldb_msg_sanity_check(ldb
, message
);
188 if (ret
!= LDB_SUCCESS
) {
192 ret
= ldb_build_mod_req(&req
, ldb
, ldb
,
196 ldb_modify_default_callback
,
199 if (ret
!= LDB_SUCCESS
) {
203 ret
= ldb_transaction_start(ldb
);
204 if (ret
!= LDB_SUCCESS
) {
208 ret
= ldb_request(ldb
, req
);
209 if (ret
== LDB_SUCCESS
) {
210 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
213 if (ret
== LDB_SUCCESS
) {
214 ret
= ldb_transaction_commit(ldb
);
217 ldb_transaction_cancel(ldb
);
225 connect to the sam database
227 NTSTATUS
ldapsrv_backend_Init(struct ldapsrv_connection
*conn
)
229 conn
->ldb
= ldb_wrap_connect(conn
,
230 conn
->connection
->event
.ctx
,
232 lp_sam_url(conn
->lp_ctx
),
234 samdb_credentials(conn
->connection
->event
.ctx
, conn
->lp_ctx
),
235 conn
->global_catalog
? LDB_FLG_RDONLY
: 0);
236 if (conn
->ldb
== NULL
) {
237 return NT_STATUS_INTERNAL_DB_CORRUPTION
;
240 if (conn
->server_credentials
) {
241 char **sasl_mechs
= NULL
;
242 struct gensec_security_ops
**backends
= gensec_security_all();
243 struct gensec_security_ops
**ops
244 = gensec_use_kerberos_mechs(conn
, backends
, conn
->server_credentials
);
245 unsigned int i
, j
= 0;
246 for (i
= 0; ops
&& ops
[i
]; i
++) {
247 if (!lp_parm_bool(conn
->lp_ctx
, NULL
, "gensec", ops
[i
]->name
, ops
[i
]->enabled
))
250 if (ops
[i
]->sasl_name
&& ops
[i
]->server_start
) {
251 char *sasl_name
= talloc_strdup(conn
, ops
[i
]->sasl_name
);
254 return NT_STATUS_NO_MEMORY
;
256 sasl_mechs
= talloc_realloc(conn
, sasl_mechs
, char *, j
+ 2);
258 return NT_STATUS_NO_MEMORY
;
260 sasl_mechs
[j
] = sasl_name
;
261 talloc_steal(sasl_mechs
, sasl_name
);
262 sasl_mechs
[j
+1] = NULL
;
266 talloc_unlink(conn
, ops
);
267 ldb_set_opaque(conn
->ldb
, "supportedSASLMechanims", sasl_mechs
);
273 struct ldapsrv_reply
*ldapsrv_init_reply(struct ldapsrv_call
*call
, uint8_t type
)
275 struct ldapsrv_reply
*reply
;
277 reply
= talloc(call
, struct ldapsrv_reply
);
281 reply
->msg
= talloc(reply
, struct ldap_message
);
282 if (reply
->msg
== NULL
) {
287 reply
->msg
->messageid
= call
->request
->messageid
;
288 reply
->msg
->type
= type
;
289 reply
->msg
->controls
= NULL
;
294 void ldapsrv_queue_reply(struct ldapsrv_call
*call
, struct ldapsrv_reply
*reply
)
296 DLIST_ADD_END(call
->replies
, reply
, struct ldapsrv_reply
*);
299 static NTSTATUS
ldapsrv_unwilling(struct ldapsrv_call
*call
, int error
)
301 struct ldapsrv_reply
*reply
;
302 struct ldap_ExtendedResponse
*r
;
304 DEBUG(10,("Unwilling type[%d] id[%d]\n", call
->request
->type
, call
->request
->messageid
));
306 reply
= ldapsrv_init_reply(call
, LDAP_TAG_ExtendedResponse
);
308 return NT_STATUS_NO_MEMORY
;
311 r
= &reply
->msg
->r
.ExtendedResponse
;
312 r
->response
.resultcode
= error
;
313 r
->response
.dn
= NULL
;
314 r
->response
.errormessage
= NULL
;
315 r
->response
.referral
= NULL
;
319 ldapsrv_queue_reply(call
, reply
);
323 int ldb_add_with_context(struct ldb_context
*ldb
,
324 const struct ldb_message
*message
,
327 struct ldb_request
*req
;
330 ret
= ldb_msg_sanity_check(ldb
, message
);
331 if (ret
!= LDB_SUCCESS
) {
335 ret
= ldb_build_add_req(&req
, ldb
, ldb
,
339 ldb_modify_default_callback
,
342 if (ret
!= LDB_SUCCESS
) return ret
;
344 ret
= ldb_transaction_start(ldb
);
345 if (ret
!= LDB_SUCCESS
) {
349 ret
= ldb_request(ldb
, req
);
350 if (ret
== LDB_SUCCESS
) {
351 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
354 if (ret
== LDB_SUCCESS
) {
355 ret
= ldb_transaction_commit(ldb
);
358 ldb_transaction_cancel(ldb
);
365 int ldb_delete_with_context(struct ldb_context
*ldb
,
369 struct ldb_request
*req
;
372 ret
= ldb_build_del_req(&req
, ldb
, ldb
,
376 ldb_modify_default_callback
,
379 if (ret
!= LDB_SUCCESS
) return ret
;
381 ret
= ldb_transaction_start(ldb
);
382 if (ret
!= LDB_SUCCESS
) {
386 ret
= ldb_request(ldb
, req
);
387 if (ret
== LDB_SUCCESS
) {
388 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
391 if (ret
== LDB_SUCCESS
) {
392 ret
= ldb_transaction_commit(ldb
);
395 ldb_transaction_cancel(ldb
);
402 int ldb_rename_with_context(struct ldb_context
*ldb
,
403 struct ldb_dn
*olddn
,
404 struct ldb_dn
*newdn
,
407 struct ldb_request
*req
;
410 ret
= ldb_build_rename_req(&req
, ldb
, ldb
,
415 ldb_modify_default_callback
,
418 if (ret
!= LDB_SUCCESS
) return ret
;
420 ret
= ldb_transaction_start(ldb
);
421 if (ret
!= LDB_SUCCESS
) {
425 ret
= ldb_request(ldb
, req
);
426 if (ret
== LDB_SUCCESS
) {
427 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
430 if (ret
== LDB_SUCCESS
) {
431 ret
= ldb_transaction_commit(ldb
);
434 ldb_transaction_cancel(ldb
);
441 static NTSTATUS
ldapsrv_SearchRequest(struct ldapsrv_call
*call
)
443 struct ldap_SearchRequest
*req
= &call
->request
->r
.SearchRequest
;
444 struct ldap_SearchResEntry
*ent
;
445 struct ldap_Result
*done
;
446 struct ldapsrv_reply
*ent_r
, *done_r
;
447 TALLOC_CTX
*local_ctx
;
448 struct ldb_context
*samdb
= talloc_get_type(call
->conn
->ldb
, struct ldb_context
);
449 struct ldb_dn
*basedn
;
450 struct ldb_result
*res
= NULL
;
451 struct ldb_request
*lreq
;
452 struct ldb_control
*search_control
;
453 struct ldb_search_options_control
*search_options
;
454 struct ldb_control
*extended_dn_control
;
455 struct ldb_extended_dn_control
*extended_dn_decoded
= NULL
;
456 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
457 const char **attrs
= NULL
;
458 const char *scope_str
, *errstr
= NULL
;
459 int success_limit
= 1;
463 int extended_type
= 1;
465 DEBUG(10, ("SearchRequest"));
466 DEBUGADD(10, (" basedn: %s", req
->basedn
));
467 DEBUGADD(10, (" filter: %s\n", ldb_filter_from_tree(call
, req
->tree
)));
469 local_ctx
= talloc_new(call
);
470 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
472 basedn
= ldb_dn_new(local_ctx
, samdb
, req
->basedn
);
473 VALID_DN_SYNTAX(basedn
);
475 DEBUG(10, ("SearchRequest: basedn: [%s]\n", req
->basedn
));
476 DEBUG(10, ("SearchRequest: filter: [%s]\n", ldb_filter_from_tree(call
, req
->tree
)));
478 switch (req
->scope
) {
479 case LDAP_SEARCH_SCOPE_BASE
:
481 scope
= LDB_SCOPE_BASE
;
484 case LDAP_SEARCH_SCOPE_SINGLE
:
486 scope
= LDB_SCOPE_ONELEVEL
;
489 case LDAP_SEARCH_SCOPE_SUB
:
491 scope
= LDB_SCOPE_SUBTREE
;
495 result
= LDAP_PROTOCOL_ERROR
;
496 map_ldb_error(local_ctx
, LDB_ERR_PROTOCOL_ERROR
, NULL
,
498 errstr
= talloc_asprintf(local_ctx
,
499 "%s. Invalid scope", errstr
);
502 DEBUG(10,("SearchRequest: scope: [%s]\n", scope_str
));
504 if (req
->num_attributes
>= 1) {
505 attrs
= talloc_array(local_ctx
, const char *, req
->num_attributes
+1);
506 NT_STATUS_HAVE_NO_MEMORY(attrs
);
508 for (i
=0; i
< req
->num_attributes
; i
++) {
509 DEBUG(10,("SearchRequest: attrs: [%s]\n",req
->attributes
[i
]));
510 attrs
[i
] = req
->attributes
[i
];
515 DEBUG(5,("ldb_request %s dn=%s filter=%s\n",
516 scope_str
, req
->basedn
, ldb_filter_from_tree(call
, req
->tree
)));
518 res
= talloc_zero(local_ctx
, struct ldb_result
);
519 NT_STATUS_HAVE_NO_MEMORY(res
);
521 ldb_ret
= ldb_build_search_req_ex(&lreq
, samdb
, local_ctx
,
524 call
->request
->controls
,
525 res
, ldb_search_default_callback
,
528 if (ldb_ret
!= LDB_SUCCESS
) {
532 if (call
->conn
->global_catalog
) {
533 search_control
= ldb_request_get_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
);
535 search_options
= NULL
;
536 if (search_control
) {
537 search_options
= talloc_get_type(search_control
->data
, struct ldb_search_options_control
);
538 search_options
->search_options
|= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
540 search_options
= talloc(lreq
, struct ldb_search_options_control
);
541 NT_STATUS_HAVE_NO_MEMORY(search_options
);
542 search_options
->search_options
= LDB_SEARCH_OPTION_PHANTOM_ROOT
;
543 ldb_request_add_control(lreq
, LDB_CONTROL_SEARCH_OPTIONS_OID
, false, search_options
);
547 extended_dn_control
= ldb_request_get_control(lreq
, LDB_CONTROL_EXTENDED_DN_OID
);
549 if (extended_dn_control
) {
550 if (extended_dn_control
->data
) {
551 extended_dn_decoded
= talloc_get_type(extended_dn_control
->data
, struct ldb_extended_dn_control
);
552 extended_type
= extended_dn_decoded
->type
;
558 ldb_set_timeout(samdb
, lreq
, req
->timelimit
);
560 ldb_ret
= ldb_request(samdb
, lreq
);
562 if (ldb_ret
!= LDB_SUCCESS
) {
566 ldb_ret
= ldb_wait(lreq
->handle
, LDB_WAIT_ALL
);
568 if (ldb_ret
== LDB_SUCCESS
) {
569 for (i
= 0; i
< res
->count
; i
++) {
570 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultEntry
);
571 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
573 /* Better to have the whole message kept here,
574 * than to find someone further up didn't put
575 * a value in the right spot in the talloc tree */
576 talloc_steal(ent_r
, res
->msgs
[i
]);
578 ent
= &ent_r
->msg
->r
.SearchResultEntry
;
579 ent
->dn
= ldb_dn_get_extended_linearized(ent_r
, res
->msgs
[i
]->dn
, extended_type
);
580 ent
->num_attributes
= 0;
581 ent
->attributes
= NULL
;
582 if (res
->msgs
[i
]->num_elements
== 0) {
585 ent
->num_attributes
= res
->msgs
[i
]->num_elements
;
586 ent
->attributes
= talloc_array(ent_r
, struct ldb_message_element
, ent
->num_attributes
);
587 NT_STATUS_HAVE_NO_MEMORY(ent
->attributes
);
588 for (j
=0; j
< ent
->num_attributes
; j
++) {
589 ent
->attributes
[j
].name
= res
->msgs
[i
]->elements
[j
].name
;
590 ent
->attributes
[j
].num_values
= 0;
591 ent
->attributes
[j
].values
= NULL
;
592 if (req
->attributesonly
&& (res
->msgs
[i
]->elements
[j
].num_values
== 0)) {
595 ent
->attributes
[j
].num_values
= res
->msgs
[i
]->elements
[j
].num_values
;
596 ent
->attributes
[j
].values
= res
->msgs
[i
]->elements
[j
].values
;
599 ldapsrv_queue_reply(call
, ent_r
);
602 /* Send back referrals if they do exist (search operations) */
603 if (res
->refs
!= NULL
) {
605 struct ldap_SearchResRef
*ent_ref
;
607 for (ref
= res
->refs
; *ref
!= NULL
; ++ref
) {
608 ent_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultReference
);
609 NT_STATUS_HAVE_NO_MEMORY(ent_r
);
611 /* Better to have the whole referrals kept here,
612 * than to find someone further up didn't put
613 * a value in the right spot in the talloc tree
615 talloc_steal(ent_r
, *ref
);
617 ent_ref
= &ent_r
->msg
->r
.SearchResultReference
;
618 ent_ref
->referral
= *ref
;
620 ldapsrv_queue_reply(call
, ent_r
);
626 done_r
= ldapsrv_init_reply(call
, LDAP_TAG_SearchResultDone
);
627 NT_STATUS_HAVE_NO_MEMORY(done_r
);
629 done
= &done_r
->msg
->r
.SearchResultDone
;
631 done
->referral
= NULL
;
634 } else if (ldb_ret
== LDB_SUCCESS
) {
635 if (res
->count
>= success_limit
) {
636 DEBUG(10,("SearchRequest: results: [%d]\n", res
->count
));
637 result
= LDAP_SUCCESS
;
641 done_r
->msg
->controls
= res
->controls
;
642 talloc_steal(done_r
, res
->controls
);
645 DEBUG(10,("SearchRequest: error\n"));
646 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
650 done
->resultcode
= result
;
651 done
->errormessage
= (errstr
?talloc_strdup(done_r
, errstr
):NULL
);
653 talloc_free(local_ctx
);
655 ldapsrv_queue_reply(call
, done_r
);
659 static NTSTATUS
ldapsrv_ModifyRequest(struct ldapsrv_call
*call
)
661 struct ldap_ModifyRequest
*req
= &call
->request
->r
.ModifyRequest
;
662 struct ldap_Result
*modify_result
;
663 struct ldapsrv_reply
*modify_reply
;
664 TALLOC_CTX
*local_ctx
;
665 struct ldb_context
*samdb
= call
->conn
->ldb
;
666 struct ldb_message
*msg
= NULL
;
668 const char *errstr
= NULL
;
669 int result
= LDAP_SUCCESS
;
672 struct ldb_result
*res
= NULL
;
674 DEBUG(10, ("ModifyRequest"));
675 DEBUGADD(10, (" dn: %s", req
->dn
));
677 local_ctx
= talloc_named(call
, 0, "ModifyRequest local memory context");
678 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
680 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
683 DEBUG(10, ("ModifyRequest: dn: [%s]\n", req
->dn
));
685 msg
= talloc(local_ctx
, struct ldb_message
);
686 NT_STATUS_HAVE_NO_MEMORY(msg
);
689 msg
->num_elements
= 0;
690 msg
->elements
= NULL
;
692 if (req
->num_mods
> 0) {
693 msg
->num_elements
= req
->num_mods
;
694 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, req
->num_mods
);
695 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
697 for (i
=0; i
< msg
->num_elements
; i
++) {
698 msg
->elements
[i
].name
= discard_const_p(char, req
->mods
[i
].attrib
.name
);
699 msg
->elements
[i
].num_values
= 0;
700 msg
->elements
[i
].values
= NULL
;
702 switch (req
->mods
[i
].type
) {
704 result
= LDAP_PROTOCOL_ERROR
;
705 map_ldb_error(local_ctx
,
706 LDB_ERR_PROTOCOL_ERROR
, NULL
, &errstr
);
707 errstr
= talloc_asprintf(local_ctx
,
708 "%s. Invalid LDAP_MODIFY_* type", errstr
);
710 case LDAP_MODIFY_ADD
:
711 msg
->elements
[i
].flags
= LDB_FLAG_MOD_ADD
;
713 case LDAP_MODIFY_DELETE
:
714 msg
->elements
[i
].flags
= LDB_FLAG_MOD_DELETE
;
716 case LDAP_MODIFY_REPLACE
:
717 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
721 msg
->elements
[i
].num_values
= req
->mods
[i
].attrib
.num_values
;
722 if (msg
->elements
[i
].num_values
> 0) {
723 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
724 msg
->elements
[i
].num_values
);
725 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
727 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
728 msg
->elements
[i
].values
[j
].length
= req
->mods
[i
].attrib
.values
[j
].length
;
729 msg
->elements
[i
].values
[j
].data
= req
->mods
[i
].attrib
.values
[j
].data
;
736 modify_reply
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyResponse
);
737 NT_STATUS_HAVE_NO_MEMORY(modify_reply
);
739 if (result
== LDAP_SUCCESS
) {
740 res
= talloc_zero(local_ctx
, struct ldb_result
);
741 NT_STATUS_HAVE_NO_MEMORY(res
);
742 ldb_ret
= ldb_mod_req_with_controls(samdb
, msg
, call
->request
->controls
, res
);
743 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
747 modify_result
= &modify_reply
->msg
->r
.AddResponse
;
748 modify_result
->dn
= NULL
;
750 if (res
->refs
!= NULL
) {
751 modify_result
->resultcode
= map_ldb_error(local_ctx
,
754 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
755 modify_result
->referral
= talloc_strdup(call
, *res
->refs
);
757 modify_result
->resultcode
= result
;
758 modify_result
->errormessage
= (errstr
?talloc_strdup(modify_reply
, errstr
):NULL
);
759 modify_result
->referral
= NULL
;
761 talloc_free(local_ctx
);
763 ldapsrv_queue_reply(call
, modify_reply
);
768 static NTSTATUS
ldapsrv_AddRequest(struct ldapsrv_call
*call
)
770 struct ldap_AddRequest
*req
= &call
->request
->r
.AddRequest
;
771 struct ldap_Result
*add_result
;
772 struct ldapsrv_reply
*add_reply
;
773 TALLOC_CTX
*local_ctx
;
774 struct ldb_context
*samdb
= call
->conn
->ldb
;
775 struct ldb_message
*msg
= NULL
;
777 const char *errstr
= NULL
;
778 int result
= LDAP_SUCCESS
;
781 struct ldb_result
*res
= NULL
;
783 DEBUG(10, ("AddRequest"));
784 DEBUGADD(10, (" dn: %s", req
->dn
));
786 local_ctx
= talloc_named(call
, 0, "AddRequest local memory context");
787 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
789 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
792 DEBUG(10, ("AddRequest: dn: [%s]\n", req
->dn
));
794 msg
= talloc(local_ctx
, struct ldb_message
);
795 NT_STATUS_HAVE_NO_MEMORY(msg
);
798 msg
->num_elements
= 0;
799 msg
->elements
= NULL
;
801 if (req
->num_attributes
> 0) {
802 msg
->num_elements
= req
->num_attributes
;
803 msg
->elements
= talloc_array(msg
, struct ldb_message_element
, msg
->num_elements
);
804 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
);
806 for (i
=0; i
< msg
->num_elements
; i
++) {
807 msg
->elements
[i
].name
= discard_const_p(char, req
->attributes
[i
].name
);
808 msg
->elements
[i
].flags
= 0;
809 msg
->elements
[i
].num_values
= 0;
810 msg
->elements
[i
].values
= NULL
;
812 if (req
->attributes
[i
].num_values
> 0) {
813 msg
->elements
[i
].num_values
= req
->attributes
[i
].num_values
;
814 msg
->elements
[i
].values
= talloc_array(msg
->elements
, struct ldb_val
,
815 msg
->elements
[i
].num_values
);
816 NT_STATUS_HAVE_NO_MEMORY(msg
->elements
[i
].values
);
818 for (j
=0; j
< msg
->elements
[i
].num_values
; j
++) {
819 msg
->elements
[i
].values
[j
].length
= req
->attributes
[i
].values
[j
].length
;
820 msg
->elements
[i
].values
[j
].data
= req
->attributes
[i
].values
[j
].data
;
827 add_reply
= ldapsrv_init_reply(call
, LDAP_TAG_AddResponse
);
828 NT_STATUS_HAVE_NO_MEMORY(add_reply
);
830 if (result
== LDAP_SUCCESS
) {
831 res
= talloc_zero(local_ctx
, struct ldb_result
);
832 NT_STATUS_HAVE_NO_MEMORY(res
);
833 ldb_ret
= ldb_add_with_context(samdb
, msg
, res
);
834 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
838 add_result
= &add_reply
->msg
->r
.AddResponse
;
839 add_result
->dn
= NULL
;
840 if (res
->refs
!= NULL
) {
841 add_result
->resultcode
= map_ldb_error(local_ctx
,
842 LDB_ERR_REFERRAL
, NULL
,
844 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
845 add_result
->referral
= talloc_strdup(call
, *res
->refs
);
847 add_result
->resultcode
= result
;
848 add_result
->errormessage
= (errstr
?talloc_strdup(add_reply
,errstr
):NULL
);
849 add_result
->referral
= NULL
;
851 talloc_free(local_ctx
);
853 ldapsrv_queue_reply(call
, add_reply
);
858 static NTSTATUS
ldapsrv_DelRequest(struct ldapsrv_call
*call
)
860 struct ldap_DelRequest
*req
= &call
->request
->r
.DelRequest
;
861 struct ldap_Result
*del_result
;
862 struct ldapsrv_reply
*del_reply
;
863 TALLOC_CTX
*local_ctx
;
864 struct ldb_context
*samdb
= call
->conn
->ldb
;
866 const char *errstr
= NULL
;
867 int result
= LDAP_SUCCESS
;
869 struct ldb_result
*res
= NULL
;
871 DEBUG(10, ("DelRequest"));
872 DEBUGADD(10, (" dn: %s", req
->dn
));
874 local_ctx
= talloc_named(call
, 0, "DelRequest local memory context");
875 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
877 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
880 DEBUG(10, ("DelRequest: dn: [%s]\n", req
->dn
));
883 del_reply
= ldapsrv_init_reply(call
, LDAP_TAG_DelResponse
);
884 NT_STATUS_HAVE_NO_MEMORY(del_reply
);
886 if (result
== LDAP_SUCCESS
) {
887 res
= talloc_zero(local_ctx
, struct ldb_result
);
888 NT_STATUS_HAVE_NO_MEMORY(res
);
889 ldb_ret
= ldb_delete_with_context(samdb
, dn
, res
);
890 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
894 del_result
= &del_reply
->msg
->r
.DelResponse
;
895 del_result
->dn
= NULL
;
896 if (res
->refs
!= NULL
) {
897 del_result
->resultcode
= map_ldb_error(local_ctx
,
898 LDB_ERR_REFERRAL
, NULL
,
900 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
901 del_result
->referral
= talloc_strdup(call
, *res
->refs
);
903 del_result
->resultcode
= result
;
904 del_result
->errormessage
= (errstr
?talloc_strdup(del_reply
,errstr
):NULL
);
905 del_result
->referral
= NULL
;
908 talloc_free(local_ctx
);
910 ldapsrv_queue_reply(call
, del_reply
);
914 static NTSTATUS
ldapsrv_ModifyDNRequest(struct ldapsrv_call
*call
)
916 struct ldap_ModifyDNRequest
*req
= &call
->request
->r
.ModifyDNRequest
;
917 struct ldap_Result
*modifydn
;
918 struct ldapsrv_reply
*modifydn_r
;
919 TALLOC_CTX
*local_ctx
;
920 struct ldb_context
*samdb
= call
->conn
->ldb
;
921 struct ldb_dn
*olddn
, *newdn
=NULL
, *newrdn
;
922 struct ldb_dn
*parentdn
= NULL
;
923 const char *errstr
= NULL
;
924 int result
= LDAP_SUCCESS
;
926 struct ldb_result
*res
= NULL
;
928 DEBUG(10, ("ModifyDNRequest"));
929 DEBUGADD(10, (" dn: %s", req
->dn
));
930 DEBUGADD(10, (" newrdn: %s", req
->newrdn
));
932 local_ctx
= talloc_named(call
, 0, "ModifyDNRequest local memory context");
933 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
935 olddn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
936 VALID_DN_SYNTAX(olddn
);
938 newrdn
= ldb_dn_new(local_ctx
, samdb
, req
->newrdn
);
939 VALID_DN_SYNTAX(newrdn
);
941 DEBUG(10, ("ModifyDNRequest: olddn: [%s]\n", req
->dn
));
942 DEBUG(10, ("ModifyDNRequest: newrdn: [%s]\n", req
->newrdn
));
944 if (ldb_dn_get_comp_num(newrdn
) != 1) {
945 result
= LDAP_INVALID_DN_SYNTAX
;
946 map_ldb_error(local_ctx
, LDB_ERR_INVALID_DN_SYNTAX
, NULL
,
951 /* we can't handle the rename if we should not remove the old dn */
952 if (!req
->deleteolddn
) {
953 result
= LDAP_UNWILLING_TO_PERFORM
;
954 map_ldb_error(local_ctx
, LDB_ERR_UNWILLING_TO_PERFORM
, NULL
,
956 errstr
= talloc_asprintf(local_ctx
,
957 "%s. Old RDN must be deleted", errstr
);
961 if (req
->newsuperior
) {
962 parentdn
= ldb_dn_new(local_ctx
, samdb
, req
->newsuperior
);
963 VALID_DN_SYNTAX(parentdn
);
964 DEBUG(10, ("ModifyDNRequest: newsuperior: [%s]\n", req
->newsuperior
));
966 if (ldb_dn_get_comp_num(parentdn
) < 1) {
967 result
= LDAP_AFFECTS_MULTIPLE_DSAS
;
968 map_ldb_error(local_ctx
, LDB_ERR_AFFECTS_MULTIPLE_DSAS
,
970 errstr
= talloc_asprintf(local_ctx
,
971 "%s. Error new Superior DN invalid", errstr
);
977 parentdn
= ldb_dn_get_parent(local_ctx
, olddn
);
978 NT_STATUS_HAVE_NO_MEMORY(parentdn
);
981 if ( ! ldb_dn_add_child(parentdn
, newrdn
)) {
988 modifydn_r
= ldapsrv_init_reply(call
, LDAP_TAG_ModifyDNResponse
);
989 NT_STATUS_HAVE_NO_MEMORY(modifydn_r
);
991 if (result
== LDAP_SUCCESS
) {
992 res
= talloc_zero(local_ctx
, struct ldb_result
);
993 NT_STATUS_HAVE_NO_MEMORY(res
);
994 ldb_ret
= ldb_rename_with_context(samdb
, olddn
, newdn
, res
);
995 result
= map_ldb_error(local_ctx
, ldb_ret
, ldb_errstring(samdb
),
999 modifydn
= &modifydn_r
->msg
->r
.ModifyDNResponse
;
1000 modifydn
->dn
= NULL
;
1001 if (res
->refs
!= NULL
) {
1002 modifydn
->resultcode
= map_ldb_error(local_ctx
,
1003 LDB_ERR_REFERRAL
, NULL
,
1005 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1006 modifydn
->referral
= talloc_strdup(call
, *res
->refs
);
1008 modifydn
->resultcode
= result
;
1009 modifydn
->errormessage
= (errstr
?talloc_strdup(modifydn_r
,errstr
):NULL
);
1010 modifydn
->referral
= NULL
;
1013 talloc_free(local_ctx
);
1015 ldapsrv_queue_reply(call
, modifydn_r
);
1016 return NT_STATUS_OK
;
1019 static NTSTATUS
ldapsrv_CompareRequest(struct ldapsrv_call
*call
)
1021 struct ldap_CompareRequest
*req
= &call
->request
->r
.CompareRequest
;
1022 struct ldap_Result
*compare
;
1023 struct ldapsrv_reply
*compare_r
;
1024 TALLOC_CTX
*local_ctx
;
1025 struct ldb_context
*samdb
= call
->conn
->ldb
;
1026 struct ldb_result
*res
= NULL
;
1028 const char *attrs
[1];
1029 const char *errstr
= NULL
;
1030 const char *filter
= NULL
;
1031 int result
= LDAP_SUCCESS
;
1034 DEBUG(10, ("CompareRequest"));
1035 DEBUGADD(10, (" dn: %s", req
->dn
));
1037 local_ctx
= talloc_named(call
, 0, "CompareRequest local_memory_context");
1038 NT_STATUS_HAVE_NO_MEMORY(local_ctx
);
1040 dn
= ldb_dn_new(local_ctx
, samdb
, req
->dn
);
1041 VALID_DN_SYNTAX(dn
);
1043 DEBUG(10, ("CompareRequest: dn: [%s]\n", req
->dn
));
1044 filter
= talloc_asprintf(local_ctx
, "(%s=%*s)", req
->attribute
,
1045 (int)req
->value
.length
, req
->value
.data
);
1046 NT_STATUS_HAVE_NO_MEMORY(filter
);
1048 DEBUGADD(10, ("CompareRequest: attribute: [%s]\n", filter
));
1053 compare_r
= ldapsrv_init_reply(call
, LDAP_TAG_CompareResponse
);
1054 NT_STATUS_HAVE_NO_MEMORY(compare_r
);
1056 if (result
== LDAP_SUCCESS
) {
1057 ldb_ret
= ldb_search(samdb
, local_ctx
, &res
,
1058 dn
, LDB_SCOPE_BASE
, attrs
, "%s", filter
);
1059 if (ldb_ret
!= LDB_SUCCESS
) {
1060 result
= map_ldb_error(local_ctx
, ldb_ret
,
1061 ldb_errstring(samdb
), &errstr
);
1062 DEBUG(10,("CompareRequest: error: %s\n", errstr
));
1063 } else if (res
->count
== 0) {
1064 DEBUG(10,("CompareRequest: doesn't matched\n"));
1065 result
= LDAP_COMPARE_FALSE
;
1067 } else if (res
->count
== 1) {
1068 DEBUG(10,("CompareRequest: matched\n"));
1069 result
= LDAP_COMPARE_TRUE
;
1071 } else if (res
->count
> 1) {
1072 result
= LDAP_OTHER
;
1073 map_ldb_error(local_ctx
, LDB_ERR_OTHER
, NULL
, &errstr
);
1074 errstr
= talloc_asprintf(local_ctx
,
1075 "%s. Too many objects match!", errstr
);
1076 DEBUG(10,("CompareRequest: %d results: %s\n", res
->count
, errstr
));
1080 compare
= &compare_r
->msg
->r
.CompareResponse
;
1082 compare
->resultcode
= result
;
1083 compare
->errormessage
= (errstr
?talloc_strdup(compare_r
,errstr
):NULL
);
1084 compare
->referral
= NULL
;
1086 talloc_free(local_ctx
);
1088 ldapsrv_queue_reply(call
, compare_r
);
1089 return NT_STATUS_OK
;
1092 static NTSTATUS
ldapsrv_AbandonRequest(struct ldapsrv_call
*call
)
1094 /* struct ldap_AbandonRequest *req = &call->request.r.AbandonRequest;*/
1095 DEBUG(10, ("AbandonRequest\n"));
1096 return NT_STATUS_OK
;
1099 NTSTATUS
ldapsrv_do_call(struct ldapsrv_call
*call
)
1102 struct ldap_message
*msg
= call
->request
;
1103 /* Check for undecoded critical extensions */
1104 for (i
=0; msg
->controls
&& msg
->controls
[i
]; i
++) {
1105 if (!msg
->controls_decoded
[i
] &&
1106 msg
->controls
[i
]->critical
) {
1107 DEBUG(3, ("ldapsrv_do_call: Critical extension %s is not known to this server\n",
1108 msg
->controls
[i
]->oid
));
1109 return ldapsrv_unwilling(call
, LDAP_UNAVAILABLE_CRITICAL_EXTENSION
);
1113 switch(call
->request
->type
) {
1114 case LDAP_TAG_BindRequest
:
1115 return ldapsrv_BindRequest(call
);
1116 case LDAP_TAG_UnbindRequest
:
1117 return ldapsrv_UnbindRequest(call
);
1118 case LDAP_TAG_SearchRequest
:
1119 return ldapsrv_SearchRequest(call
);
1120 case LDAP_TAG_ModifyRequest
:
1121 return ldapsrv_ModifyRequest(call
);
1122 case LDAP_TAG_AddRequest
:
1123 return ldapsrv_AddRequest(call
);
1124 case LDAP_TAG_DelRequest
:
1125 return ldapsrv_DelRequest(call
);
1126 case LDAP_TAG_ModifyDNRequest
:
1127 return ldapsrv_ModifyDNRequest(call
);
1128 case LDAP_TAG_CompareRequest
:
1129 return ldapsrv_CompareRequest(call
);
1130 case LDAP_TAG_AbandonRequest
:
1131 return ldapsrv_AbandonRequest(call
);
1132 case LDAP_TAG_ExtendedRequest
:
1133 return ldapsrv_ExtendedRequest(call
);
1135 return ldapsrv_unwilling(call
, 2);