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