smbd/posix_acls.c: Use create_conn_struct(). Don't hand-create connection structs.
[Samba/vl.git] / source3 / utils / dbwrap_tool.c
blobeb28902f55023534ab44bc1164f0a85e81ed2378
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 status = dbwrap_trans_store_int32_bystring(db, keyname, value);
141 if (!NT_STATUS_IS_OK(status)) {
142 d_fprintf(stderr, "ERROR: could not store int32 key '%s': %s\n",
143 keyname, nt_errstr(status));
144 return -1;
147 return 0;
150 static int dbwrap_tool_store_uint32(struct db_context *db,
151 const char *keyname,
152 const char *data)
154 NTSTATUS status;
155 uint32_t value = (uint32_t)strtol(data, NULL, 10);
157 status = dbwrap_trans_store_uint32_bystring(db, keyname, value);
159 if (!NT_STATUS_IS_OK(status)) {
160 d_fprintf(stderr,
161 "ERROR: could not store uint32 key '%s': %s\n",
162 keyname, nt_errstr(status));
163 return -1;
166 return 0;
169 static int dbwrap_tool_store_string(struct db_context *db,
170 const char *keyname,
171 const char *data)
173 NTSTATUS status;
175 status = dbwrap_trans_store_bystring(db, keyname,
176 string_term_tdb_data(data), TDB_REPLACE);
178 if (!NT_STATUS_IS_OK(status)) {
179 d_fprintf(stderr,
180 "ERROR: could not store string key '%s': %s\n",
181 keyname, nt_errstr(status));
182 return -1;
185 return 0;
188 static int dbwrap_tool_store_hex(struct db_context *db,
189 const char *keyname,
190 const char *data)
192 NTSTATUS status;
193 DATA_BLOB datablob;
194 TDB_DATA tdbdata;
195 TALLOC_CTX *tmp_ctx = talloc_stackframe();
197 datablob = strhex_to_data_blob(tmp_ctx, data);
198 if(strlen(data) > 0 && datablob.length == 0) {
199 d_fprintf(stderr,
200 "ERROR: could not convert hex string to data blob\n"
201 " Not a valid hex string?\n");
202 talloc_free(tmp_ctx);
203 return -1;
206 tdbdata.dptr = (unsigned char *)datablob.data;
207 tdbdata.dsize = datablob.length;
209 status = dbwrap_trans_store_bystring(db, keyname,
210 tdbdata,
211 TDB_REPLACE);
212 if (!NT_STATUS_IS_OK(status)) {
213 d_fprintf(stderr,
214 "ERROR: could not store string key '%s': %s\n",
215 keyname, nt_errstr(status));
216 talloc_free(tmp_ctx);
217 return -1;
220 talloc_free(tmp_ctx);
221 return 0;
224 static int dbwrap_tool_delete(struct db_context *db,
225 const char *keyname,
226 const char *data)
228 NTSTATUS status;
230 status = dbwrap_trans_delete_bystring(db, keyname);
232 if (!NT_STATUS_IS_OK(status)) {
233 d_fprintf(stderr, "ERROR deleting record %s : %s\n",
234 keyname, nt_errstr(status));
235 return -1;
238 return 0;
241 static int delete_fn(struct db_record *rec, void *priv)
243 dbwrap_record_delete(rec);
244 return 0;
248 * dbwrap_tool_erase: erase the whole data base
249 * the keyname argument is not used.
251 static int dbwrap_tool_erase(struct db_context *db,
252 const char *keyname,
253 const char *data)
255 NTSTATUS status;
257 status = dbwrap_traverse(db, delete_fn, NULL, NULL);
259 if (!NT_STATUS_IS_OK(status)) {
260 d_fprintf(stderr, "ERROR erasing the database\n");
261 return -1;
264 return 0;
267 static int listkey_fn(struct db_record *rec, void *private_data)
269 int length = dbwrap_record_get_key(rec).dsize;
270 unsigned char *p = (unsigned char *)dbwrap_record_get_key(rec).dptr;
272 while (length--) {
273 if (isprint(*p) && !strchr("\"\\", *p)) {
274 d_printf("%c", *p);
275 } else {
276 d_printf("\\%02X", *p);
278 p++;
281 d_printf("\n");
283 return 0;
286 static int dbwrap_tool_listkeys(struct db_context *db,
287 const char *keyname,
288 const char *data)
290 NTSTATUS status;
292 status = dbwrap_traverse_read(db, listkey_fn, NULL, NULL);
294 if (!NT_STATUS_IS_OK(status)) {
295 d_fprintf(stderr, "ERROR listing db keys\n");
296 return -1;
299 return 0;
302 static int dbwrap_tool_listwatchers_cb(const uint8_t *db_id, size_t db_id_len,
303 const TDB_DATA key,
304 const struct server_id *watchers,
305 size_t num_watchers,
306 void *private_data)
308 uint32_t i;
309 dump_data_file(db_id, db_id_len, false, stdout);
310 dump_data_file(key.dptr, key.dsize, false, stdout);
312 for (i=0; i<num_watchers; i++) {
313 char *str = server_id_str(talloc_tos(), &watchers[i]);
314 printf("%s\n", str);
315 TALLOC_FREE(str);
317 printf("\n");
318 return 0;
322 static int dbwrap_tool_listwatchers(struct db_context *db,
323 const char *keyname,
324 const char *data)
326 dbwrap_watchers_traverse_read(dbwrap_tool_listwatchers_cb, NULL);
327 return 0;
330 struct dbwrap_op_dispatch_table {
331 enum dbwrap_op op;
332 enum dbwrap_type type;
333 int (*cmd)(struct db_context *db,
334 const char *keyname,
335 const char *data);
338 struct dbwrap_op_dispatch_table dispatch_table[] = {
339 { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
340 { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
341 { OP_FETCH, TYPE_STRING, dbwrap_tool_fetch_string },
342 { OP_FETCH, TYPE_HEX, dbwrap_tool_fetch_hex },
343 { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
344 { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
345 { OP_STORE, TYPE_STRING, dbwrap_tool_store_string },
346 { OP_STORE, TYPE_HEX, dbwrap_tool_store_hex },
347 { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
348 { OP_ERASE, TYPE_INT32, dbwrap_tool_erase },
349 { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
350 { OP_LISTWATCHERS, TYPE_NONE, dbwrap_tool_listwatchers },
351 { 0, 0, NULL },
354 int main(int argc, const char **argv)
356 struct tevent_context *evt_ctx;
357 struct messaging_context *msg_ctx;
358 struct db_context *db;
360 uint16_t count;
362 const char *dbname;
363 const char *opname;
364 enum dbwrap_op op;
365 const char *keyname = "";
366 const char *keytype = "int32";
367 enum dbwrap_type type;
368 const char *valuestr = "0";
370 TALLOC_CTX *mem_ctx = talloc_stackframe();
372 int ret = 1;
374 struct poptOption popt_options[] = {
375 POPT_AUTOHELP
376 POPT_COMMON_SAMBA
377 POPT_TABLEEND
379 int opt;
380 const char **extra_argv;
381 int extra_argc = 0;
382 poptContext pc;
384 load_case_tables();
385 lp_set_cmdline("log level", "0");
386 setup_logging(argv[0], DEBUG_STDERR);
388 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
390 while ((opt = poptGetNextOpt(pc)) != -1) {
391 switch (opt) {
392 default:
393 fprintf(stderr, "Invalid option %s: %s\n",
394 poptBadOption(pc, 0), poptStrerror(opt));
395 goto done;
399 /* setup the remaining options for the main program to use */
400 extra_argv = poptGetArgs(pc);
401 if (extra_argv) {
402 extra_argv++;
403 while (extra_argv[extra_argc]) extra_argc++;
406 lp_load_global(get_dyn_CONFIGFILE());
408 if ((extra_argc < 2) || (extra_argc > 5)) {
409 d_fprintf(stderr,
410 "USAGE: %s <database> <op> [<key> [<type> [<value>]]]\n"
411 " ops: fetch, store, delete, erase, listkeys, "
412 "listwatchers\n"
413 " types: int32, uint32, string, hex\n",
414 argv[0]);
415 goto done;
418 dbname = extra_argv[0];
419 opname = extra_argv[1];
421 if (strcmp(opname, "store") == 0) {
422 if (extra_argc != 5) {
423 d_fprintf(stderr, "ERROR: operation 'store' requires "
424 "value argument\n");
425 goto done;
427 valuestr = extra_argv[4];
428 keytype = extra_argv[3];
429 keyname = extra_argv[2];
430 op = OP_STORE;
431 } else if (strcmp(opname, "fetch") == 0) {
432 if (extra_argc != 4) {
433 d_fprintf(stderr, "ERROR: operation 'fetch' requires "
434 "type but not value argument\n");
435 goto done;
437 op = OP_FETCH;
438 keytype = extra_argv[3];
439 keyname = extra_argv[2];
440 } else if (strcmp(opname, "delete") == 0) {
441 if (extra_argc != 3) {
442 d_fprintf(stderr, "ERROR: operation 'delete' does "
443 "not allow type nor value argument\n");
444 goto done;
446 keyname = extra_argv[2];
447 op = OP_DELETE;
448 } else if (strcmp(opname, "erase") == 0) {
449 if (extra_argc != 2) {
450 d_fprintf(stderr, "ERROR: operation 'erase' does "
451 "not take a key argument\n");
452 goto done;
454 op = OP_ERASE;
455 } else if (strcmp(opname, "listkeys") == 0) {
456 if (extra_argc != 2) {
457 d_fprintf(stderr, "ERROR: operation 'listkeys' does "
458 "not take a key argument\n");
459 goto done;
461 op = OP_LISTKEYS;
462 } else if (strcmp(opname, "listwatchers") == 0) {
463 if (extra_argc != 2) {
464 d_fprintf(stderr, "ERROR: operation 'listwatchers' "
465 "does not take an argument\n");
466 goto done;
468 op = OP_LISTWATCHERS;
469 keytype = "none";
470 } else {
471 d_fprintf(stderr,
472 "ERROR: invalid op '%s' specified\n"
473 " supported ops: fetch, store, delete\n",
474 opname);
475 goto done;
478 if (strcmp(keytype, "int32") == 0) {
479 type = TYPE_INT32;
480 } else if (strcmp(keytype, "uint32") == 0) {
481 type = TYPE_UINT32;
482 } else if (strcmp(keytype, "string") == 0) {
483 type = TYPE_STRING;
484 } else if (strcmp(keytype, "hex") == 0) {
485 type = TYPE_HEX;
486 } else if (strcmp(keytype, "none") == 0) {
487 type = TYPE_NONE;
488 } else {
489 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
490 " supported types: int32, uint32, "
491 "string, hex, none\n",
492 keytype);
493 goto done;
496 evt_ctx = tevent_context_init(mem_ctx);
497 if (evt_ctx == NULL) {
498 d_fprintf(stderr, "ERROR: could not init event context\n");
499 goto done;
502 msg_ctx = messaging_init(mem_ctx, evt_ctx);
503 if (msg_ctx == NULL) {
504 d_fprintf(stderr, "ERROR: could not init messaging context\n");
505 goto done;
508 switch (op) {
509 case OP_FETCH:
510 case OP_STORE:
511 case OP_DELETE:
512 case OP_ERASE:
513 case OP_LISTKEYS:
514 db = db_open(mem_ctx, dbname, 0, TDB_DEFAULT, O_RDWR | O_CREAT,
515 0644, DBWRAP_LOCK_ORDER_1);
516 if (db == NULL) {
517 d_fprintf(stderr, "ERROR: could not open dbname\n");
518 goto done;
520 break;
521 default:
522 db = NULL;
523 break;
526 for (count = 0; dispatch_table[count].cmd != NULL; count++) {
527 if ((op == dispatch_table[count].op) &&
528 (type == dispatch_table[count].type))
530 ret = dispatch_table[count].cmd(db, keyname, valuestr);
531 break;
535 done:
536 TALLOC_FREE(mem_ctx);
537 return ret;