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/cli_lsa.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
;
153 static bool got_domain_sid
;
155 struct rpc_pipe_client
*lsapipe
= NULL
;
156 union lsa_PolicyInformation
*info
= NULL
;
158 if (got_domain_sid
) return;
160 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
161 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
165 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
167 if (!NT_STATUS_IS_OK(result
)) {
168 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
172 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
173 SEC_FLAG_MAXIMUM_ALLOWED
,
175 if (!NT_STATUS_IS_OK(result
)) {
179 result
= rpccli_lsa_QueryInfoPolicy(lsapipe
, mem_ctx
,
181 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
183 if (!NT_STATUS_IS_OK(result
)) {
187 got_domain_sid
= True
;
188 sid_copy(&domain_sid
, info
->account_domain
.sid
);
190 rpccli_lsa_Close(lsapipe
, mem_ctx
, &pol
);
191 TALLOC_FREE(lsapipe
);
192 talloc_destroy(mem_ctx
);
199 TALLOC_FREE(lsapipe
);
202 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
204 if (!NT_STATUS_IS_OK(result
)) {
205 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
211 /* List the available commands on a given pipe */
213 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
214 int argc
, const char **argv
)
216 struct cmd_list
*tmp
;
217 struct cmd_set
*tmp_set
;
223 printf("Usage: %s <pipe>\n", argv
[0]);
227 /* Help on one command */
229 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
231 tmp_set
= tmp
->cmd_set
;
233 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
235 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
239 while(tmp_set
->name
) {
240 printf("%30s", tmp_set
->name
);
247 /* drop out of the loop */
256 /* Display help on commands */
258 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
259 int argc
, const char **argv
)
261 struct cmd_list
*tmp
;
262 struct cmd_set
*tmp_set
;
267 printf("Usage: %s [command]\n", argv
[0]);
271 /* Help on one command */
274 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
276 tmp_set
= tmp
->cmd_set
;
278 while(tmp_set
->name
) {
279 if (strequal(argv
[1], tmp_set
->name
)) {
280 if (tmp_set
->usage
&&
282 printf("%s\n", tmp_set
->usage
);
284 printf("No help for %s\n", tmp_set
->name
);
293 printf("No such command: %s\n", argv
[1]);
297 /* List all commands */
299 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
301 tmp_set
= tmp
->cmd_set
;
303 while(tmp_set
->name
) {
305 printf("%15s\t\t%s\n", tmp_set
->name
,
306 tmp_set
->description
? tmp_set
->description
:
316 /* Change the debug level */
318 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
319 int argc
, const char **argv
)
322 printf("Usage: %s [debuglevel]\n", argv
[0]);
327 lp_set_cmdline("log level", argv
[1]);
330 printf("debuglevel is %d\n", DEBUGLEVEL
);
335 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
336 int argc
, const char **argv
)
339 return NT_STATUS_OK
; /* NOTREACHED */
342 static NTSTATUS
cmd_set_ss_level(void)
344 struct cmd_list
*tmp
;
346 /* Close any existing connections not at this level. */
348 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
349 struct cmd_set
*tmp_set
;
351 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
352 if (tmp_set
->rpc_pipe
== NULL
) {
356 if ((tmp_set
->rpc_pipe
->auth
->auth_type
357 != pipe_default_auth_type
)
358 || (tmp_set
->rpc_pipe
->auth
->auth_level
359 != pipe_default_auth_level
)) {
360 TALLOC_FREE(tmp_set
->rpc_pipe
);
361 tmp_set
->rpc_pipe
= NULL
;
368 static NTSTATUS
cmd_set_transport(void)
370 struct cmd_list
*tmp
;
372 /* Close any existing connections not at this level. */
374 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
375 struct cmd_set
*tmp_set
;
377 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
378 if (tmp_set
->rpc_pipe
== NULL
) {
382 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
383 TALLOC_FREE(tmp_set
->rpc_pipe
);
384 tmp_set
->rpc_pipe
= NULL
;
391 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
392 int argc
, const char **argv
)
394 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
395 const char *type
= "NTLMSSP";
397 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
398 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
401 printf("Usage: %s %s\n", argv
[0], p
);
407 if (strequal(type
, "KRB5")) {
408 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
409 } else if (strequal(type
, "KRB5_SPNEGO")) {
410 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
411 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
412 } else if (strequal(type
, "NTLMSSP")) {
413 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
414 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
415 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
416 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
417 } else if (strequal(type
, "SCHANNEL")) {
418 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
420 printf("unknown type %s\n", type
);
421 printf("Usage: %s %s\n", argv
[0], p
);
422 return NT_STATUS_INVALID_LEVEL
;
426 d_printf("Setting %s - sign\n", type
);
428 return cmd_set_ss_level();
431 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
432 int argc
, const char **argv
)
434 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
435 const char *type
= "NTLMSSP";
437 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
438 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
441 printf("Usage: %s %s\n", argv
[0], p
);
447 if (strequal(type
, "KRB5")) {
448 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
449 } else if (strequal(type
, "KRB5_SPNEGO")) {
450 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
451 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
452 } else if (strequal(type
, "NTLMSSP")) {
453 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
454 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
455 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
456 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
457 } else if (strequal(type
, "SCHANNEL")) {
458 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
460 printf("unknown type %s\n", type
);
461 printf("Usage: %s %s\n", argv
[0], p
);
462 return NT_STATUS_INVALID_LEVEL
;
466 d_printf("Setting %s - sign and seal\n", type
);
468 return cmd_set_ss_level();
471 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
472 int argc
, const char **argv
)
474 struct cmd_list
*tmp
;
477 printf("Usage: %s timeout\n", argv
[0]);
482 timeout
= atoi(argv
[1]);
484 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
486 struct cmd_set
*tmp_set
;
488 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
489 if (tmp_set
->rpc_pipe
== NULL
) {
493 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
498 printf("timeout is %d\n", timeout
);
504 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
505 int argc
, const char **argv
)
507 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
508 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
509 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
511 return cmd_set_ss_level();
514 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
515 int argc
, const char **argv
)
517 d_printf("Setting schannel - sign and seal\n");
518 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
519 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
521 return cmd_set_ss_level();
524 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
525 int argc
, const char **argv
)
527 d_printf("Setting schannel - sign only\n");
528 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
529 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
531 return cmd_set_ss_level();
534 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
535 int argc
, const char **argv
)
540 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
544 if (strequal(argv
[1], "NCACN_NP")) {
545 default_transport
= NCACN_NP
;
546 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
547 default_transport
= NCACN_IP_TCP
;
549 printf("transport type: %s unknown or not supported\n", argv
[1]);
550 return NT_STATUS_NOT_SUPPORTED
;
553 status
= cmd_set_transport();
554 if (!NT_STATUS_IS_OK(status
)) {
558 printf("default transport is now: %s\n", argv
[1]);
563 /* Built in rpcclient commands */
565 static struct cmd_set rpcclient_commands
[] = {
567 { "GENERAL OPTIONS" },
569 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
570 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
571 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
572 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
573 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
574 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
575 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
576 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
577 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
578 { "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.", "" },
579 { "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.", "" },
580 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseonds) for RPC operations", "" },
581 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
582 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
587 static struct cmd_set separator_command
[] = {
588 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
593 /* Various pipe commands */
595 extern struct cmd_set lsarpc_commands
[];
596 extern struct cmd_set samr_commands
[];
597 extern struct cmd_set spoolss_commands
[];
598 extern struct cmd_set netlogon_commands
[];
599 extern struct cmd_set srvsvc_commands
[];
600 extern struct cmd_set dfs_commands
[];
601 extern struct cmd_set ds_commands
[];
602 extern struct cmd_set echo_commands
[];
603 extern struct cmd_set epmapper_commands
[];
604 extern struct cmd_set shutdown_commands
[];
605 extern struct cmd_set test_commands
[];
606 extern struct cmd_set wkssvc_commands
[];
607 extern struct cmd_set ntsvcs_commands
[];
608 extern struct cmd_set drsuapi_commands
[];
609 extern struct cmd_set eventlog_commands
[];
610 extern struct cmd_set winreg_commands
[];
612 static struct cmd_set
*rpcclient_command_list
[] = {
633 static void add_command_set(struct cmd_set
*cmd_set
)
635 struct cmd_list
*entry
;
637 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
638 DEBUG(0, ("out of memory\n"));
644 entry
->cmd_set
= cmd_set
;
645 DLIST_ADD(cmd_list
, entry
);
650 * Call an rpcclient function, passing an argv array.
652 * @param cmd Command to run, as a single string.
654 static NTSTATUS
do_cmd(struct cli_state
*cli
,
655 struct user_auth_info
*auth_info
,
656 struct cmd_set
*cmd_entry
,
657 struct dcerpc_binding
*binding
,
658 int argc
, char **argv
)
667 if (!(mem_ctx
= talloc_init("do_cmd"))) {
668 DEBUG(0, ("talloc_init() failed\n"));
669 return NT_STATUS_NO_MEMORY
;
674 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
675 switch (pipe_default_auth_type
) {
676 case DCERPC_AUTH_TYPE_NONE
:
677 ntresult
= cli_rpc_pipe_open_noauth_transport(
678 cli
, default_transport
,
679 cmd_entry
->interface
,
680 &cmd_entry
->rpc_pipe
);
682 case DCERPC_AUTH_TYPE_SPNEGO
:
683 switch (pipe_default_auth_spnego_type
) {
684 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
685 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
686 cli
, cmd_entry
->interface
,
688 pipe_default_auth_level
,
689 get_cmdline_auth_info_domain(auth_info
),
690 get_cmdline_auth_info_username(auth_info
),
691 get_cmdline_auth_info_password(auth_info
),
692 &cmd_entry
->rpc_pipe
);
694 case PIPE_AUTH_TYPE_SPNEGO_KRB5
:
695 ntresult
= cli_rpc_pipe_open_spnego_krb5(
696 cli
, cmd_entry
->interface
,
698 pipe_default_auth_level
,
701 &cmd_entry
->rpc_pipe
);
704 ntresult
= NT_STATUS_INTERNAL_ERROR
;
707 case DCERPC_AUTH_TYPE_NTLMSSP
:
708 ntresult
= cli_rpc_pipe_open_ntlmssp(
709 cli
, cmd_entry
->interface
,
711 pipe_default_auth_level
,
712 get_cmdline_auth_info_domain(auth_info
),
713 get_cmdline_auth_info_username(auth_info
),
714 get_cmdline_auth_info_password(auth_info
),
715 &cmd_entry
->rpc_pipe
);
717 case DCERPC_AUTH_TYPE_SCHANNEL
:
718 ntresult
= cli_rpc_pipe_open_schannel(
719 cli
, cmd_entry
->interface
,
721 pipe_default_auth_level
,
722 get_cmdline_auth_info_domain(auth_info
),
723 &cmd_entry
->rpc_pipe
);
725 case DCERPC_AUTH_TYPE_KRB5
:
726 ntresult
= cli_rpc_pipe_open_krb5(
727 cli
, cmd_entry
->interface
,
729 pipe_default_auth_level
,
732 &cmd_entry
->rpc_pipe
);
735 DEBUG(0, ("Could not initialise %s. Invalid "
737 get_pipe_name_from_syntax(
739 cmd_entry
->interface
),
740 pipe_default_auth_type
));
741 return NT_STATUS_UNSUCCESSFUL
;
743 if (!NT_STATUS_IS_OK(ntresult
)) {
744 DEBUG(0, ("Could not initialise %s. Error was %s\n",
745 get_pipe_name_from_syntax(
746 talloc_tos(), cmd_entry
->interface
),
747 nt_errstr(ntresult
) ));
751 if (ndr_syntax_id_equal(cmd_entry
->interface
,
752 &ndr_table_netlogon
.syntax_id
)) {
753 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
754 enum netr_SchannelType sec_channel_type
;
755 uchar trust_password
[16];
756 const char *machine_account
;
758 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
759 trust_password
, &machine_account
,
762 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
765 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
766 cli
->desthost
, /* server name */
767 get_cmdline_auth_info_domain(auth_info
), /* domain */
768 global_myname(), /* client name */
769 machine_account
, /* machine account name */
774 if (!NT_STATUS_IS_OK(ntresult
)) {
775 DEBUG(0, ("Could not initialise credentials for %s.\n",
776 get_pipe_name_from_syntax(
778 cmd_entry
->interface
)));
786 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
787 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
788 if (!NT_STATUS_IS_OK(ntresult
)) {
789 printf("result was %s\n", nt_errstr(ntresult
));
792 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
793 /* print out the DOS error */
794 if (!W_ERROR_IS_OK(wresult
)) {
795 printf( "result was %s\n", win_errstr(wresult
));
797 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
802 talloc_destroy(mem_ctx
);
809 * Process a command entered at the prompt or as part of -c
811 * @returns The NTSTATUS from running the command.
813 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
814 struct cli_state
*cli
,
815 struct dcerpc_binding
*binding
,
818 struct cmd_list
*temp_list
;
819 NTSTATUS result
= NT_STATUS_OK
;
824 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
825 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
826 return NT_STATUS_UNSUCCESSFUL
;
830 /* Walk through a dlist of arrays of commands. */
831 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
832 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
834 while (temp_set
->name
) {
835 if (strequal(argv
[0], temp_set
->name
)) {
836 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
837 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
838 fprintf (stderr
, "Invalid command\n");
842 result
= do_cmd(cli
, auth_info
, temp_set
,
843 binding
, argc
, argv
);
852 printf("command not found: %s\n", argv
[0]);
857 if (!NT_STATUS_IS_OK(result)) {
858 printf("result was %s\n", nt_errstr(result));
862 /* NOTE: popt allocates the whole argv, including the
863 * strings, as a single block. So a single free is
864 * enough to release it -- we don't free the
865 * individual strings. rtfm. */
874 int main(int argc
, char *argv
[])
877 static char *cmdstr
= NULL
;
879 struct cli_state
*cli
= NULL
;
880 static char *opt_ipaddr
=NULL
;
881 struct cmd_set
**cmd_set
;
882 struct sockaddr_storage server_ss
;
884 static int opt_port
= 0;
885 fstring new_workgroup
;
887 TALLOC_CTX
*frame
= talloc_stackframe();
889 struct dcerpc_binding
*binding
= NULL
;
890 const char *binding_string
= NULL
;
891 char *user
, *domain
, *q
;
893 /* make sure the vars that get altered (4th field) are in
894 a fixed location or certain compilers complain */
896 struct poptOption long_options
[] = {
898 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
899 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
900 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
902 POPT_COMMON_CONNECTION
903 POPT_COMMON_CREDENTIALS
909 zero_sockaddr(&server_ss
);
913 /* the following functions are part of the Samba debugging
914 facilities. See lib/debug.c */
915 setup_logging("rpcclient", DEBUG_STDOUT
);
917 rpcclient_auth_info
= user_auth_info_init(frame
);
918 if (rpcclient_auth_info
== NULL
) {
921 popt_common_set_auth_info(rpcclient_auth_info
);
925 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
929 poptPrintHelp(pc
, stderr
, 0);
933 while((opt
= poptGetNextOpt(pc
)) != -1) {
937 if (!interpret_string_addr(&server_ss
,
940 fprintf(stderr
, "%s not a valid IP address\n",
948 /* Get server as remaining unparsed argument. Print usage if more
949 than one unparsed argument is present. */
951 server
= poptGetArg(pc
);
953 if (!server
|| poptGetArg(pc
)) {
954 poptPrintHelp(pc
, stderr
, 0);
968 /* save the workgroup...
970 FIXME!! do we need to do this for other options as well
971 (or maybe a generic way to keep lp_load() from overwriting
974 fstrcpy( new_workgroup
, lp_workgroup() );
976 /* Load smb.conf file */
978 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
979 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
981 if ( strlen(new_workgroup
) != 0 )
982 set_global_myworkgroup( new_workgroup
);
986 * from stdin if necessary
989 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
990 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
995 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
997 if ((server
[0] == '/' && server
[1] == '/') ||
998 (server
[0] == '\\' && server
[1] == '\\')) {
1002 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1004 if (!NT_STATUS_IS_OK(nt_status
)) {
1006 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1007 strip_hostname(server
));
1008 if (!binding_string
) {
1013 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1014 if (!NT_STATUS_IS_OK(nt_status
)) {
1020 if (binding
->transport
== NCA_UNKNOWN
) {
1021 binding
->transport
= NCACN_NP
;
1024 if (binding
->flags
& DCERPC_SIGN
) {
1025 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
1026 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1028 if (binding
->flags
& DCERPC_SEAL
) {
1029 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
1030 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1032 if (binding
->flags
& DCERPC_AUTH_SPNEGO
) {
1033 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1034 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1036 if (binding
->flags
& DCERPC_AUTH_NTLM
) {
1037 /* If neither Integrity or Privacy are requested then
1038 * Use just Connect level */
1039 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1040 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1043 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1044 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1046 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1049 if (binding
->flags
& DCERPC_AUTH_KRB5
) {
1050 /* If neither Integrity or Privacy are requested then
1051 * Use just Connect level */
1052 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1053 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1056 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1057 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1059 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1063 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1064 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1065 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1067 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1068 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1071 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1072 SMB_ASSERT(user
!= NULL
);
1073 domain
= talloc_strdup(frame
, lp_workgroup());
1074 SMB_ASSERT(domain
!= NULL
);
1075 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1077 if ((q
= strchr_m(user
,'\\'))) {
1079 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1080 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1084 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
1085 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1087 get_cmdline_auth_info_username(rpcclient_auth_info
),
1088 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1089 get_cmdline_auth_info_password(rpcclient_auth_info
),
1091 get_cmdline_auth_info_signing_state(rpcclient_auth_info
));
1093 if (!NT_STATUS_IS_OK(nt_status
)) {
1094 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1099 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1100 nt_status
= cli_cm_force_encryption(cli
,
1101 get_cmdline_auth_info_username(rpcclient_auth_info
),
1102 get_cmdline_auth_info_password(rpcclient_auth_info
),
1103 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1105 if (!NT_STATUS_IS_OK(nt_status
)) {
1111 #if 0 /* COMMENT OUT FOR TESTING */
1112 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1115 /* Load command lists */
1117 timeout
= cli_set_timeout(cli
, 10000);
1119 cmd_set
= rpcclient_command_list
;
1122 add_command_set(*cmd_set
);
1123 add_command_set(separator_command
);
1127 default_transport
= binding
->transport
;
1129 fetch_machine_sid(cli
);
1131 /* Do anything specified with -c */
1132 if (cmdstr
&& cmdstr
[0]) {
1138 while((cmd
=next_command(&p
)) != NULL
) {
1139 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1142 result
= NT_STATUS_IS_ERR(cmd_result
);
1148 /* Loop around accepting commands */
1153 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1158 if (line
[0] != '\n')
1159 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);