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/>.
24 #include "system/filesys.h"
25 #include "lib/cmdline/cmdline.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_open.h"
30 #include "cmdline_contexts.h"
31 #include "lib/param/param.h"
33 enum dbwrap_op
{ OP_FETCH
, OP_STORE
, OP_DELETE
, OP_ERASE
, OP_LISTKEYS
,
36 enum dbwrap_type
{ TYPE_INT32
, TYPE_UINT32
, TYPE_STRING
, TYPE_HEX
, TYPE_NONE
};
38 static int dbwrap_tool_fetch_int32(struct db_context
*db
,
45 status
= dbwrap_fetch_int32_bystring(db
, keyname
, &value
);
46 if (!NT_STATUS_IS_OK(status
)) {
47 d_printf("Error fetching int32 from key '%s': %s\n",
48 keyname
, nt_errstr(status
));
51 d_printf("%d\n", value
);
56 static int dbwrap_tool_fetch_uint32(struct db_context
*db
,
63 ret
= dbwrap_fetch_uint32_bystring(db
, keyname
, &value
);
64 if (NT_STATUS_IS_OK(ret
)) {
65 d_printf("%u\n", value
);
68 d_fprintf(stderr
, "ERROR: could not fetch uint32 key '%s': "
69 "%s\n", nt_errstr(ret
), keyname
);
74 static int dbwrap_tool_fetch_string(struct db_context
*db
,
80 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
83 status
= dbwrap_fetch_bystring(db
, tmp_ctx
, keyname
, &tdbdata
);
84 if (NT_STATUS_IS_OK(status
)) {
85 d_printf("%-*.*s\n", (int)tdbdata
.dsize
, (int)tdbdata
.dsize
,
89 d_fprintf(stderr
, "ERROR: could not fetch string key '%s': "
90 "%s\n", nt_errstr(status
), keyname
);
98 static int dbwrap_tool_fetch_hex(struct db_context
*db
,
105 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
109 status
= dbwrap_fetch_bystring(db
, tmp_ctx
, keyname
, &tdbdata
);
110 if (NT_STATUS_IS_OK(status
)) {
111 datablob
.data
= tdbdata
.dptr
;
112 datablob
.length
= tdbdata
.dsize
;
114 hex_string
= data_blob_hex_string_upper(tmp_ctx
, &datablob
);
115 if (hex_string
== NULL
) {
116 d_fprintf(stderr
, "ERROR: could not get hex string "
120 d_printf("%s\n", hex_string
);
124 d_fprintf(stderr
, "ERROR: could not fetch hex key '%s': "
125 "%s\n", nt_errstr(status
), keyname
);
129 talloc_free(tmp_ctx
);
133 static int dbwrap_tool_store_int32(struct db_context
*db
,
138 int32_t value
= (int32_t)strtol(data
, NULL
, 10);
140 if (dbwrap_is_persistent(db
)) {
141 status
= dbwrap_trans_store_int32_bystring(db
, keyname
, value
);
143 status
= dbwrap_store_int32_bystring(db
, keyname
, value
);
145 if (!NT_STATUS_IS_OK(status
)) {
146 d_fprintf(stderr
, "ERROR: could not store int32 key '%s': %s\n",
147 keyname
, nt_errstr(status
));
154 static int dbwrap_tool_store_uint32(struct db_context
*db
,
159 uint32_t value
= (uint32_t)strtol(data
, NULL
, 10);
161 if (dbwrap_is_persistent(db
)) {
162 status
= dbwrap_trans_store_uint32_bystring(db
, keyname
, value
);
164 status
= dbwrap_store_uint32_bystring(db
, keyname
, value
);
166 if (!NT_STATUS_IS_OK(status
)) {
168 "ERROR: could not store uint32 key '%s': %s\n",
169 keyname
, nt_errstr(status
));
176 static int dbwrap_tool_store_string(struct db_context
*db
,
183 tdbdata
= string_term_tdb_data(data
);
185 if (dbwrap_is_persistent(db
)) {
186 status
= dbwrap_trans_store_bystring(db
, keyname
,
190 status
= dbwrap_store_bystring(db
, keyname
,
194 if (!NT_STATUS_IS_OK(status
)) {
196 "ERROR: could not store string key '%s': %s\n",
197 keyname
, nt_errstr(status
));
204 static int dbwrap_tool_store_hex(struct db_context
*db
,
211 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
213 datablob
= strhex_to_data_blob(tmp_ctx
, data
);
214 if(strlen(data
) > 0 && datablob
.length
== 0) {
216 "ERROR: could not convert hex string to data blob\n"
217 " Not a valid hex string?\n");
218 talloc_free(tmp_ctx
);
222 tdbdata
.dptr
= (unsigned char *)datablob
.data
;
223 tdbdata
.dsize
= datablob
.length
;
225 if (dbwrap_is_persistent(db
)) {
226 status
= dbwrap_trans_store_bystring(db
, keyname
,
230 status
= dbwrap_store_bystring(db
, keyname
,
234 if (!NT_STATUS_IS_OK(status
)) {
236 "ERROR: could not store string key '%s': %s\n",
237 keyname
, nt_errstr(status
));
238 talloc_free(tmp_ctx
);
242 talloc_free(tmp_ctx
);
246 static int dbwrap_tool_delete(struct db_context
*db
,
252 if (dbwrap_is_persistent(db
)) {
253 status
= dbwrap_trans_delete_bystring(db
, keyname
);
255 status
= dbwrap_delete_bystring(db
, keyname
);
258 if (!NT_STATUS_IS_OK(status
)) {
259 d_fprintf(stderr
, "ERROR deleting record %s : %s\n",
260 keyname
, nt_errstr(status
));
267 static int dbwrap_tool_exists(struct db_context
*db
,
273 result
= dbwrap_exists(db
, string_term_tdb_data(keyname
));
276 d_fprintf(stdout
, "Key %s exists\n", keyname
);
278 d_fprintf(stdout
, "Key %s does not exist\n", keyname
);
285 * dbwrap_tool_erase: erase the whole data base
286 * the keyname argument is not used.
288 static int dbwrap_tool_erase(struct db_context
*db
,
294 ret
= dbwrap_wipe(db
);
297 d_fprintf(stderr
, "ERROR erasing the database\n");
304 static int listkey_fn(struct db_record
*rec
, void *private_data
)
306 TDB_DATA key
= dbwrap_record_get_key(rec
);
307 size_t length
= key
.dsize
;
308 unsigned char *p
= (unsigned char *)key
.dptr
;
311 if (isprint(*p
) && !strchr("\"\\", *p
)) {
314 d_printf("\\%02X", *p
);
324 static int dbwrap_tool_listkeys(struct db_context
*db
,
330 status
= dbwrap_traverse_read(db
, listkey_fn
, NULL
, NULL
);
332 if (!NT_STATUS_IS_OK(status
)) {
333 d_fprintf(stderr
, "ERROR listing db keys\n");
340 struct dbwrap_op_dispatch_table
{
342 enum dbwrap_type type
;
343 int (*cmd
)(struct db_context
*db
,
348 struct dbwrap_op_dispatch_table dispatch_table
[] = {
349 { OP_FETCH
, TYPE_INT32
, dbwrap_tool_fetch_int32
},
350 { OP_FETCH
, TYPE_UINT32
, dbwrap_tool_fetch_uint32
},
351 { OP_FETCH
, TYPE_STRING
, dbwrap_tool_fetch_string
},
352 { OP_FETCH
, TYPE_HEX
, dbwrap_tool_fetch_hex
},
353 { OP_STORE
, TYPE_INT32
, dbwrap_tool_store_int32
},
354 { OP_STORE
, TYPE_UINT32
, dbwrap_tool_store_uint32
},
355 { OP_STORE
, TYPE_STRING
, dbwrap_tool_store_string
},
356 { OP_STORE
, TYPE_HEX
, dbwrap_tool_store_hex
},
357 { OP_DELETE
, TYPE_INT32
, dbwrap_tool_delete
},
358 { OP_ERASE
, TYPE_INT32
, dbwrap_tool_erase
},
359 { OP_LISTKEYS
, TYPE_INT32
, dbwrap_tool_listkeys
},
360 { OP_EXISTS
, TYPE_STRING
, dbwrap_tool_exists
},
364 int main(int argc
, const char **argv
)
366 struct tevent_context
*evt_ctx
;
367 struct messaging_context
*msg_ctx
;
368 struct db_context
*db
;
375 const char *keyname
= "";
376 const char *keytype
= "int32";
377 enum dbwrap_type type
;
378 const char *valuestr
= "0";
380 int non_persistent
= 0;
381 int tdb_flags
= TDB_DEFAULT
;
383 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
384 struct loadparm_context
*lp_ctx
= NULL
;
389 struct poptOption popt_options
[] = {
392 { "non-persistent", 0, POPT_ARG_NONE
, &non_persistent
, 0,
393 "treat the database as non-persistent "
394 "(CAVEAT: This mode might wipe your database!)",
396 { "persistent", 0, POPT_ARG_NONE
, &persistent
, 0,
397 "treat the database as persistent",
403 const char **extra_argv
;
409 setup_logging(argv
[0], DEBUG_DEFAULT_STDERR
);
411 ok
= samba_cmdline_init(mem_ctx
,
412 SAMBA_CMDLINE_CONFIG_CLIENT
,
413 false /* require_smbconf */);
415 DBG_ERR("Failed to init cmdline parser!\n");
416 TALLOC_FREE(mem_ctx
);
419 lp_ctx
= samba_cmdline_get_lp_ctx();
420 lpcfg_set_cmdline(lp_ctx
, "log level", "0");
422 pc
= samba_popt_get_context(getprogname(),
426 POPT_CONTEXT_KEEP_FIRST
);
428 DBG_ERR("Failed to setup popt context!\n");
429 TALLOC_FREE(mem_ctx
);
433 while ((opt
= poptGetNextOpt(pc
)) != -1) {
436 fprintf(stderr
, "Invalid option %s: %s\n",
437 poptBadOption(pc
, 0), poptStrerror(opt
));
442 /* setup the remaining options for the main program to use */
443 extra_argv
= poptGetArgs(pc
);
446 while (extra_argv
[extra_argc
]) extra_argc
++;
449 if ((extra_argc
< 2) || (extra_argc
> 5)) {
451 "USAGE: %s [options] <database> <op> [<key> [<type> "
453 " ops: fetch, store, delete, exists, "
455 " types: int32, uint32, string, hex\n",
460 if ((persistent
+ non_persistent
) != 1) {
461 d_fprintf(stderr
, "ERROR: you must specify exactly one "
462 "of --persistent and --non-persistent\n");
465 if (non_persistent
== 1) {
466 tdb_flags
|= TDB_CLEAR_IF_FIRST
;
469 dbname
= extra_argv
[0];
470 opname
= extra_argv
[1];
472 if (strcmp(opname
, "store") == 0) {
473 if (extra_argc
!= 5) {
474 d_fprintf(stderr
, "ERROR: operation 'store' requires "
478 valuestr
= extra_argv
[4];
479 keytype
= extra_argv
[3];
480 keyname
= extra_argv
[2];
482 } else if (strcmp(opname
, "fetch") == 0) {
483 if (extra_argc
!= 4) {
484 d_fprintf(stderr
, "ERROR: operation 'fetch' requires "
485 "type but not value argument\n");
489 keytype
= extra_argv
[3];
490 keyname
= extra_argv
[2];
491 } else if (strcmp(opname
, "delete") == 0) {
492 if (extra_argc
!= 3) {
493 d_fprintf(stderr
, "ERROR: operation 'delete' does "
494 "not allow type nor value argument\n");
497 keyname
= extra_argv
[2];
499 } else if (strcmp(opname
, "erase") == 0) {
500 if (extra_argc
!= 2) {
501 d_fprintf(stderr
, "ERROR: operation 'erase' does "
502 "not take a key argument\n");
506 } else if (strcmp(opname
, "listkeys") == 0) {
507 if (extra_argc
!= 2) {
508 d_fprintf(stderr
, "ERROR: operation 'listkeys' does "
509 "not take a key argument\n");
513 } else if (strcmp(opname
, "exists") == 0) {
514 if (extra_argc
!= 3) {
515 d_fprintf(stderr
, "ERROR: operation 'exists' does "
516 "not allow type nor value argument\n");
519 keyname
= extra_argv
[2];
524 "ERROR: invalid op '%s' specified\n"
525 " supported ops: fetch, store, delete, exists, "
531 if (strcmp(keytype
, "int32") == 0) {
533 } else if (strcmp(keytype
, "uint32") == 0) {
535 } else if (strcmp(keytype
, "string") == 0) {
537 } else if (strcmp(keytype
, "hex") == 0) {
539 } else if (strcmp(keytype
, "none") == 0) {
542 d_fprintf(stderr
, "ERROR: invalid type '%s' specified.\n"
543 " supported types: int32, uint32, "
544 "string, hex, none\n",
549 evt_ctx
= samba_tevent_context_init(mem_ctx
);
550 if (evt_ctx
== NULL
) {
551 d_fprintf(stderr
, "ERROR: could not init event context\n");
555 msg_ctx
= messaging_init(mem_ctx
, evt_ctx
);
556 if (msg_ctx
== NULL
) {
557 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
568 db
= db_open(mem_ctx
, dbname
, 0, tdb_flags
, O_RDWR
| O_CREAT
,
569 0644, DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
571 d_fprintf(stderr
, "ERROR: could not open dbname\n");
580 for (count
= 0; dispatch_table
[count
].cmd
!= NULL
; count
++) {
581 if ((op
== dispatch_table
[count
].op
) &&
582 (type
== dispatch_table
[count
].type
))
584 ret
= dispatch_table
[count
].cmd(db
, keyname
, valuestr
);
591 TALLOC_FREE(mem_ctx
);