r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / tdb / tools / tdbtool.c
blobe90acd18f0a55b856d8d9e7e37b9516f3b09fdf2
1 /*
2 Unix SMB/CIFS implementation.
3 Samba database functions
4 Copyright (C) Andrew Tridgell 1999-2000
5 Copyright (C) Paul `Rusty' Russell 2000
6 Copyright (C) Jeremy Allison 2000
7 Copyright (C) Andrew Esh 2001
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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "replace.h"
25 #include "system/locale.h"
26 #include "system/time.h"
27 #include "system/filesys.h"
28 #include "tdb.h"
30 static int do_command(void);
31 const char *cmdname;
32 char *arg1, *arg2;
33 size_t arg1len, arg2len;
34 int bIterate = 0;
35 char *line;
36 TDB_DATA iterate_kbuf;
37 char cmdline[1024];
38 static int disable_mmap;
40 enum commands {
41 CMD_CREATE_TDB,
42 CMD_OPEN_TDB,
43 CMD_ERASE,
44 CMD_DUMP,
45 CMD_INSERT,
46 CMD_MOVE,
47 CMD_STORE,
48 CMD_SHOW,
49 CMD_KEYS,
50 CMD_HEXKEYS,
51 CMD_DELETE,
52 CMD_LIST_HASH_FREE,
53 CMD_LIST_FREE,
54 CMD_INFO,
55 CMD_MMAP,
56 CMD_SPEED,
57 CMD_FIRST,
58 CMD_NEXT,
59 CMD_SYSTEM,
60 CMD_QUIT,
61 CMD_HELP
64 typedef struct {
65 const char *name;
66 enum commands cmd;
67 } COMMAND_TABLE;
69 COMMAND_TABLE cmd_table[] = {
70 {"create", CMD_CREATE_TDB},
71 {"open", CMD_OPEN_TDB},
72 {"erase", CMD_ERASE},
73 {"dump", CMD_DUMP},
74 {"insert", CMD_INSERT},
75 {"move", CMD_MOVE},
76 {"store", CMD_STORE},
77 {"show", CMD_SHOW},
78 {"keys", CMD_KEYS},
79 {"hexkeys", CMD_HEXKEYS},
80 {"delete", CMD_DELETE},
81 {"list", CMD_LIST_HASH_FREE},
82 {"free", CMD_LIST_FREE},
83 {"info", CMD_INFO},
84 {"speed", CMD_SPEED},
85 {"mmap", CMD_MMAP},
86 {"first", CMD_FIRST},
87 {"1", CMD_FIRST},
88 {"next", CMD_NEXT},
89 {"n", CMD_NEXT},
90 {"quit", CMD_QUIT},
91 {"q", CMD_QUIT},
92 {"!", CMD_SYSTEM},
93 {NULL, CMD_HELP}
96 struct timeval tp1,tp2;
98 static void _start_timer(void)
100 gettimeofday(&tp1,NULL);
103 static double _end_timer(void)
105 gettimeofday(&tp2,NULL);
106 return((tp2.tv_sec - tp1.tv_sec) +
107 (tp2.tv_usec - tp1.tv_usec)*1.0e-6);
110 /* a tdb tool for manipulating a tdb database */
112 static TDB_CONTEXT *tdb;
114 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
115 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
116 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
118 static void print_asc(const char *buf,int len)
120 int i;
122 /* We're probably printing ASCII strings so don't try to display
123 the trailing NULL character. */
125 if (buf[len - 1] == 0)
126 len--;
128 for (i=0;i<len;i++)
129 printf("%c",isprint(buf[i])?buf[i]:'.');
132 static void print_data(const char *buf,int len)
134 int i=0;
135 if (len<=0) return;
136 printf("[%03X] ",i);
137 for (i=0;i<len;) {
138 printf("%02X ",(int)buf[i]);
139 i++;
140 if (i%8 == 0) printf(" ");
141 if (i%16 == 0) {
142 print_asc(&buf[i-16],8); printf(" ");
143 print_asc(&buf[i-8],8); printf("\n");
144 if (i<len) printf("[%03X] ",i);
147 if (i%16) {
148 int n;
150 n = 16 - (i%16);
151 printf(" ");
152 if (n>8) printf(" ");
153 while (n--) printf(" ");
155 n = i%16;
156 if (n > 8) n = 8;
157 print_asc(&buf[i-(i%16)],n); printf(" ");
158 n = (i%16) - n;
159 if (n>0) print_asc(&buf[i-n],n);
160 printf("\n");
164 static void help(void)
166 printf("\n"
167 "tdbtool: \n"
168 " create dbname : create a database\n"
169 " open dbname : open an existing database\n"
170 " erase : erase the database\n"
171 " dump : dump the database as strings\n"
172 " keys : dump the database keys as strings\n"
173 " hexkeys : dump the database keys as hex values\n"
174 " info : print summary info about the database\n"
175 " insert key data : insert a record\n"
176 " move key file : move a record to a destination tdb\n"
177 " store key data : store a record (replace)\n"
178 " show key : show a record by key\n"
179 " delete key : delete a record by key\n"
180 " list : print the database hash table and freelist\n"
181 " free : print the database freelist\n"
182 " ! command : execute system command\n"
183 " 1 | first : print the first record\n"
184 " n | next : print the next record\n"
185 " q | quit : terminate\n"
186 " \\n : repeat 'next' command\n"
187 "\n");
190 static void terror(const char *why)
192 printf("%s\n", why);
195 static void create_tdb(const char *tdbname)
197 if (tdb) tdb_close(tdb);
198 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST | (disable_mmap?TDB_NOMMAP:0),
199 O_RDWR | O_CREAT | O_TRUNC, 0600);
200 if (!tdb) {
201 printf("Could not create %s: %s\n", tdbname, strerror(errno));
205 static void open_tdb(const char *tdbname)
207 if (tdb) tdb_close(tdb);
208 tdb = tdb_open(tdbname, 0, disable_mmap?TDB_NOMMAP:0, O_RDWR, 0600);
209 if (!tdb) {
210 printf("Could not open %s: %s\n", tdbname, strerror(errno));
214 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
216 TDB_DATA key, dbuf;
218 if ((keyname == NULL) || (keylen == 0)) {
219 terror("need key");
220 return;
223 key.dptr = (unsigned char *)keyname;
224 key.dsize = keylen;
225 dbuf.dptr = (unsigned char *)data;
226 dbuf.dsize = datalen;
228 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
229 terror("insert failed");
233 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
235 TDB_DATA key, dbuf;
237 if ((keyname == NULL) || (keylen == 0)) {
238 terror("need key");
239 return;
242 if ((data == NULL) || (datalen == 0)) {
243 terror("need data");
244 return;
247 key.dptr = (unsigned char *)keyname;
248 key.dsize = keylen;
249 dbuf.dptr = (unsigned char *)data;
250 dbuf.dsize = datalen;
252 printf("Storing key:\n");
253 print_rec(tdb, key, dbuf, NULL);
255 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
256 terror("store failed");
260 static void show_tdb(char *keyname, size_t keylen)
262 TDB_DATA key, dbuf;
264 if ((keyname == NULL) || (keylen == 0)) {
265 terror("need key");
266 return;
269 key.dptr = (unsigned char *)keyname;
270 key.dsize = keylen;
272 dbuf = tdb_fetch(tdb, key);
273 if (!dbuf.dptr) {
274 terror("fetch failed");
275 return;
278 print_rec(tdb, key, dbuf, NULL);
280 free( dbuf.dptr );
282 return;
285 static void delete_tdb(char *keyname, size_t keylen)
287 TDB_DATA key;
289 if ((keyname == NULL) || (keylen == 0)) {
290 terror("need key");
291 return;
294 key.dptr = (unsigned char *)keyname;
295 key.dsize = keylen;
297 if (tdb_delete(tdb, key) != 0) {
298 terror("delete failed");
302 static void move_rec(char *keyname, size_t keylen, char* tdbname)
304 TDB_DATA key, dbuf;
305 TDB_CONTEXT *dst_tdb;
307 if ((keyname == NULL) || (keylen == 0)) {
308 terror("need key");
309 return;
312 if ( !tdbname ) {
313 terror("need destination tdb name");
314 return;
317 key.dptr = (unsigned char *)keyname;
318 key.dsize = keylen;
320 dbuf = tdb_fetch(tdb, key);
321 if (!dbuf.dptr) {
322 terror("fetch failed");
323 return;
326 print_rec(tdb, key, dbuf, NULL);
328 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
329 if ( !dst_tdb ) {
330 terror("unable to open destination tdb");
331 return;
334 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
335 terror("failed to move record");
337 else
338 printf("record moved\n");
340 tdb_close( dst_tdb );
342 return;
345 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
347 printf("\nkey %d bytes\n", (int)key.dsize);
348 print_asc((const char *)key.dptr, key.dsize);
349 printf("\ndata %d bytes\n", (int)dbuf.dsize);
350 print_data((const char *)dbuf.dptr, dbuf.dsize);
351 return 0;
354 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
356 printf("key %d bytes: ", (int)key.dsize);
357 print_asc((const char *)key.dptr, key.dsize);
358 printf("\n");
359 return 0;
362 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
364 printf("key %d bytes\n", (int)key.dsize);
365 print_data((const char *)key.dptr, key.dsize);
366 printf("\n");
367 return 0;
370 static int total_bytes;
372 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
374 total_bytes += dbuf.dsize;
375 return 0;
378 static void info_tdb(void)
380 int count;
381 total_bytes = 0;
382 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
383 printf("Error = %s\n", tdb_errorstr(tdb));
384 else
385 printf("%d records totalling %d bytes\n", count, total_bytes);
388 static void speed_tdb(const char *tlimit)
390 unsigned timelimit = tlimit?atoi(tlimit):0;
391 double t;
392 int ops=0;
393 if (timelimit == 0) timelimit = 10;
394 printf("Testing traverse speed for %u seconds\n", timelimit);
395 _start_timer();
396 while ((t=_end_timer()) < timelimit) {
397 tdb_traverse(tdb, traverse_fn, NULL);
398 printf("%10.3f ops/sec\r", (++ops)/t);
400 printf("\n");
403 static void toggle_mmap(void)
405 disable_mmap = !disable_mmap;
406 if (disable_mmap) {
407 printf("mmap is disabled\n");
408 } else {
409 printf("mmap is enabled\n");
413 static char *tdb_getline(const char *prompt)
415 static char thisline[1024];
416 char *p;
417 fputs(prompt, stdout);
418 thisline[0] = 0;
419 p = fgets(thisline, sizeof(thisline)-1, stdin);
420 if (p) p = strchr(p, '\n');
421 if (p) *p = 0;
422 return p?thisline:NULL;
425 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
426 void *state)
428 return tdb_delete(the_tdb, key);
431 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
433 TDB_DATA dbuf;
434 *pkey = tdb_firstkey(the_tdb);
436 dbuf = tdb_fetch(the_tdb, *pkey);
437 if (!dbuf.dptr) terror("fetch failed");
438 else {
439 print_rec(the_tdb, *pkey, dbuf, NULL);
443 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
445 TDB_DATA dbuf;
446 *pkey = tdb_nextkey(the_tdb, *pkey);
448 dbuf = tdb_fetch(the_tdb, *pkey);
449 if (!dbuf.dptr)
450 terror("fetch failed");
451 else
452 print_rec(the_tdb, *pkey, dbuf, NULL);
455 static int do_command(void)
457 COMMAND_TABLE *ctp = cmd_table;
458 enum commands mycmd = CMD_HELP;
459 int cmd_len;
461 if (cmdname && strlen(cmdname) == 0) {
462 mycmd = CMD_NEXT;
463 } else {
464 while (ctp->name) {
465 cmd_len = strlen(ctp->name);
466 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
467 mycmd = ctp->cmd;
468 break;
470 ctp++;
474 switch (mycmd) {
475 case CMD_CREATE_TDB:
476 bIterate = 0;
477 create_tdb(arg1);
478 return 0;
479 case CMD_OPEN_TDB:
480 bIterate = 0;
481 open_tdb(arg1);
482 return 0;
483 case CMD_SYSTEM:
484 /* Shell command */
485 system(arg1);
486 return 0;
487 case CMD_QUIT:
488 return 1;
489 default:
490 /* all the rest require a open database */
491 if (!tdb) {
492 bIterate = 0;
493 terror("database not open");
494 help();
495 return 0;
497 switch (mycmd) {
498 case CMD_ERASE:
499 bIterate = 0;
500 tdb_traverse(tdb, do_delete_fn, NULL);
501 return 0;
502 case CMD_DUMP:
503 bIterate = 0;
504 tdb_traverse(tdb, print_rec, NULL);
505 return 0;
506 case CMD_INSERT:
507 bIterate = 0;
508 insert_tdb(arg1, arg1len,arg2,arg2len);
509 return 0;
510 case CMD_MOVE:
511 bIterate = 0;
512 move_rec(arg1,arg1len,arg2);
513 return 0;
514 case CMD_STORE:
515 bIterate = 0;
516 store_tdb(arg1,arg1len,arg2,arg2len);
517 return 0;
518 case CMD_SHOW:
519 bIterate = 0;
520 show_tdb(arg1, arg1len);
521 return 0;
522 case CMD_KEYS:
523 tdb_traverse(tdb, print_key, NULL);
524 return 0;
525 case CMD_HEXKEYS:
526 tdb_traverse(tdb, print_hexkey, NULL);
527 return 0;
528 case CMD_DELETE:
529 bIterate = 0;
530 delete_tdb(arg1,arg1len);
531 return 0;
532 case CMD_LIST_HASH_FREE:
533 tdb_dump_all(tdb);
534 return 0;
535 case CMD_LIST_FREE:
536 tdb_printfreelist(tdb);
537 return 0;
538 case CMD_INFO:
539 info_tdb();
540 return 0;
541 case CMD_SPEED:
542 speed_tdb(arg1);
543 return 0;
544 case CMD_MMAP:
545 toggle_mmap();
546 return 0;
547 case CMD_FIRST:
548 bIterate = 1;
549 first_record(tdb, &iterate_kbuf);
550 return 0;
551 case CMD_NEXT:
552 if (bIterate)
553 next_record(tdb, &iterate_kbuf);
554 return 0;
555 case CMD_HELP:
556 help();
557 return 0;
558 case CMD_CREATE_TDB:
559 case CMD_OPEN_TDB:
560 case CMD_SYSTEM:
561 case CMD_QUIT:
563 * unhandled commands. cases included here to avoid compiler
564 * warnings.
566 return 0;
570 return 0;
573 static char *convert_string(char *instring, size_t *sizep)
575 size_t length = 0;
576 char *outp, *inp;
577 char temp[3];
580 outp = inp = instring;
582 while (*inp) {
583 if (*inp == '\\') {
584 inp++;
585 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
586 temp[0] = *inp++;
587 temp[1] = '\0';
588 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
589 temp[1] = *inp++;
590 temp[2] = '\0';
592 *outp++ = (char)strtol((const char *)temp,NULL,16);
593 } else {
594 *outp++ = *inp++;
596 } else {
597 *outp++ = *inp++;
599 length++;
601 *sizep = length;
602 return instring;
605 int main(int argc, char *argv[])
607 cmdname = "";
608 arg1 = NULL;
609 arg1len = 0;
610 arg2 = NULL;
611 arg2len = 0;
613 if (argv[1]) {
614 cmdname = "open";
615 arg1 = argv[1];
616 do_command();
617 cmdname = "";
618 arg1 = NULL;
621 switch (argc) {
622 case 1:
623 case 2:
624 /* Interactive mode */
625 while ((cmdname = tdb_getline("tdb> "))) {
626 arg2 = arg1 = NULL;
627 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
628 arg1++;
629 arg2 = arg1;
630 while (*arg2) {
631 if (*arg2 == ' ') {
632 *arg2++ = '\0';
633 break;
635 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
636 arg2++;
640 if (arg1) arg1 = convert_string(arg1,&arg1len);
641 if (arg2) arg2 = convert_string(arg2,&arg2len);
642 if (do_command()) break;
644 break;
645 case 5:
646 arg2 = convert_string(argv[4],&arg2len);
647 case 4:
648 arg1 = convert_string(argv[3],&arg1len);
649 case 3:
650 cmdname = argv[2];
651 default:
652 do_command();
653 break;
656 if (tdb) tdb_close(tdb);
658 return 0;