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.
41 static int do_command(void);
44 size_t arg1len, arg2len;
47 TDB_DATA iterate_kbuf;
77 COMMAND_TABLE cmd_table[] = {
78 {"create", CMD_CREATE_TDB},
79 {"open", CMD_OPEN_TDB},
82 {"insert", CMD_INSERT},
87 {"hexkeys", CMD_HEXKEYS},
88 {"delete", CMD_DELETE},
89 {"list", CMD_LIST_HASH_FREE},
90 {"free", CMD_LIST_FREE},
102 /* a tdb tool for manipulating a tdb database */
104 static TDB_CONTEXT *tdb;
106 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
107 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
108 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
110 static void print_asc(const char *buf,int len)
114 /* We're probably printing ASCII strings so don't try to display
115 the trailing NULL character. */
117 if (buf[len - 1] == 0)
121 printf("%c",isprint(buf[i])?buf[i]:'.');
124 static void print_data(const char *buf,int len)
130 printf("%02X ",(int)buf[i]);
132 if (i%8 == 0) printf(" ");
134 print_asc(&buf[i-16],8); printf(" ");
135 print_asc(&buf[i-8],8); printf("\n");
136 if (i<len) printf("[%03X] ",i);
144 if (n>8) printf(" ");
145 while (n--) printf(" ");
149 print_asc(&buf[i-(i%16)],n); printf(" ");
151 if (n>0) print_asc(&buf[i-n],n);
156 static void help(void)
160 " create dbname : create a database\n"
161 " open dbname : open an existing database\n"
162 " erase : erase the database\n"
163 " dump : dump the database as strings\n"
164 " keys : dump the database keys as strings\n"
165 " hexkeys : dump the database keys as hex values\n"
166 " info : print summary info about the database\n"
167 " insert key data : insert a record\n"
168 " move key file : move a record to a destination tdb\n"
169 " store key data : store a record (replace)\n"
170 " show key : show a record by key\n"
171 " delete key : delete a record by key\n"
172 " list : print the database hash table and freelist\n"
173 " free : print the database freelist\n"
174 " ! command : execute system command\n"
175 " 1 | first : print the first record\n"
176 " n | next : print the next record\n"
177 " q | quit : terminate\n"
178 " \\n : repeat 'next' command\n"
182 static void terror(const char *why)
187 static void create_tdb(const char *tdbname)
189 if (tdb) tdb_close(tdb);
190 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST,
191 O_RDWR | O_CREAT | O_TRUNC, 0600);
193 printf("Could not create %s: %s\n", tdbname, strerror(errno));
197 static void open_tdb(const char *tdbname)
199 if (tdb) tdb_close(tdb);
200 tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
202 printf("Could not open %s: %s\n", tdbname, strerror(errno));
206 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
210 if ((keyname == NULL) || (keylen == 0)) {
215 key.dptr = (unsigned char *)keyname;
217 dbuf.dptr = (unsigned char *)data;
218 dbuf.dsize = datalen;
220 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
221 terror("insert failed");
225 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
229 if ((keyname == NULL) || (keylen == 0)) {
234 if ((data == NULL) || (datalen == 0)) {
239 key.dptr = (unsigned char *)keyname;
241 dbuf.dptr = (unsigned char *)data;
242 dbuf.dsize = datalen;
244 printf("Storing key:\n");
245 print_rec(tdb, key, dbuf, NULL);
247 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
248 terror("store failed");
252 static void show_tdb(char *keyname, size_t keylen)
256 if ((keyname == NULL) || (keylen == 0)) {
261 key.dptr = (unsigned char *)keyname;
264 dbuf = tdb_fetch(tdb, key);
266 terror("fetch failed");
270 print_rec(tdb, key, dbuf, NULL);
277 static void delete_tdb(char *keyname, size_t keylen)
281 if ((keyname == NULL) || (keylen == 0)) {
286 key.dptr = (unsigned char *)keyname;
289 if (tdb_delete(tdb, key) != 0) {
290 terror("delete failed");
294 static void move_rec(char *keyname, size_t keylen, char* tdbname)
297 TDB_CONTEXT *dst_tdb;
299 if ((keyname == NULL) || (keylen == 0)) {
305 terror("need destination tdb name");
309 key.dptr = (unsigned char *)keyname;
312 dbuf = tdb_fetch(tdb, key);
314 terror("fetch failed");
318 print_rec(tdb, key, dbuf, NULL);
320 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
322 terror("unable to open destination tdb");
326 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
327 terror("failed to move record");
330 printf("record moved\n");
332 tdb_close( dst_tdb );
337 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
339 printf("\nkey %d bytes\n", (int)key.dsize);
340 print_asc((const char *)key.dptr, key.dsize);
341 printf("\ndata %d bytes\n", (int)dbuf.dsize);
342 print_data((const char *)dbuf.dptr, dbuf.dsize);
346 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
348 printf("key %d bytes: ", (int)key.dsize);
349 print_asc((const char *)key.dptr, key.dsize);
354 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
356 printf("key %d bytes\n", (int)key.dsize);
357 print_data((const char *)key.dptr, key.dsize);
362 static int total_bytes;
364 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
366 total_bytes += dbuf.dsize;
370 static void info_tdb(void)
374 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
375 printf("Error = %s\n", tdb_errorstr(tdb));
377 printf("%d records totalling %d bytes\n", count, total_bytes);
380 static char *tdb_getline(const char *prompt)
382 static char thisline[1024];
384 fputs(prompt, stdout);
386 p = fgets(thisline, sizeof(thisline)-1, stdin);
387 if (p) p = strchr(p, '\n');
389 return p?thisline:NULL;
392 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
395 return tdb_delete(the_tdb, key);
398 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
401 *pkey = tdb_firstkey(the_tdb);
403 dbuf = tdb_fetch(the_tdb, *pkey);
404 if (!dbuf.dptr) terror("fetch failed");
406 print_rec(the_tdb, *pkey, dbuf, NULL);
410 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
413 *pkey = tdb_nextkey(the_tdb, *pkey);
415 dbuf = tdb_fetch(the_tdb, *pkey);
417 terror("fetch failed");
419 print_rec(the_tdb, *pkey, dbuf, NULL);
422 static int do_command(void)
424 COMMAND_TABLE *ctp = cmd_table;
425 enum commands mycmd = CMD_HELP;
428 if (cmdname && strlen(cmdname) == 0) {
432 cmd_len = strlen(ctp->name);
433 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
457 /* all the rest require a open database */
460 terror("database not open");
467 tdb_traverse(tdb, do_delete_fn, NULL);
471 tdb_traverse(tdb, print_rec, NULL);
475 insert_tdb(arg1, arg1len,arg2,arg2len);
479 move_rec(arg1,arg1len,arg2);
483 store_tdb(arg1,arg1len,arg2,arg2len);
487 show_tdb(arg1, arg1len);
490 tdb_traverse(tdb, print_key, NULL);
493 tdb_traverse(tdb, print_hexkey, NULL);
497 delete_tdb(arg1,arg1len);
499 case CMD_LIST_HASH_FREE:
503 tdb_printfreelist(tdb);
510 first_record(tdb, &iterate_kbuf);
514 next_record(tdb, &iterate_kbuf);
524 * unhandled commands. cases included here to avoid compiler
534 static char *convert_string(char *instring, size_t *sizep)
541 outp = inp = instring;
546 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
549 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
553 *outp++ = (char)strtol((const char *)temp,NULL,16);
566 int main(int argc, char *argv[])
585 /* Interactive mode */
586 while ((cmdname = tdb_getline("tdb> "))) {
588 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
596 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
601 if (arg1) arg1 = convert_string(arg1,&arg1len);
602 if (arg2) arg2 = convert_string(arg2,&arg2len);
603 if (do_command()) break;
607 arg2 = convert_string(argv[4],&arg2len);
609 arg1 = convert_string(argv[3],&arg1len);
617 if (tdb) tdb_close(tdb);