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 enum pipe_auth_type_spnego
{
34 PIPE_AUTH_TYPE_SPNEGO_NONE
= 0,
35 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
,
36 PIPE_AUTH_TYPE_SPNEGO_KRB5
39 struct dom_sid domain_sid
;
41 static enum dcerpc_AuthType pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
42 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type
= 0;
43 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
44 static unsigned int timeout
= 0;
45 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
47 struct user_auth_info
*rpcclient_auth_info
;
49 /* List to hold groups of commands.
51 * Commands are defined in a list of arrays: arrays are easy to
52 * statically declare, and lists are easier to dynamically extend.
55 static struct cmd_list
{
56 struct cmd_list
*prev
, *next
;
57 struct cmd_set
*cmd_set
;
60 /****************************************************************************
61 handle completion of commands for readline
62 ****************************************************************************/
63 static char **completion_fn(const char *text
, int start
, int end
)
65 #define MAX_COMPLETIONS 1000
68 struct cmd_list
*commands
= cmd_list
;
71 /* FIXME!!! -- what to do when completing argument? */
72 /* for words not at the start of the line fallback
73 to filename completion */
78 /* make sure we have a list of valid commands */
83 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
88 matches
[count
++] = SMB_STRDUP(text
);
94 while (commands
&& count
< MAX_COMPLETIONS
-1) {
95 if (!commands
->cmd_set
) {
99 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
100 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
101 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
102 commands
->cmd_set
[i
].ntfn
) ||
103 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
104 commands
->cmd_set
[i
].wfn
))) {
105 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
106 if (!matches
[count
]) {
107 for (i
= 0; i
< count
; i
++) {
108 SAFE_FREE(matches
[count
]);
116 commands
= commands
->next
;
120 SAFE_FREE(matches
[0]);
121 matches
[0] = SMB_STRDUP(matches
[1]);
123 matches
[count
] = NULL
;
127 static char *next_command (char **cmdstr
)
132 if (!cmdstr
|| !(*cmdstr
))
135 p
= strchr_m(*cmdstr
, ';');
138 command
= SMB_STRDUP(*cmdstr
);
147 /* Fetch the SID for this computer */
149 static void fetch_machine_sid(struct cli_state
*cli
)
151 struct policy_handle pol
;
152 NTSTATUS result
= NT_STATUS_OK
, status
;
153 static bool got_domain_sid
;
155 struct rpc_pipe_client
*lsapipe
= NULL
;
156 union lsa_PolicyInformation
*info
= NULL
;
157 struct dcerpc_binding_handle
*b
;
159 if (got_domain_sid
) return;
161 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
162 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
166 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
168 if (!NT_STATUS_IS_OK(result
)) {
169 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
173 b
= lsapipe
->binding_handle
;
175 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
176 SEC_FLAG_MAXIMUM_ALLOWED
,
178 if (!NT_STATUS_IS_OK(result
)) {
182 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
184 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
187 if (!NT_STATUS_IS_OK(status
)) {
191 if (!NT_STATUS_IS_OK(result
)) {
195 got_domain_sid
= True
;
196 sid_copy(&domain_sid
, info
->account_domain
.sid
);
198 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
199 TALLOC_FREE(lsapipe
);
200 talloc_destroy(mem_ctx
);
207 TALLOC_FREE(lsapipe
);
210 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
212 if (!NT_STATUS_IS_OK(result
)) {
213 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
219 /* List the available commands on a given pipe */
221 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
222 int argc
, const char **argv
)
224 struct cmd_list
*tmp
;
225 struct cmd_set
*tmp_set
;
231 printf("Usage: %s <pipe>\n", argv
[0]);
235 /* Help on one command */
237 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
239 tmp_set
= tmp
->cmd_set
;
241 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
243 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
247 while(tmp_set
->name
) {
248 printf("%30s", tmp_set
->name
);
255 /* drop out of the loop */
264 /* Display help on commands */
266 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
267 int argc
, const char **argv
)
269 struct cmd_list
*tmp
;
270 struct cmd_set
*tmp_set
;
275 printf("Usage: %s [command]\n", argv
[0]);
279 /* Help on one command */
282 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
284 tmp_set
= tmp
->cmd_set
;
286 while(tmp_set
->name
) {
287 if (strequal(argv
[1], tmp_set
->name
)) {
288 if (tmp_set
->usage
&&
290 printf("%s\n", tmp_set
->usage
);
292 printf("No help for %s\n", tmp_set
->name
);
301 printf("No such command: %s\n", argv
[1]);
305 /* List all commands */
307 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
309 tmp_set
= tmp
->cmd_set
;
311 while(tmp_set
->name
) {
313 printf("%15s\t\t%s\n", tmp_set
->name
,
314 tmp_set
->description
? tmp_set
->description
:
324 /* Change the debug level */
326 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
327 int argc
, const char **argv
)
330 printf("Usage: %s [debuglevel]\n", argv
[0]);
335 lp_set_cmdline("log level", argv
[1]);
338 printf("debuglevel is %d\n", DEBUGLEVEL
);
343 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
344 int argc
, const char **argv
)
347 return NT_STATUS_OK
; /* NOTREACHED */
350 static NTSTATUS
cmd_set_ss_level(void)
352 struct cmd_list
*tmp
;
354 /* Close any existing connections not at this level. */
356 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
357 struct cmd_set
*tmp_set
;
359 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
360 if (tmp_set
->rpc_pipe
== NULL
) {
364 if ((tmp_set
->rpc_pipe
->auth
->auth_type
365 != pipe_default_auth_type
)
366 || (tmp_set
->rpc_pipe
->auth
->auth_level
367 != pipe_default_auth_level
)) {
368 TALLOC_FREE(tmp_set
->rpc_pipe
);
369 tmp_set
->rpc_pipe
= NULL
;
376 static NTSTATUS
cmd_set_transport(void)
378 struct cmd_list
*tmp
;
380 /* Close any existing connections not at this level. */
382 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
383 struct cmd_set
*tmp_set
;
385 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
386 if (tmp_set
->rpc_pipe
== NULL
) {
390 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
391 TALLOC_FREE(tmp_set
->rpc_pipe
);
392 tmp_set
->rpc_pipe
= NULL
;
399 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
400 int argc
, const char **argv
)
402 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
403 const char *type
= "NTLMSSP";
405 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
406 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
409 printf("Usage: %s %s\n", argv
[0], p
);
415 if (strequal(type
, "KRB5")) {
416 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
417 } else if (strequal(type
, "KRB5_SPNEGO")) {
418 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
419 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
420 } else if (strequal(type
, "NTLMSSP")) {
421 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
422 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
423 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
424 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
425 } else if (strequal(type
, "SCHANNEL")) {
426 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
428 printf("unknown type %s\n", type
);
429 printf("Usage: %s %s\n", argv
[0], p
);
430 return NT_STATUS_INVALID_LEVEL
;
434 d_printf("Setting %s - sign\n", type
);
436 return cmd_set_ss_level();
439 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
440 int argc
, const char **argv
)
442 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
443 const char *type
= "NTLMSSP";
445 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
446 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
449 printf("Usage: %s %s\n", argv
[0], p
);
455 if (strequal(type
, "KRB5")) {
456 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
457 } else if (strequal(type
, "KRB5_SPNEGO")) {
458 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
459 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
460 } else if (strequal(type
, "NTLMSSP")) {
461 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
462 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
463 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
464 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
465 } else if (strequal(type
, "SCHANNEL")) {
466 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
468 printf("unknown type %s\n", type
);
469 printf("Usage: %s %s\n", argv
[0], p
);
470 return NT_STATUS_INVALID_LEVEL
;
474 d_printf("Setting %s - sign and seal\n", type
);
476 return cmd_set_ss_level();
479 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
480 int argc
, const char **argv
)
482 struct cmd_list
*tmp
;
485 printf("Usage: %s timeout\n", argv
[0]);
490 timeout
= atoi(argv
[1]);
492 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
494 struct cmd_set
*tmp_set
;
496 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
497 if (tmp_set
->rpc_pipe
== NULL
) {
501 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
506 printf("timeout is %d\n", timeout
);
512 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
513 int argc
, const char **argv
)
515 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
516 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
517 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
519 return cmd_set_ss_level();
522 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
523 int argc
, const char **argv
)
525 d_printf("Setting schannel - sign and seal\n");
526 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
527 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
529 return cmd_set_ss_level();
532 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
533 int argc
, const char **argv
)
535 d_printf("Setting schannel - sign only\n");
536 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
537 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
539 return cmd_set_ss_level();
542 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
543 int argc
, const char **argv
)
548 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
552 if (strequal(argv
[1], "NCACN_NP")) {
553 default_transport
= NCACN_NP
;
554 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
555 default_transport
= NCACN_IP_TCP
;
557 printf("transport type: %s unknown or not supported\n", argv
[1]);
558 return NT_STATUS_NOT_SUPPORTED
;
561 status
= cmd_set_transport();
562 if (!NT_STATUS_IS_OK(status
)) {
566 printf("default transport is now: %s\n", argv
[1]);
571 /* Built in rpcclient commands */
573 static struct cmd_set rpcclient_commands
[] = {
575 { "GENERAL OPTIONS" },
577 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
578 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
579 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
580 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
581 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
582 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
583 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
584 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
585 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
586 { "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.", "" },
587 { "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.", "" },
588 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseonds) for RPC operations", "" },
589 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
590 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
595 static struct cmd_set separator_command
[] = {
596 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
601 /* Various pipe commands */
603 extern struct cmd_set lsarpc_commands
[];
604 extern struct cmd_set samr_commands
[];
605 extern struct cmd_set spoolss_commands
[];
606 extern struct cmd_set netlogon_commands
[];
607 extern struct cmd_set srvsvc_commands
[];
608 extern struct cmd_set dfs_commands
[];
609 extern struct cmd_set ds_commands
[];
610 extern struct cmd_set echo_commands
[];
611 extern struct cmd_set epmapper_commands
[];
612 extern struct cmd_set shutdown_commands
[];
613 extern struct cmd_set test_commands
[];
614 extern struct cmd_set wkssvc_commands
[];
615 extern struct cmd_set ntsvcs_commands
[];
616 extern struct cmd_set drsuapi_commands
[];
617 extern struct cmd_set eventlog_commands
[];
618 extern struct cmd_set winreg_commands
[];
620 static struct cmd_set
*rpcclient_command_list
[] = {
641 static void add_command_set(struct cmd_set
*cmd_set
)
643 struct cmd_list
*entry
;
645 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
646 DEBUG(0, ("out of memory\n"));
652 entry
->cmd_set
= cmd_set
;
653 DLIST_ADD(cmd_list
, entry
);
658 * Call an rpcclient function, passing an argv array.
660 * @param cmd Command to run, as a single string.
662 static NTSTATUS
do_cmd(struct cli_state
*cli
,
663 struct user_auth_info
*auth_info
,
664 struct cmd_set
*cmd_entry
,
665 struct dcerpc_binding
*binding
,
666 int argc
, char **argv
)
675 if (!(mem_ctx
= talloc_init("do_cmd"))) {
676 DEBUG(0, ("talloc_init() failed\n"));
677 return NT_STATUS_NO_MEMORY
;
682 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
683 switch (pipe_default_auth_type
) {
684 case DCERPC_AUTH_TYPE_NONE
:
685 ntresult
= cli_rpc_pipe_open_noauth_transport(
686 cli
, default_transport
,
687 cmd_entry
->interface
,
688 &cmd_entry
->rpc_pipe
);
690 case DCERPC_AUTH_TYPE_SPNEGO
:
691 switch (pipe_default_auth_spnego_type
) {
692 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
693 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
694 cli
, cmd_entry
->interface
,
696 pipe_default_auth_level
,
697 get_cmdline_auth_info_domain(auth_info
),
698 get_cmdline_auth_info_username(auth_info
),
699 get_cmdline_auth_info_password(auth_info
),
700 &cmd_entry
->rpc_pipe
);
702 case PIPE_AUTH_TYPE_SPNEGO_KRB5
:
703 ntresult
= cli_rpc_pipe_open_spnego_krb5(
704 cli
, cmd_entry
->interface
,
706 pipe_default_auth_level
,
709 &cmd_entry
->rpc_pipe
);
712 ntresult
= NT_STATUS_INTERNAL_ERROR
;
715 case DCERPC_AUTH_TYPE_NTLMSSP
:
716 ntresult
= cli_rpc_pipe_open_ntlmssp(
717 cli
, cmd_entry
->interface
,
719 pipe_default_auth_level
,
720 get_cmdline_auth_info_domain(auth_info
),
721 get_cmdline_auth_info_username(auth_info
),
722 get_cmdline_auth_info_password(auth_info
),
723 &cmd_entry
->rpc_pipe
);
725 case DCERPC_AUTH_TYPE_SCHANNEL
:
726 ntresult
= cli_rpc_pipe_open_schannel(
727 cli
, cmd_entry
->interface
,
729 pipe_default_auth_level
,
730 get_cmdline_auth_info_domain(auth_info
),
731 &cmd_entry
->rpc_pipe
);
733 case DCERPC_AUTH_TYPE_KRB5
:
734 ntresult
= cli_rpc_pipe_open_krb5(
735 cli
, cmd_entry
->interface
,
737 pipe_default_auth_level
,
740 &cmd_entry
->rpc_pipe
);
743 DEBUG(0, ("Could not initialise %s. Invalid "
745 get_pipe_name_from_syntax(
747 cmd_entry
->interface
),
748 pipe_default_auth_type
));
749 return NT_STATUS_UNSUCCESSFUL
;
751 if (!NT_STATUS_IS_OK(ntresult
)) {
752 DEBUG(0, ("Could not initialise %s. Error was %s\n",
753 get_pipe_name_from_syntax(
754 talloc_tos(), cmd_entry
->interface
),
755 nt_errstr(ntresult
) ));
759 if (ndr_syntax_id_equal(cmd_entry
->interface
,
760 &ndr_table_netlogon
.syntax_id
)) {
761 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
762 enum netr_SchannelType sec_channel_type
;
763 uchar trust_password
[16];
764 const char *machine_account
;
766 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
767 trust_password
, &machine_account
,
770 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
773 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
774 cli
->desthost
, /* server name */
775 get_cmdline_auth_info_domain(auth_info
), /* domain */
776 global_myname(), /* client name */
777 machine_account
, /* machine account name */
782 if (!NT_STATUS_IS_OK(ntresult
)) {
783 DEBUG(0, ("Could not initialise credentials for %s.\n",
784 get_pipe_name_from_syntax(
786 cmd_entry
->interface
)));
794 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
795 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
796 if (!NT_STATUS_IS_OK(ntresult
)) {
797 printf("result was %s\n", nt_errstr(ntresult
));
800 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
801 /* print out the DOS error */
802 if (!W_ERROR_IS_OK(wresult
)) {
803 printf( "result was %s\n", win_errstr(wresult
));
805 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
810 talloc_destroy(mem_ctx
);
817 * Process a command entered at the prompt or as part of -c
819 * @returns The NTSTATUS from running the command.
821 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
822 struct cli_state
*cli
,
823 struct dcerpc_binding
*binding
,
826 struct cmd_list
*temp_list
;
827 NTSTATUS result
= NT_STATUS_OK
;
832 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
833 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
834 return NT_STATUS_UNSUCCESSFUL
;
838 /* Walk through a dlist of arrays of commands. */
839 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
840 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
842 while (temp_set
->name
) {
843 if (strequal(argv
[0], temp_set
->name
)) {
844 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
845 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
846 fprintf (stderr
, "Invalid command\n");
850 result
= do_cmd(cli
, auth_info
, temp_set
,
851 binding
, argc
, argv
);
860 printf("command not found: %s\n", argv
[0]);
865 if (!NT_STATUS_IS_OK(result)) {
866 printf("result was %s\n", nt_errstr(result));
870 /* NOTE: popt allocates the whole argv, including the
871 * strings, as a single block. So a single free is
872 * enough to release it -- we don't free the
873 * individual strings. rtfm. */
882 int main(int argc
, char *argv
[])
885 static char *cmdstr
= NULL
;
887 struct cli_state
*cli
= NULL
;
888 static char *opt_ipaddr
=NULL
;
889 struct cmd_set
**cmd_set
;
890 struct sockaddr_storage server_ss
;
892 static int opt_port
= 0;
893 fstring new_workgroup
;
895 TALLOC_CTX
*frame
= talloc_stackframe();
897 struct dcerpc_binding
*binding
= NULL
;
898 const char *binding_string
= NULL
;
899 char *user
, *domain
, *q
;
901 /* make sure the vars that get altered (4th field) are in
902 a fixed location or certain compilers complain */
904 struct poptOption long_options
[] = {
906 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
907 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
908 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
910 POPT_COMMON_CONNECTION
911 POPT_COMMON_CREDENTIALS
917 zero_sockaddr(&server_ss
);
921 /* the following functions are part of the Samba debugging
922 facilities. See lib/debug.c */
923 setup_logging("rpcclient", DEBUG_STDOUT
);
925 rpcclient_auth_info
= user_auth_info_init(frame
);
926 if (rpcclient_auth_info
== NULL
) {
929 popt_common_set_auth_info(rpcclient_auth_info
);
933 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
937 poptPrintHelp(pc
, stderr
, 0);
941 while((opt
= poptGetNextOpt(pc
)) != -1) {
945 if (!interpret_string_addr(&server_ss
,
948 fprintf(stderr
, "%s not a valid IP address\n",
956 /* Get server as remaining unparsed argument. Print usage if more
957 than one unparsed argument is present. */
959 server
= poptGetArg(pc
);
961 if (!server
|| poptGetArg(pc
)) {
962 poptPrintHelp(pc
, stderr
, 0);
976 /* save the workgroup...
978 FIXME!! do we need to do this for other options as well
979 (or maybe a generic way to keep lp_load() from overwriting
982 fstrcpy( new_workgroup
, lp_workgroup() );
984 /* Load smb.conf file */
986 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
987 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
989 if ( strlen(new_workgroup
) != 0 )
990 set_global_myworkgroup( new_workgroup
);
994 * from stdin if necessary
997 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
998 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
1003 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
1005 if ((server
[0] == '/' && server
[1] == '/') ||
1006 (server
[0] == '\\' && server
[1] == '\\')) {
1010 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1012 if (!NT_STATUS_IS_OK(nt_status
)) {
1014 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1015 strip_hostname(server
));
1016 if (!binding_string
) {
1021 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1022 if (!NT_STATUS_IS_OK(nt_status
)) {
1028 if (binding
->transport
== NCA_UNKNOWN
) {
1029 binding
->transport
= NCACN_NP
;
1032 if (binding
->flags
& DCERPC_SIGN
) {
1033 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
1034 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1036 if (binding
->flags
& DCERPC_SEAL
) {
1037 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
1038 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1040 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
1041 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1042 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1044 if (binding
->flags
& DCERPC_AUTH_NTLM
) {
1045 /* If neither Integrity or Privacy are requested then
1046 * Use just Connect level */
1047 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1048 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1051 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1052 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1054 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1057 if (binding
->flags
& DCERPC_AUTH_KRB5
) {
1058 /* If neither Integrity or Privacy are requested then
1059 * Use just Connect level */
1060 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1061 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1064 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1065 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1067 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1071 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1072 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1073 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1075 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1076 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1079 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1080 SMB_ASSERT(user
!= NULL
);
1081 domain
= talloc_strdup(frame
, lp_workgroup());
1082 SMB_ASSERT(domain
!= NULL
);
1083 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1085 if ((q
= strchr_m(user
,'\\'))) {
1087 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1088 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1092 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
1093 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1095 get_cmdline_auth_info_username(rpcclient_auth_info
),
1096 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1097 get_cmdline_auth_info_password(rpcclient_auth_info
),
1099 get_cmdline_auth_info_signing_state(rpcclient_auth_info
));
1101 if (!NT_STATUS_IS_OK(nt_status
)) {
1102 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1107 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1108 nt_status
= cli_cm_force_encryption(cli
,
1109 get_cmdline_auth_info_username(rpcclient_auth_info
),
1110 get_cmdline_auth_info_password(rpcclient_auth_info
),
1111 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1113 if (!NT_STATUS_IS_OK(nt_status
)) {
1119 #if 0 /* COMMENT OUT FOR TESTING */
1120 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1123 /* Load command lists */
1125 timeout
= cli_set_timeout(cli
, 10000);
1127 cmd_set
= rpcclient_command_list
;
1130 add_command_set(*cmd_set
);
1131 add_command_set(separator_command
);
1135 default_transport
= binding
->transport
;
1137 fetch_machine_sid(cli
);
1139 /* Do anything specified with -c */
1140 if (cmdstr
&& cmdstr
[0]) {
1146 while((cmd
=next_command(&p
)) != NULL
) {
1147 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1150 result
= NT_STATUS_IS_ERR(cmd_result
);
1156 /* Loop around accepting commands */
1161 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1166 if (line
[0] != '\n')
1167 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);