Initial import
[samba] / 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         {
289                 int i;
290                 for (i=0; i<PI_MAX_PIPES; i++)
291                         smb_cli->pipes[i].fnum   = 0xffff;
292         }
293
294         setup_logging(pname, True);
295
296         if (!get_myname(global_myname))
297         {
298                 fprintf(stderr, "Failed to get my hostname.\n");
299         }
300
301         password[0] = 0;
302
303         if (argc < 2)
304         {
305                 usage(pname);
306                 exit(1);
307         }
308
309         if (*argv[1] != '-')
310         {
311                 pstrcpy(cli_info.service, argv[1]);  
312                 /* Convert any '/' characters in the service name to '\' characters */
313                 string_replace( cli_info.service, '/','\\');
314                 argc--;
315                 argv++;
316
317                 DEBUG(1,("service: %s\n", cli_info.service));
318
319                 if (count_chars(cli_info.service,'\\') < 3)
320                 {
321                         usage(pname);
322                         printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
323                         exit(1);
324                 }
325
326                 /*
327                 if (count_chars(cli_info.service,'\\') > 3)
328                 {
329                         usage(pname);
330                         printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
331                         exit(1);
332                 }
333                 */
334
335                 if (argc > 1 && (*argv[1] != '-'))
336                 {
337                         got_pass = True;
338                         pstrcpy(password,argv[1]);  
339                         memset(argv[1],'X',strlen(argv[1]));
340                         argc--;
341                         argv++;
342                 }
343
344                 cli_action = CLIENT_SVC;
345         }
346
347         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)
348         {
349                 switch (opt)
350                 {
351                         case 'm':
352                         {
353                                 /* FIXME ... max_protocol seems to be funny here */
354
355                                 int max_protocol = 0;
356                                 max_protocol = interpret_protocol(optarg,max_protocol);
357                                 fprintf(stderr, "max protocol not currently supported\n");
358                                 break;
359                         }
360
361                         case 'O':
362                         {
363                                 pstrcpy(user_socket_options,optarg);
364                                 break;  
365                         }
366
367                         case 'S':
368                         {
369                                 pstrcpy(cli_info.dest_host,optarg);
370                                 strupper_m(cli_info.dest_host);
371                                 cli_action = CLIENT_IPC;
372                                 break;
373                         }
374
375                         case 'i':
376                         {
377                                 pstrcpy(scope, optarg);
378                                 break;
379                         }
380
381                         case 'U':
382                         {
383                                 char *lp;
384                                 pstrcpy(smb_cli->user_name,optarg);
385                                 if ((lp=strchr_m(smb_cli->user_name,'%')))
386                                 {
387                                         *lp = 0;
388                                         pstrcpy(password,lp+1);
389                                         got_pass = True;
390                                         memset(strchr_m(optarg,'%')+1,'X',strlen(password));
391                                 }
392                                 break;
393                         }
394
395                         case 'W':
396                         {
397                                 pstrcpy(smb_cli->domain,optarg);
398                                 break;
399                         }
400
401                         case 'E':
402                         {
403                                 dbf = x_stderr;
404                                 break;
405                         }
406
407                         case 'I':
408                         {
409                                 cli_info.dest_ip = *interpret_addr2(optarg);
410                                 if (is_zero_ip(cli_info.dest_ip))
411                                 {
412                                         exit(1);
413                                 }
414                                 break;
415                         }
416
417                         case 'N':
418                         {
419                                 nprocs = atoi(optarg);
420                                 break;
421                         }
422
423                         case 'o':
424                         {
425                                 numops = atoi(optarg);
426                                 break;
427                         }
428
429                         case 'n':
430                         {
431                                 fstrcpy(global_myname, optarg);
432                                 break;
433                         }
434
435                         case 'd':
436                         {
437                                 if (*optarg == 'A')
438                                         DEBUGLEVEL = 10000;
439                                 else
440                                         DEBUGLEVEL = atoi(optarg);
441                                 break;
442                         }
443
444                         case 'l':
445                         {
446                                 slprintf(logfile, sizeof(logfile)-1,
447                                          "%s.client",optarg);
448                                 lp_set_logfile(logfile);
449                                 break;
450                         }
451
452                         case 'c':
453                         {
454                                 cmd_str = optarg;
455                                 got_pass = True;
456                                 break;
457                         }
458
459                         case 'h':
460                         {
461                                 usage(pname);
462                                 exit(0);
463                                 break;
464                         }
465
466                         case 's':
467                         {
468                                 pstrcpy(dyn_CONFIGFILE, optarg);
469                                 break;
470                         }
471
472                         case 't':
473                         {
474                                 pstrcpy(term_code, optarg);
475                                 break;
476                         }
477
478                         default:
479                         {
480                                 usage(pname);
481                                 exit(1);
482                                 break;
483                         }
484                 }
485         }
486
487         if (cli_action == CLIENT_NONE)
488         {
489                 usage(pname);
490                 exit(1);
491         }
492
493         strupper_m(global_myname);
494         fstrcpy(cli_info.myhostname, global_myname);
495
496         DEBUG(3,("%s client started (version %s)\n",timestring(False),SAMBA_VERSION_STRING));
497
498         if (*smb_cli->domain == 0)
499         {
500                 pstrcpy(smb_cli->domain,lp_workgroup());
501         }
502         strupper_m(smb_cli->domain);
503
504         load_interfaces();
505
506         if (cli_action == CLIENT_IPC)
507         {
508                 pstrcpy(cli_info.share, "IPC$");
509                 pstrcpy(cli_info.svc_type, "IPC");
510         }
511
512         fstrcpy(cli_info.mach_acct, cli_info.myhostname);
513         strupper_m(cli_info.mach_acct);
514         fstrcat(cli_info.mach_acct, "$");
515
516         /* set the password cache info */
517         if (got_pass)
518         {
519                 if (password[0] == 0)
520                 {
521                         pwd_set_nullpwd(&(smb_cli->pwd));
522                 }
523                 else
524                 {
525                         pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
526                 }
527         }
528         else 
529         {
530                 char *pwd = getpass("Enter Password:");
531                 safe_strcpy(password, pwd, sizeof(password));
532                 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
533         }
534
535         create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test);
536
537         if (password[0] != 0)
538         {
539                 create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test);
540         }
541
542         fflush(out_hnd);
543
544         return(0);
545 }