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