enumeration tests (files, sessions, shares, connections).
[samba.git] / source3 / utils / rpctorture.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
25
26 #include "includes.h"
27
28 #ifndef REGISTER
29 #define REGISTER 0
30 #endif
31
32 extern pstring scope;
33 extern pstring global_myname;
34
35 extern pstring user_socket_options;
36
37
38 extern pstring debugf;
39 extern int DEBUGLEVEL;
40
41
42 extern file_info def_finfo;
43
44 #define CNV_LANG(s) dos2unix_format(s,False)
45 #define CNV_INPUT(s) unix2dos_format(s,True)
46
47 static struct cli_state smbcli;
48 struct cli_state *smb_cli = &smbcli;
49
50 FILE *out_hnd;
51
52 static pstring password; /* local copy only, if one is entered */
53
54 /****************************************************************************
55 initialise smb client structure
56 ****************************************************************************/
57 void rpcclient_init(void)
58 {
59         bzero(smb_cli, sizeof(smb_cli));
60         cli_initialise(smb_cli);
61         smb_cli->capabilities |= CAP_NT_SMBS;
62 }
63
64 /****************************************************************************
65 make smb client connection
66 ****************************************************************************/
67 static BOOL rpcclient_connect(struct client_info *info)
68 {
69         struct nmb_name calling;
70         struct nmb_name called;
71
72         make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type, scope);
73         make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0            , scope);
74
75         if (!cli_establish_connection(smb_cli, 
76                                   info->dest_host, &info->dest_ip, 
77                                   &calling, &called,
78                                   info->share, info->svc_type,
79                                   False, True))
80         {
81                 DEBUG(0,("rpcclient_connect: connection failed\n"));
82                 cli_shutdown(smb_cli);
83                 return False;
84         }
85
86         return True;
87 }
88
89 /****************************************************************************
90 stop the smb connection(s?)
91 ****************************************************************************/
92 static void rpcclient_stop(void)
93 {
94         cli_shutdown(smb_cli);
95 }
96
97 /****************************************************************************
98   log in as an nt user, log out again. 
99 ****************************************************************************/
100 void run_enums_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
101 {
102         int i;
103
104         /* establish connections.  nothing to stop these being re-established. */
105         rpcclient_connect(cli_info);
106
107         DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
108         if (cli->fd <= 0)
109         {
110                 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
111                                  cli_info->dest_host, cli_info->name_type);
112                 return;
113         }
114         
115         for (i = 0; i < num_ops; i++)
116         {
117                 cmd_srv_enum_sess(cli_info);
118                 cmd_srv_enum_shares(cli_info);
119                 cmd_srv_enum_files(cli_info);
120                 cmd_srv_enum_conn(cli_info);
121         }
122
123         rpcclient_stop();
124
125 }
126
127 /****************************************************************************
128   log in as an nt user, log out again. 
129 ****************************************************************************/
130 void run_ntlogin_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
131 {
132         pstring cmd;
133         int i;
134
135         /* establish connections.  nothing to stop these being re-established. */
136         rpcclient_connect(cli_info);
137
138         DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
139         if (cli->fd <= 0)
140         {
141                 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
142                                  cli_info->dest_host, cli_info->name_type);
143                 return;
144         }
145         
146         for (i = 0; i < num_ops; i++)
147         {
148                 slprintf(cmd, sizeof(cmd)-1, "%s %s", cli->user_name, password);
149                 set_first_token(cmd);
150
151                 cmd_netlogon_login_test(cli_info);
152         }
153
154         rpcclient_stop();
155
156 }
157
158 /****************************************************************************
159   runs n simultaneous functions.
160 ****************************************************************************/
161 static void create_procs(int nprocs, int numops, 
162                 struct client_info *cli_info, struct cli_state *cli,
163                 void (*fn)(int, struct client_info *, struct cli_state *))
164 {
165         int i, status;
166
167         for (i=0;i<nprocs;i++)
168         {
169                 if (fork() == 0)
170                 {
171                         int mypid = getpid();
172                         sys_srandom(mypid ^ time(NULL));
173                         fn(numops, cli_info, cli);
174                         fflush(out_hnd);
175                         _exit(0);
176                 }
177         }
178
179         for (i=0;i<nprocs;i++)
180         {
181                 waitpid(0, &status, 0);
182         }
183 }
184 /****************************************************************************
185 usage on the program - OUT OF DATE!
186 ****************************************************************************/
187 static void usage(char *pname)
188 {
189   fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
190            pname);
191
192   fprintf(out_hnd, "\nVersion %s\n",VERSION);
193   fprintf(out_hnd, "\t-d debuglevel         set the debuglevel\n");
194   fprintf(out_hnd, "\t-l log basename.      Basename for log/debug files\n");
195   fprintf(out_hnd, "\t-n netbios name.      Use this name as my netbios name\n");
196   fprintf(out_hnd, "\t-m max protocol       set the max protocol level\n");
197   fprintf(out_hnd, "\t-I dest IP            use this IP to connect to\n");
198   fprintf(out_hnd, "\t-E                    write messages to stderr instead of stdout\n");
199   fprintf(out_hnd, "\t-U username           set the network username\n");
200   fprintf(out_hnd, "\t-W workgroup          set the workgroup name\n");
201   fprintf(out_hnd, "\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
202   fprintf(out_hnd, "\n");
203 }
204
205 enum client_action
206 {
207         CLIENT_NONE,
208         CLIENT_IPC,
209         CLIENT_SVC
210 };
211
212 /****************************************************************************
213   main program
214 ****************************************************************************/
215  int main(int argc,char *argv[])
216 {
217         char *pname = argv[0];
218         int opt;
219         extern FILE *dbf;
220         extern char *optarg;
221         extern int optind;
222         static pstring servicesf = CONFIGFILE;
223         pstring term_code;
224         BOOL got_pass = False;
225         char *cmd_str="";
226         mode_t myumask = 0755;
227         enum client_action cli_action = CLIENT_NONE;
228         int nprocs = 1;
229         int numops = 100;
230
231         struct client_info cli_info;
232
233         out_hnd = stdout;
234
235         rpcclient_init();
236
237 #ifdef KANJI
238         pstrcpy(term_code, KANJI);
239 #else /* KANJI */
240         *term_code = 0;
241 #endif /* KANJI */
242
243         if (!lp_load(servicesf,True, False, False))
244         {
245                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
246         }
247
248         codepage_initialise(lp_client_code_page());
249
250         DEBUGLEVEL = 0;
251
252         cli_info.put_total_size = 0;
253         cli_info.put_total_time_ms = 0;
254         cli_info.get_total_size = 0;
255         cli_info.get_total_time_ms = 0;
256
257         cli_info.dir_total = 0;
258         cli_info.newer_than = 0;
259         cli_info.archive_level = 0;
260         cli_info.print_mode = 1;
261
262         cli_info.translation = False;
263         cli_info.recurse_dir = False;
264         cli_info.lowercase = False;
265         cli_info.prompt = True;
266         cli_info.abort_mget = True;
267
268         cli_info.dest_ip.s_addr = 0;
269         cli_info.name_type = 0x20;
270
271         pstrcpy(cli_info.cur_dir , "\\");
272         pstrcpy(cli_info.file_sel, "");
273         pstrcpy(cli_info.base_dir, "");
274         pstrcpy(smb_cli->domain, "");
275         pstrcpy(smb_cli->user_name, "");
276         pstrcpy(cli_info.myhostname, "");
277         pstrcpy(cli_info.dest_host, "");
278
279         pstrcpy(cli_info.svc_type, "A:");
280         pstrcpy(cli_info.share, "");
281         pstrcpy(cli_info.service, "");
282
283         pstrcpy(cli_info.dom.level3_sid, "");
284         pstrcpy(cli_info.dom.level3_dom, "");
285         pstrcpy(cli_info.dom.level5_sid, "");
286         pstrcpy(cli_info.dom.level5_dom, "");
287
288         smb_cli->nt_pipe_fnum   = 0xffff;
289
290         setup_logging(pname, True);
291
292         TimeInit();
293         charset_initialise();
294
295         myumask = umask(0);
296         umask(myumask);
297
298         if (!get_myname(global_myname, NULL))
299         {
300                 fprintf(stderr, "Failed to get my hostname.\n");
301         }
302
303         password[0] = 0;
304
305         if (argc < 2)
306         {
307                 usage(pname);
308                 exit(1);
309         }
310
311         if (*argv[1] != '-')
312         {
313                 pstrcpy(cli_info.service, argv[1]);  
314                 /* Convert any '/' characters in the service name to '\' characters */
315                 string_replace( cli_info.service, '/','\\');
316                 argc--;
317                 argv++;
318
319                 DEBUG(1,("service: %s\n", cli_info.service));
320
321                 if (count_chars(cli_info.service,'\\') < 3)
322                 {
323                         usage(pname);
324                         printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
325                         exit(1);
326                 }
327
328                 /*
329                 if (count_chars(cli_info.service,'\\') > 3)
330                 {
331                         usage(pname);
332                         printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
333                         exit(1);
334                 }
335                 */
336
337                 if (argc > 1 && (*argv[1] != '-'))
338                 {
339                         got_pass = True;
340                         pstrcpy(password,argv[1]);  
341                         memset(argv[1],'X',strlen(argv[1]));
342                         argc--;
343                         argv++;
344                 }
345
346                 cli_action = CLIENT_SVC;
347         }
348
349         while ((opt = getopt(argc, argv,"s:B:O:M:S:i:N:o:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
350         {
351                 switch (opt)
352                 {
353                         case 'm':
354                         {
355                                 /* FIXME ... max_protocol seems to be funny here */
356
357                                 int max_protocol = 0;
358                                 max_protocol = interpret_protocol(optarg,max_protocol);
359                                 fprintf(stderr, "max protocol not currently supported\n");
360                                 break;
361                         }
362
363                         case 'O':
364                         {
365                                 pstrcpy(user_socket_options,optarg);
366                                 break;  
367                         }
368
369                         case 'S':
370                         {
371                                 pstrcpy(cli_info.dest_host,optarg);
372                                 strupper(cli_info.dest_host);
373                                 cli_action = CLIENT_IPC;
374                                 break;
375                         }
376
377                         case 'B':
378                         {
379                                 iface_set_default(NULL,optarg,NULL);
380                                 break;
381                         }
382
383                         case 'i':
384                         {
385                                 pstrcpy(scope, optarg);
386                                 break;
387                         }
388
389                         case 'U':
390                         {
391                                 char *lp;
392                                 pstrcpy(smb_cli->user_name,optarg);
393                                 if ((lp=strchr(smb_cli->user_name,'%')))
394                                 {
395                                         *lp = 0;
396                                         pstrcpy(password,lp+1);
397                                         got_pass = True;
398                                         memset(strchr(optarg,'%')+1,'X',strlen(password));
399                                 }
400                                 break;
401                         }
402
403                         case 'W':
404                         {
405                                 pstrcpy(smb_cli->domain,optarg);
406                                 break;
407                         }
408
409                         case 'E':
410                         {
411                                 dbf = stderr;
412                                 break;
413                         }
414
415                         case 'I':
416                         {
417                                 cli_info.dest_ip = *interpret_addr2(optarg);
418                                 if (zero_ip(cli_info.dest_ip))
419                                 {
420                                         exit(1);
421                                 }
422                                 break;
423                         }
424
425                         case 'N':
426                         {
427                                 nprocs = atoi(optarg);
428                                 break;
429                         }
430
431                         case 'o':
432                         {
433                                 numops = atoi(optarg);
434                                 break;
435                         }
436
437                         case 'n':
438                         {
439                                 fstrcpy(global_myname, optarg);
440                                 break;
441                         }
442
443                         case 'd':
444                         {
445                                 if (*optarg == 'A')
446                                         DEBUGLEVEL = 10000;
447                                 else
448                                         DEBUGLEVEL = atoi(optarg);
449                                 break;
450                         }
451
452                         case 'l':
453                         {
454                                 slprintf(debugf, sizeof(debugf)-1,
455                                          "%s.client",optarg);
456                                 break;
457                         }
458
459                         case 'c':
460                         {
461                                 cmd_str = optarg;
462                                 got_pass = True;
463                                 break;
464                         }
465
466                         case 'h':
467                         {
468                                 usage(pname);
469                                 exit(0);
470                                 break;
471                         }
472
473                         case 's':
474                         {
475                                 pstrcpy(servicesf, optarg);
476                                 break;
477                         }
478
479                         case 't':
480                         {
481                                 pstrcpy(term_code, optarg);
482                                 break;
483                         }
484
485                         default:
486                         {
487                                 usage(pname);
488                                 exit(1);
489                                 break;
490                         }
491                 }
492         }
493
494         if (cli_action == CLIENT_NONE)
495         {
496                 usage(pname);
497                 exit(1);
498         }
499
500         strupper(global_myname);
501         fstrcpy(cli_info.myhostname, global_myname);
502
503         DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
504
505         if (*smb_cli->domain == 0)
506         {
507                 pstrcpy(smb_cli->domain,lp_workgroup());
508         }
509         strupper(smb_cli->domain);
510
511         load_interfaces();
512
513         if (cli_action == CLIENT_IPC)
514         {
515                 pstrcpy(cli_info.share, "IPC$");
516                 pstrcpy(cli_info.svc_type, "IPC");
517         }
518
519         fstrcpy(cli_info.mach_acct, cli_info.myhostname);
520         strupper(cli_info.mach_acct);
521         fstrcat(cli_info.mach_acct, "$");
522
523         /* set the password cache info */
524         if (got_pass)
525         {
526                 if (password[0] == 0)
527                 {
528                         pwd_set_nullpwd(&(smb_cli->pwd));
529                 }
530                 else
531                 {
532                         pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
533                 }
534         }
535         else 
536         {
537                 char *pwd = getpass("Enter Password:");
538                 safe_strcpy(password, pwd, sizeof(password));
539                 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
540         }
541
542         create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test);
543
544         if (password[0] != 0)
545         {
546                 create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test);
547         }
548
549         fflush(out_hnd);
550
551         return(0);
552 }