2 Unix SMB/CIFS implementation.
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
7 Copyright (C) Jelmer Vernooij 2002
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 /****************************************************************************
37 handle completion of commands for readline
38 ****************************************************************************/
39 static char **completion_fn(char *text, int start, int end)
41 #define MAX_COMPLETIONS 100
44 struct cmd_list *commands = cmd_list;
49 /* make sure we have a list of valid commands */
53 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
54 if (!matches) return NULL;
56 matches[count++] = strdup(text);
57 if (!matches[0]) return NULL;
59 while (commands && count < MAX_COMPLETIONS-1)
61 if (!commands->cmd_set)
64 for (i=0; commands->cmd_set[i].name; i++)
66 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
67 commands->cmd_set[i].fn)
69 matches[count] = strdup(commands->cmd_set[i].name);
76 commands = commands->next;
81 SAFE_FREE(matches[0]);
82 matches[0] = strdup(matches[1]);
84 matches[count] = NULL;
88 static char* next_command(char** cmdstr)
90 static pstring command;
93 if (!cmdstr || !(*cmdstr))
96 p = strchr_m(*cmdstr, ';');
99 pstrcpy(command, *cmdstr);
105 /* Load specified configuration file */
106 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
107 int argc, char **argv)
110 printf("Usage: %s <smb.conf>\n", argv[0]);
114 if (!lp_load(argv[1], False, True, False)) {
115 printf("Error loading \"%s\"\n", argv[1]);
119 printf("\"%s\" successfully loaded\n", argv[1]);
123 /* Display help on commands */
124 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
125 int argc, const char **argv)
127 struct cmd_list *tmp;
128 struct cmd_set *tmp_set;
132 printf("Usage: %s [command]\n", argv[0]);
136 /* Help on one command */
139 for (tmp = cmd_list; tmp; tmp = tmp->next) {
141 tmp_set = tmp->cmd_set;
143 while(tmp_set->name) {
144 if (strequal(argv[1], tmp_set->name)) {
145 if (tmp_set->usage &&
147 printf("%s\n", tmp_set->usage);
149 printf("No help for %s\n", tmp_set->name);
158 printf("No such command: %s\n", argv[1]);
162 /* List all commands */
164 for (tmp = cmd_list; tmp; tmp = tmp->next) {
166 tmp_set = tmp->cmd_set;
168 while(tmp_set->name) {
170 printf("%15s\t\t%s\n", tmp_set->name,
171 tmp_set->description ? tmp_set->description:
181 /* Change the debug level */
182 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
185 printf("Usage: %s [debuglevel]\n", argv[0]);
190 DEBUGLEVEL = atoi(argv[1]);
193 printf("debuglevel is %d\n", DEBUGLEVEL);
198 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
201 talloc_destroy(mem_ctx);
208 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
211 talloc_destroy(mem_ctx);
214 return NT_STATUS_OK; /* NOTREACHED */
217 static struct cmd_set vfstest_commands[] = {
219 { "GENERAL OPTIONS" },
221 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
222 { "help", cmd_help, "Get help on commands", "" },
223 { "?", cmd_help, "Get help on commands", "" },
224 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
225 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
226 { "exit", cmd_quit, "Exit program", "" },
227 { "quit", cmd_quit, "Exit program", "" },
232 static struct cmd_set separator_command[] = {
233 { "---------------", NULL, "----------------------" },
238 extern struct cmd_set vfs_commands[];
239 static struct cmd_set *vfstest_command_list[] = {
245 static void add_command_set(struct cmd_set *cmd_set)
247 struct cmd_list *entry;
249 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
250 DEBUG(0, ("out of memory\n"));
256 entry->cmd_set = cmd_set;
257 DLIST_ADD(cmd_list, entry);
260 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
262 char *p = cmd, **argv = NULL;
263 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
265 TALLOC_CTX *mem_ctx = NULL;
268 /* Count number of arguments first time through the loop then
269 allocate memory and strdup them. */
272 while(next_token(&p, buf, " ", sizeof(buf))) {
274 argv[argc] = strdup(buf);
282 /* Create argument list */
284 argv = (char **)malloc(sizeof(char *) * argc);
285 memset(argv, 0, sizeof(char *) * argc);
288 fprintf(stderr, "out of memory\n");
289 result = NT_STATUS_NO_MEMORY;
299 /* Call the function */
303 if (mem_ctx == NULL) {
305 if (!(mem_ctx = talloc_init("do_cmd"))) {
306 DEBUG(0, ("talloc_init() failed\n"));
312 result = cmd_entry->fn(vfs, mem_ctx, argc, argv);
315 fprintf (stderr, "Invalid command\n");
324 for (i = 0; i < argc; i++)
333 /* Process a command entered at the prompt or as part of -c */
334 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
336 struct cmd_list *temp_list;
340 NTSTATUS result = NT_STATUS_OK;
343 if (cmd[strlen(cmd) - 1] == '\n')
344 cmd[strlen(cmd) - 1] = '\0';
346 if (!next_token(&p, buf, " ", sizeof(buf))) {
350 /* strip the trainly \n if it exsists */
352 if (buf[len-1] == '\n')
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)) {
363 result = do_cmd(vfs, temp_set, cmd);
372 if (!found && buf[0]) {
373 printf("command not found: %s\n", buf);
377 if (!NT_STATUS_IS_OK(result)) {
378 printf("result was %s\n", nt_errstr(result));
384 static void process_file(struct vfs_state *pvfs, char *filename) {
386 char command[3 * PATH_MAX];
388 if (*filename == '-') {
391 file = fopen(filename, "r");
393 printf("vfstest: error reading file (%s)!", filename);
394 printf("errno n.%d: %s", errno, strerror(errno));
399 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
400 process_cmd(pvfs, command);
404 void exit_server(const char *reason)
406 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
410 static int server_fd = -1;
411 int last_message = -1;
413 int smbd_server_fd(void)
418 /****************************************************************************
419 Reload the services file.
420 **************************************************************************/
422 BOOL reload_services(BOOL test)
428 pstrcpy(fname,lp_configfile());
429 if (file_exist(fname, NULL) &&
430 !strcsequal(fname, dyn_CONFIGFILE)) {
431 pstrcpy(dyn_CONFIGFILE, fname);
438 if (test && !lp_file_list_changed())
441 lp_killunused(conn_snum_used);
443 ret = lp_load(dyn_CONFIGFILE, False, False, True);
447 /* perhaps the config filename is now set */
449 reload_services(True);
455 if (smbd_server_fd() != -1) {
456 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
457 set_socket_options(smbd_server_fd(), lp_socket_options());
460 mangle_reset_cache();
463 /* this forces service parameters to be flushed */
464 set_current_service(NULL,True);
471 int main(int argc, char *argv[])
473 BOOL interactive = True;
475 static char *cmdstr = "";
476 static char *opt_logfile=NULL;
477 static int opt_debuglevel;
479 struct cmd_set **cmd_set;
480 extern BOOL AllowDebugChange;
481 static struct vfs_state vfs;
483 static const char *filename = "";
485 /* make sure the vars that get altered (4th field) are in
486 a fixed location or certain compilers complain */
488 struct poptOption long_options[] = {
490 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
491 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
492 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Write output to specified logfile" },
493 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
494 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
502 AllowDebugChange = False;
504 pc = poptGetContext("vfstest", argc, (const char **) argv,
507 while((opt = poptGetNextOpt(pc)) != -1) {
510 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
512 lp_set_logfile(logfile);
517 DEBUGLEVEL = opt_debuglevel;
525 /* TODO: check output */
526 reload_services(False);
528 /* the following functions are part of the Samba debugging
529 facilities. See lib/debug.c */
530 setup_logging("vfstest", interactive);
534 /* Load command lists */
536 cmd_set = vfstest_command_list;
539 add_command_set(*cmd_set);
540 add_command_set(separator_command);
544 /* some basic initialization stuff */
546 vfs.conn = conn_new();
547 vfs.conn->user = "vfstest";
548 for (i=0; i < 1024; i++)
551 /* some advanced initiliazation stuff */
552 smbd_vfs_init(vfs.conn);
554 /* Do we have a file input? */
556 process_file(&vfs, filename);
560 /* Do anything specified with -c */
565 while((cmd=next_command(&p)) != NULL) {
566 process_cmd(&vfs, cmd);
572 /* Loop around accepting commands */
578 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
580 line = smb_readline(prompt, NULL, completion_fn);
586 process_cmd(&vfs, line);