2 Unix SMB/Netbios implementation.
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.
33 extern pstring global_myname;
35 extern pstring user_socket_options;
38 extern pstring debugf;
39 extern int DEBUGLEVEL;
42 extern file_info def_finfo;
44 #define CNV_LANG(s) dos2unix_format(s,False)
45 #define CNV_INPUT(s) unix2dos_format(s,True)
47 static struct cli_state smbcli;
48 struct cli_state *smb_cli = &smbcli;
52 static pstring user_name; /* local copy only, if one is entered */
53 static pstring password; /* local copy only, if one is entered */
54 static pstring domain; /* local copy only, if one is entered */
55 BOOL got_pass = False;
57 static struct nmb_name calling;
58 static struct nmb_name called;
60 static void get_passwd(void)
62 /* set the password cache info */
67 pwd_set_nullpwd(&(smb_cli->pwd));
71 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
76 char *pwd = getpass("Enter Password:");
77 safe_strcpy(password, pwd, sizeof(password));
78 pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
83 /****************************************************************************
84 initialise smb client structure
85 ****************************************************************************/
86 void rpcclient_init(void)
88 bzero(smb_cli, sizeof(smb_cli));
89 cli_initialise(smb_cli);
90 smb_cli->capabilities |= CAP_NT_SMBS;
91 smb_cli->capabilities |= CAP_STATUS32;
93 pstrcpy(smb_cli->user_name, user_name);
94 smb_cli->nt_pipe_fnum = 0xffff;
100 pstrcpy(smb_cli->domain,lp_workgroup());
104 pstrcpy(smb_cli->domain, domain);
107 strupper(smb_cli->domain);
110 /****************************************************************************
111 make smb client connection
112 ****************************************************************************/
113 static BOOL rpcclient_connect(struct client_info *info)
117 if (!cli_establish_connection(smb_cli,
118 info->dest_host, &info->dest_ip,
120 info->share, info->svc_type,
123 DEBUG(0,("rpcclient_connect: connection failed\n"));
124 cli_shutdown(smb_cli);
131 /****************************************************************************
132 stop the smb connection(s?)
133 ****************************************************************************/
134 static void rpcclient_stop(void)
136 cli_shutdown(smb_cli);
139 /****************************************************************************
140 log in as an nt user, log out again.
141 ****************************************************************************/
142 void run_enums_test(int num_ops, struct client_info *cli_info)
147 /* establish connections. nothing to stop these being re-established. */
148 rpcclient_connect(cli_info);
150 DEBUG(5,("rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd));
151 if (smb_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);
158 for (i = 0; i < num_ops; i++)
161 cmd_srv_enum_sess(cli_info);
163 cmd_srv_enum_shares(cli_info);
165 cmd_srv_enum_files(cli_info);
167 if (password[0] != 0)
169 slprintf(cmd, sizeof(cmd)-1, "1");
170 set_first_token(cmd);
176 cmd_srv_enum_conn(cli_info);
183 /****************************************************************************
184 log in as an nt user, log out again.
185 ****************************************************************************/
186 void run_ntlogin_test(int num_ops, struct client_info *cli_info)
191 /* establish connections. nothing to stop these being re-established. */
192 rpcclient_connect(cli_info);
194 DEBUG(5,("rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd));
195 if (smb_cli->fd <= 0)
197 fprintf(out_hnd, "warning: connection could not be established to %s<%02x>\n",
198 cli_info->dest_host, cli_info->name_type);
202 for (i = 0; i < num_ops; i++)
204 slprintf(cmd, sizeof(cmd)-1, "%s %s", smb_cli->user_name, password);
205 set_first_token(cmd);
207 cmd_netlogon_login_test(cli_info);
214 /* generate a random buffer */
215 static void rand_buf(char *buf, int len)
223 /****************************************************************************
224 do a random rpc command
225 ****************************************************************************/
226 BOOL do_random_rpc(struct cli_state *cli, int max_len)
232 BOOL response = False;
234 if ((sys_random() % 20) == 0)
236 param_len = (sys_random() % 256) + 4;
240 param_len = (sys_random() % max_len) + 4;
243 prs_init(&buf , param_len, 4, SAFETY_MARGIN, False);
244 prs_init(&rbuf, 0 , 4, SAFETY_MARGIN, True );
246 opcode = sys_random() % 256;
248 /* turn parameters into data stream */
249 rand_buf(mem_data(&buf.data, 0), param_len);
250 buf.offset = param_len;
252 /* send the data on \PIPE\ */
253 if (rpc_api_pipe_req(cli, opcode, &buf, &rbuf))
255 response = rbuf.offset != 0;
259 DEBUG(0,("response! opcode: 0x%x\n", opcode));
260 DEBUG(0,("request: length %d\n", param_len));
261 dump_data(0, mem_data(&buf.data , 0), MIN(param_len, 128));
262 DEBUG(0,("response: length %d\n", rbuf.data->offset.end));
263 dump_data(0, mem_data(&rbuf.data, 0), rbuf.data->offset.end);
274 /* send random IPC commands */
275 static void random_rpc_pipe_enc(char *pipe_name, struct client_info *cli_info,
281 DEBUG(0,("starting random rpc test on %s (encryped)\n", pipe_name));
283 /* establish connections. nothing to stop these being re-established. */
284 if (!rpcclient_connect(cli_info))
286 DEBUG(0,("random rpc test: connection failed\n"));
290 cli_nt_set_ntlmssp_flgs(smb_cli,
291 NTLMSSP_NEGOTIATE_UNICODE |
292 NTLMSSP_NEGOTIATE_OEM |
293 NTLMSSP_NEGOTIATE_SIGN |
294 NTLMSSP_NEGOTIATE_SEAL |
295 NTLMSSP_NEGOTIATE_LM_KEY |
296 NTLMSSP_NEGOTIATE_NTLM |
297 NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
298 NTLMSSP_NEGOTIATE_00001000 |
299 NTLMSSP_NEGOTIATE_00002000);
301 for (i = 1; i <= numops * 100; i++)
304 cli_nt_session_open(smb_cli, pipe_name, &nt_pipe_fnum);
306 do_random_rpc(smb_cli, 1024);
309 DEBUG(0,("calls: %i\n", i));
312 /* close the session */
313 cli_nt_session_close(smb_cli, nt_pipe_fnum);
316 /* close the rpc pipe */
319 DEBUG(0,("finished random rpc test on %s\n", pipe_name));
322 /* send random IPC commands */
323 static void random_rpc_pipe(char *pipe_name, struct client_info *cli_info,
329 DEBUG(0,("starting random rpc test on %s\n", pipe_name));
331 /* establish connections. nothing to stop these being re-established. */
332 if (!rpcclient_connect(cli_info))
334 DEBUG(0,("random rpc test: connection failed\n"));
339 if (!cli_nt_session_open(smb_cli, pipe_name, &nt_pipe_fnum))
341 DEBUG(0,("random rpc test: session open failed\n"));
345 for (i = 1; i <= numops * 100; i++)
347 do_random_rpc(smb_cli, 8192);
350 DEBUG(0,("calls: %i\n", i));
354 /* close the session */
355 cli_nt_session_close(smb_cli, nt_pipe_fnum);
357 /* close the rpc pipe */
360 DEBUG(0,("finished random rpc test on %s\n", pipe_name));
363 static void run_randomrpc(int numops, struct client_info *cli_info)
379 while (pipes[i] != NULL)
381 random_rpc_pipe(pipes[i], cli_info, numops);
383 random_rpc_pipe_enc(pipes[i], cli_info, numops);
391 static void run_samhandles(int numops, struct client_info *cli_info)
399 fstrcpy(srv_name, "\\\\");
400 fstrcat(srv_name, cli_info->dest_host);
403 DEBUG(0,("starting sam handle test\n"));
405 /* establish connections. nothing to stop these being re-established. */
406 while (retry > 0 && !rpcclient_connect(cli_info))
413 DEBUG(0,("samhandle test: connection failed\n"));
418 if (!cli_nt_session_open(smb_cli, PIPE_SAMR, &nt_pipe_fnum))
420 DEBUG(0,("samhandle test: session open failed\n"));
424 for (i = 1; i <= numops * 100; i++)
428 if (!do_samr_connect(smb_cli, srv_name, 0x20, &pol))
433 if (!do_samr_open_domain(smb_cli, srv_name, 0x00000020, &pol))
435 DEBUG(0,("samhandle domain open test (%i): failed\n", i));
440 DEBUG(0,("calls: %i\n", i));
445 /* close the session */
446 cli_nt_session_close(smb_cli, nt_pipe_fnum);
448 /* close the rpc pipe */
451 DEBUG(0,("finished samhandle test. count: %d failed: %d\n", count, failed));
455 static void run_lsahandles(int numops, struct client_info *cli_info)
463 fstrcpy(srv_name, "\\\\");
464 fstrcat(srv_name, cli_info->myhostname);
467 DEBUG(0,("starting lsa handle test\n"));
469 /* establish connections. nothing to stop these being re-established. */
470 while (retry > 0 && !rpcclient_connect(cli_info))
477 DEBUG(0,("lsahandle test: connection failed\n"));
481 if (!cli_nt_session_open(smb_cli, PIPE_LSARPC, &nt_pipe_fnum))
483 DEBUG(0,("lsahandle test: session open failed\n"));
487 for (i = 1; i <= numops * 100; i++)
490 if (!do_lsa_open_policy(smb_cli, srv_name, &pol, False))
496 DEBUG(0,("calls: %i\n", i));
501 /* close the session */
502 cli_nt_session_close(smb_cli, nt_pipe_fnum);
504 /* close the rpc pipe */
507 DEBUG(0,("finished lsahandle test. count: %d failed: %d\n", count, failed));
511 static void run_pipegobble(int numops, struct client_info *cli_info, char *pipe_name)
519 fstrcpy(srv_name, "\\\\");
520 fstrcat(srv_name, cli_info->myhostname);
523 DEBUG(0,("starting pipe gobble test (%s)\n", pipe_name));
525 /* establish connections. nothing to stop these being re-established. */
526 while (retry > 0 && !rpcclient_connect(cli_info))
533 DEBUG(0,("pipe gobble test: connection failed\n"));
536 for (i = 1; i <= numops * 100; i++)
539 if (!cli_nt_session_open(smb_cli, pipe_name, &nt_pipe_fnum))
541 DEBUG(0,("pipe gobble test: session open failed\n"));
546 DEBUG(0,("calls: %i\n", i));
553 DEBUG(0,("finished pipe gobble test (%s). count: %d failed: %d\n",
554 pipe_name, count, failed));
558 static void run_handles(int numops, struct client_info *cli_info)
560 run_samhandles(numops, cli_info);
561 run_lsahandles(numops, cli_info);
564 static void run_pipegobbler(int numops, struct client_info *cli_info)
566 run_pipegobble(numops, cli_info, PIPE_SAMR);
567 run_pipegobble(numops, cli_info, PIPE_LSARPC);
570 /****************************************************************************
572 ****************************************************************************/
573 static void run_tcpconnect(int numops, struct client_info *info)
578 for (i = 0; i < numops; i++)
582 if (!cli_connect(smb_cli, info->dest_host, &info->dest_ip))
586 cli_shutdown(smb_cli);
589 DEBUG(0,("tcp connections: count: %d failed: %d\n", numops, failed));
592 /****************************************************************************
593 runs n simultaneous functions.
594 ****************************************************************************/
595 static void create_procs(int nprocs, int numops,
596 struct client_info *cli_info,
597 void (*fn)(int, struct client_info *))
601 for (i=0;i<nprocs;i++)
605 int mypid = getpid();
606 sys_srandom(mypid ^ time(NULL));
607 fn(numops, cli_info);
613 for (i=0;i<nprocs;i++)
615 waitpid(0, &status, 0);
620 /****************************************************************************
621 usage on the program - OUT OF DATE!
622 ****************************************************************************/
623 static void usage(char *pname)
625 fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
628 fprintf(out_hnd, "\nVersion %s\n",VERSION);
629 fprintf(out_hnd, "\t-d debuglevel set the debuglevel\n");
630 fprintf(out_hnd, "\t-l log basename. Basename for log/debug files\n");
631 fprintf(out_hnd, "\t-n netbios name. Use this name as my netbios name\n");
632 fprintf(out_hnd, "\t-m max protocol set the max protocol level\n");
633 fprintf(out_hnd, "\t-I dest IP use this IP to connect to\n");
634 fprintf(out_hnd, "\t-E write messages to stderr instead of stdout\n");
635 fprintf(out_hnd, "\t-U username set the network username\n");
636 fprintf(out_hnd, "\t-W workgroup set the workgroup name\n");
637 fprintf(out_hnd, "\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
638 fprintf(out_hnd, "\n");
648 /****************************************************************************
650 ****************************************************************************/
651 int main(int argc,char *argv[])
653 char *pname = argv[0];
658 static pstring servicesf = CONFIGFILE;
661 mode_t myumask = 0755;
662 enum client_action cli_action = CLIENT_NONE;
666 struct client_info cli_info;
671 pstrcpy(term_code, KANJI);
676 if (!lp_load(servicesf,True, False, False))
678 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
681 codepage_initialise(lp_client_code_page());
685 cli_info.put_total_size = 0;
686 cli_info.put_total_time_ms = 0;
687 cli_info.get_total_size = 0;
688 cli_info.get_total_time_ms = 0;
690 cli_info.dir_total = 0;
691 cli_info.newer_than = 0;
692 cli_info.archive_level = 0;
693 cli_info.print_mode = 1;
695 cli_info.translation = False;
696 cli_info.recurse_dir = False;
697 cli_info.lowercase = False;
698 cli_info.prompt = True;
699 cli_info.abort_mget = True;
701 cli_info.dest_ip.s_addr = 0;
702 cli_info.name_type = 0x20;
704 pstrcpy(cli_info.cur_dir , "\\");
705 pstrcpy(cli_info.file_sel, "");
706 pstrcpy(cli_info.base_dir, "");
707 pstrcpy(cli_info.myhostname, "");
708 pstrcpy(cli_info.dest_host, "");
710 pstrcpy(cli_info.svc_type, "A:");
711 pstrcpy(cli_info.share, "");
712 pstrcpy(cli_info.service, "");
714 ZERO_STRUCT(cli_info.dom.level3_sid);
715 pstrcpy(cli_info.dom.level3_dom, "");
716 ZERO_STRUCT(cli_info.dom.level5_sid);
717 pstrcpy(cli_info.dom.level5_dom, "");
720 setup_logging(pname, True);
723 charset_initialise();
728 if (!get_myname(global_myname, NULL))
730 fprintf(stderr, "Failed to get my hostname.\n");
743 pstrcpy(cli_info.service, argv[1]);
744 /* Convert any '/' characters in the service name to '\' characters */
745 string_replace( cli_info.service, '/','\\');
749 DEBUG(1,("service: %s\n", cli_info.service));
751 if (count_chars(cli_info.service,'\\') < 3)
754 printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
759 if (count_chars(cli_info.service,'\\') > 3)
762 printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
767 if (argc > 1 && (*argv[1] != '-'))
770 pstrcpy(password,argv[1]);
771 memset(argv[1],'X',strlen(argv[1]));
776 cli_action = CLIENT_SVC;
779 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)
785 /* FIXME ... max_protocol seems to be funny here */
787 int max_protocol = 0;
788 max_protocol = interpret_protocol(optarg,max_protocol);
789 fprintf(stderr, "max protocol not currently supported\n");
795 pstrcpy(user_socket_options,optarg);
801 pstrcpy(cli_info.dest_host,optarg);
802 strupper(cli_info.dest_host);
803 cli_action = CLIENT_IPC;
809 iface_set_default(NULL,optarg,NULL);
815 pstrcpy(scope, optarg);
822 pstrcpy(user_name,optarg);
823 if ((lp=strchr(user_name,'%')))
826 pstrcpy(password,lp+1);
828 memset(strchr(optarg,'%')+1,'X',strlen(password));
835 pstrcpy(domain,optarg);
847 cli_info.dest_ip = *interpret_addr2(optarg);
848 if (zero_ip(cli_info.dest_ip))
857 nprocs = atoi(optarg);
863 numops = atoi(optarg);
869 fstrcpy(global_myname, optarg);
878 DEBUGLEVEL = atoi(optarg);
884 slprintf(debugf, sizeof(debugf)-1,
905 pstrcpy(servicesf, optarg);
911 pstrcpy(term_code, optarg);
924 if (cli_action == CLIENT_NONE)
930 strupper(global_myname);
931 fstrcpy(cli_info.myhostname, global_myname);
933 DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
937 if (cli_action == CLIENT_IPC)
939 pstrcpy(cli_info.share, "IPC$");
940 pstrcpy(cli_info.svc_type, "IPC");
943 fstrcpy(cli_info.mach_acct, cli_info.myhostname);
944 strupper(cli_info.mach_acct);
945 fstrcat(cli_info.mach_acct, "$");
947 make_nmb_name(&called , dns_to_netbios_name(cli_info.dest_host ), cli_info.name_type, scope);
948 make_nmb_name(&calling, dns_to_netbios_name(cli_info.myhostname), 0x0 , scope);
952 create_procs(nprocs, numops, &cli_info, run_enums_test);
954 if (password[0] != 0)
956 create_procs(nprocs, numops, &cli_info, run_ntlogin_test);
960 create_procs(nprocs, numops, &cli_info, run_pipegobbler);
962 create_procs(nprocs, numops, &cli_info, run_tcpconnect);
963 create_procs(nprocs, numops, &cli_info, run_handles);
964 create_procs(nprocs, numops, &cli_info, run_randomrpc);