2 Unix SMB/CIFS implementation.
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Martin Pool 2003
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "rpcclient.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "../librpc/gen_ndr/ndr_netlogon.h"
27 #include "rpc_client/cli_netlogon.h"
31 static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
32 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
33 static unsigned int timeout = 0;
34 static enum dcerpc_transport_t default_transport = NCACN_NP;
36 struct user_auth_info *rpcclient_auth_info;
38 /* List to hold groups of commands.
40 * Commands are defined in a list of arrays: arrays are easy to
41 * statically declare, and lists are easier to dynamically extend.
44 static struct cmd_list {
45 struct cmd_list *prev, *next;
46 struct cmd_set *cmd_set;
49 /****************************************************************************
50 handle completion of commands for readline
51 ****************************************************************************/
52 static char **completion_fn(const char *text, int start, int end)
54 #define MAX_COMPLETIONS 100
57 struct cmd_list *commands = cmd_list;
60 /* FIXME!!! -- what to do when completing argument? */
61 /* for words not at the start of the line fallback
62 to filename completion */
67 /* make sure we have a list of valid commands */
72 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
77 matches[count++] = SMB_STRDUP(text);
83 while (commands && count < MAX_COMPLETIONS-1) {
84 if (!commands->cmd_set) {
88 for (i=0; commands->cmd_set[i].name; i++) {
89 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
90 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
91 commands->cmd_set[i].ntfn ) ||
92 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
93 commands->cmd_set[i].wfn))) {
94 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
95 if (!matches[count]) {
96 for (i = 0; i < count; i++) {
97 SAFE_FREE(matches[count]);
105 commands = commands->next;
110 SAFE_FREE(matches[0]);
111 matches[0] = SMB_STRDUP(matches[1]);
113 matches[count] = NULL;
117 static char *next_command (char **cmdstr)
122 if (!cmdstr || !(*cmdstr))
125 p = strchr_m(*cmdstr, ';');
128 command = SMB_STRDUP(*cmdstr);
137 /* Fetch the SID for this computer */
139 static void fetch_machine_sid(struct cli_state *cli)
141 struct policy_handle pol;
142 NTSTATUS result = NT_STATUS_OK;
143 static bool got_domain_sid;
145 struct rpc_pipe_client *lsapipe = NULL;
146 union lsa_PolicyInformation *info = NULL;
148 if (got_domain_sid) return;
150 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
151 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
155 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
157 if (!NT_STATUS_IS_OK(result)) {
158 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
162 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
163 SEC_FLAG_MAXIMUM_ALLOWED,
165 if (!NT_STATUS_IS_OK(result)) {
169 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
171 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
173 if (!NT_STATUS_IS_OK(result)) {
177 got_domain_sid = True;
178 sid_copy(&domain_sid, info->account_domain.sid);
180 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
181 TALLOC_FREE(lsapipe);
182 talloc_destroy(mem_ctx);
189 TALLOC_FREE(lsapipe);
192 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
194 if (!NT_STATUS_IS_OK(result)) {
195 fprintf(stderr, "error: %s\n", nt_errstr(result));
201 /* List the available commands on a given pipe */
203 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
204 int argc, const char **argv)
206 struct cmd_list *tmp;
207 struct cmd_set *tmp_set;
213 printf("Usage: %s <pipe>\n", argv[0]);
217 /* Help on one command */
219 for (tmp = cmd_list; tmp; tmp = tmp->next)
221 tmp_set = tmp->cmd_set;
223 if (!StrCaseCmp(argv[1], tmp_set->name))
225 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
229 while(tmp_set->name) {
230 printf("%30s", tmp_set->name);
237 /* drop out of the loop */
246 /* Display help on commands */
248 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
249 int argc, const char **argv)
251 struct cmd_list *tmp;
252 struct cmd_set *tmp_set;
257 printf("Usage: %s [command]\n", argv[0]);
261 /* Help on one command */
264 for (tmp = cmd_list; tmp; tmp = tmp->next) {
266 tmp_set = tmp->cmd_set;
268 while(tmp_set->name) {
269 if (strequal(argv[1], tmp_set->name)) {
270 if (tmp_set->usage &&
272 printf("%s\n", tmp_set->usage);
274 printf("No help for %s\n", tmp_set->name);
283 printf("No such command: %s\n", argv[1]);
287 /* List all commands */
289 for (tmp = cmd_list; tmp; tmp = tmp->next) {
291 tmp_set = tmp->cmd_set;
293 while(tmp_set->name) {
295 printf("%15s\t\t%s\n", tmp_set->name,
296 tmp_set->description ? tmp_set->description:
306 /* Change the debug level */
308 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
309 int argc, const char **argv)
312 printf("Usage: %s [debuglevel]\n", argv[0]);
317 DEBUGLEVEL = atoi(argv[1]);
320 printf("debuglevel is %d\n", DEBUGLEVEL);
325 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
326 int argc, const char **argv)
329 return NT_STATUS_OK; /* NOTREACHED */
332 static NTSTATUS cmd_set_ss_level(void)
334 struct cmd_list *tmp;
336 /* Close any existing connections not at this level. */
338 for (tmp = cmd_list; tmp; tmp = tmp->next) {
339 struct cmd_set *tmp_set;
341 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
342 if (tmp_set->rpc_pipe == NULL) {
346 if ((tmp_set->rpc_pipe->auth->auth_type
347 != pipe_default_auth_type)
348 || (tmp_set->rpc_pipe->auth->auth_level
349 != pipe_default_auth_level)) {
350 TALLOC_FREE(tmp_set->rpc_pipe);
351 tmp_set->rpc_pipe = NULL;
358 static NTSTATUS cmd_set_transport(void)
360 struct cmd_list *tmp;
362 /* Close any existing connections not at this level. */
364 for (tmp = cmd_list; tmp; tmp = tmp->next) {
365 struct cmd_set *tmp_set;
367 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
368 if (tmp_set->rpc_pipe == NULL) {
372 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
373 TALLOC_FREE(tmp_set->rpc_pipe);
374 tmp_set->rpc_pipe = NULL;
381 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
382 int argc, const char **argv)
384 const char *type = "NTLMSSP";
386 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
387 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
390 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
396 if (strequal(type, "NTLMSSP")) {
397 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
398 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
399 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
400 } else if (strequal(type, "SCHANNEL")) {
401 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
403 printf("unknown type %s\n", type);
404 return NT_STATUS_INVALID_LEVEL;
408 d_printf("Setting %s - sign\n", type);
410 return cmd_set_ss_level();
413 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
414 int argc, const char **argv)
416 const char *type = "NTLMSSP";
418 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
419 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
422 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
428 if (strequal(type, "NTLMSSP")) {
429 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
430 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
431 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
432 } else if (strequal(type, "SCHANNEL")) {
433 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
435 printf("unknown type %s\n", type);
436 return NT_STATUS_INVALID_LEVEL;
440 d_printf("Setting %s - sign and seal\n", type);
442 return cmd_set_ss_level();
445 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
446 int argc, const char **argv)
448 struct cmd_list *tmp;
451 printf("Usage: %s timeout\n", argv[0]);
456 timeout = atoi(argv[1]);
458 for (tmp = cmd_list; tmp; tmp = tmp->next) {
460 struct cmd_set *tmp_set;
462 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
463 if (tmp_set->rpc_pipe == NULL) {
467 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
472 printf("timeout is %d\n", timeout);
478 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
479 int argc, const char **argv)
481 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
482 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
484 return cmd_set_ss_level();
487 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
488 int argc, const char **argv)
490 d_printf("Setting schannel - sign and seal\n");
491 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
492 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
494 return cmd_set_ss_level();
497 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
498 int argc, const char **argv)
500 d_printf("Setting schannel - sign only\n");
501 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
502 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
504 return cmd_set_ss_level();
507 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
508 int argc, const char **argv)
513 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
517 if (strequal(argv[1], "NCACN_NP")) {
518 default_transport = NCACN_NP;
519 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
520 default_transport = NCACN_IP_TCP;
522 printf("transport type: %s unknown or not supported\n", argv[1]);
523 return NT_STATUS_NOT_SUPPORTED;
526 status = cmd_set_transport();
527 if (!NT_STATUS_IS_OK(status)) {
531 printf("default transport is now: %s\n", argv[1]);
536 /* Built in rpcclient commands */
538 static struct cmd_set rpcclient_commands[] = {
540 { "GENERAL OPTIONS" },
542 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
543 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
544 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
545 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
546 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
547 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
548 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
549 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
550 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
551 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, NULL, NULL, "Force RPC pipe connections to be sealed with 'schannel'. Assumes valid machine account to this domain controller.", "" },
552 { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'. Assumes valid machine account to this domain controller.", "" },
553 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
554 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
555 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
560 static struct cmd_set separator_command[] = {
561 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
566 /* Various pipe commands */
568 extern struct cmd_set lsarpc_commands[];
569 extern struct cmd_set samr_commands[];
570 extern struct cmd_set spoolss_commands[];
571 extern struct cmd_set netlogon_commands[];
572 extern struct cmd_set srvsvc_commands[];
573 extern struct cmd_set dfs_commands[];
574 extern struct cmd_set ds_commands[];
575 extern struct cmd_set echo_commands[];
576 extern struct cmd_set epmapper_commands[];
577 extern struct cmd_set shutdown_commands[];
578 extern struct cmd_set test_commands[];
579 extern struct cmd_set wkssvc_commands[];
580 extern struct cmd_set ntsvcs_commands[];
581 extern struct cmd_set drsuapi_commands[];
582 extern struct cmd_set eventlog_commands[];
584 static struct cmd_set *rpcclient_command_list[] = {
604 static void add_command_set(struct cmd_set *cmd_set)
606 struct cmd_list *entry;
608 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
609 DEBUG(0, ("out of memory\n"));
615 entry->cmd_set = cmd_set;
616 DLIST_ADD(cmd_list, entry);
621 * Call an rpcclient function, passing an argv array.
623 * @param cmd Command to run, as a single string.
625 static NTSTATUS do_cmd(struct cli_state *cli,
626 struct user_auth_info *auth_info,
627 struct cmd_set *cmd_entry,
628 struct dcerpc_binding *binding,
629 int argc, char **argv)
638 if (!(mem_ctx = talloc_init("do_cmd"))) {
639 DEBUG(0, ("talloc_init() failed\n"));
640 return NT_STATUS_NO_MEMORY;
645 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
646 switch (pipe_default_auth_type) {
647 case PIPE_AUTH_TYPE_NONE:
648 ntresult = cli_rpc_pipe_open_noauth_transport(
649 cli, default_transport,
650 cmd_entry->interface,
651 &cmd_entry->rpc_pipe);
653 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
654 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
655 cli, cmd_entry->interface,
657 pipe_default_auth_level,
658 get_cmdline_auth_info_domain(auth_info),
659 get_cmdline_auth_info_username(auth_info),
660 get_cmdline_auth_info_password(auth_info),
661 &cmd_entry->rpc_pipe);
663 case PIPE_AUTH_TYPE_NTLMSSP:
664 ntresult = cli_rpc_pipe_open_ntlmssp(
665 cli, cmd_entry->interface,
667 pipe_default_auth_level,
668 get_cmdline_auth_info_domain(auth_info),
669 get_cmdline_auth_info_username(auth_info),
670 get_cmdline_auth_info_password(auth_info),
671 &cmd_entry->rpc_pipe);
673 case PIPE_AUTH_TYPE_SCHANNEL:
674 ntresult = cli_rpc_pipe_open_schannel(
675 cli, cmd_entry->interface,
677 pipe_default_auth_level,
678 get_cmdline_auth_info_domain(auth_info),
679 &cmd_entry->rpc_pipe);
682 DEBUG(0, ("Could not initialise %s. Invalid "
684 get_pipe_name_from_syntax(
686 cmd_entry->interface),
687 pipe_default_auth_type ));
688 return NT_STATUS_UNSUCCESSFUL;
690 if (!NT_STATUS_IS_OK(ntresult)) {
691 DEBUG(0, ("Could not initialise %s. Error was %s\n",
692 get_pipe_name_from_syntax(
693 talloc_tos(), cmd_entry->interface),
694 nt_errstr(ntresult) ));
698 if (ndr_syntax_id_equal(cmd_entry->interface,
699 &ndr_table_netlogon.syntax_id)) {
700 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
701 enum netr_SchannelType sec_channel_type;
702 uchar trust_password[16];
703 const char *machine_account;
705 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
706 trust_password, &machine_account,
709 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
712 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
713 cli->desthost, /* server name */
714 get_cmdline_auth_info_domain(auth_info), /* domain */
715 global_myname(), /* client name */
716 machine_account, /* machine account name */
721 if (!NT_STATUS_IS_OK(ntresult)) {
722 DEBUG(0, ("Could not initialise credentials for %s.\n",
723 get_pipe_name_from_syntax(
725 cmd_entry->interface)));
733 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
734 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
735 if (!NT_STATUS_IS_OK(ntresult)) {
736 printf("result was %s\n", nt_errstr(ntresult));
739 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
740 /* print out the DOS error */
741 if (!W_ERROR_IS_OK(wresult)) {
742 printf( "result was %s\n", win_errstr(wresult));
744 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
749 talloc_destroy(mem_ctx);
756 * Process a command entered at the prompt or as part of -c
758 * @returns The NTSTATUS from running the command.
760 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
761 struct cli_state *cli,
762 struct dcerpc_binding *binding,
765 struct cmd_list *temp_list;
766 NTSTATUS result = NT_STATUS_OK;
771 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
772 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
773 return NT_STATUS_UNSUCCESSFUL;
777 /* Walk through a dlist of arrays of commands. */
778 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
779 struct cmd_set *temp_set = temp_list->cmd_set;
781 while (temp_set->name) {
782 if (strequal(argv[0], temp_set->name)) {
783 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
784 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
785 fprintf (stderr, "Invalid command\n");
789 result = do_cmd(cli, auth_info, temp_set,
790 binding, argc, argv);
799 printf("command not found: %s\n", argv[0]);
804 if (!NT_STATUS_IS_OK(result)) {
805 printf("result was %s\n", nt_errstr(result));
809 /* NOTE: popt allocates the whole argv, including the
810 * strings, as a single block. So a single free is
811 * enough to release it -- we don't free the
812 * individual strings. rtfm. */
821 int main(int argc, char *argv[])
824 static char *cmdstr = NULL;
826 struct cli_state *cli = NULL;
827 static char *opt_ipaddr=NULL;
828 struct cmd_set **cmd_set;
829 struct sockaddr_storage server_ss;
831 static int opt_port = 0;
832 fstring new_workgroup;
834 TALLOC_CTX *frame = talloc_stackframe();
836 struct dcerpc_binding *binding = NULL;
837 const char *binding_string = NULL;
838 char *user, *domain, *q;
840 /* make sure the vars that get altered (4th field) are in
841 a fixed location or certain compilers complain */
843 struct poptOption long_options[] = {
845 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
846 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
847 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
849 POPT_COMMON_CONNECTION
850 POPT_COMMON_CREDENTIALS
856 zero_sockaddr(&server_ss);
860 /* the following functions are part of the Samba debugging
861 facilities. See lib/debug.c */
862 setup_logging("rpcclient", True);
864 rpcclient_auth_info = user_auth_info_init(frame);
865 if (rpcclient_auth_info == NULL) {
868 popt_common_set_auth_info(rpcclient_auth_info);
872 pc = poptGetContext("rpcclient", argc, (const char **) argv,
876 poptPrintHelp(pc, stderr, 0);
880 while((opt = poptGetNextOpt(pc)) != -1) {
884 if (!interpret_string_addr(&server_ss,
887 fprintf(stderr, "%s not a valid IP address\n",
895 /* Get server as remaining unparsed argument. Print usage if more
896 than one unparsed argument is present. */
898 server = poptGetArg(pc);
900 if (!server || poptGetArg(pc)) {
901 poptPrintHelp(pc, stderr, 0);
915 /* save the workgroup...
917 FIXME!! do we need to do this for other options as well
918 (or maybe a generic way to keep lp_load() from overwriting
921 fstrcpy( new_workgroup, lp_workgroup() );
923 /* Load smb.conf file */
925 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
926 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
928 if ( strlen(new_workgroup) != 0 )
929 set_global_myworkgroup( new_workgroup );
933 * from stdin if necessary
936 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
937 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
942 set_cmdline_auth_info_getpass(rpcclient_auth_info);
944 if ((server[0] == '/' && server[1] == '/') ||
945 (server[0] == '\\' && server[1] == '\\')) {
949 nt_status = dcerpc_parse_binding(frame, server, &binding);
951 if (!NT_STATUS_IS_OK(nt_status)) {
953 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
954 strip_hostname(server));
955 if (!binding_string) {
960 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
961 if (!NT_STATUS_IS_OK(nt_status)) {
967 if (binding->transport == NCA_UNKNOWN) {
968 binding->transport = NCACN_NP;
971 if (binding->flags & DCERPC_SIGN) {
972 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
973 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
975 if (binding->flags & DCERPC_SEAL) {
976 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
977 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
979 if (binding->flags & DCERPC_AUTH_SPNEGO) {
980 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
982 if (binding->flags & DCERPC_AUTH_NTLM) {
983 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
985 if (binding->flags & DCERPC_AUTH_KRB5) {
986 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
989 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
990 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
991 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
993 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
994 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
997 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
998 SMB_ASSERT(user != NULL);
999 domain = talloc_strdup(frame, lp_workgroup());
1000 SMB_ASSERT(domain != NULL);
1001 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1003 if ((q = strchr_m(user,'\\'))) {
1005 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1006 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1010 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1011 opt_ipaddr ? &server_ss : NULL, opt_port,
1013 get_cmdline_auth_info_username(rpcclient_auth_info),
1014 get_cmdline_auth_info_domain(rpcclient_auth_info),
1015 get_cmdline_auth_info_password(rpcclient_auth_info),
1017 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
1020 if (!NT_STATUS_IS_OK(nt_status)) {
1021 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1026 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1027 nt_status = cli_cm_force_encryption(cli,
1028 get_cmdline_auth_info_username(rpcclient_auth_info),
1029 get_cmdline_auth_info_password(rpcclient_auth_info),
1030 get_cmdline_auth_info_domain(rpcclient_auth_info),
1032 if (!NT_STATUS_IS_OK(nt_status)) {
1038 #if 0 /* COMMENT OUT FOR TESTING */
1039 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1042 /* Load command lists */
1044 timeout = cli_set_timeout(cli, 10000);
1046 cmd_set = rpcclient_command_list;
1049 add_command_set(*cmd_set);
1050 add_command_set(separator_command);
1054 default_transport = binding->transport;
1056 fetch_machine_sid(cli);
1058 /* Do anything specified with -c */
1059 if (cmdstr && cmdstr[0]) {
1065 while((cmd=next_command(&p)) != NULL) {
1066 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1069 result = NT_STATUS_IS_ERR(cmd_result);
1075 /* Loop around accepting commands */
1080 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1085 if (line[0] != '\n')
1086 process_cmd(rpcclient_auth_info, cli, binding, line);