2 Unix SMB/CIFS implementation.
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 2 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, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #define HAVE_SMB_REGISTER_AUTH
31 #define HAVE_SMB_REGISTER_PASSDB
32 #define HAVE_RPC_PIPE_REGISTER_COMMANDS
33 #define HAVE_SMB_REGISTER_VFS
34 #include "module_dummy.h"
36 /* List to hold groups of commands */
37 static struct cmd_list {
38 struct cmd_list *prev, *next;
39 struct cmd_set *cmd_set;
42 extern pstring user_socket_options;
44 /****************************************************************************
45 handle completion of commands for readline
46 ****************************************************************************/
47 static char **completion_fn(char *text, int start, int end)
49 #define MAX_COMPLETIONS 100
52 struct cmd_list *commands = cmd_list;
57 /* make sure we have a list of valid commands */
61 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
62 if (!matches) return NULL;
64 matches[count++] = strdup(text);
65 if (!matches[0]) return NULL;
67 while (commands && count < MAX_COMPLETIONS-1)
69 if (!commands->cmd_set)
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] = strdup(commands->cmd_set[i].name);
84 commands = commands->next;
89 SAFE_FREE(matches[0]);
90 matches[0] = strdup(matches[1]);
92 matches[count] = NULL;
96 static char* next_command(char** cmdstr)
98 static pstring command;
101 if (!cmdstr || !(*cmdstr))
104 p = strchr_m(*cmdstr, ';');
107 pstrcpy(command, *cmdstr);
113 /* Load specified configuration file */
114 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
115 int argc, char **argv)
118 printf("Usage: %s <smb.conf>\n", argv[0]);
122 if (!lp_load(argv[1], False, True, False)) {
123 printf("Error loading \"%s\"\n", argv[1]);
127 printf("\"%s\" successfully loaded\n", argv[1]);
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;
140 printf("Usage: %s [command]\n", argv[0]);
144 /* Help on one command */
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 &&
155 printf("%s\n", tmp_set->usage);
157 printf("No help for %s\n", tmp_set->name);
166 printf("No such command: %s\n", argv[1]);
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:
189 /* Change the debug level */
190 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
193 printf("Usage: %s [debuglevel]\n", argv[0]);
198 DEBUGLEVEL = atoi(argv[1]);
201 printf("debuglevel is %d\n", DEBUGLEVEL);
206 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
209 talloc_destroy(mem_ctx);
216 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
219 talloc_destroy(mem_ctx);
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", "" },
240 static struct cmd_set separator_command[] = {
241 { "---------------", NULL, "----------------------" },
246 extern struct cmd_set vfs_commands[];
247 static struct cmd_set *vfstest_command_list[] = {
253 static void add_command_set(struct cmd_set *cmd_set)
255 struct cmd_list *entry;
257 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
258 DEBUG(0, ("out of memory\n"));
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 char *p = cmd, **argv = NULL;
271 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
273 TALLOC_CTX *mem_ctx = NULL;
276 /* Count number of arguments first time through the loop then
277 allocate memory and strdup them. */
280 while(next_token(&p, buf, " ", sizeof(buf))) {
282 argv[argc] = strdup(buf);
290 /* Create argument list */
292 argv = (char **)malloc(sizeof(char *) * argc);
293 memset(argv, 0, sizeof(char *) * argc);
296 fprintf(stderr, "out of memory\n");
297 result = NT_STATUS_NO_MEMORY;
307 /* Call the function */
311 if (mem_ctx == NULL) {
313 if (!(mem_ctx = talloc_init("do_cmd"))) {
314 DEBUG(0, ("talloc_init() failed\n"));
320 result = cmd_entry->fn(vfs, mem_ctx, argc, argv);
323 fprintf (stderr, "Invalid command\n");
332 for (i = 0; i < argc; i++)
341 /* Process a command entered at the prompt or as part of -c */
342 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
344 struct cmd_list *temp_list;
348 NTSTATUS result = NT_STATUS_OK;
351 if (cmd[strlen(cmd) - 1] == '\n')
352 cmd[strlen(cmd) - 1] = '\0';
354 if (!next_token(&p, buf, " ", sizeof(buf))) {
358 /* strip the trainly \n if it exsists */
360 if (buf[len-1] == '\n')
363 /* Search for matching commands */
365 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
366 struct cmd_set *temp_set = temp_list->cmd_set;
368 while(temp_set->name) {
369 if (strequal(buf, temp_set->name)) {
371 result = do_cmd(vfs, temp_set, cmd);
380 if (!found && buf[0]) {
381 printf("command not found: %s\n", buf);
385 if (!NT_STATUS_IS_OK(result)) {
386 printf("result was %s\n", nt_errstr(result));
392 static void process_file(struct vfs_state *pvfs, char *filename) {
394 char command[3 * PATH_MAX];
396 if (*filename == '-') {
399 file = fopen(filename, "r");
401 printf("vfstest: error reading file (%s)!", filename);
402 printf("errno n.%d: %s", errno, strerror(errno));
407 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
408 process_cmd(pvfs, command);
412 void exit_server(const char *reason)
414 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
418 static int server_fd = -1;
419 int last_message = -1;
421 int smbd_server_fd(void)
426 /****************************************************************************
427 Reload the services file.
428 **************************************************************************/
430 BOOL reload_services(BOOL test)
436 pstrcpy(fname,lp_configfile());
437 if (file_exist(fname, NULL) &&
438 !strcsequal(fname, dyn_CONFIGFILE)) {
439 pstrcpy(dyn_CONFIGFILE, fname);
446 if (test && !lp_file_list_changed())
449 lp_killunused(conn_snum_used);
451 ret = lp_load(dyn_CONFIGFILE, False, False, True);
455 /* perhaps the config filename is now set */
457 reload_services(True);
464 if (smbd_server_fd() != -1) {
465 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
466 set_socket_options(smbd_server_fd(), user_socket_options);
470 mangle_reset_cache();
473 /* this forces service parameters to be flushed */
474 set_current_service(NULL,True);
481 int main(int argc, char *argv[])
483 static char *cmdstr = NULL;
484 struct cmd_set **cmd_set;
485 static struct vfs_state vfs;
487 static const char *filename = NULL;
489 /* make sure the vars that get altered (4th field) are in
490 a fixed location or certain compilers complain */
492 struct poptOption long_options[] = {
494 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
495 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
503 pc = poptGetContext("vfstest", argc, (const char **) argv,
506 while(poptGetNextOpt(pc) != -1);
511 /* TODO: check output */
512 reload_services(False);
514 /* the following functions are part of the Samba debugging
515 facilities. See lib/debug.c */
516 setup_logging("vfstest", True);
518 /* Load command lists */
520 cmd_set = vfstest_command_list;
523 add_command_set(*cmd_set);
524 add_command_set(separator_command);
528 /* some basic initialization stuff */
530 vfs.conn = conn_new();
531 vfs.conn->user = "vfstest";
532 for (i=0; i < 1024; i++)
535 /* some advanced initiliazation stuff */
536 smbd_vfs_init(vfs.conn);
538 /* Do we have a file input? */
539 if (filename && filename[0]) {
540 process_file(&vfs, filename);
544 /* Do anything specified with -c */
545 if (cmdstr && cmdstr[0]) {
549 while((cmd=next_command(&p)) != NULL) {
550 process_cmd(&vfs, cmd);
556 /* Loop around accepting commands */
562 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
564 line = smb_readline(prompt, NULL, completion_fn);
570 process_cmd(&vfs, line);