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/>.
25 #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_c.h"
38 unsigned int num_controls;
40 unsigned int num_partitions;
41 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);
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 struct ldb_result *res;
206 const char *dns_attrs[] = { "dNSHostName", NULL };
207 ret = dsdb_module_search_dn(module, msg, &res, samdb_server_dn(ldb, msg),
208 dns_attrs, DSDB_FLAG_NEXT_MODULE, req);
209 if (ret == LDB_SUCCESS) {
210 const char *hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
211 if (hostname != NULL) {
212 if (ldb_msg_add_string(msg, "dNSHostName", hostname)) {
219 if (do_attribute(attrs, "ldapServiceName")) {
220 struct loadparm_context *lp_ctx
221 = talloc_get_type(ldb_get_opaque(ldb, "loadparm"),
222 struct loadparm_context);
223 char *ldap_service_name, *hostname;
225 hostname = talloc_strdup(msg, lpcfg_netbios_name(lp_ctx));
226 if (hostname == NULL) {
229 strlower_m(hostname);
231 ldap_service_name = talloc_asprintf(msg, "%s:%s$@%s",
232 samdb_forest_name(ldb, msg),
233 hostname, lpcfg_realm(lp_ctx));
234 if (ldap_service_name == NULL) {
238 if (ldb_msg_add_string(msg, "ldapServiceName",
239 ldap_service_name) != LDB_SUCCESS) {
244 if (do_attribute(attrs, "currentTime")) {
245 if (ldb_msg_add_steal_string(msg, "currentTime",
246 ldb_timestring(msg, time(NULL))) != LDB_SUCCESS) {
251 if (priv && do_attribute(attrs, "supportedControl")) {
253 for (i = 0; i < priv->num_controls; i++) {
254 char *control = talloc_strdup(msg, priv->controls[i]);
258 if (ldb_msg_add_steal_string(msg, "supportedControl",
259 control) != LDB_SUCCESS) {
265 if (priv && do_attribute(attrs, "namingContexts")) {
267 for (i = 0; i < priv->num_partitions; i++) {
268 struct ldb_dn *dn = priv->partitions[i];
269 if (ldb_msg_add_steal_string(msg, "namingContexts",
270 ldb_dn_alloc_linearized(msg, dn)) != LDB_SUCCESS) {
276 server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
278 if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
280 for (i = 0; server_sasl && server_sasl[i]; i++) {
281 char *sasl_name = talloc_strdup(msg, server_sasl[i]);
285 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
286 sasl_name) != LDB_SUCCESS) {
292 if (do_attribute(attrs, "highestCommittedUSN")) {
294 int ret = ldb_sequence_number(ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
295 if (ret == LDB_SUCCESS) {
296 if (samdb_msg_add_uint64(ldb, msg, msg,
297 "highestCommittedUSN",
298 seq_num) != LDB_SUCCESS) {
304 if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
305 struct dsdb_attribute *cur;
308 for (cur = schema->attributes; cur; cur = cur->next) {
312 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaAttrCount",
318 if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
319 struct dsdb_class *cur;
322 for (cur = schema->classes; cur; cur = cur->next) {
326 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaClassCount",
332 if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
333 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaPrefixCount",
334 schema->prefixmap->length) != LDB_SUCCESS) {
339 if (do_attribute_explicit(attrs, "validFSMOs")) {
340 const struct dsdb_naming_fsmo *naming_fsmo;
341 const struct dsdb_pdc_fsmo *pdc_fsmo;
344 if (schema && schema->fsmo.we_are_master) {
345 dn_str = ldb_dn_get_linearized(ldb_get_schema_basedn(ldb));
346 if (dn_str && dn_str[0]) {
347 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
353 naming_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_naming_fsmo"),
354 struct dsdb_naming_fsmo);
355 if (naming_fsmo && naming_fsmo->we_are_master) {
356 dn_str = ldb_dn_get_linearized(samdb_partitions_dn(ldb, msg));
357 if (dn_str && dn_str[0]) {
358 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
364 pdc_fsmo = talloc_get_type(ldb_get_opaque(ldb, "dsdb_pdc_fsmo"),
365 struct dsdb_pdc_fsmo);
366 if (pdc_fsmo && pdc_fsmo->we_are_master) {
367 dn_str = ldb_dn_get_linearized(ldb_get_default_basedn(ldb));
368 if (dn_str && dn_str[0]) {
369 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != LDB_SUCCESS) {
376 if (do_attribute_explicit(attrs, "vendorVersion")) {
377 if (ldb_msg_add_fmt(msg, "vendorVersion",
378 "%s", SAMBA_VERSION_STRING) != LDB_SUCCESS) {
383 if (do_attribute(attrs, "domainFunctionality")) {
384 if (samdb_msg_add_int(ldb, msg, msg, "domainFunctionality",
385 dsdb_functional_level(ldb)) != LDB_SUCCESS) {
390 if (do_attribute(attrs, "forestFunctionality")) {
391 if (samdb_msg_add_int(ldb, msg, msg, "forestFunctionality",
392 dsdb_forest_functional_level(ldb)) != LDB_SUCCESS) {
397 if (do_attribute(attrs, "domainControllerFunctionality")
398 && (val = talloc_get_type(ldb_get_opaque(ldb, "domainControllerFunctionality"), int))) {
399 if (samdb_msg_add_int(ldb, msg, msg,
400 "domainControllerFunctionality",
401 *val) != LDB_SUCCESS) {
406 if (do_attribute(attrs, "isGlobalCatalogReady")) {
407 /* MS-ADTS 3.1.1.3.2.10
408 Note, we should only return true here is we have
409 completed at least one synchronisation. As both
410 provision and vampire do a full sync, this means we
411 can return true is the gc bit is set in the NTDSDSA
413 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
414 "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != LDB_SUCCESS) {
419 if (do_attribute_explicit(attrs, "tokenGroups")) {
421 /* Obtain the user's session_info */
422 struct auth_session_info *session_info
423 = (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
424 if (session_info && session_info->security_token) {
425 /* The list of groups this user is in */
426 for (i = 0; i < session_info->security_token->num_sids; i++) {
427 if (samdb_msg_add_dom_sid(ldb, msg, msg,
429 &session_info->security_token->sids[i]) != LDB_SUCCESS) {
436 /* TODO: lots more dynamic attributes should be added here */
438 edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
440 /* if the client sent us the EXTENDED_DN control then we need
441 to expand the DNs to have GUID and SID. W2K8 join relies on
446 for (i=0; dn_attrs[i]; i++) {
447 if (!do_attribute(attrs, dn_attrs[i])) continue;
448 ret = expand_dn_in_message(module, msg, dn_attrs[i],
450 if (ret != LDB_SUCCESS) {
451 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
461 return ldb_operr(ldb);
465 handle search requests
468 struct rootdse_context {
469 struct ldb_module *module;
470 struct ldb_request *req;
473 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
474 struct ldb_request *req)
476 struct ldb_context *ldb;
477 struct rootdse_context *ac;
479 ldb = ldb_module_get_ctx(module);
481 ac = talloc_zero(req, struct rootdse_context);
483 ldb_set_errstring(ldb, "Out of Memory");
493 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
495 struct rootdse_context *ac;
498 ac = talloc_get_type(req->context, struct rootdse_context);
501 return ldb_module_done(ac->req, NULL, NULL,
502 LDB_ERR_OPERATIONS_ERROR);
504 if (ares->error != LDB_SUCCESS) {
505 return ldb_module_done(ac->req, ares->controls,
506 ares->response, ares->error);
509 switch (ares->type) {
510 case LDB_REPLY_ENTRY:
512 * if the client explicit asks for the 'netlogon' attribute
513 * the reply_entry needs to be skipped
515 if (ac->req->op.search.attrs &&
516 ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
521 /* for each record returned post-process to add any dynamic
522 attributes that have been asked for */
523 ret = rootdse_add_dynamic(ac->module, ares->message,
524 ac->req->op.search.attrs, ac->req);
525 if (ret != LDB_SUCCESS) {
527 return ldb_module_done(ac->req, NULL, NULL, ret);
530 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
532 case LDB_REPLY_REFERRAL:
533 /* should we allow the backend to return referrals in this case
538 return ldb_module_done(ac->req, ares->controls,
539 ares->response, ares->error);
547 filter from controls from clients in several ways
549 1) mark our registered controls as non-critical in the request
551 This is needed as clients may mark controls as critical even if
552 they are not needed at all in a request. For example, the centrify
553 client sets the SD_FLAGS control as critical on ldap modify
554 requests which are setting the dNSHostName attribute on the
555 machine account. That request doesn't need SD_FLAGS at all, but
556 centrify adds it on all ldap requests.
558 2) if this request is untrusted then remove any non-registered
559 controls that are non-critical
561 This is used on ldap:// connections to prevent remote users from
562 setting an internal control that may be dangerous
564 3) if this request is untrusted then fail any request that includes
565 a critical non-registered control
567 static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request *req)
570 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
573 if (!req->controls) {
577 is_untrusted = ldb_req_is_untrusted(req);
579 for (i=0; req->controls[i]; i++) {
580 bool is_registered = false;
581 bool is_critical = (req->controls[i]->critical != 0);
583 if (req->controls[i]->oid == NULL) {
587 if (is_untrusted || is_critical) {
588 for (j=0; j<priv->num_controls; j++) {
589 if (strcasecmp(priv->controls[j], req->controls[i]->oid) == 0) {
590 is_registered = true;
596 if (is_untrusted && !is_registered) {
598 /* remove it by marking the oid NULL */
599 req->controls[i]->oid = NULL;
600 req->controls[i]->data = NULL;
601 req->controls[i]->critical = 0;
604 /* its a critical unregistered control - give
606 ldb_asprintf_errstring(ldb_module_get_ctx(module),
607 "Attempt to use critical non-registered control '%s'",
608 req->controls[i]->oid);
609 return LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION;
617 req->controls[i]->critical = 0;
624 /* Ensure that anonymous users are not allowed to make anything other than rootDSE search operations */
626 static int rootdse_filter_operations(struct ldb_module *module, struct ldb_request *req)
628 struct auth_session_info *session_info;
629 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
630 bool is_untrusted = ldb_req_is_untrusted(req);
631 bool is_anonymous = true;
632 if (is_untrusted == false) {
636 session_info = (struct auth_session_info *)ldb_get_opaque(ldb_module_get_ctx(module), "sessionInfo");
638 is_anonymous = security_token_is_anonymous(session_info->security_token);
641 if (is_anonymous == false || (priv && priv->block_anonymous == false)) {
645 if (req->operation == LDB_SEARCH) {
646 if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
650 ldb_set_errstring(ldb_module_get_ctx(module), "Operation unavailable without authentication");
651 return LDB_ERR_OPERATIONS_ERROR;
654 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
656 struct ldb_context *ldb;
657 struct rootdse_context *ac;
658 struct ldb_request *down_req;
661 ret = rootdse_filter_operations(module, req);
662 if (ret != LDB_SUCCESS) {
666 ret = rootdse_filter_controls(module, req);
667 if (ret != LDB_SUCCESS) {
671 ldb = ldb_module_get_ctx(module);
673 /* see if its for the rootDSE - only a base search on the "" DN qualifies */
674 if (!(req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base))) {
675 /* Otherwise, pass down to the rest of the stack */
676 return ldb_next_request(module, req);
679 ac = rootdse_init_context(module, req);
681 return ldb_operr(ldb);
684 /* in our db we store the rootDSE with a DN of @ROOTDSE */
685 ret = ldb_build_search_req(&down_req, ldb, ac,
686 ldb_dn_new(ac, ldb, "@ROOTDSE"),
689 req->op.search.attrs,
690 NULL,/* for now skip the controls from the client */
691 ac, rootdse_callback,
693 LDB_REQ_SET_LOCATION(down_req);
694 if (ret != LDB_SUCCESS) {
698 return ldb_next_request(module, down_req);
701 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
703 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
706 list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
708 return ldb_oom(ldb_module_get_ctx(module));
711 list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
712 if (!list[priv->num_controls]) {
713 return ldb_oom(ldb_module_get_ctx(module));
716 priv->num_controls += 1;
717 priv->controls = list;
719 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
722 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
724 struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
725 struct ldb_dn **list;
727 list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
729 return ldb_oom(ldb_module_get_ctx(module));
732 list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
733 if (!list[priv->num_partitions]) {
734 return ldb_operr(ldb_module_get_ctx(module));
737 priv->num_partitions += 1;
738 priv->partitions = list;
740 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
744 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
746 switch (req->operation) {
748 case LDB_REQ_REGISTER_CONTROL:
749 return rootdse_register_control(module, req);
750 case LDB_REQ_REGISTER_PARTITION:
751 return rootdse_register_partition(module, req);
756 return ldb_next_request(module, req);
759 static int rootdse_init(struct ldb_module *module)
762 struct ldb_context *ldb;
763 struct ldb_result *res;
764 struct private_data *data;
765 const char *attrs[] = { "msDS-Behavior-Version", NULL };
766 const char *ds_attrs[] = { "dsServiceName", NULL };
769 ldb = ldb_module_get_ctx(module);
771 data = talloc_zero(module, struct private_data);
776 data->num_controls = 0;
777 data->controls = NULL;
778 data->num_partitions = 0;
779 data->partitions = NULL;
780 data->block_anonymous = true;
782 ldb_module_set_private(module, data);
784 ldb_set_default_dns(ldb);
786 ret = ldb_next_init(module);
788 if (ret != LDB_SUCCESS) {
792 mem_ctx = talloc_new(data);
797 /* Now that the partitions are set up, do a search for:
798 - domainControllerFunctionality
799 - domainFunctionality
800 - forestFunctionality
802 Then stuff these values into an opaque
804 ret = dsdb_module_search(module, mem_ctx, &res,
805 ldb_get_default_basedn(ldb),
806 LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
807 if (ret == LDB_SUCCESS && res->count == 1) {
808 int domain_behaviour_version
809 = ldb_msg_find_attr_as_int(res->msgs[0],
810 "msDS-Behavior-Version", -1);
811 if (domain_behaviour_version != -1) {
812 int *val = talloc(ldb, int);
814 talloc_free(mem_ctx);
817 *val = domain_behaviour_version;
818 ret = ldb_set_opaque(ldb, "domainFunctionality", val);
819 if (ret != LDB_SUCCESS) {
820 talloc_free(mem_ctx);
826 ret = dsdb_module_search(module, mem_ctx, &res,
827 samdb_partitions_dn(ldb, mem_ctx),
828 LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
829 if (ret == LDB_SUCCESS && res->count == 1) {
830 int forest_behaviour_version
831 = ldb_msg_find_attr_as_int(res->msgs[0],
832 "msDS-Behavior-Version", -1);
833 if (forest_behaviour_version != -1) {
834 int *val = talloc(ldb, int);
836 talloc_free(mem_ctx);
839 *val = forest_behaviour_version;
840 ret = ldb_set_opaque(ldb, "forestFunctionality", val);
841 if (ret != LDB_SUCCESS) {
842 talloc_free(mem_ctx);
848 /* For now, our own server's location in the DB is recorded in
849 * the @ROOTDSE record */
850 ret = dsdb_module_search(module, mem_ctx, &res,
851 ldb_dn_new(mem_ctx, ldb, "@ROOTDSE"),
852 LDB_SCOPE_BASE, ds_attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
853 if (ret == LDB_SUCCESS && res->count == 1) {
855 = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
858 ret = dsdb_module_search(module, mem_ctx, &res, ds_dn,
859 LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE, NULL, NULL);
860 if (ret == LDB_SUCCESS && res->count == 1) {
861 int domain_controller_behaviour_version
862 = ldb_msg_find_attr_as_int(res->msgs[0],
863 "msDS-Behavior-Version", -1);
864 if (domain_controller_behaviour_version != -1) {
865 int *val = talloc(ldb, int);
867 talloc_free(mem_ctx);
870 *val = domain_controller_behaviour_version;
871 ret = ldb_set_opaque(ldb,
872 "domainControllerFunctionality", val);
873 if (ret != LDB_SUCCESS) {
874 talloc_free(mem_ctx);
882 data->block_anonymous = dsdb_block_anonymous_ops(module, NULL);
884 talloc_free(mem_ctx);
890 * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
891 * to a DN and a GUID object
893 static int get_optional_feature_dn_guid(struct ldb_request *req, struct ldb_context *ldb,
895 struct ldb_dn **op_feature_scope_dn,
896 struct GUID *op_feature_guid)
898 const struct ldb_message *msg = req->op.mod.message;
899 const char *ldb_val_str;
901 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
904 ldb_val_str = ldb_msg_find_attr_as_string(msg, "enableOptionalFeature", NULL);
906 ldb_set_errstring(ldb,
907 "rootdse: unable to find 'enableOptionalFeature'!");
908 return LDB_ERR_UNWILLING_TO_PERFORM;
911 guid = strchr(ldb_val_str, ':');
913 ldb_set_errstring(ldb,
914 "rootdse: unable to find GUID in 'enableOptionalFeature'!");
915 return LDB_ERR_UNWILLING_TO_PERFORM;
917 status = GUID_from_string(guid+1, op_feature_guid);
918 if (!NT_STATUS_IS_OK(status)) {
919 ldb_set_errstring(ldb,
920 "rootdse: bad GUID in 'enableOptionalFeature'!");
921 return LDB_ERR_UNWILLING_TO_PERFORM;
924 dn = talloc_strndup(tmp_ctx, ldb_val_str, guid-ldb_val_str);
926 ldb_set_errstring(ldb,
927 "rootdse: bad DN in 'enableOptionalFeature'!");
928 return LDB_ERR_UNWILLING_TO_PERFORM;
931 *op_feature_scope_dn = ldb_dn_new(mem_ctx, ldb, dn);
933 talloc_free(tmp_ctx);
938 * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
939 * ldb_message object.
941 static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_context *ldb,
942 TALLOC_CTX *mem_ctx, struct GUID op_feature_guid, struct ldb_message **msg,
943 struct ldb_request *parent)
945 struct ldb_result *res;
946 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
949 ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
951 DSDB_FLAG_NEXT_MODULE |
952 DSDB_SEARCH_SEARCH_ALL_PARTITIONS,
954 "(&(objectClass=msDS-OptionalFeature)"
955 "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx, &op_feature_guid));
957 if (ret != LDB_SUCCESS) {
958 talloc_free(tmp_ctx);
961 if (res->count == 0) {
962 talloc_free(tmp_ctx);
963 return LDB_ERR_NO_SUCH_OBJECT;
965 if (res->count != 1) {
966 ldb_asprintf_errstring(ldb,
967 "More than one object found matching optional feature GUID %s\n",
968 GUID_string(tmp_ctx, &op_feature_guid));
969 talloc_free(tmp_ctx);
970 return LDB_ERR_OPERATIONS_ERROR;
973 *msg = talloc_steal(mem_ctx, res->msgs[0]);
975 talloc_free(tmp_ctx);
979 static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_context *ldb,
980 TALLOC_CTX *mem_ctx, struct ldb_dn *op_feature_scope_dn,
981 struct ldb_message *op_feature_msg, struct ldb_request *parent)
984 const int domain_func_level = dsdb_functional_level(ldb);
985 struct ldb_dn *ntds_settings_dn;
987 unsigned int el_count = 0;
988 struct ldb_message *msg;
990 ret = ldb_msg_find_attr_as_int(op_feature_msg, "msDS-RequiredForestBehaviorVersion", 0);
991 if (domain_func_level < ret){
992 ldb_asprintf_errstring(ldb,
993 "rootdse_enable_recycle_bin: Domain functional level must be at least %d\n",
995 return LDB_ERR_UNWILLING_TO_PERFORM;
998 tmp_ctx = talloc_new(mem_ctx);
999 ntds_settings_dn = samdb_ntds_settings_dn(ldb);
1000 if (!ntds_settings_dn) {
1001 talloc_free(tmp_ctx);
1002 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Failed to find NTDS settings DN");
1005 ntds_settings_dn = ldb_dn_copy(tmp_ctx, ntds_settings_dn);
1006 if (!ntds_settings_dn) {
1007 talloc_free(tmp_ctx);
1008 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, "Failed to copy NTDS settings DN");
1011 msg = ldb_msg_new(tmp_ctx);
1012 msg->dn = ntds_settings_dn;
1014 ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
1015 msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
1017 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1018 if (ret != LDB_SUCCESS) {
1019 ldb_asprintf_errstring(ldb,
1020 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1021 ldb_dn_get_linearized(ntds_settings_dn),
1022 ldb_errstring(ldb));
1023 talloc_free(tmp_ctx);
1027 msg->dn = op_feature_scope_dn;
1028 ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1029 if (ret != LDB_SUCCESS) {
1030 ldb_asprintf_errstring(ldb,
1031 "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1032 ldb_dn_get_linearized(op_feature_scope_dn),
1033 ldb_errstring(ldb));
1034 talloc_free(tmp_ctx);
1041 static int rootdse_enableoptionalfeature(struct ldb_module *module, struct ldb_request *req)
1045 - check for system (only system can enable features)
1046 - extract GUID from the request
1047 - find the feature object
1048 - check functional level, must be at least msDS-RequiredForestBehaviorVersion
1049 - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
1050 - add/modify objects (see ntdsconnection code for an example)
1053 struct ldb_context *ldb = ldb_module_get_ctx(module);
1054 struct GUID op_feature_guid;
1055 struct ldb_dn *op_feature_scope_dn;
1056 struct ldb_message *op_feature_msg;
1057 struct auth_session_info *session_info =
1058 (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1059 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1061 const char *guid_string;
1063 if (security_session_user_level(session_info, NULL) != SECURITY_SYSTEM) {
1064 ldb_set_errstring(ldb, "rootdse: Insufficient rights for enableoptionalfeature");
1065 return LDB_ERR_UNWILLING_TO_PERFORM;
1068 ret = get_optional_feature_dn_guid(req, ldb, tmp_ctx, &op_feature_scope_dn, &op_feature_guid);
1069 if (ret != LDB_SUCCESS) {
1070 talloc_free(tmp_ctx);
1074 guid_string = GUID_string(tmp_ctx, &op_feature_guid);
1076 ldb_set_errstring(ldb, "rootdse: bad optional feature GUID");
1077 return LDB_ERR_UNWILLING_TO_PERFORM;
1080 ret = dsdb_find_optional_feature(module, ldb, tmp_ctx, op_feature_guid, &op_feature_msg, req);
1081 if (ret != LDB_SUCCESS) {
1082 ldb_asprintf_errstring(ldb,
1083 "rootdse: unable to find optional feature for %s - %s",
1084 guid_string, ldb_errstring(ldb));
1085 talloc_free(tmp_ctx);
1089 if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN, guid_string) == 0) {
1090 ret = rootdse_enable_recycle_bin(module, ldb,
1091 tmp_ctx, op_feature_scope_dn,
1092 op_feature_msg, req);
1094 ldb_asprintf_errstring(ldb,
1095 "rootdse: unknown optional feature %s",
1097 talloc_free(tmp_ctx);
1098 return LDB_ERR_UNWILLING_TO_PERFORM;
1100 if (ret != LDB_SUCCESS) {
1101 ldb_asprintf_errstring(ldb,
1102 "rootdse: failed to set optional feature for %s - %s",
1103 guid_string, ldb_errstring(ldb));
1104 talloc_free(tmp_ctx);
1108 talloc_free(tmp_ctx);
1109 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);;
1112 static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request *req)
1114 struct ldb_context *ldb = ldb_module_get_ctx(module);
1115 struct ldb_result *ext_res;
1117 struct ldb_dn *schema_dn;
1119 schema_dn = ldb_get_schema_basedn(ldb);
1121 ldb_reset_err_string(ldb);
1122 ldb_debug(ldb, LDB_DEBUG_WARNING,
1123 "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
1124 return ldb_next_request(module, req);
1127 ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
1128 if (ret != LDB_SUCCESS) {
1129 return ldb_operr(ldb);
1132 talloc_free(ext_res);
1133 return ldb_module_done(req, NULL, NULL, ret);
1136 static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
1138 struct ldb_context *ldb = ldb_module_get_ctx(module);
1141 ret = rootdse_filter_operations(module, req);
1142 if (ret != LDB_SUCCESS) {
1146 ret = rootdse_filter_controls(module, req);
1147 if (ret != LDB_SUCCESS) {
1152 If dn is not "" we should let it pass through
1154 if (!ldb_dn_is_null(req->op.add.message->dn)) {
1155 return ldb_next_request(module, req);
1158 ldb_set_errstring(ldb, "rootdse_add: you cannot add a new rootdse entry!");
1159 return LDB_ERR_NAMING_VIOLATION;
1162 static int rootdse_become_master(struct ldb_module *module,
1163 struct ldb_request *req,
1164 enum drepl_role_master role)
1166 struct drepl_takeFSMORole r;
1167 struct messaging_context *msg;
1168 struct ldb_context *ldb = ldb_module_get_ctx(module);
1169 TALLOC_CTX *tmp_ctx = talloc_new(req);
1170 struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
1171 NTSTATUS status_call;
1174 struct dcerpc_binding_handle *irpc_handle;
1177 ret = samdb_rodc(ldb, &am_rodc);
1178 if (ret != LDB_SUCCESS) {
1179 return ldb_error(ldb, ret, "Could not determine if server is RODC.");
1183 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM,
1184 "RODC cannot become a role master.");
1187 msg = messaging_client_init(tmp_ctx, lpcfg_messaging_path(tmp_ctx, lp_ctx),
1188 ldb_get_event_context(ldb));
1190 ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s", lpcfg_messaging_path(tmp_ctx, lp_ctx));
1191 return LDB_ERR_OPERATIONS_ERROR;
1193 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg,
1196 if (irpc_handle == NULL) {
1197 return ldb_oom(ldb);
1201 status_call = dcerpc_drepl_takeFSMORole_r(irpc_handle, tmp_ctx, &r);
1202 if (!NT_STATUS_IS_OK(status_call)) {
1203 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, nt_errstr(status_call));
1205 status_fn = r.out.result;
1206 if (!W_ERROR_IS_OK(status_fn)) {
1207 return ldb_error(ldb, LDB_ERR_OPERATIONS_ERROR, win_errstr(status_fn));
1209 return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1212 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
1214 struct ldb_context *ldb = ldb_module_get_ctx(module);
1217 ret = rootdse_filter_operations(module, req);
1218 if (ret != LDB_SUCCESS) {
1222 ret = rootdse_filter_controls(module, req);
1223 if (ret != LDB_SUCCESS) {
1228 If dn is not "" we should let it pass through
1230 if (!ldb_dn_is_null(req->op.mod.message->dn)) {
1231 return ldb_next_request(module, req);
1235 dn is empty so check for schemaUpdateNow attribute
1236 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
1238 if (ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow")) {
1239 return rootdse_schemaupdatenow(module, req);
1241 if (ldb_msg_find_element(req->op.mod.message, "becomeDomainMaster")) {
1242 return rootdse_become_master(module, req, DREPL_NAMING_MASTER);
1244 if (ldb_msg_find_element(req->op.mod.message, "becomeInfrastructureMaster")) {
1245 return rootdse_become_master(module, req, DREPL_INFRASTRUCTURE_MASTER);
1247 if (ldb_msg_find_element(req->op.mod.message, "becomeRidMaster")) {
1248 return rootdse_become_master(module, req, DREPL_RID_MASTER);
1250 if (ldb_msg_find_element(req->op.mod.message, "becomeSchemaMaster")) {
1251 return rootdse_become_master(module, req, DREPL_SCHEMA_MASTER);
1253 if (ldb_msg_find_element(req->op.mod.message, "becomePdc")) {
1254 return rootdse_become_master(module, req, DREPL_PDC_MASTER);
1256 if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
1257 return rootdse_enableoptionalfeature(module, req);
1260 ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
1261 return LDB_ERR_UNWILLING_TO_PERFORM;
1264 static int rootdse_rename(struct ldb_module *module, struct ldb_request *req)
1266 struct ldb_context *ldb = ldb_module_get_ctx(module);
1269 ret = rootdse_filter_operations(module, req);
1270 if (ret != LDB_SUCCESS) {
1274 ret = rootdse_filter_controls(module, req);
1275 if (ret != LDB_SUCCESS) {
1280 If dn is not "" we should let it pass through
1282 if (!ldb_dn_is_null(req->op.rename.olddn)) {
1283 return ldb_next_request(module, req);
1286 ldb_set_errstring(ldb, "rootdse_remove: you cannot rename the rootdse entry!");
1287 return LDB_ERR_NO_SUCH_OBJECT;
1290 static int rootdse_delete(struct ldb_module *module, struct ldb_request *req)
1292 struct ldb_context *ldb = ldb_module_get_ctx(module);
1295 ret = rootdse_filter_operations(module, req);
1296 if (ret != LDB_SUCCESS) {
1300 ret = rootdse_filter_controls(module, req);
1301 if (ret != LDB_SUCCESS) {
1306 If dn is not "" we should let it pass through
1308 if (!ldb_dn_is_null(req->op.del.dn)) {
1309 return ldb_next_request(module, req);
1312 ldb_set_errstring(ldb, "rootdse_remove: you cannot delete the rootdse entry!");
1313 return LDB_ERR_NO_SUCH_OBJECT;
1316 static int rootdse_extended(struct ldb_module *module, struct ldb_request *req)
1320 ret = rootdse_filter_operations(module, req);
1321 if (ret != LDB_SUCCESS) {
1325 ret = rootdse_filter_controls(module, req);
1326 if (ret != LDB_SUCCESS) {
1330 return ldb_next_request(module, req);
1333 static const struct ldb_module_ops ldb_rootdse_module_ops = {
1335 .init_context = rootdse_init,
1336 .search = rootdse_search,
1337 .request = rootdse_request,
1339 .modify = rootdse_modify,
1340 .rename = rootdse_rename,
1341 .extended = rootdse_extended,
1342 .del = rootdse_delete
1345 int ldb_rootdse_module_init(const char *version)
1347 LDB_MODULE_CHECK_VERSION(version);
1348 return ldb_register_module(&ldb_rootdse_module_ops);