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