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