Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into 4-0-abartlet
[ira/wip.git] / source4 / dsdb / samdb / ldb_modules / rootdse.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    rootDSE ldb module
5
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Simo Sorce 2005
8    
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.
13    
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.
18    
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/>.
21 */
22
23 #include "includes.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/ldb/include/ldb_private.h"
27 #include "system/time.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "version.h"
30
31 struct private_data {
32         int num_controls;
33         char **controls;
34         int num_partitions;
35         struct ldb_dn **partitions;
36 };
37
38 /*
39   return 1 if a specific attribute has been requested
40 */
41 static int do_attribute(const char * const *attrs, const char *name)
42 {
43         return attrs == NULL ||
44                 ldb_attr_in_list(attrs, name) ||
45                 ldb_attr_in_list(attrs, "*");
46 }
47
48 static int do_attribute_explicit(const char * const *attrs, const char *name)
49 {
50         return attrs != NULL && ldb_attr_in_list(attrs, name);
51 }
52
53
54 /*
55   add dynamically generated attributes to rootDSE result
56 */
57 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg, const char * const *attrs)
58 {
59         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
60         char **server_sasl;
61         const struct dsdb_schema *schema;
62
63         schema = dsdb_get_schema(module->ldb);
64
65         msg->dn = ldb_dn_new(msg, module->ldb, NULL);
66
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");
71
72         if (do_attribute(attrs, "currentTime")) {
73                 if (ldb_msg_add_steal_string(msg, "currentTime", 
74                                              ldb_timestring(msg, time(NULL))) != 0) {
75                         goto failed;
76                 }
77         }
78
79         if (do_attribute(attrs, "supportedControl")) {
80                 int i;
81                 for (i = 0; i < priv->num_controls; i++) {
82                         char *control = talloc_strdup(msg, priv->controls[i]);
83                         if (!control) {
84                                 goto failed;
85                         }
86                         if (ldb_msg_add_steal_string(msg, "supportedControl",
87                                                      control) != 0) {
88                                 goto failed;
89                         }
90                 }
91         }
92
93         if (do_attribute(attrs, "namingContexts")) {
94                 int i;
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) {
99                                 goto failed;
100                         }
101                 }
102         }
103
104         server_sasl = talloc_get_type(ldb_get_opaque(module->ldb, "supportedSASLMechanims"), 
105                                        char *);
106         if (server_sasl && do_attribute(attrs, "supportedSASLMechanisms")) {
107                 int i;
108                 for (i = 0; server_sasl && server_sasl[i]; i++) {
109                         char *sasl_name = talloc_strdup(msg, server_sasl[i]);
110                         if (!sasl_name) {
111                                 goto failed;
112                         }
113                         if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
114                                                      sasl_name) != 0) {
115                                 goto failed;
116                         }
117                 }
118         }
119
120         if (do_attribute(attrs, "highestCommittedUSN")) {
121                 uint64_t seq_num;
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) {
126                                 goto failed;
127                         }
128                 }
129         }
130
131         if (schema && do_attribute_explicit(attrs, "dsSchemaAttrCount")) {
132                 struct dsdb_attribute *cur;
133                 uint32_t n = 0;
134
135                 for (cur = schema->attributes; cur; cur = cur->next) {
136                         n++;
137                 }
138
139                 if (ldb_msg_add_fmt(msg, "dsSchemaAttrCount", 
140                                     "%u", n) != 0) {
141                         goto failed;
142                 }
143         }
144
145         if (schema && do_attribute_explicit(attrs, "dsSchemaClassCount")) {
146                 struct dsdb_class *cur;
147                 uint32_t n = 0;
148
149                 for (cur = schema->classes; cur; cur = cur->next) {
150                         n++;
151                 }
152
153                 if (ldb_msg_add_fmt(msg, "dsSchemaClassCount", 
154                                     "%u", n) != 0) {
155                         goto failed;
156                 }
157         }
158
159         if (schema && do_attribute_explicit(attrs, "dsSchemaPrefixCount")) {
160                 if (ldb_msg_add_fmt(msg, "dsSchemaPrefixCount", 
161                                     "%u", schema->num_prefixes) != 0) {
162                         goto failed;
163                 }
164         }
165
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;
170                 const char *dn_str;
171
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) {
178                                         goto failed;
179                                 }
180                         }
181                 }
182
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) {
189                                         goto failed;
190                                 }
191                         }
192                 }
193
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) {
200                                         goto failed;
201                                 }
202                         }
203                 }
204         }
205
206         if (schema && do_attribute_explicit(attrs, "vendorVersion")) {
207                 if (ldb_msg_add_fmt(msg, "vendorVersion", 
208                                     "%s", SAMBA_VERSION_STRING) != 0) {
209                         goto failed;
210                 }
211         }
212
213         /* TODO: lots more dynamic attributes should be added here */
214
215         return LDB_SUCCESS;
216
217 failed:
218         return LDB_ERR_OPERATIONS_ERROR;
219 }
220
221 /*
222   handle search requests
223 */
224
225 struct rootdse_context {
226         struct ldb_module *module;
227         void *up_context;
228         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
229
230         const char * const * attrs;
231 };
232
233 static int rootdse_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
234 {
235         struct rootdse_context *ac;
236
237         ac = talloc_get_type(context, struct rootdse_context);
238
239         if (ares->type == LDB_REPLY_ENTRY) {
240                 /*
241                  * if the client explicit asks for the 'netlogon' attribute
242                  * the reply_entry needs to be skipped
243                  */
244                 if (ac->attrs && ldb_attr_in_list(ac->attrs, "netlogon")) {
245                         talloc_free(ares);
246                         return LDB_SUCCESS;
247                 }
248
249                 /* for each record returned post-process to add any dynamic
250                    attributes that have been asked for */
251                 if (rootdse_add_dynamic(ac->module, ares->message, ac->attrs) != LDB_SUCCESS) {
252                         goto error;
253                 }
254         }
255
256         return ac->up_callback(ldb, ac->up_context, ares);
257
258 error:
259         talloc_free(ares);
260         return LDB_ERR_OPERATIONS_ERROR;
261 }
262
263 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
264 {
265         struct rootdse_context *ac;
266         struct ldb_request *down_req;
267         int ret;
268
269         /* see if its for the rootDSE */
270         if (req->op.search.scope != LDB_SCOPE_BASE ||
271             ( ! ldb_dn_is_null(req->op.search.base))) {
272                 return ldb_next_request(module, req);
273         }
274
275         ac = talloc(req, struct rootdse_context);
276         if (ac == NULL) {
277                 return LDB_ERR_OPERATIONS_ERROR;
278         }
279
280         ac->module = module;
281         ac->up_context = req->context;
282         ac->up_callback = req->callback;
283         ac->attrs = req->op.search.attrs;
284
285         down_req = talloc_zero(req, struct ldb_request);
286         if (down_req == NULL) {
287                 return LDB_ERR_OPERATIONS_ERROR;
288         }
289
290         down_req->operation = req->operation;
291         /* in our db we store the rootDSE with a DN of @ROOTDSE */
292         down_req->op.search.base = ldb_dn_new(down_req, module->ldb, "@ROOTDSE");
293         down_req->op.search.scope = LDB_SCOPE_BASE;
294         down_req->op.search.tree = ldb_parse_tree(down_req, NULL);
295         if (down_req->op.search.base == NULL || down_req->op.search.tree == NULL) {
296                 ldb_oom(module->ldb);
297                 talloc_free(down_req);
298                 return LDB_ERR_OPERATIONS_ERROR;
299         }
300         down_req->op.search.attrs = req->op.search.attrs;
301         down_req->controls = req->controls;
302
303         down_req->context = ac;
304         down_req->callback = rootdse_callback;
305         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
306
307         /* perform the search */
308         ret = ldb_next_request(module, down_req);
309
310         /* do not free down_req as the call results may be linked to it,
311          * it will be freed when the upper level request get freed */
312         if (ret == LDB_SUCCESS) {
313                 req->handle = down_req->handle;
314         }
315
316         return ret;
317 }
318
319 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
320 {
321         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
322         char **list;
323
324         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
325         if (!list) {
326                 return LDB_ERR_OPERATIONS_ERROR;
327         }
328
329         list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
330         if (!list[priv->num_controls]) {
331                 return LDB_ERR_OPERATIONS_ERROR;
332         }
333
334         priv->num_controls += 1;
335         priv->controls = list;
336
337         return LDB_SUCCESS;
338 }
339  
340 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
341 {
342         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
343         struct ldb_dn **list;
344
345         list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
346         if (!list) {
347                 return LDB_ERR_OPERATIONS_ERROR;
348         }
349
350         list[priv->num_partitions] = ldb_dn_copy(list, req->op.reg_partition.dn);
351         if (!list[priv->num_partitions]) {
352                 return LDB_ERR_OPERATIONS_ERROR;
353         }
354
355         priv->num_partitions += 1;
356         priv->partitions = list;
357
358         return LDB_SUCCESS;
359 }
360  
361
362 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
363 {
364         switch (req->operation) {
365
366         case LDB_REQ_REGISTER_CONTROL:
367                 return rootdse_register_control(module, req);
368         case LDB_REQ_REGISTER_PARTITION:
369                 return rootdse_register_partition(module, req);
370
371         default:
372                 break;
373         }
374         return ldb_next_request(module, req);
375 }
376
377 static int rootdse_init(struct ldb_module *module)
378 {
379         struct private_data *data;
380
381         data = talloc(module, struct private_data);
382         if (data == NULL) {
383                 return -1;
384         }
385
386         data->num_controls = 0;
387         data->controls = NULL;
388         data->num_partitions = 0;
389         data->partitions = NULL;
390         module->private_data = data;
391
392         ldb_set_default_dns(module->ldb);
393
394         return ldb_next_init(module);
395 }
396
397 _PUBLIC_ const struct ldb_module_ops ldb_rootdse_module_ops = {
398         .name                   = "rootdse",
399         .init_context           = rootdse_init,
400         .search                 = rootdse_search,
401         .request                = rootdse_request
402 };