2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Simo Sorce 2005
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "lib/ldb/include/ldb.h"
26 #include "lib/ldb/include/ldb_errors.h"
27 #include "lib/ldb/include/ldb_private.h"
28 #include "system/time.h"
29 #include "dsdb/samdb/samdb.h"
35 struct ldb_dn **partitions;
39 return 1 if a specific attribute has been requested
41 static int do_attribute(const char * const *attrs, const char *name)
43 return attrs == NULL ||
44 ldb_attr_in_list(attrs, name) ||
45 ldb_attr_in_list(attrs, "*");
48 static int do_attribute_explicit(const char * const *attrs, const char *name)
50 return attrs != NULL && ldb_attr_in_list(attrs, name);
55 add dynamically generated attributes to rootDSE result
57 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg, const char * const *attrs)
59 struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
61 const struct dsdb_schema *schema;
63 schema = dsdb_get_schema(module->ldb);
65 msg->dn = ldb_dn_new(msg, module->ldb, NULL);
67 /* don't return the distinduishedName, cn and name attributes */
68 ldb_msg_remove_attr(msg, "distinguishedName");
69 ldb_msg_remove_attr(msg, "cn");
70 ldb_msg_remove_attr(msg, "name");
72 if (do_attribute(attrs, "currentTime")) {
73 if (ldb_msg_add_steal_string(msg, "currentTime",
74 ldb_timestring(msg, time(NULL))) != 0) {
79 if (do_attribute(attrs, "supportedControl")) {
81 for (i = 0; i < priv->num_controls; i++) {
82 char *control = talloc_strdup(msg, priv->controls[i]);
86 if (ldb_msg_add_steal_string(msg, "supportedControl",
93 if (do_attribute(attrs, "namingContexts")) {
95 for (i = 0; i < priv->num_partitions; i++) {
96 struct ldb_dn *dn = priv->partitions[i];
97 if (ldb_msg_add_steal_string(msg, "namingContexts",
98 ldb_dn_alloc_linearized(msg, dn)) != 0) {
104 server_sasl = talloc_get_type(ldb_get_opaque(module->ldb, "supportedSASLMechanims"),
106 if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
108 for (i = 0; server_sasl && server_sasl[i]; i++) {
109 char *sasl_name = talloc_strdup(msg, server_sasl[i]);
113 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
120 if (do_attribute(attrs, "highestCommittedUSN")) {
122 int ret = ldb_sequence_number(module->ldb, LDB_SEQ_HIGHEST_SEQ, &seq_num);
123 if (ret == LDB_SUCCESS) {
124 if (ldb_msg_add_fmt(msg, "highestCommittedUSN",
125 "%llu", (unsigned long long)seq_num) != 0) {
131 if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
132 struct dsdb_attribute *cur;
135 for (cur = schema->attributes; cur; cur = cur->next) {
139 if (ldb_msg_add_fmt(msg, "dsSchemaAttrCount",
145 if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
146 struct dsdb_class *cur;
149 for (cur = schema->classes; cur; cur = cur->next) {
153 if (ldb_msg_add_fmt(msg, "dsSchemaClassCount",
159 if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
160 if (ldb_msg_add_fmt(msg, "dsSchemaPrefixCount",
161 "%u", schema->num_prefixes) != 0) {
166 if (do_attribute_explicit(attrs, "validFSMOs")) {
167 const struct dsdb_schema_fsmo *schema_fsmo;
168 const struct dsdb_naming_fsmo *naming_fsmo;
169 const struct dsdb_pdc_fsmo *pdc_fsmo;
172 schema_fsmo = talloc_get_type(ldb_get_opaque(module->ldb, "dsdb_schema_fsmo"),
173 struct dsdb_schema_fsmo);
174 if (schema_fsmo && schema_fsmo->we_are_master) {
175 dn_str = ldb_dn_get_linearized(samdb_schema_dn(module->ldb));
176 if (dn_str && dn_str[0]) {
177 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
183 naming_fsmo = talloc_get_type(ldb_get_opaque(module->ldb, "dsdb_naming_fsmo"),
184 struct dsdb_naming_fsmo);
185 if (naming_fsmo && naming_fsmo->we_are_master) {
186 dn_str = ldb_dn_get_linearized(samdb_partitions_dn(module->ldb, msg));
187 if (dn_str && dn_str[0]) {
188 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
194 pdc_fsmo = talloc_get_type(ldb_get_opaque(module->ldb, "dsdb_pdc_fsmo"),
195 struct dsdb_pdc_fsmo);
196 if (pdc_fsmo && pdc_fsmo->we_are_master) {
197 dn_str = ldb_dn_get_linearized(samdb_base_dn(module->ldb));
198 if (dn_str && dn_str[0]) {
199 if (ldb_msg_add_fmt(msg, "validFSMOs", "%s", dn_str) != 0) {
206 /* TODO: lots more dynamic attributes should be added here */
211 return LDB_ERR_OPERATIONS_ERROR;
215 handle search requests
218 struct rootdse_context {
219 struct ldb_module *module;
221 int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
223 const char * const * attrs;
226 static int rootdse_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
228 struct rootdse_context *ac;
230 if (!context || !ares) {
231 ldb_set_errstring(ldb, "NULL Context or Result in callback");
235 ac = talloc_get_type(context, struct rootdse_context);
237 if (ares->type == LDB_REPLY_ENTRY) {
239 * if the client explicit asks for the 'netlogon' attribute
240 * the reply_entry needs to be skipped
242 if (ac->attrs && ldb_attr_in_list(ac->attrs, "netlogon")) {
247 /* for each record returned post-process to add any dynamic
248 attributes that have been asked for */
249 if (rootdse_add_dynamic(ac->module, ares->message, ac->attrs) != LDB_SUCCESS) {
254 return ac->up_callback(ldb, ac->up_context, ares);
258 return LDB_ERR_OPERATIONS_ERROR;
261 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
263 struct rootdse_context *ac;
264 struct ldb_request *down_req;
267 /* see if its for the rootDSE */
268 if (req->op.search.scope != LDB_SCOPE_BASE ||
269 ( ! ldb_dn_is_null(req->op.search.base))) {
270 return ldb_next_request(module, req);
273 ac = talloc(req, struct rootdse_context);
275 return LDB_ERR_OPERATIONS_ERROR;
279 ac->up_context = req->context;
280 ac->up_callback = req->callback;
281 ac->attrs = req->op.search.attrs;
283 down_req = talloc_zero(req, struct ldb_request);
284 if (down_req == NULL) {
285 return LDB_ERR_OPERATIONS_ERROR;
288 down_req->operation = req->operation;
289 /* in our db we store the rootDSE with a DN of cn=rootDSE */
290 down_req->op.search.base = ldb_dn_new(down_req, module->ldb, "cn=rootDSE");
291 down_req->op.search.scope = LDB_SCOPE_BASE;
292 down_req->op.search.tree = ldb_parse_tree(down_req, NULL);
293 if (down_req->op.search.base == NULL || down_req->op.search.tree == NULL) {
294 ldb_oom(module->ldb);
295 talloc_free(down_req);
296 return LDB_ERR_OPERATIONS_ERROR;
298 down_req->op.search.attrs = req->op.search.attrs;
299 down_req->controls = req->controls;
301 down_req->context = ac;
302 down_req->callback = rootdse_callback;
303 ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
305 /* perform the search */
306 ret = ldb_next_request(module, down_req);
308 /* do not free down_req as the call results may be linked to it,
309 * it will be freed when the upper level request get freed */
310 if (ret == LDB_SUCCESS) {
311 req->handle = down_req->handle;
317 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
319 struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
322 list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
324 return LDB_ERR_OPERATIONS_ERROR;
327 list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
328 if (!list[priv->num_controls]) {
329 return LDB_ERR_OPERATIONS_ERROR;
332 priv->num_controls += 1;
333 priv->controls = list;
338 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
340 struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
341 struct ldb_dn **list;
343 list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
345 return LDB_ERR_OPERATIONS_ERROR;
348 list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
349 if (!list[priv->num_partitions]) {
350 return LDB_ERR_OPERATIONS_ERROR;
353 priv->num_partitions += 1;
354 priv->partitions = list;
360 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
362 switch (req->operation) {
364 case LDB_REQ_REGISTER_CONTROL:
365 return rootdse_register_control(module, req);
366 case LDB_REQ_REGISTER_PARTITION:
367 return rootdse_register_partition(module, req);
372 return ldb_next_request(module, req);
375 static int rootdse_init(struct ldb_module *module)
377 struct private_data *data;
379 data = talloc(module, struct private_data);
384 data->num_controls = 0;
385 data->controls = NULL;
386 data->num_partitions = 0;
387 data->partitions = NULL;
388 module->private_data = data;
390 ldb_set_default_dns(module->ldb);
392 return ldb_next_init(module);
395 static const struct ldb_module_ops rootdse_ops = {
397 .init_context = rootdse_init,
398 .search = rootdse_search,
399 .request = rootdse_request
402 int rootdse_module_init(void)
404 return ldb_register_module(&rootdse_ops);