ldb: change the version to 2.8.0 for Samba 4.19
[Samba.git] / source3 / torture / vfstest.c
blob3f01c4312893451ac842c2d62ebd910968f464d6
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"
41 /* List to hold groups of commands */
42 static struct cmd_list {
43 struct cmd_list *prev, *next;
44 struct cmd_set *cmd_set;
45 } *cmd_list;
47 /* shall we do talloc_report after each command? */
48 static int memreports = 0;
50 /****************************************************************************
51 handle completion of commands for readline
52 ****************************************************************************/
53 static char **completion_fn(const char *text, int start, int end)
55 #define MAX_COMPLETIONS 100
56 char **matches;
57 int i, count=0;
58 struct cmd_list *commands = cmd_list;
60 if (start)
61 return NULL;
63 /* make sure we have a list of valid commands */
64 if (!commands)
65 return NULL;
67 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
68 if (!matches) return NULL;
70 matches[count++] = SMB_STRDUP(text);
71 if (!matches[0]) return NULL;
73 while (commands && count < MAX_COMPLETIONS-1)
75 if (!commands->cmd_set)
76 break;
78 for (i=0; commands->cmd_set[i].name; i++)
80 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
81 commands->cmd_set[i].fn)
83 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
84 if (!matches[count])
85 return NULL;
86 count++;
90 commands = commands->next;
93 if (count == 2) {
94 SAFE_FREE(matches[0]);
95 matches[0] = SMB_STRDUP(matches[1]);
97 matches[count] = NULL;
98 return matches;
101 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
103 char *command;
104 char *p;
106 if (!cmdstr || !(*cmdstr))
107 return NULL;
109 p = strchr_m(*cmdstr, ';');
110 if (p)
111 *p = '\0';
112 command = talloc_strdup(ctx, *cmdstr);
114 /* Pass back the remaining cmdstring
115 (a trailing delimiter ";" does also work),
116 or NULL at last cmdstring.
118 *cmdstr = p ? p + 1 : p;
120 return command;
123 /* Load specified configuration file */
124 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
125 int argc, const char **argv)
127 if (argc != 2) {
128 printf("Usage: %s <smb.conf>\n", argv[0]);
129 return NT_STATUS_OK;
132 if (!lp_load_with_shares(argv[1])) {
133 printf("Error loading \"%s\"\n", argv[1]);
134 return NT_STATUS_OK;
137 printf("\"%s\" successfully loaded\n", argv[1]);
138 return NT_STATUS_OK;
141 /* Display help on commands */
142 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
143 int argc, const char **argv)
145 struct cmd_list *tmp;
146 struct cmd_set *tmp_set;
148 /* Usage */
149 if (argc > 2) {
150 printf("Usage: %s [command]\n", argv[0]);
151 return NT_STATUS_OK;
154 /* Help on one command */
156 if (argc == 2) {
157 for (tmp = cmd_list; tmp; tmp = tmp->next) {
159 tmp_set = tmp->cmd_set;
161 while(tmp_set->name) {
162 if (strequal(argv[1], tmp_set->name)) {
163 if (tmp_set->usage &&
164 tmp_set->usage[0])
165 printf("%s\n", tmp_set->usage);
166 else
167 printf("No help for %s\n", tmp_set->name);
169 return NT_STATUS_OK;
172 tmp_set++;
176 printf("No such command: %s\n", argv[1]);
177 return NT_STATUS_OK;
180 /* List all commands */
182 for (tmp = cmd_list; tmp; tmp = tmp->next) {
184 tmp_set = tmp->cmd_set;
186 while(tmp_set->name) {
188 printf("%15s\t\t%s\n", tmp_set->name,
189 tmp_set->description ? tmp_set->description:
190 "");
192 tmp_set++;
196 return NT_STATUS_OK;
199 /* Change the debug level */
200 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
202 if (argc > 2) {
203 printf("Usage: %s [debuglevel]\n", argv[0]);
204 return NT_STATUS_OK;
207 if (argc == 2) {
208 lp_set_cmdline("log level", argv[1]);
211 printf("debuglevel is %d\n", DEBUGLEVEL);
213 return NT_STATUS_OK;
216 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
218 /* Cleanup */
219 talloc_destroy(mem_ctx);
220 mem_ctx = NULL;
221 vfs->data = NULL;
222 vfs->data_size = 0;
223 return NT_STATUS_OK;
226 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
228 /* Cleanup */
229 talloc_destroy(mem_ctx);
231 exit(0);
232 return NT_STATUS_OK; /* NOTREACHED */
235 static struct cmd_set vfstest_commands[] = {
237 { .name = "GENERAL OPTIONS" },
239 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
240 { "help", cmd_help, "Get help on commands", "" },
241 { "?", cmd_help, "Get help on commands", "" },
242 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
243 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
244 { "exit", cmd_quit, "Exit program", "" },
245 { "quit", cmd_quit, "Exit program", "" },
247 { .name = NULL }
250 static struct cmd_set separator_command[] = {
252 .name = "---------------",
253 .description = "----------------------"
256 .name = NULL,
261 extern struct cmd_set vfs_commands[];
262 static struct cmd_set *vfstest_command_list[] = {
263 vfstest_commands,
264 vfs_commands,
265 NULL
268 static void add_command_set(struct cmd_set *cmd_set)
270 struct cmd_list *entry;
272 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
273 DEBUG(0, ("out of memory\n"));
274 return;
277 ZERO_STRUCTP(entry);
279 entry->cmd_set = cmd_set;
280 DLIST_ADD(cmd_list, entry);
283 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
285 const char *p = cmd;
286 const char **argv = NULL;
287 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
288 char *buf;
289 TALLOC_CTX *mem_ctx = talloc_stackframe();
290 int argc = 0;
292 /* Count number of arguments first time through the loop then
293 allocate memory and strdup them. */
295 again:
296 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
297 if (argv) {
298 argv[argc] = talloc_strdup(argv, buf);
300 argc++;
303 if (!argv) {
304 /* Create argument list */
306 argv = talloc_zero_array(mem_ctx, const char *, argc);
307 if (argv == NULL) {
308 fprintf(stderr, "out of memory\n");
309 result = NT_STATUS_NO_MEMORY;
310 goto done;
313 p = cmd;
314 argc = 0;
316 goto again;
319 /* Call the function */
321 if (cmd_entry->fn) {
322 /* Run command */
323 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
324 } else {
325 fprintf (stderr, "Invalid command\n");
326 goto done;
329 done:
331 /* Cleanup */
333 if (argv) {
334 char **_argv = discard_const_p(char *, argv);
335 TALLOC_FREE(_argv);
336 argv = NULL;
339 if (memreports != 0) {
340 talloc_report_full(mem_ctx, stdout);
342 TALLOC_FREE(mem_ctx);
343 return result;
346 /* Process a command entered at the prompt or as part of -c */
347 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
349 struct cmd_list *temp_list;
350 bool found = False;
351 char *buf;
352 const char *p = cmd;
353 NTSTATUS result = NT_STATUS_OK;
354 TALLOC_CTX *mem_ctx = talloc_stackframe();
355 int len = 0;
357 if (cmd[strlen(cmd) - 1] == '\n')
358 cmd[strlen(cmd) - 1] = '\0';
360 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
361 TALLOC_FREE(mem_ctx);
362 return NT_STATUS_OK;
365 /* Strip the trailing \n if it exists */
366 len = strlen(buf);
367 if (buf[len-1] == '\n')
368 buf[len-1] = '\0';
370 /* Search for matching commands */
372 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
373 struct cmd_set *temp_set = temp_list->cmd_set;
375 while(temp_set->name) {
376 if (strequal(buf, temp_set->name)) {
377 found = True;
378 result = do_cmd(vfs, temp_set, cmd);
380 goto done;
382 temp_set++;
386 done:
387 if (!found && buf[0]) {
388 printf("command not found: %s\n", buf);
389 TALLOC_FREE(mem_ctx);
390 return NT_STATUS_OK;
393 if (!NT_STATUS_IS_OK(result)) {
394 printf("result was %s\n", nt_errstr(result));
397 TALLOC_FREE(mem_ctx);
398 return result;
401 static void process_file(struct vfs_state *pvfs, char *filename) {
402 FILE *file;
403 char command[3 * PATH_MAX];
405 if (*filename == '-') {
406 file = stdin;
407 } else {
408 file = fopen(filename, "r");
409 if (file == NULL) {
410 printf("vfstest: error reading file (%s)!", filename);
411 printf("errno n.%d: %s", errno, strerror(errno));
412 exit(-1);
416 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
417 process_cmd(pvfs, command);
420 if (file != stdin) {
421 fclose(file);
425 static void vfstest_exit_server(const char * const reason) _NORETURN_;
426 static void vfstest_exit_server(const char * const reason)
428 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
429 exit(0);
432 static void vfstest_exit_server_cleanly(const char * const reason) _NORETURN_;
433 static void vfstest_exit_server_cleanly(const char * const reason)
435 vfstest_exit_server("normal exit");
438 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
439 struct vfs_state *vfs)
441 struct smb_request *result;
442 uint8_t *inbuf;
444 result = talloc_zero(mem_ctx, struct smb_request);
445 if (result == NULL) {
446 return NULL;
448 result->sconn = vfs->conn->sconn;
449 result->mid = ++vfs->mid;
451 inbuf = talloc_array(result, uint8_t, smb_size);
452 if (inbuf == NULL) {
453 goto fail;
455 SSVAL(inbuf, smb_mid, result->mid);
456 smb_setlen(inbuf, smb_size-4);
457 result->inbuf = inbuf;
458 return result;
459 fail:
460 TALLOC_FREE(result);
461 return NULL;
464 /* Main function */
466 int main(int argc, const char *argv[])
468 char *cmdstr = NULL;
469 struct cmd_set **cmd_set;
470 struct conn_struct_tos *c = NULL;
471 struct vfs_state *vfs;
472 int opt;
473 int i;
474 char *filename = NULL;
475 char *cwd = NULL;
476 TALLOC_CTX *frame = talloc_stackframe();
477 struct auth_session_info *session_info = NULL;
478 NTSTATUS status = NT_STATUS_OK;
479 bool ok;
481 /* make sure the vars that get altered (4th field) are in
482 a fixed location or certain compilers complain */
483 poptContext pc;
484 struct poptOption long_options[] = {
485 POPT_AUTOHELP
487 .longName = "file",
488 .shortName = 'f',
489 .argInfo = POPT_ARG_STRING,
490 .arg = &filename,
493 .longName = "command",
494 .shortName = 'c',
495 .argInfo = POPT_ARG_STRING,
496 .arg = &cmdstr,
497 .val = 0,
498 .descrip = "Execute specified list of commands",
501 .longName = "memreport",
502 .shortName = 'm',
503 .argInfo = POPT_ARG_INT,
504 .arg = &memreports,
505 .descrip = "Report memory left on talloc stackframe after each command",
507 POPT_COMMON_SAMBA
508 POPT_COMMON_VERSION
509 POPT_TABLEEND
511 static const struct smbd_shim vfstest_shim_fns =
513 .exit_server = vfstest_exit_server,
514 .exit_server_cleanly = vfstest_exit_server_cleanly,
517 smb_init_locale();
519 setlinebuf(stdout);
521 ok = samba_cmdline_init(frame,
522 SAMBA_CMDLINE_CONFIG_SERVER,
523 true /* require_smbconf */);
524 if (!ok) {
525 TALLOC_FREE(frame);
526 exit(1);
529 pc = samba_popt_get_context("vfstest", argc, argv, long_options, 0);
530 if (pc == NULL) {
531 TALLOC_FREE(frame);
532 exit(1);
535 while ((opt = poptGetNextOpt(pc)) != -1) {
536 switch (opt) {
537 case POPT_ERROR_BADOPT:
538 fprintf(stderr, "\nInvalid option %s: %s\n\n",
539 poptBadOption(pc, 0), poptStrerror(opt));
540 poptPrintUsage(pc, stderr, 0);
541 exit(1);
545 poptFreeContext(pc);
547 /* we want total control over the permissions on created files,
548 so set our umask to 0 */
549 umask(0);
551 /* TODO: check output */
552 reload_services(NULL, NULL, false);
554 per_thread_cwd_check();
556 set_smbd_shim(&vfstest_shim_fns);
558 /* Load command lists */
560 cmd_set = vfstest_command_list;
562 while(*cmd_set) {
563 add_command_set(*cmd_set);
564 add_command_set(separator_command);
565 cmd_set++;
568 /* some basic initialization stuff */
569 sec_init();
570 init_guest_session_info(frame);
571 locking_init();
572 vfs = talloc_zero(frame, struct vfs_state);
573 if (vfs == NULL) {
574 return 1;
576 status = make_session_info_guest(vfs, &session_info);
577 if (!NT_STATUS_IS_OK(status)) {
578 return 1;
581 /* Provided by libreplace if not present. Always mallocs. */
582 cwd = get_current_dir_name();
583 if (cwd == NULL) {
584 return -1;
587 status = create_conn_struct_tos_cwd(global_messaging_context(),
589 cwd,
590 session_info,
591 &c);
592 SAFE_FREE(cwd);
593 if (!NT_STATUS_IS_OK(status)) {
594 return 1;
596 vfs->conn = c->conn;
598 vfs->conn->share_access = FILE_GENERIC_ALL;
599 vfs->conn->read_only = false;
601 file_init(vfs->conn->sconn);
602 for (i=0; i < 1024; i++)
603 vfs->files[i] = NULL;
605 if (!posix_locking_init(false)) {
606 return 1;
609 /* Do we have a file input? */
610 if (filename && filename[0]) {
611 process_file(vfs, filename);
612 return 0;
615 /* Do anything specified with -c */
616 if (cmdstr && cmdstr[0]) {
617 char *cmd;
618 char *p = cmdstr;
620 while((cmd=next_command(frame, &p)) != NULL) {
621 status = process_cmd(vfs, cmd);
624 TALLOC_FREE(cmd);
625 return NT_STATUS_IS_OK(status) ? 0 : 1;
628 /* Loop around accepting commands */
630 while(1) {
631 char *line = NULL;
633 line = smb_readline("vfstest $> ", NULL, completion_fn);
635 if (line == NULL) {
636 break;
639 if (line[0] != '\n') {
640 status = process_cmd(vfs, line);
642 SAFE_FREE(line);
645 TALLOC_FREE(vfs);
646 TALLOC_FREE(frame);
647 return NT_STATUS_IS_OK(status) ? 0 : 1;