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.
32 extern pstring debugf;
34 extern pstring global_myname;
36 extern pstring user_socket_options;
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 int process_tok(fstring tok);
48 static void cmd_help(struct client_info *info);
49 static void cmd_quit(struct client_info *info);
51 static struct cli_state smbcli;
52 struct cli_state *smb_cli = &smbcli;
56 /****************************************************************************
57 initialise smb client structure
58 ****************************************************************************/
59 void rpcclient_init(void)
61 bzero(smb_cli, sizeof(smb_cli));
62 cli_initialise(smb_cli);
63 smb_cli->capabilities |= CAP_NT_SMBS | CAP_STATUS32;
66 /****************************************************************************
67 make smb client connection
68 ****************************************************************************/
69 static BOOL rpcclient_connect(struct client_info *info)
71 struct nmb_name calling;
72 struct nmb_name called;
74 make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type, scope);
75 make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0 , scope);
77 if (!cli_establish_connection(smb_cli,
78 info->dest_host, &info->dest_ip,
80 info->share, info->svc_type,
83 DEBUG(0,("rpcclient_connect: connection failed\n"));
84 cli_shutdown(smb_cli);
91 /****************************************************************************
92 stop the smb connection(s?)
93 ****************************************************************************/
94 static void rpcclient_stop(void)
96 cli_shutdown(smb_cli);
98 /****************************************************************************
99 This defines the commands supported by this client
100 ****************************************************************************/
104 void (*fn)(struct client_info*);
108 {"svcenum", cmd_svc_enum, "[-i] Lists Services Manager"},
109 {"regenum", cmd_reg_enum, "<keyname> Registry Enumeration (keys, values)"},
110 {"regdeletekey",cmd_reg_delete_key, "<keyname> Registry Key Delete"},
111 {"regcreatekey",cmd_reg_create_key, "<keyname> [keyclass] Registry Key Create"},
112 {"shutdown",cmd_reg_shutdown, "[-m message] [-t timeout] [-r or --reboot] Server Shutdown"},
113 {"regquerykey",cmd_reg_query_key, "<keyname> Registry Key Query"},
114 {"regdeleteval",cmd_reg_delete_val, "<valname> Registry Value Delete"},
115 {"regcreateval",cmd_reg_create_val, "<valname> <valtype> <value> Registry Key Create"},
116 {"reggetsec", cmd_reg_get_key_sec, "<keyname> Registry Key Security"},
117 {"regtestsec", cmd_reg_test_key_sec, "<keyname> Test Registry Key Security"},
118 {"ntlogin", cmd_netlogon_login_test, "[username] [password] NT Domain login test"},
119 {"domtrust", cmd_netlogon_domain_test, "<domain> NT Inter-Domain test"},
120 {"wksinfo", cmd_wks_query_info, "Workstation Query Info"},
121 {"srvinfo", cmd_srv_query_info, "Server Query Info"},
122 {"srvsessions",cmd_srv_enum_sess, "List sessions on a server"},
123 {"srvshares", cmd_srv_enum_shares, "List shares on a server"},
124 {"srvconnections",cmd_srv_enum_conn, "List connections on a server"},
125 {"srvfiles", cmd_srv_enum_files, "List files on a server"},
126 {"lsaquery", cmd_lsa_query_info, "Query Info Policy (domain member or server)"},
127 {"lookupsids", cmd_lsa_lookup_sids, "Resolve names from SIDs"},
128 {"lookupnames",cmd_lsa_lookup_names, "Resolve SIDs from names"},
129 {"lookupdomain",cmd_sam_lookup_domain, "Obtain SID for a local domain"},
130 {"enumusers", cmd_sam_enum_users, "SAM User Database Query (experimental!)"},
131 {"addgroupmem",cmd_sam_add_groupmem,"<group rid> [member rid1] [member rid2] ... SAM Add Domain Group Member"},
132 {"addaliasmem",cmd_sam_add_aliasmem,"<alias rid> [member sid1] [member sid2] ... SAM Add Domain Alias Member"},
133 {"creategroup",cmd_sam_create_dom_group,"SAM Create Domain Group"},
134 {"createalias",cmd_sam_create_dom_alias,"SAM Create Domain Alias"},
135 {"delgroup", cmd_sam_delete_dom_group,"SAM Delete Domain Group"},
136 {"delalias", cmd_sam_delete_dom_alias,"SAM Delete Domain Alias"},
137 {"ntpass", cmd_sam_ntchange_pwd, "NT SAM Password Change"},
138 {"samuser", cmd_sam_query_user, "<username> SAM User Query (experimental!)"},
139 {"samtest", cmd_sam_test , "SAM User Encrypted RPC test (experimental!)"},
140 {"enumaliases",cmd_sam_enum_aliases, "SAM Aliases Database Query (experimental!)"},
141 {"enumgroups", cmd_sam_enum_groups, "SAM Group Database Query (experimental!)"},
142 {"samgroups", cmd_sam_query_dominfo, "SAM Query Domain Info(experimental!)"},
143 {"quit", cmd_quit, "logoff the server"},
144 {"q", cmd_quit, "logoff the server"},
145 {"exit", cmd_quit, "logoff the server"},
146 {"bye", cmd_quit, "logoff the server"},
147 {"help", cmd_help, "[command] give help on a command"},
148 {"?", cmd_help, "[command] give help on a command"},
149 {"!", NULL, "run a shell command on the local system"},
154 /****************************************************************************
155 do a (presumably graceful) quit...
156 ****************************************************************************/
157 static void cmd_quit(struct client_info *info)
163 smb_mem_write_status(dbf);
164 smb_mem_write_errors(dbf);
165 smb_mem_write_verbose(dbf);
172 /****************************************************************************
174 ****************************************************************************/
175 static void cmd_help(struct client_info *info)
180 if (next_token(NULL,buf,NULL, sizeof(buf)))
182 if ((i = process_tok(buf)) >= 0)
183 fprintf(out_hnd, "HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
186 while (commands[i].description)
188 for (j=0; commands[i].description && (j<5); j++) {
189 fprintf(out_hnd, "%-15s",commands[i].name);
192 fprintf(out_hnd, "\n");
196 /*******************************************************************
197 lookup a command string in the list of commands, including
199 ******************************************************************/
200 static int process_tok(fstring tok)
202 int i = 0, matches = 0;
204 int tok_len = strlen(tok);
206 while (commands[i].fn != NULL)
208 if (strequal(commands[i].name,tok))
214 else if (strnequal(commands[i].name, tok, tok_len))
224 else if (matches == 1)
230 /****************************************************************************
231 wait for keyboard activity, swallowing network packets
232 ****************************************************************************/
233 static void wait_keyboard(struct cli_state *cli)
236 struct timeval timeout;
241 FD_SET(cli->fd,&fds);
242 FD_SET(fileno(stdin),&fds);
246 sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
248 if (FD_ISSET(fileno(stdin),&fds))
251 /* We deliberately use receive_smb instead of
252 client_receive_smb as we want to receive
253 session keepalives and then drop them here.
255 if (FD_ISSET(cli->fd,&fds))
256 receive_smb(cli->fd,cli->inbuf,0);
260 /****************************************************************************
261 process commands from the client
262 ****************************************************************************/
263 static void do_command(struct client_info *info, char *tok, char *line)
267 if ((i = process_tok(tok)) >= 0)
269 commands[i].fn(info);
273 fprintf(out_hnd, "%s: command abbreviation ambiguous\n", CNV_LANG(tok));
277 fprintf(out_hnd, "%s: command not found\n", CNV_LANG(tok));
281 /****************************************************************************
282 process commands from the client
283 ****************************************************************************/
284 static BOOL process( struct client_info *info, char *cmd_str)
289 if (cmd[0] != '\0') while (cmd[0] != '\0')
294 if ((p = strchr(cmd, ';')) == 0)
296 strncpy(line, cmd, 999);
302 if (p - cmd > 999) p = cmd + 999;
303 strncpy(line, cmd, p - cmd);
304 line[p - cmd] = '\0';
308 /* input language code to internal one */
311 /* get the first part of the command */
314 if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
317 do_command(info, tok, line);
319 else while (!feof(stdin))
323 /* display a prompt */
324 fprintf(out_hnd, "smb: %s> ", CNV_LANG(info->cur_dir));
328 line[0] = wait_keyboard(smb_cli);
329 /* this might not be such a good idea... */
335 wait_keyboard(smb_cli);
338 /* and get a response */
340 fgets( &line[1],999, stdin);
342 if (!fgets(line,1000,stdin))
348 /* input language code to internal one */
351 /* special case - first char is ! */
358 fprintf(out_hnd, "%s\n", line);
360 /* get the first part of the command */
363 if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
366 do_command(info, tok, line);
372 /****************************************************************************
374 ****************************************************************************/
375 static void usage(char *pname)
377 fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
380 fprintf(out_hnd, "\nVersion %s\n",VERSION);
381 fprintf(out_hnd, "\t-d debuglevel set the debuglevel\n");
382 fprintf(out_hnd, "\t-l log basename. Basename for log/debug files\n");
383 fprintf(out_hnd, "\t-n netbios name. Use this name as my netbios name\n");
384 fprintf(out_hnd, "\t-N don't ask for a password\n");
385 fprintf(out_hnd, "\t-m max protocol set the max protocol level\n");
386 fprintf(out_hnd, "\t-I dest IP use this IP to connect to\n");
387 fprintf(out_hnd, "\t-E write messages to stderr instead of stdout\n");
388 fprintf(out_hnd, "\t-U username set the network username\n");
389 fprintf(out_hnd, "\t-W workgroup set the workgroup name\n");
390 fprintf(out_hnd, "\t-c command string execute semicolon separated commands\n");
391 fprintf(out_hnd, "\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
392 fprintf(out_hnd, "\n");
402 /****************************************************************************
404 ****************************************************************************/
405 int main(int argc,char *argv[])
407 BOOL interactive = True;
413 static pstring servicesf = CONFIGFILE;
416 BOOL got_pass = False;
418 mode_t myumask = 0755;
419 enum client_action cli_action = CLIENT_NONE;
421 struct client_info cli_info;
423 pstring password; /* local copy only, if one is entered */
426 fstrcpy(debugf, argv[0]);
431 pstrcpy(term_code, KANJI);
438 cli_info.put_total_size = 0;
439 cli_info.put_total_time_ms = 0;
440 cli_info.get_total_size = 0;
441 cli_info.get_total_time_ms = 0;
443 cli_info.dir_total = 0;
444 cli_info.newer_than = 0;
445 cli_info.archive_level = 0;
446 cli_info.print_mode = 1;
448 cli_info.translation = False;
449 cli_info.recurse_dir = False;
450 cli_info.lowercase = False;
451 cli_info.prompt = True;
452 cli_info.abort_mget = True;
454 cli_info.dest_ip.s_addr = 0;
455 cli_info.name_type = 0x20;
457 pstrcpy(cli_info.cur_dir , "\\");
458 pstrcpy(cli_info.file_sel, "");
459 pstrcpy(cli_info.base_dir, "");
460 pstrcpy(smb_cli->domain, "");
461 pstrcpy(smb_cli->user_name, "");
462 pstrcpy(cli_info.myhostname, "");
463 pstrcpy(cli_info.dest_host, "");
465 pstrcpy(cli_info.svc_type, "A:");
466 pstrcpy(cli_info.share, "");
467 pstrcpy(cli_info.service, "");
469 ZERO_STRUCT(cli_info.dom.level3_sid);
470 ZERO_STRUCT(cli_info.dom.level5_sid);
471 fstrcpy(cli_info.dom.level3_dom, "");
472 fstrcpy(cli_info.dom.level5_dom, "");
475 charset_initialise();
480 if (!get_myname(global_myname, NULL))
482 fprintf(stderr, "Failed to get my hostname.\n");
487 pstrcpy(smb_cli->user_name,getenv("USER"));
489 /* modification to support userid%passwd syntax in the USER var
490 25.Aug.97, jdblair@uab.edu */
492 if ((p=strchr(smb_cli->user_name,'%')))
495 pstrcpy(password,p+1);
497 memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
499 strupper(smb_cli->user_name);
504 /* modification to support PASSWD environmental var
505 25.Aug.97, jdblair@uab.edu */
506 if (getenv("PASSWD"))
508 pstrcpy(password,getenv("PASSWD"));
511 if (*smb_cli->user_name == 0 && getenv("LOGNAME"))
513 pstrcpy(smb_cli->user_name,getenv("LOGNAME"));
514 strupper(smb_cli->user_name);
526 pstrcpy(cli_info.service, argv[1]);
527 /* Convert any '/' characters in the service name to '\' characters */
528 string_replace( cli_info.service, '/','\\');
532 fprintf(out_hnd, "service: %s\n", cli_info.service);
534 if (count_chars(cli_info.service,'\\') < 3)
537 printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
542 if (count_chars(cli_info.service,'\\') > 3)
545 printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
550 if (argc > 1 && (*argv[1] != '-'))
553 pstrcpy(password,argv[1]);
554 memset(argv[1],'X',strlen(argv[1]));
559 cli_action = CLIENT_SVC;
562 while ((opt = getopt(argc, argv,"s:B:O:M:S:i:N:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
568 /* FIXME ... max_protocol seems to be funny here */
570 int max_protocol = 0;
571 max_protocol = interpret_protocol(optarg,max_protocol);
572 fprintf(stderr, "max protocol not currently supported\n");
578 pstrcpy(user_socket_options,optarg);
584 pstrcpy(cli_info.dest_host,optarg);
585 strupper(cli_info.dest_host);
586 cli_action = CLIENT_IPC;
592 iface_set_default(NULL,optarg,NULL);
598 pstrcpy(scope, optarg);
605 pstrcpy(smb_cli->user_name,optarg);
606 if ((lp=strchr(smb_cli->user_name,'%')))
609 pstrcpy(password,lp+1);
611 memset(strchr(optarg,'%')+1,'X',strlen(password));
618 pstrcpy(smb_cli->domain,optarg);
630 cli_info.dest_ip = *interpret_addr2(optarg);
631 if (zero_ip(cli_info.dest_ip))
640 fstrcpy(global_myname, optarg);
655 DEBUGLEVEL = atoi(optarg);
661 slprintf(debugf, sizeof(debugf)-1,
662 "%s.client", optarg);
683 pstrcpy(servicesf, optarg);
689 pstrcpy(term_code, optarg);
702 setup_logging(debugf, interactive);
704 if (cli_action == CLIENT_NONE)
710 strupper(global_myname);
711 fstrcpy(cli_info.myhostname, global_myname);
713 DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
715 if (!lp_load(servicesf,True, False, False))
717 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
720 codepage_initialise(lp_client_code_page());
722 if (*smb_cli->domain == 0) pstrcpy(smb_cli->domain,lp_workgroup());
726 if (cli_action == CLIENT_IPC)
728 pstrcpy(cli_info.share, "IPC$");
729 pstrcpy(cli_info.svc_type, "IPC");
732 fstrcpy(cli_info.mach_acct, cli_info.myhostname);
733 strupper(cli_info.mach_acct);
734 fstrcat(cli_info.mach_acct, "$");
736 /* set the password cache info */
739 if (password[0] == 0)
741 pwd_set_nullpwd(&(smb_cli->pwd));
745 /* generate 16 byte hashes */
746 pwd_make_lm_nt_16(&(smb_cli->pwd), password);
751 pwd_read(&(smb_cli->pwd), "Enter Password:", True);
754 /* paranoia: destroy the local copy of the password */
755 bzero(password, sizeof(password));
757 /* establish connections. nothing to stop these being re-established. */
758 rpcclient_connect(&cli_info);
760 DEBUG(5,("rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd));
761 if (smb_cli->fd <= 0)
763 fprintf(stderr, "warning: connection could not be established to %s<%02x>\n",
764 cli_info.dest_host, cli_info.name_type);
765 fprintf(stderr, "this version of smbclient may crash if you proceed\n");
773 process(&cli_info, cmd_str);
779 fprintf(stderr, "unknown client action requested\n");