s4-dsdb: Explicitly mark some internal ldb requests as trusted
[nivanova/samba.git] / source4 / dsdb / samdb / ldb_modules / rootdse.c
1 /*
2    Unix SMB/CIFS implementation.
3    rootDSE ldb module
4
5    Copyright (C) Andrew Tridgell 2005
6    Copyright (C) Simo Sorce 2005-2008
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/ldb/include/ldb.h"
24 #include "lib/ldb/include/ldb_module.h"
25 #include "system/time.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "version.h"
28 #include "dsdb/samdb/ldb_modules/util.h"
29 #include "libcli/security/security.h"
30 #include "libcli/security/session.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"
36
37 struct private_data {
38         unsigned int num_controls;
39         char **controls;
40         unsigned int num_partitions;
41         struct ldb_dn **partitions;
42         bool block_anonymous;
43 };
44
45 /*
46   return 1 if a specific attribute has been requested
47 */
48 static int do_attribute(const char * const *attrs, const char *name)
49 {
50         return attrs == NULL ||
51                 ldb_attr_in_list(attrs, name) ||
52                 ldb_attr_in_list(attrs, "*");
53 }
54
55 static int do_attribute_explicit(const char * const *attrs, const char *name)
56 {
57         return attrs != NULL && ldb_attr_in_list(attrs, name);
58 }
59
60
61 /*
62   expand a DN attribute to include extended DN information if requested
63  */
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)
67 {
68         struct ldb_dn *dn, *dn2;
69         struct ldb_val *v;
70         int ret;
71         struct ldb_request *req2;
72         char *dn_string;
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;
78         int edn_type = 0;
79
80         ldb = ldb_module_get_ctx(module);
81
82         edn = talloc_get_type(edn_control->data, struct ldb_extended_dn_control);
83         if (edn) {
84                 edn_type = edn->type;
85         }
86
87         v = discard_const_p(struct ldb_val, ldb_msg_find_ldb_val(msg, attrname));
88         if (v == NULL) {
89                 talloc_free(tmp_ctx);
90                 return LDB_SUCCESS;
91         }
92
93         dn_string = talloc_strndup(tmp_ctx, (const char *)v->data, v->length);
94         if (dn_string == NULL) {
95                 talloc_free(tmp_ctx);
96                 return ldb_operr(ldb);
97         }
98
99         res = talloc_zero(tmp_ctx, struct ldb_result);
100         if (res == NULL) {
101                 talloc_free(tmp_ctx);
102                 return ldb_operr(ldb);
103         }
104
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);
109         }
110
111         ret = ldb_build_search_req(&req2, ldb, tmp_ctx,
112                                    dn,
113                                    LDB_SCOPE_BASE,
114                                    NULL,
115                                    no_attrs,
116                                    NULL,
117                                    res, ldb_search_default_callback,
118                                    req);
119         LDB_REQ_SET_LOCATION(req2);
120         if (ret != LDB_SUCCESS) {
121                 talloc_free(tmp_ctx);
122                 return ret;
123         }
124
125
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);
131                 return ret;
132         }
133
134         ret = ldb_next_request(module, req2);
135         if (ret == LDB_SUCCESS) {
136                 ret = ldb_wait(req2->handle, LDB_WAIT_ALL);
137         }
138         if (ret != LDB_SUCCESS) {
139                 talloc_free(tmp_ctx);
140                 return ret;
141         }
142
143         if (!res || res->count != 1) {
144                 talloc_free(tmp_ctx);
145                 return ldb_operr(ldb);
146         }
147
148         dn2 = res->msgs[0]->dn;
149
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);
154         }
155         v->length = strlen((char *)v->data);
156
157
158         talloc_free(tmp_ctx);
159
160         return LDB_SUCCESS;
161 }
162
163
164 /*
165   add dynamically generated attributes to rootDSE result
166 */
167 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg,
168                                const char * const *attrs, struct ldb_request *req)
169 {
170         struct ldb_context *ldb;
171         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
172         char **server_sasl;
173         const struct dsdb_schema *schema;
174         int *val;
175         struct ldb_control *edn_control;
176         const char *dn_attrs[] = {
177                 "configurationNamingContext",
178                 "defaultNamingContext",
179                 "dsServiceName",
180                 "rootDomainNamingContext",
181                 "schemaNamingContext",
182                 "serverName",
183                 NULL
184         };
185
186         ldb = ldb_module_get_ctx(module);
187         schema = dsdb_get_schema(ldb, NULL);
188
189         msg->dn = ldb_dn_new(msg, ldb, NULL);
190
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");
195
196         if (do_attribute(attrs, "serverName")) {
197                 if (ldb_msg_add_linearized_dn(msg, "serverName",
198                         samdb_server_dn(ldb, msg)) != LDB_SUCCESS) {
199                         goto failed;
200                 }
201         }
202
203         if (do_attribute(attrs, "dnsHostName")) {
204                 struct ldb_result *res;
205                 int ret;
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|DSDB_FLAG_TRUSTED, 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)) {
213                                         goto failed;
214                                 }
215                         }
216                 }
217         }
218
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;
224
225                 hostname = talloc_strdup(msg, lpcfg_netbios_name(lp_ctx));
226                 if (hostname == NULL) {
227                         goto failed;
228                 }
229                 strlower_m(hostname);
230
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) {
235                         goto failed;
236                 }
237
238                 if (ldb_msg_add_string(msg, "ldapServiceName",
239                                        ldap_service_name) != LDB_SUCCESS) {
240                         goto failed;
241                 }
242         }
243
244         if (do_attribute(attrs, "currentTime")) {
245                 if (ldb_msg_add_steal_string(msg, "currentTime",
246                                              ldb_timestring(msg, time(NULL))) != LDB_SUCCESS) {
247                         goto failed;
248                 }
249         }
250
251         if (priv && do_attribute(attrs, "supportedControl")) {
252                 unsigned int i;
253                 for (i = 0; i < priv->num_controls; i++) {
254                         char *control = talloc_strdup(msg, priv->controls[i]);
255                         if (!control) {
256                                 goto failed;
257                         }
258                         if (ldb_msg_add_steal_string(msg, "supportedControl",
259                                                      control) != LDB_SUCCESS) {
260                                 goto failed;
261                         }
262                 }
263         }
264
265         if (priv && do_attribute(attrs, "namingContexts")) {
266                 unsigned int i;
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) {
271                                 goto failed;
272                         }
273                 }
274         }
275
276         server_sasl = talloc_get_type(ldb_get_opaque(ldb, "supportedSASLMechanisms"),
277                                        char *);
278         if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
279                 unsigned int i;
280                 for (i = 0; server_sasl && server_sasl[i]; i++) {
281                         char *sasl_name = talloc_strdup(msg, server_sasl[i]);
282                         if (!sasl_name) {
283                                 goto failed;
284                         }
285                         if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
286                                                      sasl_name) != LDB_SUCCESS) {
287                                 goto failed;
288                         }
289                 }
290         }
291
292         if (do_attribute(attrs, "highestCommittedUSN")) {
293                 uint64_t seq_num;
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) {
299                                 goto failed;
300                         }
301                 }
302         }
303
304         if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
305                 struct dsdb_attribute *cur;
306                 unsigned int n = 0;
307
308                 for (cur = schema->attributes; cur; cur = cur->next) {
309                         n++;
310                 }
311
312                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaAttrCount",
313                                        n) != LDB_SUCCESS) {
314                         goto failed;
315                 }
316         }
317
318         if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
319                 struct dsdb_class *cur;
320                 unsigned int n = 0;
321
322                 for (cur = schema->classes; cur; cur = cur->next) {
323                         n++;
324                 }
325
326                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaClassCount",
327                                        n) != LDB_SUCCESS) {
328                         goto failed;
329                 }
330         }
331
332         if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
333                 if (samdb_msg_add_uint(ldb, msg, msg, "dsSchemaPrefixCount",
334                                        schema->prefixmap->length) != LDB_SUCCESS) {
335                         goto failed;
336                 }
337         }
338
339         if (do_attribute_explicit(attrs, "validFSMOs")) {
340                 const struct dsdb_naming_fsmo *naming_fsmo;
341                 const struct dsdb_pdc_fsmo *pdc_fsmo;
342                 const char *dn_str;
343
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) {
348                                         goto failed;
349                                 }
350                         }
351                 }
352
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) {
359                                         goto failed;
360                                 }
361                         }
362                 }
363
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) {
370                                         goto failed;
371                                 }
372                         }
373                 }
374         }
375
376         if (do_attribute_explicit(attrs, "vendorVersion")) {
377                 if (ldb_msg_add_fmt(msg, "vendorVersion",
378                                     "%s", SAMBA_VERSION_STRING) != LDB_SUCCESS) {
379                         goto failed;
380                 }
381         }
382
383         if (do_attribute(attrs, "domainFunctionality")) {
384                 if (samdb_msg_add_int(ldb, msg, msg, "domainFunctionality",
385                                       dsdb_functional_level(ldb)) != LDB_SUCCESS) {
386                         goto failed;
387                 }
388         }
389
390         if (do_attribute(attrs, "forestFunctionality")) {
391                 if (samdb_msg_add_int(ldb, msg, msg, "forestFunctionality",
392                                       dsdb_forest_functional_level(ldb)) != LDB_SUCCESS) {
393                         goto failed;
394                 }
395         }
396
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) {
402                         goto failed;
403                 }
404         }
405
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
412                    options */
413                 if (ldb_msg_add_fmt(msg, "isGlobalCatalogReady",
414                                     "%s", samdb_is_gc(ldb)?"TRUE":"FALSE") != LDB_SUCCESS) {
415                         goto failed;
416                 }
417         }
418
419         if (do_attribute_explicit(attrs, "tokenGroups")) {
420                 unsigned int i;
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,
428                                                           "tokenGroups",
429                                                           &session_info->security_token->sids[i]) != LDB_SUCCESS) {
430                                         goto failed;
431                                 }
432                         }
433                 }
434         }
435
436         /* TODO: lots more dynamic attributes should be added here */
437
438         edn_control = ldb_request_get_control(req, LDB_CONTROL_EXTENDED_DN_OID);
439
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
442            this */
443         if (edn_control) {
444                 unsigned int i;
445                 int ret;
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],
449                                                    edn_control, req);
450                         if (ret != LDB_SUCCESS) {
451                                 DEBUG(0,(__location__ ": Failed to expand DN in rootDSE for %s\n",
452                                          dn_attrs[i]));
453                                 goto failed;
454                         }
455                 }
456         }
457
458         return LDB_SUCCESS;
459
460 failed:
461         return ldb_operr(ldb);
462 }
463
464 /*
465   handle search requests
466 */
467
468 struct rootdse_context {
469         struct ldb_module *module;
470         struct ldb_request *req;
471 };
472
473 static struct rootdse_context *rootdse_init_context(struct ldb_module *module,
474                                                     struct ldb_request *req)
475 {
476         struct ldb_context *ldb;
477         struct rootdse_context *ac;
478
479         ldb = ldb_module_get_ctx(module);
480
481         ac = talloc_zero(req, struct rootdse_context);
482         if (ac == NULL) {
483                 ldb_set_errstring(ldb, "Out of Memory");
484                 return NULL;
485         }
486
487         ac->module = module;
488         ac->req = req;
489
490         return ac;
491 }
492
493 static int rootdse_callback(struct ldb_request *req, struct ldb_reply *ares)
494 {
495         struct rootdse_context *ac;
496         int ret;
497
498         ac = talloc_get_type(req->context, struct rootdse_context);
499
500         if (!ares) {
501                 return ldb_module_done(ac->req, NULL, NULL,
502                                         LDB_ERR_OPERATIONS_ERROR);
503         }
504         if (ares->error != LDB_SUCCESS) {
505                 return ldb_module_done(ac->req, ares->controls,
506                                         ares->response, ares->error);
507         }
508
509         switch (ares->type) {
510         case LDB_REPLY_ENTRY:
511                 /*
512                  * if the client explicit asks for the 'netlogon' attribute
513                  * the reply_entry needs to be skipped
514                  */
515                 if (ac->req->op.search.attrs &&
516                     ldb_attr_in_list(ac->req->op.search.attrs, "netlogon")) {
517                         talloc_free(ares);
518                         return LDB_SUCCESS;
519                 }
520
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) {
526                         talloc_free(ares);
527                         return ldb_module_done(ac->req, NULL, NULL, ret);
528                 }
529
530                 return ldb_module_send_entry(ac->req, ares->message, ares->controls);
531
532         case LDB_REPLY_REFERRAL:
533                 /* should we allow the backend to return referrals in this case
534                  * ?? */
535                 break;
536
537         case LDB_REPLY_DONE:
538                 return ldb_module_done(ac->req, ares->controls,
539                                         ares->response, ares->error);
540         }
541
542         talloc_free(ares);
543         return LDB_SUCCESS;
544 }
545
546 /*
547   filter from controls from clients in several ways
548
549   1) mark our registered controls as non-critical in the request
550
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.
557
558   2) if this request is untrusted then remove any non-registered
559      controls that are non-critical
560
561     This is used on ldap:// connections to prevent remote users from
562     setting an internal control that may be dangerous
563
564   3) if this request is untrusted then fail any request that includes
565      a critical non-registered control
566  */
567 static int rootdse_filter_controls(struct ldb_module *module, struct ldb_request *req)
568 {
569         unsigned int i, j;
570         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
571         bool is_untrusted;
572
573         if (!req->controls) {
574                 return LDB_SUCCESS;
575         }
576
577         is_untrusted = ldb_req_is_untrusted(req);
578
579         for (i=0; req->controls[i]; i++) {
580                 bool is_registered = false;
581                 bool is_critical = (req->controls[i]->critical != 0);
582
583                 if (req->controls[i]->oid == NULL) {
584                         continue;
585                 }
586
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;
591                                         break;
592                                 }
593                         }
594                 }
595
596                 if (is_untrusted && !is_registered) {
597                         if (!is_critical) {
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;
602                                 continue;
603                         }
604                         /* its a critical unregistered control - give
605                            an error */
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;
610                 }
611
612                 if (!is_critical) {
613                         continue;
614                 }
615
616                 if (is_registered) {
617                         req->controls[i]->critical = 0;
618                 }
619         }
620
621         return LDB_SUCCESS;
622 }
623
624 /* Ensure that anonymous users are not allowed to make anything other than rootDSE search operations */
625
626 static int rootdse_filter_operations(struct ldb_module *module, struct ldb_request *req)
627 {
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) {
633                 return LDB_SUCCESS;
634         }
635
636         session_info = (struct auth_session_info *)ldb_get_opaque(ldb_module_get_ctx(module), "sessionInfo");
637         if (session_info) {
638                 is_anonymous = security_token_is_anonymous(session_info->security_token);
639         }
640         
641         if (is_anonymous == false || (priv && priv->block_anonymous == false)) {
642                 return LDB_SUCCESS;
643         }
644         
645         if (req->operation == LDB_SEARCH) {
646                 if (req->op.search.scope == LDB_SCOPE_BASE && ldb_dn_is_null(req->op.search.base)) {
647                         return LDB_SUCCESS;
648                 }
649         }
650         ldb_set_errstring(ldb_module_get_ctx(module), "Operation unavailable without authentication");
651         return LDB_ERR_OPERATIONS_ERROR;
652 }
653
654 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
655 {
656         struct ldb_context *ldb;
657         struct rootdse_context *ac;
658         struct ldb_request *down_req;
659         int ret;
660
661         ret = rootdse_filter_operations(module, req);
662         if (ret != LDB_SUCCESS) {
663                 return ret;
664         }
665
666         ret = rootdse_filter_controls(module, req);
667         if (ret != LDB_SUCCESS) {
668                 return ret;
669         }
670
671         ldb = ldb_module_get_ctx(module);
672
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);
677         }
678
679         ac = rootdse_init_context(module, req);
680         if (ac == NULL) {
681                 return ldb_operr(ldb);
682         }
683
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"),
687                                         LDB_SCOPE_BASE,
688                                         NULL,
689                                         req->op.search.attrs,
690                                         NULL,/* for now skip the controls from the client */
691                                         ac, rootdse_callback,
692                                         req);
693         LDB_REQ_SET_LOCATION(down_req);
694         if (ret != LDB_SUCCESS) {
695                 return ret;
696         }
697
698         return ldb_next_request(module, down_req);
699 }
700
701 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
702 {
703         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
704         char **list;
705
706         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
707         if (!list) {
708                 return ldb_oom(ldb_module_get_ctx(module));
709         }
710
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));
714         }
715
716         priv->num_controls += 1;
717         priv->controls = list;
718
719         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
720 }
721
722 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
723 {
724         struct private_data *priv = talloc_get_type(ldb_module_get_private(module), struct private_data);
725         struct ldb_dn **list;
726
727         list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
728         if (!list) {
729                 return ldb_oom(ldb_module_get_ctx(module));
730         }
731
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));
735         }
736
737         priv->num_partitions += 1;
738         priv->partitions = list;
739
740         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
741 }
742
743
744 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
745 {
746         switch (req->operation) {
747
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);
752
753         default:
754                 break;
755         }
756         return ldb_next_request(module, req);
757 }
758
759 static int rootdse_init(struct ldb_module *module)
760 {
761         int ret;
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 };
767         TALLOC_CTX *mem_ctx;
768
769         ldb = ldb_module_get_ctx(module);
770
771         data = talloc_zero(module, struct private_data);
772         if (data == NULL) {
773                 return ldb_oom(ldb);
774         }
775
776         data->num_controls = 0;
777         data->controls = NULL;
778         data->num_partitions = 0;
779         data->partitions = NULL;
780         data->block_anonymous = true;
781
782         ldb_module_set_private(module, data);
783
784         ldb_set_default_dns(ldb);
785
786         ret = ldb_next_init(module);
787
788         if (ret != LDB_SUCCESS) {
789                 return ret;
790         }
791
792         mem_ctx = talloc_new(data);
793         if (!mem_ctx) {
794                 return ldb_oom(ldb);
795         }
796
797         /* Now that the partitions are set up, do a search for:
798            - domainControllerFunctionality
799            - domainFunctionality
800            - forestFunctionality
801
802            Then stuff these values into an opaque
803         */
804         ret = dsdb_module_search(module, mem_ctx, &res,
805                                  ldb_get_default_basedn(ldb),
806                                  LDB_SCOPE_BASE, attrs, DSDB_FLAG_NEXT_MODULE|DSDB_FLAG_TRUSTED, 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);
813                         if (!val) {
814                                 talloc_free(mem_ctx);
815                                 return ldb_oom(ldb);
816                         }
817                         *val = domain_behaviour_version;
818                         ret = ldb_set_opaque(ldb, "domainFunctionality", val);
819                         if (ret != LDB_SUCCESS) {
820                                 talloc_free(mem_ctx);
821                                 return ret;
822                         }
823                 }
824         }
825
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|DSDB_FLAG_TRUSTED, 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);
835                         if (!val) {
836                                 talloc_free(mem_ctx);
837                                 return ldb_oom(ldb);
838                         }
839                         *val = forest_behaviour_version;
840                         ret = ldb_set_opaque(ldb, "forestFunctionality", val);
841                         if (ret != LDB_SUCCESS) {
842                                 talloc_free(mem_ctx);
843                                 return ret;
844                         }
845                 }
846         }
847
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|DSDB_FLAG_TRUSTED, NULL, NULL);
853         if (ret == LDB_SUCCESS && res->count == 1) {
854                 struct ldb_dn *ds_dn
855                         = ldb_msg_find_attr_as_dn(ldb, mem_ctx, res->msgs[0],
856                                                   "dsServiceName");
857                 if (ds_dn) {
858                         ret = dsdb_module_search(module, mem_ctx, &res, ds_dn,
859                                                  LDB_SCOPE_BASE, attrs,
860                                                  DSDB_FLAG_NEXT_MODULE|DSDB_FLAG_TRUSTED, NULL, NULL);
861                         if (ret == LDB_SUCCESS && res->count == 1) {
862                                 int domain_controller_behaviour_version
863                                         = ldb_msg_find_attr_as_int(res->msgs[0],
864                                                                    "msDS-Behavior-Version", -1);
865                                 if (domain_controller_behaviour_version != -1) {
866                                         int *val = talloc(ldb, int);
867                                         if (!val) {
868                                                 talloc_free(mem_ctx);
869                                                 return ldb_oom(ldb);
870                                         }
871                                         *val = domain_controller_behaviour_version;
872                                         ret = ldb_set_opaque(ldb,
873                                                              "domainControllerFunctionality", val);
874                                         if (ret != LDB_SUCCESS) {
875                                                 talloc_free(mem_ctx);
876                                                 return ret;
877                                         }
878                                 }
879                         }
880                 }
881         }
882
883         data->block_anonymous = dsdb_block_anonymous_ops(module, NULL);
884
885         talloc_free(mem_ctx);
886
887         return LDB_SUCCESS;
888 }
889
890 /*
891  * This function gets the string SCOPE_DN:OPTIONAL_FEATURE_GUID and parse it
892  * to a DN and a GUID object
893  */
894 static int get_optional_feature_dn_guid(struct ldb_request *req, struct ldb_context *ldb,
895                                                 TALLOC_CTX *mem_ctx,
896                                                 struct ldb_dn **op_feature_scope_dn,
897                                                 struct GUID *op_feature_guid)
898 {
899         const struct ldb_message *msg = req->op.mod.message;
900         const char *ldb_val_str;
901         char *dn, *guid;
902         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
903         NTSTATUS status;
904
905         ldb_val_str = ldb_msg_find_attr_as_string(msg, "enableOptionalFeature", NULL);
906         if (!ldb_val_str) {
907                 ldb_set_errstring(ldb,
908                                   "rootdse: unable to find 'enableOptionalFeature'!");
909                 return LDB_ERR_UNWILLING_TO_PERFORM;
910         }
911
912         guid = strchr(ldb_val_str, ':');
913         if (!guid) {
914                 ldb_set_errstring(ldb,
915                                   "rootdse: unable to find GUID in 'enableOptionalFeature'!");
916                 return LDB_ERR_UNWILLING_TO_PERFORM;
917         }
918         status = GUID_from_string(guid+1, op_feature_guid);
919         if (!NT_STATUS_IS_OK(status)) {
920                 ldb_set_errstring(ldb,
921                                   "rootdse: bad GUID in 'enableOptionalFeature'!");
922                 return LDB_ERR_UNWILLING_TO_PERFORM;
923         }
924
925         dn = talloc_strndup(tmp_ctx, ldb_val_str, guid-ldb_val_str);
926         if (!dn) {
927                 ldb_set_errstring(ldb,
928                                   "rootdse: bad DN in 'enableOptionalFeature'!");
929                 return LDB_ERR_UNWILLING_TO_PERFORM;
930         }
931
932         *op_feature_scope_dn = ldb_dn_new(mem_ctx, ldb, dn);
933
934         talloc_free(tmp_ctx);
935         return LDB_SUCCESS;
936 }
937
938 /*
939  * This function gets the OPTIONAL_FEATURE_GUID and looks for the optional feature
940  * ldb_message object.
941  */
942 static int dsdb_find_optional_feature(struct ldb_module *module, struct ldb_context *ldb,
943                                       TALLOC_CTX *mem_ctx, struct GUID op_feature_guid, struct ldb_message **msg,
944                                       struct ldb_request *parent)
945 {
946         struct ldb_result *res;
947         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
948         int ret;
949
950         ret = dsdb_module_search(module, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
951                                  NULL,
952                                  DSDB_FLAG_NEXT_MODULE |
953                                  DSDB_SEARCH_SEARCH_ALL_PARTITIONS |
954                                  DSDB_FLAG_TRUSTED,
955                                  parent,
956                                  "(&(objectClass=msDS-OptionalFeature)"
957                                  "(msDS-OptionalFeatureGUID=%s))",GUID_string(tmp_ctx, &op_feature_guid));
958
959         if (ret != LDB_SUCCESS) {
960                 talloc_free(tmp_ctx);
961                 return ret;
962         }
963         if (res->count == 0) {
964                 talloc_free(tmp_ctx);
965                 return LDB_ERR_NO_SUCH_OBJECT;
966         }
967         if (res->count != 1) {
968                 ldb_asprintf_errstring(ldb,
969                                        "More than one object found matching optional feature GUID %s\n",
970                                        GUID_string(tmp_ctx, &op_feature_guid));
971                 talloc_free(tmp_ctx);
972                 return LDB_ERR_OPERATIONS_ERROR;
973         }
974
975         *msg = talloc_steal(mem_ctx, res->msgs[0]);
976
977         talloc_free(tmp_ctx);
978         return LDB_SUCCESS;
979 }
980
981 static int rootdse_enable_recycle_bin(struct ldb_module *module,struct ldb_context *ldb,
982                                       TALLOC_CTX *mem_ctx, struct ldb_dn *op_feature_scope_dn,
983                                       struct ldb_message *op_feature_msg, struct ldb_request *parent)
984 {
985         int ret;
986         const int domain_func_level = dsdb_functional_level(ldb);
987         struct ldb_dn *ntds_settings_dn;
988         TALLOC_CTX *tmp_ctx;
989         unsigned int el_count = 0;
990         struct ldb_message *msg;
991
992         ret = ldb_msg_find_attr_as_int(op_feature_msg, "msDS-RequiredForestBehaviorVersion", 0);
993         if (domain_func_level < ret){
994                 ldb_asprintf_errstring(ldb,
995                                        "rootdse_enable_recycle_bin: Domain functional level must be at least %d\n",
996                                        ret);
997                 return LDB_ERR_UNWILLING_TO_PERFORM;
998         }
999
1000         tmp_ctx = talloc_new(mem_ctx);
1001         ntds_settings_dn = samdb_ntds_settings_dn(ldb);
1002         if (!ntds_settings_dn) {
1003                 DEBUG(0, (__location__ ": Failed to find NTDS settings DN\n"));
1004                 ret = LDB_ERR_OPERATIONS_ERROR;
1005                 talloc_free(tmp_ctx);
1006                 return ret;
1007         }
1008
1009         ntds_settings_dn = ldb_dn_copy(tmp_ctx, ntds_settings_dn);
1010         if (!ntds_settings_dn) {
1011                 DEBUG(0, (__location__ ": Failed to copy NTDS settings DN\n"));
1012                 ret = LDB_ERR_OPERATIONS_ERROR;
1013                 talloc_free(tmp_ctx);
1014                 return ret;
1015         }
1016
1017         msg = ldb_msg_new(tmp_ctx);
1018         msg->dn = ntds_settings_dn;
1019
1020         ldb_msg_add_linearized_dn(msg, "msDS-EnabledFeature", op_feature_msg->dn);
1021         msg->elements[el_count++].flags = LDB_FLAG_MOD_ADD;
1022
1023         ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1024         if (ret != LDB_SUCCESS) {
1025                 ldb_asprintf_errstring(ldb,
1026                                        "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1027                                        ldb_dn_get_linearized(ntds_settings_dn),
1028                                        ldb_errstring(ldb));
1029                 talloc_free(tmp_ctx);
1030                 return ret;
1031         }
1032
1033         msg->dn = op_feature_scope_dn;
1034         ret = dsdb_module_modify(module, msg, DSDB_FLAG_NEXT_MODULE, parent);
1035         if (ret != LDB_SUCCESS) {
1036                 ldb_asprintf_errstring(ldb,
1037                                        "rootdse_enable_recycle_bin: Failed to modify object %s - %s",
1038                                        ldb_dn_get_linearized(op_feature_scope_dn),
1039                                        ldb_errstring(ldb));
1040                 talloc_free(tmp_ctx);
1041                 return ret;
1042         }
1043
1044         return LDB_SUCCESS;
1045 }
1046
1047 static int rootdse_enableoptionalfeature(struct ldb_module *module, struct ldb_request *req)
1048 {
1049         /*
1050           steps:
1051                - check for system (only system can enable features)
1052                - extract GUID from the request
1053                - find the feature object
1054                - check functional level, must be at least msDS-RequiredForestBehaviorVersion
1055                - check if it is already enabled (if enabled return LDAP_ATTRIBUTE_OR_VALUE_EXISTS) - probably not needed, just return error from the add/modify
1056                - add/modify objects (see ntdsconnection code for an example)
1057          */
1058
1059         struct ldb_context *ldb = ldb_module_get_ctx(module);
1060         struct GUID op_feature_guid;
1061         struct ldb_dn *op_feature_scope_dn;
1062         struct ldb_message *op_feature_msg;
1063         struct auth_session_info *session_info =
1064                                 (struct auth_session_info *)ldb_get_opaque(ldb, "sessionInfo");
1065         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
1066         int ret;
1067         const char *guid_string;
1068
1069         if (security_session_user_level(session_info, NULL) != SECURITY_SYSTEM) {
1070                 ldb_set_errstring(ldb, "rootdse: Insufficient rights for enableoptionalfeature");
1071                 return LDB_ERR_UNWILLING_TO_PERFORM;
1072         }
1073
1074         ret = get_optional_feature_dn_guid(req, ldb, tmp_ctx, &op_feature_scope_dn, &op_feature_guid);
1075         if (ret != LDB_SUCCESS) {
1076                 talloc_free(tmp_ctx);
1077                 return ret;
1078         }
1079
1080         guid_string = GUID_string(tmp_ctx, &op_feature_guid);
1081         if (!guid_string) {
1082                 ldb_set_errstring(ldb, "rootdse: bad optional feature GUID");
1083                 return LDB_ERR_UNWILLING_TO_PERFORM;
1084         }
1085
1086         ret = dsdb_find_optional_feature(module, ldb, tmp_ctx, op_feature_guid, &op_feature_msg, req);
1087         if (ret != LDB_SUCCESS) {
1088                 ldb_asprintf_errstring(ldb,
1089                                        "rootdse: unable to find optional feature for %s - %s",
1090                                        guid_string, ldb_errstring(ldb));
1091                 talloc_free(tmp_ctx);
1092                 return ret;
1093         }
1094
1095         if (strcasecmp(DS_GUID_FEATURE_RECYCLE_BIN, guid_string) == 0) {
1096                         ret = rootdse_enable_recycle_bin(module, ldb,
1097                                                          tmp_ctx, op_feature_scope_dn,
1098                                                          op_feature_msg, req);
1099         } else {
1100                 ldb_asprintf_errstring(ldb,
1101                                        "rootdse: unknown optional feature %s",
1102                                        guid_string);
1103                 talloc_free(tmp_ctx);
1104                 return LDB_ERR_UNWILLING_TO_PERFORM;
1105         }
1106         if (ret != LDB_SUCCESS) {
1107                 ldb_asprintf_errstring(ldb,
1108                                        "rootdse: failed to set optional feature for %s - %s",
1109                                        guid_string, ldb_errstring(ldb));
1110                 talloc_free(tmp_ctx);
1111                 return ret;
1112         }
1113
1114         talloc_free(tmp_ctx);
1115         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);;
1116 }
1117
1118 static int rootdse_schemaupdatenow(struct ldb_module *module, struct ldb_request *req)
1119 {
1120         struct ldb_context *ldb = ldb_module_get_ctx(module);
1121         struct ldb_result *ext_res;
1122         int ret;
1123         struct ldb_dn *schema_dn;
1124
1125         schema_dn = ldb_get_schema_basedn(ldb);
1126         if (!schema_dn) {
1127                 ldb_reset_err_string(ldb);
1128                 ldb_debug(ldb, LDB_DEBUG_WARNING,
1129                           "rootdse_modify: no schema dn present: (skip ldb_extended call)\n");
1130                 return ldb_next_request(module, req);
1131         }
1132
1133         ret = ldb_extended(ldb, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID, schema_dn, &ext_res);
1134         if (ret != LDB_SUCCESS) {
1135                 return ldb_operr(ldb);
1136         }
1137
1138         talloc_free(ext_res);
1139         return ldb_module_done(req, NULL, NULL, ret);
1140 }
1141
1142 static int rootdse_add(struct ldb_module *module, struct ldb_request *req)
1143 {
1144         struct ldb_context *ldb = ldb_module_get_ctx(module);
1145         int ret;
1146
1147         ret = rootdse_filter_operations(module, req);
1148         if (ret != LDB_SUCCESS) {
1149                 return ret;
1150         }
1151
1152         ret = rootdse_filter_controls(module, req);
1153         if (ret != LDB_SUCCESS) {
1154                 return ret;
1155         }
1156
1157         /*
1158                 If dn is not "" we should let it pass through
1159         */
1160         if (!ldb_dn_is_null(req->op.add.message->dn)) {
1161                 return ldb_next_request(module, req);
1162         }
1163
1164         ldb_set_errstring(ldb, "rootdse_add: you cannot add a new rootdse entry!");
1165         return LDB_ERR_NAMING_VIOLATION;
1166 }
1167
1168 static int rootdse_become_master(struct ldb_module *module,
1169                                  struct ldb_request *req,
1170                                  enum drepl_role_master role)
1171 {
1172         struct drepl_takeFSMORole r;
1173         struct messaging_context *msg;
1174         struct ldb_context *ldb = ldb_module_get_ctx(module);
1175         TALLOC_CTX *tmp_ctx = talloc_new(req);
1176         struct loadparm_context *lp_ctx = ldb_get_opaque(ldb, "loadparm");
1177         NTSTATUS status_call;
1178         WERROR status_fn;
1179         bool am_rodc;
1180         struct dcerpc_binding_handle *irpc_handle;
1181         int ret;
1182
1183         ret = samdb_rodc(ldb, &am_rodc);
1184         if (ret != LDB_SUCCESS) {
1185                 return ldb_error(ldb, ret, "Could not determine if server is RODC.");
1186         }
1187
1188         if (am_rodc) {
1189                 return ldb_error(ldb, LDB_ERR_UNWILLING_TO_PERFORM,
1190                                  "RODC cannot become a role master.");
1191         }
1192
1193         msg = messaging_client_init(tmp_ctx, lpcfg_messaging_path(tmp_ctx, lp_ctx),
1194                                     ldb_get_event_context(ldb));
1195         if (!msg) {
1196                 ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s", lpcfg_messaging_path(tmp_ctx, lp_ctx));
1197                 return LDB_ERR_OPERATIONS_ERROR;
1198         }
1199         irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg,
1200                                                   "dreplsrv",
1201                                                   &ndr_table_irpc);
1202         if (irpc_handle == NULL) {
1203                 return ldb_oom(ldb);
1204         }
1205         r.in.role = role;
1206
1207         status_call = dcerpc_drepl_takeFSMORole_r(irpc_handle, tmp_ctx, &r);
1208         if (!NT_STATUS_IS_OK(status_call)) {
1209                 return LDB_ERR_OPERATIONS_ERROR;
1210         }
1211         status_fn = r.out.result;
1212         if (!W_ERROR_IS_OK(status_fn)) {
1213                 return LDB_ERR_OPERATIONS_ERROR;
1214         }
1215         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
1216 }
1217
1218 static int rootdse_modify(struct ldb_module *module, struct ldb_request *req)
1219 {
1220         struct ldb_context *ldb = ldb_module_get_ctx(module);
1221         int ret;
1222
1223         ret = rootdse_filter_operations(module, req);
1224         if (ret != LDB_SUCCESS) {
1225                 return ret;
1226         }
1227
1228         ret = rootdse_filter_controls(module, req);
1229         if (ret != LDB_SUCCESS) {
1230                 return ret;
1231         }
1232
1233         /*
1234                 If dn is not "" we should let it pass through
1235         */
1236         if (!ldb_dn_is_null(req->op.mod.message->dn)) {
1237                 return ldb_next_request(module, req);
1238         }
1239
1240         /*
1241                 dn is empty so check for schemaUpdateNow attribute
1242                 "The type of modification and values specified in the LDAP modify operation do not matter." MSDN
1243         */
1244         if (ldb_msg_find_element(req->op.mod.message, "schemaUpdateNow")) {
1245                 return rootdse_schemaupdatenow(module, req);
1246         }
1247         if (ldb_msg_find_element(req->op.mod.message, "becomeDomainMaster")) {
1248                 return rootdse_become_master(module, req, DREPL_NAMING_MASTER);
1249         }
1250         if (ldb_msg_find_element(req->op.mod.message, "becomeInfrastructureMaster")) {
1251                 return rootdse_become_master(module, req, DREPL_INFRASTRUCTURE_MASTER);
1252         }
1253         if (ldb_msg_find_element(req->op.mod.message, "becomeRidMaster")) {
1254                 return rootdse_become_master(module, req, DREPL_RID_MASTER);
1255         }
1256         if (ldb_msg_find_element(req->op.mod.message, "becomeSchemaMaster")) {
1257                 return rootdse_become_master(module, req, DREPL_SCHEMA_MASTER);
1258         }
1259         if (ldb_msg_find_element(req->op.mod.message, "becomePdc")) {
1260                 return rootdse_become_master(module, req, DREPL_PDC_MASTER);
1261         }
1262         if (ldb_msg_find_element(req->op.mod.message, "enableOptionalFeature")) {
1263                 return rootdse_enableoptionalfeature(module, req);
1264         }
1265
1266         ldb_set_errstring(ldb, "rootdse_modify: unknown attribute to change!");
1267         return LDB_ERR_UNWILLING_TO_PERFORM;
1268 }
1269
1270 static int rootdse_rename(struct ldb_module *module, struct ldb_request *req)
1271 {
1272         struct ldb_context *ldb = ldb_module_get_ctx(module);
1273         int ret;
1274
1275         ret = rootdse_filter_operations(module, req);
1276         if (ret != LDB_SUCCESS) {
1277                 return ret;
1278         }
1279
1280         ret = rootdse_filter_controls(module, req);
1281         if (ret != LDB_SUCCESS) {
1282                 return ret;
1283         }
1284
1285         /*
1286                 If dn is not "" we should let it pass through
1287         */
1288         if (!ldb_dn_is_null(req->op.rename.olddn)) {
1289                 return ldb_next_request(module, req);
1290         }
1291
1292         ldb_set_errstring(ldb, "rootdse_remove: you cannot rename the rootdse entry!");
1293         return LDB_ERR_NO_SUCH_OBJECT;
1294 }
1295
1296 static int rootdse_delete(struct ldb_module *module, struct ldb_request *req)
1297 {
1298         struct ldb_context *ldb = ldb_module_get_ctx(module);
1299         int ret;
1300
1301         ret = rootdse_filter_operations(module, req);
1302         if (ret != LDB_SUCCESS) {
1303                 return ret;
1304         }
1305
1306         ret = rootdse_filter_controls(module, req);
1307         if (ret != LDB_SUCCESS) {
1308                 return ret;
1309         }
1310
1311         /*
1312                 If dn is not "" we should let it pass through
1313         */
1314         if (!ldb_dn_is_null(req->op.del.dn)) {
1315                 return ldb_next_request(module, req);
1316         }
1317
1318         ldb_set_errstring(ldb, "rootdse_remove: you cannot delete the rootdse entry!");
1319         return LDB_ERR_NO_SUCH_OBJECT;
1320 }
1321
1322 static int rootdse_extended(struct ldb_module *module, struct ldb_request *req)
1323 {
1324         int ret;
1325
1326         ret = rootdse_filter_operations(module, req);
1327         if (ret != LDB_SUCCESS) {
1328                 return ret;
1329         }
1330
1331         ret = rootdse_filter_controls(module, req);
1332         if (ret != LDB_SUCCESS) {
1333                 return ret;
1334         }
1335
1336         return ldb_next_request(module, req);
1337 }
1338
1339 static const struct ldb_module_ops ldb_rootdse_module_ops = {
1340         .name           = "rootdse",
1341         .init_context   = rootdse_init,
1342         .search         = rootdse_search,
1343         .request        = rootdse_request,
1344         .add            = rootdse_add,
1345         .modify         = rootdse_modify,
1346         .rename         = rootdse_rename,
1347         .extended       = rootdse_extended,
1348         .del            = rootdse_delete
1349 };
1350
1351 int ldb_rootdse_module_init(const char *version)
1352 {
1353         LDB_MODULE_CHECK_VERSION(version);
1354         return ldb_register_module(&ldb_rootdse_module_ops);
1355 }