summaryrefslogtreecommitdiff
path: root/source3/rpcclient/rpcclient.c
diff options
context:
space:
mode:
Diffstat (limited to 'source3/rpcclient/rpcclient.c')
-rw-r--r--source3/rpcclient/rpcclient.c164
1 files changed, 147 insertions, 17 deletions
diff --git a/source3/rpcclient/rpcclient.c b/source3/rpcclient/rpcclient.c
index efc883ff9f..0cb4ba98c8 100644
--- a/source3/rpcclient/rpcclient.c
+++ b/source3/rpcclient/rpcclient.c
@@ -97,6 +97,70 @@ static char **completion_fn(char *text, int start, int end)
return matches;
}
+/***********************************************************************
+ * read in username/password credentials from a file
+ */
+static void read_authfile (
+ char *filename,
+ char* username,
+ char* password,
+ char* domain
+)
+{
+ 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;
+}
+
static char* next_command (char** cmdstr)
{
static pstring command;
@@ -117,6 +181,28 @@ static char* next_command (char** cmdstr)
return command;
}
+
+/**
+ * Find default username from environment variables.
+ *
+ * @param username fstring to receive username; not touched if none is
+ * known.
+ **/
+static void get_username (char *username)
+{
+ if (getenv("USER"))
+ fstrcpy(username,getenv("USER"));
+
+ if (*username == 0 && getenv("LOGNAME"))
+ fstrcpy(username,getenv("LOGNAME"));
+
+ if (*username == 0) {
+ fstrcpy(username,"GUEST");
+ }
+
+ return;
+}
+
/* Fetch the SID for this computer */
static void fetch_machine_sid(struct cli_state *cli)
@@ -336,7 +422,6 @@ extern struct cmd_set srvsvc_commands[];
extern struct cmd_set dfs_commands[];
extern struct cmd_set reg_commands[];
extern struct cmd_set ds_commands[];
-extern struct cmd_set echo_commands[];
static struct cmd_set *rpcclient_command_list[] = {
rpcclient_commands,
@@ -348,7 +433,6 @@ static struct cmd_set *rpcclient_command_list[] = {
srvsvc_commands,
dfs_commands,
reg_commands,
- echo_commands,
NULL
};
@@ -402,14 +486,13 @@ static NTSTATUS do_cmd(struct cli_state *cli,
if (!cli_nt_open_netlogon(cli, trust_password,
SEC_CHAN_WKSTA)) {
- DEBUG(0, ("Could not initialise NETLOGON pipe\n"));
+ DEBUG(0, ("Could not initialize NETLOGON pipe\n"));
return NT_STATUS_UNSUCCESSFUL;
}
} else {
if (cmd_entry->pipe_idx != -1) {
if (!cli_nt_session_open(cli, cmd_entry->pipe_idx)) {
- DEBUG(0, ("Could not initialise %s\n",
- get_pipe_name_from_index(cmd_entry->pipe_idx)));
+ DEBUG(0, ("Could not initialize pipe\n"));
return NT_STATUS_UNSUCCESSFUL;
}
}
@@ -509,12 +592,21 @@ out_free:
int main(int argc, char *argv[])
{
+ static int got_pass = 0;
BOOL interactive = True;
int opt;
- static char *cmdstr = NULL;
+ static char *cmdstr = "";
const char *server;
struct cli_state *cli;
- static char *opt_ipaddr=NULL;
+ fstring password="",
+ username="",
+ domain="";
+ static char *opt_authfile=NULL,
+ *opt_username=NULL,
+ *opt_domain=NULL,
+ *opt_logfile=NULL,
+ *opt_ipaddr=NULL;
+ pstring logfile;
struct cmd_set **cmd_set;
struct in_addr server_ip;
NTSTATUS nt_status;
@@ -524,12 +616,17 @@ out_free:
poptContext pc;
struct poptOption long_options[] = {
POPT_AUTOHELP
+ {"authfile", 'A', POPT_ARG_STRING, &opt_authfile, 'A', "File containing user credentials", "AUTHFILE"},
+ {"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", "USER"},
+ {"workgroup", 'W', POPT_ARG_STRING, &opt_domain, 'W', "Set the domain name for user account", "DOMAIN"},
{"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
+ {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l', "Logfile to use instead of stdout", "LOGFILE" },
{"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
- POPT_COMMON_SAMBA
- POPT_COMMON_CONNECTION
- POPT_COMMON_CREDENTIALS
- POPT_TABLEEND
+ { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_debug },
+ { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_configfile },
+ { NULL, 0, POPT_ARG_INCLUDE_TABLE, popt_common_version},
+ { NULL }
};
ZERO_STRUCT(server_ip);
@@ -548,13 +645,43 @@ out_free:
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 'U': {
+ char *lp;
+ fstrcpy(username,opt_username);
+
+ if ((lp=strchr_m(username,'%'))) {
+ *lp = 0;
+ fstrcpy(password,lp+1);
+ got_pass = 1;
+ memset(strchr_m(opt_username,'%') + 1, 'X',
+ strlen(password));
+ }
+ break;
+ }
case 'I':
if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
fprintf(stderr, "%s not a valid IP address\n",
opt_ipaddr);
return 1;
}
+ case 'W':
+ fstrcpy(domain, opt_domain);
+ break;
}
}
@@ -591,25 +718,28 @@ out_free:
* from stdin if necessary
*/
- if (!cmdline_auth_info.got_pass) {
+ if (!got_pass) {
char *pass = getpass("Password:");
if (pass) {
- pstrcpy(cmdline_auth_info.password, pass);
+ fstrcpy(password, pass);
}
}
+ if (!strlen(username) && !got_pass)
+ get_username(username);
+
nt_status = cli_full_connection(&cli, global_myname(), server,
opt_ipaddr ? &server_ip : NULL, 0,
"IPC$", "IPC",
- cmdline_auth_info.username, lp_workgroup(),
- cmdline_auth_info.password, 0, NULL);
+ username, domain,
+ password, 0, NULL);
if (!NT_STATUS_IS_OK(nt_status)) {
DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
return 1;
}
- memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
+ memset(password,'X',sizeof(password));
/* Load command lists */
@@ -624,7 +754,7 @@ out_free:
fetch_machine_sid(cli);
/* Do anything specified with -c */
- if (cmdstr && cmdstr[0]) {
+ if (cmdstr[0]) {
char *cmd;
char *p = cmdstr;