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"
28 /* List to hold groups of commands */
30 static struct cmd_list {
31 struct cmd_list *prev, *next;
32 struct cmd_set *cmd_set;
35 /****************************************************************************
36 handle completion of commands for readline
37 ****************************************************************************/
38 static char **completion_fn(char *text, int start, int end)
40 #define MAX_COMPLETIONS 100
43 struct cmd_list *commands = cmd_list;
46 /* FIXME!!! -- what to do when completing argument? */
47 /* for words not at the start of the line fallback
48 to filename completion */
53 /* make sure we have a list of valid commands */
57 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
58 if (!matches) return NULL;
60 matches[count++] = strdup(text);
61 if (!matches[0]) return NULL;
63 while (commands && count < MAX_COMPLETIONS-1)
65 if (!commands->cmd_set)
68 for (i=0; commands->cmd_set[i].name; i++)
70 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
71 commands->cmd_set[i].fn)
73 matches[count] = strdup(commands->cmd_set[i].name);
80 commands = commands->next;
85 SAFE_FREE(matches[0]);
86 matches[0] = strdup(matches[1]);
88 matches[count] = NULL;
92 /***********************************************************************
93 * read in username/password credentials from a file
95 static void read_authfile (
105 char *ptr, *val, *param;
107 if ((auth=sys_fopen(filename, "r")) == NULL)
109 printf ("ERROR: Unable to open credentials file!\n");
115 /* get a line from the file */
116 if (!fgets (buf, sizeof(buf), auth))
121 /* skip empty lines */
122 if ((len) && (buf[len-1]=='\n'))
130 /* break up the line into parameter & value.
131 will need to eat a little whitespace possibly */
133 if (!(ptr = strchr_m(buf, '=')))
138 /* eat leading white space */
139 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
142 if (strwicmp("password", param) == 0)
143 fstrcpy (password, val);
144 else if (strwicmp("username", param) == 0)
145 fstrcpy (username, val);
146 else if (strwicmp("domain", param) == 0)
147 fstrcpy (domain, val);
149 memset(buf, 0, sizeof(buf));
156 static char* next_command (char** cmdstr)
158 static pstring command;
161 if (!cmdstr || !(*cmdstr))
164 p = strchr_m(*cmdstr, ';');
167 pstrcpy(command, *cmdstr);
173 static void get_username (char *username)
176 pstrcpy(username,getenv("USER"));
178 if (*username == 0 && getenv("LOGNAME"))
179 pstrcpy(username,getenv("LOGNAME"));
181 if (*username == 0) {
182 pstrcpy(username,"GUEST");
188 /* Fetch the SID for this computer */
190 void fetch_machine_sid(struct cli_state *cli)
193 NTSTATUS result = NT_STATUS_OK;
194 uint32 info_class = 5;
196 static BOOL got_domain_sid;
199 if (got_domain_sid) return;
201 if (!(mem_ctx=talloc_init()))
203 DEBUG(0,("fetch_domain_sid: talloc_init returned NULL!\n"));
208 if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
209 fprintf(stderr, "could not initialise lsa pipe\n");
213 result = cli_lsa_open_policy(cli, mem_ctx, True,
214 SEC_RIGHTS_MAXIMUM_ALLOWED,
216 if (!NT_STATUS_IS_OK(result)) {
220 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
221 domain_name, &domain_sid);
222 if (!NT_STATUS_IS_OK(result)) {
226 got_domain_sid = True;
228 cli_lsa_close(cli, mem_ctx, &pol);
229 cli_nt_session_close(cli);
230 talloc_destroy(mem_ctx);
235 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
237 if (!NT_STATUS_IS_OK(result)) {
238 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
245 /* Display help on commands */
247 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
248 int argc, char **argv)
250 struct cmd_list *tmp;
251 struct cmd_set *tmp_set;
256 printf("Usage: %s [command]\n", argv[0]);
260 /* Help on one command */
263 for (tmp = cmd_list; tmp; tmp = tmp->next) {
265 tmp_set = tmp->cmd_set;
267 while(tmp_set->name) {
268 if (strequal(argv[1], tmp_set->name)) {
269 if (tmp_set->usage &&
271 printf("%s\n", tmp_set->usage);
273 printf("No help for %s\n", tmp_set->name);
282 printf("No such command: %s\n", argv[1]);
286 /* List all commands */
288 for (tmp = cmd_list; tmp; tmp = tmp->next) {
290 tmp_set = tmp->cmd_set;
292 while(tmp_set->name) {
294 printf("%15s\t\t%s\n", tmp_set->name,
295 tmp_set->description);
304 /* Change the debug level */
306 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
307 int argc, char **argv)
310 printf("Usage: %s [debuglevel]\n", argv[0]);
315 DEBUGLEVEL = atoi(argv[1]);
318 printf("debuglevel is %d\n", DEBUGLEVEL);
323 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
324 int argc, char **argv)
327 return NT_STATUS_OK; /* NOTREACHED */
330 /* Build in rpcclient commands */
332 static struct cmd_set rpcclient_commands[] = {
334 { "GENERAL OPTIONS" },
336 { "help", cmd_help, NULL, "Get help on commands", "[command]" },
337 { "?", cmd_help, NULL, "Get help on commands", "[command]" },
338 { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" },
339 { "exit", cmd_quit, NULL, "Exit program", "" },
340 { "quit", cmd_quit, NULL, "Exit program", "" },
345 static struct cmd_set separator_command[] = {
346 { "---------------", NULL, NULL, "----------------------" },
351 /* Various pipe commands */
353 extern struct cmd_set lsarpc_commands[];
354 extern struct cmd_set samr_commands[];
355 extern struct cmd_set spoolss_commands[];
356 extern struct cmd_set netlogon_commands[];
357 extern struct cmd_set srvsvc_commands[];
358 extern struct cmd_set dfs_commands[];
359 extern struct cmd_set reg_commands[];
361 static struct cmd_set *rpcclient_command_list[] = {
373 static void add_command_set(struct cmd_set *cmd_set)
375 struct cmd_list *entry;
377 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
378 DEBUG(0, ("out of memory\n"));
384 entry->cmd_set = cmd_set;
385 DLIST_ADD(cmd_list, entry);
388 static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry,
391 char *p = cmd, **argv = NULL;
392 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
396 /* Count number of arguments first time through the loop then
397 allocate memory and strdup them. */
400 while(next_token(&p, buf, " ", sizeof(buf))) {
402 argv[argc] = strdup(buf);
410 /* Create argument list */
412 argv = (char **)malloc(sizeof(char *) * argc);
413 memset(argv, 0, sizeof(char *) * argc);
416 fprintf(stderr, "out of memory\n");
417 result = NT_STATUS_NO_MEMORY;
427 /* Call the function */
434 if (!(mem_ctx = talloc_init())) {
435 DEBUG(0, ("talloc_init() failed\n"));
442 if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
443 DEBUG(0, ("Could not initialise %s\n",
450 result = cmd_entry->fn(cli, mem_ctx, argc, argv);
455 cli_nt_session_close(cli);
457 talloc_destroy(mem_ctx);
460 fprintf (stderr, "Invalid command\n");
469 for (i = 0; i < argc; i++)
478 /* Process a command entered at the prompt or as part of -c */
480 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
482 struct cmd_list *temp_list;
486 NTSTATUS result = NT_STATUS_OK;
489 if (cmd[strlen(cmd) - 1] == '\n')
490 cmd[strlen(cmd) - 1] = '\0';
492 if (!next_token(&p, buf, " ", sizeof(buf))) {
496 /* strip the trainly \n if it exsists */
498 if (buf[len-1] == '\n')
501 /* Search for matching commands */
503 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
504 struct cmd_set *temp_set = temp_list->cmd_set;
506 while(temp_set->name) {
507 if (strequal(buf, temp_set->name)) {
509 result = do_cmd(cli, temp_set, cmd);
518 if (!found && buf[0]) {
519 printf("command not found: %s\n", buf);
523 if (!NT_STATUS_IS_OK(result)) {
524 printf("result was %s\n", get_nt_error_msg(result));
531 /* Print usage information */
532 static void usage(void)
534 printf("Usage: rpcclient server [options]\n");
536 printf("\t-A authfile file containing user credentials\n");
537 printf("\t-c \"command string\" execute semicolon separated cmds\n");
538 printf("\t-d debuglevel set the debuglevel\n");
539 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
540 printf("\t-h Print this help message.\n");
541 printf("\t-N don't ask for a password\n");
542 printf("\t-s configfile specify an alternative config file\n");
543 printf("\t-U username set the network username\n");
544 printf("\t-W domain set the domain name for user account\n");
550 int main(int argc, char *argv[])
554 extern pstring global_myname;
555 BOOL got_pass = False;
556 BOOL interactive = True;
560 struct cli_state *cli;
565 struct cmd_set **cmd_set;
566 struct in_addr server_ip;
573 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:h")) != EOF) {
576 /* only get the username, password, and domain from the file */
577 read_authfile (optarg, username, password, domain);
578 if (strlen (password))
583 pstrcpy(cmdstr, optarg);
587 DEBUGLEVEL = atoi(optarg);
591 slprintf(logfile, sizeof(logfile) - 1, "%s.client", optarg);
592 lp_set_logfile(logfile);
601 pstrcpy(dyn_CONFIGFILE, optarg);
606 pstrcpy(username,optarg);
607 if ((lp=strchr_m(username,'%'))) {
609 pstrcpy(password,lp+1);
611 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
617 pstrcpy(domain, optarg);
636 if (strncmp("//", argv[0], 2) == 0 || strncmp("\\\\", argv[0], 2) == 0)
639 pstrcpy(server, argv[0]);
641 /* the following functions are part of the Samba debugging
642 facilities. See lib/debug.c */
643 setup_logging("rpcclient", interactive);
647 /* Load smb.conf file */
648 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
649 olddebug = DEBUGLEVEL;
650 if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
651 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
653 DEBUGLEVEL = olddebug;
657 get_myname((*global_myname)?NULL:global_myname);
658 strupper(global_myname);
661 /* resolve the IP address */
662 if (!resolve_name(server, &server_ip, 0x20)) {
663 DEBUG(1,("Unable to resolve server name\n"));
669 * from stdin if necessary
673 char *pass = getpass("Password:");
675 fstrcpy(password, pass);
679 if (!strlen(username) && !got_pass)
680 get_username(username);
682 nt_status = cli_full_connection(&cli, global_myname, server,
686 password, strlen(password));
688 if (!NT_STATUS_IS_OK(nt_status)) {
689 DEBUG(1,("Cannot connect to server. Error was %s\n", get_nt_error_msg(nt_status)));
693 memset(password,'X',sizeof(password));
695 /* Load command lists */
697 cmd_set = rpcclient_command_list;
700 add_command_set(*cmd_set);
701 add_command_set(separator_command);
705 fetch_machine_sid(cli);
707 /* Do anything specified with -c */
712 while((cmd=next_command(&p)) != NULL) {
713 process_cmd(cli, cmd);
720 /* Loop around accepting commands */
726 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
728 line = smb_readline(prompt, NULL, completion_fn);
734 process_cmd(cli, line);