fixed error check which caused domain logons to fail
[Samba.git] / source / utils / rpctorture.c
blob76461072b6d70b1738810e376490a4045736d0cb
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 SMB client
5 Copyright (C) Andrew Tridgell 1994-1998
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.
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.
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.
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
26 #include "includes.h"
28 #ifndef REGISTER
29 #define REGISTER 0
30 #endif
32 extern pstring global_myname;
34 extern pstring user_socket_options;
37 extern pstring debugf;
40 extern file_info def_finfo;
42 #define CNV_LANG(s) dos2unix_format(s,False)
43 #define CNV_INPUT(s) unix2dos_format(s,True)
45 static struct cli_state smbcli;
46 struct cli_state *smb_cli = &smbcli;
48 FILE *out_hnd;
50 static pstring password; /* local copy only, if one is entered */
52 /****************************************************************************
53 initialise smb client structure
54 ****************************************************************************/
55 void rpcclient_init(void)
57 memset((char *)smb_cli, '\0', sizeof(smb_cli));
58 cli_initialise(smb_cli);
59 smb_cli->capabilities |= CAP_NT_SMBS;
62 /****************************************************************************
63 make smb client connection
64 ****************************************************************************/
65 static BOOL rpcclient_connect(struct client_info *info)
67 struct nmb_name calling;
68 struct nmb_name called;
70 make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type);
71 make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0);
73 if (!cli_establish_connection(smb_cli,
74 info->dest_host, &info->dest_ip,
75 &calling, &called,
76 info->share, info->svc_type,
77 False, True))
79 DEBUG(0,("rpcclient_connect: connection failed\n"));
80 cli_shutdown(smb_cli);
81 return False;
84 return True;
87 /****************************************************************************
88 stop the smb connection(s?)
89 ****************************************************************************/
90 static void rpcclient_stop(void)
92 cli_shutdown(smb_cli);
95 /****************************************************************************
96 log in as an nt user, log out again.
97 ****************************************************************************/
98 void run_enums_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
100 pstring cmd;
101 int i;
103 /* establish connections. nothing to stop these being re-established. */
104 rpcclient_connect(cli_info);
106 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
107 if (cli->fd <= 0)
109 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
110 cli_info->dest_host, cli_info->name_type);
111 return;
114 for (i = 0; i < num_ops; i++)
116 set_first_token("");
117 cmd_srv_enum_sess(cli_info);
118 set_first_token("");
119 cmd_srv_enum_shares(cli_info);
120 set_first_token("");
121 cmd_srv_enum_files(cli_info);
123 if (password[0] != 0)
125 slprintf(cmd, sizeof(cmd)-1, "1");
126 set_first_token(cmd);
128 else
130 set_first_token("");
132 cmd_srv_enum_conn(cli_info);
135 rpcclient_stop();
139 /****************************************************************************
140 log in as an nt user, log out again.
141 ****************************************************************************/
142 void run_ntlogin_test(int num_ops, struct client_info *cli_info, struct cli_state *cli)
144 pstring cmd;
145 int i;
147 /* establish connections. nothing to stop these being re-established. */
148 rpcclient_connect(cli_info);
150 DEBUG(5,("rpcclient_connect: cli->fd:%d\n", cli->fd));
151 if (cli->fd <= 0)
153 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
154 cli_info->dest_host, cli_info->name_type);
155 return;
158 for (i = 0; i < num_ops; i++)
160 slprintf(cmd, sizeof(cmd)-1, "%s %s", cli->user_name, password);
161 set_first_token(cmd);
163 cmd_netlogon_login_test(cli_info);
166 rpcclient_stop();
170 /****************************************************************************
171 runs n simultaneous functions.
172 ****************************************************************************/
173 static void create_procs(int nprocs, int numops,
174 struct client_info *cli_info, struct cli_state *cli,
175 void (*fn)(int, struct client_info *, struct cli_state *))
177 int i, status;
179 for (i=0;i<nprocs;i++)
181 if (fork() == 0)
183 pid_t mypid = getpid();
184 sys_srandom(mypid ^ time(NULL));
185 fn(numops, cli_info, cli);
186 fflush(out_hnd);
187 _exit(0);
191 for (i=0;i<nprocs;i++)
193 waitpid(0, &status, 0);
196 /****************************************************************************
197 usage on the program - OUT OF DATE!
198 ****************************************************************************/
199 static void usage(char *pname)
201 fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
202 pname);
204 fprintf(out_hnd, "\nVersion %s\n",VERSION);
205 fprintf(out_hnd, "\t-d debuglevel set the debuglevel\n");
206 fprintf(out_hnd, "\t-l log basename. Basename for log/debug files\n");
207 fprintf(out_hnd, "\t-n netbios name. Use this name as my netbios name\n");
208 fprintf(out_hnd, "\t-m max protocol set the max protocol level\n");
209 fprintf(out_hnd, "\t-I dest IP use this IP to connect to\n");
210 fprintf(out_hnd, "\t-E write messages to stderr instead of stdout\n");
211 fprintf(out_hnd, "\t-U username set the network username\n");
212 fprintf(out_hnd, "\t-W workgroup set the workgroup name\n");
213 fprintf(out_hnd, "\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
214 fprintf(out_hnd, "\n");
217 enum client_action
219 CLIENT_NONE,
220 CLIENT_IPC,
221 CLIENT_SVC
224 /****************************************************************************
225 main program
226 ****************************************************************************/
227 int main(int argc,char *argv[])
229 char *pname = argv[0];
230 int opt;
231 extern FILE *dbf;
232 extern char *optarg;
233 extern int optind;
234 static pstring servicesf = CONFIGFILE;
235 pstring term_code;
236 BOOL got_pass = False;
237 char *cmd_str="";
238 mode_t myumask = 0755;
239 enum client_action cli_action = CLIENT_NONE;
240 int nprocs = 1;
241 int numops = 100;
243 struct client_info cli_info;
245 out_hnd = stdout;
247 rpcclient_init();
249 #ifdef KANJI
250 pstrcpy(term_code, KANJI);
251 #else /* KANJI */
252 *term_code = 0;
253 #endif /* KANJI */
255 if (!lp_load(servicesf,True, False, False))
257 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
260 codepage_initialise(lp_client_code_page());
262 DEBUGLEVEL = 0;
264 cli_info.put_total_size = 0;
265 cli_info.put_total_time_ms = 0;
266 cli_info.get_total_size = 0;
267 cli_info.get_total_time_ms = 0;
269 cli_info.dir_total = 0;
270 cli_info.newer_than = 0;
271 cli_info.archive_level = 0;
272 cli_info.print_mode = 1;
274 cli_info.translation = False;
275 cli_info.recurse_dir = False;
276 cli_info.lowercase = False;
277 cli_info.prompt = True;
278 cli_info.abort_mget = True;
280 cli_info.dest_ip.s_addr = 0;
281 cli_info.name_type = 0x20;
283 pstrcpy(cli_info.cur_dir , "\\");
284 pstrcpy(cli_info.file_sel, "");
285 pstrcpy(cli_info.base_dir, "");
286 pstrcpy(smb_cli->domain, "");
287 pstrcpy(smb_cli->user_name, "");
288 pstrcpy(cli_info.myhostname, "");
289 pstrcpy(cli_info.dest_host, "");
291 pstrcpy(cli_info.svc_type, "A:");
292 pstrcpy(cli_info.share, "");
293 pstrcpy(cli_info.service, "");
295 ZERO_STRUCT(cli_info.dom.level3_sid);
296 pstrcpy(cli_info.dom.level3_dom, "");
297 ZERO_STRUCT(cli_info.dom.level5_sid);
298 pstrcpy(cli_info.dom.level5_dom, "");
300 smb_cli->nt_pipe_fnum = 0xffff;
302 setup_logging(pname, True);
304 TimeInit();
305 charset_initialise();
307 myumask = umask(0);
308 umask(myumask);
310 if (!get_myname(global_myname))
312 fprintf(stderr, "Failed to get my hostname.\n");
315 password[0] = 0;
317 if (argc < 2)
319 usage(pname);
320 exit(1);
323 if (*argv[1] != '-')
325 pstrcpy(cli_info.service, argv[1]);
326 /* Convert any '/' characters in the service name to '\' characters */
327 string_replace( cli_info.service, '/','\\');
328 argc--;
329 argv++;
331 DEBUG(1,("service: %s\n", cli_info.service));
333 if (count_chars(cli_info.service,'\\') < 3)
335 usage(pname);
336 printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
337 exit(1);
341 if (count_chars(cli_info.service,'\\') > 3)
343 usage(pname);
344 printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
345 exit(1);
349 if (argc > 1 && (*argv[1] != '-'))
351 got_pass = True;
352 pstrcpy(password,argv[1]);
353 memset(argv[1],'X',strlen(argv[1]));
354 argc--;
355 argv++;
358 cli_action = CLIENT_SVC;
361 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)
363 switch (opt)
365 case 'm':
367 /* FIXME ... max_protocol seems to be funny here */
369 int max_protocol = 0;
370 max_protocol = interpret_protocol(optarg,max_protocol);
371 fprintf(stderr, "max protocol not currently supported\n");
372 break;
375 case 'O':
377 pstrcpy(user_socket_options,optarg);
378 break;
381 case 'S':
383 pstrcpy(cli_info.dest_host,optarg);
384 strupper(cli_info.dest_host);
385 cli_action = CLIENT_IPC;
386 break;
389 case 'i':
391 pstrcpy(scope, optarg);
392 break;
395 case 'U':
397 char *lp;
398 pstrcpy(smb_cli->user_name,optarg);
399 if ((lp=strchr(smb_cli->user_name,'%')))
401 *lp = 0;
402 pstrcpy(password,lp+1);
403 got_pass = True;
404 memset(strchr(optarg,'%')+1,'X',strlen(password));
406 break;
409 case 'W':
411 pstrcpy(smb_cli->domain,optarg);
412 break;
415 case 'E':
417 dbf = stderr;
418 break;
421 case 'I':
423 cli_info.dest_ip = *interpret_addr2(optarg);
424 if (zero_ip(cli_info.dest_ip))
426 exit(1);
428 break;
431 case 'N':
433 nprocs = atoi(optarg);
434 break;
437 case 'o':
439 numops = atoi(optarg);
440 break;
443 case 'n':
445 fstrcpy(global_myname, optarg);
446 break;
449 case 'd':
451 if (*optarg == 'A')
452 DEBUGLEVEL = 10000;
453 else
454 DEBUGLEVEL = atoi(optarg);
455 break;
458 case 'l':
460 slprintf(debugf, sizeof(debugf)-1,
461 "%s.client",optarg);
462 break;
465 case 'c':
467 cmd_str = optarg;
468 got_pass = True;
469 break;
472 case 'h':
474 usage(pname);
475 exit(0);
476 break;
479 case 's':
481 pstrcpy(servicesf, optarg);
482 break;
485 case 't':
487 pstrcpy(term_code, optarg);
488 break;
491 default:
493 usage(pname);
494 exit(1);
495 break;
500 if (cli_action == CLIENT_NONE)
502 usage(pname);
503 exit(1);
506 strupper(global_myname);
507 fstrcpy(cli_info.myhostname, global_myname);
509 DEBUG(3,("%s client started (version %s)\n",timestring(False),VERSION));
511 if (*smb_cli->domain == 0)
513 pstrcpy(smb_cli->domain,lp_workgroup());
515 strupper(smb_cli->domain);
517 load_interfaces();
519 if (cli_action == CLIENT_IPC)
521 pstrcpy(cli_info.share, "IPC$");
522 pstrcpy(cli_info.svc_type, "IPC");
525 fstrcpy(cli_info.mach_acct, cli_info.myhostname);
526 strupper(cli_info.mach_acct);
527 fstrcat(cli_info.mach_acct, "$");
529 /* set the password cache info */
530 if (got_pass)
532 if (password[0] == 0)
534 pwd_set_nullpwd(&(smb_cli->pwd));
536 else
538 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
541 else
543 char *pwd = getpass("Enter Password:");
544 safe_strcpy(password, pwd, sizeof(password));
545 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
548 create_procs(nprocs, numops, &cli_info, smb_cli, run_enums_test);
550 if (password[0] != 0)
552 create_procs(nprocs, numops, &cli_info, smb_cli, run_ntlogin_test);
555 fflush(out_hnd);
557 return(0);