📄 tdbtool.c
字号:
/* Unix SMB/CIFS implementation. Samba database functions Copyright (C) Andrew Tridgell 1999-2000 Copyright (C) Paul `Rusty' Russell 2000 Copyright (C) Jeremy Allison 2000 Copyright (C) Andrew Esh 2001 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.*/#include <errno.h>#include <stdlib.h>#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <string.h>#include <fcntl.h>#include <time.h>#include <sys/mman.h>#include <sys/stat.h>#include <sys/time.h>#include <ctype.h>#include <signal.h>#include "tdb.h"#include "pstring.h"static int do_command(void);char *cmdname, *arg1, *arg2;size_t arg1len, arg2len;int do_connections;int bIterate = 0;char *line;TDB_DATA iterate_kbuf;char cmdline[1024];enum commands { CMD_CREATE_TDB, CMD_OPEN_TDB, CMD_ERASE, CMD_DUMP, CMD_CDUMP, CMD_INSERT, CMD_MOVE, CMD_STORE, CMD_SHOW, CMD_KEYS, CMD_HEXKEYS, CMD_DELETE, CMD_LIST_HASH_FREE, CMD_LIST_FREE, CMD_INFO, CMD_FIRST, CMD_NEXT, CMD_SYSTEM, CMD_QUIT, CMD_HELP};typedef struct { const char *name; enum commands cmd;} COMMAND_TABLE;COMMAND_TABLE cmd_table[] = { {"create", CMD_CREATE_TDB}, {"open", CMD_OPEN_TDB}, {"erase", CMD_ERASE}, {"dump", CMD_DUMP}, {"cdump", CMD_CDUMP}, {"insert", CMD_INSERT}, {"move", CMD_MOVE}, {"store", CMD_STORE}, {"show", CMD_SHOW}, {"keys", CMD_KEYS}, {"hexkeys", CMD_HEXKEYS}, {"delete", CMD_DELETE}, {"list", CMD_LIST_HASH_FREE}, {"free", CMD_LIST_FREE}, {"info", CMD_INFO}, {"first", CMD_FIRST}, {"1", CMD_FIRST}, {"next", CMD_NEXT}, {"n", CMD_NEXT}, {"quit", CMD_QUIT}, {"q", CMD_QUIT}, {"!", CMD_SYSTEM}, {NULL, CMD_HELP}};/* a tdb tool for manipulating a tdb database *//* these are taken from smb.h - make sure they are in sync */typedef struct connections_key { pid_t pid; int cnum; fstring name;} connections_key;typedef struct connections_data { int magic; pid_t pid; int cnum; uid_t uid; gid_t gid; char name[24]; char addr[24]; char machine[FSTRING_LEN]; time_t start; unsigned bcast_msg_flags;} connections_data;static TDB_CONTEXT *tdb;static int print_crec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);static int print_arec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);static int print_rec(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);static int print_key(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);static int print_hexkey(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf, void *state);static void print_asc(const char *buf,int len){ int i; /* We're probably printing ASCII strings so don't try to display the trailing NULL character. */ if (buf[len - 1] == 0) len--; for (i=0;i<len;i++) printf("%c",isprint(buf[i])?buf[i]:'.');}static void print_data(const char *buf,int len){ int i=0; if (len<=0) return; printf("[%03X] ",i); for (i=0;i<len;) { printf("%02X ",(int)buf[i]); i++; if (i%8 == 0) printf(" "); if (i%16 == 0) { print_asc(&buf[i-16],8); printf(" "); print_asc(&buf[i-8],8); printf("\n"); if (i<len) printf("[%03X] ",i); } } if (i%16) { int n; n = 16 - (i%16); printf(" "); if (n>8) printf(" "); while (n--) printf(" "); n = i%16; if (n > 8) n = 8; print_asc(&buf[i-(i%16)],n); printf(" "); n = (i%16) - n; if (n>0) print_asc(&buf[i-n],n); printf("\n"); }}static void help(void){ printf("\n""tdbtool: \n"" create dbname : create a database\n"" open dbname : open an existing database\n"" erase : erase the database\n"" dump : dump the database as strings\n"" cdump : dump the database as connection records\n"" keys : dump the database keys as strings\n"" hexkeys : dump the database keys as hex values\n"" info : print summary info about the database\n"" insert key data : insert a record\n"" 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"" delete key : delete a record by key\n"" list : print the database hash table and freelist\n"" free : print the database freelist\n"" ! command : execute system command\n" " 1 | first : print the first record\n"" n | next : print the next record\n"" q | quit : terminate\n"" \\n : repeat 'next' command\n""\n");}static void terror(const char *why){ printf("%s\n", why);}static void create_tdb(char * tdbname){ if (tdb) tdb_close(tdb); tdb = tdb_open(tdbname, 0, TDB_CLEAR_IF_FIRST, O_RDWR | O_CREAT | O_TRUNC, 0600); if (!tdb) { printf("Could not create %s: %s\n", tdbname, strerror(errno)); }}static void open_tdb(char *tdbname){ if (tdb) tdb_close(tdb); tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600); if (!tdb) { printf("Could not open %s: %s\n", tdbname, strerror(errno)); }}static void insert_tdb(char *keyname, size_t keylen, char* data, size_t datalen){ TDB_DATA key, dbuf; if ((keyname == NULL) || (keylen == 0)) { terror("need key"); return; } key.dptr = keyname; key.dsize = keylen; dbuf.dptr = data; dbuf.dsize = datalen; if (tdb_store(tdb, key, dbuf, TDB_INSERT) == -1) { terror("insert failed"); }}static void store_tdb(char *keyname, size_t keylen, char* data, size_t datalen){ TDB_DATA key, dbuf; if ((keyname == NULL) || (keylen == 0)) { terror("need key"); return; } if ((data == NULL) || (datalen == 0)) { terror("need data"); return; } key.dptr = keyname; key.dsize = keylen; dbuf.dptr = data; dbuf.dsize = datalen; printf("Storing key:\n"); print_rec(tdb, key, dbuf, NULL); if (tdb_store(tdb, key, dbuf, TDB_REPLACE) == -1) { terror("store failed"); }}static void show_tdb(char *keyname, size_t keylen){ TDB_DATA key, dbuf; if ((keyname == NULL) || (keylen == 0)) { terror("need key"); return; } key.dptr = keyname; key.dsize = keylen; dbuf = tdb_fetch(tdb, key); if (!dbuf.dptr) { terror("fetch failed"); return; } print_rec(tdb, key, dbuf, NULL); free( dbuf.dptr ); return;}static void delete_tdb(char *keyname, size_t keylen){ TDB_DATA key; if ((keyname == NULL) || (keylen == 0)) { terror("need key"); return; } key.dptr = keyname; key.dsize = keylen; if (tdb_delete(tdb, key) != 0) { terror("delete failed"); }}static void move_rec(char *keyname, size_t keylen, char* tdbname){ TDB_DATA key, dbuf; TDB_CONTEXT *dst_tdb; if ((keyname == NULL) || (keylen == 0)) { terror("need key"); return; } if ( !tdbname ) { terror("need destination tdb name"); return; } key.dptr = keyname; key.dsize = keylen; dbuf = tdb_fetch(tdb, key); if (!dbuf.dptr) { terror("fetch failed"); return; } print_rec(tdb, key, dbuf, NULL); dst_tdb = tdb_open(tdbname, 0, 0, O_RDWR, 0600); if ( !dst_tdb ) { terror("unable to open destination tdb"); return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -