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"
36 #include "lib/smbd_shim.h"
38 /* List to hold groups of commands */
39 static struct cmd_list
{
40 struct cmd_list
*prev
, *next
;
41 struct cmd_set
*cmd_set
;
44 /****************************************************************************
45 handle completion of commands for readline
46 ****************************************************************************/
47 static char **completion_fn(const char *text
, int start
, int end
)
49 #define MAX_COMPLETIONS 100
52 struct cmd_list
*commands
= cmd_list
;
57 /* make sure we have a list of valid commands */
61 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
62 if (!matches
) return NULL
;
64 matches
[count
++] = SMB_STRDUP(text
);
65 if (!matches
[0]) return NULL
;
67 while (commands
&& count
< MAX_COMPLETIONS
-1)
69 if (!commands
->cmd_set
)
72 for (i
=0; commands
->cmd_set
[i
].name
; i
++)
74 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
75 commands
->cmd_set
[i
].fn
)
77 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
84 commands
= commands
->next
;
88 SAFE_FREE(matches
[0]);
89 matches
[0] = SMB_STRDUP(matches
[1]);
91 matches
[count
] = NULL
;
95 static char *next_command(TALLOC_CTX
*ctx
, char **cmdstr
)
100 if (!cmdstr
|| !(*cmdstr
))
103 p
= strchr_m(*cmdstr
, ';');
106 command
= talloc_strdup(ctx
, *cmdstr
);
112 /* Load specified configuration file */
113 static NTSTATUS
cmd_conf(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
114 int argc
, const char **argv
)
117 printf("Usage: %s <smb.conf>\n", argv
[0]);
121 if (!lp_load(argv
[1], False
, True
, False
, True
)) {
122 printf("Error loading \"%s\"\n", argv
[1]);
126 printf("\"%s\" successfully loaded\n", argv
[1]);
130 /* Display help on commands */
131 static NTSTATUS
cmd_help(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
132 int argc
, const char **argv
)
134 struct cmd_list
*tmp
;
135 struct cmd_set
*tmp_set
;
139 printf("Usage: %s [command]\n", argv
[0]);
143 /* Help on one command */
146 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
148 tmp_set
= tmp
->cmd_set
;
150 while(tmp_set
->name
) {
151 if (strequal(argv
[1], tmp_set
->name
)) {
152 if (tmp_set
->usage
&&
154 printf("%s\n", tmp_set
->usage
);
156 printf("No help for %s\n", tmp_set
->name
);
165 printf("No such command: %s\n", argv
[1]);
169 /* List all commands */
171 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
173 tmp_set
= tmp
->cmd_set
;
175 while(tmp_set
->name
) {
177 printf("%15s\t\t%s\n", tmp_set
->name
,
178 tmp_set
->description
? tmp_set
->description
:
188 /* Change the debug level */
189 static NTSTATUS
cmd_debuglevel(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
192 printf("Usage: %s [debuglevel]\n", argv
[0]);
197 lp_set_cmdline("log level", argv
[1]);
200 printf("debuglevel is %d\n", DEBUGLEVEL
);
205 static NTSTATUS
cmd_freemem(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
208 talloc_destroy(mem_ctx
);
215 static NTSTATUS
cmd_quit(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
218 talloc_destroy(mem_ctx
);
221 return NT_STATUS_OK
; /* NOTREACHED */
224 static struct cmd_set vfstest_commands
[] = {
226 { "GENERAL OPTIONS" },
228 { "conf", cmd_conf
, "Load smb configuration file", "conf <smb.conf>" },
229 { "help", cmd_help
, "Get help on commands", "" },
230 { "?", cmd_help
, "Get help on commands", "" },
231 { "debuglevel", cmd_debuglevel
, "Set debug level", "" },
232 { "freemem", cmd_freemem
, "Free currently allocated buffers", "" },
233 { "exit", cmd_quit
, "Exit program", "" },
234 { "quit", cmd_quit
, "Exit program", "" },
239 static struct cmd_set separator_command
[] = {
240 { "---------------", NULL
, "----------------------" },
245 extern struct cmd_set vfs_commands
[];
246 static struct cmd_set
*vfstest_command_list
[] = {
252 static void add_command_set(struct cmd_set
*cmd_set
)
254 struct cmd_list
*entry
;
256 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
257 DEBUG(0, ("out of memory\n"));
263 entry
->cmd_set
= cmd_set
;
264 DLIST_ADD(cmd_list
, entry
);
267 static NTSTATUS
do_cmd(struct vfs_state
*vfs
, struct cmd_set
*cmd_entry
, char *cmd
)
271 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
273 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
276 /* Count number of arguments first time through the loop then
277 allocate memory and strdup them. */
280 while(next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
282 argv
[argc
] = SMB_STRDUP(buf
);
288 /* Create argument list */
290 argv
= SMB_MALLOC_ARRAY(char *, argc
);
291 memset(argv
, 0, sizeof(char *) * argc
);
294 fprintf(stderr
, "out of memory\n");
295 result
= NT_STATUS_NO_MEMORY
;
305 /* Call the function */
309 result
= cmd_entry
->fn(vfs
, mem_ctx
, argc
, (const char **)argv
);
311 fprintf (stderr
, "Invalid command\n");
320 for (i
= 0; i
< argc
; i
++)
326 TALLOC_FREE(mem_ctx
);
330 /* Process a command entered at the prompt or as part of -c */
331 static NTSTATUS
process_cmd(struct vfs_state
*vfs
, char *cmd
)
333 struct cmd_list
*temp_list
;
337 NTSTATUS result
= NT_STATUS_OK
;
338 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
341 if (cmd
[strlen(cmd
) - 1] == '\n')
342 cmd
[strlen(cmd
) - 1] = '\0';
344 if (!next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
345 TALLOC_FREE(mem_ctx
);
349 /* Strip the trailing \n if it exists */
351 if (buf
[len
-1] == '\n')
354 /* Search for matching commands */
356 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
357 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
359 while(temp_set
->name
) {
360 if (strequal(buf
, temp_set
->name
)) {
362 result
= do_cmd(vfs
, temp_set
, cmd
);
371 if (!found
&& buf
[0]) {
372 printf("command not found: %s\n", buf
);
373 TALLOC_FREE(mem_ctx
);
377 if (!NT_STATUS_IS_OK(result
)) {
378 printf("result was %s\n", nt_errstr(result
));
381 TALLOC_FREE(mem_ctx
);
385 static void process_file(struct vfs_state
*pvfs
, char *filename
) {
387 char command
[3 * PATH_MAX
];
389 if (*filename
== '-') {
392 file
= fopen(filename
, "r");
394 printf("vfstest: error reading file (%s)!", filename
);
395 printf("errno n.%d: %s", errno
, strerror(errno
));
400 while (fgets(command
, 3 * PATH_MAX
, file
) != NULL
) {
401 process_cmd(pvfs
, command
);
409 static void vfstest_exit_server(const char * const reason
)
411 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
415 static void vfstest_exit_server_cleanly(const char * const reason
)
417 vfstest_exit_server("normal exit");
420 struct smb_request
*vfstest_get_smbreq(TALLOC_CTX
*mem_ctx
,
421 struct vfs_state
*vfs
)
423 struct smb_request
*result
;
425 result
= talloc_zero(mem_ctx
, struct smb_request
);
426 if (result
== NULL
) {
429 result
->sconn
= vfs
->conn
->sconn
;
430 result
->mid
= ++vfs
->mid
;
432 result
->inbuf
= talloc_array(result
, uint8_t, smb_size
);
433 if (result
->inbuf
== NULL
) {
436 SSVAL(result
->inbuf
, smb_mid
, result
->mid
);
437 smb_setlen(result
->inbuf
, smb_size
-4);
446 int main(int argc
, char *argv
[])
449 struct cmd_set
**cmd_set
;
450 struct vfs_state
*vfs
;
452 char *filename
= NULL
;
453 char cwd
[MAXPATHLEN
];
454 TALLOC_CTX
*frame
= talloc_stackframe();
455 struct tevent_context
*ev
= tevent_context_init(NULL
);
456 NTSTATUS status
= NT_STATUS_OK
;
458 /* make sure the vars that get altered (4th field) are in
459 a fixed location or certain compilers complain */
461 struct poptOption long_options
[] = {
463 {"file", 'f', POPT_ARG_STRING
, &filename
, 0, },
464 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 0, "Execute specified list of commands" },
468 static const struct smbd_shim vfstest_shim_fns
=
470 .exit_server
= vfstest_exit_server
,
471 .exit_server_cleanly
= vfstest_exit_server_cleanly
,
478 pc
= poptGetContext("vfstest", argc
, (const char **) argv
,
481 while(poptGetNextOpt(pc
) != -1);
486 lp_load_initial_only(get_dyn_CONFIGFILE());
488 /* TODO: check output */
489 reload_services(NULL
, NULL
, false);
491 /* the following functions are part of the Samba debugging
492 facilities. See lib/debug.c */
493 setup_logging("vfstest", DEBUG_STDOUT
);
495 set_smbd_shim(&vfstest_shim_fns
);
497 /* Load command lists */
499 cmd_set
= vfstest_command_list
;
502 add_command_set(*cmd_set
);
503 add_command_set(separator_command
);
507 /* some basic initialization stuff */
511 serverid_parent_init(NULL
);
512 vfs
= talloc_zero(NULL
, struct vfs_state
);
513 vfs
->conn
= talloc_zero(vfs
, connection_struct
);
514 vfs
->conn
->share_access
= FILE_GENERIC_ALL
;
515 vfs
->conn
->params
= talloc_zero(vfs
->conn
, struct share_params
);
516 vfs
->conn
->sconn
= talloc_zero(NULL
, struct smbd_server_connection
);
517 vfs
->conn
->sconn
->msg_ctx
= messaging_init(vfs
->conn
->sconn
, ev
);
518 vfs
->conn
->sconn
->ev_ctx
= ev
;
519 serverid_register(messaging_server_id(vfs
->conn
->sconn
->msg_ctx
), 0);
520 make_session_info_guest(NULL
, &vfs
->conn
->session_info
);
521 file_init(vfs
->conn
->sconn
);
522 set_conn_connectpath(vfs
->conn
, getcwd(cwd
, sizeof(cwd
)));
523 for (i
=0; i
< 1024; i
++)
524 vfs
->files
[i
] = NULL
;
526 /* some advanced initialization stuff */
527 smbd_vfs_init(vfs
->conn
);
529 if (!posix_locking_init(false)) {
533 /* Do we have a file input? */
534 if (filename
&& filename
[0]) {
535 process_file(vfs
, filename
);
539 /* Do anything specified with -c */
540 if (cmdstr
&& cmdstr
[0]) {
544 while((cmd
=next_command(frame
, &p
)) != NULL
) {
545 status
= process_cmd(vfs
, cmd
);
549 return NT_STATUS_IS_OK(status
) ? 0 : 1;
552 /* Loop around accepting commands */
557 line
= smb_readline("vfstest $> ", NULL
, completion_fn
);
563 if (line
[0] != '\n') {
564 status
= process_cmd(vfs
, line
);
571 return NT_STATUS_IS_OK(status
) ? 0 : 1;