r16036: Add a couple of new functions to corretly deal with timeouts.
[kai/samba.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 };
35
36 /*
37   return 1 if a specific attribute has been requested
38 */
39 static int do_attribute(const char * const *attrs, const char *name)
40 {
41         return attrs == NULL ||
42                 ldb_attr_in_list(attrs, name) ||
43                 ldb_attr_in_list(attrs, "*");
44 }
45
46
47 /*
48   add dynamically generated attributes to rootDSE result
49 */
50 static int rootdse_add_dynamic(struct ldb_module *module, struct ldb_message *msg, const char * const *attrs)
51 {
52         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
53         struct cli_credentials *server_creds;
54
55         msg->dn = ldb_dn_explode(msg, "");
56
57         /* don't return the distinduishedName attribute if any */
58         ldb_msg_remove_attr(msg, "distinguishedName");
59
60         if (do_attribute(attrs, "currentTime")) {
61                 if (ldb_msg_add_steal_string(msg, "currentTime", 
62                                              ldb_timestring(msg, time(NULL))) != 0) {
63                         goto failed;
64                 }
65         }
66
67         if (do_attribute(attrs, "supportedControl")) {
68                 int i;
69                 for (i = 0; i < priv->num_controls; i++) {
70                         char *control = talloc_strdup(msg, priv->controls[i]);
71                         if (!control) {
72                                 goto failed;
73                         }
74                         if (ldb_msg_add_steal_string(msg, "supportedControl",
75                                                      control) != 0) {
76                                 goto failed;
77                         }
78                 }
79         }
80
81         server_creds = talloc_get_type(ldb_get_opaque(module->ldb, "server_credentials"), 
82                                        struct cli_credentials);
83         if (server_creds && do_attribute(attrs, "supportedSASLMechanisms")) {
84                 struct gensec_security_ops **backends = gensec_security_all();
85                 enum credentials_use_kerberos use_kerberos
86                         = cli_credentials_get_kerberos_state(server_creds);
87                 struct gensec_security_ops **ops
88                         = gensec_use_kerberos_mechs(msg, backends, use_kerberos);
89                 int i;
90                 for (i = 0; ops && ops[i]; i++) {
91                         if (ops[i]->sasl_name) {
92                                 char *sasl_name = talloc_strdup(msg, ops[i]->sasl_name);
93                                 if (!sasl_name) {
94                                         goto failed;
95                                 }
96                                 if (ldb_msg_add_steal_string(msg, "supportedSASLMechanisms",
97                                                              sasl_name) != 0) {
98                                         goto failed;
99                                 }
100                         }
101                 }
102         }
103
104         if (do_attribute(attrs, "highestCommittedUSN")) {
105                 if (module->ldb->sequence_number != NULL && 
106                     ldb_msg_add_fmt(msg, "highestCommittedUSN", 
107                                     "%llu", module->ldb->sequence_number(module->ldb)) != 0) {
108                         goto failed;
109                 }
110         }
111         
112         /* TODO: lots more dynamic attributes should be added here */
113
114         return LDB_SUCCESS;
115
116 failed:
117         return LDB_ERR_OPERATIONS_ERROR;
118 }
119
120 /*
121   handle search requests
122 */
123
124 struct rootdse_async_context {
125         struct ldb_module *module;
126         void *up_context;
127         int (*up_callback)(struct ldb_context *, void *, struct ldb_async_result *);
128
129         const char * const * attrs;
130 };
131
132 static int rootdse_async_callback(struct ldb_context *ldb, void *context, struct ldb_async_result *ares)
133 {
134         struct rootdse_async_context *ac;
135
136         if (!context || !ares) {
137                 ldb_set_errstring(ldb, talloc_asprintf(ldb, "NULL Context or Result in callback"));
138                 goto error;
139         }
140
141         ac = talloc_get_type(context, struct rootdse_async_context);
142
143         if (ares->type == LDB_REPLY_ENTRY) {
144                 /* for each record returned post-process to add any dynamic
145                    attributes that have been asked for */
146                 if (rootdse_add_dynamic(ac->module, ares->message, ac->attrs) != LDB_SUCCESS) {
147                         goto error;
148                 }
149         }
150
151         return ac->up_callback(ldb, ac->up_context, ares);
152
153 error:
154         talloc_free(ares);
155         return LDB_ERR_OPERATIONS_ERROR;
156 }
157
158 static int rootdse_search(struct ldb_module *module, struct ldb_request *req)
159 {
160         struct rootdse_async_context *ac;
161         struct ldb_request *down_req;
162         int ret;
163
164         /* see if its for the rootDSE */
165         if (req->op.search.scope != LDB_SCOPE_BASE ||
166             (req->op.search.base && req->op.search.base->comp_num != 0)) {
167                 return ldb_next_request(module, req);
168         }
169
170         ac = talloc(req, struct rootdse_async_context);
171         if (ac == NULL) {
172                 return LDB_ERR_OPERATIONS_ERROR;
173         }
174
175         ac->module = module;
176         ac->up_context = req->async.context;
177         ac->up_callback = req->async.callback;
178         ac->attrs = req->op.search.attrs;
179
180         down_req = talloc_zero(req, struct ldb_request);
181         if (down_req == NULL) {
182                 return LDB_ERR_OPERATIONS_ERROR;
183         }
184
185         down_req->operation = req->operation;
186         /* in our db we store the rootDSE with a DN of cn=rootDSE */
187         down_req->op.search.base = ldb_dn_explode(down_req, "cn=rootDSE");
188         down_req->op.search.scope = LDB_SCOPE_BASE;
189         down_req->op.search.tree = ldb_parse_tree(down_req, "dn=*");
190         if (down_req->op.search.base == NULL || down_req->op.search.tree == NULL) {
191                 ldb_oom(module->ldb);
192                 talloc_free(down_req);
193                 return LDB_ERR_OPERATIONS_ERROR;
194         }
195         down_req->op.search.attrs = req->op.search.attrs;
196         down_req->controls = req->controls;
197
198         down_req->async.context = ac;
199         down_req->async.callback = rootdse_async_callback;
200         ldb_set_timeout_from_prev_req(module->ldb, req, down_req);
201
202         /* perform the search */
203         ret = ldb_next_request(module, down_req);
204
205         /* do not free down_req as the call results may be linked to it,
206          * it will be freed when the upper level request get freed */
207         if (ret == LDB_SUCCESS) {
208                 req->async.handle = down_req->async.handle;
209         }
210
211         return ret;
212 }
213
214 static int rootdse_register_control(struct ldb_module *module, struct ldb_request *req)
215 {
216         struct private_data *priv = talloc_get_type(module->private_data, struct private_data);
217         char **list;
218
219         list = talloc_realloc(priv, priv->controls, char *, priv->num_controls + 1);
220         if (!list) {
221                 return LDB_ERR_OPERATIONS_ERROR;
222         }
223
224         list[priv->num_controls] = talloc_strdup(list, req->op.reg.oid);
225         if (!list[priv->num_controls]) {
226                 return LDB_ERR_OPERATIONS_ERROR;
227         }
228
229         priv->num_controls += 1;
230         priv->controls = list;
231
232         return LDB_SUCCESS;
233 }
234  
235
236 static int rootdse_request(struct ldb_module *module, struct ldb_request *req)
237 {
238         switch (req->operation) {
239
240         case LDB_REQ_REGISTER:
241                 return rootdse_register_control(module, req);
242
243         default:
244                 break;
245         }
246         return ldb_next_request(module, req);
247 }
248
249 static int rootdse_init(struct ldb_module *module)
250 {
251         struct private_data *data;
252
253         data = talloc(module, struct private_data);
254         if (data == NULL) {
255                 return -1;
256         }
257
258         data->num_controls = 0;
259         data->controls = NULL;
260         module->private_data = data;
261
262         return ldb_next_init(module);
263 }
264
265 static const struct ldb_module_ops rootdse_ops = {
266         .name                   = "rootdse",
267         .init_context           = rootdse_init,
268         .search                 = rootdse_search,
269         .request                = rootdse_request
270 };
271
272 int rootdse_module_init(void)
273 {
274         return ldb_register_module(&rootdse_ops);
275 }
276