2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2000-2001
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.
23 #include "rpcclient.h"
27 /* List to hold groups of commands */
29 static struct cmd_list {
30 struct cmd_list *prev, *next;
31 struct cmd_set *cmd_set;
34 /****************************************************************************
35 handle completion of commands for readline
36 ****************************************************************************/
37 static char **completion_fn(char *text, int start, int end)
39 #define MAX_COMPLETIONS 100
42 struct cmd_list *commands = cmd_list;
45 /* FIXME!!! -- what to do when completing argument? */
46 /* for words not at the start of the line fallback
47 to filename completion */
52 /* make sure we have a list of valid commands */
56 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
57 if (!matches) return NULL;
59 matches[count++] = strdup(text);
60 if (!matches[0]) return NULL;
62 while (commands && count < MAX_COMPLETIONS-1)
64 if (!commands->cmd_set)
67 for (i=0; commands->cmd_set[i].name; i++)
69 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
70 commands->cmd_set[i].fn)
72 matches[count] = strdup(commands->cmd_set[i].name);
79 commands = commands->next;
84 SAFE_FREE(matches[0]);
85 matches[0] = strdup(matches[1]);
87 matches[count] = NULL;
91 /***********************************************************************
92 * read in username/password credentials from a file
94 static void read_authfile (
104 char *ptr, *val, *param;
106 if ((auth=sys_fopen(filename, "r")) == NULL)
108 printf ("ERROR: Unable to open credentials file!\n");
114 /* get a line from the file */
115 if (!fgets (buf, sizeof(buf), auth))
120 /* skip empty lines */
121 if ((len) && (buf[len-1]=='\n'))
129 /* break up the line into parameter & value.
130 will need to eat a little whitespace possibly */
132 if (!(ptr = strchr_m(buf, '=')))
137 /* eat leading white space */
138 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
141 if (strwicmp("password", param) == 0)
142 fstrcpy (password, val);
143 else if (strwicmp("username", param) == 0)
144 fstrcpy (username, val);
145 else if (strwicmp("domain", param) == 0)
146 fstrcpy (domain, val);
148 memset(buf, 0, sizeof(buf));
155 static char* next_command (char** cmdstr)
157 static pstring command;
160 if (!cmdstr || !(*cmdstr))
163 p = strchr_m(*cmdstr, ';');
166 pstrcpy(command, *cmdstr);
172 static void get_username (char *username)
175 pstrcpy(username,getenv("USER"));
177 if (*username == 0 && getenv("LOGNAME"))
178 pstrcpy(username,getenv("LOGNAME"));
180 if (*username == 0) {
181 pstrcpy(username,"GUEST");
187 /* Fetch the SID for this computer */
189 static void fetch_machine_sid(struct cli_state *cli)
192 NTSTATUS result = NT_STATUS_OK;
193 uint32 info_class = 5;
195 static BOOL got_domain_sid;
198 if (got_domain_sid) return;
200 if (!(mem_ctx=talloc_init()))
202 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
207 if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
208 fprintf(stderr, "could not initialise lsa pipe\n");
212 result = cli_lsa_open_policy(cli, mem_ctx, True,
213 SEC_RIGHTS_MAXIMUM_ALLOWED,
215 if (!NT_STATUS_IS_OK(result)) {
219 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
220 domain_name, &domain_sid);
221 if (!NT_STATUS_IS_OK(result)) {
225 got_domain_sid = True;
227 cli_lsa_close(cli, mem_ctx, &pol);
228 cli_nt_session_close(cli);
229 talloc_destroy(mem_ctx);
234 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
236 if (!NT_STATUS_IS_OK(result)) {
237 fprintf(stderr, "error: %s\n", nt_errstr(result));
243 /* List the available commands on a given pipe */
245 static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
246 int argc, char **argv)
248 struct cmd_list *tmp;
249 struct cmd_set *tmp_set;
255 printf("Usage: %s <pipe>\n", argv[0]);
259 /* Help on one command */
261 for (tmp = cmd_list; tmp; tmp = tmp->next)
263 tmp_set = tmp->cmd_set;
265 if (!StrCaseCmp(argv[1], tmp_set->name))
267 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
271 while(tmp_set->name) {
272 printf("%20s", tmp_set->name);
279 /* drop out of the loop */
288 /* Display help on commands */
290 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
291 int argc, char **argv)
293 struct cmd_list *tmp;
294 struct cmd_set *tmp_set;
299 printf("Usage: %s [command]\n", argv[0]);
303 /* Help on one command */
306 for (tmp = cmd_list; tmp; tmp = tmp->next) {
308 tmp_set = tmp->cmd_set;
310 while(tmp_set->name) {
311 if (strequal(argv[1], tmp_set->name)) {
312 if (tmp_set->usage &&
314 printf("%s\n", tmp_set->usage);
316 printf("No help for %s\n", tmp_set->name);
325 printf("No such command: %s\n", argv[1]);
329 /* List all commands */
331 for (tmp = cmd_list; tmp; tmp = tmp->next) {
333 tmp_set = tmp->cmd_set;
335 while(tmp_set->name) {
337 printf("%15s\t\t%s\n", tmp_set->name,
338 tmp_set->description ? tmp_set->description:
348 /* Change the debug level */
350 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
351 int argc, char **argv)
354 printf("Usage: %s [debuglevel]\n", argv[0]);
359 DEBUGLEVEL = atoi(argv[1]);
362 printf("debuglevel is %d\n", DEBUGLEVEL);
367 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
368 int argc, char **argv)
371 return NT_STATUS_OK; /* NOTREACHED */
374 /* Build in rpcclient commands */
376 static struct cmd_set rpcclient_commands[] = {
378 { "GENERAL OPTIONS" },
380 { "help", cmd_help, NULL, "Get help on commands", "[command]" },
381 { "?", cmd_help, NULL, "Get help on commands", "[command]" },
382 { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" },
383 { "list", cmd_listcommands, NULL, "List available commands on <pipe>", "pipe" },
384 { "exit", cmd_quit, NULL, "Exit program", "" },
385 { "quit", cmd_quit, NULL, "Exit program", "" },
390 static struct cmd_set separator_command[] = {
391 { "---------------", NULL, NULL, "----------------------" },
396 /* Various pipe commands */
398 extern struct cmd_set lsarpc_commands[];
399 extern struct cmd_set samr_commands[];
400 extern struct cmd_set spoolss_commands[];
401 extern struct cmd_set netlogon_commands[];
402 extern struct cmd_set srvsvc_commands[];
403 extern struct cmd_set dfs_commands[];
404 extern struct cmd_set reg_commands[];
406 static struct cmd_set *rpcclient_command_list[] = {
418 static void add_command_set(struct cmd_set *cmd_set)
420 struct cmd_list *entry;
422 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
423 DEBUG(0, ("out of memory\n"));
429 entry->cmd_set = cmd_set;
430 DLIST_ADD(cmd_list, entry);
433 static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry,
436 char *p = cmd, **argv = NULL;
437 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
441 /* Count number of arguments first time through the loop then
442 allocate memory and strdup them. */
445 while(next_token(&p, buf, " ", sizeof(buf))) {
447 argv[argc] = strdup(buf);
455 /* Create argument list */
457 argv = (char **)malloc(sizeof(char *) * argc);
458 memset(argv, 0, sizeof(char *) * argc);
461 fprintf(stderr, "out of memory\n");
462 result = NT_STATUS_NO_MEMORY;
472 /* Call the function */
479 if (!(mem_ctx = talloc_init())) {
480 DEBUG(0, ("talloc_init() failed\n"));
487 if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
488 DEBUG(0, ("Could not initialise %s\n",
495 result = cmd_entry->fn(cli, mem_ctx, argc, argv);
500 cli_nt_session_close(cli);
502 talloc_destroy(mem_ctx);
505 fprintf (stderr, "Invalid command\n");
514 for (i = 0; i < argc; i++)
523 /* Process a command entered at the prompt or as part of -c */
525 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
527 struct cmd_list *temp_list;
531 NTSTATUS result = NT_STATUS_OK;
534 if (cmd[strlen(cmd) - 1] == '\n')
535 cmd[strlen(cmd) - 1] = '\0';
537 if (!next_token(&p, buf, " ", sizeof(buf))) {
541 /* strip the trainly \n if it exsists */
543 if (buf[len-1] == '\n')
546 /* Search for matching commands */
548 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
549 struct cmd_set *temp_set = temp_list->cmd_set;
551 while(temp_set->name) {
552 if (strequal(buf, temp_set->name)) {
554 result = do_cmd(cli, temp_set, cmd);
563 if (!found && buf[0]) {
564 printf("command not found: %s\n", buf);
568 if (!NT_STATUS_IS_OK(result)) {
569 printf("result was %s\n", nt_errstr(result));
576 /* Print usage information */
577 static void usage(void)
579 printf("Usage: rpcclient [options] server\n");
581 printf("\t-A or --authfile authfile File containing user credentials\n");
582 printf("\t-c or --command \"command string\" Execute semicolon separated cmds\n");
583 printf("\t-d or --debug debuglevel Set the debuglevel\n");
584 printf("\t-l or --logfile logfile Logfile to use instead of stdout\n");
585 printf("\t-h or --help Print this help message.\n");
586 printf("\t-N or --nopass Don't ask for a password\n");
587 printf("\t-s or --conf configfile Specify an alternative config file\n");
588 printf("\t-U or --user username Set the network username\n");
589 printf("\t-W or --workgroup domain Set the domain name for user account\n");
590 printf("\t-I or --dest-ip ip Specify destination IP address\n");
596 int main(int argc, char *argv[])
598 extern pstring global_myname;
599 static int got_pass = 0;
600 BOOL interactive = True;
603 static char *cmdstr = "";
605 struct cli_state *cli;
609 static char *opt_authfile=NULL,
612 *opt_configfile=NULL,
615 static int opt_debuglevel;
617 struct cmd_set **cmd_set;
618 struct in_addr server_ip;
620 extern BOOL AllowDebugChange;
622 /* make sure the vars that get altered (4th field) are in
623 a fixed location or certain compilers complain */
625 struct poptOption long_options[] = {
626 {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A'},
627 {"conf", 's', POPT_ARG_STRING, &opt_configfile, 's'},
628 {"nopass", 'N', POPT_ARG_NONE, &got_pass},
629 {"debug", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
630 {"debuglevel", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
631 {"user", 'U', POPT_ARG_STRING, &opt_username, 'U'},
632 {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W'},
633 {"command", 'c', POPT_ARG_STRING, &cmdstr},
634 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l'},
635 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
636 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I'},
644 AllowDebugChange = False;
653 pc = poptGetContext("rpcclient", argc, (const char **) argv,
656 while((opt = poptGetNextOpt(pc)) != -1) {
659 /* only get the username, password, and domain from the file */
660 read_authfile (opt_authfile, username, password, domain);
661 if (strlen (password))
666 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
668 lp_set_logfile(logfile);
673 pstrcpy(dyn_CONFIGFILE, opt_configfile);
677 DEBUGLEVEL = opt_debuglevel;
683 pstrcpy(username,opt_username);
685 if ((lp=strchr_m(username,'%'))) {
687 pstrcpy(password,lp+1);
689 memset(strchr_m(opt_username,'%') + 1, 'X',
695 if (!inet_aton(opt_ipaddr, &server_ip)) {
696 fprintf(stderr, "%s not a valid IP address\n",
701 pstrcpy(domain, opt_domain);
711 /* Get server as remaining unparsed argument. Print usage if more
712 than one unparsed argument is present. */
714 server = poptGetArg(pc);
716 if (!server || poptGetArg(pc)) {
723 /* the following functions are part of the Samba debugging
724 facilities. See lib/debug.c */
725 setup_logging("rpcclient", interactive);
729 /* Load smb.conf file */
730 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
731 olddebug = DEBUGLEVEL;
732 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
733 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
735 DEBUGLEVEL = olddebug;
739 get_myname((*global_myname)?NULL:global_myname);
740 strupper(global_myname);
742 /* Resolve the IP address */
744 if (!opt_ipaddr && !resolve_name(server, &server_ip, 0x20)) {
745 DEBUG(1,("Unable to resolve %s\n", server));
751 * from stdin if necessary
755 char *pass = getpass("Password:");
757 fstrcpy(password, pass);
761 if (!strlen(username) && !got_pass)
762 get_username(username);
764 nt_status = cli_full_connection(&cli, global_myname, server,
770 if (!NT_STATUS_IS_OK(nt_status)) {
771 DEBUG(1,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
775 memset(password,'X',sizeof(password));
777 /* Load command lists */
779 cmd_set = rpcclient_command_list;
782 add_command_set(*cmd_set);
783 add_command_set(separator_command);
787 fetch_machine_sid(cli);
789 /* Do anything specified with -c */
794 while((cmd=next_command(&p)) != NULL) {
795 process_cmd(cli, cmd);
802 /* Loop around accepting commands */
808 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
810 line = smb_readline(prompt, NULL, completion_fn);
816 process_cmd(cli, line);