2 Unix SMB/Netbios implementation.
4 Samba database functions
5 Copyright (C) Andrew Tridgell 1999-2000
6 Copyright (C) Paul `Rusty' Russell 2000
7 Copyright (C) Jeremy Allison 2000
8 Copyright (C) Andrew Esh 2001
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 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 *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");
282 static int print_conn_key(TDB_DATA key)
284 printf( "pid =%5d ", ((connections_key*)key.dptr)->pid);
285 printf( "cnum =%10d ", ((connections_key*)key.dptr)->cnum);
286 printf( "name =[%s]\n", ((connections_key*)key.dptr)->name);
290 static int print_conn_data(TDB_DATA dbuf)
292 printf( "pid =%5d ", ((connections_data*)dbuf.dptr)->pid);
293 printf( "cnum =%10d ", ((connections_data*)dbuf.dptr)->cnum);
294 printf( "name =[%s]\n", ((connections_data*)dbuf.dptr)->name);
296 printf( "uid =%5d ", ((connections_data*)dbuf.dptr)->uid);
297 printf( "addr =[%s]\n", ((connections_data*)dbuf.dptr)->addr);
298 printf( "gid =%5d ", ((connections_data*)dbuf.dptr)->gid);
299 printf( "machine=[%s]\n", ((connections_data*)dbuf.dptr)->machine);
300 printf( "start = %s\n", ctime(&((connections_data*)dbuf.dptr)->start));
304 static int print_rec(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
308 print_conn_data(dbuf);
311 printf("\nkey %d bytes\n", key.dsize);
312 print_asc(key.dptr, key.dsize);
313 printf("data %d bytes\n", dbuf.dsize);
314 print_data(dbuf.dptr, dbuf.dsize);
319 static int print_key(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
321 print_asc(key.dptr, key.dsize);
326 static int total_bytes;
328 static int traverse_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
330 total_bytes += dbuf.dsize;
334 static void info_tdb(void)
338 if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
339 printf("Error = %s\n", tdb_errorstr(tdb));
341 printf("%d records totalling %d bytes\n", count, total_bytes);
344 static char *tdb_getline(char *prompt)
346 static char line[1024];
348 fputs(prompt, stdout);
350 p = fgets(line, sizeof(line)-1, stdin);
351 if (p) p = strchr(p, '\n');
356 static int do_delete_fn(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA dbuf,
359 return tdb_delete(tdb, key);
362 static void first_record(TDB_CONTEXT *tdb, TDB_DATA *pkey)
365 *pkey = tdb_firstkey(tdb);
367 dbuf = tdb_fetch(tdb, *pkey);
368 if (!dbuf.dptr) terror("fetch failed");
369 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
370 print_rec(tdb, *pkey, dbuf, NULL);
373 static void next_record(TDB_CONTEXT *tdb, TDB_DATA *pkey)
376 *pkey = tdb_nextkey(tdb, *pkey);
378 dbuf = tdb_fetch(tdb, *pkey);
380 terror("fetch failed");
382 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
383 print_rec(tdb, *pkey, dbuf, NULL);
386 int main(int argc, char *argv[])
391 TDB_DATA iterate_kbuf;
394 static char tmp[1024];
395 sprintf(tmp, "open %s", argv[1]);
400 while ((line = tdb_getline("tdb> "))) {
404 if (line[0] == '!') {
409 if ((tok = strtok(line," "))==NULL) {
411 next_record(tdb, &iterate_kbuf);
414 if (strcmp(tok,"create") == 0) {
418 } else if (strcmp(tok,"open") == 0) {
421 } else if ((strcmp(tok, "q") == 0) ||
422 (strcmp(tok, "quit") == 0)) {
426 /* all the rest require a open database */
429 terror("database not open");
434 if (strcmp(tok,"insert") == 0) {
437 } else if (strcmp(tok,"store") == 0) {
440 } else if (strcmp(tok,"show") == 0) {
443 } else if (strcmp(tok,"erase") == 0) {
445 tdb_traverse(tdb, do_delete_fn, NULL);
446 } else if (strcmp(tok,"delete") == 0) {
449 } else if (strcmp(tok,"dump") == 0) {
451 tdb_traverse(tdb, print_rec, NULL);
452 } else if (strcmp(tok,"list") == 0) {
454 } else if (strcmp(tok, "free") == 0) {
455 tdb_printfreelist(tdb);
456 } else if (strcmp(tok,"info") == 0) {
458 } else if ( (strcmp(tok, "1") == 0) ||
459 (strcmp(tok, "first") == 0)) {
461 first_record(tdb, &iterate_kbuf);
462 } else if ((strcmp(tok, "n") == 0) ||
463 (strcmp(tok, "next") == 0)) {
464 next_record(tdb, &iterate_kbuf);
465 } else if ((strcmp(tok, "keys") == 0)) {
467 tdb_traverse(tdb, print_key, NULL);
473 if (tdb) tdb_close(tdb);