This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[samba.git] / source3 / torture / samtest.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SAM module tester
4
5    Copyright (C) 2002 Jelmer Vernooij
6
7    Parts of the code stolen from vfstest by Simo Sorce and Eric Lorimer
8    Parts of the code stolen from rpcclient by Tim Potter
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include "includes.h"
26 #include "samtest.h"
27
28 struct func_entry {
29         char *name;
30         int (*fn)(struct connection_struct *conn, const char *path);
31 };
32
33 /* List to hold groups of commands */
34 static struct cmd_list {
35         struct cmd_list *prev, *next;
36         struct cmd_set *cmd_set;
37 } *cmd_list;
38
39 static char* next_command (char** cmdstr)
40 {
41         static pstring          command;
42         char                    *p;
43         
44         if (!cmdstr || !(*cmdstr))
45                 return NULL;
46         
47         p = strchr_m(*cmdstr, ';');
48         if (p)
49                 *p = '\0';
50         pstrcpy(command, *cmdstr);
51         *cmdstr = p;
52         
53         return command;
54 }
55
56 /* Load specified configuration file */
57 static NTSTATUS cmd_conf(struct samtest_state *sam, TALLOC_CTX *mem_ctx,
58                                                  int argc, char **argv)
59 {
60         if (argc != 2) {
61                 printf("Usage: %s <smb.conf>\n", argv[0]);
62                 return NT_STATUS_OK;
63         }
64
65         if (!lp_load(argv[1], False, True, False)) {
66                 printf("Error loading \"%s\"\n", argv[1]);
67                 return NT_STATUS_OK;
68         }
69
70         printf("\"%s\" successfully loaded\n", argv[1]);
71         return NT_STATUS_OK;
72 }
73
74 /* Display help on commands */
75 static NTSTATUS cmd_help(struct samtest_state *st, TALLOC_CTX *mem_ctx,
76                          int argc, char **argv)
77 {
78         struct cmd_list *tmp;
79         struct cmd_set *tmp_set;
80
81         /* Usage */
82         if (argc > 2) {
83                 printf("Usage: %s [command]\n", argv[0]);
84                 return NT_STATUS_OK;
85         }
86
87         /* Help on one command */
88
89         if (argc == 2) {
90                 for (tmp = cmd_list; tmp; tmp = tmp->next) {
91                         
92                         tmp_set = tmp->cmd_set;
93
94                         while(tmp_set->name) {
95                                 if (strequal(argv[1], tmp_set->name)) {
96                                         if (tmp_set->usage &&
97                                             tmp_set->usage[0])
98                                                 printf("%s\n", tmp_set->usage);
99                                         else
100                                                 printf("No help for %s\n", tmp_set->name);
101
102                                         return NT_STATUS_OK;
103                                 }
104
105                                 tmp_set++;
106                         }
107                 }
108
109                 printf("No such command: %s\n", argv[1]);
110                 return NT_STATUS_OK;
111         }
112
113         /* List all commands */
114
115         for (tmp = cmd_list; tmp; tmp = tmp->next) {
116
117                 tmp_set = tmp->cmd_set;
118
119                 while(tmp_set->name) {
120
121                         printf("%20s\t%s\n", tmp_set->name,
122                                tmp_set->description ? tmp_set->description:
123                                "");
124
125                         tmp_set++;
126                 }
127         }
128
129         return NT_STATUS_OK;
130 }
131
132 /* Change the debug level */
133 static NTSTATUS cmd_debuglevel(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
134 {
135         if (argc > 2) {
136                 printf("Usage: %s [debuglevel]\n", argv[0]);
137                 return NT_STATUS_OK;
138         }
139
140         if (argc == 2) {
141                 DEBUGLEVEL = atoi(argv[1]);
142         }
143
144         printf("debuglevel is %d\n", DEBUGLEVEL);
145
146         return NT_STATUS_OK;
147 }
148
149 static NTSTATUS cmd_quit(struct samtest_state *st, TALLOC_CTX *mem_ctx, int argc, char **argv)
150 {
151         /* Cleanup */
152         talloc_destroy(mem_ctx);
153
154         exit(0);
155         return NT_STATUS_OK; /* NOTREACHED */
156 }
157
158 static struct cmd_set samtest_commands[] = {
159
160         { "GENERAL OPTIONS" },
161
162         { "help",       cmd_help,       "Get help on commands", "" },
163         { "?",          cmd_help,       "Get help on commands", "" },
164         { "conf",   cmd_conf,   "Load smb configuration file", "conf <smb.conf>" },
165         { "debuglevel", cmd_debuglevel, "Set debug level", "" },
166         { "exit",       cmd_quit,       "Exit program", "" },
167         { "quit",       cmd_quit,       "Exit program", "" },
168
169         { NULL }
170 };
171
172 static struct cmd_set separator_command[] = {
173         { "---------------", NULL,      "----------------------" },
174         { NULL }
175 };
176
177
178 /*extern struct cmd_set sam_commands[];*/
179 extern struct cmd_set sam_general_commands[];
180 extern struct cmd_set sam_domain_commands[];
181 extern struct cmd_set sam_account_commands[];
182 extern struct cmd_set sam_group_commands[];
183 static struct cmd_set *samtest_command_list[] = {
184         samtest_commands,
185         sam_general_commands,
186         sam_domain_commands,
187         sam_account_commands,
188         sam_group_commands,
189         NULL
190 };
191
192 static void add_command_set(struct cmd_set *cmd_set)
193 {
194         struct cmd_list *entry;
195
196         if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
197                 DEBUG(0, ("out of memory\n"));
198                 return;
199         }
200
201         ZERO_STRUCTP(entry);
202
203         entry->cmd_set = cmd_set;
204         DLIST_ADD(cmd_list, entry);
205 }
206
207 static NTSTATUS do_cmd(struct samtest_state *st, struct cmd_set *cmd_entry, char *cmd)
208 {
209         char *p = cmd, **argv = NULL;
210         NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
211         TALLOC_CTX *mem_ctx = NULL;
212         pstring buf;
213         int argc = 0, i;
214
215         /* Count number of arguments first time through the loop then
216            allocate memory and strdup them. */
217
218  again:
219         while(next_token(&p, buf, " ", sizeof(buf))) {
220                 if (argv) {
221                         argv[argc] = strdup(buf);
222                 }
223                 
224                 argc++;
225         }
226                                 
227         if (!argv) {
228
229                 /* Create argument list */
230
231                 argv = (char **)malloc(sizeof(char *) * argc);
232                 memset(argv, 0, sizeof(char *) * argc);
233
234                 if (!argv) {
235                         fprintf(stderr, "out of memory\n");
236                         result = NT_STATUS_NO_MEMORY;
237                         goto done;
238                 }
239                                         
240                 p = cmd;
241                 argc = 0;
242                                         
243                 goto again;
244         }
245
246         /* Call the function */
247
248         if (cmd_entry->fn) {
249
250                 if (mem_ctx == NULL) {
251                         /* Create mem_ctx */
252                         if (!(mem_ctx = talloc_init())) {
253                                 DEBUG(0, ("talloc_init() failed\n"));
254                                 goto done;
255                         }
256                 }
257
258                 /* Run command */
259                 result = cmd_entry->fn(st, mem_ctx, argc, argv);
260
261         } else {
262                 fprintf (stderr, "Invalid command\n");
263                 goto done;
264         }
265
266  done:
267                                                 
268         /* Cleanup */
269
270         if (argv) {
271                 for (i = 0; i < argc; i++)
272                         SAFE_FREE(argv[i]);
273         
274                 SAFE_FREE(argv);
275         }
276         
277         return result;
278 }
279
280 /* Process a command entered at the prompt or as part of -c */
281 static NTSTATUS process_cmd(struct samtest_state *st, char *cmd)
282 {
283         struct cmd_list *temp_list;
284         BOOL found = False;
285         pstring buf;
286         char *p = cmd;
287         NTSTATUS result = NT_STATUS_OK;
288         int len = 0;
289
290         if (cmd[strlen(cmd) - 1] == '\n')
291                 cmd[strlen(cmd) - 1] = '\0';
292
293         if (!next_token(&p, buf, " ", sizeof(buf))) {
294                 return NT_STATUS_OK;
295         }
296
297         /* strip the trainly \n if it exsists */
298         len = strlen(buf);
299         if (buf[len-1] == '\n')
300                 buf[len-1] = '\0';
301
302         /* Search for matching commands */
303
304         for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
305                 struct cmd_set *temp_set = temp_list->cmd_set;
306
307                 while(temp_set->name) {
308                         if (strequal(buf, temp_set->name)) {
309                                 found = True;
310                                 result = do_cmd(st, temp_set, cmd);
311
312                                 goto done;
313                         }
314                         temp_set++;
315                 }
316         }
317
318  done:
319         if (!found && buf[0]) {
320                 printf("command not found: %s\n", buf);
321                 return NT_STATUS_OK;
322         }
323
324         if (!NT_STATUS_IS_OK(result)) {
325                 printf("result was %s\n", nt_errstr(result));
326         }
327
328         return result;
329 }
330
331 void exit_server(char *reason)
332 {
333         DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
334         exit(0);
335 }
336
337 static int server_fd = -1;
338 int last_message = -1;
339
340 int smbd_server_fd(void)
341 {
342                 return server_fd;
343 }
344
345 BOOL reload_services(BOOL test)
346 {
347         return True;
348 }
349
350 /* Main function */
351
352 int main(int argc, char *argv[])
353 {
354         BOOL                    interactive = True;
355         int                     opt;
356         static char             *cmdstr = "";
357         static char *opt_logfile=NULL;
358         static char *config_file = dyn_CONFIGFILE;
359         pstring                 logfile;
360         struct cmd_set          **cmd_set;
361         struct samtest_state st;
362
363
364         /* make sure the vars that get altered (4th field) are in
365            a fixed location or certain compilers complain */
366         poptContext pc;
367         struct poptOption long_options[] = {
368                 POPT_AUTOHELP
369                 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
370                 {"command",     'e', POPT_ARG_STRING,   &cmdstr, 'e', "Execute semicolon seperated cmds"},
371                 {"logfile",     'l', POPT_ARG_STRING,   &opt_logfile, 'l', "Logfile to use instead of stdout"},
372                 {"configfile", 'c', POPT_ARG_STRING, &config_file, 0,"use different configuration file",NULL},
373                 { 0, 0, 0, 0}
374         };
375
376         ZERO_STRUCT(st);
377
378         setlinebuf(stdout);
379
380         DEBUGLEVEL = 1;
381
382         pc = poptGetContext("samtest", argc, (const char **) argv,
383                             long_options, 0);
384         
385         while((opt = poptGetNextOpt(pc)) != -1) {
386                 switch (opt) {
387                 case 'l':
388                         slprintf(logfile, sizeof(logfile) - 1, "%s.client", 
389                                  opt_logfile);
390                         lp_set_logfile(logfile);
391                         interactive = False;
392                         break;
393                 }
394         }
395
396         if (!lp_load(config_file,True,False,False)) {
397                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", config_file);
398                 exit(1);
399         }
400
401         poptFreeContext(pc);
402
403         /* the following functions are part of the Samba debugging
404            facilities.  See lib/debug.c */
405         setup_logging("samtest", interactive);
406         if (!interactive) 
407                 reopen_logs();
408         
409         /* Load command lists */
410
411         cmd_set = samtest_command_list;
412
413         while(*cmd_set) {
414                 add_command_set(*cmd_set);
415                 add_command_set(separator_command);
416                 cmd_set++;
417         }
418
419        /* Do anything specified with -c */
420         if (cmdstr[0]) {
421                 char    *cmd;
422                 char    *p = cmdstr;
423  
424                 while((cmd=next_command(&p)) != NULL) {
425                         process_cmd(&st, cmd);
426                 }
427                 
428                 return 0;
429         }
430
431         /* Loop around accepting commands */
432
433         while(1) {
434                 pstring prompt;
435                 char *line;
436
437                 slprintf(prompt, sizeof(prompt) - 1, "samtest $> ");
438
439                 line = smb_readline(prompt, NULL, NULL);
440
441                 if (line == NULL)
442                         break;
443
444                 if (line[0] != '\n')
445                         process_cmd(&st, line);
446         }
447         
448         return 0;
449 }