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));
246 /* Initialise client credentials for authenticated pipe access */
248 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
249 char* domain, char* password)
253 if (lp_encrypted_passwords()) {
254 pwd_make_lm_nt_16(&creds->pwd, password);
256 pwd_set_cleartext(&creds->pwd, password);
259 fstrcpy(creds->user_name, username);
260 fstrcpy(creds->domain, domain);
263 creds->pwd.null_pwd = True;
268 /* Display help on commands */
270 static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
271 int argc, char **argv)
273 struct cmd_list *tmp;
274 struct cmd_set *tmp_set;
279 printf("Usage: %s [command]\n", argv[0]);
283 /* Help on one command */
286 for (tmp = cmd_list; tmp; tmp = tmp->next) {
288 tmp_set = tmp->cmd_set;
290 while(tmp_set->name) {
291 if (strequal(argv[1], tmp_set->name)) {
292 if (tmp_set->usage &&
294 printf("%s\n", tmp_set->usage);
296 printf("No help for %s\n", tmp_set->name);
305 printf("No such command: %s\n", argv[1]);
309 /* List all commands */
311 for (tmp = cmd_list; tmp; tmp = tmp->next) {
313 tmp_set = tmp->cmd_set;
315 while(tmp_set->name) {
317 printf("%15s\t\t%s\n", tmp_set->name,
318 tmp_set->description);
327 /* Change the debug level */
329 static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
330 int argc, char **argv)
333 printf("Usage: %s [debuglevel]\n", argv[0]);
338 DEBUGLEVEL = atoi(argv[1]);
341 printf("debuglevel is %d\n", DEBUGLEVEL);
346 static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
347 int argc, char **argv)
350 return NT_STATUS_OK; /* NOTREACHED */
353 /* Build in rpcclient commands */
355 static struct cmd_set rpcclient_commands[] = {
357 { "GENERAL OPTIONS" },
359 { "help", cmd_help, NULL, "Get help on commands", "[command]" },
360 { "?", cmd_help, NULL, "Get help on commands", "[command]" },
361 { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" },
362 { "exit", cmd_quit, NULL, "Exit program", "" },
363 { "quit", cmd_quit, NULL, "Exit program", "" },
368 static struct cmd_set separator_command[] = {
369 { "---------------", NULL, NULL, "----------------------" },
374 /* Various pipe commands */
376 extern struct cmd_set lsarpc_commands[];
377 extern struct cmd_set samr_commands[];
378 extern struct cmd_set spoolss_commands[];
379 extern struct cmd_set netlogon_commands[];
380 extern struct cmd_set srvsvc_commands[];
381 extern struct cmd_set dfs_commands[];
382 extern struct cmd_set reg_commands[];
384 static struct cmd_set *rpcclient_command_list[] = {
396 static void add_command_set(struct cmd_set *cmd_set)
398 struct cmd_list *entry;
400 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
401 DEBUG(0, ("out of memory\n"));
407 entry->cmd_set = cmd_set;
408 DLIST_ADD(cmd_list, entry);
411 static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry,
414 char *p = cmd, **argv = NULL;
415 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
419 /* Count number of arguments first time through the loop then
420 allocate memory and strdup them. */
423 while(next_token(&p, buf, " ", sizeof(buf))) {
425 argv[argc] = strdup(buf);
433 /* Create argument list */
435 argv = (char **)malloc(sizeof(char *) * argc);
436 memset(argv, 0, sizeof(char *) * argc);
439 fprintf(stderr, "out of memory\n");
440 result = NT_STATUS_NO_MEMORY;
450 /* Call the function */
457 if (!(mem_ctx = talloc_init())) {
458 DEBUG(0, ("talloc_init() failed\n"));
465 if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
466 DEBUG(0, ("Could not initialise %s\n",
473 result = cmd_entry->fn(cli, mem_ctx, argc, argv);
478 cli_nt_session_close(cli);
480 talloc_destroy(mem_ctx);
483 fprintf (stderr, "Invalid command\n");
492 for (i = 0; i < argc; i++)
501 /* Process a command entered at the prompt or as part of -c */
503 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
505 struct cmd_list *temp_list;
509 NTSTATUS result = NT_STATUS_OK;
512 if (cmd[strlen(cmd) - 1] == '\n')
513 cmd[strlen(cmd) - 1] = '\0';
515 if (!next_token(&p, buf, " ", sizeof(buf))) {
519 /* strip the trainly \n if it exsists */
521 if (buf[len-1] == '\n')
524 /* Search for matching commands */
526 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
527 struct cmd_set *temp_set = temp_list->cmd_set;
529 while(temp_set->name) {
530 if (strequal(buf, temp_set->name)) {
532 result = do_cmd(cli, temp_set, cmd);
541 if (!found && buf[0]) {
542 printf("command not found: %s\n", buf);
546 if (!NT_STATUS_IS_OK(result)) {
547 printf("result was %s\n", get_nt_error_msg(result));
553 /************************************************************************/
554 struct cli_state *setup_connection(struct cli_state *cli, char *system_name,
555 struct ntuser_creds *creds)
557 struct in_addr dest_ip;
558 struct nmb_name calling, called;
560 extern pstring global_myname;
561 struct ntuser_creds anon;
563 /* Initialise cli_state information */
564 if (!cli_initialise(cli)) {
570 anon.pwd.null_pwd = 1;
574 cli_init_creds(cli, creds);
576 /* Establish a SMB connection */
577 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
581 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
582 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
584 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
585 &called, "IPC$", "IPC", False, True)) {
593 /* Print usage information */
594 static void usage(void)
596 printf("Usage: rpcclient server [options]\n");
598 printf("\t-A authfile file containing user credentials\n");
599 printf("\t-c \"command string\" execute semicolon separated cmds\n");
600 printf("\t-d debuglevel set the debuglevel\n");
601 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
602 printf("\t-h Print this help message.\n");
603 printf("\t-N don't ask for a password\n");
604 printf("\t-s configfile specify an alternative config file\n");
605 printf("\t-U username set the network username\n");
606 printf("\t-W domain set the domain name for user account\n");
612 int main(int argc, char *argv[])
616 extern pstring global_myname;
617 BOOL got_pass = False;
618 BOOL interactive = True;
622 servicesf = CONFIGFILE;
623 struct ntuser_creds creds;
624 struct cli_state cli;
629 struct cmd_set **cmd_set;
635 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:h")) != EOF) {
638 /* only get the username, password, and domain from the file */
639 read_authfile (optarg, username, password, domain);
640 if (strlen (password))
645 pstrcpy(cmdstr, optarg);
649 DEBUGLEVEL = atoi(optarg);
653 slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
662 pstrcpy(servicesf, optarg);
667 pstrcpy(username,optarg);
668 if ((lp=strchr_m(username,'%'))) {
670 pstrcpy(password,lp+1);
672 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
678 pstrcpy(domain, optarg);
697 if (strncmp("//", argv[0], 2) == 0 || strncmp("\\\\", argv[0], 2) == 0)
700 pstrcpy(server, argv[0]);
702 /* the following functions are part of the Samba debugging
703 facilities. See lib/debug.c */
704 setup_logging("rpcclient", interactive);
708 /* Load smb.conf file */
709 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
710 olddebug = DEBUGLEVEL;
711 if (!lp_load(servicesf,True,False,False)) {
712 fprintf(stderr, "Can't load %s\n", servicesf);
714 DEBUGLEVEL = olddebug;
720 get_myname((*global_myname)?NULL:global_myname);
721 strupper(global_myname);
724 * initialize the credentials struct. Get password
725 * from stdin if necessary
727 if (!strlen(username) && !got_pass)
728 get_username(username);
731 init_rpcclient_creds (&creds, username, domain, "");
732 pwd_read(&creds.pwd, "Enter Password: ", lp_encrypted_passwords());
735 init_rpcclient_creds (&creds, username, domain, password);
737 memset(password,'X',sizeof(password));
739 /* open a connection to the specified server */
741 if (!setup_connection (&cli, server, &creds)) {
745 /* There are no pointers in ntuser_creds struct so zero it out */
747 ZERO_STRUCTP (&creds);
749 /* Load command lists */
751 cmd_set = rpcclient_command_list;
754 add_command_set(*cmd_set);
755 add_command_set(separator_command);
759 fetch_machine_sid(&cli);
761 /* Do anything specified with -c */
766 while((cmd=next_command(&p)) != NULL) {
767 process_cmd(&cli, cmd);
773 /* Loop around accepting commands */
779 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
781 line = smb_readline(prompt, NULL, completion_fn);
787 process_cmd(&cli, line);