2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Simo Sorce 2005-2008
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_module.h"
26 #include "system/time.h"
27 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/samdb/ldb_modules/util.h"
30 #include "libcli/security/security.h"
31 #include "librpc/ndr/libndr.h"
34 unsigned int num_controls
;
36 unsigned int num_partitions
;
37 struct ldb_dn
**partitions
;
41 return 1 if a specific attribute has been requested
43 static int do_attribute(const char * const *attrs
, const char *name
)
45 return attrs
== NULL
||
46 ldb_attr_in_list(attrs
, name
) ||
47 ldb_attr_in_list(attrs
, "*");
50 static int do_attribute_explicit(const char * const *attrs
, const char *name
)
52 return attrs
!= NULL
&& ldb_attr_in_list(attrs
, name
);
57 expand a DN attribute to include extended DN information if requested
59 static int expand_dn_in_message(struct ldb_module
*module
, struct ldb_message
*msg
,
60 const char *attrname
, struct ldb_control
*edn_control
,
61 struct ldb_request
*req
)
63 struct ldb_dn
*dn
, *dn2
;
66 struct ldb_request
*req2
;
68 const char *no_attrs
[] = { NULL
};
69 struct ldb_result
*res
;
70 struct ldb_extended_dn_control
*edn
;
71 TALLOC_CTX
*tmp_ctx
= talloc_new(req
);
72 struct ldb_context
*ldb
;
75 ldb
= ldb_module_get_ctx(module
);
77 edn
= talloc_get_type(edn_control
->data
, struct ldb_extended_dn_control
);
82 v
= discard_const_p(struct ldb_val
, ldb_msg_find_ldb_val(msg
, attrname
));
88 dn_string
= talloc_strndup(tmp_ctx
, (const char *)v
->data
, v
->length
);
89 if (dn_string
== NULL
) {
91 return LDB_ERR_OPERATIONS_ERROR
;
94 res
= talloc_zero(tmp_ctx
, struct ldb_result
);
97 return LDB_ERR_OPERATIONS_ERROR
;
100 dn
= ldb_dn_new(tmp_ctx
, ldb
, dn_string
);
101 if (!ldb_dn_validate(dn
)) {
102 talloc_free(tmp_ctx
);
103 return LDB_ERR_OPERATIONS_ERROR
;
106 ret
= ldb_build_search_req(&req2
, ldb
, tmp_ctx
,
112 res
, ldb_search_default_callback
,
114 if (ret
!= LDB_SUCCESS
) {
115 talloc_free(tmp_ctx
);
120 ret
= ldb_request_add_control(req2
,
121 LDB_CONTROL_EXTENDED_DN_OID
,
122 edn_control
->critical
, edn
);
123 if (ret
!= LDB_SUCCESS
) {
124 talloc_free(tmp_ctx
);
128 ret
= ldb_next_request(module
, req2
);
129 if (ret
== LDB_SUCCESS
) {
130 ret
= ldb_wait(req2
->handle
, LDB_WAIT_ALL
);
132 if (ret
!= LDB_SUCCESS
) {
133 talloc_free(tmp_ctx
);
137 if (!res
|| res
->count
!= 1) {
138 talloc_free(tmp_ctx
);
139 return LDB_ERR_OPERATIONS_ERROR
;
142 dn2
= res
->msgs
[0]->dn
;
144 v
->data
= (uint8_t *)ldb_dn_get_extended_linearized(msg
->elements
, dn2
, edn_type
);
145 v
->length
= strlen((char *)v
->data
);
147 if (v
->data
== NULL
) {
148 talloc_free(tmp_ctx
);
149 return LDB_ERR_OPERATIONS_ERROR
;
152 talloc_free(tmp_ctx
);
159 add dynamically generated attributes to rootDSE result
161 static int rootdse_add_dynamic(struct ldb_module
*module
, struct ldb_message
*msg
,
162 const char * const *attrs
, struct ldb_request
*req
)
164 struct ldb_context
*ldb
;
165 struct private_data
*priv
= talloc_get_type(ldb_module_get_private(module
), struct private_data
);
167 const struct dsdb_schema
*schema
;
169 struct ldb_control
*edn_control
;
170 const char *dn_attrs
[] = {
171 "configurationNamingContext",
172 "defaultNamingContext",
174 "rootDomainNamingContext",
175 "schemaNamingContext",
180 ldb
= ldb_module_get_ctx(module
);
181 schema
= dsdb_get_schema(ldb
, NULL
);
183 msg
->dn
= ldb_dn_new(msg
, ldb
, NULL
);
185 /* don't return the distinduishedName, cn and name attributes */
186 ldb_msg_remove_attr(msg
, "distinguishedName");
187 ldb_msg_remove_attr(msg
, "cn");
188 ldb_msg_remove_attr(msg
, "name");
190 if (do_attribute(attrs
, "currentTime")) {
191 if (ldb_msg_add_steal_string(msg
, "currentTime",
192 ldb_timestring(msg
, time(NULL
))) != 0) {
197 if (priv
&& do_attribute(attrs
, "supportedControl")) {
199 for (i
= 0; i
< priv
->num_controls
; i
++) {
200 char *control
= talloc_strdup(msg
, priv
->controls
[i
]);
204 if (ldb_msg_add_steal_string(msg
, "supportedControl",
211 if (priv
&& do_attribute(attrs
, "namingContexts")) {
213 for (i
= 0; i
< priv
->num_partitions
; i
++) {
214 struct ldb_dn
*dn
= priv
->partitions
[i
];
215 if (ldb_msg_add_steal_string(msg
, "namingContexts",
216 ldb_dn_alloc_linearized(msg
, dn
)) != 0) {
222 server_sasl
= talloc_get_type(ldb_get_opaque(ldb
, "supportedSASLMechanims"),
224 if (server_sasl
&& do_attribute(attrs
, "supportedSASLMechanisms")) {
226 for (i
= 0; server_sasl
&& server_sasl
[i
]; i
++) {
227 char *sasl_name
= talloc_strdup(msg
, server_sasl
[i
]);
231 if (ldb_msg_add_steal_string(msg
, "supportedSASLMechanisms",
238 if (do_attribute(attrs
, "highestCommittedUSN")) {
240 int ret
= ldb_sequence_number(ldb
, LDB_SEQ_HIGHEST_SEQ
, &seq_num
);
241 if (ret
== LDB_SUCCESS
) {
242 if (ldb_msg_add_fmt(msg
, "highestCommittedUSN",
243 "%llu", (unsigned long long)seq_num
) != 0) {
249 if (schema
&& do_attribute_explicit(attrs
, "dsSchemaAttrCount")) {
250 struct dsdb_attribute
*cur
;
253 for (cur
= schema
->attributes
; cur
; cur
= cur
->next
) {
257 if (ldb_msg_add_fmt(msg
, "dsSchemaAttrCount",
263 if (schema
&& do_attribute_explicit(attrs
, "dsSchemaClassCount")) {
264 struct dsdb_class
*cur
;
267 for (cur
= schema
->classes
; cur
; cur
= cur
->next
) {
271 if (ldb_msg_add_fmt(msg
, "dsSchemaClassCount",
277 if (schema
&& do_attribute_explicit(attrs
, "dsSchemaPrefixCount")) {
278 if (ldb_msg_add_fmt(msg
, "dsSchemaPrefixCount",
279 "%u", schema
->prefixmap
->length
) != 0) {
284 if (do_attribute_explicit(attrs
, "validFSMOs")) {
285 const struct dsdb_naming_fsmo
*naming_fsmo
;
286 const struct dsdb_pdc_fsmo
*pdc_fsmo
;
289 if (schema
&& schema
->fsmo
.we_are_master
) {
290 dn_str
= ldb_dn_get_linearized(samdb_schema_dn(ldb
));
291 if (dn_str
&& dn_str
[0]) {
292 if (ldb_msg_add_fmt(msg
, "validFSMOs", "%s", dn_str
) != 0) {
298 naming_fsmo
= talloc_get_type(ldb_get_opaque(ldb
, "dsdb_naming_fsmo"),
299 struct dsdb_naming_fsmo
);
300 if (naming_fsmo
&& naming_fsmo
->we_are_master
) {
301 dn_str
= ldb_dn_get_linearized(samdb_partitions_dn(ldb
, msg
));
302 if (dn_str
&& dn_str
[0]) {
303 if (ldb_msg_add_fmt(msg
, "validFSMOs", "%s", dn_str
) != 0) {
309 pdc_fsmo
= talloc_get_type(ldb_get_opaque(ldb
, "dsdb_pdc_fsmo"),
310 struct dsdb_pdc_fsmo
);
311 if (pdc_fsmo
&& pdc_fsmo
->we_are_master
) {
312 dn_str
= ldb_dn_get_linearized(samdb_base_dn(ldb
));
313 if (dn_str
&& dn_str
[0]) {
314 if (ldb_msg_add_fmt(msg
, "validFSMOs", "%s", dn_str
) != 0) {
321 if (do_attribute_explicit(attrs
, "vendorVersion")) {
322 if (ldb_msg_add_fmt(msg
, "vendorVersion",
323 "%s", SAMBA_VERSION_STRING
) != 0) {
328 if (priv
&& do_attribute(attrs
, "domainFunctionality")) {
329 if (ldb_msg_add_fmt(msg
, "domainFunctionality",
330 "%d", dsdb_functional_level(ldb
)) != 0) {
335 if (priv
&& do_attribute(attrs
, "forestFunctionality")
336 && (val
= talloc_get_type(ldb_get_opaque(ldb
, "forestFunctionality"), int))) {
337 if (ldb_msg_add_fmt(msg
, "forestFunctionality",
343 if (priv
&& do_attribute(attrs
, "domainControllerFunctionality")
344 && (val
= talloc_get_type(ldb_get_opaque(ldb
, "domainControllerFunctionality"), int))) {
345 if (ldb_msg_add_fmt(msg
, "domainControllerFunctionality",
351 edn_control
= ldb_request_get_control(req
, LDB_CONTROL_EXTENDED_DN_OID
);
353 /* if the client sent us the EXTENDED_DN control then we need
354 to expand the DNs to have GUID and SID. W2K8 join relies on
359 for (i
=0; dn_attrs
[i
]; i
++) {
360 if (!do_attribute(attrs
, dn_attrs
[i
])) continue;
361 ret
= expand_dn_in_message(module
, msg
, dn_attrs
[i
],
363 if (ret
!= LDB_SUCCESS
) {
364 DEBUG(0,(__location__
": Failed to expand DN in rootDSE for %s\n",
371 if (do_attribute(attrs
, "isGlobalCatalogReady")) {
372 /* MS-ADTS 3.1.1.3.2.10
373 Note, we should only return true here is we have
374 completed at least one synchronisation. As both
375 provision and vampire do a full sync, this means we
376 can return true is the gc bit is set in the NTDSDSA
378 if (ldb_msg_add_fmt(msg
, "isGlobalCatalogReady",
379 "%s", samdb_is_gc(ldb
)?"TRUE":"FALSE") != 0) {
384 /* TODO: lots more dynamic attributes should be added here */
389 return LDB_ERR_OPERATIONS_ERROR
;
393 handle search requests
396 struct rootdse_context
{
397 struct ldb_module
*module
;
398 struct ldb_request
*req
;
401 static struct rootdse_context
*rootdse_init_context(struct ldb_module
*module
,
402 struct ldb_request
*req
)
404 struct ldb_context
*ldb
;
405 struct rootdse_context
*ac
;
407 ldb
= ldb_module_get_ctx(module
);
409 ac
= talloc_zero(req
, struct rootdse_context
);
411 ldb_set_errstring(ldb
, "Out of Memory");
421 static int rootdse_callback(struct ldb_request
*req
, struct ldb_reply
*ares
)
423 struct rootdse_context
*ac
;
426 ac
= talloc_get_type(req
->context
, struct rootdse_context
);
429 return ldb_module_done(ac
->req
, NULL
, NULL
,
430 LDB_ERR_OPERATIONS_ERROR
);
432 if (ares
->error
!= LDB_SUCCESS
) {
433 return ldb_module_done(ac
->req
, ares
->controls
,
434 ares
->response
, ares
->error
);
437 switch (ares
->type
) {
438 case LDB_REPLY_ENTRY
:
440 * if the client explicit asks for the 'netlogon' attribute
441 * the reply_entry needs to be skipped
443 if (ac
->req
->op
.search
.attrs
&&
444 ldb_attr_in_list(ac
->req
->op
.search
.attrs
, "netlogon")) {
449 /* for each record returned post-process to add any dynamic
450 attributes that have been asked for */
451 ret
= rootdse_add_dynamic(ac
->module
, ares
->message
,
452 ac
->req
->op
.search
.attrs
, ac
->req
);
453 if (ret
!= LDB_SUCCESS
) {
455 return ldb_module_done(ac
->req
, NULL
, NULL
, ret
);
458 return ldb_module_send_entry(ac
->req
, ares
->message
, ares
->controls
);
460 case LDB_REPLY_REFERRAL
:
461 /* should we allow the backend to return referrals in this case
466 return ldb_module_done(ac
->req
, ares
->controls
,
467 ares
->response
, ares
->error
);
474 static int rootdse_search(struct ldb_module
*module
, struct ldb_request
*req
)
476 struct ldb_context
*ldb
;
477 struct rootdse_context
*ac
;
478 struct ldb_request
*down_req
;
481 ldb
= ldb_module_get_ctx(module
);
483 /* see if its for the rootDSE - only a base search on the "" DN qualifies */
484 if (!(req
->op
.search
.scope
== LDB_SCOPE_BASE
&& ldb_dn_is_null(req
->op
.search
.base
))) {
485 /* Otherwise, pass down to the rest of the stack */
486 return ldb_next_request(module
, req
);
489 ac
= rootdse_init_context(module
, req
);
491 return LDB_ERR_OPERATIONS_ERROR
;
494 /* in our db we store the rootDSE with a DN of @ROOTDSE */
495 ret
= ldb_build_search_req(&down_req
, ldb
, ac
,
496 ldb_dn_new(ac
, ldb
, "@ROOTDSE"),
499 req
->op
.search
.attrs
,
500 NULL
,/* for now skip the controls from the client */
501 ac
, rootdse_callback
,
503 if (ret
!= LDB_SUCCESS
) {
507 return ldb_next_request(module
, down_req
);
510 static int rootdse_register_control(struct ldb_module
*module
, struct ldb_request
*req
)
512 struct private_data
*priv
= talloc_get_type(ldb_module_get_private(module
), struct private_data
);
515 list
= talloc_realloc(priv
, priv
->controls
, char *, priv
->num_controls
+ 1);
517 return LDB_ERR_OPERATIONS_ERROR
;
520 list
[priv
->num_controls
] = talloc_strdup(list
, req
->op
.reg_control
.oid
);
521 if (!list
[priv
->num_controls
]) {
522 return LDB_ERR_OPERATIONS_ERROR
;
525 priv
->num_controls
+= 1;
526 priv
->controls
= list
;
528 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
531 static int rootdse_register_partition(struct ldb_module
*module
, struct ldb_request
*req
)
533 struct private_data
*priv
= talloc_get_type(ldb_module_get_private(module
), struct private_data
);
534 struct ldb_dn
**list
;
536 list
= talloc_realloc(priv
, priv
->partitions
, struct ldb_dn
*, priv
->num_partitions
+ 1);
538 return LDB_ERR_OPERATIONS_ERROR
;
541 list
[priv
->num_partitions
] = ldb_dn_copy(list
, req
->op
.reg_partition
.dn
);
542 if (!list
[priv
->num_partitions
]) {
543 return LDB_ERR_OPERATIONS_ERROR
;
546 priv
->num_partitions
+= 1;
547 priv
->partitions
= list
;
549 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);
553 static int rootdse_request(struct ldb_module
*module
, struct ldb_request
*req
)
555 switch (req
->operation
) {
557 case LDB_REQ_REGISTER_CONTROL
:
558 return rootdse_register_control(module
, req
);
559 case LDB_REQ_REGISTER_PARTITION
:
560 return rootdse_register_partition(module
, req
);
565 return ldb_next_request(module
, req
);
568 static int rootdse_init(struct ldb_module
*module
)
571 struct ldb_context
*ldb
;
572 struct ldb_result
*res
;
573 struct private_data
*data
;
574 const char *attrs
[] = { "msDS-Behavior-Version", NULL
};
575 const char *ds_attrs
[] = { "dsServiceName", NULL
};
578 ldb
= ldb_module_get_ctx(module
);
580 data
= talloc_zero(module
, struct private_data
);
585 data
->num_controls
= 0;
586 data
->controls
= NULL
;
587 data
->num_partitions
= 0;
588 data
->partitions
= NULL
;
589 ldb_module_set_private(module
, data
);
591 ldb_set_default_dns(ldb
);
593 ret
= ldb_next_init(module
);
599 mem_ctx
= talloc_new(data
);
602 return LDB_ERR_OPERATIONS_ERROR
;
605 /* Now that the partitions are set up, do a search for:
606 - domainControllerFunctionality
607 - domainFunctionality
608 - forestFunctionality
610 Then stuff these values into an opaque
612 ret
= ldb_search(ldb
, mem_ctx
, &res
,
613 ldb_get_default_basedn(ldb
),
614 LDB_SCOPE_BASE
, attrs
, NULL
);
615 if (ret
== LDB_SUCCESS
&& res
->count
== 1) {
616 int domain_behaviour_version
617 = ldb_msg_find_attr_as_int(res
->msgs
[0],
618 "msDS-Behavior-Version", -1);
619 if (domain_behaviour_version
!= -1) {
620 int *val
= talloc(ldb
, int);
623 talloc_free(mem_ctx
);
624 return LDB_ERR_OPERATIONS_ERROR
;
626 *val
= domain_behaviour_version
;
627 ret
= ldb_set_opaque(ldb
, "domainFunctionality", val
);
628 if (ret
!= LDB_SUCCESS
) {
629 talloc_free(mem_ctx
);
635 ret
= ldb_search(ldb
, mem_ctx
, &res
,
636 samdb_partitions_dn(ldb
, mem_ctx
),
637 LDB_SCOPE_BASE
, attrs
, NULL
);
638 if (ret
== LDB_SUCCESS
&& res
->count
== 1) {
639 int forest_behaviour_version
640 = ldb_msg_find_attr_as_int(res
->msgs
[0],
641 "msDS-Behavior-Version", -1);
642 if (forest_behaviour_version
!= -1) {
643 int *val
= talloc(ldb
, int);
646 talloc_free(mem_ctx
);
647 return LDB_ERR_OPERATIONS_ERROR
;
649 *val
= forest_behaviour_version
;
650 ret
= ldb_set_opaque(ldb
, "forestFunctionality", val
);
651 if (ret
!= LDB_SUCCESS
) {
652 talloc_free(mem_ctx
);
658 ret
= ldb_search(ldb
, mem_ctx
, &res
,
659 ldb_dn_new(mem_ctx
, ldb
, ""),
660 LDB_SCOPE_BASE
, ds_attrs
, NULL
);
661 if (ret
== LDB_SUCCESS
&& res
->count
== 1) {
663 = ldb_msg_find_attr_as_dn(ldb
, mem_ctx
, res
->msgs
[0],
666 ret
= ldb_search(ldb
, mem_ctx
, &res
, ds_dn
,
667 LDB_SCOPE_BASE
, attrs
, NULL
);
668 if (ret
== LDB_SUCCESS
&& res
->count
== 1) {
669 int domain_controller_behaviour_version
670 = ldb_msg_find_attr_as_int(res
->msgs
[0],
671 "msDS-Behavior-Version", -1);
672 if (domain_controller_behaviour_version
!= -1) {
673 int *val
= talloc(ldb
, int);
676 talloc_free(mem_ctx
);
677 return LDB_ERR_OPERATIONS_ERROR
;
679 *val
= domain_controller_behaviour_version
;
680 ret
= ldb_set_opaque(ldb
,
681 "domainControllerFunctionality", val
);
682 if (ret
!= LDB_SUCCESS
) {
683 talloc_free(mem_ctx
);
691 talloc_free(mem_ctx
);
697 * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
698 * to a DN and a GUID object
700 static int get_optional_feature_dn_guid(struct ldb_request
*req
, struct ldb_context
*ldb
,
702 struct ldb_dn
**op_feature_scope_dn
,
703 struct GUID
*op_feature_guid
)
705 const struct ldb_message
*msg
= req
->op
.mod
.message
;
706 const char *ldb_val_str
;
708 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
711 ldb_val_str
= ldb_msg_find_attr_as_string(msg
, "enableOptionalFeature", NULL
);
713 ldb_asprintf_errstring(ldb
,
714 "rootdse: unable to find enableOptionalFeature\n");
715 return LDB_ERR_UNWILLING_TO_PERFORM
;
718 guid
= strchr(ldb_val_str
, ':');
720 ldb_asprintf_errstring(ldb
,
721 "rootdse: unable to find GUID in enableOptionalFeature\n");
722 return LDB_ERR_UNWILLING_TO_PERFORM
;
724 status
= GUID_from_string(guid
+1, op_feature_guid
);
725 if (!NT_STATUS_IS_OK(status
)) {
726 ldb_asprintf_errstring(ldb
,
727 "rootdse: bad GUID in enableOptionalFeature\n");
728 return LDB_ERR_UNWILLING_TO_PERFORM
;
731 dn
= talloc_strndup(tmp_ctx
, ldb_val_str
, guid
-ldb_val_str
);
733 ldb_asprintf_errstring(ldb
,
734 "rootdse: bad DN in enableOptionalFeature\n");
735 return LDB_ERR_UNWILLING_TO_PERFORM
;
738 *op_feature_scope_dn
= ldb_dn_new(mem_ctx
, ldb
, dn
);
740 talloc_free(tmp_ctx
);
745 * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
746 * ldb_message object.
748 static int dsdb_find_optional_feature(struct ldb_module
*module
, struct ldb_context
*ldb
,
749 TALLOC_CTX
*mem_ctx
, struct GUID op_feature_guid
, struct ldb_message
**msg
)
751 struct ldb_result
*res
;
752 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
755 ret
= dsdb_module_search(module
, tmp_ctx
, &res
, NULL
, LDB_SCOPE_SUBTREE
,
757 DSDB_SEARCH_SEARCH_ALL_PARTITIONS
,
758 "(&(objectClass=msDS-OptionalFeature)"
759 "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx
, &op_feature_guid
));
761 if (ret
!= LDB_SUCCESS
) {
762 talloc_free(tmp_ctx
);
765 if (res
->count
== 0) {
766 talloc_free(tmp_ctx
);
767 return LDB_ERR_NO_SUCH_OBJECT
;
769 if (res
->count
!= 1) {
770 ldb_asprintf_errstring(ldb
,
771 "More than one object found matching optional feature GUID %s\n",
772 GUID_string(tmp_ctx
, &op_feature_guid
));
773 talloc_free(tmp_ctx
);
774 return LDB_ERR_OPERATIONS_ERROR
;
777 *msg
= talloc_steal(mem_ctx
, res
->msgs
[0]);
779 talloc_free(tmp_ctx
);
783 static int rootdse_enable_recycle_bin(struct ldb_module
*module
,struct ldb_context
*ldb
,
784 TALLOC_CTX
*mem_ctx
, struct ldb_dn
*op_feature_scope_dn
,
785 struct ldb_message
*op_feature_msg
)
788 const int domain_func_level
= dsdb_functional_level(ldb
);
789 struct ldb_dn
*ntds_settings_dn
;
791 unsigned int el_count
= 0;
792 struct ldb_message
*msg
;
794 ret
= ldb_msg_find_attr_as_int(op_feature_msg
, "msDS-RequiredForestBehaviorVersion", 0);
795 if (domain_func_level
< ret
){
796 ldb_asprintf_errstring(ldb
,
797 "rootdse: Domain functional level must be at least %d\n",
799 return LDB_ERR_UNWILLING_TO_PERFORM
;
802 tmp_ctx
= talloc_new(mem_ctx
);
803 ntds_settings_dn
= samdb_ntds_settings_dn(ldb
);
804 if (!ntds_settings_dn
) {
805 ldb_asprintf_errstring(ldb
,
806 __location__
": Failed to find NTDS settings DN\n");
807 ret
= LDB_ERR_OPERATIONS_ERROR
;
808 talloc_free(tmp_ctx
);
812 ntds_settings_dn
= ldb_dn_copy(tmp_ctx
, ntds_settings_dn
);
813 if (!ntds_settings_dn
) {
814 DEBUG(0, (__location__
": Failed to copy NTDS settings DN\n"));
815 ret
= LDB_ERR_OPERATIONS_ERROR
;
816 talloc_free(tmp_ctx
);
820 msg
= ldb_msg_new(tmp_ctx
);
821 msg
->dn
= ntds_settings_dn
;
823 ldb_msg_add_linearized_dn(msg
, "msDS-EnabledFeature", op_feature_msg
->dn
);
824 msg
->elements
[el_count
++].flags
= LDB_FLAG_MOD_ADD
;
826 ret
= dsdb_module_modify(module
, msg
, 0);
827 if (ret
!= LDB_SUCCESS
) {
828 ldb_asprintf_errstring(ldb
,
829 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
830 ldb_dn_get_linearized(ntds_settings_dn
), ldb_errstring(ldb
));
831 talloc_free(tmp_ctx
);
835 msg
->dn
= op_feature_scope_dn
;
836 ret
= dsdb_module_modify(module
, msg
, 0);
837 if (ret
!= LDB_SUCCESS
) {
838 ldb_asprintf_errstring(ldb
, "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
839 ldb_dn_get_linearized(op_feature_scope_dn
), ldb_errstring(ldb
));
840 talloc_free(tmp_ctx
);
847 static int rootdse_enableoptionalfeature(struct ldb_module
*module
, struct ldb_request
*req
)
851 - check for system (only system can enable features)
852 - extract GUID from the request
853 - find the feature object
854 - check functional level, must be at least msDS-RequiredForestBehaviorVersion
855 - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
856 - add/modify objects (see ntdsconnection code for an example)
859 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
860 struct GUID op_feature_guid
;
861 struct ldb_dn
*op_feature_scope_dn
;
862 struct ldb_message
*op_feature_msg
;
863 struct auth_session_info
*session_info
=
864 (struct auth_session_info
*)ldb_get_opaque(ldb
, "sessionInfo");
865 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
867 const char *guid_string
;
869 if (security_session_user_level(session_info
) != SECURITY_SYSTEM
) {
870 ldb_asprintf_errstring(ldb
, "rootdse: Insufficient rights for enableoptionalfeature");
871 return LDB_ERR_UNWILLING_TO_PERFORM
;
874 ret
= get_optional_feature_dn_guid(req
, ldb
, tmp_ctx
, &op_feature_scope_dn
, &op_feature_guid
);
875 if (ret
!= LDB_SUCCESS
) {
876 talloc_free(tmp_ctx
);
880 guid_string
= GUID_string(tmp_ctx
, &op_feature_guid
);
882 ldb_asprintf_errstring(ldb
, "rootdse: bad optional feature GUID");
883 return LDB_ERR_UNWILLING_TO_PERFORM
;
886 ret
= dsdb_find_optional_feature(module
, ldb
, tmp_ctx
, op_feature_guid
, &op_feature_msg
);
887 if (ret
!= LDB_SUCCESS
) {
888 ldb_asprintf_errstring(ldb
, "rootdse: unable to find optional feature for %s - %s",
889 guid_string
, ldb_errstring(ldb
));
890 talloc_free(tmp_ctx
);
894 if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN
, guid_string
) == 0) {
895 ret
= rootdse_enable_recycle_bin(module
, ldb
,
896 tmp_ctx
, op_feature_scope_dn
,
899 ldb_asprintf_errstring(ldb
, "rootdse: unknown optional feature %s",
901 talloc_free(tmp_ctx
);
902 return LDB_ERR_UNWILLING_TO_PERFORM
;
904 if (ret
!= LDB_SUCCESS
) {
905 ldb_asprintf_errstring(ldb
, "rootdse: failed to set optional feature for %s - %s",
906 guid_string
, ldb_errstring(ldb
));
907 talloc_free(tmp_ctx
);
911 talloc_free(tmp_ctx
);
912 return ldb_module_done(req
, NULL
, NULL
, LDB_SUCCESS
);;
915 static int rootdse_schemaupdatenow(struct ldb_module
*module
, struct ldb_request
*req
)
917 struct ldb_context
*ldb
= ldb_module_get_ctx(module
);
918 struct ldb_result
*ext_res
;
920 struct ldb_dn
*schema_dn
;
922 schema_dn
= samdb_schema_dn(ldb
);
924 ldb_reset_err_string(ldb
);
925 ldb_debug(ldb
, LDB_DEBUG_WARNING
,
926 "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
927 return ldb_next_request(module
, req
);
930 ret
= ldb_extended(ldb
, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID
, schema_dn
, &ext_res
);
931 if (ret
!= LDB_SUCCESS
) {
932 return LDB_ERR_OPERATIONS_ERROR
;
935 talloc_free(ext_res
);
936 return ldb_module_done(req
, NULL
, NULL
, ret
);
939 static int rootdse_modify(struct ldb_module
*module
, struct ldb_request
*req
)
941 struct ldb_context
*ldb
;
944 If dn is not "" we should let it pass through
946 if (!ldb_dn_is_null(req
->op
.mod
.message
->dn
)) {
947 return ldb_next_request(module
, req
);
950 ldb
= ldb_module_get_ctx(module
);
953 dn is empty so check for schemaUpdateNow attribute
954 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
956 if (ldb_msg_find_element(req
->op
.mod
.message
, "schemaUpdateNow")) {
957 return rootdse_schemaupdatenow(module
, req
);
960 if (ldb_msg_find_element(req
->op
.mod
.message
, "enableOptionalFeature")) {
961 return rootdse_enableoptionalfeature(module
, req
);
964 return LDB_ERR_OPERATIONS_ERROR
;
967 _PUBLIC_
const struct ldb_module_ops ldb_rootdse_module_ops
= {
969 .init_context
= rootdse_init
,
970 .search
= rootdse_search
,
971 .request
= rootdse_request
,
972 .modify
= rootdse_modify