libcli/cldap: make use of samba_tevent_context_init()
[Samba/gebeck_regimport.git] / source3 / torture / vfstest.c
blob439e186c90d24505638065237cd98197c149fe79
1 /*
2 Unix SMB/CIFS implementation.
3 VFS module tester
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
7 Copyright (C) Jelmer Vernooij 2002,2003
9 Most of this code was ripped off of rpcclient.
10 Copyright (C) Tim Potter 2000-2001
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.
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.
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/>.
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 #include "lib/smbd_shim.h"
37 #include "system/filesys.h"
39 /* List to hold groups of commands */
40 static struct cmd_list {
41 struct cmd_list *prev, *next;
42 struct cmd_set *cmd_set;
43 } *cmd_list;
45 /* shall we do talloc_report after each command? */
46 static int memreports = 0;
48 /****************************************************************************
49 handle completion of commands for readline
50 ****************************************************************************/
51 static char **completion_fn(const char *text, int start, int end)
53 #define MAX_COMPLETIONS 100
54 char **matches;
55 int i, count=0;
56 struct cmd_list *commands = cmd_list;
58 if (start)
59 return NULL;
61 /* make sure we have a list of valid commands */
62 if (!commands)
63 return NULL;
65 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
66 if (!matches) return NULL;
68 matches[count++] = SMB_STRDUP(text);
69 if (!matches[0]) return NULL;
71 while (commands && count < MAX_COMPLETIONS-1)
73 if (!commands->cmd_set)
74 break;
76 for (i=0; commands->cmd_set[i].name; i++)
78 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
79 commands->cmd_set[i].fn)
81 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
82 if (!matches[count])
83 return NULL;
84 count++;
88 commands = commands->next;
91 if (count == 2) {
92 SAFE_FREE(matches[0]);
93 matches[0] = SMB_STRDUP(matches[1]);
95 matches[count] = NULL;
96 return matches;
99 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
101 char *command;
102 char *p;
104 if (!cmdstr || !(*cmdstr))
105 return NULL;
107 p = strchr_m(*cmdstr, ';');
108 if (p)
109 *p = '\0';
110 command = talloc_strdup(ctx, *cmdstr);
111 *cmdstr = p;
113 return command;
116 /* Load specified configuration file */
117 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
118 int argc, const char **argv)
120 if (argc != 2) {
121 printf("Usage: %s <smb.conf>\n", argv[0]);
122 return NT_STATUS_OK;
125 if (!lp_load(argv[1], False, True, False, True)) {
126 printf("Error loading \"%s\"\n", argv[1]);
127 return NT_STATUS_OK;
130 printf("\"%s\" successfully loaded\n", argv[1]);
131 return NT_STATUS_OK;
134 /* Display help on commands */
135 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
136 int argc, const char **argv)
138 struct cmd_list *tmp;
139 struct cmd_set *tmp_set;
141 /* Usage */
142 if (argc > 2) {
143 printf("Usage: %s [command]\n", argv[0]);
144 return NT_STATUS_OK;
147 /* Help on one command */
149 if (argc == 2) {
150 for (tmp = cmd_list; tmp; tmp = tmp->next) {
152 tmp_set = tmp->cmd_set;
154 while(tmp_set->name) {
155 if (strequal(argv[1], tmp_set->name)) {
156 if (tmp_set->usage &&
157 tmp_set->usage[0])
158 printf("%s\n", tmp_set->usage);
159 else
160 printf("No help for %s\n", tmp_set->name);
162 return NT_STATUS_OK;
165 tmp_set++;
169 printf("No such command: %s\n", argv[1]);
170 return NT_STATUS_OK;
173 /* List all commands */
175 for (tmp = cmd_list; tmp; tmp = tmp->next) {
177 tmp_set = tmp->cmd_set;
179 while(tmp_set->name) {
181 printf("%15s\t\t%s\n", tmp_set->name,
182 tmp_set->description ? tmp_set->description:
183 "");
185 tmp_set++;
189 return NT_STATUS_OK;
192 /* Change the debug level */
193 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
195 if (argc > 2) {
196 printf("Usage: %s [debuglevel]\n", argv[0]);
197 return NT_STATUS_OK;
200 if (argc == 2) {
201 lp_set_cmdline("log level", argv[1]);
204 printf("debuglevel is %d\n", DEBUGLEVEL);
206 return NT_STATUS_OK;
209 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
211 /* Cleanup */
212 talloc_destroy(mem_ctx);
213 mem_ctx = NULL;
214 vfs->data = NULL;
215 vfs->data_size = 0;
216 return NT_STATUS_OK;
219 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
221 /* Cleanup */
222 talloc_destroy(mem_ctx);
224 exit(0);
225 return NT_STATUS_OK; /* NOTREACHED */
228 static struct cmd_set vfstest_commands[] = {
230 { "GENERAL OPTIONS" },
232 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
233 { "help", cmd_help, "Get help on commands", "" },
234 { "?", cmd_help, "Get help on commands", "" },
235 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
236 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
237 { "exit", cmd_quit, "Exit program", "" },
238 { "quit", cmd_quit, "Exit program", "" },
240 { NULL }
243 static struct cmd_set separator_command[] = {
244 { "---------------", NULL, "----------------------" },
245 { NULL }
249 extern struct cmd_set vfs_commands[];
250 static struct cmd_set *vfstest_command_list[] = {
251 vfstest_commands,
252 vfs_commands,
253 NULL
256 static void add_command_set(struct cmd_set *cmd_set)
258 struct cmd_list *entry;
260 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
261 DEBUG(0, ("out of memory\n"));
262 return;
265 ZERO_STRUCTP(entry);
267 entry->cmd_set = cmd_set;
268 DLIST_ADD(cmd_list, entry);
271 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
273 const char *p = cmd;
274 char **argv = NULL;
275 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
276 char *buf;
277 TALLOC_CTX *mem_ctx = talloc_stackframe();
278 int argc = 0, i;
280 /* Count number of arguments first time through the loop then
281 allocate memory and strdup them. */
283 again:
284 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
285 if (argv) {
286 argv[argc] = SMB_STRDUP(buf);
288 argc++;
291 if (!argv) {
292 /* Create argument list */
294 argv = SMB_MALLOC_ARRAY(char *, argc);
295 memset(argv, 0, sizeof(char *) * argc);
297 if (!argv) {
298 fprintf(stderr, "out of memory\n");
299 result = NT_STATUS_NO_MEMORY;
300 goto done;
303 p = cmd;
304 argc = 0;
306 goto again;
309 /* Call the function */
311 if (cmd_entry->fn) {
312 /* Run command */
313 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
314 } else {
315 fprintf (stderr, "Invalid command\n");
316 goto done;
319 done:
321 /* Cleanup */
323 if (argv) {
324 for (i = 0; i < argc; i++)
325 SAFE_FREE(argv[i]);
327 SAFE_FREE(argv);
330 if (memreports != 0) {
331 talloc_report_full(mem_ctx, stdout);
333 TALLOC_FREE(mem_ctx);
334 return result;
337 /* Process a command entered at the prompt or as part of -c */
338 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
340 struct cmd_list *temp_list;
341 bool found = False;
342 char *buf;
343 const char *p = cmd;
344 NTSTATUS result = NT_STATUS_OK;
345 TALLOC_CTX *mem_ctx = talloc_stackframe();
346 int len = 0;
348 if (cmd[strlen(cmd) - 1] == '\n')
349 cmd[strlen(cmd) - 1] = '\0';
351 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
352 TALLOC_FREE(mem_ctx);
353 return NT_STATUS_OK;
356 /* Strip the trailing \n if it exists */
357 len = strlen(buf);
358 if (buf[len-1] == '\n')
359 buf[len-1] = '\0';
361 /* Search for matching commands */
363 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
364 struct cmd_set *temp_set = temp_list->cmd_set;
366 while(temp_set->name) {
367 if (strequal(buf, temp_set->name)) {
368 found = True;
369 result = do_cmd(vfs, temp_set, cmd);
371 goto done;
373 temp_set++;
377 done:
378 if (!found && buf[0]) {
379 printf("command not found: %s\n", buf);
380 TALLOC_FREE(mem_ctx);
381 return NT_STATUS_OK;
384 if (!NT_STATUS_IS_OK(result)) {
385 printf("result was %s\n", nt_errstr(result));
388 TALLOC_FREE(mem_ctx);
389 return result;
392 static void process_file(struct vfs_state *pvfs, char *filename) {
393 FILE *file;
394 char command[3 * PATH_MAX];
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);
407 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
408 process_cmd(pvfs, command);
411 if (file != stdin) {
412 fclose(file);
416 static void vfstest_exit_server(const char * const reason)
418 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
419 exit(0);
422 static void vfstest_exit_server_cleanly(const char * const reason)
424 vfstest_exit_server("normal exit");
427 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
428 struct vfs_state *vfs)
430 struct smb_request *result;
432 result = talloc_zero(mem_ctx, struct smb_request);
433 if (result == NULL) {
434 return NULL;
436 result->sconn = vfs->conn->sconn;
437 result->mid = ++vfs->mid;
439 result->inbuf = talloc_array(result, uint8_t, smb_size);
440 if (result->inbuf == NULL) {
441 goto fail;
443 SSVAL(result->inbuf, smb_mid, result->mid);
444 smb_setlen(result->inbuf, smb_size-4);
445 return result;
446 fail:
447 TALLOC_FREE(result);
448 return NULL;
451 /* Main function */
453 int main(int argc, char *argv[])
455 char *cmdstr = NULL;
456 struct cmd_set **cmd_set;
457 struct vfs_state *vfs;
458 int i;
459 char *filename = NULL;
460 char cwd[MAXPATHLEN];
461 TALLOC_CTX *frame = talloc_stackframe();
462 struct tevent_context *ev = tevent_context_init(NULL);
463 struct auth_session_info *session_info = NULL;
464 NTSTATUS status = NT_STATUS_OK;
466 /* make sure the vars that get altered (4th field) are in
467 a fixed location or certain compilers complain */
468 poptContext pc;
469 struct poptOption long_options[] = {
470 POPT_AUTOHELP
471 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
472 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
473 {"memreport", 'm', POPT_ARG_INT, &memreports, 0,
474 "Report memory left on talloc stackframe after each command" },
475 POPT_COMMON_SAMBA
476 POPT_TABLEEND
478 static const struct smbd_shim vfstest_shim_fns =
480 .exit_server = vfstest_exit_server,
481 .exit_server_cleanly = vfstest_exit_server_cleanly,
484 load_case_tables();
486 setlinebuf(stdout);
488 pc = poptGetContext("vfstest", argc, (const char **) argv,
489 long_options, 0);
491 while(poptGetNextOpt(pc) != -1);
494 poptFreeContext(pc);
496 /* we want total control over the permissions on created files,
497 so set our umask to 0 */
498 umask(0);
500 lp_load_initial_only(get_dyn_CONFIGFILE());
502 /* TODO: check output */
503 reload_services(NULL, NULL, false);
505 /* the following functions are part of the Samba debugging
506 facilities. See lib/debug.c */
507 setup_logging("vfstest", DEBUG_STDOUT);
509 set_smbd_shim(&vfstest_shim_fns);
511 /* Load command lists */
513 cmd_set = vfstest_command_list;
515 while(*cmd_set) {
516 add_command_set(*cmd_set);
517 add_command_set(separator_command);
518 cmd_set++;
521 /* some basic initialization stuff */
522 sec_init();
523 init_guest_info();
524 locking_init();
525 serverid_parent_init(NULL);
526 vfs = talloc_zero(NULL, struct vfs_state);
527 if (vfs == NULL) {
528 return 1;
530 status = make_session_info_guest(vfs, &session_info);
531 if (!NT_STATUS_IS_OK(status)) {
532 return 1;
535 status = create_conn_struct(vfs,
537 messaging_init(vfs, ev),
538 &vfs->conn,
540 getcwd(cwd, sizeof(cwd)),
541 session_info);
542 if (!NT_STATUS_IS_OK(status)) {
543 return 1;
546 vfs->conn->share_access = FILE_GENERIC_ALL;
547 vfs->conn->read_only = false;
549 serverid_register(messaging_server_id(vfs->conn->sconn->msg_ctx), 0);
550 file_init(vfs->conn->sconn);
551 for (i=0; i < 1024; i++)
552 vfs->files[i] = NULL;
554 if (!posix_locking_init(false)) {
555 return 1;
558 /* Do we have a file input? */
559 if (filename && filename[0]) {
560 process_file(vfs, filename);
561 return 0;
564 /* Do anything specified with -c */
565 if (cmdstr && cmdstr[0]) {
566 char *cmd;
567 char *p = cmdstr;
569 while((cmd=next_command(frame, &p)) != NULL) {
570 status = process_cmd(vfs, cmd);
573 TALLOC_FREE(cmd);
574 return NT_STATUS_IS_OK(status) ? 0 : 1;
577 /* Loop around accepting commands */
579 while(1) {
580 char *line = NULL;
582 line = smb_readline("vfstest $> ", NULL, completion_fn);
584 if (line == NULL) {
585 break;
588 if (line[0] != '\n') {
589 status = process_cmd(vfs, line);
591 SAFE_FREE(line);
594 TALLOC_FREE(vfs);
595 TALLOC_FREE(frame);
596 return NT_STATUS_IS_OK(status) ? 0 : 1;