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