2 Unix SMB/CIFS implementation.
5 Copyright (C) 2002 Jelmer Vernooij
7 Parts of the code stolen from vfstest by Simo Sorce and Eric Lorimer
8 Parts of the code stolen from rpcclient by Tim Potter
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 int (*fn)(struct connection_struct *conn, const char *path);
33 /* List to hold groups of commands */
34 static struct cmd_list {
35 struct cmd_list *prev, *next;
36 struct cmd_set *cmd_set;
39 static char* next_command (char** cmdstr)
41 static pstring command;
44 if (!cmdstr || !(*cmdstr))
47 p = strchr_m(*cmdstr, ';');
50 pstrcpy(command, *cmdstr);
56 /* Load specified configuration file */
57 static NTSTATUS cmd_conf(struct samtest_state *sam, TALLOC_CTX *mem_ctx,
58 int argc, char **argv)
61 printf("Usage: %s <smb.conf>\n", argv[0]);
65 if (!lp_load(argv[1], False, True, False)) {
66 printf("Error loading \"%s\"\n", argv[1]);
70 printf("\"%s\" successfully loaded\n", argv[1]);
74 /* Display help on commands */
75 static NTSTATUS cmd_help(struct samtest_state *st, TALLOC_CTX *mem_ctx,
76 int argc, char **argv)
79 struct cmd_set *tmp_set;
83 printf("Usage: %s [command]\n", argv[0]);
87 /* Help on one command */
90 for (tmp = cmd_list; tmp; tmp = tmp->next) {
92 tmp_set = tmp->cmd_set;
94 while(tmp_set->name) {
95 if (strequal(argv[1], tmp_set->name)) {
98 printf("%s\n", tmp_set->usage);
100 printf("No help for %s\n", tmp_set->name);
109 printf("No such command: %s\n", argv[1]);
113 /* List all commands */
115 for (tmp = cmd_list; tmp; tmp = tmp->next) {
117 tmp_set = tmp->cmd_set;
119 while(tmp_set->name) {
121 printf("%20s\t%s\n", tmp_set->name,
122 tmp_set->description ? tmp_set->description:
132 /* Change the debug level */
133 static NTSTATUS cmd_debuglevel(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
136 printf("Usage: %s [debuglevel]\n", argv[0]);
141 DEBUGLEVEL = atoi(argv[1]);
144 printf("debuglevel is %d\n", DEBUGLEVEL);
149 static NTSTATUS cmd_quit(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
152 talloc_destroy(mem_ctx);
155 return NT_STATUS_OK; /* NOTREACHED */
158 static struct cmd_set samtest_commands[] = {
160 { "GENERAL OPTIONS" },
162 { "help", cmd_help, "Get help on commands", "" },
163 { "?", cmd_help, "Get help on commands", "" },
164 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
165 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
166 { "exit", cmd_quit, "Exit program", "" },
167 { "quit", cmd_quit, "Exit program", "" },
172 static struct cmd_set separator_command[] = {
173 { "---------------", NULL, "----------------------" },
178 /*extern struct cmd_set sam_commands[];*/
179 extern struct cmd_set sam_general_commands[];
180 extern struct cmd_set sam_domain_commands[];
181 extern struct cmd_set sam_account_commands[];
182 extern struct cmd_set sam_group_commands[];
183 static struct cmd_set *samtest_command_list[] = {
185 sam_general_commands,
187 sam_account_commands,
192 static void add_command_set(struct cmd_set *cmd_set)
194 struct cmd_list *entry;
196 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
197 DEBUG(0, ("out of memory\n"));
203 entry->cmd_set = cmd_set;
204 DLIST_ADD(cmd_list, entry);
207 static NTSTATUS do_cmd(struct samtest_state *st, struct cmd_set *cmd_entry, char *cmd)
209 char *p = cmd, **argv = NULL;
210 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
211 TALLOC_CTX *mem_ctx = NULL;
215 /* Count number of arguments first time through the loop then
216 allocate memory and strdup them. */
219 while(next_token(&p, buf, " ", sizeof(buf))) {
221 argv[argc] = strdup(buf);
229 /* Create argument list */
231 argv = (char **)malloc(sizeof(char *) * argc);
232 memset(argv, 0, sizeof(char *) * argc);
235 fprintf(stderr, "out of memory\n");
236 result = NT_STATUS_NO_MEMORY;
246 /* Call the function */
250 if (mem_ctx == NULL) {
252 if (!(mem_ctx = talloc_init())) {
253 DEBUG(0, ("talloc_init() failed\n"));
259 result = cmd_entry->fn(st, mem_ctx, argc, argv);
262 fprintf (stderr, "Invalid command\n");
271 for (i = 0; i < argc; i++)
280 /* Process a command entered at the prompt or as part of -c */
281 static NTSTATUS process_cmd(struct samtest_state *st, char *cmd)
283 struct cmd_list *temp_list;
287 NTSTATUS result = NT_STATUS_OK;
290 if (cmd[strlen(cmd) - 1] == '\n')
291 cmd[strlen(cmd) - 1] = '\0';
293 if (!next_token(&p, buf, " ", sizeof(buf))) {
297 /* strip the trainly \n if it exsists */
299 if (buf[len-1] == '\n')
302 /* Search for matching commands */
304 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
305 struct cmd_set *temp_set = temp_list->cmd_set;
307 while(temp_set->name) {
308 if (strequal(buf, temp_set->name)) {
310 result = do_cmd(st, temp_set, cmd);
319 if (!found && buf[0]) {
320 printf("command not found: %s\n", buf);
324 if (!NT_STATUS_IS_OK(result)) {
325 printf("result was %s\n", nt_errstr(result));
331 void exit_server(char *reason)
333 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
337 static int server_fd = -1;
338 int last_message = -1;
340 int smbd_server_fd(void)
345 BOOL reload_services(BOOL test)
352 int main(int argc, char *argv[])
354 BOOL interactive = True;
356 static char *cmdstr = "";
357 static char *opt_logfile=NULL;
358 static char *config_file = dyn_CONFIGFILE;
360 struct cmd_set **cmd_set;
361 struct samtest_state st;
364 /* make sure the vars that get altered (4th field) are in
365 a fixed location or certain compilers complain */
367 struct poptOption long_options[] = {
369 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
370 {"command", 'e', POPT_ARG_STRING, &cmdstr, 'e', "Execute semicolon seperated cmds"},
371 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Logfile to use instead of stdout"},
372 {"configfile", 'c', POPT_ARG_STRING, &config_file, 0,"use different configuration file",NULL},
382 pc = poptGetContext("samtest", argc, (const char **) argv,
385 while((opt = poptGetNextOpt(pc)) != -1) {
388 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
390 lp_set_logfile(logfile);
396 if (!lp_load(config_file,True,False,False)) {
397 fprintf(stderr, "Can't load %s - run testparm to debug it\n", config_file);
403 /* the following functions are part of the Samba debugging
404 facilities. See lib/debug.c */
405 setup_logging("samtest", interactive);
409 /* Load command lists */
411 cmd_set = samtest_command_list;
414 add_command_set(*cmd_set);
415 add_command_set(separator_command);
419 /* Do anything specified with -c */
424 while((cmd=next_command(&p)) != NULL) {
425 process_cmd(&st, cmd);
431 /* Loop around accepting commands */
437 slprintf(prompt, sizeof(prompt) - 1, "samtest $> ");
439 line = smb_readline(prompt, NULL, NULL);
445 process_cmd(&st, line);