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