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/>.
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "popt_common.h"
31 #include "../libcli/smbreadline/smbreadline.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 /****************************************************************************
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
;
86 SAFE_FREE(matches
[0]);
87 matches
[0] = SMB_STRDUP(matches
[1]);
89 matches
[count
] = NULL
;
93 static char *next_command(TALLOC_CTX
*ctx
, char **cmdstr
)
98 if (!cmdstr
|| !(*cmdstr
))
101 p
= strchr_m(*cmdstr
, ';');
104 command
= talloc_strdup(ctx
, *cmdstr
);
110 /* Load specified configuration file */
111 static NTSTATUS
cmd_conf(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
112 int argc
, const char **argv
)
115 printf("Usage: %s <smb.conf>\n", argv
[0]);
119 if (!lp_load(argv
[1], False
, True
, False
, True
)) {
120 printf("Error loading \"%s\"\n", argv
[1]);
124 printf("\"%s\" successfully loaded\n", argv
[1]);
128 /* Display help on commands */
129 static NTSTATUS
cmd_help(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
130 int argc
, const char **argv
)
132 struct cmd_list
*tmp
;
133 struct cmd_set
*tmp_set
;
137 printf("Usage: %s [command]\n", argv
[0]);
141 /* Help on one command */
144 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
146 tmp_set
= tmp
->cmd_set
;
148 while(tmp_set
->name
) {
149 if (strequal(argv
[1], tmp_set
->name
)) {
150 if (tmp_set
->usage
&&
152 printf("%s\n", tmp_set
->usage
);
154 printf("No help for %s\n", tmp_set
->name
);
163 printf("No such command: %s\n", argv
[1]);
167 /* List all commands */
169 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
171 tmp_set
= tmp
->cmd_set
;
173 while(tmp_set
->name
) {
175 printf("%15s\t\t%s\n", tmp_set
->name
,
176 tmp_set
->description
? tmp_set
->description
:
186 /* Change the debug level */
187 static NTSTATUS
cmd_debuglevel(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
190 printf("Usage: %s [debuglevel]\n", argv
[0]);
195 lp_set_cmdline("log level", argv
[1]);
198 printf("debuglevel is %d\n", DEBUGLEVEL
);
203 static NTSTATUS
cmd_freemem(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
206 talloc_destroy(mem_ctx
);
213 static NTSTATUS
cmd_quit(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
216 talloc_destroy(mem_ctx
);
219 return NT_STATUS_OK
; /* NOTREACHED */
222 static struct cmd_set vfstest_commands
[] = {
224 { "GENERAL OPTIONS" },
226 { "conf", cmd_conf
, "Load smb configuration file", "conf <smb.conf>" },
227 { "help", cmd_help
, "Get help on commands", "" },
228 { "?", cmd_help
, "Get help on commands", "" },
229 { "debuglevel", cmd_debuglevel
, "Set debug level", "" },
230 { "freemem", cmd_freemem
, "Free currently allocated buffers", "" },
231 { "exit", cmd_quit
, "Exit program", "" },
232 { "quit", cmd_quit
, "Exit program", "" },
237 static struct cmd_set separator_command
[] = {
238 { "---------------", NULL
, "----------------------" },
243 extern struct cmd_set vfs_commands
[];
244 static struct cmd_set
*vfstest_command_list
[] = {
250 static void add_command_set(struct cmd_set
*cmd_set
)
252 struct cmd_list
*entry
;
254 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
255 DEBUG(0, ("out of memory\n"));
261 entry
->cmd_set
= cmd_set
;
262 DLIST_ADD(cmd_list
, entry
);
265 static NTSTATUS
do_cmd(struct vfs_state
*vfs
, struct cmd_set
*cmd_entry
, char *cmd
)
269 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
271 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
274 /* Count number of arguments first time through the loop then
275 allocate memory and strdup them. */
278 while(next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
280 argv
[argc
] = SMB_STRDUP(buf
);
286 /* Create argument list */
288 argv
= SMB_MALLOC_ARRAY(char *, argc
);
289 memset(argv
, 0, sizeof(char *) * argc
);
292 fprintf(stderr
, "out of memory\n");
293 result
= NT_STATUS_NO_MEMORY
;
303 /* Call the function */
307 result
= cmd_entry
->fn(vfs
, mem_ctx
, argc
, (const char **)argv
);
309 fprintf (stderr
, "Invalid command\n");
318 for (i
= 0; i
< argc
; i
++)
324 TALLOC_FREE(mem_ctx
);
328 /* Process a command entered at the prompt or as part of -c */
329 static NTSTATUS
process_cmd(struct vfs_state
*vfs
, char *cmd
)
331 struct cmd_list
*temp_list
;
335 NTSTATUS result
= NT_STATUS_OK
;
336 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
339 if (cmd
[strlen(cmd
) - 1] == '\n')
340 cmd
[strlen(cmd
) - 1] = '\0';
342 if (!next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
343 TALLOC_FREE(mem_ctx
);
347 /* Strip the trailing \n if it exists */
349 if (buf
[len
-1] == '\n')
352 /* Search for matching commands */
354 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
355 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
357 while(temp_set
->name
) {
358 if (strequal(buf
, temp_set
->name
)) {
360 result
= do_cmd(vfs
, temp_set
, cmd
);
369 if (!found
&& buf
[0]) {
370 printf("command not found: %s\n", buf
);
371 TALLOC_FREE(mem_ctx
);
375 if (!NT_STATUS_IS_OK(result
)) {
376 printf("result was %s\n", nt_errstr(result
));
379 TALLOC_FREE(mem_ctx
);
383 static void process_file(struct vfs_state
*pvfs
, char *filename
) {
385 char command
[3 * PATH_MAX
];
387 if (*filename
== '-') {
390 file
= fopen(filename
, "r");
392 printf("vfstest: error reading file (%s)!", filename
);
393 printf("errno n.%d: %s", errno
, strerror(errno
));
398 while (fgets(command
, 3 * PATH_MAX
, file
) != NULL
) {
399 process_cmd(pvfs
, command
);
407 void exit_server(const char *reason
)
409 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
413 void exit_server_cleanly(const char *const reason
)
415 exit_server("normal exit");
418 int last_message
= -1;
422 int main(int argc
, char *argv
[])
425 struct cmd_set
**cmd_set
;
426 struct vfs_state vfs
= { 0, };
428 char *filename
= NULL
;
429 char cwd
[MAXPATHLEN
];
430 TALLOC_CTX
*frame
= talloc_stackframe();
431 struct tevent_context
*ev
= tevent_context_init(NULL
);
433 /* make sure the vars that get altered (4th field) are in
434 a fixed location or certain compilers complain */
436 struct poptOption long_options
[] = {
438 {"file", 'f', POPT_ARG_STRING
, &filename
, 0, },
439 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 0, "Execute specified list of commands" },
448 pc
= poptGetContext("vfstest", argc
, (const char **) argv
,
451 while(poptGetNextOpt(pc
) != -1);
456 lp_load_initial_only(get_dyn_CONFIGFILE());
458 /* TODO: check output */
459 reload_services(NULL
, NULL
, false);
461 /* the following functions are part of the Samba debugging
462 facilities. See lib/debug.c */
463 setup_logging("vfstest", DEBUG_STDOUT
);
465 /* Load command lists */
467 cmd_set
= vfstest_command_list
;
470 add_command_set(*cmd_set
);
471 add_command_set(separator_command
);
475 /* some basic initialization stuff */
479 serverid_parent_init(NULL
);
480 vfs
.conn
= talloc_zero(NULL
, connection_struct
);
481 vfs
.conn
->params
= talloc_zero(vfs
.conn
, struct share_params
);
482 vfs
.conn
->sconn
= talloc_zero(NULL
, struct smbd_server_connection
);
483 vfs
.conn
->sconn
->msg_ctx
= messaging_init(vfs
.conn
->sconn
, ev
);
484 make_session_info_guest(NULL
, &vfs
.conn
->session_info
);
485 file_init(vfs
.conn
->sconn
);
486 set_conn_connectpath(vfs
.conn
, getcwd(cwd
, sizeof(cwd
)));
487 for (i
=0; i
< 1024; i
++)
490 /* some advanced initialization stuff */
491 smbd_vfs_init(vfs
.conn
);
493 if (!posix_locking_init(false)) {
497 /* Do we have a file input? */
498 if (filename
&& filename
[0]) {
499 process_file(&vfs
, filename
);
503 /* Do anything specified with -c */
504 if (cmdstr
&& cmdstr
[0]) {
508 while((cmd
=next_command(frame
, &p
)) != NULL
) {
509 process_cmd(&vfs
, cmd
);
516 /* Loop around accepting commands */
521 line
= smb_readline("vfstest $> ", NULL
, completion_fn
);
527 if (line
[0] != '\n') {
528 process_cmd(&vfs
, line
);
533 TALLOC_FREE(vfs
.conn
);