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 "popt_common.h"
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31 #include "../libcli/security/security.h"
33 #include "libsmb/libsmb.h"
35 enum pipe_auth_type_spnego
{
36 PIPE_AUTH_TYPE_SPNEGO_NONE
= 0,
37 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
,
38 PIPE_AUTH_TYPE_SPNEGO_KRB5
41 struct dom_sid domain_sid
;
43 static enum dcerpc_AuthType pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
44 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type
= 0;
45 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
46 static unsigned int timeout
= 0;
47 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
49 struct user_auth_info
*rpcclient_auth_info
;
51 /* List to hold groups of commands.
53 * Commands are defined in a list of arrays: arrays are easy to
54 * statically declare, and lists are easier to dynamically extend.
57 static struct cmd_list
{
58 struct cmd_list
*prev
, *next
;
59 struct cmd_set
*cmd_set
;
62 /****************************************************************************
63 handle completion of commands for readline
64 ****************************************************************************/
65 static char **completion_fn(const char *text
, int start
, int end
)
67 #define MAX_COMPLETIONS 1000
70 struct cmd_list
*commands
= cmd_list
;
73 /* FIXME!!! -- what to do when completing argument? */
74 /* for words not at the start of the line fallback
75 to filename completion */
80 /* make sure we have a list of valid commands */
85 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
90 matches
[count
++] = SMB_STRDUP(text
);
96 while (commands
&& count
< MAX_COMPLETIONS
-1) {
97 if (!commands
->cmd_set
) {
101 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
102 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
103 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
104 commands
->cmd_set
[i
].ntfn
) ||
105 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
106 commands
->cmd_set
[i
].wfn
))) {
107 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
108 if (!matches
[count
]) {
109 for (i
= 0; i
< count
; i
++) {
110 SAFE_FREE(matches
[count
]);
118 commands
= commands
->next
;
122 SAFE_FREE(matches
[0]);
123 matches
[0] = SMB_STRDUP(matches
[1]);
125 matches
[count
] = NULL
;
129 static char *next_command (char **cmdstr
)
134 if (!cmdstr
|| !(*cmdstr
))
137 p
= strchr_m(*cmdstr
, ';');
140 command
= SMB_STRDUP(*cmdstr
);
149 /* Fetch the SID for this computer */
151 static void fetch_machine_sid(struct cli_state
*cli
)
153 struct policy_handle pol
;
154 NTSTATUS result
= NT_STATUS_OK
, status
;
155 static bool got_domain_sid
;
157 struct rpc_pipe_client
*lsapipe
= NULL
;
158 union lsa_PolicyInformation
*info
= NULL
;
159 struct dcerpc_binding_handle
*b
;
161 if (got_domain_sid
) return;
163 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
164 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
168 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
170 if (!NT_STATUS_IS_OK(result
)) {
171 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
175 b
= lsapipe
->binding_handle
;
177 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
178 SEC_FLAG_MAXIMUM_ALLOWED
,
180 if (!NT_STATUS_IS_OK(result
)) {
184 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
186 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
189 if (!NT_STATUS_IS_OK(status
)) {
193 if (!NT_STATUS_IS_OK(result
)) {
197 got_domain_sid
= True
;
198 sid_copy(&domain_sid
, info
->account_domain
.sid
);
200 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
201 TALLOC_FREE(lsapipe
);
202 talloc_destroy(mem_ctx
);
209 TALLOC_FREE(lsapipe
);
212 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
214 if (!NT_STATUS_IS_OK(result
)) {
215 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
221 /* List the available commands on a given pipe */
223 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
224 int argc
, const char **argv
)
226 struct cmd_list
*tmp
;
227 struct cmd_set
*tmp_set
;
233 printf("Usage: %s <pipe>\n", argv
[0]);
237 /* Help on one command */
239 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
241 tmp_set
= tmp
->cmd_set
;
243 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
245 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
249 while(tmp_set
->name
) {
250 printf("%30s", tmp_set
->name
);
257 /* drop out of the loop */
266 /* Display help on commands */
268 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
269 int argc
, const char **argv
)
271 struct cmd_list
*tmp
;
272 struct cmd_set
*tmp_set
;
277 printf("Usage: %s [command]\n", argv
[0]);
281 /* Help on one command */
284 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
286 tmp_set
= tmp
->cmd_set
;
288 while(tmp_set
->name
) {
289 if (strequal(argv
[1], tmp_set
->name
)) {
290 if (tmp_set
->usage
&&
292 printf("%s\n", tmp_set
->usage
);
294 printf("No help for %s\n", tmp_set
->name
);
303 printf("No such command: %s\n", argv
[1]);
307 /* List all commands */
309 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
311 tmp_set
= tmp
->cmd_set
;
313 while(tmp_set
->name
) {
315 printf("%15s\t\t%s\n", tmp_set
->name
,
316 tmp_set
->description
? tmp_set
->description
:
326 /* Change the debug level */
328 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
329 int argc
, const char **argv
)
332 printf("Usage: %s [debuglevel]\n", argv
[0]);
337 lp_set_cmdline("log level", argv
[1]);
340 printf("debuglevel is %d\n", DEBUGLEVEL
);
345 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
346 int argc
, const char **argv
)
349 return NT_STATUS_OK
; /* NOTREACHED */
352 static NTSTATUS
cmd_set_ss_level(void)
354 struct cmd_list
*tmp
;
356 /* Close any existing connections not at this level. */
358 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
359 struct cmd_set
*tmp_set
;
361 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
362 if (tmp_set
->rpc_pipe
== NULL
) {
366 if ((tmp_set
->rpc_pipe
->auth
->auth_type
367 != pipe_default_auth_type
)
368 || (tmp_set
->rpc_pipe
->auth
->auth_level
369 != pipe_default_auth_level
)) {
370 TALLOC_FREE(tmp_set
->rpc_pipe
);
371 tmp_set
->rpc_pipe
= NULL
;
378 static NTSTATUS
cmd_set_transport(void)
380 struct cmd_list
*tmp
;
382 /* Close any existing connections not at this level. */
384 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
385 struct cmd_set
*tmp_set
;
387 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
388 if (tmp_set
->rpc_pipe
== NULL
) {
392 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
393 TALLOC_FREE(tmp_set
->rpc_pipe
);
394 tmp_set
->rpc_pipe
= NULL
;
401 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
402 int argc
, const char **argv
)
404 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
405 const char *type
= "NTLMSSP";
407 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
408 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
411 printf("Usage: %s %s\n", argv
[0], p
);
417 if (strequal(type
, "KRB5")) {
418 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
419 } else if (strequal(type
, "KRB5_SPNEGO")) {
420 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
421 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
422 } else if (strequal(type
, "NTLMSSP")) {
423 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
424 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
425 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
426 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
427 } else if (strequal(type
, "SCHANNEL")) {
428 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
430 printf("unknown type %s\n", type
);
431 printf("Usage: %s %s\n", argv
[0], p
);
432 return NT_STATUS_INVALID_LEVEL
;
436 d_printf("Setting %s - sign\n", type
);
438 return cmd_set_ss_level();
441 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
442 int argc
, const char **argv
)
444 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
445 const char *type
= "NTLMSSP";
447 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
448 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
451 printf("Usage: %s %s\n", argv
[0], p
);
457 if (strequal(type
, "KRB5")) {
458 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
459 } else if (strequal(type
, "KRB5_SPNEGO")) {
460 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
461 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
462 } else if (strequal(type
, "NTLMSSP")) {
463 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
464 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
465 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
466 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
467 } else if (strequal(type
, "SCHANNEL")) {
468 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
470 printf("unknown type %s\n", type
);
471 printf("Usage: %s %s\n", argv
[0], p
);
472 return NT_STATUS_INVALID_LEVEL
;
476 d_printf("Setting %s - sign and seal\n", type
);
478 return cmd_set_ss_level();
481 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
482 int argc
, const char **argv
)
484 struct cmd_list
*tmp
;
487 printf("Usage: %s timeout\n", argv
[0]);
492 timeout
= atoi(argv
[1]);
494 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
496 struct cmd_set
*tmp_set
;
498 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
499 if (tmp_set
->rpc_pipe
== NULL
) {
503 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
508 printf("timeout is %d\n", timeout
);
514 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
515 int argc
, const char **argv
)
517 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
518 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
519 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
521 return cmd_set_ss_level();
524 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
525 int argc
, const char **argv
)
527 d_printf("Setting schannel - sign and seal\n");
528 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
529 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
531 return cmd_set_ss_level();
534 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
535 int argc
, const char **argv
)
537 d_printf("Setting schannel - sign only\n");
538 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
539 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
541 return cmd_set_ss_level();
544 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
545 int argc
, const char **argv
)
550 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
554 if (strequal(argv
[1], "NCACN_NP")) {
555 default_transport
= NCACN_NP
;
556 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
557 default_transport
= NCACN_IP_TCP
;
559 printf("transport type: %s unknown or not supported\n", argv
[1]);
560 return NT_STATUS_NOT_SUPPORTED
;
563 status
= cmd_set_transport();
564 if (!NT_STATUS_IS_OK(status
)) {
568 printf("default transport is now: %s\n", argv
[1]);
573 /* Built in rpcclient commands */
575 static struct cmd_set rpcclient_commands
[] = {
577 { "GENERAL OPTIONS" },
579 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
580 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
581 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
582 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
583 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
584 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
585 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
586 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
587 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
588 { "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.", "" },
589 { "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.", "" },
590 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseconds) for RPC operations", "" },
591 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
592 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
597 static struct cmd_set separator_command
[] = {
598 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
603 /* Various pipe commands */
605 extern struct cmd_set lsarpc_commands
[];
606 extern struct cmd_set samr_commands
[];
607 extern struct cmd_set spoolss_commands
[];
608 extern struct cmd_set netlogon_commands
[];
609 extern struct cmd_set srvsvc_commands
[];
610 extern struct cmd_set dfs_commands
[];
611 extern struct cmd_set ds_commands
[];
612 extern struct cmd_set echo_commands
[];
613 extern struct cmd_set epmapper_commands
[];
614 extern struct cmd_set shutdown_commands
[];
615 extern struct cmd_set test_commands
[];
616 extern struct cmd_set wkssvc_commands
[];
617 extern struct cmd_set ntsvcs_commands
[];
618 extern struct cmd_set drsuapi_commands
[];
619 extern struct cmd_set eventlog_commands
[];
620 extern struct cmd_set winreg_commands
[];
622 static struct cmd_set
*rpcclient_command_list
[] = {
643 static void add_command_set(struct cmd_set
*cmd_set
)
645 struct cmd_list
*entry
;
647 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
648 DEBUG(0, ("out of memory\n"));
654 entry
->cmd_set
= cmd_set
;
655 DLIST_ADD(cmd_list
, entry
);
660 * Call an rpcclient function, passing an argv array.
662 * @param cmd Command to run, as a single string.
664 static NTSTATUS
do_cmd(struct cli_state
*cli
,
665 struct user_auth_info
*auth_info
,
666 struct cmd_set
*cmd_entry
,
667 struct dcerpc_binding
*binding
,
668 int argc
, char **argv
)
677 if (!(mem_ctx
= talloc_init("do_cmd"))) {
678 DEBUG(0, ("talloc_init() failed\n"));
679 return NT_STATUS_NO_MEMORY
;
684 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
685 switch (pipe_default_auth_type
) {
686 case DCERPC_AUTH_TYPE_NONE
:
687 ntresult
= cli_rpc_pipe_open_noauth_transport(
688 cli
, default_transport
,
689 cmd_entry
->interface
,
690 &cmd_entry
->rpc_pipe
);
692 case DCERPC_AUTH_TYPE_SPNEGO
:
693 switch (pipe_default_auth_spnego_type
) {
694 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
695 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
696 cli
, cmd_entry
->interface
,
698 pipe_default_auth_level
,
699 get_cmdline_auth_info_domain(auth_info
),
700 get_cmdline_auth_info_username(auth_info
),
701 get_cmdline_auth_info_password(auth_info
),
702 &cmd_entry
->rpc_pipe
);
704 case PIPE_AUTH_TYPE_SPNEGO_KRB5
:
705 ntresult
= cli_rpc_pipe_open_spnego_krb5(
706 cli
, cmd_entry
->interface
,
708 pipe_default_auth_level
,
711 &cmd_entry
->rpc_pipe
);
714 ntresult
= NT_STATUS_INTERNAL_ERROR
;
717 case DCERPC_AUTH_TYPE_NTLMSSP
:
718 ntresult
= cli_rpc_pipe_open_ntlmssp(
719 cli
, cmd_entry
->interface
,
721 pipe_default_auth_level
,
722 get_cmdline_auth_info_domain(auth_info
),
723 get_cmdline_auth_info_username(auth_info
),
724 get_cmdline_auth_info_password(auth_info
),
725 &cmd_entry
->rpc_pipe
);
727 case DCERPC_AUTH_TYPE_SCHANNEL
:
728 ntresult
= cli_rpc_pipe_open_schannel(
729 cli
, cmd_entry
->interface
,
731 pipe_default_auth_level
,
732 get_cmdline_auth_info_domain(auth_info
),
733 &cmd_entry
->rpc_pipe
);
735 case DCERPC_AUTH_TYPE_KRB5
:
736 ntresult
= cli_rpc_pipe_open_krb5(
737 cli
, cmd_entry
->interface
,
739 pipe_default_auth_level
,
742 &cmd_entry
->rpc_pipe
);
745 DEBUG(0, ("Could not initialise %s. Invalid "
747 get_pipe_name_from_syntax(
749 cmd_entry
->interface
),
750 pipe_default_auth_type
));
751 return NT_STATUS_UNSUCCESSFUL
;
753 if (!NT_STATUS_IS_OK(ntresult
)) {
754 DEBUG(0, ("Could not initialise %s. Error was %s\n",
755 get_pipe_name_from_syntax(
756 talloc_tos(), cmd_entry
->interface
),
757 nt_errstr(ntresult
) ));
761 if (ndr_syntax_id_equal(cmd_entry
->interface
,
762 &ndr_table_netlogon
.syntax_id
)) {
763 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
764 enum netr_SchannelType sec_channel_type
;
765 uchar trust_password
[16];
766 const char *machine_account
;
768 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
769 trust_password
, &machine_account
,
772 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
775 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
776 cli
->desthost
, /* server name */
777 get_cmdline_auth_info_domain(auth_info
), /* domain */
778 global_myname(), /* client name */
779 machine_account
, /* machine account name */
784 if (!NT_STATUS_IS_OK(ntresult
)) {
785 DEBUG(0, ("Could not initialise credentials for %s.\n",
786 get_pipe_name_from_syntax(
788 cmd_entry
->interface
)));
796 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
797 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
798 if (!NT_STATUS_IS_OK(ntresult
)) {
799 printf("result was %s\n", nt_errstr(ntresult
));
802 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
803 /* print out the DOS error */
804 if (!W_ERROR_IS_OK(wresult
)) {
805 printf( "result was %s\n", win_errstr(wresult
));
807 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
812 talloc_destroy(mem_ctx
);
819 * Process a command entered at the prompt or as part of -c
821 * @returns The NTSTATUS from running the command.
823 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
824 struct cli_state
*cli
,
825 struct dcerpc_binding
*binding
,
828 struct cmd_list
*temp_list
;
829 NTSTATUS result
= NT_STATUS_OK
;
834 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
835 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
836 return NT_STATUS_UNSUCCESSFUL
;
840 /* Walk through a dlist of arrays of commands. */
841 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
842 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
844 while (temp_set
->name
) {
845 if (strequal(argv
[0], temp_set
->name
)) {
846 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
847 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
848 fprintf (stderr
, "Invalid command\n");
852 result
= do_cmd(cli
, auth_info
, temp_set
,
853 binding
, argc
, argv
);
862 printf("command not found: %s\n", argv
[0]);
867 if (!NT_STATUS_IS_OK(result)) {
868 printf("result was %s\n", nt_errstr(result));
872 /* NOTE: popt allocates the whole argv, including the
873 * strings, as a single block. So a single free is
874 * enough to release it -- we don't free the
875 * individual strings. rtfm. */
884 int main(int argc
, char *argv
[])
887 static char *cmdstr
= NULL
;
889 struct cli_state
*cli
= NULL
;
890 static char *opt_ipaddr
=NULL
;
891 struct cmd_set
**cmd_set
;
892 struct sockaddr_storage server_ss
;
894 static int opt_port
= 0;
895 fstring new_workgroup
;
897 TALLOC_CTX
*frame
= talloc_stackframe();
899 struct dcerpc_binding
*binding
= NULL
;
900 const char *binding_string
= NULL
;
901 char *user
, *domain
, *q
;
903 /* make sure the vars that get altered (4th field) are in
904 a fixed location or certain compilers complain */
906 struct poptOption long_options
[] = {
908 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
909 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
910 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
912 POPT_COMMON_CONNECTION
913 POPT_COMMON_CREDENTIALS
919 zero_sockaddr(&server_ss
);
923 /* the following functions are part of the Samba debugging
924 facilities. See lib/debug.c */
925 setup_logging("rpcclient", DEBUG_STDOUT
);
927 rpcclient_auth_info
= user_auth_info_init(frame
);
928 if (rpcclient_auth_info
== NULL
) {
931 popt_common_set_auth_info(rpcclient_auth_info
);
935 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
939 poptPrintHelp(pc
, stderr
, 0);
943 while((opt
= poptGetNextOpt(pc
)) != -1) {
947 if (!interpret_string_addr(&server_ss
,
950 fprintf(stderr
, "%s not a valid IP address\n",
958 /* Get server as remaining unparsed argument. Print usage if more
959 than one unparsed argument is present. */
961 server
= poptGetArg(pc
);
963 if (!server
|| poptGetArg(pc
)) {
964 poptPrintHelp(pc
, stderr
, 0);
978 /* save the workgroup...
980 FIXME!! do we need to do this for other options as well
981 (or maybe a generic way to keep lp_load() from overwriting
984 fstrcpy( new_workgroup
, lp_workgroup() );
986 /* Load smb.conf file */
988 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
989 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
991 if ( strlen(new_workgroup
) != 0 )
992 set_global_myworkgroup( new_workgroup
);
996 * from stdin if necessary
999 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
1000 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
1005 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
1007 if ((server
[0] == '/' && server
[1] == '/') ||
1008 (server
[0] == '\\' && server
[1] == '\\')) {
1012 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1014 if (!NT_STATUS_IS_OK(nt_status
)) {
1016 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1017 strip_hostname(server
));
1018 if (!binding_string
) {
1023 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1024 if (!NT_STATUS_IS_OK(nt_status
)) {
1030 if (binding
->transport
== NCA_UNKNOWN
) {
1031 binding
->transport
= NCACN_NP
;
1034 if (binding
->flags
& DCERPC_SIGN
) {
1035 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
1036 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1038 if (binding
->flags
& DCERPC_SEAL
) {
1039 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
1040 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1042 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
1043 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1044 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1046 if (binding
->flags
& DCERPC_AUTH_NTLM
) {
1047 /* If neither Integrity or Privacy are requested then
1048 * Use just Connect level */
1049 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1050 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1053 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1054 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1056 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1059 if (binding
->flags
& DCERPC_AUTH_KRB5
) {
1060 /* If neither Integrity or Privacy are requested then
1061 * Use just Connect level */
1062 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1063 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1066 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1067 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1069 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1073 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1074 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1075 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1077 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1078 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1081 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1082 SMB_ASSERT(user
!= NULL
);
1083 domain
= talloc_strdup(frame
, lp_workgroup());
1084 SMB_ASSERT(domain
!= NULL
);
1085 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1087 if ((q
= strchr_m(user
,'\\'))) {
1089 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1090 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1094 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
1095 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1097 get_cmdline_auth_info_username(rpcclient_auth_info
),
1098 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1099 get_cmdline_auth_info_password(rpcclient_auth_info
),
1101 get_cmdline_auth_info_signing_state(rpcclient_auth_info
));
1103 if (!NT_STATUS_IS_OK(nt_status
)) {
1104 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1109 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1110 nt_status
= cli_cm_force_encryption(cli
,
1111 get_cmdline_auth_info_username(rpcclient_auth_info
),
1112 get_cmdline_auth_info_password(rpcclient_auth_info
),
1113 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1115 if (!NT_STATUS_IS_OK(nt_status
)) {
1121 #if 0 /* COMMENT OUT FOR TESTING */
1122 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1125 /* Load command lists */
1127 timeout
= cli_set_timeout(cli
, 10000);
1129 cmd_set
= rpcclient_command_list
;
1132 add_command_set(*cmd_set
);
1133 add_command_set(separator_command
);
1137 default_transport
= binding
->transport
;
1139 fetch_machine_sid(cli
);
1141 /* Do anything specified with -c */
1142 if (cmdstr
&& cmdstr
[0]) {
1148 while((cmd
=next_command(&p
)) != NULL
) {
1149 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1152 result
= NT_STATUS_IS_ERR(cmd_result
);
1158 /* Loop around accepting commands */
1163 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1168 if (line
[0] != '\n')
1169 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);