s3:smbd: use session_global_id as session number for pam and utmp
[Samba/gebeck_regimport.git] / source3 / torture / vfstest.c
blob72156a5752bab08f85eebf096799dcafb079ce41
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"
38 /* List to hold groups of commands */
39 static struct cmd_list {
40 struct cmd_list *prev, *next;
41 struct cmd_set *cmd_set;
42 } *cmd_list;
44 /****************************************************************************
45 handle completion of commands for readline
46 ****************************************************************************/
47 static char **completion_fn(const char *text, int start, int end)
49 #define MAX_COMPLETIONS 100
50 char **matches;
51 int i, count=0;
52 struct cmd_list *commands = cmd_list;
54 if (start)
55 return NULL;
57 /* make sure we have a list of valid commands */
58 if (!commands)
59 return NULL;
61 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
62 if (!matches) return NULL;
64 matches[count++] = SMB_STRDUP(text);
65 if (!matches[0]) return NULL;
67 while (commands && count < MAX_COMPLETIONS-1)
69 if (!commands->cmd_set)
70 break;
72 for (i=0; commands->cmd_set[i].name; i++)
74 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
75 commands->cmd_set[i].fn)
77 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
78 if (!matches[count])
79 return NULL;
80 count++;
84 commands = commands->next;
87 if (count == 2) {
88 SAFE_FREE(matches[0]);
89 matches[0] = SMB_STRDUP(matches[1]);
91 matches[count] = NULL;
92 return matches;
95 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
97 char *command;
98 char *p;
100 if (!cmdstr || !(*cmdstr))
101 return NULL;
103 p = strchr_m(*cmdstr, ';');
104 if (p)
105 *p = '\0';
106 command = talloc_strdup(ctx, *cmdstr);
107 *cmdstr = p;
109 return command;
112 /* Load specified configuration file */
113 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
114 int argc, const char **argv)
116 if (argc != 2) {
117 printf("Usage: %s <smb.conf>\n", argv[0]);
118 return NT_STATUS_OK;
121 if (!lp_load(argv[1], False, True, False, True)) {
122 printf("Error loading \"%s\"\n", argv[1]);
123 return NT_STATUS_OK;
126 printf("\"%s\" successfully loaded\n", argv[1]);
127 return NT_STATUS_OK;
130 /* Display help on commands */
131 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
132 int argc, const char **argv)
134 struct cmd_list *tmp;
135 struct cmd_set *tmp_set;
137 /* Usage */
138 if (argc > 2) {
139 printf("Usage: %s [command]\n", argv[0]);
140 return NT_STATUS_OK;
143 /* Help on one command */
145 if (argc == 2) {
146 for (tmp = cmd_list; tmp; tmp = tmp->next) {
148 tmp_set = tmp->cmd_set;
150 while(tmp_set->name) {
151 if (strequal(argv[1], tmp_set->name)) {
152 if (tmp_set->usage &&
153 tmp_set->usage[0])
154 printf("%s\n", tmp_set->usage);
155 else
156 printf("No help for %s\n", tmp_set->name);
158 return NT_STATUS_OK;
161 tmp_set++;
165 printf("No such command: %s\n", argv[1]);
166 return NT_STATUS_OK;
169 /* List all commands */
171 for (tmp = cmd_list; tmp; tmp = tmp->next) {
173 tmp_set = tmp->cmd_set;
175 while(tmp_set->name) {
177 printf("%15s\t\t%s\n", tmp_set->name,
178 tmp_set->description ? tmp_set->description:
179 "");
181 tmp_set++;
185 return NT_STATUS_OK;
188 /* Change the debug level */
189 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
191 if (argc > 2) {
192 printf("Usage: %s [debuglevel]\n", argv[0]);
193 return NT_STATUS_OK;
196 if (argc == 2) {
197 lp_set_cmdline("log level", argv[1]);
200 printf("debuglevel is %d\n", DEBUGLEVEL);
202 return NT_STATUS_OK;
205 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
207 /* Cleanup */
208 talloc_destroy(mem_ctx);
209 mem_ctx = NULL;
210 vfs->data = NULL;
211 vfs->data_size = 0;
212 return NT_STATUS_OK;
215 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
217 /* Cleanup */
218 talloc_destroy(mem_ctx);
220 exit(0);
221 return NT_STATUS_OK; /* NOTREACHED */
224 static struct cmd_set vfstest_commands[] = {
226 { "GENERAL OPTIONS" },
228 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
229 { "help", cmd_help, "Get help on commands", "" },
230 { "?", cmd_help, "Get help on commands", "" },
231 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
232 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
233 { "exit", cmd_quit, "Exit program", "" },
234 { "quit", cmd_quit, "Exit program", "" },
236 { NULL }
239 static struct cmd_set separator_command[] = {
240 { "---------------", NULL, "----------------------" },
241 { NULL }
245 extern struct cmd_set vfs_commands[];
246 static struct cmd_set *vfstest_command_list[] = {
247 vfstest_commands,
248 vfs_commands,
249 NULL
252 static void add_command_set(struct cmd_set *cmd_set)
254 struct cmd_list *entry;
256 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
257 DEBUG(0, ("out of memory\n"));
258 return;
261 ZERO_STRUCTP(entry);
263 entry->cmd_set = cmd_set;
264 DLIST_ADD(cmd_list, entry);
267 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
269 const char *p = cmd;
270 char **argv = NULL;
271 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
272 char *buf;
273 TALLOC_CTX *mem_ctx = talloc_stackframe();
274 int argc = 0, i;
276 /* Count number of arguments first time through the loop then
277 allocate memory and strdup them. */
279 again:
280 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
281 if (argv) {
282 argv[argc] = SMB_STRDUP(buf);
284 argc++;
287 if (!argv) {
288 /* Create argument list */
290 argv = SMB_MALLOC_ARRAY(char *, argc);
291 memset(argv, 0, sizeof(char *) * argc);
293 if (!argv) {
294 fprintf(stderr, "out of memory\n");
295 result = NT_STATUS_NO_MEMORY;
296 goto done;
299 p = cmd;
300 argc = 0;
302 goto again;
305 /* Call the function */
307 if (cmd_entry->fn) {
308 /* Run command */
309 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
310 } else {
311 fprintf (stderr, "Invalid command\n");
312 goto done;
315 done:
317 /* Cleanup */
319 if (argv) {
320 for (i = 0; i < argc; i++)
321 SAFE_FREE(argv[i]);
323 SAFE_FREE(argv);
326 TALLOC_FREE(mem_ctx);
327 return result;
330 /* Process a command entered at the prompt or as part of -c */
331 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
333 struct cmd_list *temp_list;
334 bool found = False;
335 char *buf;
336 const char *p = cmd;
337 NTSTATUS result = NT_STATUS_OK;
338 TALLOC_CTX *mem_ctx = talloc_stackframe();
339 int len = 0;
341 if (cmd[strlen(cmd) - 1] == '\n')
342 cmd[strlen(cmd) - 1] = '\0';
344 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
345 TALLOC_FREE(mem_ctx);
346 return NT_STATUS_OK;
349 /* Strip the trailing \n if it exists */
350 len = strlen(buf);
351 if (buf[len-1] == '\n')
352 buf[len-1] = '\0';
354 /* Search for matching commands */
356 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
357 struct cmd_set *temp_set = temp_list->cmd_set;
359 while(temp_set->name) {
360 if (strequal(buf, temp_set->name)) {
361 found = True;
362 result = do_cmd(vfs, temp_set, cmd);
364 goto done;
366 temp_set++;
370 done:
371 if (!found && buf[0]) {
372 printf("command not found: %s\n", buf);
373 TALLOC_FREE(mem_ctx);
374 return NT_STATUS_OK;
377 if (!NT_STATUS_IS_OK(result)) {
378 printf("result was %s\n", nt_errstr(result));
381 TALLOC_FREE(mem_ctx);
382 return result;
385 static void process_file(struct vfs_state *pvfs, char *filename) {
386 FILE *file;
387 char command[3 * PATH_MAX];
389 if (*filename == '-') {
390 file = stdin;
391 } else {
392 file = fopen(filename, "r");
393 if (file == NULL) {
394 printf("vfstest: error reading file (%s)!", filename);
395 printf("errno n.%d: %s", errno, strerror(errno));
396 exit(-1);
400 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
401 process_cmd(pvfs, command);
404 if (file != stdin) {
405 fclose(file);
409 static void vfstest_exit_server(const char * const reason)
411 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
412 exit(0);
415 static void vfstest_exit_server_cleanly(const char * const reason)
417 vfstest_exit_server("normal exit");
420 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
421 struct vfs_state *vfs)
423 struct smb_request *result;
425 result = talloc_zero(mem_ctx, struct smb_request);
426 if (result == NULL) {
427 return NULL;
429 result->sconn = vfs->conn->sconn;
430 result->mid = ++vfs->mid;
432 result->inbuf = talloc_array(result, uint8_t, smb_size);
433 if (result->inbuf == NULL) {
434 goto fail;
436 SSVAL(result->inbuf, smb_mid, result->mid);
437 smb_setlen(result->inbuf, smb_size-4);
438 return result;
439 fail:
440 TALLOC_FREE(result);
441 return NULL;
444 /* Main function */
446 int main(int argc, char *argv[])
448 char *cmdstr = NULL;
449 struct cmd_set **cmd_set;
450 struct vfs_state *vfs;
451 int i;
452 char *filename = NULL;
453 char cwd[MAXPATHLEN];
454 TALLOC_CTX *frame = talloc_stackframe();
455 struct tevent_context *ev = tevent_context_init(NULL);
456 NTSTATUS status = NT_STATUS_OK;
458 /* make sure the vars that get altered (4th field) are in
459 a fixed location or certain compilers complain */
460 poptContext pc;
461 struct poptOption long_options[] = {
462 POPT_AUTOHELP
463 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
464 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
465 POPT_COMMON_SAMBA
466 POPT_TABLEEND
468 static const struct smbd_shim vfstest_shim_fns =
470 .exit_server = vfstest_exit_server,
471 .exit_server_cleanly = vfstest_exit_server_cleanly,
474 load_case_tables();
476 setlinebuf(stdout);
478 pc = poptGetContext("vfstest", argc, (const char **) argv,
479 long_options, 0);
481 while(poptGetNextOpt(pc) != -1);
484 poptFreeContext(pc);
486 lp_load_initial_only(get_dyn_CONFIGFILE());
488 /* TODO: check output */
489 reload_services(NULL, NULL, false);
491 /* the following functions are part of the Samba debugging
492 facilities. See lib/debug.c */
493 setup_logging("vfstest", DEBUG_STDOUT);
495 set_smbd_shim(&vfstest_shim_fns);
497 /* Load command lists */
499 cmd_set = vfstest_command_list;
501 while(*cmd_set) {
502 add_command_set(*cmd_set);
503 add_command_set(separator_command);
504 cmd_set++;
507 /* some basic initialization stuff */
508 sec_init();
509 init_guest_info();
510 locking_init();
511 serverid_parent_init(NULL);
512 vfs = talloc_zero(NULL, struct vfs_state);
513 vfs->conn = talloc_zero(vfs, connection_struct);
514 vfs->conn->share_access = FILE_GENERIC_ALL;
515 vfs->conn->params = talloc_zero(vfs->conn, struct share_params);
516 vfs->conn->sconn = talloc_zero(NULL, struct smbd_server_connection);
517 vfs->conn->sconn->msg_ctx = messaging_init(vfs->conn->sconn, ev);
518 vfs->conn->sconn->ev_ctx = ev;
519 serverid_register(messaging_server_id(vfs->conn->sconn->msg_ctx), 0);
520 make_session_info_guest(NULL, &vfs->conn->session_info);
521 file_init(vfs->conn->sconn);
522 set_conn_connectpath(vfs->conn, getcwd(cwd, sizeof(cwd)));
523 for (i=0; i < 1024; i++)
524 vfs->files[i] = NULL;
526 /* some advanced initialization stuff */
527 smbd_vfs_init(vfs->conn);
529 if (!posix_locking_init(false)) {
530 return 1;
533 /* Do we have a file input? */
534 if (filename && filename[0]) {
535 process_file(vfs, filename);
536 return 0;
539 /* Do anything specified with -c */
540 if (cmdstr && cmdstr[0]) {
541 char *cmd;
542 char *p = cmdstr;
544 while((cmd=next_command(frame, &p)) != NULL) {
545 status = process_cmd(vfs, cmd);
548 TALLOC_FREE(cmd);
549 return NT_STATUS_IS_OK(status) ? 0 : 1;
552 /* Loop around accepting commands */
554 while(1) {
555 char *line = NULL;
557 line = smb_readline("vfstest $> ", NULL, completion_fn);
559 if (line == NULL) {
560 break;
563 if (line[0] != '\n') {
564 status = process_cmd(vfs, line);
566 SAFE_FREE(line);
569 TALLOC_FREE(vfs);
570 TALLOC_FREE(frame);
571 return NT_STATUS_IS_OK(status) ? 0 : 1;