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"
31 typedef enum { OP_FETCH
, OP_STORE
, OP_DELETE
, OP_ERASE
, OP_LISTKEYS
} dbwrap_op
;
33 typedef enum { TYPE_INT32
, TYPE_UINT32
, TYPE_STRING
} dbwrap_type
;
35 static int dbwrap_tool_fetch_int32(struct db_context
*db
,
42 status
= dbwrap_fetch_int32(db
, keyname
, &value
);
43 if (!NT_STATUS_IS_OK(status
)) {
44 d_printf("Error fetching int32 from key '%s': %s\n",
45 keyname
, nt_errstr(status
));
48 d_printf("%d\n", value
);
53 static int dbwrap_tool_fetch_uint32(struct db_context
*db
,
60 ret
= dbwrap_fetch_uint32(db
, keyname
, &value
);
61 if (NT_STATUS_IS_OK(ret
)) {
62 d_printf("%u\n", value
);
65 d_fprintf(stderr
, "ERROR: could not fetch uint32 key '%s': "
66 "%s\n", nt_errstr(ret
), keyname
);
71 static int dbwrap_tool_store_int32(struct db_context
*db
,
76 int32_t value
= (int32_t)strtol(data
, NULL
, 10);
78 status
= dbwrap_trans_store_int32(db
, keyname
, value
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 d_fprintf(stderr
, "ERROR: could not store int32 key '%s': %s\n",
82 keyname
, nt_errstr(status
));
89 static int dbwrap_tool_store_uint32(struct db_context
*db
,
94 uint32_t value
= (uint32_t)strtol(data
, NULL
, 10);
96 status
= dbwrap_trans_store_uint32(db
, keyname
, value
);
98 if (!NT_STATUS_IS_OK(status
)) {
100 "ERROR: could not store uint32 key '%s': %s\n",
101 keyname
, nt_errstr(status
));
108 static int dbwrap_tool_store_string(struct db_context
*db
,
114 status
= dbwrap_trans_store_bystring(db
, keyname
,
115 string_term_tdb_data(data
), TDB_REPLACE
);
117 if (!NT_STATUS_IS_OK(status
)) {
119 "ERROR: could not store string key '%s': %s\n",
120 keyname
, nt_errstr(status
));
127 static int dbwrap_tool_delete(struct db_context
*db
,
133 status
= dbwrap_trans_delete_bystring(db
, keyname
);
135 if (!NT_STATUS_IS_OK(status
)) {
136 d_fprintf(stderr
, "ERROR deleting record %s : %s\n",
137 keyname
, nt_errstr(status
));
144 static int delete_fn(struct db_record
*rec
, void *priv
)
146 dbwrap_record_delete(rec
);
151 * dbwrap_tool_erase: erase the whole data base
152 * the keyname argument is not used.
154 static int dbwrap_tool_erase(struct db_context
*db
,
160 status
= dbwrap_traverse(db
, delete_fn
, NULL
, NULL
);
162 if (!NT_STATUS_IS_OK(status
)) {
163 d_fprintf(stderr
, "ERROR erasing the database\n");
170 static int listkey_fn(struct db_record
*rec
, void *private_data
)
172 int length
= dbwrap_record_get_key(rec
).dsize
;
173 unsigned char *p
= (unsigned char *)dbwrap_record_get_key(rec
).dptr
;
176 if (isprint(*p
) && !strchr("\"\\", *p
)) {
179 d_printf("\\%02X", *p
);
189 static int dbwrap_tool_listkeys(struct db_context
*db
,
195 status
= dbwrap_traverse_read(db
, listkey_fn
, NULL
, NULL
);
197 if (!NT_STATUS_IS_OK(status
)) {
198 d_fprintf(stderr
, "ERROR listing db keys\n");
205 struct dbwrap_op_dispatch_table
{
208 int (*cmd
)(struct db_context
*db
,
213 struct dbwrap_op_dispatch_table dispatch_table
[] = {
214 { OP_FETCH
, TYPE_INT32
, dbwrap_tool_fetch_int32
},
215 { OP_FETCH
, TYPE_UINT32
, dbwrap_tool_fetch_uint32
},
216 { OP_STORE
, TYPE_INT32
, dbwrap_tool_store_int32
},
217 { OP_STORE
, TYPE_UINT32
, dbwrap_tool_store_uint32
},
218 { OP_STORE
, TYPE_STRING
, dbwrap_tool_store_string
},
219 { OP_DELETE
, TYPE_INT32
, dbwrap_tool_delete
},
220 { OP_ERASE
, TYPE_INT32
, dbwrap_tool_erase
},
221 { OP_LISTKEYS
, TYPE_INT32
, dbwrap_tool_listkeys
},
225 int main(int argc
, const char **argv
)
227 struct tevent_context
*evt_ctx
;
228 struct messaging_context
*msg_ctx
;
229 struct db_context
*db
;
236 const char *keyname
= "";
237 const char *keytype
= "int32";
239 const char *valuestr
= "0";
241 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
245 struct poptOption popt_options
[] = {
251 const char **extra_argv
;
256 lp_set_cmdline("log level", "0");
257 setup_logging(argv
[0], DEBUG_STDERR
);
259 pc
= poptGetContext(argv
[0], argc
, argv
, popt_options
, POPT_CONTEXT_KEEP_FIRST
);
261 while ((opt
= poptGetNextOpt(pc
)) != -1) {
264 fprintf(stderr
, "Invalid option %s: %s\n",
265 poptBadOption(pc
, 0), poptStrerror(opt
));
270 /* setup the remaining options for the main program to use */
271 extra_argv
= poptGetArgs(pc
);
274 while (extra_argv
[extra_argc
]) extra_argc
++;
277 lp_load_global(get_dyn_CONFIGFILE());
279 if ((extra_argc
< 2) || (extra_argc
> 5)) {
281 "USAGE: %s <database> <op> [<key> [<type> [<value>]]]\n"
282 " ops: fetch, store, delete, erase, listkeys\n"
283 " types: int32, uint32, string\n",
288 dbname
= extra_argv
[0];
289 opname
= extra_argv
[1];
291 if (strcmp(opname
, "store") == 0) {
292 if (extra_argc
!= 5) {
293 d_fprintf(stderr
, "ERROR: operation 'store' requires "
297 valuestr
= extra_argv
[4];
298 keytype
= extra_argv
[3];
299 keyname
= extra_argv
[2];
301 } else if (strcmp(opname
, "fetch") == 0) {
302 if (extra_argc
!= 4) {
303 d_fprintf(stderr
, "ERROR: operation 'fetch' requires "
304 "type but not value argument\n");
308 keytype
= extra_argv
[3];
309 keyname
= extra_argv
[2];
310 } else if (strcmp(opname
, "delete") == 0) {
311 if (extra_argc
!= 3) {
312 d_fprintf(stderr
, "ERROR: operation 'delete' does "
313 "not allow type nor value argument\n");
316 keyname
= extra_argv
[2];
318 } else if (strcmp(opname
, "erase") == 0) {
319 if (extra_argc
!= 2) {
320 d_fprintf(stderr
, "ERROR: operation 'erase' does "
321 "not take a key argument\n");
325 } else if (strcmp(opname
, "listkeys") == 0) {
326 if (extra_argc
!= 2) {
327 d_fprintf(stderr
, "ERROR: operation 'listkeys' does "
328 "not take a key argument\n");
334 "ERROR: invalid op '%s' specified\n"
335 " supported ops: fetch, store, delete\n",
340 if (strcmp(keytype
, "int32") == 0) {
342 } else if (strcmp(keytype
, "uint32") == 0) {
344 } else if (strcmp(keytype
, "string") == 0) {
347 d_fprintf(stderr
, "ERROR: invalid type '%s' specified.\n"
348 " supported types: int32, uint32, string\n",
353 evt_ctx
= tevent_context_init(mem_ctx
);
354 if (evt_ctx
== NULL
) {
355 d_fprintf(stderr
, "ERROR: could not init event context\n");
359 msg_ctx
= messaging_init(mem_ctx
, procid_self(), evt_ctx
);
360 if (msg_ctx
== NULL
) {
361 d_fprintf(stderr
, "ERROR: could not init messaging context\n");
365 db
= db_open(mem_ctx
, dbname
, 0, TDB_DEFAULT
, O_RDWR
| O_CREAT
, 0644);
367 d_fprintf(stderr
, "ERROR: could not open dbname\n");
371 for (count
= 0; dispatch_table
[count
].cmd
!= NULL
; count
++) {
372 if ((op
== dispatch_table
[count
].op
) &&
373 (type
== dispatch_table
[count
].type
))
375 ret
= dispatch_table
[count
].cmd(db
, keyname
, valuestr
);
381 TALLOC_FREE(mem_ctx
);