1 | /*
|
---|
2 | Samba Unix/Linux CIFS implementation
|
---|
3 |
|
---|
4 | low level TDB/CTDB tool using the dbwrap interface
|
---|
5 |
|
---|
6 | Copyright (C) 2009 Michael Adam <obnox@samba.org>
|
---|
7 | Copyright (C) 2011 Bjoern Baumbach <bb@sernet.de>
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
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.
|
---|
18 |
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
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"
|
---|
31 |
|
---|
32 | enum dbwrap_op { OP_FETCH, OP_STORE, OP_DELETE, OP_ERASE, OP_LISTKEYS,
|
---|
33 | OP_LISTWATCHERS, OP_EXISTS };
|
---|
34 |
|
---|
35 | enum dbwrap_type { TYPE_INT32, TYPE_UINT32, TYPE_STRING, TYPE_HEX, TYPE_NONE };
|
---|
36 |
|
---|
37 | static int dbwrap_tool_fetch_int32(struct db_context *db,
|
---|
38 | const char *keyname,
|
---|
39 | const char *data)
|
---|
40 | {
|
---|
41 | int32_t value;
|
---|
42 | NTSTATUS status;
|
---|
43 |
|
---|
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;
|
---|
49 | }
|
---|
50 | d_printf("%d\n", value);
|
---|
51 |
|
---|
52 | return 0;
|
---|
53 | }
|
---|
54 |
|
---|
55 | static int dbwrap_tool_fetch_uint32(struct db_context *db,
|
---|
56 | const char *keyname,
|
---|
57 | const char *data)
|
---|
58 | {
|
---|
59 | uint32_t value;
|
---|
60 | NTSTATUS ret;
|
---|
61 |
|
---|
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;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | static int dbwrap_tool_fetch_string(struct db_context *db,
|
---|
74 | const char *keyname,
|
---|
75 | const char *data)
|
---|
76 | {
|
---|
77 | TDB_DATA tdbdata;
|
---|
78 | NTSTATUS status;
|
---|
79 | TALLOC_CTX *tmp_ctx = talloc_stackframe();
|
---|
80 | int ret;
|
---|
81 |
|
---|
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;
|
---|
91 | }
|
---|
92 |
|
---|
93 | talloc_free(tmp_ctx);
|
---|
94 | return ret;
|
---|
95 | }
|
---|
96 |
|
---|
97 | static int dbwrap_tool_fetch_hex(struct db_context *db,
|
---|
98 | const char *keyname,
|
---|
99 | const char *data)
|
---|
100 | {
|
---|
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;
|
---|
107 |
|
---|
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;
|
---|
112 |
|
---|
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;
|
---|
121 | }
|
---|
122 | } else {
|
---|
123 | d_fprintf(stderr, "ERROR: could not fetch hex key '%s': "
|
---|
124 | "%s\n", nt_errstr(status), keyname);
|
---|
125 | ret = -1;
|
---|
126 | }
|
---|
127 |
|
---|
128 | talloc_free(tmp_ctx);
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 |
|
---|
132 | static int dbwrap_tool_store_int32(struct db_context *db,
|
---|
133 | const char *keyname,
|
---|
134 | const char *data)
|
---|
135 | {
|
---|
136 | NTSTATUS status;
|
---|
137 | int32_t value = (int32_t)strtol(data, NULL, 10);
|
---|
138 |
|
---|
139 | if (dbwrap_is_persistent(db)) {
|
---|
140 | status = dbwrap_trans_store_int32_bystring(db, keyname, value);
|
---|
141 | } else {
|
---|
142 | status = dbwrap_store_int32_bystring(db, keyname, value);
|
---|
143 | }
|
---|
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));
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return 0;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static int dbwrap_tool_store_uint32(struct db_context *db,
|
---|
154 | const char *keyname,
|
---|
155 | const char *data)
|
---|
156 | {
|
---|
157 | NTSTATUS status;
|
---|
158 | uint32_t value = (uint32_t)strtol(data, NULL, 10);
|
---|
159 |
|
---|
160 | if (dbwrap_is_persistent(db)) {
|
---|
161 | status = dbwrap_trans_store_uint32_bystring(db, keyname, value);
|
---|
162 | } else {
|
---|
163 | status = dbwrap_store_uint32_bystring(db, keyname, value);
|
---|
164 | }
|
---|
165 | if (!NT_STATUS_IS_OK(status)) {
|
---|
166 | d_fprintf(stderr,
|
---|
167 | "ERROR: could not store uint32 key '%s': %s\n",
|
---|
168 | keyname, nt_errstr(status));
|
---|
169 | return -1;
|
---|
170 | }
|
---|
171 |
|
---|
172 | return 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 | static int dbwrap_tool_store_string(struct db_context *db,
|
---|
176 | const char *keyname,
|
---|
177 | const char *data)
|
---|
178 | {
|
---|
179 | NTSTATUS status;
|
---|
180 | TDB_DATA tdbdata;
|
---|
181 |
|
---|
182 | tdbdata = string_term_tdb_data(data);
|
---|
183 |
|
---|
184 | if (dbwrap_is_persistent(db)) {
|
---|
185 | status = dbwrap_trans_store_bystring(db, keyname,
|
---|
186 | tdbdata,
|
---|
187 | TDB_REPLACE);
|
---|
188 | } else {
|
---|
189 | status = dbwrap_store_bystring(db, keyname,
|
---|
190 | tdbdata,
|
---|
191 | TDB_REPLACE);
|
---|
192 | }
|
---|
193 | if (!NT_STATUS_IS_OK(status)) {
|
---|
194 | d_fprintf(stderr,
|
---|
195 | "ERROR: could not store string key '%s': %s\n",
|
---|
196 | keyname, nt_errstr(status));
|
---|
197 | return -1;
|
---|
198 | }
|
---|
199 |
|
---|
200 | return 0;
|
---|
201 | }
|
---|
202 |
|
---|
203 | static int dbwrap_tool_store_hex(struct db_context *db,
|
---|
204 | const char *keyname,
|
---|
205 | const char *data)
|
---|
206 | {
|
---|
207 | NTSTATUS status;
|
---|
208 | DATA_BLOB datablob;
|
---|
209 | TDB_DATA tdbdata;
|
---|
210 | TALLOC_CTX *tmp_ctx = talloc_stackframe();
|
---|
211 |
|
---|
212 | datablob = strhex_to_data_blob(tmp_ctx, data);
|
---|
213 | if(strlen(data) > 0 && datablob.length == 0) {
|
---|
214 | d_fprintf(stderr,
|
---|
215 | "ERROR: could not convert hex string to data blob\n"
|
---|
216 | " Not a valid hex string?\n");
|
---|
217 | talloc_free(tmp_ctx);
|
---|
218 | return -1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | tdbdata.dptr = (unsigned char *)datablob.data;
|
---|
222 | tdbdata.dsize = datablob.length;
|
---|
223 |
|
---|
224 | if (dbwrap_is_persistent(db)) {
|
---|
225 | status = dbwrap_trans_store_bystring(db, keyname,
|
---|
226 | tdbdata,
|
---|
227 | TDB_REPLACE);
|
---|
228 | } else {
|
---|
229 | status = dbwrap_store_bystring(db, keyname,
|
---|
230 | tdbdata,
|
---|
231 | TDB_REPLACE);
|
---|
232 | }
|
---|
233 | if (!NT_STATUS_IS_OK(status)) {
|
---|
234 | d_fprintf(stderr,
|
---|
235 | "ERROR: could not store string key '%s': %s\n",
|
---|
236 | keyname, nt_errstr(status));
|
---|
237 | talloc_free(tmp_ctx);
|
---|
238 | return -1;
|
---|
239 | }
|
---|
240 |
|
---|
241 | talloc_free(tmp_ctx);
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|
245 | static int dbwrap_tool_delete(struct db_context *db,
|
---|
246 | const char *keyname,
|
---|
247 | const char *data)
|
---|
248 | {
|
---|
249 | NTSTATUS status;
|
---|
250 |
|
---|
251 | if (dbwrap_is_persistent(db)) {
|
---|
252 | status = dbwrap_trans_delete_bystring(db, keyname);
|
---|
253 | } else {
|
---|
254 | status = dbwrap_delete_bystring(db, keyname);
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (!NT_STATUS_IS_OK(status)) {
|
---|
258 | d_fprintf(stderr, "ERROR deleting record %s : %s\n",
|
---|
259 | keyname, nt_errstr(status));
|
---|
260 | return -1;
|
---|
261 | }
|
---|
262 |
|
---|
263 | return 0;
|
---|
264 | }
|
---|
265 |
|
---|
266 | static int dbwrap_tool_exists(struct db_context *db,
|
---|
267 | const char *keyname,
|
---|
268 | const char *data)
|
---|
269 | {
|
---|
270 | bool result;
|
---|
271 |
|
---|
272 | result = dbwrap_exists(db, string_term_tdb_data(keyname));
|
---|
273 |
|
---|
274 | if (result) {
|
---|
275 | d_fprintf(stdout, "Key %s exists\n", keyname);
|
---|
276 | } else {
|
---|
277 | d_fprintf(stdout, "Key %s does not exist\n", keyname);
|
---|
278 | }
|
---|
279 |
|
---|
280 | return (result)?0:1;
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | static int delete_fn(struct db_record *rec, void *priv)
|
---|
285 | {
|
---|
286 | dbwrap_record_delete(rec);
|
---|
287 | return 0;
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * dbwrap_tool_erase: erase the whole data base
|
---|
292 | * the keyname argument is not used.
|
---|
293 | */
|
---|
294 | static int dbwrap_tool_erase(struct db_context *db,
|
---|
295 | const char *keyname,
|
---|
296 | const char *data)
|
---|
297 | {
|
---|
298 | NTSTATUS status;
|
---|
299 |
|
---|
300 | status = dbwrap_traverse(db, delete_fn, NULL, NULL);
|
---|
301 |
|
---|
302 | if (!NT_STATUS_IS_OK(status)) {
|
---|
303 | d_fprintf(stderr, "ERROR erasing the database\n");
|
---|
304 | return -1;
|
---|
305 | }
|
---|
306 |
|
---|
307 | return 0;
|
---|
308 | }
|
---|
309 |
|
---|
310 | static int listkey_fn(struct db_record *rec, void *private_data)
|
---|
311 | {
|
---|
312 | int length = dbwrap_record_get_key(rec).dsize;
|
---|
313 | unsigned char *p = (unsigned char *)dbwrap_record_get_key(rec).dptr;
|
---|
314 |
|
---|
315 | while (length--) {
|
---|
316 | if (isprint(*p) && !strchr("\"\\", *p)) {
|
---|
317 | d_printf("%c", *p);
|
---|
318 | } else {
|
---|
319 | d_printf("\\%02X", *p);
|
---|
320 | }
|
---|
321 | p++;
|
---|
322 | }
|
---|
323 |
|
---|
324 | d_printf("\n");
|
---|
325 |
|
---|
326 | return 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | static int dbwrap_tool_listkeys(struct db_context *db,
|
---|
330 | const char *keyname,
|
---|
331 | const char *data)
|
---|
332 | {
|
---|
333 | NTSTATUS status;
|
---|
334 |
|
---|
335 | status = dbwrap_traverse_read(db, listkey_fn, NULL, NULL);
|
---|
336 |
|
---|
337 | if (!NT_STATUS_IS_OK(status)) {
|
---|
338 | d_fprintf(stderr, "ERROR listing db keys\n");
|
---|
339 | return -1;
|
---|
340 | }
|
---|
341 |
|
---|
342 | return 0;
|
---|
343 | }
|
---|
344 |
|
---|
345 | static int dbwrap_tool_listwatchers_cb(const uint8_t *db_id, size_t db_id_len,
|
---|
346 | const TDB_DATA key,
|
---|
347 | const struct server_id *watchers,
|
---|
348 | size_t num_watchers,
|
---|
349 | void *private_data)
|
---|
350 | {
|
---|
351 | uint32_t i;
|
---|
352 | dump_data_file(db_id, db_id_len, false, stdout);
|
---|
353 | dump_data_file(key.dptr, key.dsize, false, stdout);
|
---|
354 |
|
---|
355 | for (i=0; i<num_watchers; i++) {
|
---|
356 | struct server_id_buf idbuf;
|
---|
357 | printf("%s\n", server_id_str_buf(watchers[i], &idbuf));
|
---|
358 | }
|
---|
359 | printf("\n");
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | static int dbwrap_tool_listwatchers(struct db_context *db,
|
---|
365 | const char *keyname,
|
---|
366 | const char *data)
|
---|
367 | {
|
---|
368 | dbwrap_watchers_traverse_read(dbwrap_tool_listwatchers_cb, NULL);
|
---|
369 | return 0;
|
---|
370 | }
|
---|
371 |
|
---|
372 | struct dbwrap_op_dispatch_table {
|
---|
373 | enum dbwrap_op op;
|
---|
374 | enum dbwrap_type type;
|
---|
375 | int (*cmd)(struct db_context *db,
|
---|
376 | const char *keyname,
|
---|
377 | const char *data);
|
---|
378 | };
|
---|
379 |
|
---|
380 | struct dbwrap_op_dispatch_table dispatch_table[] = {
|
---|
381 | { OP_FETCH, TYPE_INT32, dbwrap_tool_fetch_int32 },
|
---|
382 | { OP_FETCH, TYPE_UINT32, dbwrap_tool_fetch_uint32 },
|
---|
383 | { OP_FETCH, TYPE_STRING, dbwrap_tool_fetch_string },
|
---|
384 | { OP_FETCH, TYPE_HEX, dbwrap_tool_fetch_hex },
|
---|
385 | { OP_STORE, TYPE_INT32, dbwrap_tool_store_int32 },
|
---|
386 | { OP_STORE, TYPE_UINT32, dbwrap_tool_store_uint32 },
|
---|
387 | { OP_STORE, TYPE_STRING, dbwrap_tool_store_string },
|
---|
388 | { OP_STORE, TYPE_HEX, dbwrap_tool_store_hex },
|
---|
389 | { OP_DELETE, TYPE_INT32, dbwrap_tool_delete },
|
---|
390 | { OP_ERASE, TYPE_INT32, dbwrap_tool_erase },
|
---|
391 | { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
|
---|
392 | { OP_LISTWATCHERS, TYPE_NONE, dbwrap_tool_listwatchers },
|
---|
393 | { OP_EXISTS, TYPE_STRING, dbwrap_tool_exists },
|
---|
394 | { 0, 0, NULL },
|
---|
395 | };
|
---|
396 |
|
---|
397 | int main(int argc, const char **argv)
|
---|
398 | {
|
---|
399 | struct tevent_context *evt_ctx;
|
---|
400 | struct messaging_context *msg_ctx;
|
---|
401 | struct db_context *db;
|
---|
402 |
|
---|
403 | uint16_t count;
|
---|
404 |
|
---|
405 | const char *dbname;
|
---|
406 | const char *opname;
|
---|
407 | enum dbwrap_op op;
|
---|
408 | const char *keyname = "";
|
---|
409 | const char *keytype = "int32";
|
---|
410 | enum dbwrap_type type;
|
---|
411 | const char *valuestr = "0";
|
---|
412 | int persistent = 0;
|
---|
413 | int non_persistent = 0;
|
---|
414 | int tdb_flags = TDB_DEFAULT;
|
---|
415 |
|
---|
416 | TALLOC_CTX *mem_ctx = talloc_stackframe();
|
---|
417 |
|
---|
418 | int ret = 1;
|
---|
419 |
|
---|
420 | struct poptOption popt_options[] = {
|
---|
421 | POPT_AUTOHELP
|
---|
422 | POPT_COMMON_SAMBA
|
---|
423 | { "non-persistent", 0, POPT_ARG_NONE, &non_persistent, 0,
|
---|
424 | "treat the database as non-persistent "
|
---|
425 | "(CAVEAT: This mode might wipe your database!)",
|
---|
426 | NULL },
|
---|
427 | { "persistent", 0, POPT_ARG_NONE, &persistent, 0,
|
---|
428 | "treat the database as persistent",
|
---|
429 | NULL },
|
---|
430 | POPT_TABLEEND
|
---|
431 | };
|
---|
432 | int opt;
|
---|
433 | const char **extra_argv;
|
---|
434 | int extra_argc = 0;
|
---|
435 | poptContext pc;
|
---|
436 |
|
---|
437 | smb_init_locale();
|
---|
438 | lp_set_cmdline("log level", "0");
|
---|
439 | setup_logging(argv[0], DEBUG_STDERR);
|
---|
440 |
|
---|
441 | pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
|
---|
442 |
|
---|
443 | while ((opt = poptGetNextOpt(pc)) != -1) {
|
---|
444 | switch (opt) {
|
---|
445 | default:
|
---|
446 | fprintf(stderr, "Invalid option %s: %s\n",
|
---|
447 | poptBadOption(pc, 0), poptStrerror(opt));
|
---|
448 | goto done;
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | /* setup the remaining options for the main program to use */
|
---|
453 | extra_argv = poptGetArgs(pc);
|
---|
454 | if (extra_argv) {
|
---|
455 | extra_argv++;
|
---|
456 | while (extra_argv[extra_argc]) extra_argc++;
|
---|
457 | }
|
---|
458 |
|
---|
459 | lp_load_global(get_dyn_CONFIGFILE());
|
---|
460 |
|
---|
461 | if ((extra_argc < 2) || (extra_argc > 5)) {
|
---|
462 | d_fprintf(stderr,
|
---|
463 | "USAGE: %s [options] <database> <op> [<key> [<type> "
|
---|
464 | "[<value>]]]\n"
|
---|
465 | " ops: fetch, store, delete, exists, "
|
---|
466 | "erase, listkeys, listwatchers\n"
|
---|
467 | " types: int32, uint32, string, hex\n",
|
---|
468 | argv[0]);
|
---|
469 | goto done;
|
---|
470 | }
|
---|
471 |
|
---|
472 | if ((persistent == 0 && non_persistent == 0) ||
|
---|
473 | (persistent == 1 && non_persistent == 1))
|
---|
474 | {
|
---|
475 | d_fprintf(stderr, "ERROR: you must specify exactly one "
|
---|
476 | "of --persistent and --non-persistent\n");
|
---|
477 | goto done;
|
---|
478 | } else if (non_persistent == 1) {
|
---|
479 | tdb_flags |= TDB_CLEAR_IF_FIRST;
|
---|
480 | }
|
---|
481 |
|
---|
482 | dbname = extra_argv[0];
|
---|
483 | opname = extra_argv[1];
|
---|
484 |
|
---|
485 | if (strcmp(opname, "store") == 0) {
|
---|
486 | if (extra_argc != 5) {
|
---|
487 | d_fprintf(stderr, "ERROR: operation 'store' requires "
|
---|
488 | "value argument\n");
|
---|
489 | goto done;
|
---|
490 | }
|
---|
491 | valuestr = extra_argv[4];
|
---|
492 | keytype = extra_argv[3];
|
---|
493 | keyname = extra_argv[2];
|
---|
494 | op = OP_STORE;
|
---|
495 | } else if (strcmp(opname, "fetch") == 0) {
|
---|
496 | if (extra_argc != 4) {
|
---|
497 | d_fprintf(stderr, "ERROR: operation 'fetch' requires "
|
---|
498 | "type but not value argument\n");
|
---|
499 | goto done;
|
---|
500 | }
|
---|
501 | op = OP_FETCH;
|
---|
502 | keytype = extra_argv[3];
|
---|
503 | keyname = extra_argv[2];
|
---|
504 | } else if (strcmp(opname, "delete") == 0) {
|
---|
505 | if (extra_argc != 3) {
|
---|
506 | d_fprintf(stderr, "ERROR: operation 'delete' does "
|
---|
507 | "not allow type nor value argument\n");
|
---|
508 | goto done;
|
---|
509 | }
|
---|
510 | keyname = extra_argv[2];
|
---|
511 | op = OP_DELETE;
|
---|
512 | } else if (strcmp(opname, "erase") == 0) {
|
---|
513 | if (extra_argc != 2) {
|
---|
514 | d_fprintf(stderr, "ERROR: operation 'erase' does "
|
---|
515 | "not take a key argument\n");
|
---|
516 | goto done;
|
---|
517 | }
|
---|
518 | op = OP_ERASE;
|
---|
519 | } else if (strcmp(opname, "listkeys") == 0) {
|
---|
520 | if (extra_argc != 2) {
|
---|
521 | d_fprintf(stderr, "ERROR: operation 'listkeys' does "
|
---|
522 | "not take a key argument\n");
|
---|
523 | goto done;
|
---|
524 | }
|
---|
525 | op = OP_LISTKEYS;
|
---|
526 | } else if (strcmp(opname, "listwatchers") == 0) {
|
---|
527 | if (extra_argc != 2) {
|
---|
528 | d_fprintf(stderr, "ERROR: operation 'listwatchers' "
|
---|
529 | "does not take an argument\n");
|
---|
530 | goto done;
|
---|
531 | }
|
---|
532 | op = OP_LISTWATCHERS;
|
---|
533 | keytype = "none";
|
---|
534 | } else if (strcmp(opname, "exists") == 0) {
|
---|
535 | if (extra_argc != 3) {
|
---|
536 | d_fprintf(stderr, "ERROR: operation 'exists' does "
|
---|
537 | "not allow type nor value argument\n");
|
---|
538 | goto done;
|
---|
539 | }
|
---|
540 | keyname = extra_argv[2];
|
---|
541 | op = OP_EXISTS;
|
---|
542 | keytype = "string";
|
---|
543 | } else {
|
---|
544 | d_fprintf(stderr,
|
---|
545 | "ERROR: invalid op '%s' specified\n"
|
---|
546 | " supported ops: fetch, store, delete, exists, "
|
---|
547 | "erase, listkeys, listwatchers\n",
|
---|
548 | opname);
|
---|
549 | goto done;
|
---|
550 | }
|
---|
551 |
|
---|
552 | if (strcmp(keytype, "int32") == 0) {
|
---|
553 | type = TYPE_INT32;
|
---|
554 | } else if (strcmp(keytype, "uint32") == 0) {
|
---|
555 | type = TYPE_UINT32;
|
---|
556 | } else if (strcmp(keytype, "string") == 0) {
|
---|
557 | type = TYPE_STRING;
|
---|
558 | } else if (strcmp(keytype, "hex") == 0) {
|
---|
559 | type = TYPE_HEX;
|
---|
560 | } else if (strcmp(keytype, "none") == 0) {
|
---|
561 | type = TYPE_NONE;
|
---|
562 | } else {
|
---|
563 | d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
|
---|
564 | " supported types: int32, uint32, "
|
---|
565 | "string, hex, none\n",
|
---|
566 | keytype);
|
---|
567 | goto done;
|
---|
568 | }
|
---|
569 |
|
---|
570 | evt_ctx = samba_tevent_context_init(mem_ctx);
|
---|
571 | if (evt_ctx == NULL) {
|
---|
572 | d_fprintf(stderr, "ERROR: could not init event context\n");
|
---|
573 | goto done;
|
---|
574 | }
|
---|
575 |
|
---|
576 | msg_ctx = messaging_init(mem_ctx, evt_ctx);
|
---|
577 | if (msg_ctx == NULL) {
|
---|
578 | d_fprintf(stderr, "ERROR: could not init messaging context\n");
|
---|
579 | goto done;
|
---|
580 | }
|
---|
581 |
|
---|
582 | switch (op) {
|
---|
583 | case OP_FETCH:
|
---|
584 | case OP_STORE:
|
---|
585 | case OP_DELETE:
|
---|
586 | case OP_ERASE:
|
---|
587 | case OP_LISTKEYS:
|
---|
588 | case OP_EXISTS:
|
---|
589 | db = db_open(mem_ctx, dbname, 0, tdb_flags, O_RDWR | O_CREAT,
|
---|
590 | 0644, DBWRAP_LOCK_ORDER_1, DBWRAP_FLAG_NONE);
|
---|
591 | if (db == NULL) {
|
---|
592 | d_fprintf(stderr, "ERROR: could not open dbname\n");
|
---|
593 | goto done;
|
---|
594 | }
|
---|
595 | break;
|
---|
596 | default:
|
---|
597 | db = NULL;
|
---|
598 | break;
|
---|
599 | }
|
---|
600 |
|
---|
601 | for (count = 0; dispatch_table[count].cmd != NULL; count++) {
|
---|
602 | if ((op == dispatch_table[count].op) &&
|
---|
603 | (type == dispatch_table[count].type))
|
---|
604 | {
|
---|
605 | ret = dispatch_table[count].cmd(db, keyname, valuestr);
|
---|
606 | break;
|
---|
607 | }
|
---|
608 | }
|
---|
609 |
|
---|
610 | done:
|
---|
611 | TALLOC_FREE(mem_ctx);
|
---|
612 | return ret;
|
---|
613 | }
|
---|