2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Martin Pool 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "rpcclient.h"
27 #define HAVE_SMB_REGISTER_PASSDB
28 #include "module_dummy.h"
33 /* List to hold groups of commands.
35 * Commands are defined in a list of arrays: arrays are easy to
36 * statically declare, and lists are easier to dynamically extend.
39 static struct cmd_list {
40 struct cmd_list *prev, *next;
41 struct cmd_set *cmd_set;
44 /****************************************************************************
45 handle completion of commands for readline
46 ****************************************************************************/
47 static char **completion_fn(char *text, int start, int end)
49 #define MAX_COMPLETIONS 100
52 struct cmd_list *commands = cmd_list;
55 /* FIXME!!! -- what to do when completing argument? */
56 /* for words not at the start of the line fallback
57 to filename completion */
62 /* make sure we have a list of valid commands */
66 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
67 if (!matches) return NULL;
69 matches[count++] = strdup(text);
70 if (!matches[0]) return NULL;
72 while (commands && count < MAX_COMPLETIONS-1)
74 if (!commands->cmd_set)
77 for (i=0; commands->cmd_set[i].name; i++)
79 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
80 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
81 commands->cmd_set[i].ntfn ) ||
82 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
83 commands->cmd_set[i].wfn)))
85 matches[count] = strdup(commands->cmd_set[i].name);
92 commands = commands->next;
97 SAFE_FREE(matches[0]);
98 matches[0] = strdup(matches[1]);
100 matches[count] = NULL;
104 static char* next_command (char** cmdstr)
106 static pstring command;
109 if (!cmdstr || !(*cmdstr))
112 p = strchr_m(*cmdstr, ';');
115 pstrcpy(command, *cmdstr);
124 /* Fetch the SID for this computer */
126 static void fetch_machine_sid(struct cli_state *cli)
129 NTSTATUS result = NT_STATUS_OK;
130 uint32 info_class = 5;
132 static BOOL got_domain_sid;
135 if (got_domain_sid) return;
137 if (!(mem_ctx=talloc_init("fetch_machine_sid")))
139 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
144 if (!cli_nt_session_open (cli, PI_LSARPC)) {
145 fprintf(stderr, "could not initialise lsa pipe\n");
149 result = cli_lsa_open_policy(cli, mem_ctx, True,
150 SEC_RIGHTS_MAXIMUM_ALLOWED,
152 if (!NT_STATUS_IS_OK(result)) {
156 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
157 domain_name, &domain_sid);
158 if (!NT_STATUS_IS_OK(result)) {
162 got_domain_sid = True;
164 cli_lsa_close(cli, mem_ctx, &pol);
165 cli_nt_session_close(cli);
166 talloc_destroy(mem_ctx);
171 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
173 if (!NT_STATUS_IS_OK(result)) {
174 fprintf(stderr, "error: %s\n", nt_errstr(result));
180 /* List the available commands on a given pipe */
182 static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
183 int argc, const char **argv)
185 struct cmd_list *tmp;
186 struct cmd_set *tmp_set;
192 printf("Usage: %s <pipe>\n", argv[0]);
196 /* Help on one command */
198 for (tmp = cmd_list; tmp; tmp = tmp->next)
200 tmp_set = tmp->cmd_set;
202 if (!StrCaseCmp(argv[1], tmp_set->name))
204 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
208 while(tmp_set->name) {
209 printf("%20s", tmp_set->name);
216 /* drop out of the loop */
225 /* Display help on commands */
227 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
228 int argc, const char **argv)
230 struct cmd_list *tmp;
231 struct cmd_set *tmp_set;
236 printf("Usage: %s [command]\n", argv[0]);
240 /* Help on one command */
243 for (tmp = cmd_list; tmp; tmp = tmp->next) {
245 tmp_set = tmp->cmd_set;
247 while(tmp_set->name) {
248 if (strequal(argv[1], tmp_set->name)) {
249 if (tmp_set->usage &&
251 printf("%s\n", tmp_set->usage);
253 printf("No help for %s\n", tmp_set->name);
262 printf("No such command: %s\n", argv[1]);
266 /* List all commands */
268 for (tmp = cmd_list; tmp; tmp = tmp->next) {
270 tmp_set = tmp->cmd_set;
272 while(tmp_set->name) {
274 printf("%15s\t\t%s\n", tmp_set->name,
275 tmp_set->description ? tmp_set->description:
285 /* Change the debug level */
287 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
288 int argc, const char **argv)
291 printf("Usage: %s [debuglevel]\n", argv[0]);
296 DEBUGLEVEL = atoi(argv[1]);
299 printf("debuglevel is %d\n", DEBUGLEVEL);
304 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
305 int argc, const char **argv)
308 return NT_STATUS_OK; /* NOTREACHED */
311 /* Built in rpcclient commands */
313 static struct cmd_set rpcclient_commands[] = {
315 { "GENERAL OPTIONS" },
317 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
318 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
319 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, "Set debug level", "level" },
320 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, "List available commands on <pipe>", "pipe" },
321 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
322 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
327 static struct cmd_set separator_command[] = {
328 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, "----------------------" },
333 /* Various pipe commands */
335 extern struct cmd_set lsarpc_commands[];
336 extern struct cmd_set samr_commands[];
337 extern struct cmd_set spoolss_commands[];
338 extern struct cmd_set netlogon_commands[];
339 extern struct cmd_set srvsvc_commands[];
340 extern struct cmd_set dfs_commands[];
341 extern struct cmd_set reg_commands[];
342 extern struct cmd_set ds_commands[];
343 extern struct cmd_set echo_commands[];
345 static struct cmd_set *rpcclient_command_list[] = {
359 static void add_command_set(struct cmd_set *cmd_set)
361 struct cmd_list *entry;
363 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
364 DEBUG(0, ("out of memory\n"));
370 entry->cmd_set = cmd_set;
371 DLIST_ADD(cmd_list, entry);
376 * Call an rpcclient function, passing an argv array.
378 * @param cmd Command to run, as a single string.
380 static NTSTATUS do_cmd(struct cli_state *cli,
381 struct cmd_set *cmd_entry,
382 int argc, char **argv)
391 if (!(mem_ctx = talloc_init("do_cmd"))) {
392 DEBUG(0, ("talloc_init() failed\n"));
393 return NT_STATUS_UNSUCCESSFUL;
398 if (cmd_entry->pipe_idx != -1) {
399 if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
400 DEBUG(0, ("Could not initialise %s\n",
401 get_pipe_name_from_index(cmd_entry->pipe_idx)));
402 return NT_STATUS_UNSUCCESSFUL;
406 if (cmd_entry->pipe_idx == PI_NETLOGON) {
407 uchar trust_password[16];
408 uint32 sec_channel_type;
410 if (!secrets_fetch_trust_account_password(lp_workgroup(),
412 NULL, &sec_channel_type)) {
413 return NT_STATUS_UNSUCCESSFUL;
416 if (!NT_STATUS_IS_OK(cli_nt_establish_netlogon(cli, sec_channel_type,
418 DEBUG(0, ("Could not initialise NETLOGON pipe\n"));
419 return NT_STATUS_UNSUCCESSFUL;
425 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
426 ntresult = cmd_entry->ntfn(cli, mem_ctx, argc, (const char **) argv);
427 if (!NT_STATUS_IS_OK(ntresult)) {
428 printf("result was %s\n", nt_errstr(ntresult));
431 wresult = cmd_entry->wfn( cli, mem_ctx, argc, (const char **) argv);
432 /* print out the DOS error */
433 if (!W_ERROR_IS_OK(wresult)) {
434 printf( "result was %s\n", dos_errstr(wresult));
436 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
442 if (cmd_entry->pipe_idx != -1)
443 cli_nt_session_close(cli);
445 talloc_destroy(mem_ctx);
452 * Process a command entered at the prompt or as part of -c
454 * @returns The NTSTATUS from running the command.
456 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
458 struct cmd_list *temp_list;
459 NTSTATUS result = NT_STATUS_OK;
464 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
465 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
466 return NT_STATUS_UNSUCCESSFUL;
470 /* Walk through a dlist of arrays of commands. */
471 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
472 struct cmd_set *temp_set = temp_list->cmd_set;
474 while (temp_set->name) {
475 if (strequal(argv[0], temp_set->name)) {
476 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
477 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
478 fprintf (stderr, "Invalid command\n");
482 result = do_cmd(cli, temp_set, argc, argv);
491 printf("command not found: %s\n", argv[0]);
496 if (!NT_STATUS_IS_OK(result)) {
497 printf("result was %s\n", nt_errstr(result));
502 /* NOTE: popt allocates the whole argv, including the
503 * strings, as a single block. So a single free is
504 * enough to release it -- we don't free the
505 * individual strings. rtfm. */
515 int main(int argc, char *argv[])
517 BOOL interactive = True;
519 static char *cmdstr = NULL;
521 struct cli_state *cli;
522 static char *opt_ipaddr=NULL;
523 struct cmd_set **cmd_set;
524 struct in_addr server_ip;
527 /* make sure the vars that get altered (4th field) are in
528 a fixed location or certain compilers complain */
530 struct poptOption long_options[] = {
532 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
533 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
535 POPT_COMMON_CONNECTION
536 POPT_COMMON_CREDENTIALS
540 ZERO_STRUCT(server_ip);
544 /* the following functions are part of the Samba debugging
545 facilities. See lib/debug.c */
546 setup_logging("rpcclient", interactive);
550 /* Load smb.conf file */
552 if (!lp_load(dyn_CONFIGFILE,True,False,False))
553 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
557 pc = poptGetContext("rpcclient", argc, (const char **) argv,
561 poptPrintHelp(pc, stderr, 0);
565 while((opt = poptGetNextOpt(pc)) != -1) {
569 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
570 fprintf(stderr, "%s not a valid IP address\n",
577 /* Get server as remaining unparsed argument. Print usage if more
578 than one unparsed argument is present. */
580 server = poptGetArg(pc);
582 if (!server || poptGetArg(pc)) {
583 poptPrintHelp(pc, stderr, 0);
596 * from stdin if necessary
599 if (!cmdline_auth_info.got_pass) {
600 char *pass = getpass("Password:");
602 pstrcpy(cmdline_auth_info.password, pass);
606 nt_status = cli_full_connection(&cli, global_myname(), server,
607 opt_ipaddr ? &server_ip : NULL, 0,
609 cmdline_auth_info.username, lp_workgroup(),
610 cmdline_auth_info.password, 0, NULL);
612 if (!NT_STATUS_IS_OK(nt_status)) {
613 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
617 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
619 /* Load command lists */
621 cmd_set = rpcclient_command_list;
624 add_command_set(*cmd_set);
625 add_command_set(separator_command);
629 fetch_machine_sid(cli);
631 /* Do anything specified with -c */
632 if (cmdstr && cmdstr[0]) {
637 while((cmd=next_command(&p)) != NULL) {
638 NTSTATUS cmd_result = process_cmd(cli, cmd);
639 result = NT_STATUS_IS_ERR(cmd_result);
646 /* Loop around accepting commands */
652 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
654 line = smb_readline(prompt, NULL, completion_fn);
660 process_cmd(cli, line);