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 "popt_common.h"
26 #include "dbwrap/dbwrap.h"
27 #include "dbwrap/dbwrap_open.h"
28 #include "dbwrap/dbwrap_watch.h"
32 enum dbwrap_op
{ OP_FETCH
, OP_STORE
, OP_DELETE
, OP_ERASE
, OP_LISTKEYS
,
33 OP_LISTWATCHERS
, 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
,
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
));
50 d_printf("%d\n", value
);
55 static int dbwrap_tool_fetch_uint32(struct db_context
*db
,
62 ret
= dbwrap_fetch_uint32_bystring(db
, keyname
, &value
);
63 if (NT_STATUS_IS_OK(ret
)) {
64 d_printf("%u\n", value
);
67 d_fprintf(stderr
, "ERROR: could not fetch uint32 key '%s': "
68 "%s\n", nt_errstr(ret
), keyname
);
73 static int dbwrap_tool_fetch_string(struct db_context
*db
,
79 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
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
,
88 d_fprintf(stderr
, "ERROR: could not fetch string key '%s': "
89 "%s\n", nt_errstr(status
), keyname
);
97 static int dbwrap_tool_fetch_hex(struct db_context
*db
,
104 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
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 "
119 d_printf("%s\n", hex_string
);
123 d_fprintf(stderr
, "ERROR: could not fetch hex key '%s': "
124 "%s\n", nt_errstr(status
), keyname
);
128 talloc_free(tmp_ctx
);
132 static int dbwrap_tool_store_int32(struct db_context
*db
,
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
);
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
));
153 static int dbwrap_tool_store_uint32(struct db_context
*db
,
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
);
163 status
= dbwrap_store_uint32_bystring(db
, keyname
, value
);
165 if (!NT_STATUS_IS_OK(status
)) {
167 "ERROR: could not store uint32 key '%s': %s\n",
168 keyname
, nt_errstr(status
));
175 static int dbwrap_tool_store_string(struct db_context
*db
,
182 tdbdata
= string_term_tdb_data(data
);
184 if (dbwrap_is_persistent(db
)) {
185 status
= dbwrap_trans_store_bystring(db
, keyname
,
189 status
= dbwrap_store_bystring(db
, keyname
,
193 if (!NT_STATUS_IS_OK(status
)) {
195 "ERROR: could not store string key '%s': %s\n",
196 keyname
, nt_errstr(status
));
203 static int dbwrap_tool_store_hex(struct db_context
*db
,
210 TALLOC_CTX
*tmp_ctx
= talloc_stackframe();
212 datablob
= strhex_to_data_blob(tmp_ctx
, data
);
213 if(strlen(data
) > 0 && datablob
.length
== 0) {
215 "ERROR: could not convert hex string to data blob\n"
216 " Not a valid hex string?\n");
217 talloc_free(tmp_ctx
);
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
,
229 status
= dbwrap_store_bystring(db
, keyname
,
233 if (!NT_STATUS_IS_OK(status
)) {
235 "ERROR: could not store string key '%s': %s\n",
236 keyname
, nt_errstr(status
));
237 talloc_free(tmp_ctx
);
241 talloc_free(tmp_ctx
);
245 static int dbwrap_tool_delete(struct db_context
*db
,
251 if (dbwrap_is_persistent(db
)) {
252 status
= dbwrap_trans_delete_bystring(db
, keyname
);
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
));
266 static int dbwrap_tool_exists(struct db_context
*db
,
272 result
= dbwrap_exists(db
, string_term_tdb_data(keyname
));
275 d_fprintf(stdout
, "Key %s exists\n", keyname
);
277 d_fprintf(stdout
, "Key %s does not exist\n", keyname
);
284 static int delete_fn(struct db_record
*rec
, void *priv
)
286 dbwrap_record_delete(rec
);
291 * dbwrap_tool_erase: erase the whole data base
292 * the keyname argument is not used.
294 static int dbwrap_tool_erase(struct db_context
*db
,
300 status
= dbwrap_traverse(db
, delete_fn
, NULL
, NULL
);
302 if (!NT_STATUS_IS_OK(status
)) {
303 d_fprintf(stderr
, "ERROR erasing the database\n");
310 static int listkey_fn(struct db_record
*rec
, void *private_data
)
312 int length
= dbwrap_record_get_key(rec
).dsize
;
313 unsigned char *p
= (unsigned char *)dbwrap_record_get_key(rec
).dptr
;
316 if (isprint(*p
) && !strchr("\"\\", *p
)) {
319 d_printf("\\%02X", *p
);
329 static int dbwrap_tool_listkeys(struct db_context
*db
,
335 status
= dbwrap_traverse_read(db
, listkey_fn
, NULL
, NULL
);
337 if (!NT_STATUS_IS_OK(status
)) {
338 d_fprintf(stderr
, "ERROR listing db keys\n");
345 static int dbwrap_tool_listwatchers_cb(const uint8_t *db_id
, size_t db_id_len
,
347 const struct server_id
*watchers
,
352 dump_data_file(db_id
, db_id_len
, false, stdout
);
353 dump_data_file(key
.dptr
, key
.dsize
, false, stdout
);
355 for (i
=0; i
<num_watchers
; i
++) {
356 char *str
= server_id_str(talloc_tos(), &watchers
[i
]);
365 static int dbwrap_tool_listwatchers(struct db_context
*db
,
369 dbwrap_watchers_traverse_read(dbwrap_tool_listwatchers_cb
, NULL
);
373 struct dbwrap_op_dispatch_table
{
375 enum dbwrap_type type
;
376 int (*cmd
)(struct db_context
*db
,
381 struct dbwrap_op_dispatch_table dispatch_table
[] = {
382 { OP_FETCH
, TYPE_INT32
, dbwrap_tool_fetch_int32
},
383 { OP_FETCH
, TYPE_UINT32
, dbwrap_tool_fetch_uint32
},
384 { OP_FETCH
, TYPE_STRING
, dbwrap_tool_fetch_string
},
385 { OP_FETCH
, TYPE_HEX
, dbwrap_tool_fetch_hex
},
386 { OP_STORE
, TYPE_INT32
, dbwrap_tool_store_int32
},
387 { OP_STORE
, TYPE_UINT32
, dbwrap_tool_store_uint32
},
388 { OP_STORE
, TYPE_STRING
, dbwrap_tool_store_string
},
389 { OP_STORE
, TYPE_HEX
, dbwrap_tool_store_hex
},
390 { OP_DELETE
, TYPE_INT32
, dbwrap_tool_delete
},
391 { OP_ERASE
, TYPE_INT32
, dbwrap_tool_erase
},
392 { OP_LISTKEYS
, TYPE_INT32
, dbwrap_tool_listkeys
},
393 { OP_LISTWATCHERS
, TYPE_NONE
, dbwrap_tool_listwatchers
},
394 { OP_EXISTS
, TYPE_STRING
, dbwrap_tool_exists
},
398 int main(int argc
, const char **argv
)
400 struct tevent_context
*evt_ctx
;
401 struct messaging_context
*msg_ctx
;
402 struct db_context
*db
;
409 const char *keyname
= "";
410 const char *keytype
= "int32";
411 enum dbwrap_type type
;
412 const char *valuestr
= "0";
414 int non_persistent
= 0;
415 int tdb_flags
= TDB_DEFAULT
;
417 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
421 struct poptOption popt_options
[] = {
424 { "non-persistent", 0, POPT_ARG_NONE
, &non_persistent
, 0,
425 "treat the database as non-persistent "
426 "(CAVEAT: This mode might wipe your database!)",
428 { "persistent", 0, POPT_ARG_NONE
, &persistent
, 0,
429 "treat the database as persistent",
434 const char **extra_argv
;
439 lp_set_cmdline("log level", "0");
440 setup_logging(argv
[0], DEBUG_STDERR
);
442 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
444 while ((opt
= poptGetNextOpt(pc
)) != -1) {
447 fprintf(stderr
, "Invalid option %s: %s\n",
448 poptBadOption(pc
, 0), poptStrerror(opt
));
453 /* setup the remaining options for the main program to use */
454 extra_argv
= poptGetArgs(pc
);
457 while (extra_argv
[extra_argc
]) extra_argc
++;
460 lp_load_global(get_dyn_CONFIGFILE());
462 if ((extra_argc
< 2) || (extra_argc
> 5)) {
464 "USAGE: %s [options] <database> <op> [<key> [<type> "
466 " ops: fetch, store, delete, exists, "
467 "erase, listkeys, listwatchers\n"
468 " types: int32, uint32, string, hex\n",
473 if ((persistent
== 0 && non_persistent
== 0) ||
474 (persistent
== 1 && non_persistent
== 1))
476 d_fprintf(stderr
, "ERROR: you must specify exactly one "
477 "of --persistent and --non-persistent\n");
479 } else if (non_persistent
== 1) {
480 tdb_flags
|= TDB_CLEAR_IF_FIRST
;
483 dbname
= extra_argv
[0];
484 opname
= extra_argv
[1];
486 if (strcmp(opname
, "store") == 0) {
487 if (extra_argc
!= 5) {
488 d_fprintf(stderr
, "ERROR: operation 'store' requires "
492 valuestr
= extra_argv
[4];
493 keytype
= extra_argv
[3];
494 keyname
= extra_argv
[2];
496 } else if (strcmp(opname
, "fetch") == 0) {
497 if (extra_argc
!= 4) {
498 d_fprintf(stderr
, "ERROR: operation 'fetch' requires "
499 "type but not value argument\n");
503 keytype
= extra_argv
[3];
504 keyname
= extra_argv
[2];
505 } else if (strcmp(opname
, "delete") == 0) {
506 if (extra_argc
!= 3) {
507 d_fprintf(stderr
, "ERROR: operation 'delete' does "
508 "not allow type nor value argument\n");
511 keyname
= extra_argv
[2];
513 } else if (strcmp(opname
, "erase") == 0) {
514 if (extra_argc
!= 2) {
515 d_fprintf(stderr
, "ERROR: operation 'erase' does "
516 "not take a key argument\n");
520 } else if (strcmp(opname
, "listkeys") == 0) {
521 if (extra_argc
!= 2) {
522 d_fprintf(stderr
, "ERROR: operation 'listkeys' does "
523 "not take a key argument\n");
527 } else if (strcmp(opname
, "listwatchers") == 0) {
528 if (extra_argc
!= 2) {
529 d_fprintf(stderr
, "ERROR: operation 'listwatchers' "
530 "does not take an argument\n");
533 op
= OP_LISTWATCHERS
;
535 } else if (strcmp(opname
, "exists") == 0) {
536 if (extra_argc
!= 3) {
537 d_fprintf(stderr
, "ERROR: operation 'exists' does "
538 "not allow type nor value argument\n");
541 keyname
= extra_argv
[2];
546 "ERROR: invalid op '%s' specified\n"
547 " supported ops: fetch, store, delete, exists, "
548 "erase, listkeys, listwatchers\n",
553 if (strcmp(keytype
, "int32") == 0) {
555 } else if (strcmp(keytype
, "uint32") == 0) {
557 } else if (strcmp(keytype
, "string") == 0) {
559 } else if (strcmp(keytype
, "hex") == 0) {
561 } else if (strcmp(keytype
, "none") == 0) {
564 d_fprintf(stderr
, "ERROR: invalid type '%s' specified.\n"
565 " supported types: int32, uint32, "
566 "string, hex, none\n",
571 evt_ctx
= samba_tevent_context_init(mem_ctx
);
572 if (evt_ctx
== NULL
) {
573 d_fprintf(stderr
, "ERROR: could not init event context\n");
577 msg_ctx
= messaging_init(mem_ctx
, evt_ctx
);
578 if (msg_ctx
== NULL
) {
579 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
590 db
= db_open(mem_ctx
, dbname
, 0, tdb_flags
, O_RDWR
| O_CREAT
,
591 0644, DBWRAP_LOCK_ORDER_1
, DBWRAP_FLAG_NONE
);
593 d_fprintf(stderr
, "ERROR: could not open dbname\n");
602 for (count
= 0; dispatch_table
[count
].cmd
!= NULL
; count
++) {
603 if ((op
== dispatch_table
[count
].op
) &&
604 (type
== dispatch_table
[count
].type
))
606 ret
= dispatch_table
[count
].cmd(db
, keyname
, valuestr
);
612 TALLOC_FREE(mem_ctx
);