s3-vfstest: Initialize some more
[vlendec/samba-autobuild/.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 "smbd/globals.h"
29 #include "popt_common.h"
30 #include "vfstest.h"
31 #include "../libcli/smbreadline/smbreadline.h"
32 #include "auth.h"
33 #include "serverid.h"
34 #include "messages.h"
35 #include "libcli/security/security.h"
36
37 /* List to hold groups of commands */
38 static struct cmd_list {
39         struct cmd_list *prev, *next;
40         struct cmd_set *cmd_set;
41 } *cmd_list;
42
43 /****************************************************************************
44 handle completion of commands for readline
45 ****************************************************************************/
46 static char **completion_fn(const char *text, int start, int end)
47 {
48 #define MAX_COMPLETIONS 100
49         char **matches;
50         int i, count=0;
51         struct cmd_list *commands = cmd_list;
52
53         if (start)
54                 return NULL;
55
56         /* make sure we have a list of valid commands */
57         if (!commands)
58                 return NULL;
59
60         matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
61         if (!matches) return NULL;
62
63         matches[count++] = SMB_STRDUP(text);
64         if (!matches[0]) return NULL;
65
66         while (commands && count < MAX_COMPLETIONS-1)
67         {
68                 if (!commands->cmd_set)
69                         break;
70
71                 for (i=0; commands->cmd_set[i].name; i++)
72                 {
73                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
74                                 commands->cmd_set[i].fn)
75                         {
76                                 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
77                                 if (!matches[count])
78                                         return NULL;
79                                 count++;
80                         }
81                 }
82
83                 commands = commands->next;
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(TALLOC_CTX *ctx, char **cmdstr)
95 {
96         char *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         command = talloc_strdup(ctx, *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                 lp_set_cmdline("log level", 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         char *buf;
272         TALLOC_CTX *mem_ctx = talloc_stackframe();
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_talloc(mem_ctx, &p, &buf, " ")) {
280                 if (argv) {
281                         argv[argc] = SMB_STRDUP(buf);
282                 }
283                 argc++;
284         }
285
286         if (!argv) {
287                 /* Create argument list */
288
289                 argv = SMB_MALLOC_ARRAY(char *, argc);
290                 memset(argv, 0, sizeof(char *) * argc);
291
292                 if (!argv) {
293                         fprintf(stderr, "out of memory\n");
294                         result = NT_STATUS_NO_MEMORY;
295                         goto done;
296                 }
297
298                 p = cmd;
299                 argc = 0;
300
301                 goto again;
302         }
303
304         /* Call the function */
305
306         if (cmd_entry->fn) {
307                 /* Run command */
308                 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
309         } else {
310                 fprintf (stderr, "Invalid command\n");
311                 goto done;
312         }
313
314  done:
315
316         /* Cleanup */
317
318         if (argv) {
319                 for (i = 0; i < argc; i++)
320                         SAFE_FREE(argv[i]);
321
322                 SAFE_FREE(argv);
323         }
324
325         TALLOC_FREE(mem_ctx);
326         return result;
327 }
328
329 /* Process a command entered at the prompt or as part of -c */
330 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
331 {
332         struct cmd_list *temp_list;
333         bool found = False;
334         char *buf;
335         const char *p = cmd;
336         NTSTATUS result = NT_STATUS_OK;
337         TALLOC_CTX *mem_ctx = talloc_stackframe();
338         int len = 0;
339
340         if (cmd[strlen(cmd) - 1] == '\n')
341                 cmd[strlen(cmd) - 1] = '\0';
342
343         if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
344                 TALLOC_FREE(mem_ctx);
345                 return NT_STATUS_OK;
346         }
347
348         /* Strip the trailing \n if it exists */
349         len = strlen(buf);
350         if (buf[len-1] == '\n')
351                 buf[len-1] = '\0';
352
353         /* Search for matching commands */
354
355         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
356                 struct cmd_set *temp_set = temp_list->cmd_set;
357
358                 while(temp_set->name) {
359                         if (strequal(buf, temp_set->name)) {
360                                 found = True;
361                                 result = do_cmd(vfs, temp_set, cmd);
362
363                                 goto done;
364                         }
365                         temp_set++;
366                 }
367         }
368
369  done:
370         if (!found && buf[0]) {
371                 printf("command not found: %s\n", buf);
372                 TALLOC_FREE(mem_ctx);
373                 return NT_STATUS_OK;
374         }
375
376         if (!NT_STATUS_IS_OK(result)) {
377                 printf("result was %s\n", nt_errstr(result));
378         }
379
380         TALLOC_FREE(mem_ctx);
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         if (file != stdin) {
404                 fclose(file);
405         }
406 }
407
408 void exit_server(const char *reason)
409 {
410         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
411         exit(0);
412 }
413
414 void exit_server_cleanly(const char *const reason)
415 {
416         exit_server("normal exit");
417 }
418
419 /* Main function */
420
421 int main(int argc, char *argv[])
422 {
423         char *cmdstr = NULL;
424         struct cmd_set  **cmd_set;
425         struct vfs_state vfs = { 0, };
426         int i;
427         char *filename = NULL;
428         char cwd[MAXPATHLEN];
429         TALLOC_CTX *frame = talloc_stackframe();
430         struct tevent_context *ev = tevent_context_init(NULL);
431
432         /* make sure the vars that get altered (4th field) are in
433            a fixed location or certain compilers complain */
434         poptContext pc;
435         struct poptOption long_options[] = {
436                 POPT_AUTOHELP
437                 {"file",        'f', POPT_ARG_STRING,   &filename, 0, },
438                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 0, "Execute specified list of commands" },
439                 POPT_COMMON_SAMBA
440                 POPT_TABLEEND
441         };
442
443         load_case_tables();
444
445         setlinebuf(stdout);
446
447         pc = poptGetContext("vfstest", argc, (const char **) argv,
448                             long_options, 0);
449
450         while(poptGetNextOpt(pc) != -1);
451
452
453         poptFreeContext(pc);
454
455         lp_load_initial_only(get_dyn_CONFIGFILE());
456
457         /* TODO: check output */
458         reload_services(NULL, NULL, false);
459
460         /* the following functions are part of the Samba debugging
461            facilities.  See lib/debug.c */
462         setup_logging("vfstest", DEBUG_STDOUT);
463
464         /* Load command lists */
465
466         cmd_set = vfstest_command_list;
467
468         while(*cmd_set) {
469                 add_command_set(*cmd_set);
470                 add_command_set(separator_command);
471                 cmd_set++;
472         }
473
474         /* some basic initialization stuff */
475         sec_init();
476         init_guest_info();
477         locking_init();
478         serverid_parent_init(NULL);
479         vfs.conn = talloc_zero(NULL, connection_struct);
480         vfs.conn->share_access = FILE_GENERIC_ALL;
481         vfs.conn->params = talloc_zero(vfs.conn, struct share_params);
482         vfs.conn->sconn = talloc_zero(NULL, struct smbd_server_connection);
483         vfs.conn->sconn->msg_ctx = messaging_init(vfs.conn->sconn, ev);
484         vfs.conn->sconn->ev_ctx = ev;
485         serverid_register(messaging_server_id(vfs.conn->sconn->msg_ctx), 0);
486         make_session_info_guest(NULL, &vfs.conn->session_info);
487         file_init(vfs.conn->sconn);
488         set_conn_connectpath(vfs.conn, getcwd(cwd, sizeof(cwd)));
489         for (i=0; i < 1024; i++)
490                 vfs.files[i] = NULL;
491
492         /* some advanced initialization stuff */
493         smbd_vfs_init(vfs.conn);
494
495         if (!posix_locking_init(false)) {
496                 return 1;
497         }
498
499         /* Do we have a file input? */
500         if (filename && filename[0]) {
501                 process_file(&vfs, filename);
502                 return 0;
503         }
504
505         /* Do anything specified with -c */
506         if (cmdstr && cmdstr[0]) {
507                 char    *cmd;
508                 char    *p = cmdstr;
509
510                 while((cmd=next_command(frame, &p)) != NULL) {
511                         process_cmd(&vfs, cmd);
512                 }
513
514                 TALLOC_FREE(cmd);
515                 return 0;
516         }
517
518         /* Loop around accepting commands */
519
520         while(1) {
521                 char *line = NULL;
522
523                 line = smb_readline("vfstest $> ", NULL, completion_fn);
524
525                 if (line == NULL) {
526                         break;
527                 }
528
529                 if (line[0] != '\n') {
530                         process_cmd(&vfs, line);
531                 }
532                 SAFE_FREE(line);
533         }
534
535         TALLOC_FREE(vfs.conn);
536         TALLOC_FREE(frame);
537         return 0;
538 }