r17514: Simplify the way to set ldb errors and add another
[kai/samba-autobuild/.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 2 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, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24 #include "includes.h"
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 "auth/gensec/gensec.h"
29 #include "system/time.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
49 /*
50   add dynamically generated attributes to rootDSE result
51 */
52 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg, const char * const *attrs)
53 {
54         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
55         struct cli_credentials *server_creds;
56
57         msg->dn = ldb_dn_explode(msg, "");
58
59         /* don't return the distinduishedName, cn and name attributes */
60         ldb_msg_remove_attr(msg, "distinguishedName");
61         ldb_msg_remove_attr(msg, "cn");
62         ldb_msg_remove_attr(msg, "name");
63
64         if (do_attribute(attrs, "currentTime")) {
65                 if (ldb_msg_add_steal_string(msg, "currentTime", 
66                                              ldb_timestring(msg, time(NULL))) != 0) {
67                         goto failed;
68                 }
69         }
70
71         if (do_attribute(attrs, "supportedControl")) {
72                 int i;
73                 for (i = 0; i < priv->num_controls; i++) {
74                         char *control = talloc_strdup(msg, priv->controls[i]);
75                         if (!control) {
76                                 goto failed;
77                         }
78                         if (ldb_msg_add_steal_string(msg, "supportedControl",
79                                                      control) != 0) {
80                                 goto failed;
81                         }
82                 }
83         }
84
85         if (do_attribute(attrs, "namingContexts")) {
86                 int i;
87                 for (i = 0; i < priv->num_partitions; i++) {
88                         struct ldb_dn *dn = priv->partitions[i];
89                         if (ldb_msg_add_steal_string(msg, "namingContexts",
90                                                      ldb_dn_linearize(msg, dn)) != 0) {
91                                 goto failed;
92                         }
93                 }
94         }
95
96         server_creds = talloc_get_type(ldb_get_opaque(module->ldb, "server_credentials"), 
97                                        struct cli_credentials);
98         if (server_creds && do_attribute(attrs, "supportedSASLMechanisms")) {
99                 struct gensec_security_ops **backends = gensec_security_all();
100                 enum credentials_use_kerberos use_kerberos
101                         = cli_credentials_get_kerberos_state(server_creds);
102                 struct gensec_security_ops **ops
103                         = gensec_use_kerberos_mechs(msg, backends, use_kerberos);
104                 int i;
105                 for (i = 0; ops && ops[i]; i++) {
106                         if (ops[i]->sasl_name) {
107                                 char *sasl_name = talloc_strdup(msg, ops[i]->sasl_name);
108                                 if (!sasl_name) {
109                                         goto failed;
110                                 }
111                                 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
112                                                              sasl_name) != 0) {
113                                         goto failed;
114                                 }
115                         }
116                 }
117         }
118
119         if (do_attribute(attrs, "highestCommittedUSN")) {
120                 uint64_t seq_num;
121                 int ret = ldb_sequence_number(module->ldb, &seq_num);
122                 if (ret == LDB_SUCCESS) {
123                         if (ldb_msg_add_fmt(msg, "highestCommittedUSN", 
124                                             "%llu", seq_num) != 0) {
125                                 goto failed;
126                         }
127                 }
128         }
129
130         /* TODO: lots more dynamic attributes should be added here */
131
132         return LDB_SUCCESS;
133
134 failed:
135         return LDB_ERR_OPERATIONS_ERROR;
136 }
137
138 /*
139   handle search requests
140 */
141
142 struct rootdse_context {
143         struct ldb_module *module;
144         void *up_context;
145         int (*up_callback)(struct ldb_context *, void *, struct ldb_reply *);
146
147         const char * const * attrs;
148 };
149
150 static int rootdse_callback(struct ldb_context *ldb, void *context, struct ldb_reply *ares)
151 {
152         struct rootdse_context *ac;
153
154         if (!context || !ares) {
155                 ldb_set_errstring(ldb, "NULL Context or Result in callback");
156                 goto error;
157         }
158
159         ac = talloc_get_type(context, struct rootdse_context);
160
161         if (ares->type == LDB_REPLY_ENTRY) {
162                 /* for each record returned post-process to add any dynamic
163                    attributes that have been asked for */
164                 if (rootdse_add_dynamic(ac->module, ares->message, ac->attrs) != LDB_SUCCESS) {
165                         goto error;
166                 }
167         }
168
169         return ac->up_callback(ldb, ac->up_context, ares);
170
171 error:
172         talloc_free(ares);
173         return LDB_ERR_OPERATIONS_ERROR;
174 }
175
176 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
177 {
178         struct rootdse_context *ac;
179         struct ldb_request *down_req;
180         int ret;
181
182         /* see if its for the rootDSE */
183         if (req->op.search.scope != LDB_SCOPE_BASE ||
184             (req->op.search.base && req->op.search.base->comp_num != 0)) {
185                 return ldb_next_request(module, req);
186         }
187
188         ac = talloc(req, struct rootdse_context);
189         if (ac == NULL) {
190                 return LDB_ERR_OPERATIONS_ERROR;
191         }
192
193         ac->module = module;
194         ac->up_context = req->context;
195         ac->up_callback = req->callback;
196         ac->attrs = req->op.search.attrs;
197
198         down_req = talloc_zero(req, struct ldb_request);
199         if (down_req == NULL) {
200                 return LDB_ERR_OPERATIONS_ERROR;
201         }
202
203         down_req->operation = req->operation;
204         /* in our db we store the rootDSE with a DN of cn=rootDSE */
205         down_req->op.search.base = ldb_dn_explode(down_req, "cn=rootDSE");
206         down_req->op.search.scope = LDB_SCOPE_BASE;
207         down_req->op.search.tree = ldb_parse_tree(down_req, NULL);
208         if (down_req->op.search.base == NULL || down_req->op.search.tree == NULL) {
209                 ldb_oom(module->ldb);
210                 talloc_free(down_req);
211                 return LDB_ERR_OPERATIONS_ERROR;
212         }
213         down_req->op.search.attrs = req->op.search.attrs;
214         down_req->controls = req->controls;
215
216         down_req->context = ac;
217         down_req->callback = rootdse_callback;
218         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
219
220         /* perform the search */
221         ret = ldb_next_request(module, down_req);
222
223         /* do not free down_req as the call results may be linked to it,
224          * it will be freed when the upper level request get freed */
225         if (ret == LDB_SUCCESS) {
226                 req->handle = down_req->handle;
227         }
228
229         return ret;
230 }
231
232 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
233 {
234         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
235         char **list;
236
237         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
238         if (!list) {
239                 return LDB_ERR_OPERATIONS_ERROR;
240         }
241
242         list[priv->num_controls] = talloc_strdup(list, req->op.reg_control.oid);
243         if (!list[priv->num_controls]) {
244                 return LDB_ERR_OPERATIONS_ERROR;
245         }
246
247         priv->num_controls += 1;
248         priv->controls = list;
249
250         return LDB_SUCCESS;
251 }
252  
253 static int rootdse_register_partition(struct ldb_module *module, struct ldb_request *req)
254 {
255         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
256         struct ldb_dn **list;
257
258         list = talloc_realloc(priv, priv->partitions, struct ldb_dn *, priv->num_partitions + 1);
259         if (!list) {
260                 return LDB_ERR_OPERATIONS_ERROR;
261         }
262
263         list[priv->num_partitions] = talloc_reference(list, req->op.reg_partition.dn);
264         if (!list[priv->num_partitions]) {
265                 return LDB_ERR_OPERATIONS_ERROR;
266         }
267
268         priv->num_partitions += 1;
269         priv->partitions = list;
270
271         return LDB_SUCCESS;
272 }
273  
274
275 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
276 {
277         switch (req->operation) {
278
279         case LDB_REQ_REGISTER_CONTROL:
280                 return rootdse_register_control(module, req);
281         case LDB_REQ_REGISTER_PARTITION:
282                 return rootdse_register_partition(module, req);
283
284         default:
285                 break;
286         }
287         return ldb_next_request(module, req);
288 }
289
290 static int rootdse_init(struct ldb_module *module)
291 {
292         struct private_data *data;
293
294         data = talloc(module, struct private_data);
295         if (data == NULL) {
296                 return -1;
297         }
298
299         data->num_controls = 0;
300         data->controls = NULL;
301         data->num_partitions = 0;
302         data->partitions = NULL;
303         module->private_data = data;
304
305         return ldb_next_init(module);
306 }
307
308 static const struct ldb_module_ops rootdse_ops = {
309         .name                   = "rootdse",
310         .init_context           = rootdse_init,
311         .search                 = rootdse_search,
312         .request                = rootdse_request
313 };
314
315 int rootdse_module_init(void)
316 {
317         return ldb_register_module(&rootdse_ops);
318 }
319