lib: load_case_tables() -> smb_init_locale()
[kai/samba-autobuild/.git] / source3 / torture / vfstest.c
1 /*
2    Unix SMB/CIFS implementation.
3    VFS module tester
4
5    Copyright (C) Simo Sorce 2002
6    Copyright (C) Eric Lorimer 2002
7    Copyright (C) Jelmer Vernooij 2002,2003
8
9    Most of this code was ripped off of rpcclient.
10    Copyright (C) Tim Potter 2000-2001
11
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.
16
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.
21
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/>.
24 */
25
26 #include "includes.h"
27 #include "smbd/smbd.h"
28 #include "smbd/globals.h"
29 #include "popt_common.h"
30 #include "vfstest.h"
31 #include "../libcli/smbreadline/smbreadline.h"
32 #include "auth.h"
33 #include "serverid.h"
34 #include "messages.h"
35 #include "libcli/security/security.h"
36 #include "lib/smbd_shim.h"
37 #include "system/filesys.h"
38
39 /* List to hold groups of commands */
40 static struct cmd_list {
41         struct cmd_list *prev, *next;
42         struct cmd_set *cmd_set;
43 } *cmd_list;
44
45 /* shall we do talloc_report after each command? */
46 static int memreports = 0;
47
48 /****************************************************************************
49 handle completion of commands for readline
50 ****************************************************************************/
51 static char **completion_fn(const char *text, int start, int end)
52 {
53 #define MAX_COMPLETIONS 100
54         char **matches;
55         int i, count=0;
56         struct cmd_list *commands = cmd_list;
57
58         if (start)
59                 return NULL;
60
61         /* make sure we have a list of valid commands */
62         if (!commands)
63                 return NULL;
64
65         matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
66         if (!matches) return NULL;
67
68         matches[count++] = SMB_STRDUP(text);
69         if (!matches[0]) return NULL;
70
71         while (commands && count < MAX_COMPLETIONS-1)
72         {
73                 if (!commands->cmd_set)
74                         break;
75
76                 for (i=0; commands->cmd_set[i].name; i++)
77                 {
78                         if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
79                                 commands->cmd_set[i].fn)
80                         {
81                                 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
82                                 if (!matches[count])
83                                         return NULL;
84                                 count++;
85                         }
86                 }
87
88                 commands = commands->next;
89         }
90
91         if (count == 2) {
92                 SAFE_FREE(matches[0]);
93                 matches[0] = SMB_STRDUP(matches[1]);
94         }
95         matches[count] = NULL;
96         return matches;
97 }
98
99 static char *next_command(TALLOC_CTX *ctx, char **cmdstr)
100 {
101         char *command;
102         char *p;
103
104         if (!cmdstr || !(*cmdstr))
105                 return NULL;
106
107         p = strchr_m(*cmdstr, ';');
108         if (p)
109                 *p = '\0';
110         command = talloc_strdup(ctx, *cmdstr);
111
112         /* Pass back the remaining cmdstring 
113            (a trailing delimiter ";" does also work),
114            or NULL at last cmdstring.
115         */
116         *cmdstr = p ? p + 1 : p;
117
118         return command;
119 }
120
121 /* Load specified configuration file */
122 static NTSTATUS cmd_conf(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
123                         int argc, const char **argv)
124 {
125         if (argc != 2) {
126                 printf("Usage: %s <smb.conf>\n", argv[0]);
127                 return NT_STATUS_OK;
128         }
129
130         if (!lp_load(argv[1], False, True, False, True)) {
131                 printf("Error loading \"%s\"\n", argv[1]);
132                 return NT_STATUS_OK;
133         }
134
135         printf("\"%s\" successfully loaded\n", argv[1]);
136         return NT_STATUS_OK;
137 }
138
139 /* Display help on commands */
140 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
141                          int argc, const char **argv)
142 {
143         struct cmd_list *tmp;
144         struct cmd_set *tmp_set;
145
146         /* Usage */
147         if (argc > 2) {
148                 printf("Usage: %s [command]\n", argv[0]);
149                 return NT_STATUS_OK;
150         }
151
152         /* Help on one command */
153
154         if (argc == 2) {
155                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
156
157                         tmp_set = tmp->cmd_set;
158
159                         while(tmp_set->name) {
160                                 if (strequal(argv[1], tmp_set->name)) {
161                                         if (tmp_set->usage &&
162                                             tmp_set->usage[0])
163                                                 printf("%s\n", tmp_set->usage);
164                                         else
165                                                 printf("No help for %s\n", tmp_set->name);
166
167                                         return NT_STATUS_OK;
168                                 }
169
170                                 tmp_set++;
171                         }
172                 }
173
174                 printf("No such command: %s\n", argv[1]);
175                 return NT_STATUS_OK;
176         }
177
178         /* List all commands */
179
180         for (tmp = cmd_list; tmp; tmp = tmp->next) {
181
182                 tmp_set = tmp->cmd_set;
183
184                 while(tmp_set->name) {
185
186                         printf("%15s\t\t%s\n", tmp_set->name,
187                                tmp_set->description ? tmp_set->description:
188                                "");
189
190                         tmp_set++;
191                 }
192         }
193
194         return NT_STATUS_OK;
195 }
196
197 /* Change the debug level */
198 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
199 {
200         if (argc > 2) {
201                 printf("Usage: %s [debuglevel]\n", argv[0]);
202                 return NT_STATUS_OK;
203         }
204
205         if (argc == 2) {
206                 lp_set_cmdline("log level", argv[1]);
207         }
208
209         printf("debuglevel is %d\n", DEBUGLEVEL);
210
211         return NT_STATUS_OK;
212 }
213
214 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
215 {
216         /* Cleanup */
217         talloc_destroy(mem_ctx);
218         mem_ctx = NULL;
219         vfs->data = NULL;
220         vfs->data_size = 0;
221         return NT_STATUS_OK;
222 }
223
224 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, const char **argv)
225 {
226         /* Cleanup */
227         talloc_destroy(mem_ctx);
228
229         exit(0);
230         return NT_STATUS_OK; /* NOTREACHED */
231 }
232
233 static struct cmd_set vfstest_commands[] = {
234
235         { "GENERAL OPTIONS" },
236
237         { "conf",       cmd_conf,       "Load smb configuration file", "conf <smb.conf>" },
238         { "help",       cmd_help,       "Get help on commands", "" },
239         { "?",          cmd_help,       "Get help on commands", "" },
240         { "debuglevel", cmd_debuglevel, "Set debug level", "" },
241         { "freemem",    cmd_freemem,    "Free currently allocated buffers", "" },
242         { "exit",       cmd_quit,       "Exit program", "" },
243         { "quit",       cmd_quit,       "Exit program", "" },
244
245         { NULL }
246 };
247
248 static struct cmd_set separator_command[] = {
249         { "---------------", NULL,      "----------------------" },
250         { NULL }
251 };
252
253
254 extern struct cmd_set vfs_commands[];
255 static struct cmd_set *vfstest_command_list[] = {
256         vfstest_commands,
257         vfs_commands,
258         NULL
259 };
260
261 static void add_command_set(struct cmd_set *cmd_set)
262 {
263         struct cmd_list *entry;
264
265         if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
266                 DEBUG(0, ("out of memory\n"));
267                 return;
268         }
269
270         ZERO_STRUCTP(entry);
271
272         entry->cmd_set = cmd_set;
273         DLIST_ADD(cmd_list, entry);
274 }
275
276 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
277 {
278         const char *p = cmd;
279         const char **argv = NULL;
280         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
281         char *buf;
282         TALLOC_CTX *mem_ctx = talloc_stackframe();
283         int argc = 0;
284
285         /* Count number of arguments first time through the loop then
286            allocate memory and strdup them. */
287
288  again:
289         while(next_token_talloc(mem_ctx, &p, &buf, " ")) {
290                 if (argv) {
291                         argv[argc] = talloc_strdup(argv, buf);
292                 }
293                 argc++;
294         }
295
296         if (!argv) {
297                 /* Create argument list */
298
299                 argv = talloc_zero_array(mem_ctx, const char *, argc);
300                 if (argv == NULL) {
301                         fprintf(stderr, "out of memory\n");
302                         result = NT_STATUS_NO_MEMORY;
303                         goto done;
304                 }
305
306                 p = cmd;
307                 argc = 0;
308
309                 goto again;
310         }
311
312         /* Call the function */
313
314         if (cmd_entry->fn) {
315                 /* Run command */
316                 result = cmd_entry->fn(vfs, mem_ctx, argc, (const char **)argv);
317         } else {
318                 fprintf (stderr, "Invalid command\n");
319                 goto done;
320         }
321
322  done:
323
324         /* Cleanup */
325
326         if (argv) {
327                 char **_argv = discard_const_p(char *, argv);
328                 TALLOC_FREE(_argv);
329                 argv = NULL;
330         }
331
332         if (memreports != 0) {
333                 talloc_report_full(mem_ctx, stdout);
334         }
335         TALLOC_FREE(mem_ctx);
336         return result;
337 }
338
339 /* Process a command entered at the prompt or as part of -c */
340 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
341 {
342         struct cmd_list *temp_list;
343         bool found = False;
344         char *buf;
345         const char *p = cmd;
346         NTSTATUS result = NT_STATUS_OK;
347         TALLOC_CTX *mem_ctx = talloc_stackframe();
348         int len = 0;
349
350         if (cmd[strlen(cmd) - 1] == '\n')
351                 cmd[strlen(cmd) - 1] = '\0';
352
353         if (!next_token_talloc(mem_ctx, &p, &buf, " ")) {
354                 TALLOC_FREE(mem_ctx);
355                 return NT_STATUS_OK;
356         }
357
358         /* Strip the trailing \n if it exists */
359         len = strlen(buf);
360         if (buf[len-1] == '\n')
361                 buf[len-1] = '\0';
362
363         /* Search for matching commands */
364
365         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
366                 struct cmd_set *temp_set = temp_list->cmd_set;
367
368                 while(temp_set->name) {
369                         if (strequal(buf, temp_set->name)) {
370                                 found = True;
371                                 result = do_cmd(vfs, temp_set, cmd);
372
373                                 goto done;
374                         }
375                         temp_set++;
376                 }
377         }
378
379  done:
380         if (!found && buf[0]) {
381                 printf("command not found: %s\n", buf);
382                 TALLOC_FREE(mem_ctx);
383                 return NT_STATUS_OK;
384         }
385
386         if (!NT_STATUS_IS_OK(result)) {
387                 printf("result was %s\n", nt_errstr(result));
388         }
389
390         TALLOC_FREE(mem_ctx);
391         return result;
392 }
393
394 static void process_file(struct vfs_state *pvfs, char *filename) {
395         FILE *file;
396         char command[3 * PATH_MAX];
397
398         if (*filename == '-') {
399                 file = stdin;
400         } else {
401                 file = fopen(filename, "r");
402                 if (file == NULL) {
403                         printf("vfstest: error reading file (%s)!", filename);
404                         printf("errno n.%d: %s", errno, strerror(errno));
405                         exit(-1);
406                 }
407         }
408
409         while (fgets(command, 3 * PATH_MAX, file) != NULL) {
410                 process_cmd(pvfs, command);
411         }
412
413         if (file != stdin) {
414                 fclose(file);
415         }
416 }
417
418 static void vfstest_exit_server(const char * const reason) _NORETURN_;
419 static void vfstest_exit_server(const char * const reason)
420 {
421         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
422         exit(0);
423 }
424
425 static void vfstest_exit_server_cleanly(const char * const reason) _NORETURN_;
426 static void vfstest_exit_server_cleanly(const char * const reason)
427 {
428         vfstest_exit_server("normal exit");
429 }
430
431 struct smb_request *vfstest_get_smbreq(TALLOC_CTX *mem_ctx,
432                                        struct vfs_state *vfs)
433 {
434         struct smb_request *result;
435         uint8_t *inbuf;
436
437         result = talloc_zero(mem_ctx, struct smb_request);
438         if (result == NULL) {
439                 return NULL;
440         }
441         result->sconn = vfs->conn->sconn;
442         result->mid = ++vfs->mid;
443
444         inbuf = talloc_array(result, uint8_t, smb_size);
445         if (inbuf == NULL) {
446                 goto fail;
447         }
448         SSVAL(inbuf, smb_mid, result->mid);
449         smb_setlen(inbuf, smb_size-4);
450         result->inbuf = inbuf;
451         return result;
452 fail:
453         TALLOC_FREE(result);
454         return NULL;
455 }
456
457 /* Main function */
458
459 int main(int argc, const char *argv[])
460 {
461         char *cmdstr = NULL;
462         struct cmd_set  **cmd_set;
463         struct vfs_state *vfs;
464         int i;
465         char *filename = NULL;
466         char cwd[MAXPATHLEN];
467         TALLOC_CTX *frame = talloc_stackframe();
468         struct tevent_context *ev;
469         struct auth_session_info *session_info = NULL;
470         NTSTATUS status = NT_STATUS_OK;
471
472         /* make sure the vars that get altered (4th field) are in
473            a fixed location or certain compilers complain */
474         poptContext pc;
475         struct poptOption long_options[] = {
476                 POPT_AUTOHELP
477                 {"file",        'f', POPT_ARG_STRING,   &filename, 0, },
478                 {"command",     'c', POPT_ARG_STRING,   &cmdstr, 0, "Execute specified list of commands" },
479                 {"memreport",   'm', POPT_ARG_INT,      &memreports, 0,
480                  "Report memory left on talloc stackframe after each command" },
481                 POPT_COMMON_SAMBA
482                 POPT_TABLEEND
483         };
484         static const struct smbd_shim vfstest_shim_fns =
485         {
486                 .exit_server = vfstest_exit_server,
487                 .exit_server_cleanly = vfstest_exit_server_cleanly,
488         };
489
490         smb_init_locale();
491
492         setlinebuf(stdout);
493
494         pc = poptGetContext("vfstest", argc, argv, long_options, 0);
495
496         while(poptGetNextOpt(pc) != -1);
497
498
499         poptFreeContext(pc);
500
501         /* we want total control over the permissions on created files,
502            so set our umask to 0 */
503         umask(0);
504
505         lp_load_initial_only(get_dyn_CONFIGFILE());
506
507         /* TODO: check output */
508         reload_services(NULL, NULL, false);
509
510         /* the following functions are part of the Samba debugging
511            facilities.  See lib/debug.c */
512         setup_logging("vfstest", DEBUG_STDOUT);
513
514         set_smbd_shim(&vfstest_shim_fns);
515
516         /* Load command lists */
517
518         cmd_set = vfstest_command_list;
519
520         while(*cmd_set) {
521                 add_command_set(*cmd_set);
522                 add_command_set(separator_command);
523                 cmd_set++;
524         }
525
526         /* some basic initialization stuff */
527         sec_init();
528         init_guest_info();
529         locking_init();
530         serverid_parent_init(NULL);
531         vfs = talloc_zero(NULL, struct vfs_state);
532         if (vfs == NULL) {
533                 return 1;
534         }
535         status = make_session_info_guest(vfs, &session_info);
536         if (!NT_STATUS_IS_OK(status)) {
537                 return 1;
538         }
539
540         ev = server_event_context();
541
542         status = create_conn_struct(vfs,
543                                 ev,
544                                 server_messaging_context(),
545                                 &vfs->conn,
546                                 -1,
547                                 getcwd(cwd, sizeof(cwd)),
548                                 session_info);
549         if (!NT_STATUS_IS_OK(status)) {
550                 return 1;
551         }
552
553         vfs->conn->share_access = FILE_GENERIC_ALL;
554         vfs->conn->read_only = false;
555
556         serverid_register(messaging_server_id(vfs->conn->sconn->msg_ctx), 0);
557         file_init(vfs->conn->sconn);
558         for (i=0; i < 1024; i++)
559                 vfs->files[i] = NULL;
560
561         if (!posix_locking_init(false)) {
562                 return 1;
563         }
564
565         /* Do we have a file input? */
566         if (filename && filename[0]) {
567                 process_file(vfs, filename);
568                 return 0;
569         }
570
571         /* Do anything specified with -c */
572         if (cmdstr && cmdstr[0]) {
573                 char    *cmd;
574                 char    *p = cmdstr;
575
576                 while((cmd=next_command(frame, &p)) != NULL) {
577                         status = process_cmd(vfs, cmd);
578                 }
579
580                 TALLOC_FREE(cmd);
581                 return NT_STATUS_IS_OK(status) ? 0 : 1;
582         }
583
584         /* Loop around accepting commands */
585
586         while(1) {
587                 char *line = NULL;
588
589                 line = smb_readline("vfstest $> ", NULL, completion_fn);
590
591                 if (line == NULL) {
592                         break;
593                 }
594
595                 if (line[0] != '\n') {
596                         status = process_cmd(vfs, line);
597                 }
598                 SAFE_FREE(line);
599         }
600
601         TALLOC_FREE(vfs);
602         TALLOC_FREE(frame);
603         return NT_STATUS_IS_OK(status) ? 0 : 1;
604 }