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.
38 /* a tdb tool for manipulating a tdb database */
40 #define FSTRING_LEN 256
41 typedef char fstring[FSTRING_LEN];
43 typedef struct connections_key {
49 typedef struct connections_data {
61 static TDB_CONTEXT *tdb;
63 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);
65 static void print_asc(unsigned char *buf,int len)
69 /* We're probably printing ASCII strings so don't try to display
70 the trailing NULL character. */
72 if (buf[len - 1] == 0)
76 printf("%c",isprint(buf[i])?buf[i]:'.');
79 static void print_data(unsigned char *buf,int len)
85 printf("%02X ",(int)buf[i]);
87 if (i%8 == 0) printf(" ");
89 print_asc(&buf[i-16],8); printf(" ");
90 print_asc(&buf[i-8],8); printf("\n");
91 if (i<len) printf("[%03X] ",i);
100 while (n--) printf(" ");
104 print_asc(&buf[i-(i%16)],n); printf(" ");
106 if (n>0) print_asc(&buf[i-n],n);
111 static void help(void)
115 create dbname : create a database
116 open dbname : open an existing database
117 erase : erase the database
118 dump : dump the database as strings
119 insert key data : insert a record
120 store key data : store a record (replace)
121 show key : show a record by key
122 delete key : delete a record by key
123 list : print the database hash table and freelist
124 free : print the database freelist
125 1 | first : print the first record
126 n | next : print the next record
128 \\n : repeat 'next' command
132 static void terror(char *why)
137 static char *get_token(int startover)
139 static char tmp[1024];
140 static char *cont = NULL;
141 char *insert, *start;
142 char *k = strtok(NULL, " ");
153 insert = start + strlen(start) - 1;
154 while (*insert == '\\') {
156 k = strtok(NULL, " ");
160 insert = start + strlen(start) - 1;
163 /* Get ready for next call */
164 cont = start + strlen(start) + 1;
168 static void create_tdb(void)
170 char *tok = get_token(1);
175 if (tdb) tdb_close(tdb);
176 tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
177 O_RDWR | O_CREAT | O_TRUNC, 0600);
179 printf("Could not create %s: %s\n", tok, strerror(errno));
183 static void open_tdb(void)
185 char *tok = get_token(1);
190 if (tdb) tdb_close(tdb);
191 tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
193 printf("Could not open %s: %s\n", tok, strerror(errno));
197 static void insert_tdb(void)
199 char *k = get_token(1);
200 char *d = get_token(0);
209 key.dsize = strlen(k)+1;
211 dbuf.dsize = strlen(d)+1;
213 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
214 terror("insert failed");
218 static void store_tdb(void)
220 char *k = get_token(1);
221 char *d = get_token(0);
230 key.dsize = strlen(k)+1;
232 dbuf.dsize = strlen(d)+1;
234 printf("Storing key:\n");
235 print_rec(tdb, key, dbuf, NULL);
237 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
238 terror("store failed");
242 static void show_tdb(void)
244 char *k = get_token(1);
253 key.dsize = strlen(k)+1;
255 dbuf = tdb_fetch(tdb, key);
257 terror("fetch failed");
260 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
261 print_rec(tdb, key, dbuf, NULL);
264 static void delete_tdb(void)
266 char *k = get_token(1);
275 key.dsize = strlen(k)+1;
277 if (tdb_delete(tdb, key) != 0) {
278 terror("delete failed");
283 static int print_conn_key(TDB_DATA key)
285 printf( "pid =%5d ", ((connections_key*)key.dptr)->pid);
286 printf( "cnum =%10d ", ((connections_key*)key.dptr)->cnum);
287 printf( "name =[%s]\n", ((connections_key*)key.dptr)->name);
291 static int print_conn_data(TDB_DATA dbuf)
293 printf( "pid =%5d ", ((connections_data*)dbuf.dptr)->pid);
294 printf( "cnum =%10d ", ((connections_data*)dbuf.dptr)->cnum);
295 printf( "name =[%s]\n", ((connections_data*)dbuf.dptr)->name);
297 printf( "uid =%5d ", ((connections_data*)dbuf.dptr)->uid);
298 printf( "addr =[%s]\n", ((connections_data*)dbuf.dptr)->addr);
299 printf( "gid =%5d ", ((connections_data*)dbuf.dptr)->gid);
300 printf( "machine=[%s]\n", ((connections_data*)dbuf.dptr)->machine);
301 printf( "start = %s\n", ctime(&((connections_data*)dbuf.dptr)->start));
306 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
310 print_conn_data(dbuf);
313 printf("\nkey %d bytes\n", key.dsize);
314 print_asc(key.dptr, key.dsize);
315 printf("\ndata %d bytes\n", dbuf.dsize);
316 print_data(dbuf.dptr, dbuf.dsize);
321 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
323 print_asc(key.dptr, key.dsize);
328 static int total_bytes;
330 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
332 total_bytes += dbuf.dsize;
336 static void info_tdb(void)
340 if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
341 printf("Error = %s\n", tdb_errorstr(tdb));
343 printf("%d records totalling %d bytes\n", count, total_bytes);
346 static char *tdb_getline(char *prompt)
348 static char line[1024];
350 fputs(prompt, stdout);
352 p = fgets(line, sizeof(line)-1, stdin);
353 if (p) p = strchr(p, '\n');
358 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
361 return tdb_delete(the_tdb, key);
364 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
367 *pkey = tdb_firstkey(the_tdb);
369 dbuf = tdb_fetch(the_tdb, *pkey);
370 if (!dbuf.dptr) terror("fetch failed");
372 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
373 print_rec(the_tdb, *pkey, dbuf, NULL);
377 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
380 *pkey = tdb_nextkey(the_tdb, *pkey);
382 dbuf = tdb_fetch(the_tdb, *pkey);
384 terror("fetch failed");
386 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
387 print_rec(the_tdb, *pkey, dbuf, NULL);
390 int main(int argc, char *argv[])
395 TDB_DATA iterate_kbuf;
398 static char tmp[1024];
399 sprintf(tmp, "open %s", argv[1]);
404 while ((line = tdb_getline("tdb> "))) {
408 if (line[0] == '!') {
413 if ((tok = strtok(line," "))==NULL) {
415 next_record(tdb, &iterate_kbuf);
418 if (strcmp(tok,"create") == 0) {
422 } else if (strcmp(tok,"open") == 0) {
425 } else if ((strcmp(tok, "q") == 0) ||
426 (strcmp(tok, "quit") == 0)) {
430 /* all the rest require a open database */
433 terror("database not open");
438 if (strcmp(tok,"insert") == 0) {
441 } else if (strcmp(tok,"store") == 0) {
444 } else if (strcmp(tok,"show") == 0) {
447 } else if (strcmp(tok,"erase") == 0) {
449 tdb_traverse(tdb, do_delete_fn, NULL);
450 } else if (strcmp(tok,"delete") == 0) {
453 } else if (strcmp(tok,"dump") == 0) {
455 tdb_traverse(tdb, print_rec, NULL);
456 } else if (strcmp(tok,"list") == 0) {
458 } else if (strcmp(tok, "free") == 0) {
459 tdb_printfreelist(tdb);
460 } else if (strcmp(tok,"info") == 0) {
462 } else if ( (strcmp(tok, "1") == 0) ||
463 (strcmp(tok, "first") == 0)) {
465 first_record(tdb, &iterate_kbuf);
466 } else if ((strcmp(tok, "n") == 0) ||
467 (strcmp(tok, "next") == 0)) {
468 next_record(tdb, &iterate_kbuf);
469 } else if ((strcmp(tok, "keys") == 0)) {
471 tdb_traverse(tdb, print_key, NULL);
477 if (tdb) tdb_close(tdb);