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 /* List to hold groups of commands */
31 static struct cmd_list {
32 struct cmd_list *prev, *next;
33 struct cmd_set *cmd_set;
36 extern pstring user_socket_options;
38 /****************************************************************************
39 handle completion of commands for readline
40 ****************************************************************************/
41 static char **completion_fn(const char *text, int start, int end)
43 #define MAX_COMPLETIONS 100
46 struct cmd_list *commands = cmd_list;
51 /* make sure we have a list of valid commands */
55 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
56 if (!matches) return NULL;
58 matches[count++] = SMB_STRDUP(text);
59 if (!matches[0]) return NULL;
61 while (commands && count < MAX_COMPLETIONS-1)
63 if (!commands->cmd_set)
66 for (i=0; commands->cmd_set[i].name; i++)
68 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
69 commands->cmd_set[i].fn)
71 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
78 commands = commands->next;
83 SAFE_FREE(matches[0]);
84 matches[0] = SMB_STRDUP(matches[1]);
86 matches[count] = NULL;
90 static char* next_command(char** cmdstr)
92 static pstring command;
95 if (!cmdstr || !(*cmdstr))
98 p = strchr_m(*cmdstr, ';');
101 pstrcpy(command, *cmdstr);
107 /* Load specified configuration file */
108 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
109 int argc, const char **argv)
112 printf("Usage: %s <smb.conf>\n", argv[0]);
116 if (!lp_load(argv[1], False, True, False)) {
117 printf("Error loading \"%s\"\n", argv[1]);
121 printf("\"%s\" successfully loaded\n", argv[1]);
125 /* Display help on commands */
126 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
127 int argc, const char **argv)
129 struct cmd_list *tmp;
130 struct cmd_set *tmp_set;
134 printf("Usage: %s [command]\n", argv[0]);
138 /* Help on one command */
141 for (tmp = cmd_list; tmp; tmp = tmp->next) {
143 tmp_set = tmp->cmd_set;
145 while(tmp_set->name) {
146 if (strequal(argv[1], tmp_set->name)) {
147 if (tmp_set->usage &&
149 printf("%s\n", tmp_set->usage);
151 printf("No help for %s\n", tmp_set->name);
160 printf("No such command: %s\n", argv[1]);
164 /* List all commands */
166 for (tmp = cmd_list; tmp; tmp = tmp->next) {
168 tmp_set = tmp->cmd_set;
170 while(tmp_set->name) {
172 printf("%15s\t\t%s\n", tmp_set->name,
173 tmp_set->description ? tmp_set->description:
183 /* Change the debug level */
184 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
187 printf("Usage: %s [debuglevel]\n", argv[0]);
192 DEBUGLEVEL = atoi(argv[1]);
195 printf("debuglevel is %d\n", DEBUGLEVEL);
200 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
203 talloc_destroy(mem_ctx);
210 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
213 talloc_destroy(mem_ctx);
216 return NT_STATUS_OK; /* NOTREACHED */
219 static struct cmd_set vfstest_commands[] = {
221 { "GENERAL OPTIONS" },
223 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
224 { "help", cmd_help, "Get help on commands", "" },
225 { "?", cmd_help, "Get help on commands", "" },
226 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
227 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
228 { "exit", cmd_quit, "Exit program", "" },
229 { "quit", cmd_quit, "Exit program", "" },
234 static struct cmd_set separator_command[] = {
235 { "---------------", NULL, "----------------------" },
240 extern struct cmd_set vfs_commands[];
241 static struct cmd_set *vfstest_command_list[] = {
247 static void add_command_set(struct cmd_set *cmd_set)
249 struct cmd_list *entry;
251 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
252 DEBUG(0, ("out of memory\n"));
258 entry->cmd_set = cmd_set;
259 DLIST_ADD(cmd_list, entry);
262 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
266 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
268 TALLOC_CTX *mem_ctx = NULL;
271 /* Count number of arguments first time through the loop then
272 allocate memory and strdup them. */
275 while(next_token(&p, buf, " ", sizeof(buf))) {
277 argv[argc] = SMB_STRDUP(buf);
285 /* Create argument list */
287 argv = SMB_MALLOC_ARRAY(char *, argc);
288 memset(argv, 0, sizeof(char *) * argc);
291 fprintf(stderr, "out of memory\n");
292 result = NT_STATUS_NO_MEMORY;
302 /* Call the function */
306 if (mem_ctx == NULL) {
308 if (!(mem_ctx = talloc_init("do_cmd"))) {
309 DEBUG(0, ("talloc_init() failed\n"));
315 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
318 fprintf (stderr, "Invalid command\n");
327 for (i = 0; i < argc; i++)
336 /* Process a command entered at the prompt or as part of -c */
337 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
339 struct cmd_list *temp_list;
343 NTSTATUS result = NT_STATUS_OK;
346 if (cmd[strlen(cmd) - 1] == '\n')
347 cmd[strlen(cmd) - 1] = '\0';
349 if (!next_token(&p, buf, " ", sizeof(buf))) {
353 /* strip the trainly \n if it exsists */
355 if (buf[len-1] == '\n')
358 /* Search for matching commands */
360 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
361 struct cmd_set *temp_set = temp_list->cmd_set;
363 while(temp_set->name) {
364 if (strequal(buf, temp_set->name)) {
366 result = do_cmd(vfs, temp_set, cmd);
375 if (!found && buf[0]) {
376 printf("command not found: %s\n", buf);
380 if (!NT_STATUS_IS_OK(result)) {
381 printf("result was %s\n", nt_errstr(result));
387 static void process_file(struct vfs_state *pvfs, char *filename) {
389 char command[3 * PATH_MAX];
391 if (*filename == '-') {
394 file = fopen(filename, "r");
396 printf("vfstest: error reading file (%s)!", filename);
397 printf("errno n.%d: %s", errno, strerror(errno));
402 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
403 process_cmd(pvfs, command);
407 void exit_server(const char *reason)
409 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
413 static int server_fd = -1;
414 int last_message = -1;
416 int smbd_server_fd(void)
421 void reload_printers(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);
453 /* perhaps the config filename is now set */
455 reload_services(True);
462 if (smbd_server_fd() != -1) {
463 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
464 set_socket_options(smbd_server_fd(), user_socket_options);
468 mangle_reset_cache();
471 /* this forces service parameters to be flushed */
472 set_current_service(NULL,0,True);
479 int main(int argc, char *argv[])
481 static char *cmdstr = NULL;
482 struct cmd_set **cmd_set;
483 static struct vfs_state vfs;
485 static char *filename = NULL;
487 /* make sure the vars that get altered (4th field) are in
488 a fixed location or certain compilers complain */
490 struct poptOption long_options[] = {
492 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
493 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
501 pc = poptGetContext("vfstest", argc, (const char **) argv,
504 while(poptGetNextOpt(pc) != -1);
509 /* TODO: check output */
510 reload_services(False);
512 /* the following functions are part of the Samba debugging
513 facilities. See lib/debug.c */
514 setup_logging("vfstest", True);
516 /* Load command lists */
518 cmd_set = vfstest_command_list;
521 add_command_set(*cmd_set);
522 add_command_set(separator_command);
526 /* some basic initialization stuff */
529 vfs.conn = conn_new();
530 string_set(&vfs.conn->user,"vfstest");
531 for (i=0; i < 1024; i++)
534 /* some advanced initiliazation stuff */
535 smbd_vfs_init(vfs.conn);
537 /* Do we have a file input? */
538 if (filename && filename[0]) {
539 process_file(&vfs, filename);
543 /* Do anything specified with -c */
544 if (cmdstr && cmdstr[0]) {
548 while((cmd=next_command(&p)) != NULL) {
549 process_cmd(&vfs, cmd);
555 /* Loop around accepting commands */
561 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
563 line = smb_readline(prompt, NULL, completion_fn);
569 process_cmd(&vfs, line);