preparing for release of 2.2.3a
[Samba.git] / source / tdb / tdbtool.c
blobd1c199849b3db8398a0cf486cd7625422b002b49
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <time.h>
33 #include <sys/mman.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <ctype.h>
37 #include "tdb.h"
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 {
45 pid_t pid;
46 int cnum;
47 fstring name;
48 } connections_key;
50 typedef struct connections_data {
51 int magic;
52 pid_t pid;
53 int cnum;
54 uid_t uid;
55 gid_t gid;
56 char name[24];
57 char addr[24];
58 char machine[128];
59 time_t start;
60 } 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)
68 int i;
70 /* We're probably printing ASCII strings so don't try to display
71 the trailing NULL character. */
73 if (buf[len - 1] == 0)
74 len--;
76 for (i=0;i<len;i++)
77 printf("%c",isprint(buf[i])?buf[i]:'.');
80 static void print_data(unsigned char *buf,int len)
82 int i=0;
83 if (len<=0) return;
84 printf("[%03X] ",i);
85 for (i=0;i<len;) {
86 printf("%02X ",(int)buf[i]);
87 i++;
88 if (i%8 == 0) printf(" ");
89 if (i%16 == 0) {
90 print_asc(&buf[i-16],8); printf(" ");
91 print_asc(&buf[i-8],8); printf("\n");
92 if (i<len) printf("[%03X] ",i);
95 if (i%16) {
96 int n;
98 n = 16 - (i%16);
99 printf(" ");
100 if (n>8) printf(" ");
101 while (n--) printf(" ");
103 n = i%16;
104 if (n > 8) n = 8;
105 print_asc(&buf[i-(i%16)],n); printf(" ");
106 n = (i%16) - n;
107 if (n>0) print_asc(&buf[i-n],n);
108 printf("\n");
112 static void help(void)
114 printf("
115 tdbtool:
116 create dbname : create a database
117 open dbname : open an existing database
118 erase : erase the database
119 dump : dump the database as strings
120 insert key data : insert a record
121 store key data : store a record (replace)
122 show key : show a record by key
123 delete key : delete a record by key
124 list : print the database hash table and freelist
125 free : print the database freelist
126 1 | first : print the first record
127 n | next : print the next record
128 q | quit : terminate
129 \\n : repeat 'next' command
133 static void terror(char *why)
135 printf("%s\n", why);
138 static char *get_token(int startover)
140 static char tmp[1024];
141 static char *cont = NULL;
142 char *insert, *start;
143 char *k = strtok(NULL, " ");
145 if (!k)
146 return NULL;
148 if (startover)
149 start = tmp;
150 else
151 start = cont;
153 strcpy(start, k);
154 insert = start + strlen(start) - 1;
155 while (*insert == '\\') {
156 *insert++ = ' ';
157 k = strtok(NULL, " ");
158 if (!k)
159 break;
160 strcpy(insert, k);
161 insert = start + strlen(start) - 1;
164 /* Get ready for next call */
165 cont = start + strlen(start) + 1;
166 return start;
169 static void create_tdb(void)
171 char *tok = get_token(1);
172 if (!tok) {
173 help();
174 return;
176 if (tdb) tdb_close(tdb);
177 tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
178 O_RDWR | O_CREAT | O_TRUNC, 0600);
179 if (!tdb) {
180 printf("Could not create %s: %s\n", tok, strerror(errno));
184 static void open_tdb(void)
186 char *tok = get_token(1);
187 if (!tok) {
188 help();
189 return;
191 if (tdb) tdb_close(tdb);
192 tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
193 if (!tdb) {
194 printf("Could not open %s: %s\n", tok, strerror(errno));
198 static void insert_tdb(void)
200 char *k = get_token(1);
201 char *d = get_token(0);
202 TDB_DATA key, dbuf;
204 if (!k || !d) {
205 help();
206 return;
209 key.dptr = k;
210 key.dsize = strlen(k)+1;
211 dbuf.dptr = d;
212 dbuf.dsize = strlen(d)+1;
214 if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) {
215 terror("insert failed");
219 static void store_tdb(void)
221 char *k = get_token(1);
222 char *d = get_token(0);
223 TDB_DATA key, dbuf;
225 if (!k || !d) {
226 help();
227 return;
230 key.dptr = k;
231 key.dsize = strlen(k)+1;
232 dbuf.dptr = d;
233 dbuf.dsize = strlen(d)+1;
235 printf("Storing key:\n");
236 print_rec(tdb, key, dbuf, NULL);
238 if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) {
239 terror("store failed");
243 static void show_tdb(void)
245 char *k = get_token(1);
246 TDB_DATA key, dbuf;
248 if (!k) {
249 help();
250 return;
253 key.dptr = k;
254 key.dsize = strlen(k)+1;
256 dbuf = tdb_fetch(tdb, key);
257 if (!dbuf.dptr) {
258 terror("fetch failed");
259 return;
261 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
262 print_rec(tdb, key, dbuf, NULL);
265 static void delete_tdb(void)
267 char *k = get_token(1);
268 TDB_DATA key;
270 if (!k) {
271 help();
272 return;
275 key.dptr = k;
276 key.dsize = strlen(k)+1;
278 if (tdb_delete(tdb, key) != 0) {
279 terror("delete failed");
283 #if 0
284 static int print_conn_key(TDB_DATA key)
286 printf( "pid =%5d ", ((connections_key*)key.dptr)->pid);
287 printf( "cnum =%10d ", ((connections_key*)key.dptr)->cnum);
288 printf( "name =[%s]\n", ((connections_key*)key.dptr)->name);
289 return 0;
292 static int print_conn_data(TDB_DATA dbuf)
294 printf( "pid =%5d ", ((connections_data*)dbuf.dptr)->pid);
295 printf( "cnum =%10d ", ((connections_data*)dbuf.dptr)->cnum);
296 printf( "name =[%s]\n", ((connections_data*)dbuf.dptr)->name);
298 printf( "uid =%5d ", ((connections_data*)dbuf.dptr)->uid);
299 printf( "addr =[%s]\n", ((connections_data*)dbuf.dptr)->addr);
300 printf( "gid =%5d ", ((connections_data*)dbuf.dptr)->gid);
301 printf( "machine=[%s]\n", ((connections_data*)dbuf.dptr)->machine);
302 printf( "start = %s\n", ctime(&((connections_data*)dbuf.dptr)->start));
303 return 0;
305 #endif
307 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
309 #if 0
310 print_conn_key(key);
311 print_conn_data(dbuf);
312 return 0;
313 #else
314 printf("\nkey %d bytes\n", key.dsize);
315 print_asc(key.dptr, key.dsize);
316 printf("\ndata %d bytes\n", dbuf.dsize);
317 print_data(dbuf.dptr, dbuf.dsize);
318 return 0;
319 #endif
322 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
324 print_asc(key.dptr, key.dsize);
325 printf("\n");
326 return 0;
329 static int total_bytes;
331 static int traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
333 total_bytes += dbuf.dsize;
334 return 0;
337 static void info_tdb(void)
339 int count;
340 total_bytes = 0;
341 if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
342 printf("Error = %s\n", tdb_errorstr(tdb));
343 else
344 printf("%d records totalling %d bytes\n", count, total_bytes);
347 static char *tdb_getline(char *prompt)
349 static char line[1024];
350 char *p;
351 fputs(prompt, stdout);
352 line[0] = 0;
353 p = fgets(line, sizeof(line)-1, stdin);
354 if (p) p = strchr(p, '\n');
355 if (p) *p = 0;
356 return p?line:NULL;
359 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
360 void *state)
362 return tdb_delete(the_tdb, key);
365 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
367 TDB_DATA dbuf;
368 *pkey = tdb_firstkey(the_tdb);
370 dbuf = tdb_fetch(the_tdb, *pkey);
371 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);
376 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
378 TDB_DATA dbuf;
379 *pkey = tdb_nextkey(the_tdb, *pkey);
381 dbuf = tdb_fetch(the_tdb, *pkey);
382 if (!dbuf.dptr)
383 terror("fetch failed");
384 else
385 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
386 print_rec(the_tdb, *pkey, dbuf, NULL);
389 int main(int argc, char *argv[])
391 int bIterate = 0;
392 char *line;
393 char *tok;
394 TDB_DATA iterate_kbuf;
396 if (argv[1]) {
397 static char tmp[1024];
398 sprintf(tmp, "open %s", argv[1]);
399 tok=strtok(tmp," ");
400 open_tdb();
403 while ((line = tdb_getline("tdb> "))) {
405 /* Shell command */
407 if (line[0] == '!') {
408 system(line + 1);
409 continue;
412 if ((tok = strtok(line," "))==NULL) {
413 if (bIterate)
414 next_record(tdb, &iterate_kbuf);
415 continue;
417 if (strcmp(tok,"create") == 0) {
418 bIterate = 0;
419 create_tdb();
420 continue;
421 } else if (strcmp(tok,"open") == 0) {
422 open_tdb();
423 continue;
424 } else if ((strcmp(tok, "q") == 0) ||
425 (strcmp(tok, "quit") == 0)) {
426 break;
429 /* all the rest require a open database */
430 if (!tdb) {
431 bIterate = 0;
432 terror("database not open");
433 help();
434 continue;
437 if (strcmp(tok,"insert") == 0) {
438 bIterate = 0;
439 insert_tdb();
440 } else if (strcmp(tok,"store") == 0) {
441 bIterate = 0;
442 store_tdb();
443 } else if (strcmp(tok,"show") == 0) {
444 bIterate = 0;
445 show_tdb();
446 } else if (strcmp(tok,"erase") == 0) {
447 bIterate = 0;
448 tdb_traverse(tdb, do_delete_fn, NULL);
449 } else if (strcmp(tok,"delete") == 0) {
450 bIterate = 0;
451 delete_tdb();
452 } else if (strcmp(tok,"dump") == 0) {
453 bIterate = 0;
454 tdb_traverse(tdb, print_rec, NULL);
455 } else if (strcmp(tok,"list") == 0) {
456 tdb_dump_all(tdb);
457 } else if (strcmp(tok, "free") == 0) {
458 tdb_printfreelist(tdb);
459 } else if (strcmp(tok,"info") == 0) {
460 info_tdb();
461 } else if ( (strcmp(tok, "1") == 0) ||
462 (strcmp(tok, "first") == 0)) {
463 bIterate = 1;
464 first_record(tdb, &iterate_kbuf);
465 } else if ((strcmp(tok, "n") == 0) ||
466 (strcmp(tok, "next") == 0)) {
467 next_record(tdb, &iterate_kbuf);
468 } else if ((strcmp(tok, "keys") == 0)) {
469 bIterate = 0;
470 tdb_traverse(tdb, print_key, NULL);
471 } else {
472 help();
476 if (tdb) tdb_close(tdb);
478 return 0;