r7415: * big change -- volker's new async winbindd from trunk
[Samba/gbeck.git] / source / torture / rpctorture.c
blob28067c1a0ae73b3f8f48441cd735016eb256a0f8
1 /*
2 Unix SMB/CIFS implementation.
3 SMB client
4 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
21 #include "includes.h"
23 #ifndef REGISTER
24 #define REGISTER 0
25 #endif
27 extern pstring global_myname;
29 extern pstring user_socket_options;
32 extern file_info def_finfo;
34 #define CNV_LANG(s) dos2unix_format(s,False)
35 #define CNV_INPUT(s) unix2dos_format(s,True)
37 static struct cli_state smbcli;
38 struct cli_state *smb_cli = &smbcli;
40 FILE *out_hnd;
42 static pstring password; /* local copy only, if one is entered */
44 /****************************************************************************
45 initialise smb client structure
46 ****************************************************************************/
47 void rpcclient_init(void)
49 memset((char *)smb_cli, '\0', sizeof(smb_cli));
50 cli_initialise(smb_cli);
51 smb_cli->capabilities |= CAP_NT_SMBS;
54 /****************************************************************************
55 make smb client connection
56 ****************************************************************************/
57 static BOOL rpcclient_connect(struct client_info *info)
59 struct nmb_name calling;
60 struct nmb_name called;
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);
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))
71 DEBUG(0,("rpcclient_connect: connection failed\n"));
72 cli_shutdown(smb_cli);
73 return False;
76 return True;
79 /****************************************************************************
80 stop the smb connection(s?)
81 ****************************************************************************/
82 static void rpcclient_stop(void)
84 cli_shutdown(smb_cli);
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)
92 pstring cmd;
93 int i;
95 /* establish connections. nothing to stop these being re-established. */
96 rpcclient_connect(cli_info);
98 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
99 if (cli->fd <= 0)
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;
106 for (i = 0; i < num_ops; i++)
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);
115 if (password[0] != 0)
117 slprintf(cmd, sizeof(cmd)-1, "1");
118 set_first_token(cmd);
120 else
122 set_first_token("");
124 cmd_srv_enum_conn(cli_info);
127 rpcclient_stop();
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)
136 pstring cmd;
137 int i;
139 /* establish connections. nothing to stop these being re-established. */
140 rpcclient_connect(cli_info);
142 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
143 if (cli->fd <= 0)
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;
150 for (i = 0; i < num_ops; i++)
152 slprintf(cmd, sizeof(cmd)-1, "%s %s", cli->user_name, password);
153 set_first_token(cmd);
155 cmd_netlogon_login_test(cli_info);
158 rpcclient_stop();
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 *))
169 int i, status;
171 for (i=0;i<nprocs;i++)
173 if (fork() == 0)
175 pid_t mypid = getpid();
176 sys_srandom(mypid ^ time(NULL));
177 fn(numops, cli_info, cli);
178 fflush(out_hnd);
179 _exit(0);
183 for (i=0;i<nprocs;i++)
185 waitpid(0, &status, 0);
188 /****************************************************************************
189 usage on the program - OUT OF DATE!
190 ****************************************************************************/
191 static void usage(char *pname)
193 fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
194 pname);
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");
209 enum client_action
211 CLIENT_NONE,
212 CLIENT_IPC,
213 CLIENT_SVC
216 /****************************************************************************
217 main program
218 ****************************************************************************/
219 int main(int argc,char *argv[])
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;
233 struct client_info cli_info;
235 out_hnd = stdout;
237 rpcclient_init();
239 #ifdef KANJI
240 pstrcpy(term_code, KANJI);
241 #else /* KANJI */
242 *term_code = 0;
243 #endif /* KANJI */
245 if (!lp_load(dyn_CONFIGFILE,True, False, False))
247 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
250 DEBUGLEVEL = 0;
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;
257 cli_info.dir_total = 0;
258 cli_info.newer_than = 0;
259 cli_info.archive_level = 0;
260 cli_info.print_mode = 1;
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;
268 cli_info.dest_ip.s_addr = 0;
269 cli_info.name_type = 0x20;
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, "");
279 pstrcpy(cli_info.svc_type, "A:");
280 pstrcpy(cli_info.share, "");
281 pstrcpy(cli_info.service, "");
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, "");
289 int i;
290 for (i=0; i<PI_MAX_PIPES; i++)
291 smb_cli->pipes[i].fnum = 0xffff;
294 setup_logging(pname, True);
296 if (!get_myname(global_myname))
298 fprintf(stderr, "Failed to get my hostname.\n");
301 password[0] = 0;
303 if (argc < 2)
305 usage(pname);
306 exit(1);
309 if (*argv[1] != '-')
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++;
317 DEBUG(1,("service: %s\n", cli_info.service));
319 if (count_chars(cli_info.service,'\\') < 3)
321 usage(pname);
322 printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
323 exit(1);
327 if (count_chars(cli_info.service,'\\') > 3)
329 usage(pname);
330 printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
331 exit(1);
335 if (argc > 1 && (*argv[1] != '-'))
337 got_pass = True;
338 pstrcpy(password,argv[1]);
339 memset(argv[1],'X',strlen(argv[1]));
340 argc--;
341 argv++;
344 cli_action = CLIENT_SVC;
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)
349 switch (opt)
351 case 'm':
353 /* FIXME ... max_protocol seems to be funny here */
355 int max_protocol = 0;
356 max_protocol = interpret_protocol(optarg,max_protocol);
357 fprintf(stderr, "max protocol not currently supported\n");
358 break;
361 case 'O':
363 pstrcpy(user_socket_options,optarg);
364 break;
367 case 'S':
369 pstrcpy(cli_info.dest_host,optarg);
370 strupper_m(cli_info.dest_host);
371 cli_action = CLIENT_IPC;
372 break;
375 case 'i':
377 pstrcpy(scope, optarg);
378 break;
381 case 'U':
383 char *lp;
384 pstrcpy(smb_cli->user_name,optarg);
385 if ((lp=strchr_m(smb_cli->user_name,'%')))
387 *lp = 0;
388 pstrcpy(password,lp+1);
389 got_pass = True;
390 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
392 break;
395 case 'W':
397 pstrcpy(smb_cli->domain,optarg);
398 break;
401 case 'E':
403 dbf = x_stderr;
404 break;
407 case 'I':
409 cli_info.dest_ip = *interpret_addr2(optarg);
410 if (is_zero_ip(cli_info.dest_ip))
412 exit(1);
414 break;
417 case 'N':
419 nprocs = atoi(optarg);
420 break;
423 case 'o':
425 numops = atoi(optarg);
426 break;
429 case 'n':
431 fstrcpy(global_myname, optarg);
432 break;
435 case 'd':
437 if (*optarg == 'A')
438 DEBUGLEVEL = 10000;
439 else
440 DEBUGLEVEL = atoi(optarg);
441 break;
444 case 'l':
446 slprintf(logfile, sizeof(logfile)-1,
447 "%s.client",optarg);
448 lp_set_logfile(logfile);
449 break;
452 case 'c':
454 cmd_str = optarg;
455 got_pass = True;
456 break;
459 case 'h':
461 usage(pname);
462 exit(0);
463 break;
466 case 's':
468 pstrcpy(dyn_CONFIGFILE, optarg);
469 break;
472 case 't':
474 pstrcpy(term_code, optarg);
475 break;
478 default:
480 usage(pname);
481 exit(1);
482 break;
487 if (cli_action == CLIENT_NONE)
489 usage(pname);
490 exit(1);
493 strupper_m(global_myname);
494 fstrcpy(cli_info.myhostname, global_myname);
496 DEBUG(3,("%s client started (version %s)\n",timestring(False),SAMBA_VERSION_STRING));
498 if (*smb_cli->domain == 0)
500 pstrcpy(smb_cli->domain,lp_workgroup());
502 strupper_m(smb_cli->domain);
504 load_interfaces();
506 if (cli_action == CLIENT_IPC)
508 pstrcpy(cli_info.share, "IPC$");
509 pstrcpy(cli_info.svc_type, "IPC");
512 fstrcpy(cli_info.mach_acct, cli_info.myhostname);
513 strupper_m(cli_info.mach_acct);
514 fstrcat(cli_info.mach_acct, "$");
516 /* set the password cache info */
517 if (got_pass)
519 if (password[0] == 0)
521 pwd_set_nullpwd(&(smb_cli->pwd));
523 else
525 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
528 else
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 */
535 create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test);
537 if (password[0] != 0)
539 create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test);
542 fflush(out_hnd);
544 return(0);