samdb: use compat wrappers for tdb_fetch().
[metze/samba/wip.git] / source4 / dsdb / samdb / ldb_modules / partition_metadata.c
1 /*
2    Partitions ldb module - management of metadata.tdb for sequence number
3
4    Copyright (C) Amitay Isaacs <amitay@samba.org> 2011
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "dsdb/samdb/ldb_modules/partition.h"
21 #include "system/filesys.h"
22
23 #define LDB_METADATA_SEQ_NUM    "SEQ_NUM"
24
25
26 /*
27  * Read a key with uint64 value
28  */
29 static int partition_metadata_get_uint64(struct ldb_module *module,
30                                          const char *key, uint64_t *value,
31                                          uint64_t default_value)
32 {
33         struct partition_private_data *data;
34         struct tdb_context *tdb;
35         TDB_DATA tdb_key, tdb_data;
36         char *value_str;
37         TALLOC_CTX *tmp_ctx;
38
39         data = talloc_get_type_abort(ldb_module_get_private(module),
40                                      struct partition_private_data);
41
42         if (!data && !data->metadata && !data->metadata->db) {
43                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
44                                         "partition_metadata: metadata tdb not initialized");
45         }
46
47         tmp_ctx = talloc_new(NULL);
48         if (tmp_ctx == NULL) {
49                 return ldb_module_oom(module);
50         }
51
52         tdb = data->metadata->db->tdb;
53
54         tdb_key.dptr = (uint8_t *)discard_const_p(char, key);
55         tdb_key.dsize = strlen(key);
56
57         tdb_data = tdb_fetch_compat(tdb, tdb_key);
58         if (!tdb_data.dptr) {
59                 if (tdb_error(tdb) == TDB_ERR_NOEXIST) {
60                         *value = default_value;
61                         return LDB_SUCCESS;
62                 } else {
63                         return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
64                                                 tdb_errorstr_compat(tdb));
65                 }
66         }
67
68         value_str = talloc_strndup(tmp_ctx, (char *)tdb_data.dptr, tdb_data.dsize);
69         if (value_str == NULL) {
70                 SAFE_FREE(tdb_data.dptr);
71                 talloc_free(tmp_ctx);
72                 return ldb_module_oom(module);
73         }
74
75         *value = strtoull(value_str, NULL, 10);
76
77         SAFE_FREE(tdb_data.dptr);
78         talloc_free(tmp_ctx);
79
80         return LDB_SUCCESS;
81 }
82
83
84 /*
85  * Write a key with uin64 value
86  */
87 static int partition_metadata_set_uint64(struct ldb_module *module,
88                                          const char *key, uint64_t value,
89                                          bool insert)
90 {
91         struct partition_private_data *data;
92         struct tdb_context *tdb;
93         TDB_DATA tdb_key, tdb_data;
94         int tdb_flag;
95         char *value_str;
96         TALLOC_CTX *tmp_ctx;
97
98         data = talloc_get_type_abort(ldb_module_get_private(module),
99                                      struct partition_private_data);
100
101         if (!data && !data->metadata && !data->metadata->db) {
102                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
103                                         "partition_metadata: metadata tdb not initialized");
104         }
105
106         tmp_ctx = talloc_new(NULL);
107         if (tmp_ctx == NULL) {
108                 return ldb_module_oom(module);
109         }
110
111         tdb = data->metadata->db->tdb;
112
113         value_str = talloc_asprintf(tmp_ctx, "%llu", (unsigned long long)value);
114         if (value_str == NULL) {
115                 talloc_free(tmp_ctx);
116                 return ldb_module_oom(module);
117         }
118
119         tdb_key.dptr = (uint8_t *)discard_const_p(char, key);
120         tdb_key.dsize = strlen(key);
121
122         tdb_data.dptr = (uint8_t *)value_str;
123         tdb_data.dsize = strlen(value_str);
124
125         if (insert) {
126                 tdb_flag = TDB_INSERT;
127         } else {
128                 tdb_flag = TDB_MODIFY;
129         }
130
131         if (tdb_store(tdb, tdb_key, tdb_data, tdb_flag) != 0) {
132                 talloc_free(tmp_ctx);
133                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
134                                         tdb_errorstr_compat(tdb));
135         }
136
137         talloc_free(tmp_ctx);
138
139         return LDB_SUCCESS;
140 }
141
142
143 /*
144  * Open sam.ldb.d/metadata.tdb.
145  */
146 static int partition_metadata_open(struct ldb_module *module, bool create)
147 {
148         struct ldb_context *ldb = ldb_module_get_ctx(module);
149         TALLOC_CTX *tmp_ctx;
150         struct partition_private_data *data;
151         struct loadparm_context *lp_ctx;
152         const char *sam_name;
153         char *filename, *dirname;
154         int open_flags;
155
156         data = talloc_get_type_abort(ldb_module_get_private(module),
157                                      struct partition_private_data);
158         if (!data || !data->metadata) {
159                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
160                                         "partition_metadata: metadata not initialized");
161         }
162
163         tmp_ctx = talloc_new(NULL);
164         if (tmp_ctx == NULL) {
165                 return ldb_module_oom(module);
166         }
167
168         sam_name = (const char *)ldb_get_opaque(ldb, "ldb_url");
169         if (strncmp("tdb://", sam_name, 6) == 0) {
170                 sam_name += 6;
171         }
172         if (!sam_name) {
173                 talloc_free(tmp_ctx);
174                 return ldb_operr(ldb);
175         }
176         filename = talloc_asprintf(tmp_ctx, "%s.d/metadata.tdb", sam_name);
177         if (!filename) {
178                 talloc_free(tmp_ctx);
179                 return ldb_oom(ldb);
180         }
181
182         open_flags = O_RDWR;
183         if (create) {
184                 open_flags |= O_CREAT;
185
186                 /* While provisioning, sam.ldb.d directory may not exist,
187                  * so create it. Ignore errors, if it already exists. */
188                 dirname = talloc_asprintf(tmp_ctx, "%s.d", sam_name);
189                 if (!dirname) {
190                         talloc_free(tmp_ctx);
191                         return ldb_oom(ldb);
192                 }
193
194                 mkdir(dirname, 0700);
195                 talloc_free(dirname);
196         }
197
198         lp_ctx = talloc_get_type_abort(ldb_get_opaque(ldb, "loadparm"),
199                                        struct loadparm_context);
200
201         data->metadata->db = tdb_wrap_open(data->metadata, filename, 10,
202                                               TDB_DEFAULT, open_flags, 0660,
203                                               lp_ctx);
204         if (data->metadata->db == NULL) {
205                 talloc_free(tmp_ctx);
206                 if (create) {
207                         ldb_debug(ldb, LDB_DEBUG_ERROR,
208                                   "partition_metadata: Unable to create %s",
209                                   filename);
210                 }
211                 return LDB_ERR_OPERATIONS_ERROR;
212         }
213
214         talloc_free(tmp_ctx);
215         return LDB_SUCCESS;
216 }
217
218
219 /*
220  * Set the sequence number calculated from older logic (sum of primary sequence
221  * numbers for each partition) as LDB_METADATA_SEQ_NUM key.
222  */
223 static int partition_metadata_set_sequence_number(struct ldb_module *module)
224 {
225         struct partition_private_data *data;
226         struct ldb_result *res;
227         struct ldb_request *req;
228         struct ldb_seqnum_request *seq;
229         struct ldb_seqnum_result *seqr;
230         struct ldb_extended *ext;
231         TALLOC_CTX *tmp_ctx;
232         int ret;
233         uint64_t seq_number;
234
235         data = talloc_get_type_abort(ldb_module_get_private(module),
236                                     struct partition_private_data);
237         if (!data || !data->metadata) {
238                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
239                                         "partition_metadata: metadata not initialized");
240         }
241
242         tmp_ctx = talloc_new(data->metadata);
243         if (tmp_ctx == NULL) {
244                 return ldb_module_oom(module);
245         }
246
247         res = talloc_zero(tmp_ctx, struct ldb_result);
248         if (res == NULL) {
249                 talloc_free(tmp_ctx);
250                 return ldb_module_oom(module);
251         }
252
253         seq = talloc_zero(tmp_ctx, struct ldb_seqnum_request);
254         if (seq == NULL) {
255                 talloc_free(tmp_ctx);
256                 return ldb_module_oom(module);
257         }
258         seq->type = LDB_SEQ_HIGHEST_SEQ;
259
260         /* Build an extended request, so it can be passed to each partition in
261            partition_sequence_number_from_partitions() */
262         ret = ldb_build_extended_req(&req,
263                                      ldb_module_get_ctx(module),
264                                      tmp_ctx,
265                                      LDB_EXTENDED_SEQUENCE_NUMBER,
266                                      seq,
267                                      NULL,
268                                      res,
269                                      ldb_extended_default_callback,
270                                      NULL);
271         LDB_REQ_SET_LOCATION(req);
272         if (ret != LDB_SUCCESS) {
273                 talloc_free(tmp_ctx);
274                 return ret;
275         }
276
277         ret = partition_sequence_number_from_partitions(module, req, &ext);
278         if (ret != LDB_SUCCESS) {
279                 talloc_free(tmp_ctx);
280                 return ret;
281         }
282
283         seqr = talloc_get_type_abort(ext->data, struct ldb_seqnum_result);
284         seq_number = seqr->seq_num;
285
286         talloc_free(tmp_ctx);
287
288         return partition_metadata_set_uint64(module, LDB_METADATA_SEQ_NUM, seq_number, true);
289 }
290
291
292 /*
293  * Initialize metadata. Load metadata.tdb.
294  * If missing, create it and fill in sequence number
295  */
296 int partition_metadata_init(struct ldb_module *module)
297 {
298         struct partition_private_data *data;
299         int ret;
300
301         data = talloc_get_type_abort(ldb_module_get_private(module),
302                                      struct partition_private_data);
303
304         data->metadata = talloc_zero(data, struct partition_metadata);
305         if (data->metadata == NULL) {
306                 return ldb_module_oom(module);
307         }
308
309         ret = partition_metadata_open(module, false);
310         if (ret == LDB_SUCCESS) {
311                 goto end;
312         }
313
314         /* metadata.tdb does not exist, create it */
315         DEBUG(2, ("partition_metadata: Migrating partition metadata\n"));
316         ret = partition_metadata_open(module, true);
317         if (ret != LDB_SUCCESS) {
318                 talloc_free(data->metadata);
319                 data->metadata = NULL;
320                 goto end;
321         }
322
323         ret = partition_metadata_set_sequence_number(module);
324         if (ret != LDB_SUCCESS) {
325                 talloc_free(data->metadata);
326                 data->metadata = NULL;
327         }
328
329 end:
330         return ret;
331 }
332
333
334 /*
335  * Read the sequence number, default to 0 if LDB_METADATA_SEQ_NUM key is missing
336  */
337 int partition_metadata_sequence_number(struct ldb_module *module, uint64_t *value)
338 {
339         return partition_metadata_get_uint64(module,
340                                              LDB_METADATA_SEQ_NUM,
341                                              value,
342                                              0);
343 }
344
345
346 /*
347  * Increment the sequence number, returning the new sequence number
348  */
349 int partition_metadata_sequence_number_increment(struct ldb_module *module, uint64_t *value)
350 {
351         struct partition_private_data *data;
352         int ret;
353
354         data = talloc_get_type_abort(ldb_module_get_private(module),
355                                     struct partition_private_data);
356         if (!data && !data->metadata) {
357                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
358                                         "partition_metadata: metadata not initialized");
359         }
360
361         if (data->metadata->in_transaction == 0) {
362                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
363                                         "partition_metadata: increment sequence number without transaction");
364         }
365
366         ret = partition_metadata_get_uint64(module, LDB_METADATA_SEQ_NUM, value, 0);
367         if (ret != LDB_SUCCESS) {
368                 return ret;
369         }
370
371         (*value)++;
372         ret = partition_metadata_set_uint64(module, LDB_METADATA_SEQ_NUM, *value, false);
373         return ret;
374 }
375
376
377 /*
378  * Transaction start
379  */
380 int partition_metadata_start_trans(struct ldb_module *module)
381 {
382         struct partition_private_data *data;
383         struct tdb_context *tdb;
384
385         data = talloc_get_type_abort(ldb_module_get_private(module),
386                                      struct partition_private_data);
387         if (!data && !data->metadata && !data->metadata->db) {
388                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
389                                         "partition_metadata: metadata not initialized");
390         }
391         tdb = data->metadata->db->tdb;
392
393         if (tdb_transaction_start(tdb) != 0) {
394                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
395                                         tdb_errorstr_compat(tdb));
396         }
397
398         data->metadata->in_transaction++;
399
400         return LDB_SUCCESS;
401 }
402
403
404 /*
405  * Transaction prepare commit
406  */
407 int partition_metadata_prepare_commit(struct ldb_module *module)
408 {
409         struct partition_private_data *data;
410         struct tdb_context *tdb;
411
412         data = talloc_get_type_abort(ldb_module_get_private(module),
413                                      struct partition_private_data);
414         if (!data && !data->metadata && !data->metadata->db) {
415                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
416                                         "partition_metadata: metadata not initialized");
417         }
418         tdb = data->metadata->db->tdb;
419
420         if (data->metadata->in_transaction == 0) {
421                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
422                                         "partition_metadata: not in transaction");
423         }
424
425         if (tdb_transaction_prepare_commit(tdb) != 0) {
426                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
427                                         tdb_errorstr_compat(tdb));
428         }
429
430         return LDB_SUCCESS;
431 }
432
433
434 /*
435  * Transaction end
436  */
437 int partition_metadata_end_trans(struct ldb_module *module)
438 {
439         struct partition_private_data *data;
440         struct tdb_context *tdb;
441
442         data = talloc_get_type_abort(ldb_module_get_private(module),
443                                      struct partition_private_data);
444         if (!data && !data->metadata && !data->metadata->db) {
445                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
446                                         "partition_metadata: metadata not initialized");
447         }
448         tdb = data->metadata->db->tdb;
449
450         if (data->metadata->in_transaction == 0) {
451                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
452                                         "partition_metadata: not in transaction");
453         }
454
455         data->metadata->in_transaction--;
456
457         if (tdb_transaction_commit(tdb) != 0) {
458                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
459                                         tdb_errorstr_compat(tdb));
460         }
461
462         return LDB_SUCCESS;
463 }
464
465
466 /*
467  * Transaction delete
468  */
469 int partition_metadata_del_trans(struct ldb_module *module)
470 {
471         struct partition_private_data *data;
472         struct tdb_context *tdb;
473
474         data = talloc_get_type_abort(ldb_module_get_private(module),
475                                      struct partition_private_data);
476         if (!data && !data->metadata && !data->metadata->db) {
477                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
478                                         "partition_metadata: metadata not initialized");
479         }
480         tdb = data->metadata->db->tdb;
481
482         if (data->metadata->in_transaction == 0) {
483                 return ldb_module_error(module, LDB_ERR_OPERATIONS_ERROR,
484                                         "partition_metadata: not in transaction");
485         }
486
487         data->metadata->in_transaction--;
488
489         tdb_transaction_cancel(tdb);
490
491         return LDB_SUCCESS;
492 }