s3-vfstest: Correctly initialize the connection path
[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 "smbd/smbd.h"
28 #include "popt_common.h"
29 #include "vfstest.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31
32 /* List to hold groups of commands */
33 static struct cmd_list {
34         struct cmd_list *prev, *next;
35         struct cmd_set *cmd_set;
36 } *cmd_list;
37
38 /****************************************************************************
39 handle completion of commands for readline
40 ****************************************************************************/
41 static char **completion_fn(const 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 = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
56         if (!matches) return NULL;
57
58         matches[count++] = SMB_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] = SMB_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         if (count == 2) {
82                 SAFE_FREE(matches[0]);
83                 matches[0] = SMB_STRDUP(matches[1]);
84         }
85         matches[count] = NULL;
86         return matches;
87 }
88
89 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
90 {
91         char *command;
92         char *p;
93
94         if (!cmdstr || !(*cmdstr))
95                 return NULL;
96
97         p = strchr_m(*cmdstr, ';');
98         if (p)
99                 *p = '\0';
100         command = talloc_strdup(ctx, *cmdstr);
101         *cmdstr = p;
102
103         return command;
104 }
105
106 /* Load specified configuration file */
107 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
108                         int argc, const char **argv)
109 {
110         if (argc != 2) {
111                 printf("Usage: %s <smb.conf>\n", argv[0]);
112                 return NT_STATUS_OK;
113         }
114
115         if (!lp_load(argv[1], False, True, False, True)) {
116                 printf("Error loading \"%s\"\n", argv[1]);
117                 return NT_STATUS_OK;
118         }
119
120         printf("\"%s\" successfully loaded\n", argv[1]);
121         return NT_STATUS_OK;
122 }
123
124 /* Display help on commands */
125 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
126                          int argc, const char **argv)
127 {
128         struct cmd_list *tmp;
129         struct cmd_set *tmp_set;
130
131         /* Usage */
132         if (argc > 2) {
133                 printf("Usage: %s [command]\n", argv[0]);
134                 return NT_STATUS_OK;
135         }
136
137         /* Help on one command */
138
139         if (argc == 2) {
140                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
141
142                         tmp_set = tmp->cmd_set;
143
144                         while(tmp_set->name) {
145                                 if (strequal(argv[1], tmp_set->name)) {
146                                         if (tmp_set->usage &&
147                                             tmp_set->usage[0])
148                                                 printf("%s\n", tmp_set->usage);
149                                         else
150                                                 printf("No help for %s\n", tmp_set->name);
151
152                                         return NT_STATUS_OK;
153                                 }
154
155                                 tmp_set++;
156                         }
157                 }
158
159                 printf("No such command: %s\n", argv[1]);
160                 return NT_STATUS_OK;
161         }
162
163         /* List all commands */
164
165         for (tmp = cmd_list; tmp; tmp = tmp->next) {
166
167                 tmp_set = tmp->cmd_set;
168
169                 while(tmp_set->name) {
170
171                         printf("%15s\t\t%s\n", tmp_set->name,
172                                tmp_set->description ? tmp_set->description:
173                                "");
174
175                         tmp_set++;
176                 }
177         }
178
179         return NT_STATUS_OK;
180 }
181
182 /* Change the debug level */
183 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
184 {
185         if (argc > 2) {
186                 printf("Usage: %s [debuglevel]\n", argv[0]);
187                 return NT_STATUS_OK;
188         }
189
190         if (argc == 2) {
191                 lp_set_cmdline("log level", argv[1]);
192         }
193
194         printf("debuglevel is %d\n", DEBUGLEVEL);
195
196         return NT_STATUS_OK;
197 }
198
199 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
200 {
201         /* Cleanup */
202         talloc_destroy(mem_ctx);
203         mem_ctx = NULL;
204         vfs->data = NULL;
205         vfs->data_size = 0;
206         return NT_STATUS_OK;
207 }
208
209 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
210 {
211         /* Cleanup */
212         talloc_destroy(mem_ctx);
213
214         exit(0);
215         return NT_STATUS_OK; /* NOTREACHED */
216 }
217
218 static struct cmd_set vfstest_commands[] = {
219
220         { "GENERAL OPTIONS" },
221
222         { "conf",       cmd_conf,       "Load smb configuration file", "conf <smb.conf>" },
223         { "help",       cmd_help,       "Get help on commands", "" },
224         { "?",          cmd_help,       "Get help on commands", "" },
225         { "debuglevel", cmd_debuglevel, "Set debug level", "" },
226         { "freemem",    cmd_freemem,    "Free currently allocated buffers", "" },
227         { "exit",       cmd_quit,       "Exit program", "" },
228         { "quit",       cmd_quit,       "Exit program", "" },
229
230         { NULL }
231 };
232
233 static struct cmd_set separator_command[] = {
234         { "---------------", NULL,      "----------------------" },
235         { NULL }
236 };
237
238
239 extern struct cmd_set vfs_commands[];
240 static struct cmd_set *vfstest_command_list[] = {
241         vfstest_commands,
242         vfs_commands,
243         NULL
244 };
245
246 static void add_command_set(struct cmd_set *cmd_set)
247 {
248         struct cmd_list *entry;
249
250         if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
251                 DEBUG(0, ("out of memory\n"));
252                 return;
253         }
254
255         ZERO_STRUCTP(entry);
256
257         entry->cmd_set = cmd_set;
258         DLIST_ADD(cmd_list, entry);
259 }
260
261 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
262 {
263         const char *p = cmd;
264         char **argv = NULL;
265         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
266         char *buf;
267         TALLOC_CTX *mem_ctx = talloc_stackframe();
268         int argc = 0, i;
269
270         /* Count number of arguments first time through the loop then
271            allocate memory and strdup them. */
272
273  again:
274         while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
275                 if (argv) {
276                         argv[argc] = SMB_STRDUP(buf);
277                 }
278                 argc++;
279         }
280
281         if (!argv) {
282                 /* Create argument list */
283
284                 argv = SMB_MALLOC_ARRAY(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                 /* Run command */
303                 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
304         } else {
305                 fprintf (stderr, "Invalid command\n");
306                 goto done;
307         }
308
309  done:
310
311         /* Cleanup */
312
313         if (argv) {
314                 for (i = 0; i < argc; i++)
315                         SAFE_FREE(argv[i]);
316
317                 SAFE_FREE(argv);
318         }
319
320         TALLOC_FREE(mem_ctx);
321         return result;
322 }
323
324 /* Process a command entered at the prompt or as part of -c */
325 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
326 {
327         struct cmd_list *temp_list;
328         bool found = False;
329         char *buf;
330         const char *p = cmd;
331         NTSTATUS result = NT_STATUS_OK;
332         TALLOC_CTX *mem_ctx = talloc_stackframe();
333         int len = 0;
334
335         if (cmd[strlen(cmd) - 1] == '\n')
336                 cmd[strlen(cmd) - 1] = '\0';
337
338         if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
339                 TALLOC_FREE(mem_ctx);
340                 return NT_STATUS_OK;
341         }
342
343         /* Strip the trailing \n if it exists */
344         len = strlen(buf);
345         if (buf[len-1] == '\n')
346                 buf[len-1] = '\0';
347
348         /* Search for matching commands */
349
350         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
351                 struct cmd_set *temp_set = temp_list->cmd_set;
352
353                 while(temp_set->name) {
354                         if (strequal(buf, temp_set->name)) {
355                                 found = True;
356                                 result = do_cmd(vfs, temp_set, cmd);
357
358                                 goto done;
359                         }
360                         temp_set++;
361                 }
362         }
363
364  done:
365         if (!found && buf[0]) {
366                 printf("command not found: %s\n", buf);
367                 TALLOC_FREE(mem_ctx);
368                 return NT_STATUS_OK;
369         }
370
371         if (!NT_STATUS_IS_OK(result)) {
372                 printf("result was %s\n", nt_errstr(result));
373         }
374
375         TALLOC_FREE(mem_ctx);
376         return result;
377 }
378
379 static void process_file(struct vfs_state *pvfs, char *filename) {
380         FILE *file;
381         char command[3 * PATH_MAX];
382
383         if (*filename == '-') {
384                 file = stdin;
385         } else {
386                 file = fopen(filename, "r");
387                 if (file == NULL) {
388                         printf("vfstest: error reading file (%s)!", filename);
389                         printf("errno n.%d: %s", errno, strerror(errno));
390                         exit(-1);
391                 }
392         }
393
394         while (fgets(command, 3 * PATH_MAX, file) != NULL) {
395                 process_cmd(pvfs, command);
396         }
397
398         if (file != stdin) {
399                 fclose(file);
400         }
401 }
402
403 void exit_server(const char *reason)
404 {
405         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
406         exit(0);
407 }
408
409 void exit_server_cleanly(const char *const reason)
410 {
411         exit_server("normal exit");
412 }
413
414 int last_message = -1;
415
416 /* Main function */
417
418 int main(int argc, char *argv[])
419 {
420         char *cmdstr = NULL;
421         struct cmd_set  **cmd_set;
422         struct vfs_state vfs = { 0, };
423         int i;
424         char *filename = NULL;
425         char cwd[MAXPATHLEN];
426         TALLOC_CTX *frame = talloc_stackframe();
427
428         /* make sure the vars that get altered (4th field) are in
429            a fixed location or certain compilers complain */
430         poptContext pc;
431         struct poptOption long_options[] = {
432                 POPT_AUTOHELP
433                 {"file",        'f', POPT_ARG_STRING,   &filename, 0, },
434                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 0, "Execute specified list of commands" },
435                 POPT_COMMON_SAMBA
436                 POPT_TABLEEND
437         };
438
439         load_case_tables();
440
441         setlinebuf(stdout);
442
443         pc = poptGetContext("vfstest", argc, (const char **) argv,
444                             long_options, 0);
445
446         while(poptGetNextOpt(pc) != -1);
447
448
449         poptFreeContext(pc);
450
451         lp_load_initial_only(get_dyn_CONFIGFILE());
452
453         /* TODO: check output */
454         reload_services(NULL, NULL, false);
455
456         /* the following functions are part of the Samba debugging
457            facilities.  See lib/debug.c */
458         setup_logging("vfstest", DEBUG_STDOUT);
459
460         /* Load command lists */
461
462         cmd_set = vfstest_command_list;
463
464         while(*cmd_set) {
465                 add_command_set(*cmd_set);
466                 add_command_set(separator_command);
467                 cmd_set++;
468         }
469
470         /* some basic initialization stuff */
471         sec_init();
472         vfs.conn = talloc_zero(NULL, connection_struct);
473         vfs.conn->params = talloc_zero(vfs.conn, struct share_params);
474         set_conn_connectpath(vfs.conn, getcwd(cwd, sizeof(cwd)));
475         for (i=0; i < 1024; i++)
476                 vfs.files[i] = NULL;
477
478         /* some advanced initialization stuff */
479         smbd_vfs_init(vfs.conn);
480
481         /* Do we have a file input? */
482         if (filename && filename[0]) {
483                 process_file(&vfs, filename);
484                 return 0;
485         }
486
487         /* Do anything specified with -c */
488         if (cmdstr && cmdstr[0]) {
489                 char    *cmd;
490                 char    *p = cmdstr;
491
492                 while((cmd=next_command(frame, &p)) != NULL) {
493                         process_cmd(&vfs, cmd);
494                 }
495
496                 TALLOC_FREE(cmd);
497                 return 0;
498         }
499
500         /* Loop around accepting commands */
501
502         while(1) {
503                 char *line = NULL;
504
505                 line = smb_readline("vfstest $> ", NULL, completion_fn);
506
507                 if (line == NULL) {
508                         break;
509                 }
510
511                 if (line[0] != '\n') {
512                         process_cmd(&vfs, line);
513                 }
514                 SAFE_FREE(line);
515         }
516
517         TALLOC_FREE(vfs.conn);
518         TALLOC_FREE(frame);
519         return 0;
520 }