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