s3-includes: only include system/filesys.h when needed.
[samba.git] / source3 / utils / dbwrap_tool.c
1 /*
2    Samba Unix/Linux CIFS implementation
3
4    low level TDB/CTDB tool using the dbwrap interface
5
6    Copyright (C) 2009 Michael Adam <obnox@samba.org>
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "dbwrap.h"
25
26 typedef enum { OP_FETCH, OP_STORE, OP_DELETE, OP_ERASE, OP_LISTKEYS } dbwrap_op;
27
28 typedef enum { TYPE_INT32, TYPE_UINT32 } dbwrap_type;
29
30 static int dbwrap_tool_fetch_int32(struct db_context *db,
31                                    const char *keyname,
32                                    void *data)
33 {
34         int32_t value;
35
36         value = dbwrap_fetch_int32(db, keyname);
37         d_printf("%d\n", value);
38
39         return 0;
40 }
41
42 static int dbwrap_tool_fetch_uint32(struct db_context *db,
43                                     const char *keyname,
44                                     void *data)
45 {
46         uint32_t value;
47         bool ret;
48
49         ret = dbwrap_fetch_uint32(db, keyname, &value);
50         if (ret) {
51                 d_printf("%u\n", value);
52                 return 0;
53         } else {
54                 d_fprintf(stderr, "ERROR: could not fetch uint32 key '%s'\n",
55                           keyname);
56                 return -1;
57         }
58 }
59
60 static int dbwrap_tool_store_int32(struct db_context *db,
61                                    const char *keyname,
62                                    void *data)
63 {
64         NTSTATUS status;
65         int32_t value = *((int32_t *)data);
66
67         status = dbwrap_trans_store_int32(db, keyname, value);
68
69         if (!NT_STATUS_IS_OK(status)) {
70                 d_fprintf(stderr, "ERROR: could not store int32 key '%s': %s\n",
71                           keyname, nt_errstr(status));
72                 return -1;
73         }
74
75         return 0;
76 }
77
78 static int dbwrap_tool_store_uint32(struct db_context *db,
79                                     const char *keyname,
80                                     void *data)
81 {
82         NTSTATUS status;
83         uint32_t value = *((uint32_t *)data);
84
85         status = dbwrap_trans_store_uint32(db, keyname, value);
86
87         if (!NT_STATUS_IS_OK(status)) {
88                 d_fprintf(stderr,
89                           "ERROR: could not store uint32 key '%s': %s\n",
90                           keyname, nt_errstr(status));
91                 return -1;
92         }
93
94         return 0;
95 }
96
97 static int dbwrap_tool_delete(struct db_context *db,
98                               const char *keyname,
99                               void *data)
100 {
101         NTSTATUS status;
102
103         status = dbwrap_trans_delete_bystring(db, keyname);
104
105         if (!NT_STATUS_IS_OK(status)) {
106                 d_fprintf(stderr, "ERROR deleting record %s : %s\n",
107                           keyname, nt_errstr(status));
108                 return -1;
109         }
110
111         return 0;
112 }
113
114 static int delete_fn(struct db_record *rec, void *priv)
115 {
116         rec->delete_rec(rec);
117         return 0;
118 }
119
120 /**
121  * dbwrap_tool_erase: erase the whole data base
122  * the keyname argument is not used.
123  */
124 static int dbwrap_tool_erase(struct db_context *db,
125                              const char *keyname,
126                              void *data)
127 {
128         int ret;
129
130         ret = db->traverse(db, delete_fn, NULL);
131
132         if (ret < 0) {
133                 d_fprintf(stderr, "ERROR erasing the database\n");
134                 return -1;
135         }
136
137         return 0;
138 }
139
140 static int listkey_fn(struct db_record *rec, void *private_data)
141 {
142         int length = rec->key.dsize;
143         unsigned char *p = (unsigned char *)rec->key.dptr;
144
145         while (length--) {
146                 if (isprint(*p) && !strchr("\"\\", *p)) {
147                         d_printf("%c", *p);
148                 } else {
149                         d_printf("\\%02X", *p);
150                 }
151                 p++;
152         }
153
154         d_printf("\n");
155
156         return 0;
157 }
158
159 static int dbwrap_tool_listkeys(struct db_context *db,
160                                 const char *keyname,
161                                 void *data)
162 {
163         int ret;
164
165         ret = db->traverse_read(db, listkey_fn, NULL);
166
167         if (ret < 0) {
168                 d_fprintf(stderr, "ERROR listing db keys\n");
169                 return -1;
170         }
171
172         return 0;
173 }
174
175 struct dbwrap_op_dispatch_table {
176         dbwrap_op op;
177         dbwrap_type type;
178         int (*cmd)(struct db_context *db,
179                    const char *keyname,
180                    void *data);
181 };
182
183 struct dbwrap_op_dispatch_table dispatch_table[] = {
184         { OP_FETCH,  TYPE_INT32,  dbwrap_tool_fetch_int32 },
185         { OP_FETCH,  TYPE_UINT32, dbwrap_tool_fetch_uint32 },
186         { OP_STORE,  TYPE_INT32,  dbwrap_tool_store_int32 },
187         { OP_STORE,  TYPE_UINT32, dbwrap_tool_store_uint32 },
188         { OP_DELETE, TYPE_INT32,  dbwrap_tool_delete },
189         { OP_ERASE,  TYPE_INT32,  dbwrap_tool_erase },
190         { OP_LISTKEYS, TYPE_INT32, dbwrap_tool_listkeys },
191         { 0, 0, NULL },
192 };
193
194 int main(int argc, const char **argv)
195 {
196         struct tevent_context *evt_ctx;
197         struct messaging_context *msg_ctx;
198         struct db_context *db;
199
200         uint16_t count;
201
202         const char *dbname;
203         const char *opname;
204         dbwrap_op op;
205         const char *keyname = "";
206         const char *keytype = "int32";
207         dbwrap_type type;
208         const char *valuestr = "0";
209         int32_t value = 0;
210
211         TALLOC_CTX *mem_ctx = talloc_stackframe();
212
213         int ret = 1;
214
215         load_case_tables();
216         lp_set_cmdline("log level", "0");
217         setup_logging(argv[0], DEBUG_STDERR);
218         lp_load(get_dyn_CONFIGFILE(), true, false, false, true);
219
220         if ((argc < 3) || (argc > 6)) {
221                 d_fprintf(stderr,
222                           "USAGE: %s <database> <op> [<key> [<type> [<value>]]]\n"
223                           "       ops: fetch, store, delete, erase, listkeys\n"
224                           "       types: int32, uint32\n",
225                          argv[0]);
226                 goto done;
227         }
228
229         dbname = argv[1];
230         opname = argv[2];
231
232         if (strcmp(opname, "store") == 0) {
233                 if (argc != 6) {
234                         d_fprintf(stderr, "ERROR: operation 'store' requires "
235                                   "value argument\n");
236                         goto done;
237                 }
238                 valuestr = argv[5];
239                 keytype = argv[4];
240                 keyname = argv[3];
241                 op = OP_STORE;
242         } else if (strcmp(opname, "fetch") == 0) {
243                 if (argc != 5) {
244                         d_fprintf(stderr, "ERROR: operation 'fetch' requires "
245                                   "type but not value argument\n");
246                         goto done;
247                 }
248                 op = OP_FETCH;
249                 keytype = argv[4];
250                 keyname = argv[3];
251         } else if (strcmp(opname, "delete") == 0) {
252                 if (argc != 4) {
253                         d_fprintf(stderr, "ERROR: operation 'delete' does "
254                                   "not allow type nor value argument\n");
255                         goto done;
256                 }
257                 keyname = argv[3];
258                 op = OP_DELETE;
259         } else if (strcmp(opname, "erase") == 0) {
260                 if (argc != 3) {
261                         d_fprintf(stderr, "ERROR: operation 'erase' does "
262                                   "not take a key argument\n");
263                         goto done;
264                 }
265                 op = OP_ERASE;
266         } else if (strcmp(opname, "listkeys") == 0) {
267                 if (argc != 3) {
268                         d_fprintf(stderr, "ERROR: operation 'listkeys' does "
269                                   "not take a key argument\n");
270                         goto done;
271                 }
272                 op = OP_LISTKEYS;
273         } else {
274                 d_fprintf(stderr,
275                           "ERROR: invalid op '%s' specified\n"
276                           "       supported ops: fetch, store, delete\n",
277                           opname);
278                 goto done;
279         }
280
281         if (strcmp(keytype, "int32") == 0) {
282                 type = TYPE_INT32;
283                 value = (int32_t)strtol(valuestr, NULL, 10);
284         } else if (strcmp(keytype, "uint32") == 0) {
285                 type = TYPE_UINT32;
286                 value = (int32_t)strtoul(valuestr, NULL, 10);
287         } else {
288                 d_fprintf(stderr, "ERROR: invalid type '%s' specified.\n"
289                                   "       supported types: int32, uint32\n",
290                                   keytype);
291                 goto done;
292         }
293
294         evt_ctx = tevent_context_init(mem_ctx);
295         if (evt_ctx == NULL) {
296                 d_fprintf(stderr, "ERROR: could not init event context\n");
297                 goto done;
298         }
299
300         msg_ctx = messaging_init(mem_ctx, procid_self(), evt_ctx);
301         if (msg_ctx == NULL) {
302                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
303                 goto done;
304         }
305
306         db = db_open(mem_ctx, dbname, 0, TDB_DEFAULT, O_RDWR | O_CREAT, 0644);
307         if (db == NULL) {
308                 d_fprintf(stderr, "ERROR: could not open dbname\n");
309                 goto done;
310         }
311
312         for (count = 0; dispatch_table[count].cmd != NULL; count++) {
313                 if ((op == dispatch_table[count].op) &&
314                     (type == dispatch_table[count].type))
315                 {
316                         ret = dispatch_table[count].cmd(db, keyname, &value);
317                         break;
318                 }
319         }
320
321 done:
322         TALLOC_FREE(mem_ctx);
323         return ret;
324 }