s3:dbwrap_tool: add --persistent switch and mode for non-persistent DBs
[Samba/gbeck.git] / source3 / utils / dbwrap_tool.c
blob7c90b54de76d8a6902ed3f95f8b8a4c221592840
1 /*
2 Samba Unix/Linux CIFS implementation
4 low level TDB/CTDB tool using the dbwrap interface
6 Copyright (C) 2009 Michael Adam <obnox@samba.org>
7 Copyright (C) 2011 Bjoern Baumbach <bb@sernet.de>
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.
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.
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/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "popt_common.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "dbwrap/dbwrap_watch.h"
29 #include "messages.h"
30 #include "util_tdb.h"
32 enum dbwrap_op { OP_FETCH, OP_STORE, OP_DELETE, OP_ERASE, OP_LISTKEYS,
33 OP_LISTWATCHERS };
35 enum dbwrap_type { TYPE_INT32, TYPE_UINT32, TYPE_STRING, TYPE_HEX, TYPE_NONE };
37 static int dbwrap_tool_fetch_int32(struct db_context *db,
38 const char *keyname,
39 const char *data)
41 int32_t value;
42 NTSTATUS status;
44 status = dbwrap_fetch_int32_bystring(db, keyname, &value);
45 if (!NT_STATUS_IS_OK(status)) {
46 d_printf("Error fetching int32 from key '%s': %s\n",
47 keyname, nt_errstr(status));
48 return -1;
50 d_printf("%d\n", value);
52 return 0;
55 static int dbwrap_tool_fetch_uint32(struct db_context *db,
56 const char *keyname,
57 const char *data)
59 uint32_t value;
60 NTSTATUS ret;
62 ret = dbwrap_fetch_uint32_bystring(db, keyname, &value);
63 if (NT_STATUS_IS_OK(ret)) {
64 d_printf("%u\n", value);
65 return 0;
66 } else {
67 d_fprintf(stderr, "ERROR: could not fetch uint32 key '%s': "
68 "%s\n", nt_errstr(ret), keyname);
69 return -1;
73 static int dbwrap_tool_fetch_string(struct db_context *db,
74 const char *keyname,
75 const char *data)
77 TDB_DATA tdbdata;
78 NTSTATUS status;
79 TALLOC_CTX *tmp_ctx = talloc_stackframe();
80 int ret;
82 status = dbwrap_fetch_bystring(db, tmp_ctx, keyname, &tdbdata);
83 if (NT_STATUS_IS_OK(status)) {
84 d_printf("%-*.*s\n", (int)tdbdata.dsize, (int)tdbdata.dsize,
85 tdbdata.dptr);
86 ret = 0;
87 } else {
88 d_fprintf(stderr, "ERROR: could not fetch string key '%s': "
89 "%s\n", nt_errstr(status), keyname);
90 ret = -1;
93 talloc_free(tmp_ctx);
94 return ret;
97 static int dbwrap_tool_fetch_hex(struct db_context *db,
98 const char *keyname,
99 const char *data)
101 TDB_DATA tdbdata;
102 DATA_BLOB datablob;
103 NTSTATUS status;
104 TALLOC_CTX *tmp_ctx = talloc_stackframe();
105 char *hex_string;
106 int ret;
108 status = dbwrap_fetch_bystring(db, tmp_ctx, keyname, &tdbdata);
109 if (NT_STATUS_IS_OK(status)) {
110 datablob.data = tdbdata.dptr;
111 datablob.length = tdbdata.dsize;
113 hex_string = data_blob_hex_string_upper(tmp_ctx, &datablob);
114 if (hex_string == NULL) {
115 d_fprintf(stderr, "ERROR: could not get hex string "
116 "from data blob\n");
117 ret = -1;
118 } else {
119 d_printf("%s\n", hex_string);
120 ret = 0;
122 } else {
123 d_fprintf(stderr, "ERROR: could not fetch hex key '%s': "
124 "%s\n", nt_errstr(status), keyname);
125 ret = -1;
128 talloc_free(tmp_ctx);
129 return ret;
132 static int dbwrap_tool_store_int32(struct db_context *db,
133 const char *keyname,
134 const char *data)
136 NTSTATUS status;
137 int32_t value = (int32_t)strtol(data, NULL, 10);
139 if (dbwrap_is_persistent(db)) {
140 status = dbwrap_trans_store_int32_bystring(db, keyname, value);
141 } else {
142 status = dbwrap_store_int32_bystring(db, keyname, value);
144 if (!NT_STATUS_IS_OK(status)) {
145 d_fprintf(stderr, "ERROR: could not store int32 key '%s': %s\n",
146 keyname, nt_errstr(status));
147 return -1;
150 return 0;
153 static int dbwrap_tool_store_uint32(struct db_context *db,
154 const char *keyname,
155 const char *data)
157 NTSTATUS status;
158 uint32_t value = (uint32_t)strtol(data, NULL, 10);
160 if (dbwrap_is_persistent(db)) {
161 status = dbwrap_trans_store_uint32_bystring(db, keyname, value);
162 } else {
163 status = dbwrap_store_uint32_bystring(db, keyname, value);
165 if (!NT_STATUS_IS_OK(status)) {
166 d_fprintf(stderr,
167 "ERROR: could not store uint32 key '%s': %s\n",
168 keyname, nt_errstr(status));
169 return -1;
172 return 0;
175 static int dbwrap_tool_store_string(struct db_context *db,
176 const char *keyname,
177 const char *data)
179 NTSTATUS status;
180 TDB_DATA tdbdata;
182 tdbdata = string_term_tdb_data(data);
184 if (dbwrap_is_persistent(db)) {
185 status = dbwrap_trans_store_bystring(db, keyname,
186 tdbdata,
187 TDB_REPLACE);
188 } else {
189 status = dbwrap_store_bystring(db, keyname,
190 tdbdata,
191 TDB_REPLACE);
193 if (!NT_STATUS_IS_OK(status)) {
194 d_fprintf(stderr,
195 "ERROR: could not store string key '%s': %s\n",
196 keyname, nt_errstr(status));
197 return -1;
200 return 0;
203 static int dbwrap_tool_store_hex(struct db_context *db,
204 const char *keyname,
205 const char *data)
207 NTSTATUS status;
208 DATA_BLOB datablob;
209 TDB_DATA tdbdata;
210 TALLOC_CTX *tmp_ctx = talloc_stackframe();
212 datablob = strhex_to_data_blob(tmp_ctx, data);
213 if(strlen(data) > 0 && datablob.length == 0) {
214 d_fprintf(stderr,
215 "ERROR: could not convert hex string to data blob\n"
216 " Not a valid hex string?\n");
217 talloc_free(tmp_ctx);
218 return -1;
221 tdbdata.dptr = (unsigned char *)datablob.data;
222 tdbdata.dsize = datablob.length;
224 if (dbwrap_is_persistent(db)) {
225 status = dbwrap_trans_store_bystring(db, keyname,
226 tdbdata,
227 TDB_REPLACE);
228 } else {
229 status = dbwrap_store_bystring(db, keyname,
230 tdbdata,
231 TDB_REPLACE);
233 if (!NT_STATUS_IS_OK(status)) {
234 d_fprintf(stderr,
235 "ERROR: could not store string key '%s': %s\n",
236 keyname, nt_errstr(status));
237 talloc_free(tmp_ctx);
238 return -1;
241 talloc_free(tmp_ctx);
242 return 0;
245 static int dbwrap_tool_delete(struct db_context *db,
246 const char *keyname,
247 const char *data)
249 NTSTATUS status;
251 if (dbwrap_is_persistent(db)) {
252 status = dbwrap_trans_delete_bystring(db, keyname);
253 } else {
254 status = dbwrap_delete_bystring(db, keyname);
257 if (!NT_STATUS_IS_OK(status)) {
258 d_fprintf(stderr, "ERROR deleting record %s : %s\n",
259 keyname, nt_errstr(status));
260 return -1;
263 return 0;
266 static int delete_fn(struct db_record *rec, void *priv)
268 dbwrap_record_delete(rec);
269 return 0;
273 * dbwrap_tool_erase: erase the whole data base
274 * the keyname argument is not used.
276 static int dbwrap_tool_erase(struct db_context *db,
277 const char *keyname,
278 const char *data)
280 NTSTATUS status;
282 status = dbwrap_traverse(db, delete_fn, NULL, NULL);
284 if (!NT_STATUS_IS_OK(status)) {
285 d_fprintf(stderr, "ERROR erasing the database\n");
286 return -1;
289 return 0;
292 static int listkey_fn(struct db_record *rec, void *private_data)
294 int length = dbwrap_record_get_key(rec).dsize;
295 unsigned char *p = (unsigned char *)dbwrap_record_get_key(rec).dptr;
297 while (length--) {
298 if (isprint(*p) && !strchr("\"\\", *p)) {
299 d_printf("%c", *p);
300 } else {
301 d_printf("\\%02X", *p);
303 p++;
306 d_printf("\n");
308 return 0;
311 static int dbwrap_tool_listkeys(struct db_context *db,
312 const char *keyname,
313 const char *data)
315 NTSTATUS status;
317 status = dbwrap_traverse_read(db, listkey_fn, NULL, NULL);
319 if (!NT_STATUS_IS_OK(status)) {
320 d_fprintf(stderr, "ERROR listing db keys\n");
321 return -1;
324 return 0;
327 static int dbwrap_tool_listwatchers_cb(const uint8_t *db_id, size_t db_id_len,
328 const TDB_DATA key,
329 const struct server_id *watchers,
330 size_t num_watchers,
331 void *private_data)
333 uint32_t i;
334 dump_data_file(db_id, db_id_len, false, stdout);
335 dump_data_file(key.dptr, key.dsize, false, stdout);
337 for (i=0; i<num_watchers; i++) {
338 char *str = server_id_str(talloc_tos(), &watchers[i]);
339 printf("%s\n", str);
340 TALLOC_FREE(str);
342 printf("\n");
343 return 0;
347 static int dbwrap_tool_listwatchers(struct db_context *db,
348 const char *keyname,
349 const char *data)
351 dbwrap_watchers_traverse_read(dbwrap_tool_listwatchers_cb, NULL);
352 return 0;
355 struct dbwrap_op_dispatch_table {
356 enum dbwrap_op op;
357 enum dbwrap_type type;
358 int (*cmd)(struct db_context *db,
359 const char *keyname,
360 const char *data);
363 struct dbwrap_op_dispatch_table dispatch_table[] = {
364 { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
365 { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
366 { OP_FETCH, TYPE_STRING, dbwrap_tool_fetch_string },
367 { OP_FETCH, TYPE_HEX, dbwrap_tool_fetch_hex },
368 { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
369 { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
370 { OP_STORE, TYPE_STRING, dbwrap_tool_store_string },
371 { OP_STORE, TYPE_HEX, dbwrap_tool_store_hex },
372 { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
373 { OP_ERASE, TYPE_INT32, dbwrap_tool_erase },
374 { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
375 { OP_LISTWATCHERS, TYPE_NONE, dbwrap_tool_listwatchers },
376 { 0, 0, NULL },
379 int main(int argc, const char **argv)
381 struct tevent_context *evt_ctx;
382 struct messaging_context *msg_ctx;
383 struct db_context *db;
385 uint16_t count;
387 const char *dbname;
388 const char *opname;
389 enum dbwrap_op op;
390 const char *keyname = "";
391 const char *keytype = "int32";
392 enum dbwrap_type type;
393 const char *valuestr = "0";
394 int persistent = 0;
395 int tdb_flags = TDB_DEFAULT;
397 TALLOC_CTX *mem_ctx = talloc_stackframe();
399 int ret = 1;
401 struct poptOption popt_options[] = {
402 POPT_AUTOHELP
403 POPT_COMMON_SAMBA
404 { "persistent", 'p', POPT_ARG_NONE, &persistent, 0, "treat the database as persistent", NULL },
405 POPT_TABLEEND
407 int opt;
408 const char **extra_argv;
409 int extra_argc = 0;
410 poptContext pc;
412 load_case_tables();
413 lp_set_cmdline("log level", "0");
414 setup_logging(argv[0], DEBUG_STDERR);
416 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
418 while ((opt = poptGetNextOpt(pc)) != -1) {
419 switch (opt) {
420 default:
421 fprintf(stderr, "Invalid option %s: %s\n",
422 poptBadOption(pc, 0), poptStrerror(opt));
423 goto done;
427 /* setup the remaining options for the main program to use */
428 extra_argv = poptGetArgs(pc);
429 if (extra_argv) {
430 extra_argv++;
431 while (extra_argv[extra_argc]) extra_argc++;
434 lp_load_global(get_dyn_CONFIGFILE());
436 if ((extra_argc < 2) || (extra_argc > 5)) {
437 d_fprintf(stderr,
438 "USAGE: %s [options] <database> <op> [<key> [<type> "
439 "[<value>]]]\n"
440 " ops: fetch, store, delete, erase, listkeys, "
441 "listwatchers\n"
442 " types: int32, uint32, string, hex\n",
443 argv[0]);
444 goto done;
447 dbname = extra_argv[0];
448 opname = extra_argv[1];
450 if (strcmp(opname, "store") == 0) {
451 if (extra_argc != 5) {
452 d_fprintf(stderr, "ERROR: operation 'store' requires "
453 "value argument\n");
454 goto done;
456 valuestr = extra_argv[4];
457 keytype = extra_argv[3];
458 keyname = extra_argv[2];
459 op = OP_STORE;
460 } else if (strcmp(opname, "fetch") == 0) {
461 if (extra_argc != 4) {
462 d_fprintf(stderr, "ERROR: operation 'fetch' requires "
463 "type but not value argument\n");
464 goto done;
466 op = OP_FETCH;
467 keytype = extra_argv[3];
468 keyname = extra_argv[2];
469 } else if (strcmp(opname, "delete") == 0) {
470 if (extra_argc != 3) {
471 d_fprintf(stderr, "ERROR: operation 'delete' does "
472 "not allow type nor value argument\n");
473 goto done;
475 keyname = extra_argv[2];
476 op = OP_DELETE;
477 } else if (strcmp(opname, "erase") == 0) {
478 if (extra_argc != 2) {
479 d_fprintf(stderr, "ERROR: operation 'erase' does "
480 "not take a key argument\n");
481 goto done;
483 op = OP_ERASE;
484 } else if (strcmp(opname, "listkeys") == 0) {
485 if (extra_argc != 2) {
486 d_fprintf(stderr, "ERROR: operation 'listkeys' does "
487 "not take a key argument\n");
488 goto done;
490 op = OP_LISTKEYS;
491 } else if (strcmp(opname, "listwatchers") == 0) {
492 if (extra_argc != 2) {
493 d_fprintf(stderr, "ERROR: operation 'listwatchers' "
494 "does not take an argument\n");
495 goto done;
497 op = OP_LISTWATCHERS;
498 keytype = "none";
499 } else {
500 d_fprintf(stderr,
501 "ERROR: invalid op '%s' specified\n"
502 " supported ops: fetch, store, delete\n",
503 opname);
504 goto done;
507 if (strcmp(keytype, "int32") == 0) {
508 type = TYPE_INT32;
509 } else if (strcmp(keytype, "uint32") == 0) {
510 type = TYPE_UINT32;
511 } else if (strcmp(keytype, "string") == 0) {
512 type = TYPE_STRING;
513 } else if (strcmp(keytype, "hex") == 0) {
514 type = TYPE_HEX;
515 } else if (strcmp(keytype, "none") == 0) {
516 type = TYPE_NONE;
517 } else {
518 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
519 " supported types: int32, uint32, "
520 "string, hex, none\n",
521 keytype);
522 goto done;
525 evt_ctx = tevent_context_init(mem_ctx);
526 if (evt_ctx == NULL) {
527 d_fprintf(stderr, "ERROR: could not init event context\n");
528 goto done;
531 msg_ctx = messaging_init(mem_ctx, evt_ctx);
532 if (msg_ctx == NULL) {
533 d_fprintf(stderr, "ERROR: could not init messaging context\n");
534 goto done;
537 if (persistent == 0) {
538 tdb_flags |= TDB_CLEAR_IF_FIRST;
541 switch (op) {
542 case OP_FETCH:
543 case OP_STORE:
544 case OP_DELETE:
545 case OP_ERASE:
546 case OP_LISTKEYS:
547 db = db_open(mem_ctx, dbname, 0, tdb_flags, O_RDWR | O_CREAT,
548 0644, DBWRAP_LOCK_ORDER_1);
549 if (db == NULL) {
550 d_fprintf(stderr, "ERROR: could not open dbname\n");
551 goto done;
553 break;
554 default:
555 db = NULL;
556 break;
559 for (count = 0; dispatch_table[count].cmd != NULL; count++) {
560 if ((op == dispatch_table[count].op) &&
561 (type == dispatch_table[count].type))
563 ret = dispatch_table[count].cmd(db, keyname, valuestr);
564 break;
568 done:
569 TALLOC_FREE(mem_ctx);
570 return ret;