ctdb: make use of ctdb_canonicalize_ip_inplace() in ctdb_control_tcp_client()
[Samba.git] / source3 / utils / dbwrap_tool.c
blob3c7f39854c8df7392cdd5399ea125437a9a5bc8c
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 "lib/cmdline/cmdline.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "messages.h"
29 #include "util_tdb.h"
30 #include "cmdline_contexts.h"
32 enum dbwrap_op { OP_FETCH, OP_STORE, OP_DELETE, OP_ERASE, OP_LISTKEYS,
33 OP_EXISTS };
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 dbwrap_tool_exists(struct db_context *db,
267 const char *keyname,
268 const char *data)
270 bool result;
272 result = dbwrap_exists(db, string_term_tdb_data(keyname));
274 if (result) {
275 d_fprintf(stdout, "Key %s exists\n", keyname);
276 } else {
277 d_fprintf(stdout, "Key %s does not exist\n", keyname);
280 return (result)?0:1;
284 * dbwrap_tool_erase: erase the whole data base
285 * the keyname argument is not used.
287 static int dbwrap_tool_erase(struct db_context *db,
288 const char *keyname,
289 const char *data)
291 int ret;
293 ret = dbwrap_wipe(db);
295 if (ret != 0) {
296 d_fprintf(stderr, "ERROR erasing the database\n");
297 return -1;
300 return 0;
303 static int listkey_fn(struct db_record *rec, void *private_data)
305 TDB_DATA key = dbwrap_record_get_key(rec);
306 size_t length = key.dsize;
307 unsigned char *p = (unsigned char *)key.dptr;
309 while (length--) {
310 if (isprint(*p) && !strchr("\"\\", *p)) {
311 d_printf("%c", *p);
312 } else {
313 d_printf("\\%02X", *p);
315 p++;
318 d_printf("\n");
320 return 0;
323 static int dbwrap_tool_listkeys(struct db_context *db,
324 const char *keyname,
325 const char *data)
327 NTSTATUS status;
329 status = dbwrap_traverse_read(db, listkey_fn, NULL, NULL);
331 if (!NT_STATUS_IS_OK(status)) {
332 d_fprintf(stderr, "ERROR listing db keys\n");
333 return -1;
336 return 0;
339 struct dbwrap_op_dispatch_table {
340 enum dbwrap_op op;
341 enum dbwrap_type type;
342 int (*cmd)(struct db_context *db,
343 const char *keyname,
344 const char *data);
347 struct dbwrap_op_dispatch_table dispatch_table[] = {
348 { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
349 { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
350 { OP_FETCH, TYPE_STRING, dbwrap_tool_fetch_string },
351 { OP_FETCH, TYPE_HEX, dbwrap_tool_fetch_hex },
352 { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
353 { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
354 { OP_STORE, TYPE_STRING, dbwrap_tool_store_string },
355 { OP_STORE, TYPE_HEX, dbwrap_tool_store_hex },
356 { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
357 { OP_ERASE, TYPE_INT32, dbwrap_tool_erase },
358 { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
359 { OP_EXISTS, TYPE_STRING, dbwrap_tool_exists },
360 { 0, 0, NULL },
363 int main(int argc, const char **argv)
365 struct tevent_context *evt_ctx;
366 struct messaging_context *msg_ctx;
367 struct db_context *db;
369 uint16_t count;
371 const char *dbname;
372 const char *opname;
373 enum dbwrap_op op;
374 const char *keyname = "";
375 const char *keytype = "int32";
376 enum dbwrap_type type;
377 const char *valuestr = "0";
378 int persistent = 0;
379 int non_persistent = 0;
380 int tdb_flags = TDB_DEFAULT;
382 TALLOC_CTX *mem_ctx = talloc_stackframe();
384 int ret = 1;
385 bool ok;
387 struct poptOption popt_options[] = {
388 POPT_AUTOHELP
389 POPT_COMMON_SAMBA
390 { "non-persistent", 0, POPT_ARG_NONE, &non_persistent, 0,
391 "treat the database as non-persistent "
392 "(CAVEAT: This mode might wipe your database!)",
393 NULL },
394 { "persistent", 0, POPT_ARG_NONE, &persistent, 0,
395 "treat the database as persistent",
396 NULL },
397 POPT_COMMON_VERSION
398 POPT_TABLEEND
400 int opt;
401 const char **extra_argv;
402 int extra_argc = 0;
403 poptContext pc;
405 smb_init_locale();
407 setup_logging(argv[0], DEBUG_DEFAULT_STDERR);
408 lp_set_cmdline("log level", "0");
410 ok = samba_cmdline_init(mem_ctx,
411 SAMBA_CMDLINE_CONFIG_CLIENT,
412 false /* require_smbconf */);
413 if (!ok) {
414 DBG_ERR("Failed to init cmdline parser!\n");
415 TALLOC_FREE(mem_ctx);
416 exit(1);
419 pc = samba_popt_get_context(getprogname(),
420 argc,
421 argv,
422 popt_options,
423 POPT_CONTEXT_KEEP_FIRST);
424 if (!ok) {
425 DBG_ERR("Failed to setup popt context!\n");
426 TALLOC_FREE(mem_ctx);
427 exit(1);
430 while ((opt = poptGetNextOpt(pc)) != -1) {
431 switch (opt) {
432 default:
433 fprintf(stderr, "Invalid option %s: %s\n",
434 poptBadOption(pc, 0), poptStrerror(opt));
435 goto done;
439 /* setup the remaining options for the main program to use */
440 extra_argv = poptGetArgs(pc);
441 if (extra_argv) {
442 extra_argv++;
443 while (extra_argv[extra_argc]) extra_argc++;
446 if ((extra_argc < 2) || (extra_argc > 5)) {
447 d_fprintf(stderr,
448 "USAGE: %s [options] <database> <op> [<key> [<type> "
449 "[<value>]]]\n"
450 " ops: fetch, store, delete, exists, "
451 "erase, listkeys\n"
452 " types: int32, uint32, string, hex\n",
453 argv[0]);
454 goto done;
457 if ((persistent + non_persistent) != 1) {
458 d_fprintf(stderr, "ERROR: you must specify exactly one "
459 "of --persistent and --non-persistent\n");
460 goto done;
462 if (non_persistent == 1) {
463 tdb_flags |= TDB_CLEAR_IF_FIRST;
466 dbname = extra_argv[0];
467 opname = extra_argv[1];
469 if (strcmp(opname, "store") == 0) {
470 if (extra_argc != 5) {
471 d_fprintf(stderr, "ERROR: operation 'store' requires "
472 "value argument\n");
473 goto done;
475 valuestr = extra_argv[4];
476 keytype = extra_argv[3];
477 keyname = extra_argv[2];
478 op = OP_STORE;
479 } else if (strcmp(opname, "fetch") == 0) {
480 if (extra_argc != 4) {
481 d_fprintf(stderr, "ERROR: operation 'fetch' requires "
482 "type but not value argument\n");
483 goto done;
485 op = OP_FETCH;
486 keytype = extra_argv[3];
487 keyname = extra_argv[2];
488 } else if (strcmp(opname, "delete") == 0) {
489 if (extra_argc != 3) {
490 d_fprintf(stderr, "ERROR: operation 'delete' does "
491 "not allow type nor value argument\n");
492 goto done;
494 keyname = extra_argv[2];
495 op = OP_DELETE;
496 } else if (strcmp(opname, "erase") == 0) {
497 if (extra_argc != 2) {
498 d_fprintf(stderr, "ERROR: operation 'erase' does "
499 "not take a key argument\n");
500 goto done;
502 op = OP_ERASE;
503 } else if (strcmp(opname, "listkeys") == 0) {
504 if (extra_argc != 2) {
505 d_fprintf(stderr, "ERROR: operation 'listkeys' does "
506 "not take a key argument\n");
507 goto done;
509 op = OP_LISTKEYS;
510 } else if (strcmp(opname, "exists") == 0) {
511 if (extra_argc != 3) {
512 d_fprintf(stderr, "ERROR: operation 'exists' does "
513 "not allow type nor value argument\n");
514 goto done;
516 keyname = extra_argv[2];
517 op = OP_EXISTS;
518 keytype = "string";
519 } else {
520 d_fprintf(stderr,
521 "ERROR: invalid op '%s' specified\n"
522 " supported ops: fetch, store, delete, exists, "
523 "erase, listkeys\n",
524 opname);
525 goto done;
528 if (strcmp(keytype, "int32") == 0) {
529 type = TYPE_INT32;
530 } else if (strcmp(keytype, "uint32") == 0) {
531 type = TYPE_UINT32;
532 } else if (strcmp(keytype, "string") == 0) {
533 type = TYPE_STRING;
534 } else if (strcmp(keytype, "hex") == 0) {
535 type = TYPE_HEX;
536 } else if (strcmp(keytype, "none") == 0) {
537 type = TYPE_NONE;
538 } else {
539 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
540 " supported types: int32, uint32, "
541 "string, hex, none\n",
542 keytype);
543 goto done;
546 evt_ctx = samba_tevent_context_init(mem_ctx);
547 if (evt_ctx == NULL) {
548 d_fprintf(stderr, "ERROR: could not init event context\n");
549 goto done;
552 msg_ctx = messaging_init(mem_ctx, evt_ctx);
553 if (msg_ctx == NULL) {
554 d_fprintf(stderr, "ERROR: could not init messaging context\n");
555 goto done;
558 switch (op) {
559 case OP_FETCH:
560 case OP_STORE:
561 case OP_DELETE:
562 case OP_ERASE:
563 case OP_LISTKEYS:
564 case OP_EXISTS:
565 db = db_open(mem_ctx, dbname, 0, tdb_flags, O_RDWR | O_CREAT,
566 0644, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
567 if (db == NULL) {
568 d_fprintf(stderr, "ERROR: could not open dbname\n");
569 goto done;
571 break;
572 default:
573 db = NULL;
574 break;
577 for (count = 0; dispatch_table[count].cmd != NULL; count++) {
578 if ((op == dispatch_table[count].op) &&
579 (type == dispatch_table[count].type))
581 ret = dispatch_table[count].cmd(db, keyname, valuestr);
582 break;
586 done:
587 poptFreeContext(pc);
588 TALLOC_FREE(mem_ctx);
589 return ret;