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