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"
29 /* List to hold groups of commands.
31 * Commands are defined in a list of arrays: arrays are easy to
32 * statically declare, and lists are easier to dynamically extend.
35 static struct cmd_list {
36 struct cmd_list *prev, *next;
37 struct cmd_set *cmd_set;
40 /****************************************************************************
41 handle completion of commands for readline
42 ****************************************************************************/
43 static char **completion_fn(char *text, int start, int end)
45 #define MAX_COMPLETIONS 100
48 struct cmd_list *commands = cmd_list;
51 /* FIXME!!! -- what to do when completing argument? */
52 /* for words not at the start of the line fallback
53 to filename completion */
58 /* make sure we have a list of valid commands */
62 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
63 if (!matches) return NULL;
65 matches[count++] = strdup(text);
66 if (!matches[0]) return NULL;
68 while (commands && count < MAX_COMPLETIONS-1)
70 if (!commands->cmd_set)
73 for (i=0; commands->cmd_set[i].name; i++)
75 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
76 commands->cmd_set[i].fn)
78 matches[count] = strdup(commands->cmd_set[i].name);
85 commands = commands->next;
90 SAFE_FREE(matches[0]);
91 matches[0] = strdup(matches[1]);
93 matches[count] = NULL;
97 /***********************************************************************
98 * read in username/password credentials from a file
100 static void read_authfile (
110 char *ptr, *val, *param;
112 if ((auth=sys_fopen(filename, "r")) == NULL)
114 printf ("ERROR: Unable to open credentials file!\n");
120 /* get a line from the file */
121 if (!fgets (buf, sizeof(buf), auth))
126 /* skip empty lines */
127 if ((len) && (buf[len-1]=='\n'))
135 /* break up the line into parameter & value.
136 will need to eat a little whitespace possibly */
138 if (!(ptr = strchr_m(buf, '=')))
143 /* eat leading white space */
144 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
147 if (strwicmp("password", param) == 0)
148 fstrcpy (password, val);
149 else if (strwicmp("username", param) == 0)
150 fstrcpy (username, val);
151 else if (strwicmp("domain", param) == 0)
152 fstrcpy (domain, val);
154 memset(buf, 0, sizeof(buf));
161 static char* next_command (char** cmdstr)
163 static pstring command;
166 if (!cmdstr || !(*cmdstr))
169 p = strchr_m(*cmdstr, ';');
172 pstrcpy(command, *cmdstr);
183 * Find default username from environment variables.
185 * @param username fstring to receive username; not touched if none is
188 static void get_username (char *username)
191 fstrcpy(username,getenv("USER"));
193 if (*username == 0 && getenv("LOGNAME"))
194 fstrcpy(username,getenv("LOGNAME"));
196 if (*username == 0) {
197 fstrcpy(username,"GUEST");
203 /* Fetch the SID for this computer */
205 static void fetch_machine_sid(struct cli_state *cli)
208 NTSTATUS result = NT_STATUS_OK;
209 uint32 info_class = 5;
211 static BOOL got_domain_sid;
214 if (got_domain_sid) return;
216 if (!(mem_ctx=talloc_init("fetch_machine_sid")))
218 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
223 if (!cli_nt_session_open (cli, PI_LSARPC)) {
224 fprintf(stderr, "could not initialise lsa pipe\n");
228 result = cli_lsa_open_policy(cli, mem_ctx, True,
229 SEC_RIGHTS_MAXIMUM_ALLOWED,
231 if (!NT_STATUS_IS_OK(result)) {
235 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
236 domain_name, &domain_sid);
237 if (!NT_STATUS_IS_OK(result)) {
241 got_domain_sid = True;
243 cli_lsa_close(cli, mem_ctx, &pol);
244 cli_nt_session_close(cli);
245 talloc_destroy(mem_ctx);
250 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
252 if (!NT_STATUS_IS_OK(result)) {
253 fprintf(stderr, "error: %s\n", nt_errstr(result));
259 /* List the available commands on a given pipe */
261 static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
262 int argc, const char **argv)
264 struct cmd_list *tmp;
265 struct cmd_set *tmp_set;
271 printf("Usage: %s <pipe>\n", argv[0]);
275 /* Help on one command */
277 for (tmp = cmd_list; tmp; tmp = tmp->next)
279 tmp_set = tmp->cmd_set;
281 if (!StrCaseCmp(argv[1], tmp_set->name))
283 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
287 while(tmp_set->name) {
288 printf("%20s", tmp_set->name);
295 /* drop out of the loop */
304 /* Display help on commands */
306 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
307 int argc, char **argv)
309 struct cmd_list *tmp;
310 struct cmd_set *tmp_set;
315 printf("Usage: %s [command]\n", argv[0]);
319 /* Help on one command */
322 for (tmp = cmd_list; tmp; tmp = tmp->next) {
324 tmp_set = tmp->cmd_set;
326 while(tmp_set->name) {
327 if (strequal(argv[1], tmp_set->name)) {
328 if (tmp_set->usage &&
330 printf("%s\n", tmp_set->usage);
332 printf("No help for %s\n", tmp_set->name);
341 printf("No such command: %s\n", argv[1]);
345 /* List all commands */
347 for (tmp = cmd_list; tmp; tmp = tmp->next) {
349 tmp_set = tmp->cmd_set;
351 while(tmp_set->name) {
353 printf("%15s\t\t%s\n", tmp_set->name,
354 tmp_set->description ? tmp_set->description:
364 /* Change the debug level */
366 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
367 int argc, const char **argv)
370 printf("Usage: %s [debuglevel]\n", argv[0]);
375 DEBUGLEVEL = atoi(argv[1]);
378 printf("debuglevel is %d\n", DEBUGLEVEL);
383 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
384 int argc, char **argv)
387 return NT_STATUS_OK; /* NOTREACHED */
390 /* Built in rpcclient commands */
392 static struct cmd_set rpcclient_commands[] = {
394 { "GENERAL OPTIONS" },
396 { "help", cmd_help, -1, "Get help on commands", "[command]" },
397 { "?", cmd_help, -1, "Get help on commands", "[command]" },
398 { "debuglevel", cmd_debuglevel, -1, "Set debug level", "level" },
399 { "list", cmd_listcommands, -1, "List available commands on <pipe>", "pipe" },
400 { "exit", cmd_quit, -1, "Exit program", "" },
401 { "quit", cmd_quit, -1, "Exit program", "" },
406 static struct cmd_set separator_command[] = {
407 { "---------------", NULL, -1, "----------------------" },
412 /* Various pipe commands */
414 extern struct cmd_set lsarpc_commands[];
415 extern struct cmd_set samr_commands[];
416 extern struct cmd_set spoolss_commands[];
417 extern struct cmd_set netlogon_commands[];
418 extern struct cmd_set srvsvc_commands[];
419 extern struct cmd_set dfs_commands[];
420 extern struct cmd_set reg_commands[];
421 extern struct cmd_set ds_commands[];
423 static struct cmd_set *rpcclient_command_list[] = {
436 static void add_command_set(struct cmd_set *cmd_set)
438 struct cmd_list *entry;
440 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
441 DEBUG(0, ("out of memory\n"));
447 entry->cmd_set = cmd_set;
448 DLIST_ADD(cmd_list, entry);
453 * Call an rpcclient function, passing an argv array.
455 * @param cmd Command to run, as a single string.
457 static NTSTATUS do_cmd(struct cli_state *cli,
458 struct cmd_set *cmd_entry,
459 int argc, char **argv)
467 if (!(mem_ctx = talloc_init("do_cmd"))) {
468 DEBUG(0, ("talloc_init() failed\n"));
469 return NT_STATUS_UNSUCCESSFUL;
474 if (cmd_entry->pipe_idx != -1)
475 if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
476 DEBUG(0, ("Could not initialize pipe\n"));
477 return NT_STATUS_UNSUCCESSFUL;
482 result = cmd_entry->fn(cli, mem_ctx, argc, (const char **) argv);
486 if (cmd_entry->pipe_idx != -1)
487 cli_nt_session_close(cli);
489 talloc_destroy(mem_ctx);
496 * Process a command entered at the prompt or as part of -c
498 * @returns The NTSTATUS from running the command.
500 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
502 struct cmd_list *temp_list;
503 NTSTATUS result = NT_STATUS_OK;
508 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
509 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
510 return NT_STATUS_UNSUCCESSFUL;
514 /* Walk through a dlist of arrays of commands. */
515 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
516 struct cmd_set *temp_set = temp_list->cmd_set;
518 while (temp_set->name) {
519 if (strequal(argv[0], temp_set->name)) {
521 fprintf (stderr, "Invalid command\n");
525 result = do_cmd(cli, temp_set, argc, argv);
534 printf("command not found: %s\n", argv[0]);
538 if (!NT_STATUS_IS_OK(result)) {
539 printf("result was %s\n", nt_errstr(result));
543 /* NOTE: popt allocates the whole argv, including the
544 * strings, as a single block. So a single free is
545 * enough to release it -- we don't free the
546 * individual strings. rtfm. */
556 int main(int argc, char *argv[])
558 static int got_pass = 0;
559 BOOL interactive = True;
561 static char *cmdstr = "";
563 struct cli_state *cli;
567 static char *opt_authfile=NULL,
573 struct cmd_set **cmd_set;
574 struct in_addr server_ip;
577 /* make sure the vars that get altered (4th field) are in
578 a fixed location or certain compilers complain */
580 struct poptOption long_options[] = {
582 {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A', "File containing user credentials", "AUTHFILE"},
583 {"nopass", 'N', POPT_ARG_NONE, &got_pass, 'N', "Don't ask for a password"},
584 {"user", 'U', POPT_ARG_STRING, &opt_username, 'U', "Set the network username", "USER"},
585 {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W', "Set the domain name for user account", "DOMAIN"},
586 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
587 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Logfile to use instead of stdout", "LOGFILE" },
588 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
589 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
590 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
591 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
599 pc = poptGetContext("rpcclient", argc, (const char **) argv,
603 poptPrintHelp(pc, stderr, 0);
607 while((opt = poptGetNextOpt(pc)) != -1) {
610 /* only get the username, password, and domain from the file */
611 read_authfile (opt_authfile, username, password, domain);
612 if (strlen (password))
617 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
619 lp_set_logfile(logfile);
626 fstrcpy(username,opt_username);
628 if ((lp=strchr_m(username,'%'))) {
630 fstrcpy(password,lp+1);
632 memset(strchr_m(opt_username,'%') + 1, 'X',
638 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
639 fprintf(stderr, "%s not a valid IP address\n",
644 fstrcpy(domain, opt_domain);
649 /* Get server as remaining unparsed argument. Print usage if more
650 than one unparsed argument is present. */
652 server = poptGetArg(pc);
654 if (!server || poptGetArg(pc)) {
655 poptPrintHelp(pc, stderr, 0);
661 /* the following functions are part of the Samba debugging
662 facilities. See lib/debug.c */
663 setup_logging("rpcclient", interactive);
667 /* Load smb.conf file */
669 if (!lp_load(dyn_CONFIGFILE,True,False,False))
670 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
677 /* Resolve the IP address */
679 if (!opt_ipaddr && !resolve_name(server, &server_ip, 0x20)) {
680 fprintf(stderr, "Unable to resolve %s\n", server);
686 * from stdin if necessary
690 char *pass = getpass("Password:");
692 fstrcpy(password, pass);
696 if (!strlen(username) && !got_pass)
697 get_username(username);
699 nt_status = cli_full_connection(&cli, global_myname(), server,
705 if (!NT_STATUS_IS_OK(nt_status)) {
706 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
710 memset(password,'X',sizeof(password));
712 /* Load command lists */
714 cmd_set = rpcclient_command_list;
717 add_command_set(*cmd_set);
718 add_command_set(separator_command);
722 fetch_machine_sid(cli);
724 /* Do anything specified with -c */
729 while((cmd=next_command(&p)) != NULL) {
730 process_cmd(cli, cmd);
737 /* Loop around accepting commands */
743 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
745 line = smb_readline(prompt, NULL, completion_fn);
751 process_cmd(cli, line);