smbd: Fix crossing automounter mount points
[Samba.git] / source3 / torture / vfstest.c
blobb25dfdc41d5089fa41c85149fc847a3402fcefdc
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 "locking/share_mode_lock.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "lib/cmdline/cmdline.h"
31 #include "vfstest.h"
32 #include "../libcli/smbreadline/smbreadline.h"
33 #include "auth.h"
34 #include "serverid.h"
35 #include "messages.h"
36 #include "libcli/security/security.h"
37 #include "lib/smbd_shim.h"
38 #include "system/filesys.h"
39 #include "lib/global_contexts.h"
40 #include "lib/param/param.h"
42 /* List to hold groups of commands */
43 static struct cmd_list {
44 struct cmd_list *prev, *next;
45 struct cmd_set *cmd_set;
46 } *cmd_list;
48 /* shall we do talloc_report after each command? */
49 static int memreports = 0;
51 /****************************************************************************
52 handle completion of commands for readline
53 ****************************************************************************/
54 static char **completion_fn(const char *text, int start, int end)
56 #define MAX_COMPLETIONS 100
57 char **matches;
58 int i, count=0;
59 struct cmd_list *commands = cmd_list;
61 if (start)
62 return NULL;
64 /* make sure we have a list of valid commands */
65 if (!commands)
66 return NULL;
68 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
69 if (!matches) return NULL;
71 matches[count++] = SMB_STRDUP(text);
72 if (!matches[0]) return NULL;
74 while (commands && count < MAX_COMPLETIONS-1)
76 if (!commands->cmd_set)
77 break;
79 for (i=0; commands->cmd_set[i].name; i++)
81 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
82 commands->cmd_set[i].fn)
84 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
85 if (!matches[count])
86 return NULL;
87 count++;
91 commands = commands->next;
94 if (count == 2) {
95 SAFE_FREE(matches[0]);
96 matches[0] = SMB_STRDUP(matches[1]);
98 matches[count] = NULL;
99 return matches;
102 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
104 char *command;
105 char *p;
107 if (!cmdstr || !(*cmdstr))
108 return NULL;
110 p = strchr_m(*cmdstr, ';');
111 if (p)
112 *p = '\0';
113 command = talloc_strdup(ctx, *cmdstr);
115 /* Pass back the remaining cmdstring
116 (a trailing delimiter ";" does also work),
117 or NULL at last cmdstring.
119 *cmdstr = p ? p + 1 : p;
121 return command;
124 /* Load specified configuration file */
125 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
126 int argc, const char **argv)
128 if (argc != 2) {
129 printf("Usage: %s <smb.conf>\n", argv[0]);
130 return NT_STATUS_OK;
133 if (!lp_load_with_shares(argv[1])) {
134 printf("Error loading \"%s\"\n", argv[1]);
135 return NT_STATUS_OK;
138 printf("\"%s\" successfully loaded\n", argv[1]);
139 return NT_STATUS_OK;
142 /* Display help on commands */
143 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
144 int argc, const char **argv)
146 struct cmd_list *tmp;
147 struct cmd_set *tmp_set;
149 /* Usage */
150 if (argc > 2) {
151 printf("Usage: %s [command]\n", argv[0]);
152 return NT_STATUS_OK;
155 /* Help on one command */
157 if (argc == 2) {
158 for (tmp = cmd_list; tmp; tmp = tmp->next) {
160 tmp_set = tmp->cmd_set;
162 while(tmp_set->name) {
163 if (strequal(argv[1], tmp_set->name)) {
164 if (tmp_set->usage &&
165 tmp_set->usage[0])
166 printf("%s\n", tmp_set->usage);
167 else
168 printf("No help for %s\n", tmp_set->name);
170 return NT_STATUS_OK;
173 tmp_set++;
177 printf("No such command: %s\n", argv[1]);
178 return NT_STATUS_OK;
181 /* List all commands */
183 for (tmp = cmd_list; tmp; tmp = tmp->next) {
185 tmp_set = tmp->cmd_set;
187 while(tmp_set->name) {
189 printf("%15s\t\t%s\n", tmp_set->name,
190 tmp_set->description ? tmp_set->description:
191 "");
193 tmp_set++;
197 return NT_STATUS_OK;
200 /* Change the debug level */
201 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
203 if (argc > 2) {
204 printf("Usage: %s [debuglevel]\n", argv[0]);
205 return NT_STATUS_OK;
208 if (argc == 2) {
209 struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
210 lpcfg_set_cmdline(lp_ctx, "log level", argv[1]);
213 printf("debuglevel is %d\n", DEBUGLEVEL);
215 return NT_STATUS_OK;
218 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
220 /* Cleanup */
221 talloc_destroy(mem_ctx);
222 mem_ctx = NULL;
223 vfs->data = NULL;
224 vfs->data_size = 0;
225 return NT_STATUS_OK;
228 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
230 /* Cleanup */
231 talloc_destroy(mem_ctx);
233 exit(0);
234 return NT_STATUS_OK; /* NOTREACHED */
237 static struct cmd_set vfstest_commands[] = {
239 { .name = "GENERAL OPTIONS" },
241 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
242 { "help", cmd_help, "Get help on commands", "" },
243 { "?", cmd_help, "Get help on commands", "" },
244 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
245 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
246 { "exit", cmd_quit, "Exit program", "" },
247 { "quit", cmd_quit, "Exit program", "" },
249 { .name = NULL }
252 static struct cmd_set separator_command[] = {
254 .name = "---------------",
255 .description = "----------------------"
258 .name = NULL,
263 extern struct cmd_set vfs_commands[];
264 static struct cmd_set *vfstest_command_list[] = {
265 vfstest_commands,
266 vfs_commands,
267 NULL
270 static void add_command_set(struct cmd_set *cmd_set)
272 struct cmd_list *entry;
274 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
275 DEBUG(0, ("out of memory\n"));
276 return;
279 ZERO_STRUCTP(entry);
281 entry->cmd_set = cmd_set;
282 DLIST_ADD(cmd_list, entry);
285 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
287 const char *p = cmd;
288 const char **argv = NULL;
289 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
290 char *buf;
291 TALLOC_CTX *mem_ctx = talloc_stackframe();
292 int argc = 0;
294 /* Count number of arguments first time through the loop then
295 allocate memory and strdup them. */
297 again:
298 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
299 if (argv) {
300 argv[argc] = talloc_strdup(argv, buf);
302 argc++;
305 if (!argv) {
306 /* Create argument list */
308 argv = talloc_zero_array(mem_ctx, const char *, argc);
309 if (argv == NULL) {
310 fprintf(stderr, "out of memory\n");
311 result = NT_STATUS_NO_MEMORY;
312 goto done;
315 p = cmd;
316 argc = 0;
318 goto again;
321 /* Call the function */
323 if (cmd_entry->fn) {
324 /* Run command */
325 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
326 } else {
327 fprintf (stderr, "Invalid command\n");
328 goto done;
331 done:
333 /* Cleanup */
335 if (argv) {
336 char **_argv = discard_const_p(char *, argv);
337 TALLOC_FREE(_argv);
338 argv = NULL;
341 if (memreports != 0) {
342 talloc_report_full(mem_ctx, stdout);
344 TALLOC_FREE(mem_ctx);
345 return result;
348 /* Process a command entered at the prompt or as part of -c */
349 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
351 struct cmd_list *temp_list;
352 bool found = False;
353 char *buf;
354 const char *p = cmd;
355 NTSTATUS result = NT_STATUS_OK;
356 TALLOC_CTX *mem_ctx = talloc_stackframe();
357 int len = 0;
359 if (cmd[strlen(cmd) - 1] == '\n')
360 cmd[strlen(cmd) - 1] = '\0';
362 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
363 TALLOC_FREE(mem_ctx);
364 return NT_STATUS_OK;
367 /* Strip the trailing \n if it exists */
368 len = strlen(buf);
369 if (buf[len-1] == '\n')
370 buf[len-1] = '\0';
372 /* Search for matching commands */
374 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
375 struct cmd_set *temp_set = temp_list->cmd_set;
377 while(temp_set->name) {
378 if (strequal(buf, temp_set->name)) {
379 found = True;
380 result = do_cmd(vfs, temp_set, cmd);
382 goto done;
384 temp_set++;
388 done:
389 if (!found && buf[0]) {
390 printf("command not found: %s\n", buf);
391 TALLOC_FREE(mem_ctx);
392 return NT_STATUS_OK;
395 if (!NT_STATUS_IS_OK(result)) {
396 printf("result was %s\n", nt_errstr(result));
399 TALLOC_FREE(mem_ctx);
400 return result;
403 static void process_file(struct vfs_state *pvfs, char *filename) {
404 FILE *file;
405 char command[3 * PATH_MAX];
407 if (*filename == '-') {
408 file = stdin;
409 } else {
410 file = fopen(filename, "r");
411 if (file == NULL) {
412 printf("vfstest: error reading file (%s)!", filename);
413 printf("errno n.%d: %s", errno, strerror(errno));
414 exit(-1);
418 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
419 process_cmd(pvfs, command);
422 if (file != stdin) {
423 fclose(file);
427 static void vfstest_exit_server(const char * const reason) _NORETURN_;
428 static void vfstest_exit_server(const char * const reason)
430 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
431 exit(0);
434 static void vfstest_exit_server_cleanly(const char * const reason) _NORETURN_;
435 static void vfstest_exit_server_cleanly(const char * const reason)
437 vfstest_exit_server("normal exit");
440 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
441 struct vfs_state *vfs)
443 struct smb_request *result;
444 uint8_t *inbuf;
446 result = talloc_zero(mem_ctx, struct smb_request);
447 if (result == NULL) {
448 return NULL;
450 result->sconn = vfs->conn->sconn;
451 result->mid = ++vfs->mid;
453 inbuf = talloc_array(result, uint8_t, smb_size);
454 if (inbuf == NULL) {
455 goto fail;
457 SSVAL(inbuf, smb_mid, result->mid);
458 smb_setlen(inbuf, smb_size-4);
459 result->inbuf = inbuf;
460 return result;
461 fail:
462 TALLOC_FREE(result);
463 return NULL;
466 /* Main function */
468 int main(int argc, const char *argv[])
470 char *cmdstr = NULL;
471 struct cmd_set **cmd_set;
472 struct conn_struct_tos *c = NULL;
473 struct vfs_state *vfs;
474 int opt;
475 int i;
476 char *filename = NULL;
477 char *cwd = NULL;
478 TALLOC_CTX *frame = talloc_stackframe();
479 struct auth_session_info *session_info = NULL;
480 NTSTATUS status = NT_STATUS_OK;
481 bool ok;
483 /* make sure the vars that get altered (4th field) are in
484 a fixed location or certain compilers complain */
485 poptContext pc;
486 struct poptOption long_options[] = {
487 POPT_AUTOHELP
489 .longName = "file",
490 .shortName = 'f',
491 .argInfo = POPT_ARG_STRING,
492 .arg = &filename,
495 .longName = "command",
496 .shortName = 'c',
497 .argInfo = POPT_ARG_STRING,
498 .arg = &cmdstr,
499 .val = 0,
500 .descrip = "Execute specified list of commands",
503 .longName = "memreport",
504 .shortName = 'm',
505 .argInfo = POPT_ARG_INT,
506 .arg = &memreports,
507 .descrip = "Report memory left on talloc stackframe after each command",
509 POPT_COMMON_SAMBA
510 POPT_COMMON_VERSION
511 POPT_TABLEEND
513 static const struct smbd_shim vfstest_shim_fns =
515 .exit_server = vfstest_exit_server,
516 .exit_server_cleanly = vfstest_exit_server_cleanly,
519 smb_init_locale();
521 setlinebuf(stdout);
523 ok = samba_cmdline_init(frame,
524 SAMBA_CMDLINE_CONFIG_SERVER,
525 true /* require_smbconf */);
526 if (!ok) {
527 TALLOC_FREE(frame);
528 exit(1);
531 pc = samba_popt_get_context("vfstest", argc, argv, long_options, 0);
532 if (pc == NULL) {
533 TALLOC_FREE(frame);
534 exit(1);
537 while ((opt = poptGetNextOpt(pc)) != -1) {
538 switch (opt) {
539 case POPT_ERROR_BADOPT:
540 fprintf(stderr, "\nInvalid option %s: %s\n\n",
541 poptBadOption(pc, 0), poptStrerror(opt));
542 poptPrintUsage(pc, stderr, 0);
543 exit(1);
547 poptFreeContext(pc);
549 /* we want total control over the permissions on created files,
550 so set our umask to 0 */
551 umask(0);
553 /* TODO: check output */
554 reload_services(NULL, NULL, false);
556 per_thread_cwd_check();
558 set_smbd_shim(&vfstest_shim_fns);
560 /* Load command lists */
562 cmd_set = vfstest_command_list;
564 while(*cmd_set) {
565 add_command_set(*cmd_set);
566 add_command_set(separator_command);
567 cmd_set++;
570 /* some basic initialization stuff */
571 sec_init();
572 init_guest_session_info(frame);
573 locking_init();
574 vfs = talloc_zero(frame, struct vfs_state);
575 if (vfs == NULL) {
576 return 1;
578 status = make_session_info_guest(vfs, &session_info);
579 if (!NT_STATUS_IS_OK(status)) {
580 return 1;
583 /* Provided by libreplace if not present. Always mallocs. */
584 cwd = get_current_dir_name();
585 if (cwd == NULL) {
586 return -1;
589 status = create_conn_struct_tos_cwd(global_messaging_context(),
591 cwd,
592 session_info,
593 &c);
594 SAFE_FREE(cwd);
595 if (!NT_STATUS_IS_OK(status)) {
596 return 1;
598 vfs->conn = c->conn;
600 vfs->conn->share_access = FILE_GENERIC_ALL;
601 vfs->conn->read_only = false;
603 file_init(vfs->conn->sconn);
604 for (i=0; i < 1024; i++)
605 vfs->files[i] = NULL;
607 if (!posix_locking_init(false)) {
608 return 1;
611 /* Do we have a file input? */
612 if (filename && filename[0]) {
613 process_file(vfs, filename);
614 return 0;
617 /* Do anything specified with -c */
618 if (cmdstr && cmdstr[0]) {
619 char *cmd;
620 char *p = cmdstr;
622 while((cmd=next_command(frame, &p)) != NULL) {
623 status = process_cmd(vfs, cmd);
626 TALLOC_FREE(cmd);
627 return NT_STATUS_IS_OK(status) ? 0 : 1;
630 /* Loop around accepting commands */
632 while(1) {
633 char *line = NULL;
635 line = smb_readline("vfstest $> ", NULL, completion_fn);
637 if (line == NULL) {
638 break;
641 if (line[0] != '\n') {
642 status = process_cmd(vfs, line);
644 SAFE_FREE(line);
647 TALLOC_FREE(vfs);
648 TALLOC_FREE(frame);
649 return NT_STATUS_IS_OK(status) ? 0 : 1;