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.
40 static int do_command(void);
41 char *cmdname
, *arg1
, *arg2
;
42 size_t arg1len
, arg2len
;
46 TDB_DATA iterate_kbuf
;
77 COMMAND_TABLE cmd_table
[] = {
78 {"create", CMD_CREATE_TDB
},
79 {"open", CMD_OPEN_TDB
},
83 {"insert", CMD_INSERT
},
88 {"hexkeys", CMD_HEXKEYS
},
89 {"delete", CMD_DELETE
},
90 {"list", CMD_LIST_HASH_FREE
},
91 {"free", CMD_LIST_FREE
},
103 /* a tdb tool for manipulating a tdb database */
105 /* these are taken from smb.h - make sure they are in sync */
107 typedef struct connections_key
{
113 typedef struct connections_data
{
121 char machine
[FSTRING_LEN
];
123 unsigned bcast_msg_flags
;
126 static TDB_CONTEXT
*tdb
;
128 static int print_crec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
129 static int print_arec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
130 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
131 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
132 static int print_hexkey(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
134 static void print_asc(const char *buf
,int len
)
138 /* We're probably printing ASCII strings so don't try to display
139 the trailing NULL character. */
141 if (buf
[len
- 1] == 0)
145 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
148 static void print_data(const char *buf
,int len
)
154 printf("%02X ",(int)buf
[i
]);
156 if (i
%8 == 0) printf(" ");
158 print_asc(&buf
[i
-16],8); printf(" ");
159 print_asc(&buf
[i
-8],8); printf("\n");
160 if (i
<len
) printf("[%03X] ",i
);
168 if (n
>8) printf(" ");
169 while (n
--) printf(" ");
173 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
175 if (n
>0) print_asc(&buf
[i
-n
],n
);
180 static void help(void)
184 " create dbname : create a database\n"
185 " open dbname : open an existing database\n"
186 " erase : erase the database\n"
187 " dump : dump the database as strings\n"
188 " cdump : dump the database as connection records\n"
189 " keys : dump the database keys as strings\n"
190 " hexkeys : dump the database keys as hex values\n"
191 " info : print summary info about the database\n"
192 " insert key data : insert a record\n"
193 " move key file : move a record to a destination tdb\n"
194 " store key data : store a record (replace)\n"
195 " show key : show a record by key\n"
196 " delete key : delete a record by key\n"
197 " list : print the database hash table and freelist\n"
198 " free : print the database freelist\n"
199 " ! command : execute system command\n"
200 " 1 | first : print the first record\n"
201 " n | next : print the next record\n"
202 " q | quit : terminate\n"
203 " \\n : repeat 'next' command\n"
207 static void terror(const char *why
)
212 static void create_tdb(char * tdbname
)
214 if (tdb
) tdb_close(tdb
);
215 tdb
= tdb_open(tdbname
, 0, TDB_CLEAR_IF_FIRST
,
216 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
218 printf("Could not create %s: %s\n", tdbname
, strerror(errno
));
222 static void open_tdb(char *tdbname
)
224 if (tdb
) tdb_close(tdb
);
225 tdb
= tdb_open(tdbname
, 0, 0, O_RDWR
, 0600);
227 printf("Could not open %s: %s\n", tdbname
, strerror(errno
));
231 static void insert_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
235 if ((keyname
== NULL
) || (keylen
== 0)) {
243 dbuf
.dsize
= datalen
;
245 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
246 terror("insert failed");
250 static void store_tdb(char *keyname
, size_t keylen
, char* data
, size_t datalen
)
254 if ((keyname
== NULL
) || (keylen
== 0)) {
259 if ((data
== NULL
) || (datalen
== 0)) {
267 dbuf
.dsize
= datalen
;
269 printf("Storing key:\n");
270 print_rec(tdb
, key
, dbuf
, NULL
);
272 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
273 terror("store failed");
277 static void show_tdb(char *keyname
, size_t keylen
)
281 if ((keyname
== NULL
) || (keylen
== 0)) {
289 dbuf
= tdb_fetch(tdb
, key
);
291 terror("fetch failed");
295 print_rec(tdb
, key
, dbuf
, NULL
);
302 static void delete_tdb(char *keyname
, size_t keylen
)
306 if ((keyname
== NULL
) || (keylen
== 0)) {
314 if (tdb_delete(tdb
, key
) != 0) {
315 terror("delete failed");
319 static void move_rec(char *keyname
, size_t keylen
, char* tdbname
)
322 TDB_CONTEXT
*dst_tdb
;
324 if ((keyname
== NULL
) || (keylen
== 0)) {
330 terror("need destination tdb name");
337 dbuf
= tdb_fetch(tdb
, key
);
339 terror("fetch failed");
343 print_rec(tdb
, key
, dbuf
, NULL
);
345 dst_tdb
= tdb_open(tdbname
, 0, 0, O_RDWR
, 0600);
347 terror("unable to open destination tdb");
351 if ( tdb_store( dst_tdb
, key
, dbuf
, TDB_REPLACE
) == -1 ) {
352 terror("failed to move record");
355 printf("record moved\n");
357 tdb_close( dst_tdb
);
362 static int print_conn_key(TDB_DATA key
)
364 printf( "\nkey %d bytes\n", (int)key
.dsize
);
365 printf( "pid =%5d ", ((connections_key
*)key
.dptr
)->pid
);
366 printf( "cnum =%10d ", ((connections_key
*)key
.dptr
)->cnum
);
367 printf( "name =[%s]\n", ((connections_key
*)key
.dptr
)->name
);
371 static int print_conn_data(TDB_DATA dbuf
)
373 printf( "\ndata %d bytes\n", (int)dbuf
.dsize
);
374 printf( "pid =%5d ", ((connections_data
*)dbuf
.dptr
)->pid
);
375 printf( "cnum =%10d ", ((connections_data
*)dbuf
.dptr
)->cnum
);
376 printf( "name =[%s]\n", ((connections_data
*)dbuf
.dptr
)->name
);
378 printf( "uid =%5d ", ((connections_data
*)dbuf
.dptr
)->uid
);
379 printf( "addr =[%s]\n", ((connections_data
*)dbuf
.dptr
)->addr
);
380 printf( "gid =%5d ", ((connections_data
*)dbuf
.dptr
)->gid
);
381 printf( "machine=[%s]\n", ((connections_data
*)dbuf
.dptr
)->machine
);
382 printf( "start = %s\n", ctime(&((connections_data
*)dbuf
.dptr
)->start
));
383 printf( "magic = 0x%x ", ((connections_data
*)dbuf
.dptr
)->magic
);
384 printf( "flags = 0x%x\n", ((connections_data
*)dbuf
.dptr
)->bcast_msg_flags
);
388 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
390 if (do_connections
&& (dbuf
.dsize
== sizeof(connections_data
)))
391 print_crec(the_tdb
, key
, dbuf
, state
);
393 print_arec(the_tdb
, key
, dbuf
, state
);
397 static int print_crec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
400 print_conn_data(dbuf
);
404 static int print_arec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
406 printf("\nkey %d bytes\n", (int)key
.dsize
);
407 print_asc(key
.dptr
, key
.dsize
);
408 printf("\ndata %d bytes\n", (int)dbuf
.dsize
);
409 print_data(dbuf
.dptr
, dbuf
.dsize
);
413 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
415 printf("key %d bytes: ", (int)key
.dsize
);
416 print_asc(key
.dptr
, key
.dsize
);
421 static int print_hexkey(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
423 printf("key %d bytes\n", (int)key
.dsize
);
424 print_data(key
.dptr
, key
.dsize
);
429 static int total_bytes
;
431 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
433 total_bytes
+= dbuf
.dsize
;
437 static void info_tdb(void)
441 if ((count
= tdb_traverse(tdb
, traverse_fn
, NULL
)) == -1)
442 printf("Error = %s\n", tdb_errorstr(tdb
));
444 printf("%d records totalling %d bytes\n", count
, total_bytes
);
447 static char *tdb_getline(const char *prompt
)
449 static char thisline
[1024];
451 fputs(prompt
, stdout
);
453 p
= fgets(thisline
, sizeof(thisline
)-1, stdin
);
454 if (p
) p
= strchr(p
, '\n');
456 return p
?thisline
:NULL
;
459 static int do_delete_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
462 return tdb_delete(the_tdb
, key
);
465 static void first_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
468 *pkey
= tdb_firstkey(the_tdb
);
470 dbuf
= tdb_fetch(the_tdb
, *pkey
);
471 if (!dbuf
.dptr
) terror("fetch failed");
473 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
477 static void next_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
480 *pkey
= tdb_nextkey(the_tdb
, *pkey
);
482 dbuf
= tdb_fetch(the_tdb
, *pkey
);
484 terror("fetch failed");
486 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
489 static int do_command(void)
491 COMMAND_TABLE
*ctp
= cmd_table
;
492 enum commands mycmd
= CMD_HELP
;
497 if (cmdname
&& strlen(cmdname
) == 0) {
501 cmd_len
= strlen(ctp
->name
);
502 if (strncmp(ctp
->name
,cmdname
,cmd_len
) == 0) {
526 /* all the rest require a open database */
529 terror("database not open");
536 tdb_traverse(tdb
, do_delete_fn
, NULL
);
540 tdb_traverse(tdb
, print_rec
, NULL
);
545 tdb_traverse(tdb
, print_rec
, NULL
);
549 insert_tdb(arg1
, arg1len
,arg2
,arg2len
);
553 move_rec(arg1
,arg1len
,arg2
);
557 store_tdb(arg1
,arg1len
,arg2
,arg2len
);
561 show_tdb(arg1
, arg1len
);
564 tdb_traverse(tdb
, print_key
, NULL
);
567 tdb_traverse(tdb
, print_hexkey
, NULL
);
571 delete_tdb(arg1
,arg1len
);
573 case CMD_LIST_HASH_FREE
:
577 tdb_printfreelist(tdb
);
584 first_record(tdb
, &iterate_kbuf
);
588 next_record(tdb
, &iterate_kbuf
);
598 * unhandled commands. cases included here to avoid compiler
608 static char *convert_string(char *instring
, size_t *sizep
)
615 outp
= inp
= instring
;
620 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
623 if (*inp
&& strchr("0123456789abcdefABCDEF",(int)*inp
)) {
627 *outp
++ = (char)strtol((const char *)temp
,NULL
,16);
640 int main(int argc
, char *argv
[])
642 cmdname
= (char *) "";
649 cmdname
= (char *) "open";
652 cmdname
= (char *) "";
659 /* Interactive mode */
660 while ((cmdname
= tdb_getline("tdb> "))) {
662 if ((arg1
= strchr((const char *)cmdname
,' ')) != NULL
) {
670 if ((*arg2
++ == '\\') && (*arg2
== ' ')) {
675 if (arg1
) arg1
= convert_string(arg1
,&arg1len
);
676 if (arg2
) arg2
= convert_string(arg2
,&arg2len
);
677 if (do_command()) break;
681 arg2
= convert_string(argv
[4],&arg2len
);
683 arg1
= convert_string(argv
[3],&arg1len
);
691 if (tdb
) tdb_close(tdb
);