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 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/>.
24 #include "system/locale.h"
25 #include "system/time.h"
26 #include "system/filesys.h"
29 static int do_command(void);
32 size_t arg1len, arg2len;
35 TDB_DATA iterate_kbuf;
65 COMMAND_TABLE cmd_table[] = {
66 {"create", CMD_CREATE_TDB},
67 {"open", CMD_OPEN_TDB},
70 {"insert", CMD_INSERT},
75 {"hexkeys", CMD_HEXKEYS},
76 {"delete", CMD_DELETE},
77 {"list", CMD_LIST_HASH_FREE},
78 {"free", CMD_LIST_FREE},
90 /* a tdb tool for manipulating a tdb database */
92 static TDB_CONTEXT *tdb;
94 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
95 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
96 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
98 static void print_asc(const char *buf,int len)
102 /* We're probably printing ASCII strings so don't try to display
103 the trailing NULL character. */
105 if (buf[len - 1] == 0)
109 printf("%c",isprint(buf[i])?buf[i]:'.');
112 static void print_data(const char *buf,int len)
118 printf("%02X ",(int)buf[i]);
120 if (i%8 == 0) printf(" ");
122 print_asc(&buf[i-16],8); printf(" ");
123 print_asc(&buf[i-8],8); printf("\n");
124 if (i<len) printf("[%03X] ",i);
132 if (n>8) printf(" ");
133 while (n--) printf(" ");
137 print_asc(&buf[i-(i%16)],n); printf(" ");
139 if (n>0) print_asc(&buf[i-n],n);
144 static void help(void)
148 " create dbname : create a database\n"
149 " open dbname : open an existing database\n"
150 " erase : erase the database\n"
151 " dump : dump the database as strings\n"
152 " keys : dump the database keys as strings\n"
153 " hexkeys : dump the database keys as hex values\n"
154 " info : print summary info about the database\n"
155 " insert key data : insert a record\n"
156 " move key file : move a record to a destination tdb\n"
157 " store key data : store a record (replace)\n"
158 " show key : show a record by key\n"
159 " delete key : delete a record by key\n"
160 " list : print the database hash table and freelist\n"
161 " free : print the database freelist\n"
162 " ! command : execute system command\n"
163 " 1 | first : print the first record\n"
164 " n | next : print the next record\n"
165 " q | quit : terminate\n"
166 " \\n : repeat 'next' command\n"
170 static void terror(const char *why)
175 static void create_tdb(const char *tdbname)
177 if (tdb) tdb_close(tdb);
178 tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST,
179 O_RDWR | O_CREAT | O_TRUNC, 0600);
181 printf("Could not create %s: %s\n", tdbname, strerror(errno));
185 static void open_tdb(const char *tdbname)
187 if (tdb) tdb_close(tdb);
188 tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
190 printf("Could not open %s: %s\n", tdbname, strerror(errno));
194 static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
198 if ((keyname == NULL) || (keylen == 0)) {
203 key.dptr = (unsigned char *)keyname;
205 dbuf.dptr = (unsigned char *)data;
206 dbuf.dsize = datalen;
208 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
209 terror("insert failed");
213 static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
217 if ((keyname == NULL) || (keylen == 0)) {
222 if ((data == NULL) || (datalen == 0)) {
227 key.dptr = (unsigned char *)keyname;
229 dbuf.dptr = (unsigned char *)data;
230 dbuf.dsize = datalen;
232 printf("Storing key:\n");
233 print_rec(tdb, key, dbuf, NULL);
235 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
236 terror("store failed");
240 static void show_tdb(char *keyname, size_t keylen)
244 if ((keyname == NULL) || (keylen == 0)) {
249 key.dptr = (unsigned char *)keyname;
252 dbuf = tdb_fetch(tdb, key);
254 terror("fetch failed");
258 print_rec(tdb, key, dbuf, NULL);
265 static void delete_tdb(char *keyname, size_t keylen)
269 if ((keyname == NULL) || (keylen == 0)) {
274 key.dptr = (unsigned char *)keyname;
277 if (tdb_delete(tdb, key) != 0) {
278 terror("delete failed");
282 static void move_rec(char *keyname, size_t keylen, char* tdbname)
285 TDB_CONTEXT *dst_tdb;
287 if ((keyname == NULL) || (keylen == 0)) {
293 terror("need destination tdb name");
297 key.dptr = (unsigned char *)keyname;
300 dbuf = tdb_fetch(tdb, key);
302 terror("fetch failed");
306 print_rec(tdb, key, dbuf, NULL);
308 dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600);
310 terror("unable to open destination tdb");
314 if ( tdb_store( dst_tdb, key, dbuf, TDB_REPLACE ) == -1 ) {
315 terror("failed to move record");
318 printf("record moved\n");
320 tdb_close( dst_tdb );
325 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
327 printf("\nkey %d bytes\n", (int)key.dsize);
328 print_asc((const char *)key.dptr, key.dsize);
329 printf("\ndata %d bytes\n", (int)dbuf.dsize);
330 print_data((const char *)dbuf.dptr, dbuf.dsize);
334 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
336 printf("key %d bytes: ", (int)key.dsize);
337 print_asc((const char *)key.dptr, key.dsize);
342 static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
344 printf("key %d bytes\n", (int)key.dsize);
345 print_data((const char *)key.dptr, key.dsize);
350 static int total_bytes;
352 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
354 total_bytes += dbuf.dsize;
358 static void info_tdb(void)
362 if ((count = tdb_traverse(tdb, traverse_fn, NULL)) == -1)
363 printf("Error = %s\n", tdb_errorstr(tdb));
365 printf("%d records totalling %d bytes\n", count, total_bytes);
368 static char *tdb_getline(const char *prompt)
370 static char thisline[1024];
372 fputs(prompt, stdout);
374 p = fgets(thisline, sizeof(thisline)-1, stdin);
375 if (p) p = strchr(p, '\n');
377 return p?thisline:NULL;
380 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
383 return tdb_delete(the_tdb, key);
386 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
389 *pkey = tdb_firstkey(the_tdb);
391 dbuf = tdb_fetch(the_tdb, *pkey);
392 if (!dbuf.dptr) terror("fetch failed");
394 print_rec(the_tdb, *pkey, dbuf, NULL);
398 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
401 *pkey = tdb_nextkey(the_tdb, *pkey);
403 dbuf = tdb_fetch(the_tdb, *pkey);
405 terror("fetch failed");
407 print_rec(the_tdb, *pkey, dbuf, NULL);
410 static int do_command(void)
412 COMMAND_TABLE *ctp = cmd_table;
413 enum commands mycmd = CMD_HELP;
416 if (cmdname && strlen(cmdname) == 0) {
420 cmd_len = strlen(ctp->name);
421 if (strncmp(ctp->name,cmdname,cmd_len) == 0) {
445 /* all the rest require a open database */
448 terror("database not open");
455 tdb_traverse(tdb, do_delete_fn, NULL);
459 tdb_traverse(tdb, print_rec, NULL);
463 insert_tdb(arg1, arg1len,arg2,arg2len);
467 move_rec(arg1,arg1len,arg2);
471 store_tdb(arg1,arg1len,arg2,arg2len);
475 show_tdb(arg1, arg1len);
478 tdb_traverse(tdb, print_key, NULL);
481 tdb_traverse(tdb, print_hexkey, NULL);
485 delete_tdb(arg1,arg1len);
487 case CMD_LIST_HASH_FREE:
491 tdb_printfreelist(tdb);
498 first_record(tdb, &iterate_kbuf);
502 next_record(tdb, &iterate_kbuf);
512 * unhandled commands. cases included here to avoid compiler
522 static char *convert_string(char *instring, size_t *sizep)
529 outp = inp = instring;
534 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
537 if (*inp && strchr("0123456789abcdefABCDEF",(int)*inp)) {
541 *outp++ = (char)strtol((const char *)temp,NULL,16);
554 int main(int argc, char *argv[])
573 /* Interactive mode */
574 while ((cmdname = tdb_getline("tdb> "))) {
576 if ((arg1 = strchr((const char *)cmdname,' ')) != NULL) {
584 if ((*arg2++ == '\\') && (*arg2 == ' ')) {
589 if (arg1) arg1 = convert_string(arg1,&arg1len);
590 if (arg2) arg2 = convert_string(arg2,&arg2len);
591 if (do_command()) break;
595 arg2 = convert_string(argv[4],&arg2len);
597 arg1 = convert_string(argv[3],&arg1len);
605 if (tdb) tdb_close(tdb);