Fix krb5_samba.c build
[Samba/bjacke.git] / source3 / torture / vfstest.c
blob40e78e87f25fee23dd097596f778c49b22ae4430
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"
37 /* List to hold groups of commands */
38 static struct cmd_list {
39 struct cmd_list *prev, *next;
40 struct cmd_set *cmd_set;
41 } *cmd_list;
43 /****************************************************************************
44 handle completion of commands for readline
45 ****************************************************************************/
46 static char **completion_fn(const char *text, int start, int end)
48 #define MAX_COMPLETIONS 100
49 char **matches;
50 int i, count=0;
51 struct cmd_list *commands = cmd_list;
53 if (start)
54 return NULL;
56 /* make sure we have a list of valid commands */
57 if (!commands)
58 return NULL;
60 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
61 if (!matches) return NULL;
63 matches[count++] = SMB_STRDUP(text);
64 if (!matches[0]) return NULL;
66 while (commands && count < MAX_COMPLETIONS-1)
68 if (!commands->cmd_set)
69 break;
71 for (i=0; commands->cmd_set[i].name; i++)
73 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
74 commands->cmd_set[i].fn)
76 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
77 if (!matches[count])
78 return NULL;
79 count++;
83 commands = commands->next;
86 if (count == 2) {
87 SAFE_FREE(matches[0]);
88 matches[0] = SMB_STRDUP(matches[1]);
90 matches[count] = NULL;
91 return matches;
94 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
96 char *command;
97 char *p;
99 if (!cmdstr || !(*cmdstr))
100 return NULL;
102 p = strchr_m(*cmdstr, ';');
103 if (p)
104 *p = '\0';
105 command = talloc_strdup(ctx, *cmdstr);
106 *cmdstr = p;
108 return command;
111 /* Load specified configuration file */
112 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
113 int argc, const char **argv)
115 if (argc != 2) {
116 printf("Usage: %s <smb.conf>\n", argv[0]);
117 return NT_STATUS_OK;
120 if (!lp_load(argv[1], False, True, False, True)) {
121 printf("Error loading \"%s\"\n", argv[1]);
122 return NT_STATUS_OK;
125 printf("\"%s\" successfully loaded\n", argv[1]);
126 return NT_STATUS_OK;
129 /* Display help on commands */
130 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
131 int argc, const char **argv)
133 struct cmd_list *tmp;
134 struct cmd_set *tmp_set;
136 /* Usage */
137 if (argc > 2) {
138 printf("Usage: %s [command]\n", argv[0]);
139 return NT_STATUS_OK;
142 /* Help on one command */
144 if (argc == 2) {
145 for (tmp = cmd_list; tmp; tmp = tmp->next) {
147 tmp_set = tmp->cmd_set;
149 while(tmp_set->name) {
150 if (strequal(argv[1], tmp_set->name)) {
151 if (tmp_set->usage &&
152 tmp_set->usage[0])
153 printf("%s\n", tmp_set->usage);
154 else
155 printf("No help for %s\n", tmp_set->name);
157 return NT_STATUS_OK;
160 tmp_set++;
164 printf("No such command: %s\n", argv[1]);
165 return NT_STATUS_OK;
168 /* List all commands */
170 for (tmp = cmd_list; tmp; tmp = tmp->next) {
172 tmp_set = tmp->cmd_set;
174 while(tmp_set->name) {
176 printf("%15s\t\t%s\n", tmp_set->name,
177 tmp_set->description ? tmp_set->description:
178 "");
180 tmp_set++;
184 return NT_STATUS_OK;
187 /* Change the debug level */
188 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
190 if (argc > 2) {
191 printf("Usage: %s [debuglevel]\n", argv[0]);
192 return NT_STATUS_OK;
195 if (argc == 2) {
196 lp_set_cmdline("log level", argv[1]);
199 printf("debuglevel is %d\n", DEBUGLEVEL);
201 return NT_STATUS_OK;
204 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
206 /* Cleanup */
207 talloc_destroy(mem_ctx);
208 mem_ctx = NULL;
209 vfs->data = NULL;
210 vfs->data_size = 0;
211 return NT_STATUS_OK;
214 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
216 /* Cleanup */
217 talloc_destroy(mem_ctx);
219 exit(0);
220 return NT_STATUS_OK; /* NOTREACHED */
223 static struct cmd_set vfstest_commands[] = {
225 { "GENERAL OPTIONS" },
227 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
228 { "help", cmd_help, "Get help on commands", "" },
229 { "?", cmd_help, "Get help on commands", "" },
230 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
231 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
232 { "exit", cmd_quit, "Exit program", "" },
233 { "quit", cmd_quit, "Exit program", "" },
235 { NULL }
238 static struct cmd_set separator_command[] = {
239 { "---------------", NULL, "----------------------" },
240 { NULL }
244 extern struct cmd_set vfs_commands[];
245 static struct cmd_set *vfstest_command_list[] = {
246 vfstest_commands,
247 vfs_commands,
248 NULL
251 static void add_command_set(struct cmd_set *cmd_set)
253 struct cmd_list *entry;
255 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
256 DEBUG(0, ("out of memory\n"));
257 return;
260 ZERO_STRUCTP(entry);
262 entry->cmd_set = cmd_set;
263 DLIST_ADD(cmd_list, entry);
266 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
268 const char *p = cmd;
269 char **argv = NULL;
270 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
271 char *buf;
272 TALLOC_CTX *mem_ctx = talloc_stackframe();
273 int argc = 0, i;
275 /* Count number of arguments first time through the loop then
276 allocate memory and strdup them. */
278 again:
279 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
280 if (argv) {
281 argv[argc] = SMB_STRDUP(buf);
283 argc++;
286 if (!argv) {
287 /* Create argument list */
289 argv = SMB_MALLOC_ARRAY(char *, argc);
290 memset(argv, 0, sizeof(char *) * argc);
292 if (!argv) {
293 fprintf(stderr, "out of memory\n");
294 result = NT_STATUS_NO_MEMORY;
295 goto done;
298 p = cmd;
299 argc = 0;
301 goto again;
304 /* Call the function */
306 if (cmd_entry->fn) {
307 /* Run command */
308 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
309 } else {
310 fprintf (stderr, "Invalid command\n");
311 goto done;
314 done:
316 /* Cleanup */
318 if (argv) {
319 for (i = 0; i < argc; i++)
320 SAFE_FREE(argv[i]);
322 SAFE_FREE(argv);
325 TALLOC_FREE(mem_ctx);
326 return result;
329 /* Process a command entered at the prompt or as part of -c */
330 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
332 struct cmd_list *temp_list;
333 bool found = False;
334 char *buf;
335 const char *p = cmd;
336 NTSTATUS result = NT_STATUS_OK;
337 TALLOC_CTX *mem_ctx = talloc_stackframe();
338 int len = 0;
340 if (cmd[strlen(cmd) - 1] == '\n')
341 cmd[strlen(cmd) - 1] = '\0';
343 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
344 TALLOC_FREE(mem_ctx);
345 return NT_STATUS_OK;
348 /* Strip the trailing \n if it exists */
349 len = strlen(buf);
350 if (buf[len-1] == '\n')
351 buf[len-1] = '\0';
353 /* Search for matching commands */
355 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
356 struct cmd_set *temp_set = temp_list->cmd_set;
358 while(temp_set->name) {
359 if (strequal(buf, temp_set->name)) {
360 found = True;
361 result = do_cmd(vfs, temp_set, cmd);
363 goto done;
365 temp_set++;
369 done:
370 if (!found && buf[0]) {
371 printf("command not found: %s\n", buf);
372 TALLOC_FREE(mem_ctx);
373 return NT_STATUS_OK;
376 if (!NT_STATUS_IS_OK(result)) {
377 printf("result was %s\n", nt_errstr(result));
380 TALLOC_FREE(mem_ctx);
381 return result;
384 static void process_file(struct vfs_state *pvfs, char *filename) {
385 FILE *file;
386 char command[3 * PATH_MAX];
388 if (*filename == '-') {
389 file = stdin;
390 } else {
391 file = fopen(filename, "r");
392 if (file == NULL) {
393 printf("vfstest: error reading file (%s)!", filename);
394 printf("errno n.%d: %s", errno, strerror(errno));
395 exit(-1);
399 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
400 process_cmd(pvfs, command);
403 if (file != stdin) {
404 fclose(file);
408 void exit_server(const char *reason)
410 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
411 exit(0);
414 void exit_server_cleanly(const char *const reason)
416 exit_server("normal exit");
419 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
420 struct vfs_state *vfs)
422 struct smb_request *result;
424 result = talloc_zero(mem_ctx, struct smb_request);
425 if (result == NULL) {
426 return NULL;
428 result->sconn = vfs->conn->sconn;
429 result->mid = ++vfs->mid;
431 result->inbuf = talloc_array(result, uint8_t, smb_size);
432 if (result->inbuf == NULL) {
433 goto fail;
435 SSVAL(result->inbuf, smb_mid, result->mid);
436 smb_setlen(result->inbuf, smb_size-4);
437 return result;
438 fail:
439 TALLOC_FREE(result);
440 return NULL;
443 /* Main function */
445 int main(int argc, char *argv[])
447 char *cmdstr = NULL;
448 struct cmd_set **cmd_set;
449 struct vfs_state vfs = { 0, };
450 int i;
451 char *filename = NULL;
452 char cwd[MAXPATHLEN];
453 TALLOC_CTX *frame = talloc_stackframe();
454 struct tevent_context *ev = tevent_context_init(NULL);
455 NTSTATUS status = NT_STATUS_OK;
457 /* make sure the vars that get altered (4th field) are in
458 a fixed location or certain compilers complain */
459 poptContext pc;
460 struct poptOption long_options[] = {
461 POPT_AUTOHELP
462 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
463 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
464 POPT_COMMON_SAMBA
465 POPT_TABLEEND
468 load_case_tables();
470 setlinebuf(stdout);
472 pc = poptGetContext("vfstest", argc, (const char **) argv,
473 long_options, 0);
475 while(poptGetNextOpt(pc) != -1);
478 poptFreeContext(pc);
480 lp_load_initial_only(get_dyn_CONFIGFILE());
482 /* TODO: check output */
483 reload_services(NULL, NULL, false);
485 /* the following functions are part of the Samba debugging
486 facilities. See lib/debug.c */
487 setup_logging("vfstest", DEBUG_STDOUT);
489 /* Load command lists */
491 cmd_set = vfstest_command_list;
493 while(*cmd_set) {
494 add_command_set(*cmd_set);
495 add_command_set(separator_command);
496 cmd_set++;
499 /* some basic initialization stuff */
500 sec_init();
501 init_guest_info();
502 locking_init();
503 serverid_parent_init(NULL);
504 vfs.conn = talloc_zero(NULL, connection_struct);
505 vfs.conn->share_access = FILE_GENERIC_ALL;
506 vfs.conn->params = talloc_zero(vfs.conn, struct share_params);
507 vfs.conn->sconn = talloc_zero(NULL, struct smbd_server_connection);
508 vfs.conn->sconn->msg_ctx = messaging_init(vfs.conn->sconn, ev);
509 vfs.conn->sconn->ev_ctx = ev;
510 serverid_register(messaging_server_id(vfs.conn->sconn->msg_ctx), 0);
511 make_session_info_guest(NULL, &vfs.conn->session_info);
512 file_init(vfs.conn->sconn);
513 set_conn_connectpath(vfs.conn, getcwd(cwd, sizeof(cwd)));
514 for (i=0; i < 1024; i++)
515 vfs.files[i] = NULL;
517 /* some advanced initialization stuff */
518 smbd_vfs_init(vfs.conn);
520 if (!posix_locking_init(false)) {
521 return 1;
524 /* Do we have a file input? */
525 if (filename && filename[0]) {
526 process_file(&vfs, filename);
527 return 0;
530 /* Do anything specified with -c */
531 if (cmdstr && cmdstr[0]) {
532 char *cmd;
533 char *p = cmdstr;
535 while((cmd=next_command(frame, &p)) != NULL) {
536 status = process_cmd(&vfs, cmd);
539 TALLOC_FREE(cmd);
540 return NT_STATUS_IS_OK(status) ? 0 : 1;
543 /* Loop around accepting commands */
545 while(1) {
546 char *line = NULL;
548 line = smb_readline("vfstest $> ", NULL, completion_fn);
550 if (line == NULL) {
551 break;
554 if (line[0] != '\n') {
555 status = process_cmd(&vfs, line);
557 SAFE_FREE(line);
560 TALLOC_FREE(vfs.conn);
561 TALLOC_FREE(frame);
562 return NT_STATUS_IS_OK(status) ? 0 : 1;