if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / tdb / tdbtool.c
blob3ae955901c2cff88d732ae1c6bf2c4ca9269176d
1 /*
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.
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include <time.h>
32 #include <sys/mman.h>
33 #include <sys/stat.h>
34 #include <sys/time.h>
35 #include <ctype.h>
36 #include "tdb.h"
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 {
44 pid_t pid;
45 int cnum;
46 fstring name;
47 } connections_key;
49 typedef struct connections_data {
50 int magic;
51 pid_t pid;
52 int cnum;
53 uid_t uid;
54 gid_t gid;
55 char name[24];
56 char addr[24];
57 char machine[128];
58 time_t start;
59 } 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)
67 int i;
69 /* We're probably printing ASCII strings so don't try to display
70 the trailing NULL character. */
72 if (buf[len - 1] == 0)
73 len--;
75 for (i=0;i<len;i++)
76 printf("%c",isprint(buf[i])?buf[i]:'.');
79 static void print_data(unsigned char *buf,int len)
81 int i=0;
82 if (len<=0) return;
83 printf("[%03X] ",i);
84 for (i=0;i<len;) {
85 printf("%02X ",(int)buf[i]);
86 i++;
87 if (i%8 == 0) printf(" ");
88 if (i%16 == 0) {
89 print_asc(&buf[i-16],8); printf(" ");
90 print_asc(&buf[i-8],8); printf("\n");
91 if (i<len) printf("[%03X] ",i);
94 if (i%16) {
95 int n;
97 n = 16 - (i%16);
98 printf(" ");
99 if (n>8) printf(" ");
100 while (n--) printf(" ");
102 n = i%16;
103 if (n > 8) n = 8;
104 print_asc(&buf[i-(i%16)],n); printf(" ");
105 n = (i%16) - n;
106 if (n>0) print_asc(&buf[i-n],n);
107 printf("\n");
111 static void help(void)
113 printf(" \n \
114 tdbtool: \n \
115 create dbname : create a database \n \
116 open dbname : open an existing database \n \
117 erase : erase the database \n \
118 dump : dump the database as strings \n \
119 insert key data : insert a record \n \
120 store key data : store a record (replace) \n \
121 show key : show a record by key \n \
122 delete key : delete a record by key \n \
123 list : print the database hash table and freelist \n \
124 free : print the database freelist \n \
125 1 | first : print the first record \n \
126 n | next : print the next record \n \
127 q | quit : terminate \n \
128 \\n : repeat 'next' command \n \
132 static void terror(char *why)
134 printf("%s\n", 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, " ");
144 if (!k)
145 return NULL;
147 if (startover)
148 start = tmp;
149 else
150 start = cont;
152 strcpy(start, k);
153 insert = start + strlen(start) - 1;
154 while (*insert == '\\') {
155 *insert++ = ' ';
156 k = strtok(NULL, " ");
157 if (!k)
158 break;
159 strcpy(insert, k);
160 insert = start + strlen(start) - 1;
163 /* Get ready for next call */
164 cont = start + strlen(start) + 1;
165 return start;
168 static void create_tdb(void)
170 char *tok = get_token(1);
171 if (!tok) {
172 help();
173 return;
175 if (tdb) tdb_close(tdb);
176 tdb = tdb_open(tok, 0, TDB_CLEAR_IF_FIRST,
177 O_RDWR | O_CREAT | O_TRUNC, 0600);
178 if (!tdb) {
179 printf("Could not create %s: %s\n", tok, strerror(errno));
183 static void open_tdb(void)
185 char *tok = get_token(1);
186 if (!tok) {
187 help();
188 return;
190 if (tdb) tdb_close(tdb);
191 tdb = tdb_open(tok, 0, 0, O_RDWR, 0600);
192 if (!tdb) {
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);
201 TDB_DATA key, dbuf;
203 if (!k || !d) {
204 help();
205 return;
208 key.dptr = k;
209 key.dsize = strlen(k)+1;
210 dbuf.dptr = d;
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);
222 TDB_DATA key, dbuf;
224 if (!k || !d) {
225 help();
226 return;
229 key.dptr = k;
230 key.dsize = strlen(k)+1;
231 dbuf.dptr = d;
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);
245 TDB_DATA key, dbuf;
247 if (!k) {
248 help();
249 return;
252 key.dptr = k;
253 key.dsize = strlen(k)+1;
255 dbuf = tdb_fetch(tdb, key);
256 if (!dbuf.dptr) {
257 terror("fetch failed");
258 return;
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);
267 TDB_DATA key;
269 if (!k) {
270 help();
271 return;
274 key.dptr = k;
275 key.dsize = strlen(k)+1;
277 if (tdb_delete(tdb, key) != 0) {
278 terror("delete failed");
282 #if 0
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);
288 return 0;
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));
302 return 0;
304 #endif
306 static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
308 #if 0
309 print_conn_key(key);
310 print_conn_data(dbuf);
311 return 0;
312 #else
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);
317 return 0;
318 #endif
321 static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state)
323 print_asc(key.dptr, key.dsize);
324 printf("\n");
325 return 0;
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;
333 return 0;
336 static void info_tdb(void)
338 int count;
339 total_bytes = 0;
340 if ((count = tdb_traverse(tdb, traverse_fn, NULL) == -1))
341 printf("Error = %s\n", tdb_errorstr(tdb));
342 else
343 printf("%d records totalling %d bytes\n", count, total_bytes);
346 static char *tdb_getline(char *prompt)
348 static char line[1024];
349 char *p;
350 fputs(prompt, stdout);
351 line[0] = 0;
352 p = fgets(line, sizeof(line)-1, stdin);
353 if (p) p = strchr(p, '\n');
354 if (p) *p = 0;
355 return p?line:NULL;
358 static int do_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
359 void *state)
361 return tdb_delete(the_tdb, key);
364 static void first_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
366 TDB_DATA dbuf;
367 *pkey = tdb_firstkey(the_tdb);
369 dbuf = tdb_fetch(the_tdb, *pkey);
370 if (!dbuf.dptr) terror("fetch failed");
371 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
372 print_rec(the_tdb, *pkey, dbuf, NULL);
375 static void next_record(TDB_CONTEXT *the_tdb, TDB_DATA *pkey)
377 TDB_DATA dbuf;
378 *pkey = tdb_nextkey(the_tdb, *pkey);
380 dbuf = tdb_fetch(the_tdb, *pkey);
381 if (!dbuf.dptr)
382 terror("fetch failed");
383 else
384 /* printf("%s : %*.*s\n", k, (int)dbuf.dsize, (int)dbuf.dsize, dbuf.dptr); */
385 print_rec(the_tdb, *pkey, dbuf, NULL);
388 int main(int argc, char *argv[])
390 int bIterate = 0;
391 char *line;
392 char *tok;
393 TDB_DATA iterate_kbuf;
395 if (argv[1]) {
396 static char tmp[1024];
397 sprintf(tmp, "open %s", argv[1]);
398 tok=strtok(tmp," ");
399 open_tdb();
402 while ((line = tdb_getline("tdb> "))) {
404 /* Shell command */
406 if (line[0] == '!') {
407 system(line + 1);
408 continue;
411 if ((tok = strtok(line," "))==NULL) {
412 if (bIterate)
413 next_record(tdb, &iterate_kbuf);
414 continue;
416 if (strcmp(tok,"create") == 0) {
417 bIterate = 0;
418 create_tdb();
419 continue;
420 } else if (strcmp(tok,"open") == 0) {
421 open_tdb();
422 continue;
423 } else if ((strcmp(tok, "q") == 0) ||
424 (strcmp(tok, "quit") == 0)) {
425 break;
428 /* all the rest require a open database */
429 if (!tdb) {
430 bIterate = 0;
431 terror("database not open");
432 help();
433 continue;
436 if (strcmp(tok,"insert") == 0) {
437 bIterate = 0;
438 insert_tdb();
439 } else if (strcmp(tok,"store") == 0) {
440 bIterate = 0;
441 store_tdb();
442 } else if (strcmp(tok,"show") == 0) {
443 bIterate = 0;
444 show_tdb();
445 } else if (strcmp(tok,"erase") == 0) {
446 bIterate = 0;
447 tdb_traverse(tdb, do_delete_fn, NULL);
448 } else if (strcmp(tok,"delete") == 0) {
449 bIterate = 0;
450 delete_tdb();
451 } else if (strcmp(tok,"dump") == 0) {
452 bIterate = 0;
453 tdb_traverse(tdb, print_rec, NULL);
454 } else if (strcmp(tok,"list") == 0) {
455 tdb_dump_all(tdb);
456 } else if (strcmp(tok, "free") == 0) {
457 tdb_printfreelist(tdb);
458 } else if (strcmp(tok,"info") == 0) {
459 info_tdb();
460 } else if ( (strcmp(tok, "1") == 0) ||
461 (strcmp(tok, "first") == 0)) {
462 bIterate = 1;
463 first_record(tdb, &iterate_kbuf);
464 } else if ((strcmp(tok, "n") == 0) ||
465 (strcmp(tok, "next") == 0)) {
466 next_record(tdb, &iterate_kbuf);
467 } else if ((strcmp(tok, "keys") == 0)) {
468 bIterate = 0;
469 tdb_traverse(tdb, print_key, NULL);
470 } else {
471 help();
475 if (tdb) tdb_close(tdb);
477 return 0;