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"
37 #include "system/filesys.h"
39 /* List to hold groups of commands */
40 static struct cmd_list
{
41 struct cmd_list
*prev
, *next
;
42 struct cmd_set
*cmd_set
;
45 /****************************************************************************
46 handle completion of commands for readline
47 ****************************************************************************/
48 static char **completion_fn(const char *text
, int start
, int end
)
50 #define MAX_COMPLETIONS 100
53 struct cmd_list
*commands
= cmd_list
;
58 /* make sure we have a list of valid commands */
62 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
63 if (!matches
) return NULL
;
65 matches
[count
++] = SMB_STRDUP(text
);
66 if (!matches
[0]) return NULL
;
68 while (commands
&& count
< MAX_COMPLETIONS
-1)
70 if (!commands
->cmd_set
)
73 for (i
=0; commands
->cmd_set
[i
].name
; i
++)
75 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
76 commands
->cmd_set
[i
].fn
)
78 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
85 commands
= commands
->next
;
89 SAFE_FREE(matches
[0]);
90 matches
[0] = SMB_STRDUP(matches
[1]);
92 matches
[count
] = NULL
;
96 static char *next_command(TALLOC_CTX
*ctx
, char **cmdstr
)
101 if (!cmdstr
|| !(*cmdstr
))
104 p
= strchr_m(*cmdstr
, ';');
107 command
= talloc_strdup(ctx
, *cmdstr
);
113 /* Load specified configuration file */
114 static NTSTATUS
cmd_conf(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
115 int argc
, const char **argv
)
118 printf("Usage: %s <smb.conf>\n", argv
[0]);
122 if (!lp_load(argv
[1], False
, True
, False
, True
)) {
123 printf("Error loading \"%s\"\n", argv
[1]);
127 printf("\"%s\" successfully loaded\n", argv
[1]);
131 /* Display help on commands */
132 static NTSTATUS
cmd_help(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
,
133 int argc
, const char **argv
)
135 struct cmd_list
*tmp
;
136 struct cmd_set
*tmp_set
;
140 printf("Usage: %s [command]\n", argv
[0]);
144 /* Help on one command */
147 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
149 tmp_set
= tmp
->cmd_set
;
151 while(tmp_set
->name
) {
152 if (strequal(argv
[1], tmp_set
->name
)) {
153 if (tmp_set
->usage
&&
155 printf("%s\n", tmp_set
->usage
);
157 printf("No help for %s\n", tmp_set
->name
);
166 printf("No such command: %s\n", argv
[1]);
170 /* List all commands */
172 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
174 tmp_set
= tmp
->cmd_set
;
176 while(tmp_set
->name
) {
178 printf("%15s\t\t%s\n", tmp_set
->name
,
179 tmp_set
->description
? tmp_set
->description
:
189 /* Change the debug level */
190 static NTSTATUS
cmd_debuglevel(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
193 printf("Usage: %s [debuglevel]\n", argv
[0]);
198 lp_set_cmdline("log level", argv
[1]);
201 printf("debuglevel is %d\n", DEBUGLEVEL
);
206 static NTSTATUS
cmd_freemem(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
209 talloc_destroy(mem_ctx
);
216 static NTSTATUS
cmd_quit(struct vfs_state
*vfs
, TALLOC_CTX
*mem_ctx
, int argc
, const char **argv
)
219 talloc_destroy(mem_ctx
);
222 return NT_STATUS_OK
; /* NOTREACHED */
225 static struct cmd_set vfstest_commands
[] = {
227 { "GENERAL OPTIONS" },
229 { "conf", cmd_conf
, "Load smb configuration file", "conf <smb.conf>" },
230 { "help", cmd_help
, "Get help on commands", "" },
231 { "?", cmd_help
, "Get help on commands", "" },
232 { "debuglevel", cmd_debuglevel
, "Set debug level", "" },
233 { "freemem", cmd_freemem
, "Free currently allocated buffers", "" },
234 { "exit", cmd_quit
, "Exit program", "" },
235 { "quit", cmd_quit
, "Exit program", "" },
240 static struct cmd_set separator_command
[] = {
241 { "---------------", NULL
, "----------------------" },
246 extern struct cmd_set vfs_commands
[];
247 static struct cmd_set
*vfstest_command_list
[] = {
253 static void add_command_set(struct cmd_set
*cmd_set
)
255 struct cmd_list
*entry
;
257 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
258 DEBUG(0, ("out of memory\n"));
264 entry
->cmd_set
= cmd_set
;
265 DLIST_ADD(cmd_list
, entry
);
268 static NTSTATUS
do_cmd(struct vfs_state
*vfs
, struct cmd_set
*cmd_entry
, char *cmd
)
272 NTSTATUS result
= NT_STATUS_UNSUCCESSFUL
;
274 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
277 /* Count number of arguments first time through the loop then
278 allocate memory and strdup them. */
281 while(next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
283 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 result
= cmd_entry
->fn(vfs
, mem_ctx
, argc
, (const char **)argv
);
312 fprintf (stderr
, "Invalid command\n");
321 for (i
= 0; i
< argc
; i
++)
327 TALLOC_FREE(mem_ctx
);
331 /* Process a command entered at the prompt or as part of -c */
332 static NTSTATUS
process_cmd(struct vfs_state
*vfs
, char *cmd
)
334 struct cmd_list
*temp_list
;
338 NTSTATUS result
= NT_STATUS_OK
;
339 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
342 if (cmd
[strlen(cmd
) - 1] == '\n')
343 cmd
[strlen(cmd
) - 1] = '\0';
345 if (!next_token_talloc(mem_ctx
, &p
, &buf
, " ")) {
346 TALLOC_FREE(mem_ctx
);
350 /* Strip the trailing \n if it exists */
352 if (buf
[len
-1] == '\n')
355 /* Search for matching commands */
357 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
358 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
360 while(temp_set
->name
) {
361 if (strequal(buf
, temp_set
->name
)) {
363 result
= do_cmd(vfs
, temp_set
, cmd
);
372 if (!found
&& buf
[0]) {
373 printf("command not found: %s\n", buf
);
374 TALLOC_FREE(mem_ctx
);
378 if (!NT_STATUS_IS_OK(result
)) {
379 printf("result was %s\n", nt_errstr(result
));
382 TALLOC_FREE(mem_ctx
);
386 static void process_file(struct vfs_state
*pvfs
, char *filename
) {
388 char command
[3 * PATH_MAX
];
390 if (*filename
== '-') {
393 file
= fopen(filename
, "r");
395 printf("vfstest: error reading file (%s)!", filename
);
396 printf("errno n.%d: %s", errno
, strerror(errno
));
401 while (fgets(command
, 3 * PATH_MAX
, file
) != NULL
) {
402 process_cmd(pvfs
, command
);
410 static void vfstest_exit_server(const char * const reason
)
412 DEBUG(3,("Server exit (%s)\n", (reason
? reason
: "")));
416 static void vfstest_exit_server_cleanly(const char * const reason
)
418 vfstest_exit_server("normal exit");
421 struct smb_request
*vfstest_get_smbreq(TALLOC_CTX
*mem_ctx
,
422 struct vfs_state
*vfs
)
424 struct smb_request
*result
;
426 result
= talloc_zero(mem_ctx
, struct smb_request
);
427 if (result
== NULL
) {
430 result
->sconn
= vfs
->conn
->sconn
;
431 result
->mid
= ++vfs
->mid
;
433 result
->inbuf
= talloc_array(result
, uint8_t, smb_size
);
434 if (result
->inbuf
== NULL
) {
437 SSVAL(result
->inbuf
, smb_mid
, result
->mid
);
438 smb_setlen(result
->inbuf
, smb_size
-4);
447 int main(int argc
, char *argv
[])
450 struct cmd_set
**cmd_set
;
451 struct vfs_state
*vfs
;
453 char *filename
= NULL
;
454 char cwd
[MAXPATHLEN
];
455 TALLOC_CTX
*frame
= talloc_stackframe();
456 struct tevent_context
*ev
= tevent_context_init(NULL
);
457 struct auth_session_info
*session_info
= NULL
;
458 NTSTATUS status
= NT_STATUS_OK
;
460 /* make sure the vars that get altered (4th field) are in
461 a fixed location or certain compilers complain */
463 struct poptOption long_options
[] = {
465 {"file", 'f', POPT_ARG_STRING
, &filename
, 0, },
466 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 0, "Execute specified list of commands" },
470 static const struct smbd_shim vfstest_shim_fns
=
472 .exit_server
= vfstest_exit_server
,
473 .exit_server_cleanly
= vfstest_exit_server_cleanly
,
480 pc
= poptGetContext("vfstest", argc
, (const char **) argv
,
483 while(poptGetNextOpt(pc
) != -1);
488 /* we want total control over the permissions on created files,
489 so set our umask to 0 */
492 lp_load_initial_only(get_dyn_CONFIGFILE());
494 /* TODO: check output */
495 reload_services(NULL
, NULL
, false);
497 /* the following functions are part of the Samba debugging
498 facilities. See lib/debug.c */
499 setup_logging("vfstest", DEBUG_STDOUT
);
501 set_smbd_shim(&vfstest_shim_fns
);
503 /* Load command lists */
505 cmd_set
= vfstest_command_list
;
508 add_command_set(*cmd_set
);
509 add_command_set(separator_command
);
513 /* some basic initialization stuff */
517 serverid_parent_init(NULL
);
518 vfs
= talloc_zero(NULL
, struct vfs_state
);
522 status
= make_session_info_guest(vfs
, &session_info
);
523 if (!NT_STATUS_IS_OK(status
)) {
527 status
= create_conn_struct(vfs
,
529 messaging_init(vfs
, ev
),
532 getcwd(cwd
, sizeof(cwd
)),
534 if (!NT_STATUS_IS_OK(status
)) {
538 vfs
->conn
->share_access
= FILE_GENERIC_ALL
;
539 vfs
->conn
->read_only
= false;
541 serverid_register(messaging_server_id(vfs
->conn
->sconn
->msg_ctx
), 0);
542 file_init(vfs
->conn
->sconn
);
543 for (i
=0; i
< 1024; i
++)
544 vfs
->files
[i
] = NULL
;
546 if (!posix_locking_init(false)) {
550 /* Do we have a file input? */
551 if (filename
&& filename
[0]) {
552 process_file(vfs
, filename
);
556 /* Do anything specified with -c */
557 if (cmdstr
&& cmdstr
[0]) {
561 while((cmd
=next_command(frame
, &p
)) != NULL
) {
562 status
= process_cmd(vfs
, cmd
);
566 return NT_STATUS_IS_OK(status
) ? 0 : 1;
569 /* Loop around accepting commands */
574 line
= smb_readline("vfstest $> ", NULL
, completion_fn
);
580 if (line
[0] != '\n') {
581 status
= process_cmd(vfs
, line
);
588 return NT_STATUS_IS_OK(status
) ? 0 : 1;