s3-util: dbwrap_tool: add store hex function
[Samba/gebeck_regimport.git] / source3 / utils / dbwrap_tool.c
blobdd744d00886c7fd5037e0ea9a845f483fe43c61a
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 "messages.h"
29 #include "util_tdb.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, TYPE_HEX } dbwrap_type;
35 static int dbwrap_tool_fetch_int32(struct db_context *db,
36 const char *keyname,
37 const char *data)
39 int32_t value;
40 NTSTATUS status;
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));
46 return -1;
48 d_printf("%d\n", value);
50 return 0;
53 static int dbwrap_tool_fetch_uint32(struct db_context *db,
54 const char *keyname,
55 const char *data)
57 uint32_t value;
58 NTSTATUS ret;
60 ret = dbwrap_fetch_uint32(db, keyname, &value);
61 if (NT_STATUS_IS_OK(ret)) {
62 d_printf("%u\n", value);
63 return 0;
64 } else {
65 d_fprintf(stderr, "ERROR: could not fetch uint32 key '%s': "
66 "%s\n", nt_errstr(ret), keyname);
67 return -1;
71 static int dbwrap_tool_store_int32(struct db_context *db,
72 const char *keyname,
73 const char *data)
75 NTSTATUS status;
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));
83 return -1;
86 return 0;
89 static int dbwrap_tool_store_uint32(struct db_context *db,
90 const char *keyname,
91 const char *data)
93 NTSTATUS status;
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)) {
99 d_fprintf(stderr,
100 "ERROR: could not store uint32 key '%s': %s\n",
101 keyname, nt_errstr(status));
102 return -1;
105 return 0;
108 static int dbwrap_tool_store_string(struct db_context *db,
109 const char *keyname,
110 const char *data)
112 NTSTATUS status;
114 status = dbwrap_trans_store_bystring(db, keyname,
115 string_term_tdb_data(data), TDB_REPLACE);
117 if (!NT_STATUS_IS_OK(status)) {
118 d_fprintf(stderr,
119 "ERROR: could not store string key '%s': %s\n",
120 keyname, nt_errstr(status));
121 return -1;
124 return 0;
127 static int dbwrap_tool_store_hex(struct db_context *db,
128 const char *keyname,
129 const char *data)
131 NTSTATUS status;
132 DATA_BLOB datablob;
133 TDB_DATA tdbdata;
134 TALLOC_CTX *tmp_ctx = talloc_stackframe();
136 datablob = strhex_to_data_blob(tmp_ctx, data);
137 if(strlen(data) > 0 && datablob.length == 0) {
138 d_fprintf(stderr,
139 "ERROR: could not convert hex string to data blob\n"
140 " Not a valid hex string?\n");
141 talloc_free(tmp_ctx);
142 return -1;
145 tdbdata.dptr = (unsigned char *)datablob.data;
146 tdbdata.dsize = datablob.length;
148 status = dbwrap_trans_store_bystring(db, keyname,
149 tdbdata,
150 TDB_REPLACE);
151 if (!NT_STATUS_IS_OK(status)) {
152 d_fprintf(stderr,
153 "ERROR: could not store string key '%s': %s\n",
154 keyname, nt_errstr(status));
155 talloc_free(tmp_ctx);
156 return -1;
159 talloc_free(tmp_ctx);
160 return 0;
163 static int dbwrap_tool_delete(struct db_context *db,
164 const char *keyname,
165 const char *data)
167 NTSTATUS status;
169 status = dbwrap_trans_delete_bystring(db, keyname);
171 if (!NT_STATUS_IS_OK(status)) {
172 d_fprintf(stderr, "ERROR deleting record %s : %s\n",
173 keyname, nt_errstr(status));
174 return -1;
177 return 0;
180 static int delete_fn(struct db_record *rec, void *priv)
182 dbwrap_record_delete(rec);
183 return 0;
187 * dbwrap_tool_erase: erase the whole data base
188 * the keyname argument is not used.
190 static int dbwrap_tool_erase(struct db_context *db,
191 const char *keyname,
192 const char *data)
194 NTSTATUS status;
196 status = dbwrap_traverse(db, delete_fn, NULL, NULL);
198 if (!NT_STATUS_IS_OK(status)) {
199 d_fprintf(stderr, "ERROR erasing the database\n");
200 return -1;
203 return 0;
206 static int listkey_fn(struct db_record *rec, void *private_data)
208 int length = dbwrap_record_get_key(rec).dsize;
209 unsigned char *p = (unsigned char *)dbwrap_record_get_key(rec).dptr;
211 while (length--) {
212 if (isprint(*p) && !strchr("\"\\", *p)) {
213 d_printf("%c", *p);
214 } else {
215 d_printf("\\%02X", *p);
217 p++;
220 d_printf("\n");
222 return 0;
225 static int dbwrap_tool_listkeys(struct db_context *db,
226 const char *keyname,
227 const char *data)
229 NTSTATUS status;
231 status = dbwrap_traverse_read(db, listkey_fn, NULL, NULL);
233 if (!NT_STATUS_IS_OK(status)) {
234 d_fprintf(stderr, "ERROR listing db keys\n");
235 return -1;
238 return 0;
241 struct dbwrap_op_dispatch_table {
242 dbwrap_op op;
243 dbwrap_type type;
244 int (*cmd)(struct db_context *db,
245 const char *keyname,
246 const char *data);
249 struct dbwrap_op_dispatch_table dispatch_table[] = {
250 { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
251 { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
252 { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
253 { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
254 { OP_STORE, TYPE_STRING, dbwrap_tool_store_string },
255 { OP_STORE, TYPE_HEX, dbwrap_tool_store_hex },
256 { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
257 { OP_ERASE, TYPE_INT32, dbwrap_tool_erase },
258 { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
259 { 0, 0, NULL },
262 int main(int argc, const char **argv)
264 struct tevent_context *evt_ctx;
265 struct messaging_context *msg_ctx;
266 struct db_context *db;
268 uint16_t count;
270 const char *dbname;
271 const char *opname;
272 dbwrap_op op;
273 const char *keyname = "";
274 const char *keytype = "int32";
275 dbwrap_type type;
276 const char *valuestr = "0";
278 TALLOC_CTX *mem_ctx = talloc_stackframe();
280 int ret = 1;
282 struct poptOption popt_options[] = {
283 POPT_AUTOHELP
284 POPT_COMMON_SAMBA
285 POPT_TABLEEND
287 int opt;
288 const char **extra_argv;
289 int extra_argc = 0;
290 poptContext pc;
292 load_case_tables();
293 lp_set_cmdline("log level", "0");
294 setup_logging(argv[0], DEBUG_STDERR);
296 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
298 while ((opt = poptGetNextOpt(pc)) != -1) {
299 switch (opt) {
300 default:
301 fprintf(stderr, "Invalid option %s: %s\n",
302 poptBadOption(pc, 0), poptStrerror(opt));
303 goto done;
307 /* setup the remaining options for the main program to use */
308 extra_argv = poptGetArgs(pc);
309 if (extra_argv) {
310 extra_argv++;
311 while (extra_argv[extra_argc]) extra_argc++;
314 lp_load_global(get_dyn_CONFIGFILE());
316 if ((extra_argc < 2) || (extra_argc > 5)) {
317 d_fprintf(stderr,
318 "USAGE: %s <database> <op> [<key> [<type> [<value>]]]\n"
319 " ops: fetch, store, delete, erase, listkeys\n"
320 " types: int32, uint32, string, hex\n",
321 argv[0]);
322 goto done;
325 dbname = extra_argv[0];
326 opname = extra_argv[1];
328 if (strcmp(opname, "store") == 0) {
329 if (extra_argc != 5) {
330 d_fprintf(stderr, "ERROR: operation 'store' requires "
331 "value argument\n");
332 goto done;
334 valuestr = extra_argv[4];
335 keytype = extra_argv[3];
336 keyname = extra_argv[2];
337 op = OP_STORE;
338 } else if (strcmp(opname, "fetch") == 0) {
339 if (extra_argc != 4) {
340 d_fprintf(stderr, "ERROR: operation 'fetch' requires "
341 "type but not value argument\n");
342 goto done;
344 op = OP_FETCH;
345 keytype = extra_argv[3];
346 keyname = extra_argv[2];
347 } else if (strcmp(opname, "delete") == 0) {
348 if (extra_argc != 3) {
349 d_fprintf(stderr, "ERROR: operation 'delete' does "
350 "not allow type nor value argument\n");
351 goto done;
353 keyname = extra_argv[2];
354 op = OP_DELETE;
355 } else if (strcmp(opname, "erase") == 0) {
356 if (extra_argc != 2) {
357 d_fprintf(stderr, "ERROR: operation 'erase' does "
358 "not take a key argument\n");
359 goto done;
361 op = OP_ERASE;
362 } else if (strcmp(opname, "listkeys") == 0) {
363 if (extra_argc != 2) {
364 d_fprintf(stderr, "ERROR: operation 'listkeys' does "
365 "not take a key argument\n");
366 goto done;
368 op = OP_LISTKEYS;
369 } else {
370 d_fprintf(stderr,
371 "ERROR: invalid op '%s' specified\n"
372 " supported ops: fetch, store, delete\n",
373 opname);
374 goto done;
377 if (strcmp(keytype, "int32") == 0) {
378 type = TYPE_INT32;
379 } else if (strcmp(keytype, "uint32") == 0) {
380 type = TYPE_UINT32;
381 } else if (strcmp(keytype, "string") == 0) {
382 type = TYPE_STRING;
383 } else if (strcmp(keytype, "hex") == 0) {
384 type = TYPE_HEX;
385 } else {
386 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
387 " supported types: int32, uint32, "
388 "string, hex\n",
389 keytype);
390 goto done;
393 evt_ctx = tevent_context_init(mem_ctx);
394 if (evt_ctx == NULL) {
395 d_fprintf(stderr, "ERROR: could not init event context\n");
396 goto done;
399 msg_ctx = messaging_init(mem_ctx, procid_self(), evt_ctx);
400 if (msg_ctx == NULL) {
401 d_fprintf(stderr, "ERROR: could not init messaging context\n");
402 goto done;
405 db = db_open(mem_ctx, dbname, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
406 if (db == NULL) {
407 d_fprintf(stderr, "ERROR: could not open dbname\n");
408 goto done;
411 for (count = 0; dispatch_table[count].cmd != NULL; count++) {
412 if ((op == dispatch_table[count].op) &&
413 (type == dispatch_table[count].type))
415 ret = dispatch_table[count].cmd(db, keyname, valuestr);
416 break;
420 done:
421 TALLOC_FREE(mem_ctx);
422 return ret;