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.
39 /* a tdb tool for manipulating a tdb database */
41 #define FSTRING_LEN 256
42 typedef char fstring
[FSTRING_LEN
];
44 typedef struct connections_key
{
50 typedef struct connections_data
{
62 static TDB_CONTEXT
*tdb
;
64 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
);
66 static void print_asc(unsigned char *buf
,int len
)
70 /* We're probably printing ASCII strings so don't try to display
71 the trailing NULL character. */
73 if (buf
[len
- 1] == 0)
77 printf("%c",isprint(buf
[i
])?buf
[i
]:'.');
80 static void print_data(unsigned char *buf
,int len
)
86 printf("%02X ",(int)buf
[i
]);
88 if (i
%8 == 0) printf(" ");
90 print_asc(&buf
[i
-16],8); printf(" ");
91 print_asc(&buf
[i
-8],8); printf("\n");
92 if (i
<len
) printf("[%03X] ",i
);
100 if (n
>8) printf(" ");
101 while (n
--) printf(" ");
105 print_asc(&buf
[i
-(i
%16)],n
); printf(" ");
107 if (n
>0) print_asc(&buf
[i
-n
],n
);
112 static void help(void)
116 " create dbname : create a database\n"
117 " open dbname : open an existing database\n"
118 " erase : erase the database\n"
119 " dump : dump the database as strings\n"
120 " insert key data : insert a record\n"
121 " move key file : move a record to a destination tdb\n"
122 " store key data : store a record (replace)\n"
123 " show key : show a record by key\n"
124 " delete key : delete a record by key\n"
125 " list : print the database hash table and freelist\n"
126 " free : print the database freelist\n"
127 " 1 | first : print the first record\n"
128 " n | next : print the next record\n"
129 " q | quit : terminate\n"
130 " \\n : repeat 'next' command\n"
134 static void terror(char *why
)
139 static char *get_token(int startover
)
141 static char tmp
[1024];
142 static char *cont
= NULL
;
143 char *insert
, *start
;
144 char *k
= strtok(NULL
, " ");
155 insert
= start
+ strlen(start
) - 1;
156 while (*insert
== '\\') {
158 k
= strtok(NULL
, " ");
162 insert
= start
+ strlen(start
) - 1;
165 /* Get ready for next call */
166 cont
= start
+ strlen(start
) + 1;
170 static void create_tdb(void)
172 char *tok
= get_token(1);
177 if (tdb
) tdb_close(tdb
);
178 tdb
= tdb_open(tok
, 0, TDB_CLEAR_IF_FIRST
,
179 O_RDWR
| O_CREAT
| O_TRUNC
, 0600);
181 printf("Could not create %s: %s\n", tok
, strerror(errno
));
185 static void open_tdb(void)
187 char *tok
= get_token(1);
192 if (tdb
) tdb_close(tdb
);
193 tdb
= tdb_open(tok
, 0, 0, O_RDWR
, 0600);
195 printf("Could not open %s: %s\n", tok
, strerror(errno
));
199 static void insert_tdb(void)
201 char *k
= get_token(1);
202 char *d
= get_token(0);
211 key
.dsize
= strlen(k
)+1;
213 dbuf
.dsize
= strlen(d
)+1;
215 if (tdb_store(tdb
, key
, dbuf
, TDB_INSERT
) == -1) {
216 terror("insert failed");
220 static void store_tdb(void)
222 char *k
= get_token(1);
223 char *d
= get_token(0);
232 key
.dsize
= strlen(k
)+1;
234 dbuf
.dsize
= strlen(d
)+1;
236 printf("Storing key:\n");
237 print_rec(tdb
, key
, dbuf
, NULL
);
239 if (tdb_store(tdb
, key
, dbuf
, TDB_REPLACE
) == -1) {
240 terror("store failed");
244 static void show_tdb(void)
246 char *k
= get_token(1);
255 key
.dsize
= strlen(k
)+1;
257 dbuf
= tdb_fetch(tdb
, key
);
259 /* maybe it is non-NULL terminated key? */
260 key
.dsize
= strlen(k
);
261 dbuf
= tdb_fetch(tdb
, key
);
264 terror("fetch failed");
269 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
270 print_rec(tdb
, key
, dbuf
, NULL
);
277 static void delete_tdb(void)
279 char *k
= get_token(1);
288 key
.dsize
= strlen(k
)+1;
290 if (tdb_delete(tdb
, key
) != 0) {
291 terror("delete failed");
295 static void move_rec(void)
297 char *k
= get_token(1);
298 char *file
= get_token(0);
300 TDB_CONTEXT
*dst_tdb
;
308 terror("need destination tdb name");
313 key
.dsize
= strlen(k
)+1;
315 dbuf
= tdb_fetch(tdb
, key
);
317 /* maybe it is non-NULL terminated key? */
318 key
.dsize
= strlen(k
);
319 dbuf
= tdb_fetch(tdb
, key
);
322 terror("fetch failed");
327 print_rec(tdb
, key
, dbuf
, NULL
);
329 dst_tdb
= tdb_open(file
, 0, 0, O_RDWR
, 0600);
331 terror("unable to open destination tdb");
335 if ( tdb_store( dst_tdb
, key
, dbuf
, TDB_REPLACE
) == -1 ) {
336 terror("failed to move record");
339 printf("record moved\n");
341 tdb_close( dst_tdb
);
347 static int print_conn_key(TDB_DATA key
)
349 printf( "pid =%5d ", ((connections_key
*)key
.dptr
)->pid
);
350 printf( "cnum =%10d ", ((connections_key
*)key
.dptr
)->cnum
);
351 printf( "name =[%s]\n", ((connections_key
*)key
.dptr
)->name
);
355 static int print_conn_data(TDB_DATA dbuf
)
357 printf( "pid =%5d ", ((connections_data
*)dbuf
.dptr
)->pid
);
358 printf( "cnum =%10d ", ((connections_data
*)dbuf
.dptr
)->cnum
);
359 printf( "name =[%s]\n", ((connections_data
*)dbuf
.dptr
)->name
);
361 printf( "uid =%5d ", ((connections_data
*)dbuf
.dptr
)->uid
);
362 printf( "addr =[%s]\n", ((connections_data
*)dbuf
.dptr
)->addr
);
363 printf( "gid =%5d ", ((connections_data
*)dbuf
.dptr
)->gid
);
364 printf( "machine=[%s]\n", ((connections_data
*)dbuf
.dptr
)->machine
);
365 printf( "start = %s\n", ctime(&((connections_data
*)dbuf
.dptr
)->start
));
370 static int print_rec(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
374 print_conn_data(dbuf
);
377 printf("\nkey %d bytes\n", key
.dsize
);
378 print_asc(key
.dptr
, key
.dsize
);
379 printf("\ndata %d bytes\n", dbuf
.dsize
);
380 print_data(dbuf
.dptr
, dbuf
.dsize
);
385 static int print_key(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
387 print_asc(key
.dptr
, key
.dsize
);
392 static int total_bytes
;
394 static int traverse_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
, void *state
)
396 total_bytes
+= dbuf
.dsize
;
400 static void info_tdb(void)
404 if ((count
= tdb_traverse(tdb
, traverse_fn
, NULL
) == -1))
405 printf("Error = %s\n", tdb_errorstr(tdb
));
407 printf("%d records totalling %d bytes\n", count
, total_bytes
);
410 static char *tdb_getline(char *prompt
)
412 static char line
[1024];
414 fputs(prompt
, stdout
);
416 p
= fgets(line
, sizeof(line
)-1, stdin
);
417 if (p
) p
= strchr(p
, '\n');
422 static int do_delete_fn(TDB_CONTEXT
*the_tdb
, TDB_DATA key
, TDB_DATA dbuf
,
425 return tdb_delete(the_tdb
, key
);
428 static void first_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
431 *pkey
= tdb_firstkey(the_tdb
);
433 dbuf
= tdb_fetch(the_tdb
, *pkey
);
434 if (!dbuf
.dptr
) terror("fetch failed");
436 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
437 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
441 static void next_record(TDB_CONTEXT
*the_tdb
, TDB_DATA
*pkey
)
444 *pkey
= tdb_nextkey(the_tdb
, *pkey
);
446 dbuf
= tdb_fetch(the_tdb
, *pkey
);
448 terror("fetch failed");
450 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
451 print_rec(the_tdb
, *pkey
, dbuf
, NULL
);
454 int main(int argc
, char *argv
[])
459 TDB_DATA iterate_kbuf
;
462 static char tmp
[1024];
463 sprintf(tmp
, "open %s", argv
[1]);
468 while ((line
= tdb_getline("tdb> "))) {
472 if (line
[0] == '!') {
477 if ((tok
= strtok(line
," "))==NULL
) {
479 next_record(tdb
, &iterate_kbuf
);
482 if (strcmp(tok
,"create") == 0) {
486 } else if (strcmp(tok
,"open") == 0) {
489 } else if ((strcmp(tok
, "q") == 0) ||
490 (strcmp(tok
, "quit") == 0)) {
494 /* all the rest require a open database */
497 terror("database not open");
502 if (strcmp(tok
,"insert") == 0) {
505 } else if (strcmp(tok
,"store") == 0) {
508 } else if (strcmp(tok
,"show") == 0) {
511 } else if (strcmp(tok
,"erase") == 0) {
513 tdb_traverse(tdb
, do_delete_fn
, NULL
);
514 } else if (strcmp(tok
,"delete") == 0) {
517 } else if (strcmp(tok
,"dump") == 0) {
519 tdb_traverse(tdb
, print_rec
, NULL
);
520 } else if (strcmp(tok
,"move") == 0) {
523 } else if (strcmp(tok
,"list") == 0) {
525 } else if (strcmp(tok
, "free") == 0) {
526 tdb_printfreelist(tdb
);
527 } else if (strcmp(tok
,"info") == 0) {
529 } else if ( (strcmp(tok
, "1") == 0) ||
530 (strcmp(tok
, "first") == 0)) {
532 first_record(tdb
, &iterate_kbuf
);
533 } else if ((strcmp(tok
, "n") == 0) ||
534 (strcmp(tok
, "next") == 0)) {
535 next_record(tdb
, &iterate_kbuf
);
536 } else if ((strcmp(tok
, "keys") == 0)) {
538 tdb_traverse(tdb
, print_key
, NULL
);
544 if (tdb
) tdb_close(tdb
);