2 Unix SMB/Netbios implementation.
6 Copyright (C) Tim Potter 2000-2001
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"
26 extern pstring debugf;
30 /* List to hold groups of commands */
32 static struct cmd_list {
33 struct cmd_list *prev, *next;
34 struct cmd_set *cmd_set;
37 /****************************************************************************
38 handle completion of commands for readline
39 ****************************************************************************/
40 static char **completion_fn(char *text, int start, int end)
42 #define MAX_COMPLETIONS 100
45 struct cmd_list *commands = cmd_list;
48 /* FIXME!!! -- what to do when completing argument? */
49 /* for words not at the start of the line fallback
50 to filename completion */
55 /* make sure we have a list of valid commands */
59 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
60 if (!matches) return NULL;
62 matches[count++] = strdup(text);
63 if (!matches[0]) return NULL;
65 while (commands && count < MAX_COMPLETIONS-1)
67 if (!commands->cmd_set)
70 for (i=0; commands->cmd_set[i].name; i++)
72 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
73 commands->cmd_set[i].fn)
75 matches[count] = strdup(commands->cmd_set[i].name);
82 commands = commands->next;
87 SAFE_FREE(matches[0]);
88 matches[0] = strdup(matches[1]);
90 matches[count] = NULL;
94 /***********************************************************************
95 * read in username/password credentials from a file
97 static void read_authfile (
107 char *ptr, *val, *param;
109 if ((auth=sys_fopen(filename, "r")) == NULL)
111 printf ("ERROR: Unable to open credentials file!\n");
117 /* get a line from the file */
118 if (!fgets (buf, sizeof(buf), auth))
123 /* skip empty lines */
124 if ((len) && (buf[len-1]=='\n'))
132 /* break up the line into parameter & value.
133 will need to eat a little whitespace possibly */
135 if (!(ptr = strchr_m(buf, '=')))
140 /* eat leading white space */
141 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
144 if (strwicmp("password", param) == 0)
145 fstrcpy (password, val);
146 else if (strwicmp("username", param) == 0)
147 fstrcpy (username, val);
148 else if (strwicmp("domain", param) == 0)
149 fstrcpy (domain, val);
151 memset(buf, 0, sizeof(buf));
158 static char* next_command (char** cmdstr)
160 static pstring command;
163 if (!cmdstr || !(*cmdstr))
166 p = strchr_m(*cmdstr, ';');
169 pstrcpy(command, *cmdstr);
175 static void get_username (char *username)
178 pstrcpy(username,getenv("USER"));
180 if (*username == 0 && getenv("LOGNAME"))
181 pstrcpy(username,getenv("LOGNAME"));
183 if (*username == 0) {
184 pstrcpy(username,"GUEST");
190 /* Fetch the SID for this computer */
192 void fetch_machine_sid(struct cli_state *cli)
195 NTSTATUS result = NT_STATUS_OK;
196 uint32 info_class = 5;
198 static BOOL got_domain_sid;
201 if (got_domain_sid) return;
203 if (!(mem_ctx=talloc_init()))
205 DEBUG(0,("fetch_domain_sid: talloc_init returned NULL!\n"));
210 if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
211 fprintf(stderr, "could not initialise lsa pipe\n");
215 result = cli_lsa_open_policy(cli, mem_ctx, True,
216 SEC_RIGHTS_MAXIMUM_ALLOWED,
218 if (!NT_STATUS_IS_OK(result)) {
222 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
223 domain_name, &domain_sid);
224 if (!NT_STATUS_IS_OK(result)) {
228 got_domain_sid = True;
230 cli_lsa_close(cli, mem_ctx, &pol);
231 cli_nt_session_close(cli);
232 talloc_destroy(mem_ctx);
237 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
239 if (!NT_STATUS_IS_OK(result)) {
240 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
247 /* Display help on commands */
249 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
250 int argc, char **argv)
252 struct cmd_list *tmp;
253 struct cmd_set *tmp_set;
258 printf("Usage: %s [command]\n", argv[0]);
262 /* Help on one command */
265 for (tmp = cmd_list; tmp; tmp = tmp->next) {
267 tmp_set = tmp->cmd_set;
269 while(tmp_set->name) {
270 if (strequal(argv[1], tmp_set->name)) {
271 if (tmp_set->usage &&
273 printf("%s\n", tmp_set->usage);
275 printf("No help for %s\n", tmp_set->name);
284 printf("No such command: %s\n", argv[1]);
288 /* List all commands */
290 for (tmp = cmd_list; tmp; tmp = tmp->next) {
292 tmp_set = tmp->cmd_set;
294 while(tmp_set->name) {
296 printf("%15s\t\t%s\n", tmp_set->name,
297 tmp_set->description);
306 /* Change the debug level */
308 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
309 int argc, char **argv)
312 printf("Usage: %s [debuglevel]\n", argv[0]);
317 DEBUGLEVEL = atoi(argv[1]);
320 printf("debuglevel is %d\n", DEBUGLEVEL);
325 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
326 int argc, char **argv)
329 return NT_STATUS_OK; /* NOTREACHED */
332 /* Build in rpcclient commands */
334 static struct cmd_set rpcclient_commands[] = {
336 { "GENERAL OPTIONS" },
338 { "help", cmd_help, NULL, "Get help on commands", "[command]" },
339 { "?", cmd_help, NULL, "Get help on commands", "[command]" },
340 { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" },
341 { "exit", cmd_quit, NULL, "Exit program", "" },
342 { "quit", cmd_quit, NULL, "Exit program", "" },
347 static struct cmd_set separator_command[] = {
348 { "---------------", NULL, NULL, "----------------------" },
353 /* Various pipe commands */
355 extern struct cmd_set lsarpc_commands[];
356 extern struct cmd_set samr_commands[];
357 extern struct cmd_set spoolss_commands[];
358 extern struct cmd_set netlogon_commands[];
359 extern struct cmd_set srvsvc_commands[];
360 extern struct cmd_set dfs_commands[];
361 extern struct cmd_set reg_commands[];
363 static struct cmd_set *rpcclient_command_list[] = {
375 static void add_command_set(struct cmd_set *cmd_set)
377 struct cmd_list *entry;
379 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
380 DEBUG(0, ("out of memory\n"));
386 entry->cmd_set = cmd_set;
387 DLIST_ADD(cmd_list, entry);
390 static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry,
393 char *p = cmd, **argv = NULL;
394 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
398 /* Count number of arguments first time through the loop then
399 allocate memory and strdup them. */
402 while(next_token(&p, buf, " ", sizeof(buf))) {
404 argv[argc] = strdup(buf);
412 /* Create argument list */
414 argv = (char **)malloc(sizeof(char *) * argc);
415 memset(argv, 0, sizeof(char *) * argc);
418 fprintf(stderr, "out of memory\n");
419 result = NT_STATUS_NO_MEMORY;
429 /* Call the function */
436 if (!(mem_ctx = talloc_init())) {
437 DEBUG(0, ("talloc_init() failed\n"));
444 if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
445 DEBUG(0, ("Could not initialise %s\n",
452 result = cmd_entry->fn(cli, mem_ctx, argc, argv);
457 cli_nt_session_close(cli);
459 talloc_destroy(mem_ctx);
462 fprintf (stderr, "Invalid command\n");
471 for (i = 0; i < argc; i++)
480 /* Process a command entered at the prompt or as part of -c */
482 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
484 struct cmd_list *temp_list;
488 NTSTATUS result = NT_STATUS_OK;
491 if (cmd[strlen(cmd) - 1] == '\n')
492 cmd[strlen(cmd) - 1] = '\0';
494 if (!next_token(&p, buf, " ", sizeof(buf))) {
498 /* strip the trainly \n if it exsists */
500 if (buf[len-1] == '\n')
503 /* Search for matching commands */
505 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
506 struct cmd_set *temp_set = temp_list->cmd_set;
508 while(temp_set->name) {
509 if (strequal(buf, temp_set->name)) {
511 result = do_cmd(cli, temp_set, cmd);
520 if (!found && buf[0]) {
521 printf("command not found: %s\n", buf);
525 if (!NT_STATUS_IS_OK(result)) {
526 printf("result was %s\n", get_nt_error_msg(result));
533 /* Print usage information */
534 static void usage(void)
536 printf("Usage: rpcclient server [options]\n");
538 printf("\t-A authfile file containing user credentials\n");
539 printf("\t-c \"command string\" execute semicolon separated cmds\n");
540 printf("\t-d debuglevel set the debuglevel\n");
541 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
542 printf("\t-h Print this help message.\n");
543 printf("\t-N don't ask for a password\n");
544 printf("\t-s configfile specify an alternative config file\n");
545 printf("\t-U username set the network username\n");
546 printf("\t-W domain set the domain name for user account\n");
552 int main(int argc, char *argv[])
556 extern pstring global_myname;
557 BOOL got_pass = False;
558 BOOL interactive = True;
562 struct cli_state *cli;
567 struct cmd_set **cmd_set;
568 struct in_addr server_ip;
575 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:h")) != EOF) {
578 /* only get the username, password, and domain from the file */
579 read_authfile (optarg, username, password, domain);
580 if (strlen (password))
585 pstrcpy(cmdstr, optarg);
589 DEBUGLEVEL = atoi(optarg);
593 slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
602 pstrcpy(dyn_CONFIGFILE, optarg);
607 pstrcpy(username,optarg);
608 if ((lp=strchr_m(username,'%'))) {
610 pstrcpy(password,lp+1);
612 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
618 pstrcpy(domain, optarg);
637 if (strncmp("//", argv[0], 2) == 0 || strncmp("\\\\", argv[0], 2) == 0)
640 pstrcpy(server, argv[0]);
642 /* the following functions are part of the Samba debugging
643 facilities. See lib/debug.c */
644 setup_logging("rpcclient", interactive);
648 /* Load smb.conf file */
649 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
650 olddebug = DEBUGLEVEL;
651 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
652 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
654 DEBUGLEVEL = olddebug;
658 get_myname((*global_myname)?NULL:global_myname);
659 strupper(global_myname);
662 /* resolve the IP address */
663 if (!resolve_name(server, &server_ip, 0x20)) {
664 DEBUG(1,("Unable to resolve server name\n"));
670 * from stdin if necessary
674 char *pass = getpass("Password:");
676 fstrcpy(password, pass);
680 if (!strlen(username) && !got_pass)
681 get_username(username);
683 nt_status = cli_full_connection(&cli, global_myname, server,
687 password, strlen(password));
689 if (!NT_STATUS_IS_OK(nt_status)) {
690 DEBUG(1,("Cannot connect to server. Error was %s\n", get_nt_error_msg(nt_status)));
694 memset(password,'X',sizeof(password));
696 /* Load command lists */
698 cmd_set = rpcclient_command_list;
701 add_command_set(*cmd_set);
702 add_command_set(separator_command);
706 fetch_machine_sid(cli);
708 /* Do anything specified with -c */
713 while((cmd=next_command(&p)) != NULL) {
714 process_cmd(cli, cmd);
721 /* Loop around accepting commands */
727 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
729 line = smb_readline(prompt, NULL, completion_fn);
735 process_cmd(cli, line);