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"
35 #include "libcli/security/security.h"
37 /* List to hold groups of commands */
38 static struct cmd_list
{
39 struct cmd_list
*prev
, *next
;
40 struct cmd_set
*cmd_set
;
43 /****************************************************************************
44 handle completion of commands for readline
45 ****************************************************************************/
46 static char **completion_fn(const char *text
, int start
, int end
)
48 #define MAX_COMPLETIONS 100
51 struct cmd_list
*commands
= cmd_list
;
56 /* make sure we have a list of valid commands */
60 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
61 if (!matches
) return NULL
;
63 matches
[count
++] = SMB_STRDUP(text
);
64 if (!matches
[0]) return NULL
;
66 while (commands
&& count
< MAX_COMPLETIONS
-1)
68 if (!commands
->cmd_set
)
71 for (i
=0; commands
->cmd_set
[i
].name
; i
++)
73 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
74 commands
->cmd_set
[i
].fn
)
76 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
83 commands
= commands
->next
;
87 SAFE_FREE(matches
[0]);
88 matches
[0] = SMB_STRDUP(matches
[1]);
90 matches
[count
] = NULL
;
94 static char *next_command(TALLOC_CTX
*ctx
, char **cmdstr
)
99 if (!cmdstr
|| !(*cmdstr
))
102 p
= strchr_m(*cmdstr
, ';');
105 command
= talloc_strdup(ctx
, *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 lp_set_cmdline("log level", 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
= talloc_stackframe();
275 /* Count number of arguments first time through the loop then
276 allocate memory and strdup them. */
279 while(next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
281 argv
[argc
] = SMB_STRDUP(buf
);
287 /* Create argument list */
289 argv
= SMB_MALLOC_ARRAY(char *, argc
);
290 memset(argv
, 0, sizeof(char *) * argc
);
293 fprintf(stderr
, "out of memory\n");
294 result
= NT_STATUS_NO_MEMORY
;
304 /* Call the function */
308 result
= cmd_entry
->fn(vfs
, mem_ctx
, argc
, (const char **)argv
);
310 fprintf (stderr
, "Invalid command\n");
319 for (i
= 0; i
< argc
; i
++)
325 TALLOC_FREE(mem_ctx
);
329 /* Process a command entered at the prompt or as part of -c */
330 static NTSTATUS
process_cmd(struct vfs_state
*vfs
, char *cmd
)
332 struct cmd_list
*temp_list
;
336 NTSTATUS result
= NT_STATUS_OK
;
337 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
340 if (cmd
[strlen(cmd
) - 1] == '\n')
341 cmd
[strlen(cmd
) - 1] = '\0';
343 if (!next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
344 TALLOC_FREE(mem_ctx
);
348 /* Strip the trailing \n if it exists */
350 if (buf
[len
-1] == '\n')
353 /* Search for matching commands */
355 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
356 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
358 while(temp_set
->name
) {
359 if (strequal(buf
, temp_set
->name
)) {
361 result
= do_cmd(vfs
, temp_set
, cmd
);
370 if (!found
&& buf
[0]) {
371 printf("command not found: %s\n", buf
);
372 TALLOC_FREE(mem_ctx
);
376 if (!NT_STATUS_IS_OK(result
)) {
377 printf("result was %s\n", nt_errstr(result
));
380 TALLOC_FREE(mem_ctx
);
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
);
408 void exit_server(const char *reason
)
410 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
414 void exit_server_cleanly(const char *const reason
)
416 exit_server("normal exit");
419 struct smb_request
*vfstest_get_smbreq(TALLOC_CTX
*mem_ctx
,
420 struct vfs_state
*vfs
)
422 struct smb_request
*result
;
424 result
= talloc_zero(mem_ctx
, struct smb_request
);
425 if (result
== NULL
) {
428 result
->sconn
= vfs
->conn
->sconn
;
429 result
->mid
= ++vfs
->mid
;
431 result
->inbuf
= talloc_array(result
, uint8_t, smb_size
);
432 if (result
->inbuf
== NULL
) {
435 SSVAL(result
->inbuf
, smb_mid
, result
->mid
);
436 smb_setlen(result
->inbuf
, smb_size
-4);
445 int main(int argc
, char *argv
[])
448 struct cmd_set
**cmd_set
;
449 struct vfs_state vfs
= { 0, };
451 char *filename
= NULL
;
452 char cwd
[MAXPATHLEN
];
453 TALLOC_CTX
*frame
= talloc_stackframe();
454 struct tevent_context
*ev
= tevent_context_init(NULL
);
455 NTSTATUS status
= NT_STATUS_OK
;
457 /* make sure the vars that get altered (4th field) are in
458 a fixed location or certain compilers complain */
460 struct poptOption long_options
[] = {
462 {"file", 'f', POPT_ARG_STRING
, &filename
, 0, },
463 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 0, "Execute specified list of commands" },
472 pc
= poptGetContext("vfstest", argc
, (const char **) argv
,
475 while(poptGetNextOpt(pc
) != -1);
480 lp_load_initial_only(get_dyn_CONFIGFILE());
482 /* TODO: check output */
483 reload_services(NULL
, NULL
, false);
485 /* the following functions are part of the Samba debugging
486 facilities. See lib/debug.c */
487 setup_logging("vfstest", DEBUG_STDOUT
);
489 /* Load command lists */
491 cmd_set
= vfstest_command_list
;
494 add_command_set(*cmd_set
);
495 add_command_set(separator_command
);
499 /* some basic initialization stuff */
503 serverid_parent_init(NULL
);
504 vfs
.conn
= talloc_zero(NULL
, connection_struct
);
505 vfs
.conn
->share_access
= FILE_GENERIC_ALL
;
506 vfs
.conn
->params
= talloc_zero(vfs
.conn
, struct share_params
);
507 vfs
.conn
->sconn
= talloc_zero(NULL
, struct smbd_server_connection
);
508 vfs
.conn
->sconn
->msg_ctx
= messaging_init(vfs
.conn
->sconn
, ev
);
509 vfs
.conn
->sconn
->ev_ctx
= ev
;
510 serverid_register(messaging_server_id(vfs
.conn
->sconn
->msg_ctx
), 0);
511 make_session_info_guest(NULL
, &vfs
.conn
->session_info
);
512 file_init(vfs
.conn
->sconn
);
513 set_conn_connectpath(vfs
.conn
, getcwd(cwd
, sizeof(cwd
)));
514 for (i
=0; i
< 1024; i
++)
517 /* some advanced initialization stuff */
518 smbd_vfs_init(vfs
.conn
);
520 if (!posix_locking_init(false)) {
524 /* Do we have a file input? */
525 if (filename
&& filename
[0]) {
526 process_file(&vfs
, filename
);
530 /* Do anything specified with -c */
531 if (cmdstr
&& cmdstr
[0]) {
535 while((cmd
=next_command(frame
, &p
)) != NULL
) {
536 status
= process_cmd(&vfs
, cmd
);
540 return NT_STATUS_IS_OK(status
) ? 0 : 1;
543 /* Loop around accepting commands */
548 line
= smb_readline("vfstest $> ", NULL
, completion_fn
);
554 if (line
[0] != '\n') {
555 status
= process_cmd(&vfs
, line
);
560 TALLOC_FREE(vfs
.conn
);
562 return NT_STATUS_IS_OK(status
) ? 0 : 1;