/*
- Unix SMB/Netbios implementation.
- Version 1.9.
- SMB client
- Copyright (C) Andrew Tridgell 1994-1998
-
+ Unix SMB/CIFS implementation.
+ RPC pipe client
+
+ Copyright (C) Tim Potter 2000-2001
+
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-#ifdef SYSLOG
-#undef SYSLOG
-#endif
-
#include "includes.h"
+#include "rpcclient.h"
-#ifndef REGISTER
-#define REGISTER 0
-#endif
-
-extern pstring debugf;
-extern pstring global_myname;
-
-extern pstring user_socket_options;
+DOM_SID domain_sid;
+/* List to hold groups of commands */
-extern int DEBUGLEVEL;
-
-
-extern file_info def_finfo;
-
-#define CNV_LANG(s) dos2unix_format(s,False)
-#define CNV_INPUT(s) unix2dos_format(s,True)
-
-static int process_tok(fstring tok);
-static void cmd_help(struct client_info *info);
-static void cmd_quit(struct client_info *info);
-
-static struct cli_state smbcli;
-struct cli_state *smb_cli = &smbcli;
-
-FILE *out_hnd;
+static struct cmd_list {
+ struct cmd_list *prev, *next;
+ struct cmd_set *cmd_set;
+} *cmd_list;
/****************************************************************************
-initialise smb client structure
+handle completion of commands for readline
****************************************************************************/
-void rpcclient_init(void)
+static char **completion_fn(char *text, int start, int end)
{
- memset((char *)smb_cli, '\0', sizeof(smb_cli));
- cli_initialise(smb_cli);
- smb_cli->capabilities |= CAP_NT_SMBS | CAP_STATUS32;
-}
+#define MAX_COMPLETIONS 100
+ char **matches;
+ int i, count=0;
+ struct cmd_list *commands = cmd_list;
+
+#if 0 /* JERRY */
+ /* FIXME!!! -- what to do when completing argument? */
+ /* for words not at the start of the line fallback
+ to filename completion */
+ if (start)
+ return NULL;
+#endif
-/****************************************************************************
-make smb client connection
-****************************************************************************/
-static BOOL rpcclient_connect(struct client_info *info)
-{
- struct nmb_name calling;
- struct nmb_name called;
+ /* make sure we have a list of valid commands */
+ if (!commands)
+ return NULL;
- make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type);
- make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0 );
+ matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
+ if (!matches) return NULL;
- if (!cli_establish_connection(smb_cli,
- info->dest_host, &info->dest_ip,
- &calling, &called,
- info->share, info->svc_type,
- False, True))
+ matches[count++] = strdup(text);
+ if (!matches[0]) return NULL;
+
+ while (commands && count < MAX_COMPLETIONS-1)
{
- DEBUG(0,("rpcclient_connect: connection failed\n"));
- cli_shutdown(smb_cli);
- return False;
+ if (!commands->cmd_set)
+ break;
+
+ for (i=0; commands->cmd_set[i].name; i++)
+ {
+ if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
+ commands->cmd_set[i].fn)
+ {
+ matches[count] = strdup(commands->cmd_set[i].name);
+ if (!matches[count])
+ return NULL;
+ count++;
+ }
+ }
+
+ commands = commands->next;
+
}
- return True;
+ if (count == 2) {
+ SAFE_FREE(matches[0]);
+ matches[0] = strdup(matches[1]);
+ }
+ matches[count] = NULL;
+ return matches;
}
-/****************************************************************************
-stop the smb connection(s?)
-****************************************************************************/
-static void rpcclient_stop(void)
+/***********************************************************************
+ * read in username/password credentials from a file
+ */
+static void read_authfile (
+ char *filename,
+ char* username,
+ char* password,
+ char* domain
+)
{
- cli_shutdown(smb_cli);
+ FILE *auth;
+ fstring buf;
+ uint16 len = 0;
+ char *ptr, *val, *param;
+
+ if ((auth=sys_fopen(filename, "r")) == NULL)
+ {
+ printf ("ERROR: Unable to open credentials file!\n");
+ return;
+ }
+
+ while (!feof(auth))
+ {
+ /* get a line from the file */
+ if (!fgets (buf, sizeof(buf), auth))
+ continue;
+
+ len = strlen(buf);
+
+ /* skip empty lines */
+ if ((len) && (buf[len-1]=='\n'))
+ {
+ buf[len-1] = '\0';
+ len--;
+ }
+ if (len == 0)
+ continue;
+
+ /* break up the line into parameter & value.
+ will need to eat a little whitespace possibly */
+ param = buf;
+ if (!(ptr = strchr_m(buf, '=')))
+ continue;
+ val = ptr+1;
+ *ptr = '\0';
+
+ /* eat leading white space */
+ while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
+ val++;
+
+ if (strwicmp("password", param) == 0)
+ fstrcpy (password, val);
+ else if (strwicmp("username", param) == 0)
+ fstrcpy (username, val);
+ else if (strwicmp("domain", param) == 0)
+ fstrcpy (domain, val);
+
+ memset(buf, 0, sizeof(buf));
+ }
+ fclose(auth);
+
+ return;
}
-/****************************************************************************
- This defines the commands supported by this client
- ****************************************************************************/
-struct
-{
- char *name;
- void (*fn)(struct client_info*);
- char *description;
-} commands[] =
-{
- {"regenum", cmd_reg_enum, "<keyname> Registry Enumeration (keys, values)"},
- {"regdeletekey",cmd_reg_delete_key, "<keyname> Registry Key Delete"},
- {"regcreatekey",cmd_reg_create_key, "<keyname> [keyclass] Registry Key Create"},
- {"regquerykey",cmd_reg_query_key, "<keyname> Registry Key Query"},
- {"regdeleteval",cmd_reg_delete_val, "<valname> Registry Value Delete"},
- {"regcreateval",cmd_reg_create_val, "<valname> <valtype> <value> Registry Key Create"},
- {"reggetsec", cmd_reg_get_key_sec, "<keyname> Registry Key Security"},
- {"regtestsec", cmd_reg_test_key_sec, "<keyname> Test Registry Key Security"},
- {"ntlogin", cmd_netlogon_login_test, "[username] [password] NT Domain login test"},
- {"wksinfo", cmd_wks_query_info, "Workstation Query Info"},
- {"srvinfo", cmd_srv_query_info, "Server Query Info"},
- {"srvsessions",cmd_srv_enum_sess, "List sessions on a server"},
- {"srvshares", cmd_srv_enum_shares, "List shares on a server"},
- {"srvconnections",cmd_srv_enum_conn, "List connections on a server"},
- {"srvfiles", cmd_srv_enum_files, "List files on a server"},
- {"lsaquery", cmd_lsa_query_info, "Query Info Policy (domain member or server)"},
- {"lookupsids", cmd_lsa_lookup_sids, "Resolve names from SIDs"},
- {"enumusers", cmd_sam_enum_users, "SAM User Database Query (experimental!)"},
- {"ntpass", cmd_sam_ntchange_pwd, "NT SAM Password Change"},
- {"samuser", cmd_sam_query_user, "<username> SAM User Query (experimental!)"},
- {"samtest", cmd_sam_test , "SAM User Encrypted RPC test (experimental!)"},
- {"enumaliases",cmd_sam_enum_aliases, "SAM Aliases Database Query (experimental!)"},
-#if 0
- {"enumgroups", cmd_sam_enum_groups, "SAM Group Database Query (experimental!)"},
-#endif
- {"samgroups", cmd_sam_query_groups, "SAM Group Database Query (experimental!)"},
- {"quit", cmd_quit, "logoff the server"},
- {"q", cmd_quit, "logoff the server"},
- {"exit", cmd_quit, "logoff the server"},
- {"bye", cmd_quit, "logoff the server"},
- {"help", cmd_help, "[command] give help on a command"},
- {"?", cmd_help, "[command] give help on a command"},
- {"!", NULL, "run a shell command on the local system"},
- {"", NULL, NULL}
-};
-
-/****************************************************************************
-do a (presumably graceful) quit...
-****************************************************************************/
-static void cmd_quit(struct client_info *info)
+static char* next_command (char** cmdstr)
{
- rpcclient_stop();
- exit(0);
+ static pstring command;
+ char *p;
+
+ if (!cmdstr || !(*cmdstr))
+ return NULL;
+
+ p = strchr_m(*cmdstr, ';');
+ if (p)
+ *p = '\0';
+ pstrcpy(command, *cmdstr);
+ *cmdstr = p;
+
+ return command;
}
-/****************************************************************************
-help
-****************************************************************************/
-static void cmd_help(struct client_info *info)
+static void get_username (char *username)
{
- int i=0,j;
- fstring buf;
-
- if (next_token(NULL,buf,NULL, sizeof(buf)))
- {
- if ((i = process_tok(buf)) >= 0)
- fprintf(out_hnd, "HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);
- }
- else
- while (commands[i].description)
- {
- for (j=0; commands[i].description && (j<5); j++) {
- fprintf(out_hnd, "%-15s",commands[i].name);
- i++;
- }
- fprintf(out_hnd, "\n");
- }
+ if (getenv("USER"))
+ pstrcpy(username,getenv("USER"));
+
+ if (*username == 0 && getenv("LOGNAME"))
+ pstrcpy(username,getenv("LOGNAME"));
+
+ if (*username == 0) {
+ pstrcpy(username,"GUEST");
+ }
+
+ return;
}
-/*******************************************************************
- lookup a command string in the list of commands, including
- abbreviations
- ******************************************************************/
-static int process_tok(fstring tok)
+/* Fetch the SID for this computer */
+
+static void fetch_machine_sid(struct cli_state *cli)
{
- int i = 0, matches = 0;
- int cmd=0;
- int tok_len = strlen(tok);
-
- while (commands[i].fn != NULL)
- {
- if (strequal(commands[i].name,tok))
+ POLICY_HND pol;
+ NTSTATUS result = NT_STATUS_OK;
+ uint32 info_class = 5;
+ fstring domain_name;
+ static BOOL got_domain_sid;
+ TALLOC_CTX *mem_ctx;
+
+ if (got_domain_sid) return;
+
+ if (!(mem_ctx=talloc_init()))
{
- matches = 1;
- cmd = i;
- break;
+ DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
+ goto error;
}
- else if (strnequal(commands[i].name, tok, tok_len))
- {
- matches++;
- cmd = i;
+
+
+ if (!cli_nt_session_open (cli, PIPE_LSARPC)) {
+ fprintf(stderr, "could not initialise lsa pipe\n");
+ goto error;
+ }
+
+ result = cli_lsa_open_policy(cli, mem_ctx, True,
+ SEC_RIGHTS_MAXIMUM_ALLOWED,
+ &pol);
+ if (!NT_STATUS_IS_OK(result)) {
+ goto error;
}
- i++;
- }
-
- if (matches == 0)
- return(-1);
- else if (matches == 1)
- return(cmd);
- else
- return(-2);
-}
-/****************************************************************************
-wait for keyboard activity, swallowing network packets
-****************************************************************************/
-static void wait_keyboard(struct cli_state *cli)
-{
- fd_set fds;
- struct timeval timeout;
-
- while (1)
- {
- FD_ZERO(&fds);
- FD_SET(cli->fd,&fds);
- FD_SET(fileno(stdin),&fds);
-
- timeout.tv_sec = 20;
- timeout.tv_usec = 0;
- sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
-
- if (FD_ISSET(fileno(stdin),&fds))
- return;
-
- /* We deliberately use receive_smb instead of
- client_receive_smb as we want to receive
- session keepalives and then drop them here.
- */
- if (FD_ISSET(cli->fd,&fds))
- receive_smb(cli->fd,cli->inbuf,0);
- }
+ result = cli_lsa_query_info_policy(cli, mem_ctx, &pol, info_class,
+ domain_name, &domain_sid);
+ if (!NT_STATUS_IS_OK(result)) {
+ goto error;
+ }
+
+ got_domain_sid = True;
+
+ cli_lsa_close(cli, mem_ctx, &pol);
+ cli_nt_session_close(cli);
+ talloc_destroy(mem_ctx);
+
+ return;
+
+ error:
+ fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
+
+ if (!NT_STATUS_IS_OK(result)) {
+ fprintf(stderr, "error: %s\n", nt_errstr(result));
+ }
+
+ exit(1);
}
-/****************************************************************************
- process commands from the client
-****************************************************************************/
-static void do_command(struct client_info *info, char *tok, char *line)
+/* List the available commands on a given pipe */
+
+static NTSTATUS cmd_listcommands(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+ int argc, char **argv)
{
+ struct cmd_list *tmp;
+ struct cmd_set *tmp_set;
int i;
- if ((i = process_tok(tok)) >= 0)
- {
- commands[i].fn(info);
- }
- else if (i == -2)
- {
- fprintf(out_hnd, "%s: command abbreviation ambiguous\n", CNV_LANG(tok));
- }
- else
+ /* Usage */
+
+ if (argc != 2) {
+ printf("Usage: %s <pipe>\n", argv[0]);
+ return NT_STATUS_OK;
+ }
+
+ /* Help on one command */
+
+ for (tmp = cmd_list; tmp; tmp = tmp->next)
{
- fprintf(out_hnd, "%s: command not found\n", CNV_LANG(tok));
- }
+ tmp_set = tmp->cmd_set;
+
+ if (!StrCaseCmp(argv[1], tmp_set->name))
+ {
+ printf("Available commands on the %s pipe:\n\n", tmp_set->name);
+
+ i = 0;
+ tmp_set++;
+ while(tmp_set->name) {
+ printf("%20s", tmp_set->name);
+ tmp_set++;
+ i++;
+ if (i%4 == 0)
+ printf("\n");
+ }
+
+ /* drop out of the loop */
+ break;
+ }
+ }
+ printf("\n\n");
+
+ return NT_STATUS_OK;
}
-/****************************************************************************
- process commands from the client
-****************************************************************************/
-static BOOL process( struct client_info *info, char *cmd_str)
+/* Display help on commands */
+
+static NTSTATUS cmd_help(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+ int argc, char **argv)
{
- pstring line;
- char *cmd = cmd_str;
+ struct cmd_list *tmp;
+ struct cmd_set *tmp_set;
- if (cmd[0] != '\0') while (cmd[0] != '\0')
- {
- char *p;
- fstring tok;
+ /* Usage */
- if ((p = strchr(cmd, ';')) == 0)
- {
- strncpy(line, cmd, 999);
- line[1000] = '\0';
- cmd += strlen(cmd);
- }
- else
- {
- if (p - cmd > 999) p = cmd + 999;
- strncpy(line, cmd, p - cmd);
- line[p - cmd] = '\0';
- cmd = p + 1;
- }
+ if (argc > 2) {
+ printf("Usage: %s [command]\n", argv[0]);
+ return NT_STATUS_OK;
+ }
- /* input language code to internal one */
- CNV_INPUT (line);
+ /* Help on one command */
- /* get the first part of the command */
- {
- char *ptr = line;
- if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
- }
+ if (argc == 2) {
+ for (tmp = cmd_list; tmp; tmp = tmp->next) {
+
+ tmp_set = tmp->cmd_set;
- do_command(info, tok, line);
- }
- else while (!feof(stdin))
- {
- fstring tok;
+ while(tmp_set->name) {
+ if (strequal(argv[1], tmp_set->name)) {
+ if (tmp_set->usage &&
+ tmp_set->usage[0])
+ printf("%s\n", tmp_set->usage);
+ else
+ printf("No help for %s\n", tmp_set->name);
- /* display a prompt */
- fprintf(out_hnd, "smb: %s> ", CNV_LANG(info->cur_dir));
- fflush(out_hnd);
+ return NT_STATUS_OK;
+ }
-#ifdef CLIX
- line[0] = wait_keyboard(smb_cli);
- /* this might not be such a good idea... */
- if ( line[0] == EOF)
- {
- break;
- }
-#else
- wait_keyboard(smb_cli);
-#endif
+ tmp_set++;
+ }
+ }
- /* and get a response */
-#ifdef CLIX
- fgets( &line[1],999, stdin);
-#else
- if (!fgets(line,1000,stdin))
- {
- break;
- }
-#endif
+ printf("No such command: %s\n", argv[1]);
+ return NT_STATUS_OK;
+ }
- /* input language code to internal one */
- CNV_INPUT (line);
+ /* List all commands */
- /* special case - first char is ! */
- if (*line == '!')
- {
- system(line + 1);
- continue;
- }
+ for (tmp = cmd_list; tmp; tmp = tmp->next) {
- fprintf(out_hnd, "%s\n", line);
+ tmp_set = tmp->cmd_set;
- /* get the first part of the command */
- {
- char *ptr = line;
- if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
- }
+ while(tmp_set->name) {
+
+ printf("%15s\t\t%s\n", tmp_set->name,
+ tmp_set->description ? tmp_set->description:
+ "");
- do_command(info, tok, line);
+ tmp_set++;
+ }
}
- return(True);
+ return NT_STATUS_OK;
}
-/****************************************************************************
-usage on the program
-****************************************************************************/
-static void usage(char *pname)
+/* Change the debug level */
+
+static NTSTATUS cmd_debuglevel(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+ int argc, char **argv)
{
- fprintf(out_hnd, "Usage: %s service <password> [-d debuglevel] [-l log] ",
- pname);
-
- fprintf(out_hnd, "\nVersion %s\n",VERSION);
- fprintf(out_hnd, "\t-d debuglevel set the debuglevel\n");
- fprintf(out_hnd, "\t-l log basename. Basename for log/debug files\n");
- fprintf(out_hnd, "\t-n netbios name. Use this name as my netbios name\n");
- fprintf(out_hnd, "\t-N don't ask for a password\n");
- fprintf(out_hnd, "\t-m max protocol set the max protocol level\n");
- fprintf(out_hnd, "\t-I dest IP use this IP to connect to\n");
- fprintf(out_hnd, "\t-E write messages to stderr instead of stdout\n");
- fprintf(out_hnd, "\t-U username set the network username\n");
- fprintf(out_hnd, "\t-W workgroup set the workgroup name\n");
- fprintf(out_hnd, "\t-c command string execute semicolon separated commands\n");
- fprintf(out_hnd, "\t-t terminal code terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
- fprintf(out_hnd, "\n");
+ if (argc > 2) {
+ printf("Usage: %s [debuglevel]\n", argv[0]);
+ return NT_STATUS_OK;
+ }
+
+ if (argc == 2) {
+ DEBUGLEVEL = atoi(argv[1]);
+ }
+
+ printf("debuglevel is %d\n", DEBUGLEVEL);
+
+ return NT_STATUS_OK;
}
-enum client_action
+static NTSTATUS cmd_quit(struct cli_state *cli, TALLOC_CTX *mem_ctx,
+ int argc, char **argv)
{
- CLIENT_NONE,
- CLIENT_IPC,
- CLIENT_SVC
+ exit(0);
+ return NT_STATUS_OK; /* NOTREACHED */
+}
+
+/* Build in rpcclient commands */
+
+static struct cmd_set rpcclient_commands[] = {
+
+ { "GENERAL OPTIONS" },
+
+ { "help", cmd_help, NULL, "Get help on commands", "[command]" },
+ { "?", cmd_help, NULL, "Get help on commands", "[command]" },
+ { "debuglevel", cmd_debuglevel, NULL, "Set debug level", "level" },
+ { "list", cmd_listcommands, NULL, "List available commands on <pipe>", "pipe" },
+ { "exit", cmd_quit, NULL, "Exit program", "" },
+ { "quit", cmd_quit, NULL, "Exit program", "" },
+
+ { NULL }
};
-/****************************************************************************
- main program
-****************************************************************************/
- int main(int argc,char *argv[])
+static struct cmd_set separator_command[] = {
+ { "---------------", NULL, NULL, "----------------------" },
+ { NULL }
+};
+
+
+/* Various pipe commands */
+
+extern struct cmd_set lsarpc_commands[];
+extern struct cmd_set samr_commands[];
+extern struct cmd_set spoolss_commands[];
+extern struct cmd_set netlogon_commands[];
+extern struct cmd_set srvsvc_commands[];
+extern struct cmd_set dfs_commands[];
+extern struct cmd_set reg_commands[];
+
+static struct cmd_set *rpcclient_command_list[] = {
+ rpcclient_commands,
+ lsarpc_commands,
+ samr_commands,
+ spoolss_commands,
+ netlogon_commands,
+ srvsvc_commands,
+ dfs_commands,
+ reg_commands,
+ NULL
+};
+
+static void add_command_set(struct cmd_set *cmd_set)
{
- BOOL interactive = True;
-
- printf("Please use rpcclient from the SAMBA_TNG cvs tag.\n");
- printf("Please refer to http://samba.org/cvs.html for details.\n");
- exit(-1);
-
- int opt;
- extern FILE *dbf;
- extern char *optarg;
- extern int optind;
- static pstring servicesf = CONFIGFILE;
- pstring term_code;
- char *p;
- BOOL got_pass = False;
- char *cmd_str="";
- mode_t myumask = 0755;
- enum client_action cli_action = CLIENT_NONE;
-
- struct client_info cli_info;
-
- pstring password; /* local copy only, if one is entered */
-
- out_hnd = stdout;
- fstrcpy(debugf, argv[0]);
-
- rpcclient_init();
-
-#ifdef KANJI
- pstrcpy(term_code, KANJI);
-#else /* KANJI */
- *term_code = 0;
-#endif /* KANJI */
-
- DEBUGLEVEL = 2;
-
- cli_info.put_total_size = 0;
- cli_info.put_total_time_ms = 0;
- cli_info.get_total_size = 0;
- cli_info.get_total_time_ms = 0;
-
- cli_info.dir_total = 0;
- cli_info.newer_than = 0;
- cli_info.archive_level = 0;
- cli_info.print_mode = 1;
-
- cli_info.translation = False;
- cli_info.recurse_dir = False;
- cli_info.lowercase = False;
- cli_info.prompt = True;
- cli_info.abort_mget = True;
-
- cli_info.dest_ip.s_addr = 0;
- cli_info.name_type = 0x20;
-
- pstrcpy(cli_info.cur_dir , "\\");
- pstrcpy(cli_info.file_sel, "");
- pstrcpy(cli_info.base_dir, "");
- pstrcpy(smb_cli->domain, "");
- pstrcpy(smb_cli->user_name, "");
- pstrcpy(cli_info.myhostname, "");
- pstrcpy(cli_info.dest_host, "");
-
- pstrcpy(cli_info.svc_type, "A:");
- pstrcpy(cli_info.share, "");
- pstrcpy(cli_info.service, "");
-
- ZERO_STRUCT(cli_info.dom.level3_sid);
- ZERO_STRUCT(cli_info.dom.level5_sid);
- fstrcpy(cli_info.dom.level3_dom, "");
- fstrcpy(cli_info.dom.level5_dom, "");
-
- smb_cli->nt_pipe_fnum = 0xffff;
-
- TimeInit();
- charset_initialise();
-
- myumask = umask(0);
- umask(myumask);
-
- if (!get_myname(global_myname))
- {
- fprintf(stderr, "Failed to get my hostname.\n");
+ struct cmd_list *entry;
+
+ if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
+ DEBUG(0, ("out of memory\n"));
+ return;
}
- if (getenv("USER"))
- {
- pstrcpy(smb_cli->user_name,getenv("USER"));
+ ZERO_STRUCTP(entry);
- /* modification to support userid%passwd syntax in the USER var
- 25.Aug.97, jdblair@uab.edu */
+ entry->cmd_set = cmd_set;
+ DLIST_ADD(cmd_list, entry);
+}
- if ((p=strchr(smb_cli->user_name,'%')))
- {
- *p = 0;
- pstrcpy(password,p+1);
- got_pass = True;
- memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
+static NTSTATUS do_cmd(struct cli_state *cli, struct cmd_set *cmd_entry,
+ char *cmd)
+{
+ char *p = cmd, **argv = NULL;
+ NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
+ pstring buf;
+ int argc = 0, i;
+
+ /* Count number of arguments first time through the loop then
+ allocate memory and strdup them. */
+
+ again:
+ while(next_token(&p, buf, " ", sizeof(buf))) {
+ if (argv) {
+ argv[argc] = strdup(buf);
}
- strupper(smb_cli->user_name);
+
+ argc++;
}
+
+ if (!argv) {
- password[0] = 0;
+ /* Create argument list */
- /* modification to support PASSWD environmental var
- 25.Aug.97, jdblair@uab.edu */
- if (getenv("PASSWD"))
- {
- pstrcpy(password,getenv("PASSWD"));
- }
+ argv = (char **)malloc(sizeof(char *) * argc);
+ memset(argv, 0, sizeof(char *) * argc);
- if (*smb_cli->user_name == 0 && getenv("LOGNAME"))
- {
- pstrcpy(smb_cli->user_name,getenv("LOGNAME"));
- strupper(smb_cli->user_name);
+ if (!argv) {
+ fprintf(stderr, "out of memory\n");
+ result = NT_STATUS_NO_MEMORY;
+ goto done;
+ }
+
+ p = cmd;
+ argc = 0;
+
+ goto again;
}
- if (argc < 2)
- {
- usage(argv[0]);
- exit(1);
- }
+ /* Call the function */
- if (*argv[1] != '-')
- {
+ if (cmd_entry->fn) {
+ TALLOC_CTX *mem_ctx;
- pstrcpy(cli_info.service, argv[1]);
- /* Convert any '/' characters in the service name to '\' characters */
- string_replace( cli_info.service, '/','\\');
- argc--;
- argv++;
+ /* Create mem_ctx */
- fprintf(out_hnd, "service: %s\n", cli_info.service);
+ if (!(mem_ctx = talloc_init())) {
+ DEBUG(0, ("talloc_init() failed\n"));
+ goto done;
+ }
- if (count_chars(cli_info.service,'\\') < 3)
- {
- usage(argv[0]);
- printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
- exit(1);
- }
+ /* Open pipe */
- /*
- if (count_chars(cli_info.service,'\\') > 3)
- {
- usage(pname);
- printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
- exit(1);
- }
- */
+ if (cmd_entry->pipe)
+ if (!cli_nt_session_open(cli, cmd_entry->pipe)) {
+ DEBUG(0, ("Could not initialise %s\n",
+ cmd_entry->pipe));
+ goto done;
+ }
- if (argc > 1 && (*argv[1] != '-'))
- {
- got_pass = True;
- pstrcpy(password,argv[1]);
- memset(argv[1],'X',strlen(argv[1]));
- argc--;
- argv++;
- }
+ /* Run command */
- cli_action = CLIENT_SVC;
- }
+ result = cmd_entry->fn(cli, mem_ctx, argc, argv);
- while ((opt = getopt(argc, argv,"s:O:M:S:i:N:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
- {
- switch (opt)
- {
- case 'm':
- {
- /* FIXME ... max_protocol seems to be funny here */
+ /* Cleanup */
- int max_protocol = 0;
- max_protocol = interpret_protocol(optarg,max_protocol);
- fprintf(stderr, "max protocol not currently supported\n");
- break;
- }
+ if (cmd_entry->pipe)
+ cli_nt_session_close(cli);
- case 'O':
- {
- pstrcpy(user_socket_options,optarg);
- break;
- }
+ talloc_destroy(mem_ctx);
- case 'S':
- {
- pstrcpy(cli_info.dest_host,optarg);
- strupper(cli_info.dest_host);
- cli_action = CLIENT_IPC;
- break;
- }
+ } else {
+ fprintf (stderr, "Invalid command\n");
+ goto done;
+ }
- case 'i':
- {
- extern pstring global_scope;
- pstrcpy(global_scope, optarg);
- strupper(global_scope);
- break;
- }
+ done:
+
+ /* Cleanup */
- case 'U':
- {
- char *lp;
- pstrcpy(smb_cli->user_name,optarg);
- if ((lp=strchr(smb_cli->user_name,'%')))
- {
- *lp = 0;
- pstrcpy(password,lp+1);
- got_pass = True;
- memset(strchr(optarg,'%')+1,'X',strlen(password));
- }
- break;
- }
+ if (argv) {
+ for (i = 0; i < argc; i++)
+ SAFE_FREE(argv[i]);
+
+ SAFE_FREE(argv);
+ }
+
+ return result;
+}
- case 'W':
- {
- pstrcpy(smb_cli->domain,optarg);
- break;
- }
+/* Process a command entered at the prompt or as part of -c */
- case 'E':
- {
- dbf = stderr;
- break;
- }
+static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
+{
+ struct cmd_list *temp_list;
+ BOOL found = False;
+ pstring buf;
+ char *p = cmd;
+ NTSTATUS result = NT_STATUS_OK;
+ int len = 0;
+
+ if (cmd[strlen(cmd) - 1] == '\n')
+ cmd[strlen(cmd) - 1] = '\0';
+
+ if (!next_token(&p, buf, " ", sizeof(buf))) {
+ return NT_STATUS_OK;
+ }
- case 'I':
- {
- cli_info.dest_ip = *interpret_addr2(optarg);
- if (zero_ip(cli_info.dest_ip))
- {
- exit(1);
- }
- break;
- }
+ /* strip the trainly \n if it exsists */
+ len = strlen(buf);
+ if (buf[len-1] == '\n')
+ buf[len-1] = '\0';
- case 'n':
- {
- fstrcpy(global_myname, optarg);
- break;
- }
+ /* Search for matching commands */
- case 'N':
- {
- got_pass = True;
- break;
- }
+ for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
+ struct cmd_set *temp_set = temp_list->cmd_set;
- case 'd':
- {
- if (*optarg == 'A')
- DEBUGLEVEL = 10000;
- else
- DEBUGLEVEL = atoi(optarg);
- break;
- }
+ while(temp_set->name) {
+ if (strequal(buf, temp_set->name)) {
+ found = True;
+ result = do_cmd(cli, temp_set, cmd);
- case 'l':
- {
- slprintf(debugf, sizeof(debugf)-1,
- "%s.client", optarg);
- interactive = False;
- break;
+ goto done;
}
+ temp_set++;
+ }
+ }
- case 'c':
- {
- cmd_str = optarg;
- got_pass = True;
- break;
- }
+ done:
+ if (!found && buf[0]) {
+ printf("command not found: %s\n", buf);
+ return NT_STATUS_OK;
+ }
- case 'h':
- {
- usage(argv[0]);
- exit(0);
- break;
- }
+ if (!NT_STATUS_IS_OK(result)) {
+ printf("result was %s\n", nt_errstr(result));
+ }
- case 's':
- {
- pstrcpy(servicesf, optarg);
- break;
- }
+ return result;
+}
- case 't':
- {
- pstrcpy(term_code, optarg);
- break;
- }
- default:
- {
- usage(argv[0]);
- exit(1);
- break;
+/* Main function */
+
+ int main(int argc, char *argv[])
+{
+ extern pstring global_myname;
+ static int got_pass = 0;
+ BOOL interactive = True;
+ int opt;
+ int olddebug;
+ static char *cmdstr = "";
+ const char *server;
+ struct cli_state *cli;
+ fstring password="",
+ username="",
+ domain="";
+ static char *opt_authfile=NULL,
+ *opt_username=NULL,
+ *opt_domain=NULL,
+ *opt_configfile=NULL,
+ *opt_logfile=NULL,
+ *opt_ipaddr=NULL;
+ pstring logfile;
+ struct cmd_set **cmd_set;
+ struct in_addr server_ip;
+ NTSTATUS nt_status;
+ extern BOOL AllowDebugChange;
+
+ /* make sure the vars that get altered (4th field) are in
+ a fixed location or certain compilers complain */
+ poptContext pc;
+ struct poptOption long_options[] = {
+ POPT_AUTOHELP
+ {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A', "File containing user credentials"},
+ {"conf", 's', POPT_ARG_STRING, &opt_configfile, 's', "Specify an alternative config file"},
+ {"nopass", 'N', POPT_ARG_NONE, &got_pass, 'N', "Don't ask for a password"},
+ {"user", 'U', POPT_ARG_STRING, &opt_username, 'U', "Set the network username"},
+ {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W', "Set the domain name for user account"},
+ {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds"},
+ {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Logfile to use instead of stdout"},
+ {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address"},
+ { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
+ { NULL }
+ };
+
+ setlinebuf(stdout);
+
+ DEBUGLEVEL = 1;
+ AllowDebugChange = False;
+
+ /* Parse options */
+
+ pc = poptGetContext("rpcclient", argc, (const char **) argv,
+ long_options, 0);
+
+ if (argc == 1) {
+ poptPrintHelp(pc, stderr, 0);
+ return 0;
+ }
+
+ while((opt = poptGetNextOpt(pc)) != -1) {
+ switch (opt) {
+ case 'A':
+ /* only get the username, password, and domain from the file */
+ read_authfile (opt_authfile, username, password, domain);
+ if (strlen (password))
+ got_pass = 1;
+ break;
+
+ case 'l':
+ slprintf(logfile, sizeof(logfile) - 1, "%s.client",
+ opt_logfile);
+ lp_set_logfile(logfile);
+ interactive = False;
+ break;
+
+ case 's':
+ pstrcpy(dyn_CONFIGFILE, opt_configfile);
+ break;
+
+ case 'U': {
+ char *lp;
+
+ pstrcpy(username,opt_username);
+
+ if ((lp=strchr_m(username,'%'))) {
+ *lp = 0;
+ pstrcpy(password,lp+1);
+ got_pass = 1;
+ memset(strchr_m(opt_username,'%') + 1, 'X',
+ strlen(password));
}
+ break;
+ }
+ case 'I':
+ if (!inet_aton(opt_ipaddr, &server_ip)) {
+ fprintf(stderr, "%s not a valid IP address\n",
+ opt_ipaddr);
+ return 1;
+ }
+ case 'W':
+ pstrcpy(domain, opt_domain);
+ break;
}
}
- setup_logging(debugf, interactive);
+ /* Get server as remaining unparsed argument. Print usage if more
+ than one unparsed argument is present. */
- if (cli_action == CLIENT_NONE)
- {
- usage(argv[0]);
- exit(1);
+ server = poptGetArg(pc);
+
+ if (!server || poptGetArg(pc)) {
+ poptPrintHelp(pc, stderr, 0);
+ return 1;
+ }
+
+ poptFreeContext(pc);
+
+ /* the following functions are part of the Samba debugging
+ facilities. See lib/debug.c */
+ setup_logging("rpcclient", interactive);
+ if (!interactive)
+ reopen_logs();
+
+ /* Load smb.conf file */
+ /* FIXME! How to get this DEBUGLEVEL to last over lp_load()? */
+ olddebug = DEBUGLEVEL;
+ if (!lp_load(dyn_CONFIGFILE,True,False,False)) {
+ fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
}
+ DEBUGLEVEL = olddebug;
+
+ load_interfaces();
+ get_myname((*global_myname)?NULL:global_myname);
strupper(global_myname);
- fstrcpy(cli_info.myhostname, global_myname);
- DEBUG(3,("%s client started (version %s)\n",timestring(False),VERSION));
+ /* Resolve the IP address */
- if (!lp_load(servicesf,True, False, False))
- {
- fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
+ if (!opt_ipaddr && !resolve_name(server, &server_ip, 0x20)) {
+ DEBUG(1,("Unable to resolve %s\n", server));
+ return 1;
+ }
+
+ /*
+ * Get password
+ * from stdin if necessary
+ */
+
+ if (!got_pass) {
+ char *pass = getpass("Password:");
+ if (pass) {
+ fstrcpy(password, pass);
+ }
+ }
+
+ if (!strlen(username) && !got_pass)
+ get_username(username);
+
+ nt_status = cli_full_connection(&cli, global_myname, server,
+ &server_ip, 0,
+ "IPC$", "IPC",
+ username, domain,
+ password, 0);
+
+ if (!NT_STATUS_IS_OK(nt_status)) {
+ DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
+ return 1;
}
- codepage_initialise(lp_client_code_page());
+ memset(password,'X',sizeof(password));
- if (*smb_cli->domain == 0) pstrcpy(smb_cli->domain,lp_workgroup());
+ /* Load command lists */
- load_interfaces();
+ cmd_set = rpcclient_command_list;
- if (cli_action == CLIENT_IPC)
- {
- pstrcpy(cli_info.share, "IPC$");
- pstrcpy(cli_info.svc_type, "IPC");
+ while(*cmd_set) {
+ add_command_set(*cmd_set);
+ add_command_set(separator_command);
+ cmd_set++;
}
- fstrcpy(cli_info.mach_acct, cli_info.myhostname);
- strupper(cli_info.mach_acct);
- fstrcat(cli_info.mach_acct, "$");
+ fetch_machine_sid(cli);
+
+ /* Do anything specified with -c */
+ if (cmdstr[0]) {
+ char *cmd;
+ char *p = cmdstr;
+
+ while((cmd=next_command(&p)) != NULL) {
+ process_cmd(cli, cmd);
+ }
+
+ cli_shutdown(cli);
+ return 0;
+ }
- /* set the password cache info */
- if (got_pass)
- {
- if (password[0] == 0)
- {
- pwd_set_nullpwd(&(smb_cli->pwd));
- }
- else
- {
- pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
- }
- }
- else
- {
- pwd_read(&(smb_cli->pwd), "Enter Password:", True);
- }
+ /* Loop around accepting commands */
- /* paranoia: destroy the local copy of the password */
- memset((char *)password, '\0', sizeof(password));
+ while(1) {
+ pstring prompt;
+ char *line;
- /* establish connections. nothing to stop these being re-established. */
- rpcclient_connect(&cli_info);
+ slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
- DEBUG(5,("rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd));
- if (smb_cli->fd <= 0)
- {
- fprintf(stderr, "warning: connection could not be established to %s<%02x>\n",
- cli_info.dest_host, cli_info.name_type);
- fprintf(stderr, "this version of smbclient may crash if you proceed\n");
- exit(-1);
- }
+ line = smb_readline(prompt, NULL, completion_fn);
- switch (cli_action)
- {
- case CLIENT_IPC:
- {
- process(&cli_info, cmd_str);
+ if (line == NULL)
break;
- }
- default:
- {
- fprintf(stderr, "unknown client action requested\n");
- break;
- }
+ if (line[0] != '\n')
+ process_cmd(cli, line);
}
-
- rpcclient_stop();
-
- return(0);
+
+ cli_shutdown(cli);
+ return 0;
}