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 "locking/share_mode_lock.h"
28 #include "smbd/smbd.h"
29 #include "smbd/globals.h"
30 #include "lib/cmdline/cmdline.h"
32 #include "../libcli/smbreadline/smbreadline.h"
36 #include "libcli/security/security.h"
37 #include "lib/smbd_shim.h"
38 #include "system/filesys.h"
39 #include "lib/global_contexts.h"
40 #include "lib/param/param.h"
42 /* List to hold groups of commands */
43 static struct cmd_list {
44 struct cmd_list *prev, *next;
45 struct cmd_set *cmd_set;
48 /* shall we do talloc_report after each command? */
49 static int memreports = 0;
51 /****************************************************************************
52 handle completion of commands for readline
53 ****************************************************************************/
54 static char **completion_fn(const char *text, int start, int end)
56 #define MAX_COMPLETIONS 100
59 struct cmd_list *commands = cmd_list;
64 /* make sure we have a list of valid commands */
68 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
69 if (!matches) return NULL;
71 matches[count++] = SMB_STRDUP(text);
72 if (!matches[0]) return NULL;
74 while (commands && count < MAX_COMPLETIONS-1)
76 if (!commands->cmd_set)
79 for (i=0; commands->cmd_set[i].name; i++)
81 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
82 commands->cmd_set[i].fn)
84 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
91 commands = commands->next;
95 SAFE_FREE(matches[0]);
96 matches[0] = SMB_STRDUP(matches[1]);
98 matches[count] = NULL;
102 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
107 if (!cmdstr || !(*cmdstr))
110 p = strchr_m(*cmdstr, ';');
113 command = talloc_strdup(ctx, *cmdstr);
115 /* Pass back the remaining cmdstring
116 (a trailing delimiter ";" does also work),
117 or NULL at last cmdstring.
119 *cmdstr = p ? p + 1 : p;
124 /* Load specified configuration file */
125 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
126 int argc, const char **argv)
129 printf("Usage: %s <smb.conf>\n", argv[0]);
133 if (!lp_load_with_shares(argv[1])) {
134 printf("Error loading \"%s\"\n", argv[1]);
138 printf("\"%s\" successfully loaded\n", argv[1]);
142 /* Display help on commands */
143 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
144 int argc, const char **argv)
146 struct cmd_list *tmp;
147 struct cmd_set *tmp_set;
151 printf("Usage: %s [command]\n", argv[0]);
155 /* Help on one command */
158 for (tmp = cmd_list; tmp; tmp = tmp->next) {
160 tmp_set = tmp->cmd_set;
162 while(tmp_set->name) {
163 if (strequal(argv[1], tmp_set->name)) {
164 if (tmp_set->usage &&
166 printf("%s\n", tmp_set->usage);
168 printf("No help for %s\n", tmp_set->name);
177 printf("No such command: %s\n", argv[1]);
181 /* List all commands */
183 for (tmp = cmd_list; tmp; tmp = tmp->next) {
185 tmp_set = tmp->cmd_set;
187 while(tmp_set->name) {
189 printf("%15s\t\t%s\n", tmp_set->name,
190 tmp_set->description ? tmp_set->description:
200 /* Change the debug level */
201 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
204 printf("Usage: %s [debuglevel]\n", argv[0]);
209 struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
210 lpcfg_set_cmdline(lp_ctx, "log level", argv[1]);
213 printf("debuglevel is %d\n", DEBUGLEVEL);
218 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
221 talloc_destroy(mem_ctx);
228 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
231 talloc_destroy(mem_ctx);
234 return NT_STATUS_OK; /* NOTREACHED */
237 static struct cmd_set vfstest_commands[] = {
239 { .name = "GENERAL OPTIONS" },
241 { "conf", cmd_conf, "Load smb configuration file", "conf <smb.conf>" },
242 { "help", cmd_help, "Get help on commands", "" },
243 { "?", cmd_help, "Get help on commands", "" },
244 { "debuglevel", cmd_debuglevel, "Set debug level", "" },
245 { "freemem", cmd_freemem, "Free currently allocated buffers", "" },
246 { "exit", cmd_quit, "Exit program", "" },
247 { "quit", cmd_quit, "Exit program", "" },
252 static struct cmd_set separator_command[] = {
254 .name = "---------------",
255 .description = "----------------------"
263 extern struct cmd_set vfs_commands[];
264 static struct cmd_set *vfstest_command_list[] = {
270 static void add_command_set(struct cmd_set *cmd_set)
272 struct cmd_list *entry;
274 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
275 DEBUG(0, ("out of memory\n"));
281 entry->cmd_set = cmd_set;
282 DLIST_ADD(cmd_list, entry);
285 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
288 const char **argv = NULL;
289 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
291 TALLOC_CTX *mem_ctx = talloc_stackframe();
294 /* Count number of arguments first time through the loop then
295 allocate memory and strdup them. */
298 while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
300 argv[argc] = talloc_strdup(argv, buf);
306 /* Create argument list */
308 argv = talloc_zero_array(mem_ctx, const char *, argc);
310 fprintf(stderr, "out of memory\n");
311 result = NT_STATUS_NO_MEMORY;
321 /* Call the function */
325 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
327 fprintf (stderr, "Invalid command\n");
336 char **_argv = discard_const_p(char *, argv);
341 if (memreports != 0) {
342 talloc_report_full(mem_ctx, stdout);
344 TALLOC_FREE(mem_ctx);
348 /* Process a command entered at the prompt or as part of -c */
349 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
351 struct cmd_list *temp_list;
355 NTSTATUS result = NT_STATUS_OK;
356 TALLOC_CTX *mem_ctx = talloc_stackframe();
359 if (cmd[strlen(cmd) - 1] == '\n')
360 cmd[strlen(cmd) - 1] = '\0';
362 if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
363 TALLOC_FREE(mem_ctx);
367 /* Strip the trailing \n if it exists */
369 if (buf[len-1] == '\n')
372 /* Search for matching commands */
374 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
375 struct cmd_set *temp_set = temp_list->cmd_set;
377 while(temp_set->name) {
378 if (strequal(buf, temp_set->name)) {
380 result = do_cmd(vfs, temp_set, cmd);
389 if (!found && buf[0]) {
390 printf("command not found: %s\n", buf);
391 TALLOC_FREE(mem_ctx);
395 if (!NT_STATUS_IS_OK(result)) {
396 printf("result was %s\n", nt_errstr(result));
399 TALLOC_FREE(mem_ctx);
403 static void process_file(struct vfs_state *pvfs, char *filename) {
405 char command[3 * PATH_MAX];
407 if (*filename == '-') {
410 file = fopen(filename, "r");
412 printf("vfstest: error reading file (%s)!", filename);
413 printf("errno n.%d: %s", errno, strerror(errno));
418 while (fgets(command, 3 * PATH_MAX, file) != NULL) {
419 process_cmd(pvfs, command);
427 static void vfstest_exit_server(const char * const reason) _NORETURN_;
428 static void vfstest_exit_server(const char * const reason)
430 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
434 static void vfstest_exit_server_cleanly(const char * const reason) _NORETURN_;
435 static void vfstest_exit_server_cleanly(const char * const reason)
437 vfstest_exit_server("normal exit");
440 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
441 struct vfs_state *vfs)
443 struct smb_request *result;
446 result = talloc_zero(mem_ctx, struct smb_request);
447 if (result == NULL) {
450 result->sconn = vfs->conn->sconn;
451 result->mid = ++vfs->mid;
453 inbuf = talloc_array(result, uint8_t, smb_size);
457 SSVAL(inbuf, smb_mid, result->mid);
458 smb_setlen(inbuf, smb_size-4);
459 result->inbuf = inbuf;
468 int main(int argc, const char *argv[])
471 struct cmd_set **cmd_set;
472 struct conn_struct_tos *c = NULL;
473 struct vfs_state *vfs;
476 char *filename = NULL;
478 TALLOC_CTX *frame = talloc_stackframe();
479 struct auth_session_info *session_info = NULL;
480 NTSTATUS status = NT_STATUS_OK;
483 /* make sure the vars that get altered (4th field) are in
484 a fixed location or certain compilers complain */
486 struct poptOption long_options[] = {
491 .argInfo = POPT_ARG_STRING,
495 .longName = "command",
497 .argInfo = POPT_ARG_STRING,
500 .descrip = "Execute specified list of commands",
503 .longName = "memreport",
505 .argInfo = POPT_ARG_INT,
507 .descrip = "Report memory left on talloc stackframe after each command",
513 static const struct smbd_shim vfstest_shim_fns =
515 .exit_server = vfstest_exit_server,
516 .exit_server_cleanly = vfstest_exit_server_cleanly,
523 ok = samba_cmdline_init(frame,
524 SAMBA_CMDLINE_CONFIG_SERVER,
525 true /* require_smbconf */);
531 pc = samba_popt_get_context("vfstest", argc, argv, long_options, 0);
537 while ((opt = poptGetNextOpt(pc)) != -1) {
539 case POPT_ERROR_BADOPT:
540 fprintf(stderr, "\nInvalid option %s: %s\n\n",
541 poptBadOption(pc, 0), poptStrerror(opt));
542 poptPrintUsage(pc, stderr, 0);
549 /* we want total control over the permissions on created files,
550 so set our umask to 0 */
553 /* TODO: check output */
554 reload_services(NULL, NULL, false);
556 per_thread_cwd_check();
558 set_smbd_shim(&vfstest_shim_fns);
560 /* Load command lists */
562 cmd_set = vfstest_command_list;
565 add_command_set(*cmd_set);
566 add_command_set(separator_command);
570 /* some basic initialization stuff */
572 init_guest_session_info(frame);
574 vfs = talloc_zero(frame, struct vfs_state);
578 status = make_session_info_guest(vfs, &session_info);
579 if (!NT_STATUS_IS_OK(status)) {
583 /* Provided by libreplace if not present. Always mallocs. */
584 cwd = get_current_dir_name();
589 status = create_conn_struct_tos_cwd(global_messaging_context(),
595 if (!NT_STATUS_IS_OK(status)) {
600 vfs->conn->share_access = FILE_GENERIC_ALL;
601 vfs->conn->read_only = false;
603 file_init(vfs->conn->sconn);
604 for (i=0; i < 1024; i++)
605 vfs->files[i] = NULL;
607 if (!posix_locking_init(false)) {
611 /* Do we have a file input? */
612 if (filename && filename[0]) {
613 process_file(vfs, filename);
617 /* Do anything specified with -c */
618 if (cmdstr && cmdstr[0]) {
622 while((cmd=next_command(frame, &p)) != NULL) {
623 status = process_cmd(vfs, cmd);
627 return NT_STATUS_IS_OK(status) ? 0 : 1;
630 /* Loop around accepting commands */
635 line = smb_readline("vfstest $> ", NULL, completion_fn);
641 if (line[0] != '\n') {
642 status = process_cmd(vfs, line);
649 return NT_STATUS_IS_OK(status) ? 0 : 1;