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 "libcli/security/session.h"
32 #include "librpc/ndr/libndr.h"
33 #include "auth/auth.h"
34 #include "param/param.h"
35 #include "lib/messaging/irpc.h"
36 #include "librpc/gen_ndr/ndr_irpc_c.h"
39 unsigned int num_controls;
41 unsigned int num_partitions;
42 struct ldb_dn **partitions;
46 return 1 if a specific attribute has been requested
48 static int do_attribute(const char * const *attrs, const char *name)
50 return attrs == NULL ||
51 ldb_attr_in_list(attrs, name) ||
52 ldb_attr_in_list(attrs, "*");
55 static int do_attribute_explicit(const char * const *attrs, const char *name)
57 return attrs != NULL && ldb_attr_in_list(attrs, name);
62 expand a DN attribute to include extended DN information if requested
64 static int expand_dn_in_message(struct ldb_module *module, struct ldb_message *msg,
65 const char *attrname, struct ldb_control *edn_control,
66 struct ldb_request *req)
68 struct ldb_dn *dn, *dn2;
71 struct ldb_request *req2;
73 const char *no_attrs[] = { NULL };
74 struct ldb_result *res;
75 struct ldb_extended_dn_control *edn;
76 TALLOC_CTX *tmp_ctx = talloc_new(req);
77 struct ldb_context *ldb;
80 ldb = ldb_module_get_ctx(module);
82 edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
87 v = discard_const_p(struct ldb_val, ldb_msg_find_ldb_val(msg, attrname));
93 dn_string = talloc_strndup(tmp_ctx, (const char *)v->data, v->length);
94 if (dn_string == NULL) {
96 return ldb_operr(ldb);
99 res = talloc_zero(tmp_ctx, struct ldb_result);
101 talloc_free(tmp_ctx);
102 return ldb_operr(ldb);
105 dn = ldb_dn_new(tmp_ctx, ldb, dn_string);
106 if (!ldb_dn_validate(dn)) {
107 talloc_free(tmp_ctx);
108 return ldb_operr(ldb);
111 ret = ldb_build_search_req(&req2, ldb, tmp_ctx,
117 res, ldb_search_default_callback,
119 LDB_REQ_SET_LOCATION(req2);
120 if (ret != LDB_SUCCESS) {
121 talloc_free(tmp_ctx);
126 ret = ldb_request_add_control(req2,
127 LDB_CONTROL_EXTENDED_DN_OID,
128 edn_control->critical, edn);
129 if (ret != LDB_SUCCESS) {
130 talloc_free(tmp_ctx);
134 ret = ldb_next_request(module, req2);
135 if (ret == LDB_SUCCESS) {
136 ret = ldb_wait(req2->handle, LDB_WAIT_ALL);
138 if (ret != LDB_SUCCESS) {
139 talloc_free(tmp_ctx);
143 if (!res || res->count != 1) {
144 talloc_free(tmp_ctx);
145 return ldb_operr(ldb);
148 dn2 = res->msgs[0]->dn;
150 v->data = (uint8_t *)ldb_dn_get_extended_linearized(msg->elements, dn2, edn_type);
151 if (v->data == NULL) {
152 talloc_free(tmp_ctx);
153 return ldb_operr(ldb);
155 v->length = strlen((char *)v->data);
158 talloc_free(tmp_ctx);
165 add dynamically generated attributes to rootDSE result
167 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg,
168 const char * const *attrs, struct ldb_request *req)
170 struct ldb_context *ldb;
171 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
173 const struct dsdb_schema *schema;
175 struct ldb_control *edn_control;
176 const char *dn_attrs[] = {
177 "configurationNamingContext",
178 "defaultNamingContext",
180 "rootDomainNamingContext",
181 "schemaNamingContext",
186 ldb = ldb_module_get_ctx(module);
187 schema = dsdb_get_schema(ldb, NULL);
189 msg->dn = ldb_dn_new(msg, ldb, NULL);
191 /* don't return the distinguishedName, cn and name attributes */
192 ldb_msg_remove_attr(msg, "distinguishedName");
193 ldb_msg_remove_attr(msg, "cn");
194 ldb_msg_remove_attr(msg, "name");
196 if (do_attribute(attrs, "serverName")) {
197 if (ldb_msg_add_linearized_dn(msg, "serverName",
198 samdb_server_dn(ldb, msg)) != LDB_SUCCESS) {
203 if (do_attribute(attrs, "dnsHostName")) {
204 if (ldb_msg_add_string(msg, "dnsHostName",
205 samdb_search_string(ldb, msg, samdb_server_dn(ldb, msg),
206 "dNSHostName", NULL)) != LDB_SUCCESS) {
211 if (do_attribute(attrs, "ldapServiceName")) {
212 struct loadparm_context *lp_ctx
213 = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
214 struct loadparm_context);
215 char *ldap_service_name, *hostname;
217 hostname = talloc_strdup(msg, lpcfg_netbios_name(lp_ctx));
218 if (hostname == NULL) {
221 strlower_m(hostname);
223 ldap_service_name = talloc_asprintf(msg, "%s:%s$@%s",
224 samdb_forest_name(ldb, msg),
225 hostname, lpcfg_realm(lp_ctx));
226 if (ldap_service_name == NULL) {
230 if (ldb_msg_add_string(msg, "ldapServiceName",
231 ldap_service_name) != LDB_SUCCESS) {
236 if (do_attribute(attrs, "currentTime")) {
237 if (ldb_msg_add_steal_string(msg, "currentTime",
238 ldb_timestring(msg, time(NULL))) != LDB_SUCCESS) {
243 if (priv && do_attribute(attrs, "supportedControl")) {
245 for (i = 0; i < priv->num_controls; i++) {
246 char *control = talloc_strdup(msg, priv->controls[i]);
250 if (ldb_msg_add_steal_string(msg, "supportedControl",
251 control) != LDB_SUCCESS) {
257 if (priv && do_attribute(attrs, "namingContexts")) {
259 for (i = 0; i < priv->num_partitions; i++) {
260 struct ldb_dn *dn = priv->partitions[i];
261 if (ldb_msg_add_steal_string(msg, "namingContexts",
262 ldb_dn_alloc_linearized(msg, dn)) != LDB_SUCCESS) {
268 server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
270 if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
272 for (i = 0; server_sasl && server_sasl[i]; i++) {
273 char *sasl_name = talloc_strdup(msg, server_sasl[i]);
277 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
278 sasl_name) != LDB_SUCCESS) {
284 if (do_attribute(attrs, "highestCommittedUSN")) {
286 int ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
287 if (ret == LDB_SUCCESS) {
288 if (ldb_msg_add_fmt(msg, "highestCommittedUSN",
289 "%llu", (unsigned long long)seq_num) != LDB_SUCCESS) {
295 if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
296 struct dsdb_attribute *cur;
299 for (cur = schema->attributes; cur; cur = cur->next) {
303 if (ldb_msg_add_fmt(msg, "dsSchemaAttrCount",
304 "%u", n) != LDB_SUCCESS) {
309 if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
310 struct dsdb_class *cur;
313 for (cur = schema->classes; cur; cur = cur->next) {
317 if (ldb_msg_add_fmt(msg, "dsSchemaClassCount",
318 "%u", n) != LDB_SUCCESS) {
323 if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
324 if (ldb_msg_add_fmt(msg, "dsSchemaPrefixCount",
325 "%u", schema->prefixmap->length) != LDB_SUCCESS) {
330 if (do_attribute_explicit(attrs, "validFSMOs")) {
331 const struct dsdb_naming_fsmo *naming_fsmo;
332 const struct dsdb_pdc_fsmo *pdc_fsmo;
335 if (schema && schema->fsmo.we_are_master) {
336 dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
337 if (dn_str && dn_str[0]) {
338 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
344 naming_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_naming_fsmo"),
345 struct dsdb_naming_fsmo);
346 if (naming_fsmo && naming_fsmo->we_are_master) {
347 dn_str = ldb_dn_get_linearized(samdb_partitions_dn(ldb, msg));
348 if (dn_str && dn_str[0]) {
349 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
355 pdc_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_pdc_fsmo"),
356 struct dsdb_pdc_fsmo);
357 if (pdc_fsmo && pdc_fsmo->we_are_master) {
358 dn_str = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
359 if (dn_str && dn_str[0]) {
360 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
367 if (do_attribute_explicit(attrs, "vendorVersion")) {
368 if (ldb_msg_add_fmt(msg, "vendorVersion",
369 "%s", SAMBA_VERSION_STRING) != LDB_SUCCESS) {
374 if (do_attribute(attrs, "domainFunctionality")) {
375 if (ldb_msg_add_fmt(msg, "domainFunctionality",
376 "%d", dsdb_functional_level(ldb)) != LDB_SUCCESS) {
381 if (do_attribute(attrs, "forestFunctionality")) {
382 if (ldb_msg_add_fmt(msg, "forestFunctionality",
383 "%d", dsdb_forest_functional_level(ldb)) != LDB_SUCCESS) {
388 if (do_attribute(attrs, "domainControllerFunctionality")
389 && (val = talloc_get_type(ldb_get_opaque(ldb, "domainControllerFunctionality"), int))) {
390 if (ldb_msg_add_fmt(msg, "domainControllerFunctionality",
391 "%d", *val) != LDB_SUCCESS) {
396 if (do_attribute(attrs, "isGlobalCatalogReady")) {
397 /* MS-ADTS 3.1.1.3.2.10
398 Note, we should only return true here is we have
399 completed at least one synchronisation. As both
400 provision and vampire do a full sync, this means we
401 can return true is the gc bit is set in the NTDSDSA
403 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
404 "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != LDB_SUCCESS) {
409 if (do_attribute_explicit(attrs, "tokenGroups")) {
411 /* Obtain the user's session_info */
412 struct auth_session_info *session_info
413 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
414 if (session_info && session_info->security_token) {
415 /* The list of groups this user is in */
416 for (i = 0; i < session_info->security_token->num_sids; i++) {
417 if (samdb_msg_add_dom_sid(ldb, msg, msg,
419 &session_info->security_token->sids[i]) != LDB_SUCCESS) {
426 /* TODO: lots more dynamic attributes should be added here */
428 edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
430 /* if the client sent us the EXTENDED_DN control then we need
431 to expand the DNs to have GUID and SID. W2K8 join relies on
436 for (i=0; dn_attrs[i]; i++) {
437 if (!do_attribute(attrs, dn_attrs[i])) continue;
438 ret = expand_dn_in_message(module, msg, dn_attrs[i],
440 if (ret != LDB_SUCCESS) {
441 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
451 return ldb_operr(ldb);
455 handle search requests
458 struct rootdse_context {
459 struct ldb_module *module;
460 struct ldb_request *req;
463 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
464 struct ldb_request *req)
466 struct ldb_context *ldb;
467 struct rootdse_context *ac;
469 ldb = ldb_module_get_ctx(module);
471 ac = talloc_zero(req, struct rootdse_context);
473 ldb_set_errstring(ldb, "Out of Memory");
483 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
485 struct rootdse_context *ac;
488 ac = talloc_get_type(req->context, struct rootdse_context);
491 return ldb_module_done(ac->req, NULL, NULL,
492 LDB_ERR_OPERATIONS_ERROR);
494 if (ares->error != LDB_SUCCESS) {
495 return ldb_module_done(ac->req, ares->controls,
496 ares->response, ares->error);
499 switch (ares->type) {
500 case LDB_REPLY_ENTRY:
502 * if the client explicit asks for the 'netlogon' attribute
503 * the reply_entry needs to be skipped
505 if (ac->req->op.search.attrs &&
506 ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
511 /* for each record returned post-process to add any dynamic
512 attributes that have been asked for */
513 ret = rootdse_add_dynamic(ac->module, ares->message,
514 ac->req->op.search.attrs, ac->req);
515 if (ret != LDB_SUCCESS) {
517 return ldb_module_done(ac->req, NULL, NULL, ret);
520 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
522 case LDB_REPLY_REFERRAL:
523 /* should we allow the backend to return referrals in this case
528 return ldb_module_done(ac->req, ares->controls,
529 ares->response, ares->error);
537 mark our registered controls as non-critical in the request
539 This is needed as clients may mark controls as critical even if they
540 are not needed at all in a request. For example, the centrify client
541 sets the SD_FLAGS control as critical on ldap modify requests which
542 are setting the dNSHostName attribute on the machine account. That
543 request doesn't need SD_FLAGS at all, but centrify adds it on all
546 static void rootdse_mark_noncritical(struct ldb_module *module, struct ldb_control **controls)
549 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
551 if (!controls) return;
553 for (i=0; controls[i]; i++) {
554 if (controls[i]->critical == 0) {
557 for (j=0; j<priv->num_controls; j++) {
558 if (strcasecmp(priv->controls[j], controls[i]->oid) == 0) {
559 controls[i]->critical = 0;
565 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
567 struct ldb_context *ldb;
568 struct rootdse_context *ac;
569 struct ldb_request *down_req;
572 rootdse_mark_noncritical(module, req->controls);
574 ldb = ldb_module_get_ctx(module);
576 /* see if its for the rootDSE - only a base search on the "" DN qualifies */
577 if (!(req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base))) {
578 /* Otherwise, pass down to the rest of the stack */
579 return ldb_next_request(module, req);
582 ac = rootdse_init_context(module, req);
584 return ldb_operr(ldb);
587 /* in our db we store the rootDSE with a DN of @ROOTDSE */
588 ret = ldb_build_search_req(&down_req, ldb, ac,
589 ldb_dn_new(ac, ldb, "@ROOTDSE"),
592 req->op.search.attrs,
593 NULL,/* for now skip the controls from the client */
594 ac, rootdse_callback,
596 LDB_REQ_SET_LOCATION(down_req);
597 if (ret != LDB_SUCCESS) {
601 return ldb_next_request(module, down_req);
604 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
606 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
609 list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
611 return ldb_oom(ldb_module_get_ctx(module));
614 list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
615 if (!list[priv->num_controls]) {
616 return ldb_oom(ldb_module_get_ctx(module));
619 priv->num_controls += 1;
620 priv->controls = list;
622 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
625 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
627 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
628 struct ldb_dn **list;
630 list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
632 return ldb_oom(ldb_module_get_ctx(module));
635 list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
636 if (!list[priv->num_partitions]) {
637 return ldb_operr(ldb_module_get_ctx(module));
640 priv->num_partitions += 1;
641 priv->partitions = list;
643 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
647 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
649 switch (req->operation) {
651 case LDB_REQ_REGISTER_CONTROL:
652 return rootdse_register_control(module, req);
653 case LDB_REQ_REGISTER_PARTITION:
654 return rootdse_register_partition(module, req);
659 return ldb_next_request(module, req);
662 static int rootdse_init(struct ldb_module *module)
665 struct ldb_context *ldb;
666 struct ldb_result *res;
667 struct private_data *data;
668 const char *attrs[] = { "msDS-Behavior-Version", NULL };
669 const char *ds_attrs[] = { "dsServiceName", NULL };
672 ldb = ldb_module_get_ctx(module);
674 data = talloc_zero(module, struct private_data);
679 data->num_controls = 0;
680 data->controls = NULL;
681 data->num_partitions = 0;
682 data->partitions = NULL;
683 ldb_module_set_private(module, data);
685 ldb_set_default_dns(ldb);
687 ret = ldb_next_init(module);
689 if (ret != LDB_SUCCESS) {
693 mem_ctx = talloc_new(data);
698 /* Now that the partitions are set up, do a search for:
699 - domainControllerFunctionality
700 - domainFunctionality
701 - forestFunctionality
703 Then stuff these values into an opaque
705 ret = ldb_search(ldb, mem_ctx, &res,
706 ldb_get_default_basedn(ldb),
707 LDB_SCOPE_BASE, attrs, NULL);
708 if (ret == LDB_SUCCESS && res->count == 1) {
709 int domain_behaviour_version
710 = ldb_msg_find_attr_as_int(res->msgs[0],
711 "msDS-Behavior-Version", -1);
712 if (domain_behaviour_version != -1) {
713 int *val = talloc(ldb, int);
715 talloc_free(mem_ctx);
718 *val = domain_behaviour_version;
719 ret = ldb_set_opaque(ldb, "domainFunctionality", val);
720 if (ret != LDB_SUCCESS) {
721 talloc_free(mem_ctx);
727 ret = ldb_search(ldb, mem_ctx, &res,
728 samdb_partitions_dn(ldb, mem_ctx),
729 LDB_SCOPE_BASE, attrs, NULL);
730 if (ret == LDB_SUCCESS && res->count == 1) {
731 int forest_behaviour_version
732 = ldb_msg_find_attr_as_int(res->msgs[0],
733 "msDS-Behavior-Version", -1);
734 if (forest_behaviour_version != -1) {
735 int *val = talloc(ldb, int);
737 talloc_free(mem_ctx);
740 *val = forest_behaviour_version;
741 ret = ldb_set_opaque(ldb, "forestFunctionality", val);
742 if (ret != LDB_SUCCESS) {
743 talloc_free(mem_ctx);
749 ret = ldb_search(ldb, mem_ctx, &res,
750 ldb_dn_new(mem_ctx, ldb, ""),
751 LDB_SCOPE_BASE, ds_attrs, NULL);
752 if (ret == LDB_SUCCESS && res->count == 1) {
754 = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
757 ret = ldb_search(ldb, mem_ctx, &res, ds_dn,
758 LDB_SCOPE_BASE, attrs, NULL);
759 if (ret == LDB_SUCCESS && res->count == 1) {
760 int domain_controller_behaviour_version
761 = ldb_msg_find_attr_as_int(res->msgs[0],
762 "msDS-Behavior-Version", -1);
763 if (domain_controller_behaviour_version != -1) {
764 int *val = talloc(ldb, int);
766 talloc_free(mem_ctx);
769 *val = domain_controller_behaviour_version;
770 ret = ldb_set_opaque(ldb,
771 "domainControllerFunctionality", val);
772 if (ret != LDB_SUCCESS) {
773 talloc_free(mem_ctx);
781 talloc_free(mem_ctx);
787 * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
788 * to a DN and a GUID object
790 static int get_optional_feature_dn_guid(struct ldb_request *req, struct ldb_context *ldb,
792 struct ldb_dn **op_feature_scope_dn,
793 struct GUID *op_feature_guid)
795 const struct ldb_message *msg = req->op.mod.message;
796 const char *ldb_val_str;
798 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
801 ldb_val_str = ldb_msg_find_attr_as_string(msg, "enableOptionalFeature", NULL);
803 ldb_set_errstring(ldb,
804 "rootdse: unable to find 'enableOptionalFeature'!");
805 return LDB_ERR_UNWILLING_TO_PERFORM;
808 guid = strchr(ldb_val_str, ':');
810 ldb_set_errstring(ldb,
811 "rootdse: unable to find GUID in 'enableOptionalFeature'!");
812 return LDB_ERR_UNWILLING_TO_PERFORM;
814 status = GUID_from_string(guid+1, op_feature_guid);
815 if (!NT_STATUS_IS_OK(status)) {
816 ldb_set_errstring(ldb,
817 "rootdse: bad GUID in 'enableOptionalFeature'!");
818 return LDB_ERR_UNWILLING_TO_PERFORM;
821 dn = talloc_strndup(tmp_ctx, ldb_val_str, guid-ldb_val_str);
823 ldb_set_errstring(ldb,
824 "rootdse: bad DN in 'enableOptionalFeature'!");
825 return LDB_ERR_UNWILLING_TO_PERFORM;
828 *op_feature_scope_dn = ldb_dn_new(mem_ctx, ldb, dn);
830 talloc_free(tmp_ctx);
835 * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
836 * ldb_message object.
838 static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_context *ldb,
839 TALLOC_CTX *mem_ctx, struct GUID op_feature_guid, struct ldb_message **msg)
841 struct ldb_result *res;
842 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
845 ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
847 DSDB_FLAG_NEXT_MODULE |
848 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
849 "(&(objectClass=msDS-OptionalFeature)"
850 "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx, &op_feature_guid));
852 if (ret != LDB_SUCCESS) {
853 talloc_free(tmp_ctx);
856 if (res->count == 0) {
857 talloc_free(tmp_ctx);
858 return LDB_ERR_NO_SUCH_OBJECT;
860 if (res->count != 1) {
861 ldb_asprintf_errstring(ldb,
862 "More than one object found matching optional feature GUID %s\n",
863 GUID_string(tmp_ctx, &op_feature_guid));
864 talloc_free(tmp_ctx);
865 return LDB_ERR_OPERATIONS_ERROR;
868 *msg = talloc_steal(mem_ctx, res->msgs[0]);
870 talloc_free(tmp_ctx);
874 static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_context *ldb,
875 TALLOC_CTX *mem_ctx, struct ldb_dn *op_feature_scope_dn,
876 struct ldb_message *op_feature_msg)
879 const int domain_func_level = dsdb_functional_level(ldb);
880 struct ldb_dn *ntds_settings_dn;
882 unsigned int el_count = 0;
883 struct ldb_message *msg;
885 ret = ldb_msg_find_attr_as_int(op_feature_msg, "msDS-RequiredForestBehaviorVersion", 0);
886 if (domain_func_level < ret){
887 ldb_asprintf_errstring(ldb,
888 "rootdse_enable_recycle_bin: Domain functional level must be at least %d\n",
890 return LDB_ERR_UNWILLING_TO_PERFORM;
893 tmp_ctx = talloc_new(mem_ctx);
894 ntds_settings_dn = samdb_ntds_settings_dn(ldb);
895 if (!ntds_settings_dn) {
896 DEBUG(0, (__location__ ": Failed to find NTDS settings DN\n"));
897 ret = LDB_ERR_OPERATIONS_ERROR;
898 talloc_free(tmp_ctx);
902 ntds_settings_dn = ldb_dn_copy(tmp_ctx, ntds_settings_dn);
903 if (!ntds_settings_dn) {
904 DEBUG(0, (__location__ ": Failed to copy NTDS settings DN\n"));
905 ret = LDB_ERR_OPERATIONS_ERROR;
906 talloc_free(tmp_ctx);
910 msg = ldb_msg_new(tmp_ctx);
911 msg->dn = ntds_settings_dn;
913 ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
914 msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
916 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE);
917 if (ret != LDB_SUCCESS) {
918 ldb_asprintf_errstring(ldb,
919 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
920 ldb_dn_get_linearized(ntds_settings_dn),
922 talloc_free(tmp_ctx);
926 msg->dn = op_feature_scope_dn;
927 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE);
928 if (ret != LDB_SUCCESS) {
929 ldb_asprintf_errstring(ldb,
930 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
931 ldb_dn_get_linearized(op_feature_scope_dn),
933 talloc_free(tmp_ctx);
940 static int rootdse_enableoptionalfeature(struct ldb_module *module, struct ldb_request *req)
944 - check for system (only system can enable features)
945 - extract GUID from the request
946 - find the feature object
947 - check functional level, must be at least msDS-RequiredForestBehaviorVersion
948 - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
949 - add/modify objects (see ntdsconnection code for an example)
952 struct ldb_context *ldb = ldb_module_get_ctx(module);
953 struct GUID op_feature_guid;
954 struct ldb_dn *op_feature_scope_dn;
955 struct ldb_message *op_feature_msg;
956 struct auth_session_info *session_info =
957 (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
958 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
960 const char *guid_string;
962 if (security_session_user_level(session_info, NULL) != SECURITY_SYSTEM) {
963 ldb_set_errstring(ldb, "rootdse: Insufficient rights for enableoptionalfeature");
964 return LDB_ERR_UNWILLING_TO_PERFORM;
967 ret = get_optional_feature_dn_guid(req, ldb, tmp_ctx, &op_feature_scope_dn, &op_feature_guid);
968 if (ret != LDB_SUCCESS) {
969 talloc_free(tmp_ctx);
973 guid_string = GUID_string(tmp_ctx, &op_feature_guid);
975 ldb_set_errstring(ldb, "rootdse: bad optional feature GUID");
976 return LDB_ERR_UNWILLING_TO_PERFORM;
979 ret = dsdb_find_optional_feature(module, ldb, tmp_ctx, op_feature_guid, &op_feature_msg);
980 if (ret != LDB_SUCCESS) {
981 ldb_asprintf_errstring(ldb,
982 "rootdse: unable to find optional feature for %s - %s",
983 guid_string, ldb_errstring(ldb));
984 talloc_free(tmp_ctx);
988 if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN, guid_string) == 0) {
989 ret = rootdse_enable_recycle_bin(module, ldb,
990 tmp_ctx, op_feature_scope_dn,
993 ldb_asprintf_errstring(ldb,
994 "rootdse: unknown optional feature %s",
996 talloc_free(tmp_ctx);
997 return LDB_ERR_UNWILLING_TO_PERFORM;
999 if (ret != LDB_SUCCESS) {
1000 ldb_asprintf_errstring(ldb,
1001 "rootdse: failed to set optional feature for %s - %s",
1002 guid_string, ldb_errstring(ldb));
1003 talloc_free(tmp_ctx);
1007 talloc_free(tmp_ctx);
1008 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);;
1011 static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request *req)
1013 struct ldb_context *ldb = ldb_module_get_ctx(module);
1014 struct ldb_result *ext_res;
1016 struct ldb_dn *schema_dn;
1018 schema_dn = ldb_get_schema_basedn(ldb);
1020 ldb_reset_err_string(ldb);
1021 ldb_debug(ldb, LDB_DEBUG_WARNING,
1022 "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
1023 return ldb_next_request(module, req);
1026 ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
1027 if (ret != LDB_SUCCESS) {
1028 return ldb_operr(ldb);
1031 talloc_free(ext_res);
1032 return ldb_module_done(req, NULL, NULL, ret);
1035 static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
1037 struct ldb_context *ldb = ldb_module_get_ctx(module);
1039 rootdse_mark_noncritical(module, req->controls);
1042 If dn is not "" we should let it pass through
1044 if (!ldb_dn_is_null(req->op.add.message->dn)) {
1045 return ldb_next_request(module, req);
1048 ldb_set_errstring(ldb, "rootdse_add: you cannot add a new rootdse entry!");
1049 return LDB_ERR_NAMING_VIOLATION;
1052 static int rootdse_become_master(struct ldb_module *module,
1053 struct ldb_request *req,
1056 struct drepl_takeFSMORole r;
1057 struct messaging_context *msg;
1058 struct ldb_context *ldb = ldb_module_get_ctx(module);
1059 TALLOC_CTX *tmp_ctx = talloc_new(req);
1060 struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
1061 NTSTATUS status_call;
1064 struct dcerpc_binding_handle *irpc_handle;
1067 ret = samdb_rodc(ldb, &am_rodc);
1068 if (ret != LDB_SUCCESS) {
1069 return ldb_error(ldb, ret, "Could not determine if server is RODC.");
1073 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM,
1074 "RODC cannot become a role master.");
1077 msg = messaging_client_init(tmp_ctx, lpcfg_messaging_path(tmp_ctx, lp_ctx),
1078 ldb_get_event_context(ldb));
1080 ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s", lpcfg_messaging_path(tmp_ctx, lp_ctx));
1081 return LDB_ERR_OPERATIONS_ERROR;
1083 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg,
1086 if (irpc_handle == NULL) {
1087 return ldb_oom(ldb);
1091 status_call = dcerpc_drepl_takeFSMORole_r(irpc_handle, tmp_ctx, &r);
1092 if (!NT_STATUS_IS_OK(status_call)) {
1093 return LDB_ERR_OPERATIONS_ERROR;
1095 status_fn = r.out.result;
1096 if (!W_ERROR_IS_OK(status_fn)) {
1097 return LDB_ERR_OPERATIONS_ERROR;
1099 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1102 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
1104 struct ldb_context *ldb = ldb_module_get_ctx(module);
1106 rootdse_mark_noncritical(module, req->controls);
1109 If dn is not "" we should let it pass through
1111 if (!ldb_dn_is_null(req->op.mod.message->dn)) {
1112 return ldb_next_request(module, req);
1116 dn is empty so check for schemaUpdateNow attribute
1117 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
1119 if (ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow")) {
1120 return rootdse_schemaupdatenow(module, req);
1122 if (ldb_msg_find_element(req->op.mod.message, "becomeDomainMaster")) {
1123 return rootdse_become_master(module, req, DREPL_NAMING_MASTER);
1125 if (ldb_msg_find_element(req->op.mod.message, "becomeInfrastructureMaster")) {
1126 return rootdse_become_master(module, req, DREPL_INFRASTRUCTURE_MASTER);
1128 if (ldb_msg_find_element(req->op.mod.message, "becomeRidMaster")) {
1129 return rootdse_become_master(module, req, DREPL_RID_MASTER);
1131 if (ldb_msg_find_element(req->op.mod.message, "becomeSchemaMaster")) {
1132 return rootdse_become_master(module, req, DREPL_SCHEMA_MASTER);
1134 if (ldb_msg_find_element(req->op.mod.message, "becomePdc")) {
1135 return rootdse_become_master(module, req, DREPL_PDC_MASTER);
1137 if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
1138 return rootdse_enableoptionalfeature(module, req);
1141 ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
1142 return LDB_ERR_UNWILLING_TO_PERFORM;
1145 static int rootdse_delete(struct ldb_module *module, struct ldb_request *req)
1147 struct ldb_context *ldb = ldb_module_get_ctx(module);
1149 rootdse_mark_noncritical(module, req->controls);
1152 If dn is not "" we should let it pass through
1154 if (!ldb_dn_is_null(req->op.del.dn)) {
1155 return ldb_next_request(module, req);
1158 ldb_set_errstring(ldb, "rootdse_remove: you cannot delete the rootdse entry!");
1159 return LDB_ERR_NO_SUCH_OBJECT;
1162 _PUBLIC_ const struct ldb_module_ops ldb_rootdse_module_ops = {
1164 .init_context = rootdse_init,
1165 .search = rootdse_search,
1166 .request = rootdse_request,
1168 .modify = rootdse_modify,
1169 .del = rootdse_delete