s3: Lift the smbd_messaging_context from reload_services
[amitay/samba.git] / source3 / torture / vfstest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    VFS module tester
4
5    Copyright (C) Simo Sorce 2002
6    Copyright (C) Eric Lorimer 2002
7    Copyright (C) Jelmer Vernooij 2002,2003
8
9    Most of this code was ripped off of rpcclient.
10    Copyright (C) Tim Potter 2000-2001
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16    
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21    
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "popt_common.h"
28 #include "vfstest.h"
29
30 /* List to hold groups of commands */
31 static struct cmd_list {
32         struct cmd_list *prev, *next;
33         struct cmd_set *cmd_set;
34 } *cmd_list;
35
36 int get_client_fd(void)
37 {
38         return -1;
39 }
40
41 /****************************************************************************
42 handle completion of commands for readline
43 ****************************************************************************/
44 static char **completion_fn(const char *text, int start, int end)
45 {
46 #define MAX_COMPLETIONS 100
47         char **matches;
48         int i, count=0;
49         struct cmd_list *commands = cmd_list;
50
51         if (start) 
52                 return NULL;
53
54         /* make sure we have a list of valid commands */
55         if (!commands) 
56                 return NULL;
57
58         matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
59         if (!matches) return NULL;
60
61         matches[count++] = SMB_STRDUP(text);
62         if (!matches[0]) return NULL;
63
64         while (commands && count < MAX_COMPLETIONS-1) 
65         {
66                 if (!commands->cmd_set)
67                         break;
68                 
69                 for (i=0; commands->cmd_set[i].name; i++)
70                 {
71                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
72                                 commands->cmd_set[i].fn) 
73                         {
74                                 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
75                                 if (!matches[count]) 
76                                         return NULL;
77                                 count++;
78                         }
79                 }
80                 
81                 commands = commands->next;
82                 
83         }
84
85         if (count == 2) {
86                 SAFE_FREE(matches[0]);
87                 matches[0] = SMB_STRDUP(matches[1]);
88         }
89         matches[count] = NULL;
90         return matches;
91 }
92
93 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
94 {
95         char *command;
96         char *p;
97
98         if (!cmdstr || !(*cmdstr))
99                 return NULL;
100
101         p = strchr_m(*cmdstr, ';');
102         if (p)
103                 *p = '\0';
104         command = talloc_strdup(ctx, *cmdstr);
105         *cmdstr = p;
106
107         return command;
108 }
109
110 /* Load specified configuration file */
111 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
112                         int argc, const char **argv)
113 {
114         if (argc != 2) {
115                 printf("Usage: %s <smb.conf>\n", argv[0]);
116                 return NT_STATUS_OK;
117         }
118
119         if (!lp_load(argv[1], False, True, False, True)) {
120                 printf("Error loading \"%s\"\n", argv[1]);
121                 return NT_STATUS_OK;
122         }
123
124         printf("\"%s\" successfully loaded\n", argv[1]);
125         return NT_STATUS_OK;
126 }
127         
128 /* Display help on commands */
129 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
130                          int argc, const char **argv)
131 {
132         struct cmd_list *tmp;
133         struct cmd_set *tmp_set;
134
135         /* Usage */
136         if (argc > 2) {
137                 printf("Usage: %s [command]\n", argv[0]);
138                 return NT_STATUS_OK;
139         }
140
141         /* Help on one command */
142
143         if (argc == 2) {
144                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
145                         
146                         tmp_set = tmp->cmd_set;
147
148                         while(tmp_set->name) {
149                                 if (strequal(argv[1], tmp_set->name)) {
150                                         if (tmp_set->usage &&
151                                             tmp_set->usage[0])
152                                                 printf("%s\n", tmp_set->usage);
153                                         else
154                                                 printf("No help for %s\n", tmp_set->name);
155
156                                         return NT_STATUS_OK;
157                                 }
158
159                                 tmp_set++;
160                         }
161                 }
162
163                 printf("No such command: %s\n", argv[1]);
164                 return NT_STATUS_OK;
165         }
166
167         /* List all commands */
168
169         for (tmp = cmd_list; tmp; tmp = tmp->next) {
170
171                 tmp_set = tmp->cmd_set;
172
173                 while(tmp_set->name) {
174
175                         printf("%15s\t\t%s\n", tmp_set->name,
176                                tmp_set->description ? tmp_set->description:
177                                "");
178
179                         tmp_set++;
180                 }
181         }
182
183         return NT_STATUS_OK;
184 }
185
186 /* Change the debug level */
187 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
188 {
189         if (argc > 2) {
190                 printf("Usage: %s [debuglevel]\n", argv[0]);
191                 return NT_STATUS_OK;
192         }
193
194         if (argc == 2) {
195                 DEBUGLEVEL = atoi(argv[1]);
196         }
197
198         printf("debuglevel is %d\n", DEBUGLEVEL);
199
200         return NT_STATUS_OK;
201 }
202
203 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
204 {
205         /* Cleanup */
206         talloc_destroy(mem_ctx);
207         mem_ctx = NULL;
208         vfs->data = NULL;
209         vfs->data_size = 0;
210         return NT_STATUS_OK;
211 }
212
213 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
214 {
215         /* Cleanup */
216         talloc_destroy(mem_ctx);
217
218         exit(0);
219         return NT_STATUS_OK; /* NOTREACHED */
220 }
221
222 static struct cmd_set vfstest_commands[] = {
223
224         { "GENERAL OPTIONS" },
225
226         { "conf",       cmd_conf,       "Load smb configuration file", "conf <smb.conf>" },
227         { "help",       cmd_help,       "Get help on commands", "" },
228         { "?",          cmd_help,       "Get help on commands", "" },
229         { "debuglevel", cmd_debuglevel, "Set debug level", "" },
230         { "freemem",    cmd_freemem,    "Free currently allocated buffers", "" },
231         { "exit",       cmd_quit,       "Exit program", "" },
232         { "quit",       cmd_quit,       "Exit program", "" },
233
234         { NULL }
235 };
236
237 static struct cmd_set separator_command[] = {
238         { "---------------", NULL,      "----------------------" },
239         { NULL }
240 };
241
242
243 extern struct cmd_set vfs_commands[];
244 static struct cmd_set *vfstest_command_list[] = {
245         vfstest_commands,
246         vfs_commands,
247         NULL
248 };
249
250 static void add_command_set(struct cmd_set *cmd_set)
251 {
252         struct cmd_list *entry;
253
254         if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
255                 DEBUG(0, ("out of memory\n"));
256                 return;
257         }
258
259         ZERO_STRUCTP(entry);
260
261         entry->cmd_set = cmd_set;
262         DLIST_ADD(cmd_list, entry);
263 }
264
265 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
266 {
267         const char *p = cmd;
268         char **argv = NULL;
269         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
270         char *buf;
271         TALLOC_CTX *mem_ctx = talloc_stackframe();
272         int argc = 0, i;
273
274         /* Count number of arguments first time through the loop then
275            allocate memory and strdup them. */
276
277  again:
278         while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
279                 if (argv) {
280                         argv[argc] = SMB_STRDUP(buf);
281                 }
282                 argc++;
283         }
284
285         if (!argv) {
286                 /* Create argument list */
287
288                 argv = SMB_MALLOC_ARRAY(char *, argc);
289                 memset(argv, 0, sizeof(char *) * argc);
290
291                 if (!argv) {
292                         fprintf(stderr, "out of memory\n");
293                         result = NT_STATUS_NO_MEMORY;
294                         goto done;
295                 }
296
297                 p = cmd;
298                 argc = 0;
299
300                 goto again;
301         }
302
303         /* Call the function */
304
305         if (cmd_entry->fn) {
306                 /* Run command */
307                 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
308         } else {
309                 fprintf (stderr, "Invalid command\n");
310                 goto done;
311         }
312
313  done:
314
315         /* Cleanup */
316
317         if (argv) {
318                 for (i = 0; i < argc; i++)
319                         SAFE_FREE(argv[i]);
320
321                 SAFE_FREE(argv);
322         }
323
324         TALLOC_FREE(mem_ctx);
325         return result;
326 }
327
328 /* Process a command entered at the prompt or as part of -c */
329 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
330 {
331         struct cmd_list *temp_list;
332         bool found = False;
333         char *buf;
334         const char *p = cmd;
335         NTSTATUS result = NT_STATUS_OK;
336         TALLOC_CTX *mem_ctx = talloc_stackframe();
337         int len = 0;
338
339         if (cmd[strlen(cmd) - 1] == '\n')
340                 cmd[strlen(cmd) - 1] = '\0';
341
342         if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
343                 TALLOC_FREE(mem_ctx);
344                 return NT_STATUS_OK;
345         }
346
347         /* Strip the trailing \n if it exists */
348         len = strlen(buf);
349         if (buf[len-1] == '\n')
350                 buf[len-1] = '\0';
351
352         /* Search for matching commands */
353
354         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
355                 struct cmd_set *temp_set = temp_list->cmd_set;
356
357                 while(temp_set->name) {
358                         if (strequal(buf, temp_set->name)) {
359                                 found = True;
360                                 result = do_cmd(vfs, temp_set, cmd);
361
362                                 goto done;
363                         }
364                         temp_set++;
365                 }
366         }
367
368  done:
369         if (!found && buf[0]) {
370                 printf("command not found: %s\n", buf);
371                 TALLOC_FREE(mem_ctx);
372                 return NT_STATUS_OK;
373         }
374
375         if (!NT_STATUS_IS_OK(result)) {
376                 printf("result was %s\n", nt_errstr(result));
377         }
378
379         TALLOC_FREE(mem_ctx);
380         return result;
381 }
382
383 static void process_file(struct vfs_state *pvfs, char *filename) {
384         FILE *file;
385         char command[3 * PATH_MAX];
386
387         if (*filename == '-') {
388                 file = stdin;
389         } else {
390                 file = fopen(filename, "r");
391                 if (file == NULL) {
392                         printf("vfstest: error reading file (%s)!", filename);
393                         printf("errno n.%d: %s", errno, strerror(errno));
394                         exit(-1);
395                 }
396         }
397
398         while (fgets(command, 3 * PATH_MAX, file) != NULL) {
399                 process_cmd(pvfs, command);
400         }
401
402         if (file != stdin) {
403                 fclose(file);
404         }
405 }
406
407 void exit_server(const char *reason)
408 {
409         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
410         exit(0);
411 }
412
413 void exit_server_cleanly(const char *const reason)
414 {
415         exit_server("normal exit");
416 }
417
418 static int server_fd = -1;
419 int last_message = -1;
420
421 int smbd_server_fd(void)
422 {
423                 return server_fd;
424 }
425
426 struct event_context *smbd_event_context(void)
427 {
428         static struct event_context *ctx;
429
430         if (!ctx && !(ctx = event_context_init(NULL))) {
431                 smb_panic("Could not init smbd event context\n");
432         }
433         return ctx;
434 }
435
436 /* Main function */
437
438 int main(int argc, char *argv[])
439 {
440         static char             *cmdstr = NULL;
441         struct cmd_set          **cmd_set;
442         static struct vfs_state vfs;
443         int i;
444         static char             *filename = NULL;
445         TALLOC_CTX *frame = talloc_stackframe();
446
447         /* make sure the vars that get altered (4th field) are in
448            a fixed location or certain compilers complain */
449         poptContext pc;
450         struct poptOption long_options[] = {
451                 POPT_AUTOHELP
452                 {"file",        'f', POPT_ARG_STRING,   &filename, 0, },
453                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 0, "Execute specified list of commands" },
454                 POPT_COMMON_SAMBA
455                 POPT_TABLEEND
456         };
457
458         load_case_tables();
459
460         setlinebuf(stdout);
461
462         pc = poptGetContext("vfstest", argc, (const char **) argv,
463                             long_options, 0);
464         
465         while(poptGetNextOpt(pc) != -1);
466
467
468         poptFreeContext(pc);
469
470         /* TODO: check output */
471         reload_services(smbd_messaging_context(), False);
472
473         /* the following functions are part of the Samba debugging
474            facilities.  See lib/debug.c */
475         setup_logging("vfstest", True);
476         
477         /* Load command lists */
478
479         cmd_set = vfstest_command_list;
480
481         while(*cmd_set) {
482                 add_command_set(*cmd_set);
483                 add_command_set(separator_command);
484                 cmd_set++;
485         }
486
487         /* some basic initialization stuff */
488         sec_init();
489         vfs.conn = TALLOC_ZERO_P(NULL, connection_struct);
490         vfs.conn->params = TALLOC_P(vfs.conn, struct share_params);
491         for (i=0; i < 1024; i++)
492                 vfs.files[i] = NULL;
493
494         /* some advanced initiliazation stuff */
495         smbd_vfs_init(vfs.conn);
496
497         /* Do we have a file input? */
498         if (filename && filename[0]) {
499                 process_file(&vfs, filename);
500                 return 0;
501         }
502
503         /* Do anything specified with -c */
504         if (cmdstr && cmdstr[0]) {
505                 char    *cmd;
506                 char    *p = cmdstr;
507
508                 while((cmd=next_command(frame, &p)) != NULL) {
509                         process_cmd(&vfs, cmd);
510                 }
511
512                 TALLOC_FREE(cmd);
513                 return 0;
514         }
515
516         /* Loop around accepting commands */
517
518         while(1) {
519                 char *line = NULL;
520
521                 line = smb_readline("vfstest $> ", NULL, completion_fn);
522
523                 if (line == NULL) {
524                         break;
525                 }
526
527                 if (line[0] != '\n') {
528                         process_cmd(&vfs, line);
529                 }
530                 SAFE_FREE(line);
531         }
532
533         TALLOC_FREE(vfs.conn);
534         TALLOC_FREE(frame);
535         return 0;
536 }