some fixes
[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
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 2 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, write to the Free Software
24    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 */
26
27 #include "includes.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 TALLOC_CTX *global_ctx;
37
38 /****************************************************************************
39 handle completion of commands for readline
40 ****************************************************************************/
41 static char **completion_fn(char *text, int start, int end)
42 {
43 #define MAX_COMPLETIONS 100
44         char **matches;
45         int i, count=0;
46         struct cmd_list *commands = cmd_list;
47
48         if (start) 
49                 return NULL;
50
51         /* make sure we have a list of valid commands */
52         if (!commands) 
53                 return NULL;
54
55         matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
56         if (!matches) return NULL;
57
58         matches[count++] = strdup(text);
59         if (!matches[0]) return NULL;
60
61         while (commands && count < MAX_COMPLETIONS-1) 
62         {
63                 if (!commands->cmd_set)
64                         break;
65                 
66                 for (i=0; commands->cmd_set[i].name; i++)
67                 {
68                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
69                                 commands->cmd_set[i].fn) 
70                         {
71                                 matches[count] = strdup(commands->cmd_set[i].name);
72                                 if (!matches[count]) 
73                                         return NULL;
74                                 count++;
75                         }
76                 }
77                 
78                 commands = commands->next;
79                 
80         }
81
82         if (count == 2) {
83                 SAFE_FREE(matches[0]);
84                 matches[0] = strdup(matches[1]);
85         }
86         matches[count] = NULL;
87         return matches;
88 }
89
90 static char* next_command (char** cmdstr)
91 {
92         static pstring          command;
93         char                    *p;
94         
95         if (!cmdstr || !(*cmdstr))
96                 return NULL;
97         
98         p = strchr_m(*cmdstr, ';');
99         if (p)
100                 *p = '\0';
101         pstrcpy(command, *cmdstr);
102         *cmdstr = p;
103         
104         return command;
105 }
106
107 /* Load specified configuration file */
108 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
109                         int argc, char **argv)
110 {
111         if (argc != 2) {
112                 printf("Usage: %s <smb.conf>\n", argv[0]);
113                 return NT_STATUS_OK;
114         }
115
116         if (!lp_load(argv[1], False, True, False)) {
117                 printf("Error loading \"%s\"\n", argv[1]);
118                 return NT_STATUS_OK;
119         }
120
121         printf("\"%s\" successfully loaded\n", argv[1]);
122         return NT_STATUS_OK;
123 }
124         
125 /* Display help on commands */
126 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
127                          int argc, char **argv)
128 {
129         struct cmd_list *tmp;
130         struct cmd_set *tmp_set;
131
132         /* Usage */
133         if (argc > 2) {
134                 printf("Usage: %s [command]\n", argv[0]);
135                 return NT_STATUS_OK;
136         }
137
138         /* Help on one command */
139
140         if (argc == 2) {
141                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
142                         
143                         tmp_set = tmp->cmd_set;
144
145                         while(tmp_set->name) {
146                                 if (strequal(argv[1], tmp_set->name)) {
147                                         if (tmp_set->usage &&
148                                             tmp_set->usage[0])
149                                                 printf("%s\n", tmp_set->usage);
150                                         else
151                                                 printf("No help for %s\n", tmp_set->name);
152
153                                         return NT_STATUS_OK;
154                                 }
155
156                                 tmp_set++;
157                         }
158                 }
159
160                 printf("No such command: %s\n", argv[1]);
161                 return NT_STATUS_OK;
162         }
163
164         /* List all commands */
165
166         for (tmp = cmd_list; tmp; tmp = tmp->next) {
167
168                 tmp_set = tmp->cmd_set;
169
170                 while(tmp_set->name) {
171
172                         printf("%15s\t\t%s\n", tmp_set->name,
173                                tmp_set->description ? tmp_set->description:
174                                "");
175
176                         tmp_set++;
177                 }
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 /* Change the debug level */
184 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
185 {
186         if (argc > 2) {
187                 printf("Usage: %s [debuglevel]\n", argv[0]);
188                 return NT_STATUS_OK;
189         }
190
191         if (argc == 2) {
192                 DEBUGLEVEL = atoi(argv[1]);
193         }
194
195         printf("debuglevel is %d\n", DEBUGLEVEL);
196
197         return NT_STATUS_OK;
198 }
199
200 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
201 {
202         /* Cleanup */
203         talloc_destroy(global_ctx);
204         global_ctx = NULL;
205         vfs->data = NULL;
206         vfs->data_size = 0;
207         return NT_STATUS_OK;
208 }
209
210 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
211 {
212         /* Cleanup */
213         talloc_destroy(global_ctx);
214
215         exit(0);
216         return NT_STATUS_OK; /* NOTREACHED */
217 }
218
219 static struct cmd_set vfstest_commands[] = {
220
221         { "GENERAL OPTIONS" },
222
223         { "conf",       cmd_conf,       "Load smb configuration file", "conf <smb.conf>" },
224         { "help",       cmd_help,       "Get help on commands", "" },
225         { "?",          cmd_help,       "Get help on commands", "" },
226         { "debuglevel", cmd_debuglevel, "Set debug level", "" },
227         { "freemem",    cmd_freemem,    "Free currently allocated buffers", "" },
228         { "exit",       cmd_quit,       "Exit program", "" },
229         { "quit",       cmd_quit,       "Exit program", "" },
230
231         { NULL }
232 };
233
234 static struct cmd_set separator_command[] = {
235         { "---------------", NULL,      "----------------------" },
236         { NULL }
237 };
238
239
240 extern struct cmd_set vfs_commands[];
241 static struct cmd_set *vfstest_command_list[] = {
242         vfstest_commands,
243         vfs_commands,
244         NULL
245 };
246
247 static void add_command_set(struct cmd_set *cmd_set)
248 {
249         struct cmd_list *entry;
250
251         if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
252                 DEBUG(0, ("out of memory\n"));
253                 return;
254         }
255
256         ZERO_STRUCTP(entry);
257
258         entry->cmd_set = cmd_set;
259         DLIST_ADD(cmd_list, entry);
260 }
261
262 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
263 {
264         char *p = cmd, **argv = NULL;
265         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266         pstring buf;
267         int argc = 0, i;
268
269         /* Count number of arguments first time through the loop then
270            allocate memory and strdup them. */
271
272  again:
273         while(next_token(&p, buf, " ", sizeof(buf))) {
274                 if (argv) {
275                         argv[argc] = strdup(buf);
276                 }
277                 
278                 argc++;
279         }
280                                 
281         if (!argv) {
282
283                 /* Create argument list */
284
285                 argv = (char **)malloc(sizeof(char *) * argc);
286                 memset(argv, 0, sizeof(char *) * argc);
287
288                 if (!argv) {
289                         fprintf(stderr, "out of memory\n");
290                         result = NT_STATUS_NO_MEMORY;
291                         goto done;
292                 }
293                                         
294                 p = cmd;
295                 argc = 0;
296                                         
297                 goto again;
298         }
299
300         /* Call the function */
301
302         if (cmd_entry->fn) {
303
304                 if (global_ctx == NULL) {
305                         /* Create mem_ctx */
306                         if (!(global_ctx = talloc_init())) {
307                                 DEBUG(0, ("talloc_init() failed\n"));
308                                 goto done;
309                         }
310                 }
311
312                 /* Run command */
313                 result = cmd_entry->fn(vfs, global_ctx, argc, argv);
314
315         } else {
316                 fprintf (stderr, "Invalid command\n");
317                 goto done;
318         }
319
320  done:
321                                                 
322         /* Cleanup */
323
324         if (argv) {
325                 for (i = 0; i < argc; i++)
326                         SAFE_FREE(argv[i]);
327         
328                 SAFE_FREE(argv);
329         }
330         
331         return result;
332 }
333
334 /* Process a command entered at the prompt or as part of -c */
335 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
336 {
337         struct cmd_list *temp_list;
338         BOOL found = False;
339         pstring buf;
340         char *p = cmd;
341         NTSTATUS result = NT_STATUS_OK;
342         int len = 0;
343
344         if (cmd[strlen(cmd) - 1] == '\n')
345                 cmd[strlen(cmd) - 1] = '\0';
346
347         if (!next_token(&p, buf, " ", sizeof(buf))) {
348                 return NT_STATUS_OK;
349         }
350
351         /* strip the trainly \n if it exsists */
352         len = strlen(buf);
353         if (buf[len-1] == '\n')
354                 buf[len-1] = '\0';
355
356         /* Search for matching commands */
357
358         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
359                 struct cmd_set *temp_set = temp_list->cmd_set;
360
361                 while(temp_set->name) {
362                         if (strequal(buf, temp_set->name)) {
363                                 found = True;
364                                 result = do_cmd(vfs, temp_set, cmd);
365
366                                 goto done;
367                         }
368                         temp_set++;
369                 }
370         }
371
372  done:
373         if (!found && buf[0]) {
374                 printf("command not found: %s\n", buf);
375                 return NT_STATUS_OK;
376         }
377
378         if (!NT_STATUS_IS_OK(result)) {
379                 printf("result was %s\n", nt_errstr(result));
380         }
381
382         return result;
383 }
384
385 void exit_server(char *reason)
386 {
387         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
388         exit(0);
389 }
390
391 static int server_fd = -1;
392 int last_message = -1;
393
394 int smbd_server_fd(void)
395 {
396                 return server_fd;
397 }
398
399 BOOL reload_services(BOOL test)
400 {
401         return True;
402 }
403
404 /* Print usage information */
405 static void usage(void)
406 {
407         printf("Usage: vfstest [options]\n");
408
409         printf("\t-c or --command \"command string\"   execute semicolon separated cmds\n");
410         printf("\t-d or --debug debuglevel         set the debuglevel\n");
411         printf("\t-l or --logfile logfile           logfile to use instead of stdout\n");
412         printf("\t-h or --help                 Print this help message.\n");
413         printf("\n");
414 }
415
416 /* Main function */
417
418 int main(int argc, char *argv[])
419 {
420         BOOL                    interactive = True;
421         int                     opt;
422         static char             *cmdstr = "";
423         static char *opt_logfile=NULL;
424         static int              opt_debuglevel;
425         pstring                 logfile;
426         struct cmd_set          **cmd_set;
427         extern BOOL             AllowDebugChange;
428         static struct vfs_state vfs;
429         int i;
430
431
432         /* make sure the vars that get altered (4th field) are in
433            a fixed location or certain compilers complain */
434         poptContext pc;
435         struct poptOption long_options[] = {
436                 {"debug",       'd', POPT_ARG_INT,      &opt_debuglevel, 'd'},
437                 {"debuglevel",  'd', POPT_ARG_INT,      &opt_debuglevel, 'd'},
438                 {"command",     'c', POPT_ARG_STRING,   &cmdstr},
439                 {"logfile",     'l', POPT_ARG_STRING,   &opt_logfile, 'l'},
440                 {"help",        'h', POPT_ARG_NONE,     0, 'h'},
441                 { 0, 0, 0, 0}
442         };
443
444
445         setlinebuf(stdout);
446
447         DEBUGLEVEL = 1;
448         AllowDebugChange = False;
449
450         pc = poptGetContext("vfstest", argc, (const char **) argv,
451                             long_options, 0);
452         
453         while((opt = poptGetNextOpt(pc)) != -1) {
454                 switch (opt) {
455                 case 'l':
456                         slprintf(logfile, sizeof(logfile) - 1, "%s.client", 
457                                  opt_logfile);
458                         lp_set_logfile(logfile);
459                         interactive = False;
460                         break;
461                         
462                 case 'd':
463                         DEBUGLEVEL = opt_debuglevel;
464                         break;
465                         
466                 case 'h':
467                 default:
468                         usage();
469                         exit(1);
470                 }
471         }
472
473
474         poptFreeContext(pc);
475
476         /* the following functions are part of the Samba debugging
477            facilities.  See lib/debug.c */
478         setup_logging("vfstest", interactive);
479         if (!interactive) 
480                 reopen_logs();
481         
482         /* Load command lists */
483
484         cmd_set = vfstest_command_list;
485
486         while(*cmd_set) {
487                 add_command_set(*cmd_set);
488                 add_command_set(separator_command);
489                 cmd_set++;
490         }
491
492         /* some basic initialization stuff */
493         vfs.conn = (struct connection_struct *)malloc(sizeof(struct connection_struct));
494         vfs.conn->user = "vfstest";
495         for (i=0; i < 1024; i++)
496                 vfs.files[i] = NULL;
497
498        /* Do anything specified with -c */
499         if (cmdstr[0]) {
500                 char    *cmd;
501                 char    *p = cmdstr;
502  
503                 while((cmd=next_command(&p)) != NULL) {
504                         process_cmd(&vfs, cmd);
505                 }
506                 
507                 return 0;
508         }
509
510         /* Initialize VFS */
511         vfs.conn->vfs_private = NULL;
512         smbd_vfs_init(vfs.conn);
513
514         /* Loop around accepting commands */
515
516         while(1) {
517                 pstring prompt;
518                 char *line;
519
520                 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
521
522                 line = smb_readline(prompt, NULL, completion_fn);
523
524                 if (line == NULL)
525                         break;
526
527                 if (line[0] != '\n')
528                         process_cmd(&vfs, line);
529         }
530         
531         free(vfs.conn);
532         return 0;
533 }