s3: Use file_id_string in file_id_string_tos
[Samba/gbeck.git] / source3 / torture / vfstest.c
blobdd3787b658bd0fce25d2309a174c251d64930f74
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 /****************************************************************************
46 handle completion of commands for readline
47 ****************************************************************************/
48 static char **completion_fn(const char *text, int start, int end)
50 #define MAX_COMPLETIONS 100
51 char **matches;
52 int i, count=0;
53 struct cmd_list *commands = cmd_list;
55 if (start)
56 return NULL;
58 /* make sure we have a list of valid commands */
59 if (!commands)
60 return NULL;
62 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
63 if (!matches) return NULL;
65 matches[count++] = SMB_STRDUP(text);
66 if (!matches[0]) return NULL;
68 while (commands && count < MAX_COMPLETIONS-1)
70 if (!commands->cmd_set)
71 break;
73 for (i=0; commands->cmd_set[i].name; i++)
75 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
76 commands->cmd_set[i].fn)
78 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
79 if (!matches[count])
80 return NULL;
81 count++;
85 commands = commands->next;
88 if (count == 2) {
89 SAFE_FREE(matches[0]);
90 matches[0] = SMB_STRDUP(matches[1]);
92 matches[count] = NULL;
93 return matches;
96 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
98 char *command;
99 char *p;
101 if (!cmdstr || !(*cmdstr))
102 return NULL;
104 p = strchr_m(*cmdstr, ';');
105 if (p)
106 *p = '\0';
107 command = talloc_strdup(ctx, *cmdstr);
108 *cmdstr = p;
110 return command;
113 /* Load specified configuration file */
114 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
115 int argc, const char **argv)
117 if (argc != 2) {
118 printf("Usage: %s <smb.conf>\n", argv[0]);
119 return NT_STATUS_OK;
122 if (!lp_load(argv[1], False, True, False, True)) {
123 printf("Error loading \"%s\"\n", argv[1]);
124 return NT_STATUS_OK;
127 printf("\"%s\" successfully loaded\n", argv[1]);
128 return NT_STATUS_OK;
131 /* Display help on commands */
132 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
133 int argc, const char **argv)
135 struct cmd_list *tmp;
136 struct cmd_set *tmp_set;
138 /* Usage */
139 if (argc > 2) {
140 printf("Usage: %s [command]\n", argv[0]);
141 return NT_STATUS_OK;
144 /* Help on one command */
146 if (argc == 2) {
147 for (tmp = cmd_list; tmp; tmp = tmp->next) {
149 tmp_set = tmp->cmd_set;
151 while(tmp_set->name) {
152 if (strequal(argv[1], tmp_set->name)) {
153 if (tmp_set->usage &&
154 tmp_set->usage[0])
155 printf("%s\n", tmp_set->usage);
156 else
157 printf("No help for %s\n", tmp_set->name);
159 return NT_STATUS_OK;
162 tmp_set++;
166 printf("No such command: %s\n", argv[1]);
167 return NT_STATUS_OK;
170 /* List all commands */
172 for (tmp = cmd_list; tmp; tmp = tmp->next) {
174 tmp_set = tmp->cmd_set;
176 while(tmp_set->name) {
178 printf("%15s\t\t%s\n", tmp_set->name,
179 tmp_set->description ? tmp_set->description:
180 "");
182 tmp_set++;
186 return NT_STATUS_OK;
189 /* Change the debug level */
190 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
192 if (argc > 2) {
193 printf("Usage: %s [debuglevel]\n", argv[0]);
194 return NT_STATUS_OK;
197 if (argc == 2) {
198 lp_set_cmdline("log level", argv[1]);
201 printf("debuglevel is %d\n", DEBUGLEVEL);
203 return NT_STATUS_OK;
206 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
208 /* Cleanup */
209 talloc_destroy(mem_ctx);
210 mem_ctx = NULL;
211 vfs->data = NULL;
212 vfs->data_size = 0;
213 return NT_STATUS_OK;
216 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
218 /* Cleanup */
219 talloc_destroy(mem_ctx);
221 exit(0);
222 return NT_STATUS_OK; /* NOTREACHED */
225 static struct cmd_set vfstest_commands[] = {
227 { "GENERAL OPTIONS" },
229 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
230 { "help", cmd_help, "Get help on commands", "" },
231 { "?", cmd_help, "Get help on commands", "" },
232 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
233 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
234 { "exit", cmd_quit, "Exit program", "" },
235 { "quit", cmd_quit, "Exit program", "" },
237 { NULL }
240 static struct cmd_set separator_command[] = {
241 { "---------------", NULL, "----------------------" },
242 { NULL }
246 extern struct cmd_set vfs_commands[];
247 static struct cmd_set *vfstest_command_list[] = {
248 vfstest_commands,
249 vfs_commands,
250 NULL
253 static void add_command_set(struct cmd_set *cmd_set)
255 struct cmd_list *entry;
257 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
258 DEBUG(0, ("out of memory\n"));
259 return;
262 ZERO_STRUCTP(entry);
264 entry->cmd_set = cmd_set;
265 DLIST_ADD(cmd_list, entry);
268 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
270 const char *p = cmd;
271 char **argv = NULL;
272 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
273 char *buf;
274 TALLOC_CTX *mem_ctx = talloc_stackframe();
275 int argc = 0, i;
277 /* Count number of arguments first time through the loop then
278 allocate memory and strdup them. */
280 again:
281 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
282 if (argv) {
283 argv[argc] = SMB_STRDUP(buf);
285 argc++;
288 if (!argv) {
289 /* Create argument list */
291 argv = SMB_MALLOC_ARRAY(char *, argc);
292 memset(argv, 0, sizeof(char *) * argc);
294 if (!argv) {
295 fprintf(stderr, "out of memory\n");
296 result = NT_STATUS_NO_MEMORY;
297 goto done;
300 p = cmd;
301 argc = 0;
303 goto again;
306 /* Call the function */
308 if (cmd_entry->fn) {
309 /* Run command */
310 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
311 } else {
312 fprintf (stderr, "Invalid command\n");
313 goto done;
316 done:
318 /* Cleanup */
320 if (argv) {
321 for (i = 0; i < argc; i++)
322 SAFE_FREE(argv[i]);
324 SAFE_FREE(argv);
327 TALLOC_FREE(mem_ctx);
328 return result;
331 /* Process a command entered at the prompt or as part of -c */
332 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
334 struct cmd_list *temp_list;
335 bool found = False;
336 char *buf;
337 const char *p = cmd;
338 NTSTATUS result = NT_STATUS_OK;
339 TALLOC_CTX *mem_ctx = talloc_stackframe();
340 int len = 0;
342 if (cmd[strlen(cmd) - 1] == '\n')
343 cmd[strlen(cmd) - 1] = '\0';
345 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
346 TALLOC_FREE(mem_ctx);
347 return NT_STATUS_OK;
350 /* Strip the trailing \n if it exists */
351 len = strlen(buf);
352 if (buf[len-1] == '\n')
353 buf[len-1] = '\0';
355 /* Search for matching commands */
357 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
358 struct cmd_set *temp_set = temp_list->cmd_set;
360 while(temp_set->name) {
361 if (strequal(buf, temp_set->name)) {
362 found = True;
363 result = do_cmd(vfs, temp_set, cmd);
365 goto done;
367 temp_set++;
371 done:
372 if (!found && buf[0]) {
373 printf("command not found: %s\n", buf);
374 TALLOC_FREE(mem_ctx);
375 return NT_STATUS_OK;
378 if (!NT_STATUS_IS_OK(result)) {
379 printf("result was %s\n", nt_errstr(result));
382 TALLOC_FREE(mem_ctx);
383 return result;
386 static void process_file(struct vfs_state *pvfs, char *filename) {
387 FILE *file;
388 char command[3 * PATH_MAX];
390 if (*filename == '-') {
391 file = stdin;
392 } else {
393 file = fopen(filename, "r");
394 if (file == NULL) {
395 printf("vfstest: error reading file (%s)!", filename);
396 printf("errno n.%d: %s", errno, strerror(errno));
397 exit(-1);
401 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
402 process_cmd(pvfs, command);
405 if (file != stdin) {
406 fclose(file);
410 static void vfstest_exit_server(const char * const reason)
412 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
413 exit(0);
416 static void vfstest_exit_server_cleanly(const char * const reason)
418 vfstest_exit_server("normal exit");
421 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
422 struct vfs_state *vfs)
424 struct smb_request *result;
426 result = talloc_zero(mem_ctx, struct smb_request);
427 if (result == NULL) {
428 return NULL;
430 result->sconn = vfs->conn->sconn;
431 result->mid = ++vfs->mid;
433 result->inbuf = talloc_array(result, uint8_t, smb_size);
434 if (result->inbuf == NULL) {
435 goto fail;
437 SSVAL(result->inbuf, smb_mid, result->mid);
438 smb_setlen(result->inbuf, smb_size-4);
439 return result;
440 fail:
441 TALLOC_FREE(result);
442 return NULL;
445 /* Main function */
447 int main(int argc, char *argv[])
449 char *cmdstr = NULL;
450 struct cmd_set **cmd_set;
451 struct vfs_state *vfs;
452 int i;
453 char *filename = NULL;
454 char cwd[MAXPATHLEN];
455 TALLOC_CTX *frame = talloc_stackframe();
456 struct tevent_context *ev = tevent_context_init(NULL);
457 NTSTATUS status = NT_STATUS_OK;
459 /* make sure the vars that get altered (4th field) are in
460 a fixed location or certain compilers complain */
461 poptContext pc;
462 struct poptOption long_options[] = {
463 POPT_AUTOHELP
464 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
465 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
466 POPT_COMMON_SAMBA
467 POPT_TABLEEND
469 static const struct smbd_shim vfstest_shim_fns =
471 .exit_server = vfstest_exit_server,
472 .exit_server_cleanly = vfstest_exit_server_cleanly,
475 load_case_tables();
477 setlinebuf(stdout);
479 pc = poptGetContext("vfstest", argc, (const char **) argv,
480 long_options, 0);
482 while(poptGetNextOpt(pc) != -1);
485 poptFreeContext(pc);
487 /* we want total control over the permissions on created files,
488 so set our umask to 0 */
489 umask(0);
491 lp_load_initial_only(get_dyn_CONFIGFILE());
493 /* TODO: check output */
494 reload_services(NULL, NULL, false);
496 /* the following functions are part of the Samba debugging
497 facilities. See lib/debug.c */
498 setup_logging("vfstest", DEBUG_STDOUT);
500 set_smbd_shim(&vfstest_shim_fns);
502 /* Load command lists */
504 cmd_set = vfstest_command_list;
506 while(*cmd_set) {
507 add_command_set(*cmd_set);
508 add_command_set(separator_command);
509 cmd_set++;
512 /* some basic initialization stuff */
513 sec_init();
514 init_guest_info();
515 locking_init();
516 serverid_parent_init(NULL);
517 vfs = talloc_zero(NULL, struct vfs_state);
518 vfs->conn = talloc_zero(vfs, connection_struct);
519 vfs->conn->share_access = FILE_GENERIC_ALL;
520 vfs->conn->params = talloc_zero(vfs->conn, struct share_params);
521 vfs->conn->sconn = talloc_zero(NULL, struct smbd_server_connection);
522 vfs->conn->sconn->msg_ctx = messaging_init(vfs->conn->sconn, ev);
523 vfs->conn->sconn->ev_ctx = ev;
524 serverid_register(messaging_server_id(vfs->conn->sconn->msg_ctx), 0);
525 make_session_info_guest(NULL, &vfs->conn->session_info);
526 file_init(vfs->conn->sconn);
527 set_conn_connectpath(vfs->conn, getcwd(cwd, sizeof(cwd)));
528 for (i=0; i < 1024; i++)
529 vfs->files[i] = NULL;
531 /* some advanced initialization stuff */
532 smbd_vfs_init(vfs->conn);
534 if (!posix_locking_init(false)) {
535 return 1;
538 /* Do we have a file input? */
539 if (filename && filename[0]) {
540 process_file(vfs, filename);
541 return 0;
544 /* Do anything specified with -c */
545 if (cmdstr && cmdstr[0]) {
546 char *cmd;
547 char *p = cmdstr;
549 while((cmd=next_command(frame, &p)) != NULL) {
550 status = process_cmd(vfs, cmd);
553 TALLOC_FREE(cmd);
554 return NT_STATUS_IS_OK(status) ? 0 : 1;
557 /* Loop around accepting commands */
559 while(1) {
560 char *line = NULL;
562 line = smb_readline("vfstest $> ", NULL, completion_fn);
564 if (line == NULL) {
565 break;
568 if (line[0] != '\n') {
569 status = process_cmd(vfs, line);
571 SAFE_FREE(line);
574 TALLOC_FREE(vfs);
575 TALLOC_FREE(frame);
576 return NT_STATUS_IS_OK(status) ? 0 : 1;