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].returntype == RPC_RTYPE_NTSTATUS &&
77 commands->cmd_set[i].ntfn ) ||
78 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
79 commands->cmd_set[i].wfn)))
81 matches[count] = strdup(commands->cmd_set[i].name);
88 commands = commands->next;
93 SAFE_FREE(matches[0]);
94 matches[0] = strdup(matches[1]);
96 matches[count] = NULL;
100 /***********************************************************************
101 * read in username/password credentials from a file
103 static void read_authfile (
113 char *ptr, *val, *param;
115 if ((auth=sys_fopen(filename, "r")) == NULL)
117 printf ("ERROR: Unable to open credentials file!\n");
123 /* get a line from the file */
124 if (!fgets (buf, sizeof(buf), auth))
129 /* skip empty lines */
130 if ((len) && (buf[len-1]=='\n'))
138 /* break up the line into parameter & value.
139 will need to eat a little whitespace possibly */
141 if (!(ptr = strchr_m(buf, '=')))
146 /* eat leading white space */
147 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
150 if (strwicmp("password", param) == 0)
151 fstrcpy (password, val);
152 else if (strwicmp("username", param) == 0)
153 fstrcpy (username, val);
154 else if (strwicmp("domain", param) == 0)
155 fstrcpy (domain, val);
157 memset(buf, 0, sizeof(buf));
164 static char* next_command (char** cmdstr)
166 static pstring command;
169 if (!cmdstr || !(*cmdstr))
172 p = strchr_m(*cmdstr, ';');
175 pstrcpy(command, *cmdstr);
186 * Find default username from environment variables.
188 * @param username fstring to receive username; not touched if none is
191 static void get_username (char *username)
194 fstrcpy(username,getenv("USER"));
196 if (*username == 0 && getenv("LOGNAME"))
197 fstrcpy(username,getenv("LOGNAME"));
199 if (*username == 0) {
200 fstrcpy(username,"GUEST");
206 /* Fetch the SID for this computer */
208 static void fetch_machine_sid(struct cli_state *cli)
211 NTSTATUS result = NT_STATUS_OK;
212 uint32 info_class = 5;
214 static BOOL got_domain_sid;
217 if (got_domain_sid) return;
219 if (!(mem_ctx=talloc_init("fetch_machine_sid")))
221 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
226 if (!cli_nt_session_open (cli, PI_LSARPC)) {
227 fprintf(stderr, "could not initialise lsa pipe\n");
231 result = cli_lsa_open_policy(cli, mem_ctx, True,
232 SEC_RIGHTS_MAXIMUM_ALLOWED,
234 if (!NT_STATUS_IS_OK(result)) {
238 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
239 domain_name, &domain_sid);
240 if (!NT_STATUS_IS_OK(result)) {
244 got_domain_sid = True;
246 cli_lsa_close(cli, mem_ctx, &pol);
247 cli_nt_session_close(cli);
248 talloc_destroy(mem_ctx);
253 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
255 if (!NT_STATUS_IS_OK(result)) {
256 fprintf(stderr, "error: %s\n", nt_errstr(result));
262 /* List the available commands on a given pipe */
264 static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
265 int argc, const char **argv)
267 struct cmd_list *tmp;
268 struct cmd_set *tmp_set;
274 printf("Usage: %s <pipe>\n", argv[0]);
278 /* Help on one command */
280 for (tmp = cmd_list; tmp; tmp = tmp->next)
282 tmp_set = tmp->cmd_set;
284 if (!StrCaseCmp(argv[1], tmp_set->name))
286 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
290 while(tmp_set->name) {
291 printf("%20s", tmp_set->name);
298 /* drop out of the loop */
307 /* Display help on commands */
309 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
310 int argc, const char **argv)
312 struct cmd_list *tmp;
313 struct cmd_set *tmp_set;
318 printf("Usage: %s [command]\n", argv[0]);
322 /* Help on one command */
325 for (tmp = cmd_list; tmp; tmp = tmp->next) {
327 tmp_set = tmp->cmd_set;
329 while(tmp_set->name) {
330 if (strequal(argv[1], tmp_set->name)) {
331 if (tmp_set->usage &&
333 printf("%s\n", tmp_set->usage);
335 printf("No help for %s\n", tmp_set->name);
344 printf("No such command: %s\n", argv[1]);
348 /* List all commands */
350 for (tmp = cmd_list; tmp; tmp = tmp->next) {
352 tmp_set = tmp->cmd_set;
354 while(tmp_set->name) {
356 printf("%15s\t\t%s\n", tmp_set->name,
357 tmp_set->description ? tmp_set->description:
367 /* Change the debug level */
369 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
370 int argc, const char **argv)
373 printf("Usage: %s [debuglevel]\n", argv[0]);
378 DEBUGLEVEL = atoi(argv[1]);
381 printf("debuglevel is %d\n", DEBUGLEVEL);
386 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
387 int argc, const char **argv)
390 return NT_STATUS_OK; /* NOTREACHED */
393 /* Built in rpcclient commands */
395 static struct cmd_set rpcclient_commands[] = {
397 { "GENERAL OPTIONS" },
399 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
400 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, "Get help on commands", "[command]" },
401 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, "Set debug level", "level" },
402 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, "List available commands on <pipe>", "pipe" },
403 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
404 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, "Exit program", "" },
409 static struct cmd_set separator_command[] = {
410 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, "----------------------" },
415 /* Various pipe commands */
417 extern struct cmd_set lsarpc_commands[];
418 extern struct cmd_set samr_commands[];
419 extern struct cmd_set spoolss_commands[];
420 extern struct cmd_set netlogon_commands[];
421 extern struct cmd_set srvsvc_commands[];
422 extern struct cmd_set dfs_commands[];
423 extern struct cmd_set reg_commands[];
424 extern struct cmd_set ds_commands[];
425 extern struct cmd_set echo_commands[];
427 static struct cmd_set *rpcclient_command_list[] = {
441 static void add_command_set(struct cmd_set *cmd_set)
443 struct cmd_list *entry;
445 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
446 DEBUG(0, ("out of memory\n"));
452 entry->cmd_set = cmd_set;
453 DLIST_ADD(cmd_list, entry);
458 * Call an rpcclient function, passing an argv array.
460 * @param cmd Command to run, as a single string.
462 static NTSTATUS do_cmd(struct cli_state *cli,
463 struct cmd_set *cmd_entry,
464 int argc, char **argv)
473 if (!(mem_ctx = talloc_init("do_cmd"))) {
474 DEBUG(0, ("talloc_init() failed\n"));
475 return NT_STATUS_UNSUCCESSFUL;
480 if (cmd_entry->pipe_idx == PI_NETLOGON) {
481 uchar trust_password[16];
483 if (!secrets_fetch_trust_account_password(lp_workgroup(),
486 return NT_STATUS_UNSUCCESSFUL;
489 if (!cli_nt_open_netlogon(cli, trust_password,
491 DEBUG(0, ("Could not initialise NETLOGON pipe\n"));
492 return NT_STATUS_UNSUCCESSFUL;
495 if (cmd_entry->pipe_idx != -1) {
496 if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
497 DEBUG(0, ("Could not initialise %s\n",
498 get_pipe_name_from_index(cmd_entry->pipe_idx)));
499 return NT_STATUS_UNSUCCESSFUL;
506 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
507 ntresult = cmd_entry->ntfn(cli, mem_ctx, argc, (const char **) argv);
508 if (!NT_STATUS_IS_OK(ntresult)) {
509 printf("result was %s\n", nt_errstr(ntresult));
512 wresult = cmd_entry->wfn( cli, mem_ctx, argc, (const char **) argv);
513 /* print out the DOS error */
514 if (!W_ERROR_IS_OK(wresult)) {
515 printf( "result was %s\n", dos_errstr(wresult));
517 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
523 if (cmd_entry->pipe_idx != -1)
524 cli_nt_session_close(cli);
526 talloc_destroy(mem_ctx);
533 * Process a command entered at the prompt or as part of -c
535 * @returns The NTSTATUS from running the command.
537 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
539 struct cmd_list *temp_list;
540 NTSTATUS result = NT_STATUS_OK;
545 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
546 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
547 return NT_STATUS_UNSUCCESSFUL;
551 /* Walk through a dlist of arrays of commands. */
552 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
553 struct cmd_set *temp_set = temp_list->cmd_set;
555 while (temp_set->name) {
556 if (strequal(argv[0], temp_set->name)) {
557 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
558 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
559 fprintf (stderr, "Invalid command\n");
563 result = do_cmd(cli, temp_set, argc, argv);
572 printf("command not found: %s\n", argv[0]);
577 if (!NT_STATUS_IS_OK(result)) {
578 printf("result was %s\n", nt_errstr(result));
583 /* NOTE: popt allocates the whole argv, including the
584 * strings, as a single block. So a single free is
585 * enough to release it -- we don't free the
586 * individual strings. rtfm. */
596 int main(int argc, char *argv[])
598 static int got_pass = 0;
599 BOOL interactive = True;
601 static char *cmdstr = "";
603 struct cli_state *cli;
607 static char *opt_authfile=NULL,
613 struct cmd_set **cmd_set;
614 struct in_addr server_ip;
617 /* make sure the vars that get altered (4th field) are in
618 a fixed location or certain compilers complain */
620 struct poptOption long_options[] = {
622 {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A', "File containing user credentials", "AUTHFILE"},
623 {"nopass", 'N', POPT_ARG_NONE, &got_pass, 'N', "Don't ask for a password"},
624 {"user", 'U', POPT_ARG_STRING, &opt_username, 'U', "Set the network username", "USER"},
625 {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W', "Set the domain name for user account", "DOMAIN"},
626 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
627 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Logfile to use instead of stdout", "LOGFILE" },
628 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
629 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
630 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
631 { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
635 ZERO_STRUCT(server_ip);
641 pc = poptGetContext("rpcclient", argc, (const char **) argv,
645 poptPrintHelp(pc, stderr, 0);
649 while((opt = poptGetNextOpt(pc)) != -1) {
652 /* only get the username, password, and domain from the file */
653 read_authfile (opt_authfile, username, password, domain);
654 if (strlen (password))
659 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
661 lp_set_logfile(logfile);
668 fstrcpy(username,opt_username);
670 if ((lp=strchr_m(username,'%'))) {
672 fstrcpy(password,lp+1);
674 memset(strchr_m(opt_username,'%') + 1, 'X',
680 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
681 fprintf(stderr, "%s not a valid IP address\n",
686 fstrcpy(domain, opt_domain);
691 /* Get server as remaining unparsed argument. Print usage if more
692 than one unparsed argument is present. */
694 server = poptGetArg(pc);
696 if (!server || poptGetArg(pc)) {
697 poptPrintHelp(pc, stderr, 0);
703 /* the following functions are part of the Samba debugging
704 facilities. See lib/debug.c */
705 setup_logging("rpcclient", interactive);
709 /* Load smb.conf file */
711 if (!lp_load(dyn_CONFIGFILE,True,False,False))
712 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
721 * from stdin if necessary
725 char *pass = getpass("Password:");
727 fstrcpy(password, pass);
731 if (!strlen(username) && !got_pass)
732 get_username(username);
734 nt_status = cli_full_connection(&cli, global_myname(), server,
735 opt_ipaddr ? &server_ip : NULL, 0,
740 if (!NT_STATUS_IS_OK(nt_status)) {
741 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
745 memset(password,'X',sizeof(password));
747 /* Load command lists */
749 cmd_set = rpcclient_command_list;
752 add_command_set(*cmd_set);
753 add_command_set(separator_command);
757 fetch_machine_sid(cli);
759 /* Do anything specified with -c */
764 while((cmd=next_command(&p)) != NULL) {
765 process_cmd(cli, cmd);
772 /* Loop around accepting commands */
778 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
780 line = smb_readline(prompt, NULL, completion_fn);
786 process_cmd(cli, line);