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 "rpc_client/cli_lsarpc.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
28 #include "rpc_client/cli_netlogon.h"
30 struct dom_sid domain_sid
;
32 static enum dcerpc_AuthType pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
33 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type
= 0;
34 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
35 static unsigned int timeout
= 0;
36 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
38 struct user_auth_info
*rpcclient_auth_info
;
40 /* List to hold groups of commands.
42 * Commands are defined in a list of arrays: arrays are easy to
43 * statically declare, and lists are easier to dynamically extend.
46 static struct cmd_list
{
47 struct cmd_list
*prev
, *next
;
48 struct cmd_set
*cmd_set
;
51 /****************************************************************************
52 handle completion of commands for readline
53 ****************************************************************************/
54 static char **completion_fn(const char *text
, int start
, int end
)
56 #define MAX_COMPLETIONS 100
59 struct cmd_list
*commands
= cmd_list
;
62 /* FIXME!!! -- what to do when completing argument? */
63 /* for words not at the start of the line fallback
64 to filename completion */
69 /* make sure we have a list of valid commands */
74 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
79 matches
[count
++] = SMB_STRDUP(text
);
85 while (commands
&& count
< MAX_COMPLETIONS
-1) {
86 if (!commands
->cmd_set
) {
90 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
91 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
92 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
93 commands
->cmd_set
[i
].ntfn
) ||
94 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
95 commands
->cmd_set
[i
].wfn
))) {
96 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
97 if (!matches
[count
]) {
98 for (i
= 0; i
< count
; i
++) {
99 SAFE_FREE(matches
[count
]);
107 commands
= commands
->next
;
112 SAFE_FREE(matches
[0]);
113 matches
[0] = SMB_STRDUP(matches
[1]);
115 matches
[count
] = NULL
;
119 static char *next_command (char **cmdstr
)
124 if (!cmdstr
|| !(*cmdstr
))
127 p
= strchr_m(*cmdstr
, ';');
130 command
= SMB_STRDUP(*cmdstr
);
139 /* Fetch the SID for this computer */
141 static void fetch_machine_sid(struct cli_state
*cli
)
143 struct policy_handle pol
;
144 NTSTATUS result
= NT_STATUS_OK
;
145 static bool got_domain_sid
;
147 struct rpc_pipe_client
*lsapipe
= NULL
;
148 union lsa_PolicyInformation
*info
= NULL
;
150 if (got_domain_sid
) return;
152 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
153 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
157 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
159 if (!NT_STATUS_IS_OK(result
)) {
160 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
164 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
165 SEC_FLAG_MAXIMUM_ALLOWED
,
167 if (!NT_STATUS_IS_OK(result
)) {
171 result
= rpccli_lsa_QueryInfoPolicy(lsapipe
, mem_ctx
,
173 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
175 if (!NT_STATUS_IS_OK(result
)) {
179 got_domain_sid
= True
;
180 sid_copy(&domain_sid
, info
->account_domain
.sid
);
182 rpccli_lsa_Close(lsapipe
, mem_ctx
, &pol
);
183 TALLOC_FREE(lsapipe
);
184 talloc_destroy(mem_ctx
);
191 TALLOC_FREE(lsapipe
);
194 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
196 if (!NT_STATUS_IS_OK(result
)) {
197 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
203 /* List the available commands on a given pipe */
205 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
206 int argc
, const char **argv
)
208 struct cmd_list
*tmp
;
209 struct cmd_set
*tmp_set
;
215 printf("Usage: %s <pipe>\n", argv
[0]);
219 /* Help on one command */
221 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
223 tmp_set
= tmp
->cmd_set
;
225 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
227 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
231 while(tmp_set
->name
) {
232 printf("%30s", tmp_set
->name
);
239 /* drop out of the loop */
248 /* Display help on commands */
250 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
251 int argc
, const char **argv
)
253 struct cmd_list
*tmp
;
254 struct cmd_set
*tmp_set
;
259 printf("Usage: %s [command]\n", argv
[0]);
263 /* Help on one command */
266 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
268 tmp_set
= tmp
->cmd_set
;
270 while(tmp_set
->name
) {
271 if (strequal(argv
[1], tmp_set
->name
)) {
272 if (tmp_set
->usage
&&
274 printf("%s\n", tmp_set
->usage
);
276 printf("No help for %s\n", tmp_set
->name
);
285 printf("No such command: %s\n", argv
[1]);
289 /* List all commands */
291 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
293 tmp_set
= tmp
->cmd_set
;
295 while(tmp_set
->name
) {
297 printf("%15s\t\t%s\n", tmp_set
->name
,
298 tmp_set
->description
? tmp_set
->description
:
308 /* Change the debug level */
310 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
311 int argc
, const char **argv
)
314 printf("Usage: %s [debuglevel]\n", argv
[0]);
319 DEBUGLEVEL
= atoi(argv
[1]);
322 printf("debuglevel is %d\n", DEBUGLEVEL
);
327 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
328 int argc
, const char **argv
)
331 return NT_STATUS_OK
; /* NOTREACHED */
334 static NTSTATUS
cmd_set_ss_level(void)
336 struct cmd_list
*tmp
;
338 /* Close any existing connections not at this level. */
340 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
341 struct cmd_set
*tmp_set
;
343 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
344 if (tmp_set
->rpc_pipe
== NULL
) {
348 if ((tmp_set
->rpc_pipe
->auth
->auth_type
349 != pipe_default_auth_type
)
350 || (tmp_set
->rpc_pipe
->auth
->auth_level
351 != pipe_default_auth_level
)) {
352 TALLOC_FREE(tmp_set
->rpc_pipe
);
353 tmp_set
->rpc_pipe
= NULL
;
360 static NTSTATUS
cmd_set_transport(void)
362 struct cmd_list
*tmp
;
364 /* Close any existing connections not at this level. */
366 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
367 struct cmd_set
*tmp_set
;
369 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
370 if (tmp_set
->rpc_pipe
== NULL
) {
374 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
375 TALLOC_FREE(tmp_set
->rpc_pipe
);
376 tmp_set
->rpc_pipe
= NULL
;
383 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
384 int argc
, const char **argv
)
386 const char *type
= "NTLMSSP";
388 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
389 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
392 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
398 if (strequal(type
, "NTLMSSP")) {
399 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
400 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
401 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
402 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
403 } else if (strequal(type
, "SCHANNEL")) {
404 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
406 printf("unknown type %s\n", type
);
407 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
408 return NT_STATUS_INVALID_LEVEL
;
412 d_printf("Setting %s - sign\n", type
);
414 return cmd_set_ss_level();
417 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
418 int argc
, const char **argv
)
420 const char *type
= "NTLMSSP";
422 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
423 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
426 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
432 if (strequal(type
, "NTLMSSP")) {
433 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
434 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
435 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
436 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
437 } else if (strequal(type
, "SCHANNEL")) {
438 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
440 printf("unknown type %s\n", type
);
441 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
442 return NT_STATUS_INVALID_LEVEL
;
446 d_printf("Setting %s - sign and seal\n", type
);
448 return cmd_set_ss_level();
451 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
452 int argc
, const char **argv
)
454 struct cmd_list
*tmp
;
457 printf("Usage: %s timeout\n", argv
[0]);
462 timeout
= atoi(argv
[1]);
464 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
466 struct cmd_set
*tmp_set
;
468 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
469 if (tmp_set
->rpc_pipe
== NULL
) {
473 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
478 printf("timeout is %d\n", timeout
);
484 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
485 int argc
, const char **argv
)
487 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
488 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
489 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
491 return cmd_set_ss_level();
494 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
495 int argc
, const char **argv
)
497 d_printf("Setting schannel - sign and seal\n");
498 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
499 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
501 return cmd_set_ss_level();
504 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
505 int argc
, const char **argv
)
507 d_printf("Setting schannel - sign only\n");
508 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
509 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
511 return cmd_set_ss_level();
514 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
515 int argc
, const char **argv
)
520 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
524 if (strequal(argv
[1], "NCACN_NP")) {
525 default_transport
= NCACN_NP
;
526 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
527 default_transport
= NCACN_IP_TCP
;
529 printf("transport type: %s unknown or not supported\n", argv
[1]);
530 return NT_STATUS_NOT_SUPPORTED
;
533 status
= cmd_set_transport();
534 if (!NT_STATUS_IS_OK(status
)) {
538 printf("default transport is now: %s\n", argv
[1]);
543 /* Built in rpcclient commands */
545 static struct cmd_set rpcclient_commands
[] = {
547 { "GENERAL OPTIONS" },
549 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
550 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
551 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
552 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
553 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
554 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
555 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
556 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
557 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
558 { "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.", "" },
559 { "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.", "" },
560 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseonds) for RPC operations", "" },
561 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
562 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
567 static struct cmd_set separator_command
[] = {
568 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
573 /* Various pipe commands */
575 extern struct cmd_set lsarpc_commands
[];
576 extern struct cmd_set samr_commands
[];
577 extern struct cmd_set spoolss_commands
[];
578 extern struct cmd_set netlogon_commands
[];
579 extern struct cmd_set srvsvc_commands
[];
580 extern struct cmd_set dfs_commands
[];
581 extern struct cmd_set ds_commands
[];
582 extern struct cmd_set echo_commands
[];
583 extern struct cmd_set epmapper_commands
[];
584 extern struct cmd_set shutdown_commands
[];
585 extern struct cmd_set test_commands
[];
586 extern struct cmd_set wkssvc_commands
[];
587 extern struct cmd_set ntsvcs_commands
[];
588 extern struct cmd_set drsuapi_commands
[];
589 extern struct cmd_set eventlog_commands
[];
591 static struct cmd_set
*rpcclient_command_list
[] = {
611 static void add_command_set(struct cmd_set
*cmd_set
)
613 struct cmd_list
*entry
;
615 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
616 DEBUG(0, ("out of memory\n"));
622 entry
->cmd_set
= cmd_set
;
623 DLIST_ADD(cmd_list
, entry
);
628 * Call an rpcclient function, passing an argv array.
630 * @param cmd Command to run, as a single string.
632 static NTSTATUS
do_cmd(struct cli_state
*cli
,
633 struct user_auth_info
*auth_info
,
634 struct cmd_set
*cmd_entry
,
635 struct dcerpc_binding
*binding
,
636 int argc
, char **argv
)
645 if (!(mem_ctx
= talloc_init("do_cmd"))) {
646 DEBUG(0, ("talloc_init() failed\n"));
647 return NT_STATUS_NO_MEMORY
;
652 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
653 switch (pipe_default_auth_type
) {
654 case DCERPC_AUTH_TYPE_NONE
:
655 ntresult
= cli_rpc_pipe_open_noauth_transport(
656 cli
, default_transport
,
657 cmd_entry
->interface
,
658 &cmd_entry
->rpc_pipe
);
660 case DCERPC_AUTH_TYPE_SPNEGO
:
661 if (pipe_default_auth_spnego_type
!=
662 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
) {
663 DEBUG(0, ("Could not initialise %s. "
664 "Currently only NTLMSSP is "
665 "supported for SPNEGO\n",
666 get_pipe_name_from_syntax(
668 cmd_entry
->interface
)));
669 return NT_STATUS_UNSUCCESSFUL
;
671 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
672 cli
, cmd_entry
->interface
,
674 pipe_default_auth_level
,
675 get_cmdline_auth_info_domain(auth_info
),
676 get_cmdline_auth_info_username(auth_info
),
677 get_cmdline_auth_info_password(auth_info
),
678 &cmd_entry
->rpc_pipe
);
680 case DCERPC_AUTH_TYPE_NTLMSSP
:
681 ntresult
= cli_rpc_pipe_open_ntlmssp(
682 cli
, cmd_entry
->interface
,
684 pipe_default_auth_level
,
685 get_cmdline_auth_info_domain(auth_info
),
686 get_cmdline_auth_info_username(auth_info
),
687 get_cmdline_auth_info_password(auth_info
),
688 &cmd_entry
->rpc_pipe
);
690 case DCERPC_AUTH_TYPE_SCHANNEL
:
691 ntresult
= cli_rpc_pipe_open_schannel(
692 cli
, cmd_entry
->interface
,
694 pipe_default_auth_level
,
695 get_cmdline_auth_info_domain(auth_info
),
696 &cmd_entry
->rpc_pipe
);
699 DEBUG(0, ("Could not initialise %s. Invalid "
701 get_pipe_name_from_syntax(
703 cmd_entry
->interface
),
704 pipe_default_auth_type
));
705 return NT_STATUS_UNSUCCESSFUL
;
707 if (!NT_STATUS_IS_OK(ntresult
)) {
708 DEBUG(0, ("Could not initialise %s. Error was %s\n",
709 get_pipe_name_from_syntax(
710 talloc_tos(), cmd_entry
->interface
),
711 nt_errstr(ntresult
) ));
715 if (ndr_syntax_id_equal(cmd_entry
->interface
,
716 &ndr_table_netlogon
.syntax_id
)) {
717 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
718 enum netr_SchannelType sec_channel_type
;
719 uchar trust_password
[16];
720 const char *machine_account
;
722 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
723 trust_password
, &machine_account
,
726 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
729 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
730 cli
->desthost
, /* server name */
731 get_cmdline_auth_info_domain(auth_info
), /* domain */
732 global_myname(), /* client name */
733 machine_account
, /* machine account name */
738 if (!NT_STATUS_IS_OK(ntresult
)) {
739 DEBUG(0, ("Could not initialise credentials for %s.\n",
740 get_pipe_name_from_syntax(
742 cmd_entry
->interface
)));
750 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
751 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
752 if (!NT_STATUS_IS_OK(ntresult
)) {
753 printf("result was %s\n", nt_errstr(ntresult
));
756 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
757 /* print out the DOS error */
758 if (!W_ERROR_IS_OK(wresult
)) {
759 printf( "result was %s\n", win_errstr(wresult
));
761 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
766 talloc_destroy(mem_ctx
);
773 * Process a command entered at the prompt or as part of -c
775 * @returns The NTSTATUS from running the command.
777 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
778 struct cli_state
*cli
,
779 struct dcerpc_binding
*binding
,
782 struct cmd_list
*temp_list
;
783 NTSTATUS result
= NT_STATUS_OK
;
788 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
789 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
790 return NT_STATUS_UNSUCCESSFUL
;
794 /* Walk through a dlist of arrays of commands. */
795 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
796 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
798 while (temp_set
->name
) {
799 if (strequal(argv
[0], temp_set
->name
)) {
800 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
801 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
802 fprintf (stderr
, "Invalid command\n");
806 result
= do_cmd(cli
, auth_info
, temp_set
,
807 binding
, argc
, argv
);
816 printf("command not found: %s\n", argv
[0]);
821 if (!NT_STATUS_IS_OK(result)) {
822 printf("result was %s\n", nt_errstr(result));
826 /* NOTE: popt allocates the whole argv, including the
827 * strings, as a single block. So a single free is
828 * enough to release it -- we don't free the
829 * individual strings. rtfm. */
838 int main(int argc
, char *argv
[])
841 static char *cmdstr
= NULL
;
843 struct cli_state
*cli
= NULL
;
844 static char *opt_ipaddr
=NULL
;
845 struct cmd_set
**cmd_set
;
846 struct sockaddr_storage server_ss
;
848 static int opt_port
= 0;
849 fstring new_workgroup
;
851 TALLOC_CTX
*frame
= talloc_stackframe();
853 struct dcerpc_binding
*binding
= NULL
;
854 const char *binding_string
= NULL
;
855 char *user
, *domain
, *q
;
857 /* make sure the vars that get altered (4th field) are in
858 a fixed location or certain compilers complain */
860 struct poptOption long_options
[] = {
862 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
863 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
864 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
866 POPT_COMMON_CONNECTION
867 POPT_COMMON_CREDENTIALS
873 zero_sockaddr(&server_ss
);
877 /* the following functions are part of the Samba debugging
878 facilities. See lib/debug.c */
879 setup_logging("rpcclient", True
);
881 rpcclient_auth_info
= user_auth_info_init(frame
);
882 if (rpcclient_auth_info
== NULL
) {
885 popt_common_set_auth_info(rpcclient_auth_info
);
889 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
893 poptPrintHelp(pc
, stderr
, 0);
897 while((opt
= poptGetNextOpt(pc
)) != -1) {
901 if (!interpret_string_addr(&server_ss
,
904 fprintf(stderr
, "%s not a valid IP address\n",
912 /* Get server as remaining unparsed argument. Print usage if more
913 than one unparsed argument is present. */
915 server
= poptGetArg(pc
);
917 if (!server
|| poptGetArg(pc
)) {
918 poptPrintHelp(pc
, stderr
, 0);
932 /* save the workgroup...
934 FIXME!! do we need to do this for other options as well
935 (or maybe a generic way to keep lp_load() from overwriting
938 fstrcpy( new_workgroup
, lp_workgroup() );
940 /* Load smb.conf file */
942 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
943 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
945 if ( strlen(new_workgroup
) != 0 )
946 set_global_myworkgroup( new_workgroup
);
950 * from stdin if necessary
953 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
954 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
959 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
961 if ((server
[0] == '/' && server
[1] == '/') ||
962 (server
[0] == '\\' && server
[1] == '\\')) {
966 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
968 if (!NT_STATUS_IS_OK(nt_status
)) {
970 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
971 strip_hostname(server
));
972 if (!binding_string
) {
977 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
978 if (!NT_STATUS_IS_OK(nt_status
)) {
984 if (binding
->transport
== NCA_UNKNOWN
) {
985 binding
->transport
= NCACN_NP
;
988 if (binding
->flags
& DCERPC_SIGN
) {
989 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
990 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
992 if (binding
->flags
& DCERPC_SEAL
) {
993 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
994 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
996 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
997 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
998 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1000 if (binding
->flags
& DCERPC_AUTH_NTLM
) {
1001 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1003 if (binding
->flags
& DCERPC_AUTH_KRB5
) {
1004 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1005 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1008 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1009 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1010 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1012 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1013 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1016 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1017 SMB_ASSERT(user
!= NULL
);
1018 domain
= talloc_strdup(frame
, lp_workgroup());
1019 SMB_ASSERT(domain
!= NULL
);
1020 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1022 if ((q
= strchr_m(user
,'\\'))) {
1024 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1025 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1029 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
1030 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1032 get_cmdline_auth_info_username(rpcclient_auth_info
),
1033 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1034 get_cmdline_auth_info_password(rpcclient_auth_info
),
1036 get_cmdline_auth_info_signing_state(rpcclient_auth_info
),
1039 if (!NT_STATUS_IS_OK(nt_status
)) {
1040 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1045 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1046 nt_status
= cli_cm_force_encryption(cli
,
1047 get_cmdline_auth_info_username(rpcclient_auth_info
),
1048 get_cmdline_auth_info_password(rpcclient_auth_info
),
1049 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1051 if (!NT_STATUS_IS_OK(nt_status
)) {
1057 #if 0 /* COMMENT OUT FOR TESTING */
1058 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1061 /* Load command lists */
1063 timeout
= cli_set_timeout(cli
, 10000);
1065 cmd_set
= rpcclient_command_list
;
1068 add_command_set(*cmd_set
);
1069 add_command_set(separator_command
);
1073 default_transport
= binding
->transport
;
1075 fetch_machine_sid(cli
);
1077 /* Do anything specified with -c */
1078 if (cmdstr
&& cmdstr
[0]) {
1084 while((cmd
=next_command(&p
)) != NULL
) {
1085 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1088 result
= NT_STATUS_IS_ERR(cmd_result
);
1094 /* Loop around accepting commands */
1099 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1104 if (line
[0] != '\n')
1105 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);