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"
34 enum pipe_auth_type_spnego
{
35 PIPE_AUTH_TYPE_SPNEGO_NONE
= 0,
36 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
,
37 PIPE_AUTH_TYPE_SPNEGO_KRB5
40 struct dom_sid domain_sid
;
42 static enum dcerpc_AuthType pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
43 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type
= 0;
44 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
45 static unsigned int timeout
= 0;
46 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
48 struct user_auth_info
*rpcclient_auth_info
;
50 /* List to hold groups of commands.
52 * Commands are defined in a list of arrays: arrays are easy to
53 * statically declare, and lists are easier to dynamically extend.
56 static struct cmd_list
{
57 struct cmd_list
*prev
, *next
;
58 struct cmd_set
*cmd_set
;
61 /****************************************************************************
62 handle completion of commands for readline
63 ****************************************************************************/
64 static char **completion_fn(const char *text
, int start
, int end
)
66 #define MAX_COMPLETIONS 1000
69 struct cmd_list
*commands
= cmd_list
;
72 /* FIXME!!! -- what to do when completing argument? */
73 /* for words not at the start of the line fallback
74 to filename completion */
79 /* make sure we have a list of valid commands */
84 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
89 matches
[count
++] = SMB_STRDUP(text
);
95 while (commands
&& count
< MAX_COMPLETIONS
-1) {
96 if (!commands
->cmd_set
) {
100 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
101 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
102 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
103 commands
->cmd_set
[i
].ntfn
) ||
104 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
105 commands
->cmd_set
[i
].wfn
))) {
106 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
107 if (!matches
[count
]) {
108 for (i
= 0; i
< count
; i
++) {
109 SAFE_FREE(matches
[count
]);
117 commands
= commands
->next
;
121 SAFE_FREE(matches
[0]);
122 matches
[0] = SMB_STRDUP(matches
[1]);
124 matches
[count
] = NULL
;
128 static char *next_command (char **cmdstr
)
133 if (!cmdstr
|| !(*cmdstr
))
136 p
= strchr_m(*cmdstr
, ';');
139 command
= SMB_STRDUP(*cmdstr
);
148 /* Fetch the SID for this computer */
150 static void fetch_machine_sid(struct cli_state
*cli
)
152 struct policy_handle pol
;
153 NTSTATUS result
= NT_STATUS_OK
, status
;
154 static bool got_domain_sid
;
156 struct rpc_pipe_client
*lsapipe
= NULL
;
157 union lsa_PolicyInformation
*info
= NULL
;
158 struct dcerpc_binding_handle
*b
;
160 if (got_domain_sid
) return;
162 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
163 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
167 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
169 if (!NT_STATUS_IS_OK(result
)) {
170 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
174 b
= lsapipe
->binding_handle
;
176 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
177 SEC_FLAG_MAXIMUM_ALLOWED
,
179 if (!NT_STATUS_IS_OK(result
)) {
183 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
185 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
188 if (!NT_STATUS_IS_OK(status
)) {
192 if (!NT_STATUS_IS_OK(result
)) {
196 got_domain_sid
= True
;
197 sid_copy(&domain_sid
, info
->account_domain
.sid
);
199 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
200 TALLOC_FREE(lsapipe
);
201 talloc_destroy(mem_ctx
);
208 TALLOC_FREE(lsapipe
);
211 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
213 if (!NT_STATUS_IS_OK(result
)) {
214 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
220 /* List the available commands on a given pipe */
222 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
223 int argc
, const char **argv
)
225 struct cmd_list
*tmp
;
226 struct cmd_set
*tmp_set
;
232 printf("Usage: %s <pipe>\n", argv
[0]);
236 /* Help on one command */
238 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
240 tmp_set
= tmp
->cmd_set
;
242 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
244 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
248 while(tmp_set
->name
) {
249 printf("%30s", tmp_set
->name
);
256 /* drop out of the loop */
265 /* Display help on commands */
267 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
268 int argc
, const char **argv
)
270 struct cmd_list
*tmp
;
271 struct cmd_set
*tmp_set
;
276 printf("Usage: %s [command]\n", argv
[0]);
280 /* Help on one command */
283 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
285 tmp_set
= tmp
->cmd_set
;
287 while(tmp_set
->name
) {
288 if (strequal(argv
[1], tmp_set
->name
)) {
289 if (tmp_set
->usage
&&
291 printf("%s\n", tmp_set
->usage
);
293 printf("No help for %s\n", tmp_set
->name
);
302 printf("No such command: %s\n", argv
[1]);
306 /* List all commands */
308 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
310 tmp_set
= tmp
->cmd_set
;
312 while(tmp_set
->name
) {
314 printf("%15s\t\t%s\n", tmp_set
->name
,
315 tmp_set
->description
? tmp_set
->description
:
325 /* Change the debug level */
327 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
328 int argc
, const char **argv
)
331 printf("Usage: %s [debuglevel]\n", argv
[0]);
336 lp_set_cmdline("log level", argv
[1]);
339 printf("debuglevel is %d\n", DEBUGLEVEL
);
344 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
345 int argc
, const char **argv
)
348 return NT_STATUS_OK
; /* NOTREACHED */
351 static NTSTATUS
cmd_set_ss_level(void)
353 struct cmd_list
*tmp
;
355 /* Close any existing connections not at this level. */
357 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
358 struct cmd_set
*tmp_set
;
360 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
361 if (tmp_set
->rpc_pipe
== NULL
) {
365 if ((tmp_set
->rpc_pipe
->auth
->auth_type
366 != pipe_default_auth_type
)
367 || (tmp_set
->rpc_pipe
->auth
->auth_level
368 != pipe_default_auth_level
)) {
369 TALLOC_FREE(tmp_set
->rpc_pipe
);
370 tmp_set
->rpc_pipe
= NULL
;
377 static NTSTATUS
cmd_set_transport(void)
379 struct cmd_list
*tmp
;
381 /* Close any existing connections not at this level. */
383 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
384 struct cmd_set
*tmp_set
;
386 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
387 if (tmp_set
->rpc_pipe
== NULL
) {
391 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
392 TALLOC_FREE(tmp_set
->rpc_pipe
);
393 tmp_set
->rpc_pipe
= NULL
;
400 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
401 int argc
, const char **argv
)
403 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
404 const char *type
= "NTLMSSP";
406 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
407 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
410 printf("Usage: %s %s\n", argv
[0], p
);
416 if (strequal(type
, "KRB5")) {
417 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
418 } else if (strequal(type
, "KRB5_SPNEGO")) {
419 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
420 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
421 } else if (strequal(type
, "NTLMSSP")) {
422 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
423 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
424 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
425 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
426 } else if (strequal(type
, "SCHANNEL")) {
427 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
429 printf("unknown type %s\n", type
);
430 printf("Usage: %s %s\n", argv
[0], p
);
431 return NT_STATUS_INVALID_LEVEL
;
435 d_printf("Setting %s - sign\n", type
);
437 return cmd_set_ss_level();
440 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
441 int argc
, const char **argv
)
443 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
444 const char *type
= "NTLMSSP";
446 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
447 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
450 printf("Usage: %s %s\n", argv
[0], p
);
456 if (strequal(type
, "KRB5")) {
457 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
458 } else if (strequal(type
, "KRB5_SPNEGO")) {
459 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
460 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
461 } else if (strequal(type
, "NTLMSSP")) {
462 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
463 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
464 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
465 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
466 } else if (strequal(type
, "SCHANNEL")) {
467 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
469 printf("unknown type %s\n", type
);
470 printf("Usage: %s %s\n", argv
[0], p
);
471 return NT_STATUS_INVALID_LEVEL
;
475 d_printf("Setting %s - sign and seal\n", type
);
477 return cmd_set_ss_level();
480 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
481 int argc
, const char **argv
)
483 struct cmd_list
*tmp
;
486 printf("Usage: %s timeout\n", argv
[0]);
491 timeout
= atoi(argv
[1]);
493 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
495 struct cmd_set
*tmp_set
;
497 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
498 if (tmp_set
->rpc_pipe
== NULL
) {
502 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
507 printf("timeout is %d\n", timeout
);
513 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
514 int argc
, const char **argv
)
516 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
517 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
518 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
520 return cmd_set_ss_level();
523 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
524 int argc
, const char **argv
)
526 d_printf("Setting schannel - sign and seal\n");
527 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
528 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
530 return cmd_set_ss_level();
533 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
534 int argc
, const char **argv
)
536 d_printf("Setting schannel - sign only\n");
537 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
538 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
540 return cmd_set_ss_level();
543 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
544 int argc
, const char **argv
)
549 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
553 if (strequal(argv
[1], "NCACN_NP")) {
554 default_transport
= NCACN_NP
;
555 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
556 default_transport
= NCACN_IP_TCP
;
558 printf("transport type: %s unknown or not supported\n", argv
[1]);
559 return NT_STATUS_NOT_SUPPORTED
;
562 status
= cmd_set_transport();
563 if (!NT_STATUS_IS_OK(status
)) {
567 printf("default transport is now: %s\n", argv
[1]);
572 /* Built in rpcclient commands */
574 static struct cmd_set rpcclient_commands
[] = {
576 { "GENERAL OPTIONS" },
578 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
579 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
580 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
581 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
582 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
583 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
584 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
585 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
586 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
587 { "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.", "" },
588 { "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.", "" },
589 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseconds) for RPC operations", "" },
590 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
591 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
596 static struct cmd_set separator_command
[] = {
597 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
602 /* Various pipe commands */
604 extern struct cmd_set lsarpc_commands
[];
605 extern struct cmd_set samr_commands
[];
606 extern struct cmd_set spoolss_commands
[];
607 extern struct cmd_set netlogon_commands
[];
608 extern struct cmd_set srvsvc_commands
[];
609 extern struct cmd_set dfs_commands
[];
610 extern struct cmd_set ds_commands
[];
611 extern struct cmd_set echo_commands
[];
612 extern struct cmd_set epmapper_commands
[];
613 extern struct cmd_set shutdown_commands
[];
614 extern struct cmd_set test_commands
[];
615 extern struct cmd_set wkssvc_commands
[];
616 extern struct cmd_set ntsvcs_commands
[];
617 extern struct cmd_set drsuapi_commands
[];
618 extern struct cmd_set eventlog_commands
[];
619 extern struct cmd_set winreg_commands
[];
621 static struct cmd_set
*rpcclient_command_list
[] = {
642 static void add_command_set(struct cmd_set
*cmd_set
)
644 struct cmd_list
*entry
;
646 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
647 DEBUG(0, ("out of memory\n"));
653 entry
->cmd_set
= cmd_set
;
654 DLIST_ADD(cmd_list
, entry
);
659 * Call an rpcclient function, passing an argv array.
661 * @param cmd Command to run, as a single string.
663 static NTSTATUS
do_cmd(struct cli_state
*cli
,
664 struct user_auth_info
*auth_info
,
665 struct cmd_set
*cmd_entry
,
666 struct dcerpc_binding
*binding
,
667 int argc
, char **argv
)
676 if (!(mem_ctx
= talloc_init("do_cmd"))) {
677 DEBUG(0, ("talloc_init() failed\n"));
678 return NT_STATUS_NO_MEMORY
;
683 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
684 switch (pipe_default_auth_type
) {
685 case DCERPC_AUTH_TYPE_NONE
:
686 ntresult
= cli_rpc_pipe_open_noauth_transport(
687 cli
, default_transport
,
688 cmd_entry
->interface
,
689 &cmd_entry
->rpc_pipe
);
691 case DCERPC_AUTH_TYPE_SPNEGO
:
692 switch (pipe_default_auth_spnego_type
) {
693 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
694 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
695 cli
, cmd_entry
->interface
,
697 pipe_default_auth_level
,
698 get_cmdline_auth_info_domain(auth_info
),
699 get_cmdline_auth_info_username(auth_info
),
700 get_cmdline_auth_info_password(auth_info
),
701 &cmd_entry
->rpc_pipe
);
703 case PIPE_AUTH_TYPE_SPNEGO_KRB5
:
704 ntresult
= cli_rpc_pipe_open_spnego_krb5(
705 cli
, cmd_entry
->interface
,
707 pipe_default_auth_level
,
710 &cmd_entry
->rpc_pipe
);
713 ntresult
= NT_STATUS_INTERNAL_ERROR
;
716 case DCERPC_AUTH_TYPE_NTLMSSP
:
717 ntresult
= cli_rpc_pipe_open_ntlmssp(
718 cli
, cmd_entry
->interface
,
720 pipe_default_auth_level
,
721 get_cmdline_auth_info_domain(auth_info
),
722 get_cmdline_auth_info_username(auth_info
),
723 get_cmdline_auth_info_password(auth_info
),
724 &cmd_entry
->rpc_pipe
);
726 case DCERPC_AUTH_TYPE_SCHANNEL
:
727 ntresult
= cli_rpc_pipe_open_schannel(
728 cli
, cmd_entry
->interface
,
730 pipe_default_auth_level
,
731 get_cmdline_auth_info_domain(auth_info
),
732 &cmd_entry
->rpc_pipe
);
734 case DCERPC_AUTH_TYPE_KRB5
:
735 ntresult
= cli_rpc_pipe_open_krb5(
736 cli
, cmd_entry
->interface
,
738 pipe_default_auth_level
,
741 &cmd_entry
->rpc_pipe
);
744 DEBUG(0, ("Could not initialise %s. Invalid "
746 get_pipe_name_from_syntax(
748 cmd_entry
->interface
),
749 pipe_default_auth_type
));
750 return NT_STATUS_UNSUCCESSFUL
;
752 if (!NT_STATUS_IS_OK(ntresult
)) {
753 DEBUG(0, ("Could not initialise %s. Error was %s\n",
754 get_pipe_name_from_syntax(
755 talloc_tos(), cmd_entry
->interface
),
756 nt_errstr(ntresult
) ));
760 if (ndr_syntax_id_equal(cmd_entry
->interface
,
761 &ndr_table_netlogon
.syntax_id
)) {
762 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
763 enum netr_SchannelType sec_channel_type
;
764 uchar trust_password
[16];
765 const char *machine_account
;
767 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
768 trust_password
, &machine_account
,
771 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
774 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
775 cli
->desthost
, /* server name */
776 get_cmdline_auth_info_domain(auth_info
), /* domain */
777 global_myname(), /* client name */
778 machine_account
, /* machine account name */
783 if (!NT_STATUS_IS_OK(ntresult
)) {
784 DEBUG(0, ("Could not initialise credentials for %s.\n",
785 get_pipe_name_from_syntax(
787 cmd_entry
->interface
)));
795 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
796 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
797 if (!NT_STATUS_IS_OK(ntresult
)) {
798 printf("result was %s\n", nt_errstr(ntresult
));
801 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
802 /* print out the DOS error */
803 if (!W_ERROR_IS_OK(wresult
)) {
804 printf( "result was %s\n", win_errstr(wresult
));
806 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
811 talloc_destroy(mem_ctx
);
818 * Process a command entered at the prompt or as part of -c
820 * @returns The NTSTATUS from running the command.
822 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
823 struct cli_state
*cli
,
824 struct dcerpc_binding
*binding
,
827 struct cmd_list
*temp_list
;
828 NTSTATUS result
= NT_STATUS_OK
;
833 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
834 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
835 return NT_STATUS_UNSUCCESSFUL
;
839 /* Walk through a dlist of arrays of commands. */
840 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
841 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
843 while (temp_set
->name
) {
844 if (strequal(argv
[0], temp_set
->name
)) {
845 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
846 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
847 fprintf (stderr
, "Invalid command\n");
851 result
= do_cmd(cli
, auth_info
, temp_set
,
852 binding
, argc
, argv
);
861 printf("command not found: %s\n", argv
[0]);
866 if (!NT_STATUS_IS_OK(result)) {
867 printf("result was %s\n", nt_errstr(result));
871 /* NOTE: popt allocates the whole argv, including the
872 * strings, as a single block. So a single free is
873 * enough to release it -- we don't free the
874 * individual strings. rtfm. */
883 int main(int argc
, char *argv
[])
886 static char *cmdstr
= NULL
;
888 struct cli_state
*cli
= NULL
;
889 static char *opt_ipaddr
=NULL
;
890 struct cmd_set
**cmd_set
;
891 struct sockaddr_storage server_ss
;
893 static int opt_port
= 0;
894 fstring new_workgroup
;
896 TALLOC_CTX
*frame
= talloc_stackframe();
898 struct dcerpc_binding
*binding
= NULL
;
899 const char *binding_string
= NULL
;
900 char *user
, *domain
, *q
;
902 /* make sure the vars that get altered (4th field) are in
903 a fixed location or certain compilers complain */
905 struct poptOption long_options
[] = {
907 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
908 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
909 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
911 POPT_COMMON_CONNECTION
912 POPT_COMMON_CREDENTIALS
918 zero_sockaddr(&server_ss
);
922 /* the following functions are part of the Samba debugging
923 facilities. See lib/debug.c */
924 setup_logging("rpcclient", DEBUG_STDOUT
);
926 rpcclient_auth_info
= user_auth_info_init(frame
);
927 if (rpcclient_auth_info
== NULL
) {
930 popt_common_set_auth_info(rpcclient_auth_info
);
934 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
938 poptPrintHelp(pc
, stderr
, 0);
942 while((opt
= poptGetNextOpt(pc
)) != -1) {
946 if (!interpret_string_addr(&server_ss
,
949 fprintf(stderr
, "%s not a valid IP address\n",
957 /* Get server as remaining unparsed argument. Print usage if more
958 than one unparsed argument is present. */
960 server
= poptGetArg(pc
);
962 if (!server
|| poptGetArg(pc
)) {
963 poptPrintHelp(pc
, stderr
, 0);
977 /* save the workgroup...
979 FIXME!! do we need to do this for other options as well
980 (or maybe a generic way to keep lp_load() from overwriting
983 fstrcpy( new_workgroup
, lp_workgroup() );
985 /* Load smb.conf file */
987 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
988 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
990 if ( strlen(new_workgroup
) != 0 )
991 set_global_myworkgroup( new_workgroup
);
995 * from stdin if necessary
998 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
999 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
1004 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
1006 if ((server
[0] == '/' && server
[1] == '/') ||
1007 (server
[0] == '\\' && server
[1] == '\\')) {
1011 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1013 if (!NT_STATUS_IS_OK(nt_status
)) {
1015 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1016 strip_hostname(server
));
1017 if (!binding_string
) {
1022 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1023 if (!NT_STATUS_IS_OK(nt_status
)) {
1029 if (binding
->transport
== NCA_UNKNOWN
) {
1030 binding
->transport
= NCACN_NP
;
1033 if (binding
->flags
& DCERPC_SIGN
) {
1034 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
1035 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1037 if (binding
->flags
& DCERPC_SEAL
) {
1038 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
1039 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1041 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
1042 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1043 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1045 if (binding
->flags
& DCERPC_AUTH_NTLM
) {
1046 /* If neither Integrity or Privacy are requested then
1047 * Use just Connect level */
1048 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1049 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1052 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1053 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1055 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1058 if (binding
->flags
& DCERPC_AUTH_KRB5
) {
1059 /* If neither Integrity or Privacy are requested then
1060 * Use just Connect level */
1061 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1062 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1065 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1066 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1068 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1072 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1073 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1074 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1076 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1077 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1080 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1081 SMB_ASSERT(user
!= NULL
);
1082 domain
= talloc_strdup(frame
, lp_workgroup());
1083 SMB_ASSERT(domain
!= NULL
);
1084 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1086 if ((q
= strchr_m(user
,'\\'))) {
1088 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1089 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1093 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
1094 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1096 get_cmdline_auth_info_username(rpcclient_auth_info
),
1097 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1098 get_cmdline_auth_info_password(rpcclient_auth_info
),
1100 get_cmdline_auth_info_signing_state(rpcclient_auth_info
));
1102 if (!NT_STATUS_IS_OK(nt_status
)) {
1103 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1108 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1109 nt_status
= cli_cm_force_encryption(cli
,
1110 get_cmdline_auth_info_username(rpcclient_auth_info
),
1111 get_cmdline_auth_info_password(rpcclient_auth_info
),
1112 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1114 if (!NT_STATUS_IS_OK(nt_status
)) {
1120 #if 0 /* COMMENT OUT FOR TESTING */
1121 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1124 /* Load command lists */
1126 timeout
= cli_set_timeout(cli
, 10000);
1128 cmd_set
= rpcclient_command_list
;
1131 add_command_set(*cmd_set
);
1132 add_command_set(separator_command
);
1136 default_transport
= binding
->transport
;
1138 fetch_machine_sid(cli
);
1140 /* Do anything specified with -c */
1141 if (cmdstr
&& cmdstr
[0]) {
1147 while((cmd
=next_command(&p
)) != NULL
) {
1148 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1151 result
= NT_STATUS_IS_ERR(cmd_result
);
1157 /* Loop around accepting commands */
1162 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1167 if (line
[0] != '\n')
1168 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);