Finish removal of iconv_convenience in public API's.
[amitay/samba.git] / source4 / dsdb / samdb / ldb_modules / schema_load.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    The module that handles the Schema FSMO Role Owner
5    checkings, it also loads the dsdb_schema.
6    
7    Copyright (C) Stefan Metzmacher <metze@samba.org> 2007
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2009-2010
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22    
23 */
24
25 #include "includes.h"
26 #include "ldb_module.h"
27 #include "dsdb/samdb/samdb.h"
28 #include "librpc/gen_ndr/ndr_misc.h"
29 #include "librpc/gen_ndr/ndr_drsuapi.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "dsdb/samdb/ldb_modules/util.h"
33
34 struct schema_load_private_data {
35         bool in_transaction;
36 };
37
38 static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_dn, uint64_t current_usn,
39                                struct dsdb_schema **schema);
40
41 struct dsdb_schema *dsdb_schema_refresh(struct ldb_module *module, struct dsdb_schema *schema, bool is_global_schema)
42 {
43         uint64_t current_usn;
44         int ret;
45         struct ldb_result *res;
46         struct ldb_request *treq;
47         struct ldb_seqnum_request *tseq;
48         struct ldb_seqnum_result *tseqr;
49         struct dsdb_control_current_partition *ctrl;
50         struct ldb_context *ldb = ldb_module_get_ctx(module);
51         struct dsdb_schema *new_schema;
52         
53         struct schema_load_private_data *private_data = talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
54         if (!private_data) {
55                 /* We can't refresh until the init function has run */
56                 return schema;
57         }
58
59         /* We don't allow a schema reload during a transaction - nobody else can modify our schema behind our backs */
60         if (private_data->in_transaction) {
61                 return schema;
62         }
63
64         res = talloc_zero(schema, struct ldb_result);
65         if (res == NULL) {
66                 return NULL;
67         }
68         tseq = talloc_zero(res, struct ldb_seqnum_request);
69         if (tseq == NULL) {
70                 talloc_free(res);
71                 return NULL;
72         }
73         tseq->type = LDB_SEQ_HIGHEST_SEQ;
74         
75         ret = ldb_build_extended_req(&treq, ldb_module_get_ctx(module), res,
76                                      LDB_EXTENDED_SEQUENCE_NUMBER,
77                                      tseq,
78                                      NULL,
79                                      res,
80                                      ldb_extended_default_callback,
81                                      NULL);
82         if (ret != LDB_SUCCESS) {
83                 talloc_free(res);
84                 return NULL;
85         }
86         
87         ctrl = talloc(treq, struct dsdb_control_current_partition);
88         if (!ctrl) {
89                 talloc_free(res);
90                 return NULL;
91         }
92         ctrl->version = DSDB_CONTROL_CURRENT_PARTITION_VERSION;
93         ctrl->dn = schema->base_dn;
94         
95         ret = ldb_request_add_control(treq,
96                                       DSDB_CONTROL_CURRENT_PARTITION_OID,
97                                       false, ctrl);
98         if (ret != LDB_SUCCESS) {
99                 talloc_free(res);
100                 return NULL;
101         }
102         
103         ret = ldb_next_request(module, treq);
104         if (ret != LDB_SUCCESS) {
105                 talloc_free(res);
106                 return NULL;
107         }
108         ret = ldb_wait(treq->handle, LDB_WAIT_ALL);
109         if (ret != LDB_SUCCESS) {
110                 talloc_free(res);
111                 return NULL;
112         }
113         tseqr = talloc_get_type(res->extended->data,
114                                 struct ldb_seqnum_result);
115         if (tseqr->seq_num == schema->reload_seq_number) {
116                 talloc_free(res);
117                 return schema;
118         }
119
120         schema->reload_seq_number = tseqr->seq_num;
121         talloc_free(res);
122                 
123         ret = dsdb_module_load_partition_usn(module, schema->base_dn, &current_usn, NULL);
124         if (ret != LDB_SUCCESS || current_usn == schema->loaded_usn) {
125                 return schema;
126         }
127
128         ret = dsdb_schema_from_db(module, schema->base_dn, current_usn, &new_schema);
129         if (ret != LDB_SUCCESS) {
130                 return schema;
131         }
132         
133         if (is_global_schema) {
134                 dsdb_make_schema_global(ldb, new_schema);
135         }
136         return new_schema;
137 }
138
139
140 /*
141   Given an LDB module (pointing at the schema DB), and the DN, set the populated schema
142 */
143
144 static int dsdb_schema_from_db(struct ldb_module *module, struct ldb_dn *schema_dn, uint64_t current_usn,
145                                struct dsdb_schema **schema)
146 {
147         struct ldb_context *ldb = ldb_module_get_ctx(module);
148         TALLOC_CTX *tmp_ctx;
149         char *error_string;
150         int ret;
151         struct ldb_result *schema_res;
152         struct ldb_result *a_res;
153         struct ldb_result *c_res;
154         static const char *schema_attrs[] = {
155                 "prefixMap",
156                 "schemaInfo",
157                 "fSMORoleOwner",
158                 NULL
159         };
160         unsigned flags;
161
162         tmp_ctx = talloc_new(module);
163         if (!tmp_ctx) {
164                 ldb_oom(ldb);
165                 return LDB_ERR_OPERATIONS_ERROR;
166         }
167
168         /* we don't want to trace the schema load */
169         flags = ldb_get_flags(ldb);
170         ldb_set_flags(ldb, flags & ~LDB_FLG_ENABLE_TRACING);
171
172         /*
173          * setup the prefix mappings and schema info
174          */
175         ret = dsdb_module_search_dn(module, tmp_ctx, &schema_res,
176                                     schema_dn, schema_attrs, 0);
177         if (ret == LDB_ERR_NO_SUCH_OBJECT) {
178                 ldb_reset_err_string(ldb);
179                 ldb_debug(ldb, LDB_DEBUG_WARNING,
180                           "schema_load_init: no schema head present: (skip schema loading)\n");
181                 goto failed;
182         } else if (ret != LDB_SUCCESS) {
183                 ldb_asprintf_errstring(ldb, 
184                                        "dsdb_schema: failed to search the schema head: %s",
185                                        ldb_errstring(ldb));
186                 goto failed;
187         }
188
189         /*
190          * load the attribute definitions
191          */
192         ret = dsdb_module_search(module, tmp_ctx, &a_res,
193                                  schema_dn, LDB_SCOPE_ONELEVEL, NULL,
194                                  0, 
195                                  "(objectClass=attributeSchema)");
196         if (ret != LDB_SUCCESS) {
197                 ldb_asprintf_errstring(ldb, 
198                                        "dsdb_schema: failed to search attributeSchema objects: %s",
199                                        ldb_errstring(ldb));
200                 goto failed;
201         }
202
203         /*
204          * load the objectClass definitions
205          */
206         ret = dsdb_module_search(module, tmp_ctx, &c_res,
207                                  schema_dn, LDB_SCOPE_ONELEVEL, NULL,
208                                  DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT,
209                                  "(objectClass=classSchema)");
210         if (ret != LDB_SUCCESS) {
211                 ldb_asprintf_errstring(ldb, 
212                                        "dsdb_schema: failed to search classSchema objects: %s",
213                                        ldb_errstring(ldb));
214                 goto failed;
215         }
216
217         ret = dsdb_schema_from_ldb_results(tmp_ctx, ldb,
218                                            schema_res, a_res, c_res, schema, &error_string);
219         if (ret != LDB_SUCCESS) {
220                 ldb_asprintf_errstring(ldb, 
221                                        "dsdb_schema load failed: %s",
222                                        error_string);
223                 goto failed;
224         }
225
226         (*schema)->refresh_fn = dsdb_schema_refresh;
227         (*schema)->loaded_from_module = module;
228         (*schema)->loaded_usn = current_usn;
229
230         /* dsdb_set_schema() steal schema into the ldb_context */
231         ret = dsdb_set_schema(ldb, (*schema));
232
233         if (ret != LDB_SUCCESS) {
234                 ldb_debug_set(ldb, LDB_DEBUG_FATAL,
235                               "schema_load_init: dsdb_set_schema() failed: %d:%s: %s",
236                               ret, ldb_strerror(ret), ldb_errstring(ldb));
237                 talloc_free(tmp_ctx);
238                 return ret;
239         }
240
241         /* Ensure this module won't go away before the callback */
242         if (talloc_reference(*schema, ldb) == NULL) {
243                 ldb_oom(ldb);
244                 ret = LDB_ERR_OPERATIONS_ERROR;
245         }
246
247 failed:
248         if (flags & LDB_FLG_ENABLE_TRACING) {
249                 flags = ldb_get_flags(ldb);
250                 ldb_set_flags(ldb, flags | LDB_FLG_ENABLE_TRACING);
251         }
252         talloc_free(tmp_ctx);
253         return ret;
254 }       
255
256
257 static int schema_load_init(struct ldb_module *module)
258 {
259         struct schema_load_private_data *private_data;
260         struct dsdb_schema *schema;
261         struct ldb_context *ldb = ldb_module_get_ctx(module);
262         int ret;
263         uint64_t current_usn;
264         struct ldb_dn *schema_dn;
265
266         private_data = talloc_zero(module, struct schema_load_private_data);
267         if (private_data == NULL) {
268                 ldb_oom(ldb);
269                 return LDB_ERR_OPERATIONS_ERROR;
270         }
271
272         ldb_module_set_private(module, private_data);
273
274         ret = ldb_next_init(module);
275         if (ret != LDB_SUCCESS) {
276                 return ret;
277         }
278
279         if (dsdb_get_schema(ldb, NULL)) {
280                 return LDB_SUCCESS;
281         }
282
283         schema_dn = ldb_get_schema_basedn(ldb);
284         if (!schema_dn) {
285                 ldb_reset_err_string(ldb);
286                 ldb_debug(ldb, LDB_DEBUG_WARNING,
287                           "schema_load_init: no schema dn present: (skip schema loading)\n");
288                 return LDB_SUCCESS;
289         }
290
291         ret = dsdb_module_load_partition_usn(module, schema_dn, &current_usn, NULL);
292         if (ret != LDB_SUCCESS) {
293                 /* Ignore the error and just reload the DB more often */
294                 current_usn = 0;
295         }
296
297         return dsdb_schema_from_db(module, schema_dn, current_usn, &schema);
298 }
299
300 static int schema_load_start_transaction(struct ldb_module *module)
301 {
302         struct schema_load_private_data *private_data =
303                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
304
305         private_data->in_transaction = true;
306
307         return ldb_next_start_trans(module);
308 }
309
310 static int schema_load_del_transaction(struct ldb_module *module)
311 {
312         struct schema_load_private_data *private_data =
313                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
314
315         private_data->in_transaction = false;
316
317         return ldb_next_del_trans(module);
318 }
319
320 static int schema_load_prepare_commit(struct ldb_module *module)
321 {
322         int ret;
323         struct schema_load_private_data *private_data =
324                 talloc_get_type(ldb_module_get_private(module), struct schema_load_private_data);
325
326         ret = ldb_next_prepare_commit(module);
327         private_data->in_transaction = false;
328         return ret;
329 }
330
331 static int schema_load_extended(struct ldb_module *module, struct ldb_request *req)
332 {
333         struct ldb_context *ldb;
334
335         ldb = ldb_module_get_ctx(module);
336
337         if (strcmp(req->op.extended.oid, DSDB_EXTENDED_SCHEMA_UPDATE_NOW_OID) != 0) {
338                 return ldb_next_request(module, req);
339         }
340
341         /* This is a no-op.  We reload as soon as we can */
342         return ldb_module_done(req, NULL, NULL, LDB_SUCCESS);
343 }
344
345
346 _PUBLIC_ const struct ldb_module_ops ldb_schema_load_module_ops = {
347         .name           = "schema_load",
348         .init_context   = schema_load_init,
349         .extended       = schema_load_extended,
350         .start_transaction = schema_load_start_transaction,
351         .prepare_commit    = schema_load_prepare_commit,
352         .del_transaction   = schema_load_del_transaction,
353 };