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