Remove the silly "user_socket_options" global variable
[Samba.git] / source3 / torture / rpctorture.c
bloba9748ba9059b9a5d737e8392aa9455df6bcef189
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 3 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, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #ifndef REGISTER
23 #define REGISTER 0
24 #endif
26 extern pstring global_myname;
28 extern file_info def_finfo;
30 #define CNV_LANG(s) dos2unix_format(s,False)
31 #define CNV_INPUT(s) unix2dos_format(s,True)
33 static struct cli_state smbcli;
34 struct cli_state *smb_cli = &smbcli;
36 FILE *out_hnd;
38 static pstring password; /* local copy only, if one is entered */
40 /****************************************************************************
41 initialise smb client structure
42 ****************************************************************************/
43 void rpcclient_init(void)
45 memset((char *)smb_cli, '\0', sizeof(smb_cli));
46 cli_initialise(smb_cli);
47 smb_cli->capabilities |= CAP_NT_SMBS;
50 /****************************************************************************
51 make smb client connection
52 ****************************************************************************/
53 static bool rpcclient_connect(struct client_info *info)
55 struct nmb_name calling;
56 struct nmb_name called;
58 make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type);
59 make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0);
61 if (!cli_establish_connection(smb_cli,
62 info->dest_host, &info->dest_ip,
63 &calling, &called,
64 info->share, info->svc_type,
65 False, True))
67 DEBUG(0,("rpcclient_connect: connection failed\n"));
68 cli_shutdown(smb_cli);
69 return False;
72 return True;
75 /****************************************************************************
76 stop the smb connection(s?)
77 ****************************************************************************/
78 static void rpcclient_stop(void)
80 cli_shutdown(smb_cli);
83 /****************************************************************************
84 log in as an nt user, log out again.
85 ****************************************************************************/
86 void run_enums_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
88 pstring cmd;
89 int i;
91 /* establish connections. nothing to stop these being re-established. */
92 rpcclient_connect(cli_info);
94 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
95 if (cli->fd <= 0)
97 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
98 cli_info->dest_host, cli_info->name_type);
99 return;
102 for (i = 0; i < num_ops; i++)
104 set_first_token("");
105 cmd_srv_enum_sess(cli_info);
106 set_first_token("");
107 cmd_srv_enum_shares(cli_info);
108 set_first_token("");
109 cmd_srv_enum_files(cli_info);
111 if (password[0] != 0)
113 slprintf(cmd, sizeof(cmd)-1, "1");
114 set_first_token(cmd);
116 else
118 set_first_token("");
120 cmd_srv_enum_conn(cli_info);
123 rpcclient_stop();
127 /****************************************************************************
128 log in as an nt user, log out again.
129 ****************************************************************************/
130 void run_ntlogin_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
132 pstring cmd;
133 int i;
135 /* establish connections. nothing to stop these being re-established. */
136 rpcclient_connect(cli_info);
138 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
139 if (cli->fd <= 0)
141 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
142 cli_info->dest_host, cli_info->name_type);
143 return;
146 for (i = 0; i < num_ops; i++)
148 slprintf(cmd, sizeof(cmd)-1, "%s %s", cli->user_name, password);
149 set_first_token(cmd);
151 cmd_netlogon_login_test(cli_info);
154 rpcclient_stop();
158 /****************************************************************************
159 runs n simultaneous functions.
160 ****************************************************************************/
161 static void create_procs(int nprocs, int numops,
162 struct client_info *cli_info, struct cli_state *cli,
163 void (*fn)(int, struct client_info *, struct cli_state *))
165 int i, status;
167 for (i=0;i<nprocs;i++)
169 if (fork() == 0)
171 pid_t mypid = getpid();
172 sys_srandom(mypid ^ time(NULL));
173 fn(numops, cli_info, cli);
174 fflush(out_hnd);
175 _exit(0);
179 for (i=0;i<nprocs;i++)
181 waitpid(0, &status, 0);
184 /****************************************************************************
185 usage on the program - OUT OF DATE!
186 ****************************************************************************/
187 static void usage(char *pname)
189 fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
190 pname);
192 fprintf(out_hnd, "\nVersion %s\n",SAMBA_VERSION_STRING);
193 fprintf(out_hnd, "\t-d debuglevel set the debuglevel\n");
194 fprintf(out_hnd, "\t-l log basename. Basename for log/debug files\n");
195 fprintf(out_hnd, "\t-n netbios name. Use this name as my netbios name\n");
196 fprintf(out_hnd, "\t-m max protocol set the max protocol level\n");
197 fprintf(out_hnd, "\t-I dest IP use this IP to connect to\n");
198 fprintf(out_hnd, "\t-E write messages to stderr instead of stdout\n");
199 fprintf(out_hnd, "\t-U username set the network username\n");
200 fprintf(out_hnd, "\t-W workgroup set the workgroup name\n");
201 fprintf(out_hnd, "\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
202 fprintf(out_hnd, "\n");
205 enum client_action
207 CLIENT_NONE,
208 CLIENT_IPC,
209 CLIENT_SVC
212 /****************************************************************************
213 main program
214 ****************************************************************************/
215 int main(int argc,char *argv[])
217 char *pname = argv[0];
218 int opt;
219 extern char *optarg;
220 extern int optind;
221 pstring term_code;
222 bool got_pass = False;
223 char *cmd_str="";
224 enum client_action cli_action = CLIENT_NONE;
225 int nprocs = 1;
226 int numops = 100;
227 pstring logfile;
228 TALLOC_CTX *frame = talloc_stackframe();
230 struct client_info cli_info;
232 out_hnd = stdout;
234 rpcclient_init();
236 #ifdef KANJI
237 pstrcpy(term_code, KANJI);
238 #else /* KANJI */
239 *term_code = 0;
240 #endif /* KANJI */
242 if (!lp_load(dyn_CONFIGFILE,True, False, False, True))
244 fprintf(stderr, "Can't load %s - run testparm to debug it\n", dyn_CONFIGFILE);
247 DEBUGLEVEL = 0;
249 cli_info.put_total_size = 0;
250 cli_info.put_total_time_ms = 0;
251 cli_info.get_total_size = 0;
252 cli_info.get_total_time_ms = 0;
254 cli_info.dir_total = 0;
255 cli_info.newer_than = 0;
256 cli_info.archive_level = 0;
257 cli_info.print_mode = 1;
259 cli_info.translation = False;
260 cli_info.recurse_dir = False;
261 cli_info.lowercase = False;
262 cli_info.prompt = True;
263 cli_info.abort_mget = True;
265 cli_info.dest_ip.s_addr = 0;
266 cli_info.name_type = 0x20;
268 pstrcpy(cli_info.cur_dir , "\\");
269 pstrcpy(cli_info.file_sel, "");
270 pstrcpy(cli_info.base_dir, "");
271 pstrcpy(smb_cli->domain, "");
272 pstrcpy(smb_cli->user_name, "");
273 pstrcpy(cli_info.myhostname, "");
274 pstrcpy(cli_info.dest_host, "");
276 pstrcpy(cli_info.svc_type, "A:");
277 pstrcpy(cli_info.share, "");
278 pstrcpy(cli_info.service, "");
280 ZERO_STRUCT(cli_info.dom.level3_sid);
281 pstrcpy(cli_info.dom.level3_dom, "");
282 ZERO_STRUCT(cli_info.dom.level5_sid);
283 pstrcpy(cli_info.dom.level5_dom, "");
286 int i;
287 for (i=0; i<PI_MAX_PIPES; i++)
288 smb_cli->pipes[i].fnum = 0xffff;
291 setup_logging(pname, True);
293 global_myname = get_myname(global_myname);
294 if (!global_myname) {
295 fprintf(stderr, "Failed to get my hostname.\n");
298 password[0] = 0;
300 if (argc < 2)
302 usage(pname);
303 exit(1);
306 if (*argv[1] != '-')
308 pstrcpy(cli_info.service, argv[1]);
309 /* Convert any '/' characters in the service name to '\' characters */
310 string_replace( cli_info.service, '/','\\');
311 argc--;
312 argv++;
314 DEBUG(1,("service: %s\n", cli_info.service));
316 if (count_chars(cli_info.service,'\\') < 3)
318 usage(pname);
319 printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
320 exit(1);
324 if (count_chars(cli_info.service,'\\') > 3)
326 usage(pname);
327 printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
328 exit(1);
332 if (argc > 1 && (*argv[1] != '-'))
334 got_pass = True;
335 pstrcpy(password,argv[1]);
336 memset(argv[1],'X',strlen(argv[1]));
337 argc--;
338 argv++;
341 cli_action = CLIENT_SVC;
344 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)
346 switch (opt)
348 case 'm':
350 /* FIXME ... max_protocol seems to be funny here */
352 int max_protocol = 0;
353 max_protocol = interpret_protocol(optarg,max_protocol);
354 fprintf(stderr, "max protocol not currently supported\n");
355 break;
358 case 'O':
360 lp_do_parameter(-1, "socket options", optarg);
361 break;
364 case 'S':
366 pstrcpy(cli_info.dest_host,optarg);
367 strupper_m(cli_info.dest_host);
368 cli_action = CLIENT_IPC;
369 break;
372 case 'i':
374 pstrcpy(scope, optarg);
375 break;
378 case 'U':
380 char *lp;
381 pstrcpy(smb_cli->user_name,optarg);
382 if ((lp=strchr_m(smb_cli->user_name,'%')))
384 *lp = 0;
385 pstrcpy(password,lp+1);
386 got_pass = True;
387 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
389 break;
392 case 'W':
394 pstrcpy(smb_cli->domain,optarg);
395 break;
398 case 'E':
400 dbf = x_stderr;
401 break;
404 case 'I':
406 (void)interpret_addr2(&cli_info.dest_ip, optarg);
407 if (is_zero_ip(cli_info.dest_ip))
409 exit(1);
411 break;
414 case 'N':
416 nprocs = atoi(optarg);
417 break;
420 case 'o':
422 numops = atoi(optarg);
423 break;
426 case 'n':
428 fstrcpy(global_myname, optarg);
429 break;
432 case 'd':
434 if (*optarg == 'A')
435 DEBUGLEVEL = 10000;
436 else
437 DEBUGLEVEL = atoi(optarg);
438 break;
441 case 'l':
443 slprintf(logfile, sizeof(logfile)-1,
444 "%s.client",optarg);
445 lp_set_logfile(logfile);
446 break;
449 case 'c':
451 cmd_str = optarg;
452 got_pass = True;
453 break;
456 case 'h':
458 usage(pname);
459 exit(0);
460 break;
463 case 's':
465 pstrcpy(dyn_CONFIGFILE, optarg);
466 break;
469 case 't':
471 pstrcpy(term_code, optarg);
472 break;
475 default:
477 usage(pname);
478 exit(1);
479 break;
484 if (cli_action == CLIENT_NONE)
486 usage(pname);
487 exit(1);
490 strupper_m(global_myname);
491 fstrcpy(cli_info.myhostname, global_myname);
493 DEBUG(3,("%s client started (version %s)\n",current_timestring(False),SAMBA_VERSION_STRING));
495 if (*smb_cli->domain == 0)
497 pstrcpy(smb_cli->domain,lp_workgroup());
499 strupper_m(smb_cli->domain);
501 load_interfaces();
503 if (cli_action == CLIENT_IPC)
505 pstrcpy(cli_info.share, "IPC$");
506 pstrcpy(cli_info.svc_type, "IPC");
509 fstrcpy(cli_info.mach_acct, cli_info.myhostname);
510 strupper_m(cli_info.mach_acct);
511 fstrcat(cli_info.mach_acct, "$");
513 /* set the password cache info */
514 if (got_pass)
516 if (password[0] == 0)
518 pwd_set_nullpwd(&(smb_cli->pwd));
520 else
522 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
525 else
527 char *pwd = getpass("Enter Password:");
528 safe_strcpy(password, pwd, sizeof(password));
529 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
532 create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test);
534 if (password[0] != 0)
536 create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test);
539 fflush(out_hnd);
541 return(0);