2 Unix SMB/Netbios implementation.
6 Copyright (C) Tim Potter 2000
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 int DEBUGLEVEL;
26 extern fstring 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;
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 domain */
192 void fetch_domain_sid(struct cli_state *cli)
195 uint32 result = 0, 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 if ((result = cli_lsa_open_policy(cli, mem_ctx, True,
215 SEC_RIGHTS_MAXIMUM_ALLOWED,
216 &pol) != NT_STATUS_NOPROBLEMO)) {
220 if ((result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
221 domain_name, &domain_sid))
222 != NT_STATUS_NOPROBLEMO) {
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 (result != NT_STATUS_NOPROBLEMO) {
238 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
244 /* Initialise client credentials for authenticated pipe access */
246 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
247 char* domain, char* password)
251 if (lp_encrypted_passwords()) {
252 pwd_make_lm_nt_16(&creds->pwd, password);
254 pwd_set_cleartext(&creds->pwd, password);
257 fstrcpy(creds->user_name, username);
258 fstrcpy(creds->domain, domain);
261 creds->pwd.null_pwd = True;
266 /* Display help on commands */
268 static uint32 cmd_help(struct cli_state *cli, int argc, char **argv)
270 struct cmd_list *tmp;
271 struct cmd_set *tmp_set;
276 printf("Usage: %s [command]\n", argv[0]);
280 /* Help on one command */
283 for (tmp = cmd_list; tmp; tmp = tmp->next) {
285 tmp_set = tmp->cmd_set;
287 while(tmp_set->name) {
288 if (strequal(argv[1], tmp_set->name)) {
289 if (tmp_set->usage &&
291 printf("%s\n", tmp_set->usage);
293 printf("No help for %s\n", tmp_set->name);
302 printf("No such command: %s\n", argv[1]);
306 /* List all commands */
308 for (tmp = cmd_list; tmp; tmp = tmp->next) {
310 tmp_set = tmp->cmd_set;
312 while(tmp_set->name) {
314 printf("%15s\t\t%s\n", tmp_set->name,
315 tmp_set->description);
324 /* Change the debug level */
326 static uint32 cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
329 printf("Usage: %s [debuglevel]\n", argv[0]);
330 return NT_STATUS_NOPROBLEMO;
334 DEBUGLEVEL = atoi(argv[1]);
337 printf("debuglevel is %d\n", DEBUGLEVEL);
339 return NT_STATUS_NOPROBLEMO;
342 static uint32 cmd_quit(struct cli_state *cli, int argc, char **argv)
345 return NT_STATUS_NOPROBLEMO; /* NOTREACHED */
348 /* Build in rpcclient commands */
350 static struct cmd_set rpcclient_commands[] = {
352 { "GENERAL OPTIONS" },
354 { "help", cmd_help, "Get help on commands", "[command]" },
355 { "?", cmd_help, "Get help on commands", "[command]" },
356 { "debuglevel", cmd_debuglevel, "Set debug level", "level" },
357 { "exit", cmd_quit, "Exit program", "" },
358 { "quit", cmd_quit, "Exit program", "" },
363 static struct cmd_set separator_command[] = {
364 { "---------------", NULL, "----------------------" },
369 /* Various pipe commands */
371 extern struct cmd_set lsarpc_commands[];
372 extern struct cmd_set samr_commands[];
373 extern struct cmd_set spoolss_commands[];
374 extern struct cmd_set netlogon_commands[];
375 extern struct cmd_set srvsvc_commands[];
376 extern struct cmd_set dfs_commands[];
377 extern struct cmd_set reg_commands[];
379 static struct cmd_set *rpcclient_command_list[] = {
391 void add_command_set(struct cmd_set *cmd_set)
393 struct cmd_list *entry;
395 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
396 DEBUG(0, ("out of memory\n"));
402 entry->cmd_set = cmd_set;
403 DLIST_ADD(cmd_list, entry);
406 static uint32 do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *cmd)
408 char *p = cmd, **argv = NULL;
413 /* Count number of arguments first time through the loop then
414 allocate memory and strdup them. */
417 while(next_token(&p, buf, " ", sizeof(buf))) {
419 argv[argc] = strdup(buf);
427 /* Create argument list */
429 argv = (char **)malloc(sizeof(char *) * argc);
432 fprintf(stderr, "out of memory\n");
442 /* Call the function */
444 result = cmd_entry->fn(cli, argc, argv);
447 fprintf (stderr, "Invalid command\n");
448 result = NT_STATUS_INVALID_PARAMETER;
453 for (i = 0; i < argc; i++) {
462 /* Process a command entered at the prompt or as part of -c */
464 static uint32 process_cmd(struct cli_state *cli, char *cmd)
466 struct cmd_list *temp_list;
473 if (cmd[strlen(cmd) - 1] == '\n')
474 cmd[strlen(cmd) - 1] = '\0';
476 if (!next_token(&p, buf, " ", sizeof(buf))) {
480 /* strip the trainly \n if it exsists */
482 if (buf[len-1] == '\n')
485 /* Search for matching commands */
487 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
488 struct cmd_set *temp_set = temp_list->cmd_set;
490 while(temp_set->name) {
491 if (strequal(buf, temp_set->name)) {
493 result = do_cmd(cli, temp_set, cmd);
501 if (!found && buf[0]) {
502 printf("command not found: %s\n", buf);
507 printf("result was %s\n", get_nt_error_msg(result));
513 /************************************************************************/
514 struct cli_state *setup_connection(struct cli_state *cli, char *system_name,
515 struct ntuser_creds *creds)
517 struct in_addr dest_ip;
518 struct nmb_name calling, called;
520 extern pstring global_myname;
521 struct ntuser_creds anon;
523 /* Initialise cli_state information */
524 if (!cli_initialise(cli)) {
530 anon.pwd.null_pwd = 1;
534 cli_init_creds(cli, creds);
536 /* Establish a SMB connection */
537 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
541 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
542 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
544 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
545 &called, "IPC$", "IPC", False, True)) {
553 /* Print usage information */
554 static void usage(void)
556 printf("Usage: rpcclient [options] server\n");
558 printf("\t-A authfile file containing user credentials\n");
559 printf("\t-c \"command string\" execute semicolon separated cmds\n");
560 printf("\t-d debuglevel set the debuglevel\n");
561 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
562 printf("\t-h Print this help message.\n");
563 printf("\t-N don't ask for a password\n");
564 printf("\t-s configfile specify an alternative config file\n");
565 printf("\t-U username set the network username\n");
566 printf("\t-W domain set the domain name for user account\n");
572 int main(int argc, char *argv[])
576 extern pstring global_myname;
577 BOOL got_pass = False;
578 BOOL interactive = True;
582 servicesf = CONFIGFILE;
583 struct ntuser_creds creds;
584 struct cli_state cli;
589 struct cmd_set **cmd_set;
595 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:h")) != EOF) {
598 /* only get the username, password, and domain from the file */
599 read_authfile (optarg, username, password, domain);
600 if (strlen (password))
605 pstrcpy(cmdstr, optarg);
609 DEBUGLEVEL = atoi(optarg);
613 slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
622 pstrcpy(servicesf, optarg);
627 pstrcpy(username,optarg);
628 if ((lp=strchr_m(username,'%'))) {
630 pstrcpy(password,lp+1);
632 memset(strchr_m(optarg,'%')+1,'X',strlen(password));
638 pstrcpy(domain, optarg);
657 pstrcpy(server, argv[0]);
659 /* the following functions are part of the Samba debugging
660 facilities. See lib/debug.c */
661 setup_logging("rpcclient", interactive);
665 /* Load smb.conf file */
666 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
667 olddebug = DEBUGLEVEL;
668 if (!lp_load(servicesf,True,False,False)) {
669 fprintf(stderr, "Can't load %s\n", servicesf);
671 DEBUGLEVEL = olddebug;
677 get_myname((*global_myname)?NULL:global_myname);
678 strupper(global_myname);
681 * initialize the credentials struct. Get password
682 * from stdin if necessary
684 if (!strlen(username) && !got_pass)
685 get_username(username);
688 init_rpcclient_creds (&creds, username, domain, "");
689 pwd_read(&creds.pwd, "Enter Password: ", lp_encrypted_passwords());
692 init_rpcclient_creds (&creds, username, domain, password);
694 memset(password,'X',strlen(password));
696 /* open a connection to the specified server */
698 if (!setup_connection (&cli, server, &creds)) {
702 /* There are no pointers in ntuser_creds struct so zero it out */
704 ZERO_STRUCTP (&creds);
706 /* Load command lists */
708 cmd_set = rpcclient_command_list;
711 add_command_set(*cmd_set);
712 add_command_set(separator_command);
716 /* Do anything specified with -c */
721 while((cmd=next_command(&p)) != NULL) {
722 process_cmd(&cli, cmd);
728 /* Loop around accepting commands */
734 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
736 line = smb_readline(prompt, NULL, completion_fn);
742 process_cmd(&cli, line);