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 (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(*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 */
191 void fetch_domain_sid(struct cli_state *cli)
194 uint32 result = 0, 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 if ((result = cli_lsa_open_policy(cli, mem_ctx, True,
214 SEC_RIGHTS_MAXIMUM_ALLOWED,
215 &pol) != NT_STATUS_NOPROBLEMO)) {
219 if ((result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
220 domain_name, &domain_sid))
221 != NT_STATUS_NOPROBLEMO) {
225 got_domain_sid = True;
227 cli_lsa_close(cli, mem_ctx, &pol);
228 cli_nt_session_close(cli);
229 talloc_destroy(mem_ctx);
234 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
236 if (result != NT_STATUS_NOPROBLEMO) {
237 fprintf(stderr, "error: %s\n", get_nt_error_msg(result));
243 /* Initialise client credentials for authenticated pipe access */
245 void init_rpcclient_creds(struct ntuser_creds *creds, char* username,
246 char* domain, char* password)
250 if (lp_encrypted_passwords()) {
251 pwd_make_lm_nt_16(&creds->pwd, password);
253 pwd_set_cleartext(&creds->pwd, password);
256 fstrcpy(creds->user_name, username);
257 fstrcpy(creds->domain, domain);
261 static uint32 cmd_help(struct cli_state *cli, int argc, char **argv)
263 struct cmd_list *temp_list;
265 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
266 struct cmd_set *temp_set = temp_list->cmd_set;
268 while(temp_set->name) {
269 printf("%15s\t\t%s\n", temp_set->name,
270 temp_set->description);
278 static uint32 cmd_debuglevel(struct cli_state *cli, int argc, char **argv)
281 printf("Usage: %s [debuglevel]\n", argv[0]);
282 return NT_STATUS_NOPROBLEMO;
286 DEBUGLEVEL = atoi(argv[1]);
289 printf("debuglevel is %d\n", DEBUGLEVEL);
291 return NT_STATUS_NOPROBLEMO;
294 static uint32 cmd_quit(struct cli_state *cli, int argc, char **argv)
297 return NT_STATUS_NOPROBLEMO; /* NOTREACHED */
300 /* Build in rpcclient commands */
302 static struct cmd_set rpcclient_commands[] = {
303 { "GENERAL OPTIONS", NULL, "" },
304 { "help", cmd_help, "Print list of commands" },
305 { "?", cmd_help, "Print list of commands" },
306 { "debuglevel", cmd_debuglevel, "Set debug level" },
307 { "exit", cmd_quit, "Exit program" },
308 { "quit", cmd_quit, "Exit program" },
313 static struct cmd_set separator_command[] = {
314 { "---------------", NULL, "----------------------" },
319 /* Various pipe commands */
321 extern struct cmd_set lsarpc_commands[];
322 extern struct cmd_set samr_commands[];
323 extern struct cmd_set spoolss_commands[];
324 extern struct cmd_set netlogon_commands[];
325 extern struct cmd_set srvsvc_commands[];
326 extern struct cmd_set dfs_commands[];
328 static struct cmd_set *rpcclient_command_list[] = {
339 void add_command_set(struct cmd_set *cmd_set)
341 struct cmd_list *entry;
343 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
344 DEBUG(0, ("out of memory\n"));
350 entry->cmd_set = cmd_set;
351 DLIST_ADD(cmd_list, entry);
354 static uint32 do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry, char *cmd)
356 char *p = cmd, **argv = NULL;
361 next_token(&p, buf, " ", sizeof(buf));
363 /* Count number of arguments first time through the loop then
364 allocate memory and strdup them. */
367 while(next_token(NULL, buf, " ", sizeof(buf))) {
369 argv[argc] = strdup(buf);
377 /* Create argument list */
379 argv = (char **)malloc(sizeof(char *) * argc);
382 fprintf(stderr, "out of memoryx\n");
387 next_token(&p, buf, " ", sizeof(buf));
388 argv[0] = strdup(buf);
394 /* Call the function */
396 result = cmd_entry->fn(cli, argc, argv);
399 fprintf (stderr, "Invalid command\n");
400 result = NT_STATUS_INVALID_PARAMETER;
405 for (i = 0; i < argc; i++) {
414 /* Process a command entered at the prompt or as part of -c */
416 static uint32 process_cmd(struct cli_state *cli, char *cmd)
418 struct cmd_list *temp_list;
425 if (cmd[strlen(cmd) - 1] == '\n')
426 cmd[strlen(cmd) - 1] = '\0';
428 if (!next_token(&p, buf, " ", sizeof(buf))) {
432 /* strip the trainly \n if it exsists */
434 if (buf[len-1] == '\n')
437 /* Search for matching commands */
439 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
440 struct cmd_set *temp_set = temp_list->cmd_set;
442 while(temp_set->name) {
443 if (strequal(buf, temp_set->name)) {
445 result = do_cmd(cli, temp_set, cmd);
453 if (!found && buf[0]) {
454 printf("command not found: %s\n", buf);
459 printf("result was %s\n", get_nt_error_msg(result));
465 /************************************************************************/
466 struct cli_state *setup_connection(struct cli_state *cli, char *system_name,
467 struct ntuser_creds *creds)
469 struct in_addr dest_ip;
470 struct nmb_name calling, called;
472 extern pstring global_myname;
473 struct ntuser_creds anon;
475 /* Initialise cli_state information */
476 if (!cli_initialise(cli)) {
482 anon.pwd.null_pwd = 1;
486 cli_init_creds(cli, creds);
488 /* Establish a SMB connection */
489 if (!resolve_srv_name(system_name, dest_host, &dest_ip)) {
493 make_nmb_name(&called, dns_to_netbios_name(dest_host), 0x20);
494 make_nmb_name(&calling, dns_to_netbios_name(global_myname), 0);
496 if (!cli_establish_connection(cli, dest_host, &dest_ip, &calling,
497 &called, "IPC$", "IPC", False, True)) {
505 /* Print usage information */
506 static void usage(char *pname)
508 printf("Usage: %s server [options]\n", pname);
510 printf("\t-A authfile file containing user credentials\n");
511 printf("\t-c \"command string\" execute semicolon separated cmds\n");
512 printf("\t-d debuglevel set the debuglevel\n");
513 printf("\t-l logfile name of logfile to use as opposed to stdout\n");
514 printf("\t-h Print this help message.\n");
515 printf("\t-N don't ask for a password\n");
516 printf("\t-s configfile specify an alternative config file\n");
517 printf("\t-U username set the network username\n");
518 printf("\t-W domain set the domain name for user account\n");
524 int main(int argc, char *argv[])
528 extern pstring global_myname;
529 BOOL got_pass = False;
530 BOOL interactive = True;
534 servicesf = CONFIGFILE;
535 struct ntuser_creds creds;
536 struct cli_state cli;
541 struct cmd_set **cmd_set;
543 charset_initialise();
554 pstrcpy(server, argv[1]);
559 while ((opt = getopt(argc, argv, "A:s:Nd:U:W:c:l:")) != EOF) {
562 /* only get the username, password, and domain from the file */
563 read_authfile (optarg, username, password, domain);
564 if (strlen (password))
569 pstrcpy(cmdstr, optarg);
573 DEBUGLEVEL = atoi(optarg);
577 slprintf(debugf, sizeof(debugf) - 1, "%s.client", optarg);
586 pstrcpy(servicesf, optarg);
591 pstrcpy(username,optarg);
592 if ((lp=strchr(username,'%'))) {
594 pstrcpy(password,lp+1);
596 memset(strchr(optarg,'%')+1,'X',strlen(password));
602 pstrcpy(domain, optarg);
612 /* the following functions are part of the Samba debugging
613 facilities. See lib/debug.c */
614 setup_logging (argv[0], interactive);
618 /* Load smb.conf file */
619 /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
620 olddebug = DEBUGLEVEL;
621 if (!lp_load(servicesf,True,False,False)) {
622 fprintf(stderr, "Can't load %s\n", servicesf);
624 DEBUGLEVEL = olddebug;
626 codepage_initialise(lp_client_code_page());
631 get_myname((*global_myname)?NULL:global_myname);
632 strupper(global_myname);
635 * initialize the credentials struct. Get password
636 * from stdin if necessary
638 if (!strlen(username))
639 get_username (username);
642 init_rpcclient_creds (&creds, username, domain, "");
643 pwd_read(&creds.pwd, "Enter Password: ", lp_encrypted_passwords());
646 init_rpcclient_creds (&creds, username, domain, password);
648 memset(password,'X',strlen(password));
650 /* open a connection to the specified server */
652 if (!setup_connection (&cli, server, &creds)) {
656 /* There are no pointers in ntuser_creds struct so zero it out */
658 ZERO_STRUCTP (&creds);
660 /* Load command lists */
662 cmd_set = rpcclient_command_list;
665 add_command_set(*cmd_set);
666 add_command_set(separator_command);
670 /* Do anything specified with -c */
675 while((cmd=next_command(&p)) != NULL) {
676 process_cmd(&cli, cmd);
682 /* Loop around accepting commands */
688 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
690 line = smb_readline(prompt, NULL, completion_fn);
696 process_cmd(&cli, line);