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.
25 extern fstring debugf;
29 /* List to hold groups of commands */
31 static struct cmd_list {
32 struct cmd_list *prev, *next;
33 struct cmd_set *cmd_set;
36 /****************************************************************************
37 handle completion of commands for readline
38 ****************************************************************************/
39 static char **completion_fn(char *text, int start, int end)
41 #define MAX_COMPLETIONS 100
44 struct cmd_list *commands = cmd_list;
47 /* FIXME!!! -- what to do when completing argument? */
48 /* for words not at the start of the line fallback
49 to filename completion */
54 /* make sure we have a list of valid commands */
58 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
59 if (!matches) return NULL;
61 matches[count++] = strdup(text);
62 if (!matches[0]) return NULL;
64 while (commands && count < MAX_COMPLETIONS-1)
66 if (!commands->cmd_set)
69 for (i=0; commands->cmd_set[i].name; i++)
71 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
72 commands->cmd_set[i].fn)
74 matches[count] = strdup(commands->cmd_set[i].name);
81 commands = commands->next;
86 SAFE_FREE(matches[0]);
87 matches[0] = strdup(matches[1]);
89 matches[count] = NULL;
93 /***********************************************************************
94 * read in username/password credentials from a file
96 static void read_authfile (
106 char *ptr, *val, *param;
108 if ((auth=sys_fopen(filename, "r")) == NULL)
110 printf ("ERROR: Unable to open credentials file!\n");
116 /* get a line from the file */
117 if (!fgets (buf, sizeof(buf), auth))
122 /* skip empty lines */
123 if ((len) && (buf[len-1]=='\n'))
131 /* break up the line into parameter & value.
132 will need to eat a little whitespace possibly */
134 if (!(ptr = strchr_m(buf, '=')))
139 /* eat leading white space */
140 while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
143 if (strwicmp("password", param) == 0)
144 fstrcpy (password, val);
145 else if (strwicmp("username", param) == 0)
146 fstrcpy (username, val);
147 else if (strwicmp("domain", param) == 0)
148 fstrcpy (domain, val);
150 memset(buf, 0, sizeof(buf));
157 static char* next_command (char** cmdstr)
159 static pstring command;
162 if (!cmdstr || !(*cmdstr))
165 p = strchr_m(*cmdstr, ';');
168 pstrcpy(command, *cmdstr);
174 static void get_username (char *username)
177 pstrcpy(username,getenv("USER"));
179 if (*username == 0 && getenv("LOGNAME"))
180 pstrcpy(username,getenv("LOGNAME"));
182 if (*username == 0) {
183 pstrcpy(username,"GUEST");
189 /* Fetch the SID for this domain */
191 void fetch_domain_sid(struct cli_state *cli)
194 NTSTATUS result = NT_STATUS_OK;
195 uint32 info_class = 5;
197 static BOOL got_domain_sid;
200 if (got_domain_sid) return;
202 if (!(mem_ctx=talloc_init()))
204 DEBUG(0,("fetch_domain_sid: talloc_init returned NULL!\n"));
209 if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
210 fprintf(stderr, "could not initialise lsa pipe\n");
214 result = cli_lsa_open_policy(cli, mem_ctx, True,
215 SEC_RIGHTS_MAXIMUM_ALLOWED,
217 if (!NT_STATUS_IS_OK(result)) {
221 result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
222 domain_name, &domain_sid);
223 if (!NT_STATUS_IS_OK(result)) {
227 got_domain_sid = True;
229 cli_lsa_close(cli, mem_ctx, &pol);
230 cli_nt_session_close(cli);
231 talloc_destroy(mem_ctx);
236 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
238 if (!NT_STATUS_IS_OK(result)) {
239 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
245 /* Initialise client credentials for authenticated pipe access */
247 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
248 char* domain, char* password)
252 if (lp_encrypted_passwords()) {
253 pwd_make_lm_nt_16(&creds->pwd, password);
255 pwd_set_cleartext(&creds->pwd, password);
258 fstrcpy(creds->user_name, username);
259 fstrcpy(creds->domain, domain);
262 creds->pwd.null_pwd = True;
267 /* Display help on commands */
269 static NTSTATUS cmd_help(struct cli_state *cli, int argc, char **argv)
271 struct cmd_list *tmp;
272 struct cmd_set *tmp_set;
277 printf("Usage: %s [command]\n", argv[0]);
281 /* Help on one command */
284 for (tmp = cmd_list; tmp; tmp = tmp->next) {
286 tmp_set = tmp->cmd_set;
288 while(tmp_set->name) {
289 if (strequal(argv[1], tmp_set->name)) {
290 if (tmp_set->usage &&
292 printf("%s\n", tmp_set->usage);
294 printf("No help for %s\n", tmp_set->name);
303 printf("No such command: %s\n", argv[1]);
307 /* List all commands */
309 for (tmp = cmd_list; tmp; tmp = tmp->next) {
311 tmp_set = tmp->cmd_set;
313 while(tmp_set->name) {
315 printf("%15s\t\t%s\n", tmp_set->name,
316 tmp_set->description);
325 /* Change the debug level */
327 static NTSTATUS cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
330 printf("Usage: %s [debuglevel]\n", argv[0]);
335 DEBUGLEVEL = atoi(argv[1]);
338 printf("debuglevel is %d\n", DEBUGLEVEL);
343 static NTSTATUS cmd_quit(struct cli_state *cli, int argc, char **argv)
346 return NT_STATUS_OK; /* NOTREACHED */
349 /* Build in rpcclient commands */
351 static struct cmd_set rpcclient_commands[] = {
353 { "GENERAL OPTIONS" },
355 { "help", cmd_help, "Get help on commands", "[command]" },
356 { "?", cmd_help, "Get help on commands", "[command]" },
357 { "debuglevel", cmd_debuglevel, "Set debug level", "level" },
358 { "exit", cmd_quit, "Exit program", "" },
359 { "quit", cmd_quit, "Exit program", "" },
364 static struct cmd_set separator_command[] = {
365 { "---------------", NULL, "----------------------" },
370 /* Various pipe commands */
372 extern struct cmd_set lsarpc_commands[];
373 extern struct cmd_set samr_commands[];
374 extern struct cmd_set spoolss_commands[];
375 extern struct cmd_set netlogon_commands[];
376 extern struct cmd_set srvsvc_commands[];
377 extern struct cmd_set dfs_commands[];
378 extern struct cmd_set reg_commands[];
380 static struct cmd_set *rpcclient_command_list[] = {
392 void add_command_set(struct cmd_set *cmd_set)
394 struct cmd_list *entry;
396 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
397 DEBUG(0, ("out of memory\n"));
403 entry->cmd_set = cmd_set;
404 DLIST_ADD(cmd_list, entry);
407 static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *cmd)
409 char *p = cmd, **argv = NULL;
414 /* Count number of arguments first time through the loop then
415 allocate memory and strdup them. */
418 while(next_token(&p, buf, " ", sizeof(buf))) {
420 argv[argc] = strdup(buf);
428 /* Create argument list */
430 argv = (char **)malloc(sizeof(char *) * argc);
433 fprintf(stderr, "out of memory\n");
434 return NT_STATUS_NO_MEMORY;
443 /* Call the function */
445 result = cmd_entry->fn(cli, argc, argv);
448 fprintf (stderr, "Invalid command\n");
449 result = NT_STATUS_INVALID_PARAMETER;
454 for (i = 0; i < argc; i++) {
463 /* Process a command entered at the prompt or as part of -c */
465 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
467 struct cmd_list *temp_list;
471 NTSTATUS result = NT_STATUS_OK;
474 if (cmd[strlen(cmd) - 1] == '\n')
475 cmd[strlen(cmd) - 1] = '\0';
477 if (!next_token(&p, buf, " ", sizeof(buf))) {
481 /* strip the trainly \n if it exsists */
483 if (buf[len-1] == '\n')
486 /* Search for matching commands */
488 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
489 struct cmd_set *temp_set = temp_list->cmd_set;
491 while(temp_set->name) {
492 if (strequal(buf, temp_set->name)) {
494 result = do_cmd(cli, temp_set, cmd);
502 if (!found && buf[0]) {
503 printf("command not found: %s\n", buf);
507 if (!NT_STATUS_IS_OK(result)) {
508 printf("result was %s\n", get_nt_error_msg(result));
514 /************************************************************************/
515 struct cli_state *setup_connection(struct cli_state *cli, char *system_name,
516 struct ntuser_creds *creds)
518 struct in_addr dest_ip;
519 struct nmb_name calling, called;
521 extern pstring global_myname;
522 struct ntuser_creds anon;
524 /* Initialise cli_state information */
525 if (!cli_initialise(cli)) {
531 anon.pwd.null_pwd = 1;
535 cli_init_creds(cli, creds);
537 /* Establish a SMB connection */
538 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
542 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
543 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
545 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
546 &called, "IPC$", "IPC", False, True)) {
554 /* Print usage information */
555 static void usage(void)
557 printf("Usage: rpcclient server [options]\n");
559 printf("\t-A authfile file containing user credentials\n");
560 printf("\t-c \"command string\" execute semicolon separated cmds\n");
561 printf("\t-d debuglevel set the debuglevel\n");
562 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
563 printf("\t-h Print this help message.\n");
564 printf("\t-N don't ask for a password\n");
565 printf("\t-s configfile specify an alternative config file\n");
566 printf("\t-U username set the network username\n");
567 printf("\t-W domain set the domain name for user account\n");
573 int main(int argc, char *argv[])
577 extern pstring global_myname;
578 BOOL got_pass = False;
579 BOOL interactive = True;
583 servicesf = CONFIGFILE;
584 struct ntuser_creds creds;
585 struct cli_state cli;
590 struct cmd_set **cmd_set;
596 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:h")) != EOF) {
599 /* only get the username, password, and domain from the file */
600 read_authfile (optarg, username, password, domain);
601 if (strlen (password))
606 pstrcpy(cmdstr, optarg);
610 DEBUGLEVEL = atoi(optarg);
614 slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
623 pstrcpy(servicesf, optarg);
628 pstrcpy(username,optarg);
629 if ((lp=strchr_m(username,'%'))) {
631 pstrcpy(password,lp+1);
633 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
639 pstrcpy(domain, optarg);
658 if (strncmp("//", argv[0], 2) == 0 ||
659 strncmp("\\\\", argv[0], 2) == 0)
662 pstrcpy(server, argv[0]);
664 /* the following functions are part of the Samba debugging
665 facilities. See lib/debug.c */
666 setup_logging("rpcclient", interactive);
670 /* Load smb.conf file */
671 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
672 olddebug = DEBUGLEVEL;
673 if (!lp_load(servicesf,True,False,False)) {
674 fprintf(stderr, "Can't load %s\n", servicesf);
676 DEBUGLEVEL = olddebug;
682 get_myname((*global_myname)?NULL:global_myname);
683 strupper(global_myname);
686 * initialize the credentials struct. Get password
687 * from stdin if necessary
689 if (!strlen(username) && !got_pass)
690 get_username(username);
693 init_rpcclient_creds (&creds, username, domain, "");
694 pwd_read(&creds.pwd, "Enter Password: ", lp_encrypted_passwords());
697 init_rpcclient_creds (&creds, username, domain, password);
699 memset(password,'X',strlen(password));
701 /* open a connection to the specified server */
703 if (!setup_connection (&cli, server, &creds)) {
707 /* There are no pointers in ntuser_creds struct so zero it out */
709 ZERO_STRUCTP (&creds);
711 /* Load command lists */
713 cmd_set = rpcclient_command_list;
716 add_command_set(*cmd_set);
717 add_command_set(separator_command);
721 /* Do anything specified with -c */
726 while((cmd=next_command(&p)) != NULL) {
727 process_cmd(&cli, cmd);
733 /* Loop around accepting commands */
739 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
741 line = smb_readline(prompt, NULL, completion_fn);
747 process_cmd(&cli, line);