[PATCH] Trivial extension to tdbtool to make it more script-friendly

Tim Small tim at seoss.co.uk
Tue Aug 9 04:33:57 MDT 2011


Hi,

Various tools make use of tdb data files (e.g. pppd), and tdbtool is
packaged with Debian etc. but there's no easy way of retrieving tdb
data entries from a shell script or similar.  This patch adds a 'get'
argument to tdbtool:

tim at ermintrude:~/prog/samba/lib/tdb$ ./bin/tdbtool /tmp/pppd2.tdb show 'IFNAME=ppp0'

key 11 bytes
IFNAME=ppp0
data 9 bytes
[000] 70 70 70 64 33 31 30 37  38                       pppd3107 8

^^^ tricky to parse in a shell script - vs.

tim at ermintrude:~/prog/samba/lib/tdb$ ./bin/tdbtool /tmp/pppd2.tdb get 'IFNAME=ppp0'
pppd31078

^^^ no problem.

Would this be considered for inclusion in Samba?

signed-off-by: Tim Small <tim at seoss.co.uk>


diff --git a/lib/tdb/tools/tdbtool.c b/lib/tdb/tools/tdbtool.c
index 99d4841..5b5407a 100644
--- a/lib/tdb/tools/tdbtool.c
+++ b/lib/tdb/tools/tdbtool.c
@@ -49,6 +49,7 @@ enum commands {
 	CMD_MOVE,
 	CMD_STORE,
 	CMD_SHOW,
+	CMD_GET,
 	CMD_KEYS,
 	CMD_HEXKEYS,
 	CMD_DELETE,
@@ -82,6 +83,7 @@ COMMAND_TABLE cmd_table[] = {
 	{"move",	CMD_MOVE},
 	{"store",	CMD_STORE},
 	{"show",	CMD_SHOW},
+	{"get",		CMD_GET},
 	{"keys",	CMD_KEYS},
 	{"hexkeys",	CMD_HEXKEYS},
 	{"delete",	CMD_DELETE},
@@ -199,6 +201,7 @@ static void help(void)
 "  move      key  file  : move a record to a destination tdb\n"
 "  store     key  data  : store a record (replace)\n"
 "  show      key        : show a record by key\n"
+"  get       key        : show just printable data associated with a key\n"
 "  delete    key        : delete a record by key\n"
 "  list                 : print the database hash table and freelist\n"
 "  free                 : print the database freelist\n"
@@ -289,7 +292,7 @@ static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen)
 	}
 }
 
-static void show_tdb(char *keyname, size_t keylen)
+static void show_tdb(char *keyname, size_t keylen, int verbose)
 {
 	TDB_DATA key, dbuf;
 
@@ -307,8 +310,14 @@ static void show_tdb(char *keyname, size_t keylen)
 	    return;
 	}
 	
-	print_rec(tdb, key, dbuf, NULL);
-	
+	if (verbose)
+		print_rec(tdb, key, dbuf, NULL);
+	else {
+		print_asc((const char *)dbuf.dptr, dbuf.dsize); 
+		printf("\n");
+	}
+		
+
 	free( dbuf.dptr );
 	
 	return;
@@ -637,7 +646,11 @@ static int do_command(void)
 			return 0;
 		case CMD_SHOW:
 			bIterate = 0;
-			show_tdb(arg1, arg1len);
+			show_tdb(arg1, arg1len, 1);
+			return 0;
+		case CMD_GET:
+			bIterate = 0;
+			show_tdb(arg1, arg1len, 0);
 			return 0;
 		case CMD_KEYS:
 			tdb_traverse(tdb, print_key, NULL);



More information about the samba-technical mailing list