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 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/>.
29 /* List to hold groups of commands */
30 static struct cmd_list {
31 struct cmd_list *prev, *next;
32 struct cmd_set *cmd_set;
35 extern pstring user_socket_options;
37 int get_client_fd(void)
42 /****************************************************************************
43 handle completion of commands for readline
44 ****************************************************************************/
45 static char **completion_fn(const char *text, int start, int end)
47 #define MAX_COMPLETIONS 100
50 struct cmd_list *commands = cmd_list;
55 /* make sure we have a list of valid commands */
59 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
60 if (!matches) return NULL;
62 matches[count++] = SMB_STRDUP(text);
63 if (!matches[0]) return NULL;
65 while (commands && count < MAX_COMPLETIONS-1)
67 if (!commands->cmd_set)
70 for (i=0; commands->cmd_set[i].name; i++)
72 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
73 commands->cmd_set[i].fn)
75 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
82 commands = commands->next;
87 SAFE_FREE(matches[0]);
88 matches[0] = SMB_STRDUP(matches[1]);
90 matches[count] = NULL;
94 static char* next_command(char** cmdstr)
96 static pstring command;
99 if (!cmdstr || !(*cmdstr))
102 p = strchr_m(*cmdstr, ';');
105 pstrcpy(command, *cmdstr);
111 /* Load specified configuration file */
112 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
113 int argc, const char **argv)
116 printf("Usage: %s <smb.conf>\n", argv[0]);
120 if (!lp_load(argv[1], False, True, False, True)) {
121 printf("Error loading \"%s\"\n", argv[1]);
125 printf("\"%s\" successfully loaded\n", argv[1]);
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;
138 printf("Usage: %s [command]\n", argv[0]);
142 /* Help on one command */
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 &&
153 printf("%s\n", tmp_set->usage);
155 printf("No help for %s\n", tmp_set->name);
164 printf("No such command: %s\n", argv[1]);
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:
187 /* Change the debug level */
188 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
191 printf("Usage: %s [debuglevel]\n", argv[0]);
196 DEBUGLEVEL = atoi(argv[1]);
199 printf("debuglevel is %d\n", DEBUGLEVEL);
204 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
207 talloc_destroy(mem_ctx);
214 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
217 talloc_destroy(mem_ctx);
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", "" },
238 static struct cmd_set separator_command[] = {
239 { "---------------", NULL, "----------------------" },
244 extern struct cmd_set vfs_commands[];
245 static struct cmd_set *vfstest_command_list[] = {
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"));
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)
270 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
272 TALLOC_CTX *mem_ctx = NULL;
275 /* Count number of arguments first time through the loop then
276 allocate memory and strdup them. */
279 while(next_token(&p, buf, " ", sizeof(buf))) {
281 argv[argc] = SMB_STRDUP(buf);
289 /* Create argument list */
291 argv = SMB_MALLOC_ARRAY(char *, argc);
292 memset(argv, 0, sizeof(char *) * argc);
295 fprintf(stderr, "out of memory\n");
296 result = NT_STATUS_NO_MEMORY;
306 /* Call the function */
310 if (mem_ctx == NULL) {
312 if (!(mem_ctx = talloc_init("do_cmd"))) {
313 DEBUG(0, ("talloc_init() failed\n"));
319 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
322 fprintf (stderr, "Invalid command\n");
331 for (i = 0; i < argc; i++)
340 /* Process a command entered at the prompt or as part of -c */
341 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
343 struct cmd_list *temp_list;
347 NTSTATUS result = NT_STATUS_OK;
350 if (cmd[strlen(cmd) - 1] == '\n')
351 cmd[strlen(cmd) - 1] = '\0';
353 if (!next_token(&p, buf, " ", sizeof(buf))) {
357 /* strip the trainly \n if it exsists */
359 if (buf[len-1] == '\n')
362 /* Search for matching commands */
364 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
365 struct cmd_set *temp_set = temp_list->cmd_set;
367 while(temp_set->name) {
368 if (strequal(buf, temp_set->name)) {
370 result = do_cmd(vfs, temp_set, cmd);
379 if (!found && buf[0]) {
380 printf("command not found: %s\n", buf);
384 if (!NT_STATUS_IS_OK(result)) {
385 printf("result was %s\n", nt_errstr(result));
391 static void process_file(struct vfs_state *pvfs, char *filename) {
393 char command[3 * PATH_MAX];
395 if (*filename == '-') {
398 file = fopen(filename, "r");
400 printf("vfstest: error reading file (%s)!", filename);
401 printf("errno n.%d: %s", errno, strerror(errno));
406 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
407 process_cmd(pvfs, command);
411 void exit_server(const char *reason)
413 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
417 void exit_server_cleanly(const char *const reason)
419 exit_server("normal exit");
422 static int server_fd = -1;
423 int last_message = -1;
425 int smbd_server_fd(void)
430 void reload_printers(void)
435 /****************************************************************************
436 Reload the services file.
437 **************************************************************************/
439 bool reload_services(bool test)
445 pstrcpy(fname,lp_configfile());
446 if (file_exist(fname, NULL) &&
447 !strcsequal(fname, dyn_CONFIGFILE)) {
448 pstrcpy(dyn_CONFIGFILE, fname);
455 if (test && !lp_file_list_changed())
458 lp_killunused(conn_snum_used);
460 ret = lp_load(dyn_CONFIGFILE, False, False, True, True);
462 /* perhaps the config filename is now set */
464 reload_services(True);
471 if (smbd_server_fd() != -1) {
472 set_socket_options(smbd_server_fd(),"SO_KEEPALIVE");
473 set_socket_options(smbd_server_fd(), user_socket_options);
477 mangle_reset_cache();
480 /* this forces service parameters to be flushed */
481 set_current_service(NULL,0,True);
486 struct event_context *smbd_event_context(void)
488 static struct event_context *ctx;
490 if (!ctx && !(ctx = event_context_init(NULL))) {
491 smb_panic("Could not init smbd event context\n");
496 struct messaging_context *smbd_messaging_context(void)
498 static struct messaging_context *ctx;
500 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
501 smbd_event_context()))) {
502 smb_panic("Could not init smbd messaging context\n");
509 int main(int argc, char *argv[])
511 static char *cmdstr = NULL;
512 struct cmd_set **cmd_set;
513 static struct vfs_state vfs;
515 static char *filename = NULL;
517 /* make sure the vars that get altered (4th field) are in
518 a fixed location or certain compilers complain */
520 struct poptOption long_options[] = {
522 {"file", 'f', POPT_ARG_STRING, &filename, 0, },
523 {"command", 'c', POPT_ARG_STRING, &cmdstr, 0, "Execute specified list of commands" },
532 pc = poptGetContext("vfstest", argc, (const char **) argv,
535 while(poptGetNextOpt(pc) != -1);
540 /* TODO: check output */
541 reload_services(False);
543 /* the following functions are part of the Samba debugging
544 facilities. See lib/debug.c */
545 setup_logging("vfstest", True);
547 /* Load command lists */
549 cmd_set = vfstest_command_list;
552 add_command_set(*cmd_set);
553 add_command_set(separator_command);
557 /* some basic initialization stuff */
560 vfs.conn = conn_new();
561 string_set(&vfs.conn->user,"vfstest");
562 for (i=0; i < 1024; i++)
565 /* some advanced initiliazation stuff */
566 smbd_vfs_init(vfs.conn);
568 /* Do we have a file input? */
569 if (filename && filename[0]) {
570 process_file(&vfs, filename);
574 /* Do anything specified with -c */
575 if (cmdstr && cmdstr[0]) {
579 while((cmd=next_command(&p)) != NULL) {
580 process_cmd(&vfs, cmd);
586 /* Loop around accepting commands */
592 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
594 line = smb_readline(prompt, NULL, completion_fn);
600 process_cmd(&vfs, line);