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"
32 #include "auth/auth.h"
33 #include "param/param.h"
34 #include "lib/messaging/irpc.h"
35 #include "librpc/gen_ndr/ndr_irpc.h"
38 unsigned int num_controls;
40 unsigned int num_partitions;
41 struct ldb_dn **partitions;
45 return 1 if a specific attribute has been requested
47 static int do_attribute(const char * const *attrs, const char *name)
49 return attrs == NULL ||
50 ldb_attr_in_list(attrs, name) ||
51 ldb_attr_in_list(attrs, "*");
54 static int do_attribute_explicit(const char * const *attrs, const char *name)
56 return attrs != NULL && ldb_attr_in_list(attrs, name);
61 expand a DN attribute to include extended DN information if requested
63 static int expand_dn_in_message(struct ldb_module *module, struct ldb_message *msg,
64 const char *attrname, struct ldb_control *edn_control,
65 struct ldb_request *req)
67 struct ldb_dn *dn, *dn2;
70 struct ldb_request *req2;
72 const char *no_attrs[] = { NULL };
73 struct ldb_result *res;
74 struct ldb_extended_dn_control *edn;
75 TALLOC_CTX *tmp_ctx = talloc_new(req);
76 struct ldb_context *ldb;
79 ldb = ldb_module_get_ctx(module);
81 edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
86 v = discard_const_p(struct ldb_val, ldb_msg_find_ldb_val(msg, attrname));
92 dn_string = talloc_strndup(tmp_ctx, (const char *)v->data, v->length);
93 if (dn_string == NULL) {
95 return ldb_operr(ldb);
98 res = talloc_zero(tmp_ctx, struct ldb_result);
100 talloc_free(tmp_ctx);
101 return ldb_operr(ldb);
104 dn = ldb_dn_new(tmp_ctx, ldb, dn_string);
105 if (!ldb_dn_validate(dn)) {
106 talloc_free(tmp_ctx);
107 return ldb_operr(ldb);
110 ret = ldb_build_search_req(&req2, ldb, tmp_ctx,
116 res, ldb_search_default_callback,
118 if (ret != LDB_SUCCESS) {
119 talloc_free(tmp_ctx);
124 ret = ldb_request_add_control(req2,
125 LDB_CONTROL_EXTENDED_DN_OID,
126 edn_control->critical, edn);
127 if (ret != LDB_SUCCESS) {
128 talloc_free(tmp_ctx);
132 ret = ldb_next_request(module, req2);
133 if (ret == LDB_SUCCESS) {
134 ret = ldb_wait(req2->handle, LDB_WAIT_ALL);
136 if (ret != LDB_SUCCESS) {
137 talloc_free(tmp_ctx);
141 if (!res || res->count != 1) {
142 talloc_free(tmp_ctx);
143 return ldb_operr(ldb);
146 dn2 = res->msgs[0]->dn;
148 v->data = (uint8_t *)ldb_dn_get_extended_linearized(msg->elements, dn2, edn_type);
149 v->length = strlen((char *)v->data);
151 if (v->data == NULL) {
152 talloc_free(tmp_ctx);
153 return ldb_operr(ldb);
156 talloc_free(tmp_ctx);
163 add dynamically generated attributes to rootDSE result
165 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg,
166 const char * const *attrs, struct ldb_request *req)
168 struct ldb_context *ldb;
169 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
171 const struct dsdb_schema *schema;
173 struct ldb_control *edn_control;
174 const char *dn_attrs[] = {
175 "configurationNamingContext",
176 "defaultNamingContext",
178 "rootDomainNamingContext",
179 "schemaNamingContext",
184 ldb = ldb_module_get_ctx(module);
185 schema = dsdb_get_schema(ldb, NULL);
187 msg->dn = ldb_dn_new(msg, ldb, NULL);
189 /* don't return the distinduishedName, cn and name attributes */
190 ldb_msg_remove_attr(msg, "distinguishedName");
191 ldb_msg_remove_attr(msg, "cn");
192 ldb_msg_remove_attr(msg, "name");
194 if (do_attribute(attrs, "currentTime")) {
195 if (ldb_msg_add_steal_string(msg, "currentTime",
196 ldb_timestring(msg, time(NULL))) != 0) {
201 if (priv && do_attribute(attrs, "supportedControl")) {
203 for (i = 0; i < priv->num_controls; i++) {
204 char *control = talloc_strdup(msg, priv->controls[i]);
208 if (ldb_msg_add_steal_string(msg, "supportedControl",
215 if (priv && do_attribute(attrs, "namingContexts")) {
217 for (i = 0; i < priv->num_partitions; i++) {
218 struct ldb_dn *dn = priv->partitions[i];
219 if (ldb_msg_add_steal_string(msg, "namingContexts",
220 ldb_dn_alloc_linearized(msg, dn)) != 0) {
226 server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
228 if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
230 for (i = 0; server_sasl && server_sasl[i]; i++) {
231 char *sasl_name = talloc_strdup(msg, server_sasl[i]);
235 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
242 if (do_attribute(attrs, "highestCommittedUSN")) {
244 int ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
245 if (ret == LDB_SUCCESS) {
246 if (ldb_msg_add_fmt(msg, "highestCommittedUSN",
247 "%llu", (unsigned long long)seq_num) != 0) {
253 if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
254 struct dsdb_attribute *cur;
257 for (cur = schema->attributes; cur; cur = cur->next) {
261 if (ldb_msg_add_fmt(msg, "dsSchemaAttrCount",
267 if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
268 struct dsdb_class *cur;
271 for (cur = schema->classes; cur; cur = cur->next) {
275 if (ldb_msg_add_fmt(msg, "dsSchemaClassCount",
281 if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
282 if (ldb_msg_add_fmt(msg, "dsSchemaPrefixCount",
283 "%u", schema->prefixmap->length) != 0) {
288 if (do_attribute_explicit(attrs, "validFSMOs")) {
289 const struct dsdb_naming_fsmo *naming_fsmo;
290 const struct dsdb_pdc_fsmo *pdc_fsmo;
293 if (schema && schema->fsmo.we_are_master) {
294 dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
295 if (dn_str && dn_str[0]) {
296 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
302 naming_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_naming_fsmo"),
303 struct dsdb_naming_fsmo);
304 if (naming_fsmo && naming_fsmo->we_are_master) {
305 dn_str = ldb_dn_get_linearized(samdb_partitions_dn(ldb, msg));
306 if (dn_str && dn_str[0]) {
307 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
313 pdc_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_pdc_fsmo"),
314 struct dsdb_pdc_fsmo);
315 if (pdc_fsmo && pdc_fsmo->we_are_master) {
316 dn_str = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
317 if (dn_str && dn_str[0]) {
318 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
325 if (do_attribute_explicit(attrs, "vendorVersion")) {
326 if (ldb_msg_add_fmt(msg, "vendorVersion",
327 "%s", SAMBA_VERSION_STRING) != 0) {
332 if (priv && do_attribute(attrs, "domainFunctionality")) {
333 if (ldb_msg_add_fmt(msg, "domainFunctionality",
334 "%d", dsdb_functional_level(ldb)) != 0) {
339 if (priv && do_attribute(attrs, "forestFunctionality")
340 && (val = talloc_get_type(ldb_get_opaque(ldb, "forestFunctionality"), int))) {
341 if (ldb_msg_add_fmt(msg, "forestFunctionality",
347 if (priv && do_attribute(attrs, "domainControllerFunctionality")
348 && (val = talloc_get_type(ldb_get_opaque(ldb, "domainControllerFunctionality"), int))) {
349 if (ldb_msg_add_fmt(msg, "domainControllerFunctionality",
355 edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
357 /* if the client sent us the EXTENDED_DN control then we need
358 to expand the DNs to have GUID and SID. W2K8 join relies on
363 for (i=0; dn_attrs[i]; i++) {
364 if (!do_attribute(attrs, dn_attrs[i])) continue;
365 ret = expand_dn_in_message(module, msg, dn_attrs[i],
367 if (ret != LDB_SUCCESS) {
368 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
375 if (do_attribute(attrs, "isGlobalCatalogReady")) {
376 /* MS-ADTS 3.1.1.3.2.10
377 Note, we should only return true here is we have
378 completed at least one synchronisation. As both
379 provision and vampire do a full sync, this means we
380 can return true is the gc bit is set in the NTDSDSA
382 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
383 "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != 0) {
388 if (do_attribute_explicit(attrs, "tokenGroups")) {
390 /* Obtain the user's session_info */
391 struct auth_session_info *session_info
392 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
393 if (session_info && session_info->security_token) {
394 /* The list of groups this user is in */
395 for (i = 0; i < session_info->security_token->num_sids; i++) {
396 if (samdb_msg_add_dom_sid(ldb, msg, msg,
398 &session_info->security_token->sids[i]) != 0) {
405 /* TODO: lots more dynamic attributes should be added here */
410 return ldb_operr(ldb);
414 handle search requests
417 struct rootdse_context {
418 struct ldb_module *module;
419 struct ldb_request *req;
422 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
423 struct ldb_request *req)
425 struct ldb_context *ldb;
426 struct rootdse_context *ac;
428 ldb = ldb_module_get_ctx(module);
430 ac = talloc_zero(req, struct rootdse_context);
432 ldb_set_errstring(ldb, "Out of Memory");
442 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
444 struct rootdse_context *ac;
447 ac = talloc_get_type(req->context, struct rootdse_context);
450 return ldb_module_done(ac->req, NULL, NULL,
451 LDB_ERR_OPERATIONS_ERROR);
453 if (ares->error != LDB_SUCCESS) {
454 return ldb_module_done(ac->req, ares->controls,
455 ares->response, ares->error);
458 switch (ares->type) {
459 case LDB_REPLY_ENTRY:
461 * if the client explicit asks for the 'netlogon' attribute
462 * the reply_entry needs to be skipped
464 if (ac->req->op.search.attrs &&
465 ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
470 /* for each record returned post-process to add any dynamic
471 attributes that have been asked for */
472 ret = rootdse_add_dynamic(ac->module, ares->message,
473 ac->req->op.search.attrs, ac->req);
474 if (ret != LDB_SUCCESS) {
476 return ldb_module_done(ac->req, NULL, NULL, ret);
479 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
481 case LDB_REPLY_REFERRAL:
482 /* should we allow the backend to return referrals in this case
487 return ldb_module_done(ac->req, ares->controls,
488 ares->response, ares->error);
495 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
497 struct ldb_context *ldb;
498 struct rootdse_context *ac;
499 struct ldb_request *down_req;
502 ldb = ldb_module_get_ctx(module);
504 /* see if its for the rootDSE - only a base search on the "" DN qualifies */
505 if (!(req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base))) {
506 /* Otherwise, pass down to the rest of the stack */
507 return ldb_next_request(module, req);
510 ac = rootdse_init_context(module, req);
512 return ldb_operr(ldb);
515 /* in our db we store the rootDSE with a DN of @ROOTDSE */
516 ret = ldb_build_search_req(&down_req, ldb, ac,
517 ldb_dn_new(ac, ldb, "@ROOTDSE"),
520 req->op.search.attrs,
521 NULL,/* for now skip the controls from the client */
522 ac, rootdse_callback,
524 if (ret != LDB_SUCCESS) {
528 return ldb_next_request(module, down_req);
531 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
533 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
536 list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
538 return ldb_oom(ldb_module_get_ctx(module));
541 list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
542 if (!list[priv->num_controls]) {
543 return ldb_oom(ldb_module_get_ctx(module));
546 priv->num_controls += 1;
547 priv->controls = list;
549 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
552 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
554 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
555 struct ldb_dn **list;
557 list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
559 return ldb_oom(ldb_module_get_ctx(module));
562 list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
563 if (!list[priv->num_partitions]) {
564 return ldb_operr(ldb_module_get_ctx(module));
567 priv->num_partitions += 1;
568 priv->partitions = list;
570 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
574 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
576 switch (req->operation) {
578 case LDB_REQ_REGISTER_CONTROL:
579 return rootdse_register_control(module, req);
580 case LDB_REQ_REGISTER_PARTITION:
581 return rootdse_register_partition(module, req);
586 return ldb_next_request(module, req);
589 static int rootdse_init(struct ldb_module *module)
592 struct ldb_context *ldb;
593 struct ldb_result *res;
594 struct private_data *data;
595 const char *attrs[] = { "msDS-Behavior-Version", NULL };
596 const char *ds_attrs[] = { "dsServiceName", NULL };
599 ldb = ldb_module_get_ctx(module);
601 data = talloc_zero(module, struct private_data);
606 data->num_controls = 0;
607 data->controls = NULL;
608 data->num_partitions = 0;
609 data->partitions = NULL;
610 ldb_module_set_private(module, data);
612 ldb_set_default_dns(ldb);
614 ret = ldb_next_init(module);
616 if (ret != LDB_SUCCESS) {
620 mem_ctx = talloc_new(data);
625 /* Now that the partitions are set up, do a search for:
626 - domainControllerFunctionality
627 - domainFunctionality
628 - forestFunctionality
630 Then stuff these values into an opaque
632 ret = ldb_search(ldb, mem_ctx, &res,
633 ldb_get_default_basedn(ldb),
634 LDB_SCOPE_BASE, attrs, NULL);
635 if (ret == LDB_SUCCESS && res->count == 1) {
636 int domain_behaviour_version
637 = ldb_msg_find_attr_as_int(res->msgs[0],
638 "msDS-Behavior-Version", -1);
639 if (domain_behaviour_version != -1) {
640 int *val = talloc(ldb, int);
642 talloc_free(mem_ctx);
645 *val = domain_behaviour_version;
646 ret = ldb_set_opaque(ldb, "domainFunctionality", val);
647 if (ret != LDB_SUCCESS) {
648 talloc_free(mem_ctx);
654 ret = ldb_search(ldb, mem_ctx, &res,
655 samdb_partitions_dn(ldb, mem_ctx),
656 LDB_SCOPE_BASE, attrs, NULL);
657 if (ret == LDB_SUCCESS && res->count == 1) {
658 int forest_behaviour_version
659 = ldb_msg_find_attr_as_int(res->msgs[0],
660 "msDS-Behavior-Version", -1);
661 if (forest_behaviour_version != -1) {
662 int *val = talloc(ldb, int);
664 talloc_free(mem_ctx);
667 *val = forest_behaviour_version;
668 ret = ldb_set_opaque(ldb, "forestFunctionality", val);
669 if (ret != LDB_SUCCESS) {
670 talloc_free(mem_ctx);
676 ret = ldb_search(ldb, mem_ctx, &res,
677 ldb_dn_new(mem_ctx, ldb, ""),
678 LDB_SCOPE_BASE, ds_attrs, NULL);
679 if (ret == LDB_SUCCESS && res->count == 1) {
681 = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
684 ret = ldb_search(ldb, mem_ctx, &res, ds_dn,
685 LDB_SCOPE_BASE, attrs, NULL);
686 if (ret == LDB_SUCCESS && res->count == 1) {
687 int domain_controller_behaviour_version
688 = ldb_msg_find_attr_as_int(res->msgs[0],
689 "msDS-Behavior-Version", -1);
690 if (domain_controller_behaviour_version != -1) {
691 int *val = talloc(ldb, int);
693 talloc_free(mem_ctx);
696 *val = domain_controller_behaviour_version;
697 ret = ldb_set_opaque(ldb,
698 "domainControllerFunctionality", val);
699 if (ret != LDB_SUCCESS) {
700 talloc_free(mem_ctx);
708 talloc_free(mem_ctx);
714 * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
715 * to a DN and a GUID object
717 static int get_optional_feature_dn_guid(struct ldb_request *req, struct ldb_context *ldb,
719 struct ldb_dn **op_feature_scope_dn,
720 struct GUID *op_feature_guid)
722 const struct ldb_message *msg = req->op.mod.message;
723 const char *ldb_val_str;
725 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
728 ldb_val_str = ldb_msg_find_attr_as_string(msg, "enableOptionalFeature", NULL);
730 ldb_set_errstring(ldb,
731 "rootdse: unable to find 'enableOptionalFeature'!");
732 return LDB_ERR_UNWILLING_TO_PERFORM;
735 guid = strchr(ldb_val_str, ':');
737 ldb_set_errstring(ldb,
738 "rootdse: unable to find GUID in 'enableOptionalFeature'!");
739 return LDB_ERR_UNWILLING_TO_PERFORM;
741 status = GUID_from_string(guid+1, op_feature_guid);
742 if (!NT_STATUS_IS_OK(status)) {
743 ldb_set_errstring(ldb,
744 "rootdse: bad GUID in 'enableOptionalFeature'!");
745 return LDB_ERR_UNWILLING_TO_PERFORM;
748 dn = talloc_strndup(tmp_ctx, ldb_val_str, guid-ldb_val_str);
750 ldb_set_errstring(ldb,
751 "rootdse: bad DN in 'enableOptionalFeature'!");
752 return LDB_ERR_UNWILLING_TO_PERFORM;
755 *op_feature_scope_dn = ldb_dn_new(mem_ctx, ldb, dn);
757 talloc_free(tmp_ctx);
762 * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
763 * ldb_message object.
765 static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_context *ldb,
766 TALLOC_CTX *mem_ctx, struct GUID op_feature_guid, struct ldb_message **msg)
768 struct ldb_result *res;
769 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
772 ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
774 DSDB_FLAG_NEXT_MODULE |
775 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
776 "(&(objectClass=msDS-OptionalFeature)"
777 "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx, &op_feature_guid));
779 if (ret != LDB_SUCCESS) {
780 talloc_free(tmp_ctx);
783 if (res->count == 0) {
784 talloc_free(tmp_ctx);
785 return LDB_ERR_NO_SUCH_OBJECT;
787 if (res->count != 1) {
788 ldb_asprintf_errstring(ldb,
789 "More than one object found matching optional feature GUID %s\n",
790 GUID_string(tmp_ctx, &op_feature_guid));
791 talloc_free(tmp_ctx);
792 return LDB_ERR_OPERATIONS_ERROR;
795 *msg = talloc_steal(mem_ctx, res->msgs[0]);
797 talloc_free(tmp_ctx);
801 static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_context *ldb,
802 TALLOC_CTX *mem_ctx, struct ldb_dn *op_feature_scope_dn,
803 struct ldb_message *op_feature_msg)
806 const int domain_func_level = dsdb_functional_level(ldb);
807 struct ldb_dn *ntds_settings_dn;
809 unsigned int el_count = 0;
810 struct ldb_message *msg;
812 ret = ldb_msg_find_attr_as_int(op_feature_msg, "msDS-RequiredForestBehaviorVersion", 0);
813 if (domain_func_level < ret){
814 ldb_asprintf_errstring(ldb,
815 "rootdse_enable_recycle_bin: Domain functional level must be at least %d\n",
817 return LDB_ERR_UNWILLING_TO_PERFORM;
820 tmp_ctx = talloc_new(mem_ctx);
821 ntds_settings_dn = samdb_ntds_settings_dn(ldb);
822 if (!ntds_settings_dn) {
823 DEBUG(0, (__location__ ": Failed to find NTDS settings DN\n"));
824 ret = LDB_ERR_OPERATIONS_ERROR;
825 talloc_free(tmp_ctx);
829 ntds_settings_dn = ldb_dn_copy(tmp_ctx, ntds_settings_dn);
830 if (!ntds_settings_dn) {
831 DEBUG(0, (__location__ ": Failed to copy NTDS settings DN\n"));
832 ret = LDB_ERR_OPERATIONS_ERROR;
833 talloc_free(tmp_ctx);
837 msg = ldb_msg_new(tmp_ctx);
838 msg->dn = ntds_settings_dn;
840 ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
841 msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
843 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE);
844 if (ret != LDB_SUCCESS) {
845 ldb_asprintf_errstring(ldb,
846 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
847 ldb_dn_get_linearized(ntds_settings_dn),
849 talloc_free(tmp_ctx);
853 msg->dn = op_feature_scope_dn;
854 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE);
855 if (ret != LDB_SUCCESS) {
856 ldb_asprintf_errstring(ldb,
857 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
858 ldb_dn_get_linearized(op_feature_scope_dn),
860 talloc_free(tmp_ctx);
867 static int rootdse_enableoptionalfeature(struct ldb_module *module, struct ldb_request *req)
871 - check for system (only system can enable features)
872 - extract GUID from the request
873 - find the feature object
874 - check functional level, must be at least msDS-RequiredForestBehaviorVersion
875 - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
876 - add/modify objects (see ntdsconnection code for an example)
879 struct ldb_context *ldb = ldb_module_get_ctx(module);
880 struct GUID op_feature_guid;
881 struct ldb_dn *op_feature_scope_dn;
882 struct ldb_message *op_feature_msg;
883 struct auth_session_info *session_info =
884 (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
885 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
887 const char *guid_string;
889 if (security_session_user_level(session_info, NULL) != SECURITY_SYSTEM) {
890 ldb_set_errstring(ldb, "rootdse: Insufficient rights for enableoptionalfeature");
891 return LDB_ERR_UNWILLING_TO_PERFORM;
894 ret = get_optional_feature_dn_guid(req, ldb, tmp_ctx, &op_feature_scope_dn, &op_feature_guid);
895 if (ret != LDB_SUCCESS) {
896 talloc_free(tmp_ctx);
900 guid_string = GUID_string(tmp_ctx, &op_feature_guid);
902 ldb_set_errstring(ldb, "rootdse: bad optional feature GUID");
903 return LDB_ERR_UNWILLING_TO_PERFORM;
906 ret = dsdb_find_optional_feature(module, ldb, tmp_ctx, op_feature_guid, &op_feature_msg);
907 if (ret != LDB_SUCCESS) {
908 ldb_asprintf_errstring(ldb,
909 "rootdse: unable to find optional feature for %s - %s",
910 guid_string, ldb_errstring(ldb));
911 talloc_free(tmp_ctx);
915 if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN, guid_string) == 0) {
916 ret = rootdse_enable_recycle_bin(module, ldb,
917 tmp_ctx, op_feature_scope_dn,
920 ldb_asprintf_errstring(ldb,
921 "rootdse: unknown optional feature %s",
923 talloc_free(tmp_ctx);
924 return LDB_ERR_UNWILLING_TO_PERFORM;
926 if (ret != LDB_SUCCESS) {
927 ldb_asprintf_errstring(ldb,
928 "rootdse: failed to set optional feature for %s - %s",
929 guid_string, ldb_errstring(ldb));
930 talloc_free(tmp_ctx);
934 talloc_free(tmp_ctx);
935 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);;
938 static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request *req)
940 struct ldb_context *ldb = ldb_module_get_ctx(module);
941 struct ldb_result *ext_res;
943 struct ldb_dn *schema_dn;
945 schema_dn = ldb_get_schema_basedn(ldb);
947 ldb_reset_err_string(ldb);
948 ldb_debug(ldb, LDB_DEBUG_WARNING,
949 "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
950 return ldb_next_request(module, req);
953 ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
954 if (ret != LDB_SUCCESS) {
955 return ldb_operr(ldb);
958 talloc_free(ext_res);
959 return ldb_module_done(req, NULL, NULL, ret);
962 static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
964 struct ldb_context *ldb = ldb_module_get_ctx(module);
967 If dn is not "" we should let it pass through
969 if (!ldb_dn_is_null(req->op.add.message->dn)) {
970 return ldb_next_request(module, req);
973 ldb_set_errstring(ldb, "rootdse_add: you cannot add a new rootdse entry!");
974 return LDB_ERR_NAMING_VIOLATION;
977 static int rootdse_become_master(struct ldb_module *module,
978 struct ldb_request *req,
981 struct drepl_takeFSMORole r;
982 struct server_id *sid;
983 struct messaging_context *msg;
984 struct ldb_context *ldb = ldb_module_get_ctx(module);
985 TALLOC_CTX *tmp_ctx = talloc_new(req);
986 struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
987 NTSTATUS status_call, status_fn;
989 msg = messaging_client_init(tmp_ctx, lpcfg_messaging_path(tmp_ctx, lp_ctx),
990 ldb_get_event_context(ldb));
992 sid = irpc_servers_byname(msg, tmp_ctx, "dreplsrv");
994 status_call = IRPC_CALL(msg, sid[0], irpc, DREPL_TAKEFSMOROLE, &r, NULL);
995 if (!NT_STATUS_IS_OK(status_call)) {
996 return LDB_ERR_OPERATIONS_ERROR;
998 status_fn = r.out.result;
999 if (!NT_STATUS_IS_OK(status_fn)) {
1000 return LDB_ERR_OPERATIONS_ERROR;
1002 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1005 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
1007 struct ldb_context *ldb = ldb_module_get_ctx(module);
1010 If dn is not "" we should let it pass through
1012 if (!ldb_dn_is_null(req->op.mod.message->dn)) {
1013 return ldb_next_request(module, req);
1017 dn is empty so check for schemaUpdateNow attribute
1018 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
1020 if (ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow")) {
1021 return rootdse_schemaupdatenow(module, req);
1023 if (ldb_msg_find_element(req->op.mod.message, "becomeDomainMaster")) {
1024 return rootdse_become_master(module, req, DREPL_NAMING_MASTER);
1026 if (ldb_msg_find_element(req->op.mod.message, "becomeInfrastructureMaster")) {
1027 return rootdse_become_master(module, req, DREPL_INFRASTRUCTURE_MASTER);
1029 if (ldb_msg_find_element(req->op.mod.message, "becomeRidMaster")) {
1030 return rootdse_become_master(module, req, DREPL_RID_MASTER);
1032 if (ldb_msg_find_element(req->op.mod.message, "becomeSchemaMaster")) {
1033 return rootdse_become_master(module, req, DREPL_SCHEMA_MASTER);
1035 if (ldb_msg_find_element(req->op.mod.message, "becomePdc")) {
1036 return rootdse_become_master(module, req, DREPL_PDC_MASTER);
1038 if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
1039 return rootdse_enableoptionalfeature(module, req);
1042 ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
1043 return LDB_ERR_UNWILLING_TO_PERFORM;
1046 static int rootdse_delete(struct ldb_module *module, struct ldb_request *req)
1048 struct ldb_context *ldb = ldb_module_get_ctx(module);
1051 If dn is not "" we should let it pass through
1053 if (!ldb_dn_is_null(req->op.del.dn)) {
1054 return ldb_next_request(module, req);
1057 ldb_set_errstring(ldb, "rootdse_remove: you cannot delete the rootdse entry!");
1058 return LDB_ERR_NO_SUCH_OBJECT;
1061 _PUBLIC_ const struct ldb_module_ops ldb_rootdse_module_ops = {
1063 .init_context = rootdse_init,
1064 .search = rootdse_search,
1065 .request = rootdse_request,
1067 .modify = rootdse_modify,
1068 .del = rootdse_delete