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"
34 #include "auth/gensec/gensec.h"
35 #include "../libcli/smb/smbXcli_base.h"
38 enum pipe_auth_type_spnego
{
39 PIPE_AUTH_TYPE_SPNEGO_NONE
= 0,
40 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
,
41 PIPE_AUTH_TYPE_SPNEGO_KRB5
44 struct dom_sid domain_sid
;
46 static enum dcerpc_AuthType pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
47 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type
= 0;
48 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
49 static unsigned int timeout
= 0;
50 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
52 struct messaging_context
*rpcclient_msg_ctx
;
53 struct user_auth_info
*rpcclient_auth_info
;
54 struct cli_state
*rpcclient_cli_state
;
55 struct netlogon_creds_cli_context
*rpcclient_netlogon_creds
;
57 /* List to hold groups of commands.
59 * Commands are defined in a list of arrays: arrays are easy to
60 * statically declare, and lists are easier to dynamically extend.
63 static struct cmd_list
{
64 struct cmd_list
*prev
, *next
;
65 struct cmd_set
*cmd_set
;
68 /****************************************************************************
69 handle completion of commands for readline
70 ****************************************************************************/
71 static char **completion_fn(const char *text
, int start
, int end
)
73 #define MAX_COMPLETIONS 1000
76 struct cmd_list
*commands
= cmd_list
;
79 /* FIXME!!! -- what to do when completing argument? */
80 /* for words not at the start of the line fallback
81 to filename completion */
86 /* make sure we have a list of valid commands */
91 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
96 matches
[count
++] = SMB_STRDUP(text
);
102 while (commands
&& count
< MAX_COMPLETIONS
-1) {
103 if (!commands
->cmd_set
) {
107 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
108 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
109 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
110 commands
->cmd_set
[i
].ntfn
) ||
111 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
112 commands
->cmd_set
[i
].wfn
))) {
113 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
114 if (!matches
[count
]) {
115 for (i
= 0; i
< count
; i
++) {
116 SAFE_FREE(matches
[count
]);
124 commands
= commands
->next
;
128 SAFE_FREE(matches
[0]);
129 matches
[0] = SMB_STRDUP(matches
[1]);
131 matches
[count
] = NULL
;
135 static char *next_command (char **cmdstr
)
140 if (!cmdstr
|| !(*cmdstr
))
143 p
= strchr_m(*cmdstr
, ';');
146 command
= SMB_STRDUP(*cmdstr
);
155 /* Fetch the SID for this computer */
157 static void fetch_machine_sid(struct cli_state
*cli
)
159 struct policy_handle pol
;
160 NTSTATUS result
= NT_STATUS_OK
, status
;
161 static bool got_domain_sid
;
163 struct rpc_pipe_client
*lsapipe
= NULL
;
164 union lsa_PolicyInformation
*info
= NULL
;
165 struct dcerpc_binding_handle
*b
;
167 if (got_domain_sid
) return;
169 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
170 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
174 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
,
176 if (!NT_STATUS_IS_OK(result
)) {
177 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
181 b
= lsapipe
->binding_handle
;
183 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
184 SEC_FLAG_MAXIMUM_ALLOWED
,
186 if (!NT_STATUS_IS_OK(result
)) {
190 status
= dcerpc_lsa_QueryInfoPolicy(b
, mem_ctx
,
192 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
195 if (!NT_STATUS_IS_OK(status
)) {
199 if (!NT_STATUS_IS_OK(result
)) {
203 got_domain_sid
= True
;
204 sid_copy(&domain_sid
, info
->account_domain
.sid
);
206 dcerpc_lsa_Close(b
, mem_ctx
, &pol
, &result
);
207 TALLOC_FREE(lsapipe
);
208 talloc_destroy(mem_ctx
);
215 TALLOC_FREE(lsapipe
);
218 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
220 if (!NT_STATUS_IS_OK(result
)) {
221 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
227 /* List the available commands on a given pipe */
229 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
230 int argc
, const char **argv
)
232 struct cmd_list
*tmp
;
233 struct cmd_set
*tmp_set
;
239 printf("Usage: %s <pipe>\n", argv
[0]);
243 /* Help on one command */
245 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
247 tmp_set
= tmp
->cmd_set
;
249 if (!strcasecmp_m(argv
[1], tmp_set
->name
))
251 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
255 while(tmp_set
->name
) {
256 printf("%30s", tmp_set
->name
);
263 /* drop out of the loop */
272 /* Display help on commands */
274 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
275 int argc
, const char **argv
)
277 struct cmd_list
*tmp
;
278 struct cmd_set
*tmp_set
;
283 printf("Usage: %s [command]\n", argv
[0]);
287 /* Help on one command */
290 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
292 tmp_set
= tmp
->cmd_set
;
294 while(tmp_set
->name
) {
295 if (strequal(argv
[1], tmp_set
->name
)) {
296 if (tmp_set
->usage
&&
298 printf("%s\n", tmp_set
->usage
);
300 printf("No help for %s\n", tmp_set
->name
);
309 printf("No such command: %s\n", argv
[1]);
313 /* List all commands */
315 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
317 tmp_set
= tmp
->cmd_set
;
319 while(tmp_set
->name
) {
321 printf("%15s\t\t%s\n", tmp_set
->name
,
322 tmp_set
->description
? tmp_set
->description
:
332 /* Change the debug level */
334 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
335 int argc
, const char **argv
)
338 printf("Usage: %s [debuglevel]\n", argv
[0]);
343 lp_set_cmdline("log level", argv
[1]);
346 printf("debuglevel is %d\n", DEBUGLEVEL
);
351 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
352 int argc
, const char **argv
)
355 return NT_STATUS_OK
; /* NOTREACHED */
358 static NTSTATUS
cmd_set_ss_level(void)
360 struct cmd_list
*tmp
;
362 /* Close any existing connections not at this level. */
364 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
365 struct cmd_set
*tmp_set
;
367 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
368 if (tmp_set
->rpc_pipe
== NULL
) {
372 if ((tmp_set
->rpc_pipe
->auth
->auth_type
373 != pipe_default_auth_type
)
374 || (tmp_set
->rpc_pipe
->auth
->auth_level
375 != pipe_default_auth_level
)) {
376 TALLOC_FREE(tmp_set
->rpc_pipe
);
377 tmp_set
->rpc_pipe
= NULL
;
384 static NTSTATUS
cmd_set_transport(void)
386 struct cmd_list
*tmp
;
388 /* Close any existing connections not at this level. */
390 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
391 struct cmd_set
*tmp_set
;
393 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
394 if (tmp_set
->rpc_pipe
== NULL
) {
398 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
399 TALLOC_FREE(tmp_set
->rpc_pipe
);
400 tmp_set
->rpc_pipe
= NULL
;
407 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
408 int argc
, const char **argv
)
410 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
411 const char *type
= "NTLMSSP";
413 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
414 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
417 printf("Usage: %s %s\n", argv
[0], p
);
423 if (strequal(type
, "KRB5")) {
424 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
425 } else if (strequal(type
, "KRB5_SPNEGO")) {
426 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
427 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
428 } else if (strequal(type
, "NTLMSSP")) {
429 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
430 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
431 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
432 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
433 } else if (strequal(type
, "SCHANNEL")) {
434 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
436 printf("unknown type %s\n", type
);
437 printf("Usage: %s %s\n", argv
[0], p
);
438 return NT_STATUS_INVALID_LEVEL
;
442 d_printf("Setting %s - sign\n", type
);
444 return cmd_set_ss_level();
447 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
448 int argc
, const char **argv
)
450 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
451 const char *type
= "NTLMSSP";
453 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
454 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
457 printf("Usage: %s %s\n", argv
[0], p
);
463 if (strequal(type
, "KRB5")) {
464 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
465 } else if (strequal(type
, "KRB5_SPNEGO")) {
466 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
467 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
468 } else if (strequal(type
, "NTLMSSP")) {
469 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
470 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
471 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
472 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
473 } else if (strequal(type
, "SCHANNEL")) {
474 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
476 printf("unknown type %s\n", type
);
477 printf("Usage: %s %s\n", argv
[0], p
);
478 return NT_STATUS_INVALID_LEVEL
;
482 d_printf("Setting %s - sign and seal\n", type
);
484 return cmd_set_ss_level();
487 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
488 int argc
, const char **argv
)
490 struct cmd_list
*tmp
;
493 printf("Usage: %s timeout\n", argv
[0]);
498 timeout
= atoi(argv
[1]);
500 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
502 struct cmd_set
*tmp_set
;
504 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
505 if (tmp_set
->rpc_pipe
== NULL
) {
509 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
514 printf("timeout is %d\n", timeout
);
520 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
521 int argc
, const char **argv
)
523 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
524 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NONE
;
525 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NONE
;
527 return cmd_set_ss_level();
530 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
531 int argc
, const char **argv
)
533 d_printf("Setting schannel - sign and seal\n");
534 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
535 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
537 return cmd_set_ss_level();
540 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
541 int argc
, const char **argv
)
543 d_printf("Setting schannel - sign only\n");
544 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
545 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
547 return cmd_set_ss_level();
550 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
551 int argc
, const char **argv
)
556 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
560 if (strequal(argv
[1], "NCACN_NP")) {
561 default_transport
= NCACN_NP
;
562 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
563 default_transport
= NCACN_IP_TCP
;
565 printf("transport type: %s unknown or not supported\n", argv
[1]);
566 return NT_STATUS_NOT_SUPPORTED
;
569 status
= cmd_set_transport();
570 if (!NT_STATUS_IS_OK(status
)) {
574 printf("default transport is now: %s\n", argv
[1]);
579 /* Built in rpcclient commands */
581 static struct cmd_set rpcclient_commands
[] = {
583 { "GENERAL OPTIONS" },
585 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
586 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
587 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
588 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
589 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
590 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
591 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
592 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
593 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
594 { "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.", "" },
595 { "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.", "" },
596 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseconds) for RPC operations", "" },
597 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
598 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
603 static struct cmd_set separator_command
[] = {
604 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
609 /* Various pipe commands */
611 extern struct cmd_set lsarpc_commands
[];
612 extern struct cmd_set samr_commands
[];
613 extern struct cmd_set spoolss_commands
[];
614 extern struct cmd_set netlogon_commands
[];
615 extern struct cmd_set srvsvc_commands
[];
616 extern struct cmd_set dfs_commands
[];
617 extern struct cmd_set ds_commands
[];
618 extern struct cmd_set echo_commands
[];
619 extern struct cmd_set epmapper_commands
[];
620 extern struct cmd_set shutdown_commands
[];
621 extern struct cmd_set test_commands
[];
622 extern struct cmd_set wkssvc_commands
[];
623 extern struct cmd_set ntsvcs_commands
[];
624 extern struct cmd_set drsuapi_commands
[];
625 extern struct cmd_set eventlog_commands
[];
626 extern struct cmd_set winreg_commands
[];
627 extern struct cmd_set fss_commands
[];
628 extern struct cmd_set witness_commands
[];
630 static struct cmd_set
*rpcclient_command_list
[] = {
653 static void add_command_set(struct cmd_set
*cmd_set
)
655 struct cmd_list
*entry
;
657 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
658 DEBUG(0, ("out of memory\n"));
664 entry
->cmd_set
= cmd_set
;
665 DLIST_ADD(cmd_list
, entry
);
670 * Call an rpcclient function, passing an argv array.
672 * @param cmd Command to run, as a single string.
674 static NTSTATUS
do_cmd(struct cli_state
*cli
,
675 struct user_auth_info
*auth_info
,
676 struct cmd_set
*cmd_entry
,
677 struct dcerpc_binding
*binding
,
678 int argc
, const char **argv
)
688 if (!(mem_ctx
= talloc_stackframe())) {
689 DEBUG(0, ("talloc_init() failed\n"));
690 return NT_STATUS_NO_MEMORY
;
695 if ((cmd_entry
->table
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
696 switch (pipe_default_auth_type
) {
697 case DCERPC_AUTH_TYPE_NONE
:
698 ntresult
= cli_rpc_pipe_open_noauth_transport(
699 cli
, default_transport
,
701 &cmd_entry
->rpc_pipe
);
703 case DCERPC_AUTH_TYPE_SPNEGO
:
705 /* won't happen, but if it does it will fail in cli_rpc_pipe_open_spnego() eventually */
706 const char *oid
= "INVALID";
707 switch (pipe_default_auth_spnego_type
) {
708 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
709 oid
= GENSEC_OID_NTLMSSP
;
711 case PIPE_AUTH_TYPE_SPNEGO_KRB5
:
712 oid
= GENSEC_OID_KERBEROS5
;
714 case PIPE_AUTH_TYPE_SPNEGO_NONE
:
717 ntresult
= cli_rpc_pipe_open_spnego(
718 cli
, cmd_entry
->table
,
721 pipe_default_auth_level
,
722 smbXcli_conn_remote_name(cli
->conn
),
723 get_cmdline_auth_info_domain(auth_info
),
724 get_cmdline_auth_info_username(auth_info
),
725 get_cmdline_auth_info_password(auth_info
),
726 &cmd_entry
->rpc_pipe
);
729 case DCERPC_AUTH_TYPE_NTLMSSP
:
730 case DCERPC_AUTH_TYPE_KRB5
:
731 ntresult
= cli_rpc_pipe_open_generic_auth(
732 cli
, cmd_entry
->table
,
734 pipe_default_auth_type
,
735 pipe_default_auth_level
,
736 smbXcli_conn_remote_name(cli
->conn
),
737 get_cmdline_auth_info_domain(auth_info
),
738 get_cmdline_auth_info_username(auth_info
),
739 get_cmdline_auth_info_password(auth_info
),
740 &cmd_entry
->rpc_pipe
);
742 case DCERPC_AUTH_TYPE_SCHANNEL
:
743 TALLOC_FREE(rpcclient_netlogon_creds
);
744 ntresult
= cli_rpc_pipe_open_schannel(
745 cli
, rpcclient_msg_ctx
,
748 pipe_default_auth_level
,
749 get_cmdline_auth_info_domain(auth_info
),
750 &cmd_entry
->rpc_pipe
,
751 talloc_autofree_context(),
752 &rpcclient_netlogon_creds
);
755 DEBUG(0, ("Could not initialise %s. Invalid "
757 cmd_entry
->table
->name
,
758 pipe_default_auth_type
));
759 talloc_free(mem_ctx
);
760 return NT_STATUS_UNSUCCESSFUL
;
762 if (!NT_STATUS_IS_OK(ntresult
)) {
763 DEBUG(0, ("Could not initialise %s. Error was %s\n",
764 cmd_entry
->table
->name
,
765 nt_errstr(ntresult
) ));
766 talloc_free(mem_ctx
);
770 ok
= ndr_syntax_id_equal(&cmd_entry
->table
->syntax_id
,
771 &ndr_table_netlogon
.syntax_id
);
772 if (rpcclient_netlogon_creds
== NULL
&& ok
) {
773 const char *dc_name
= cmd_entry
->rpc_pipe
->desthost
;
774 const char *domain
= get_cmdline_auth_info_domain(auth_info
);
775 enum netr_SchannelType sec_chan_type
= 0;
776 const char *_account_name
= NULL
;
777 const char *account_name
= NULL
;
778 struct samr_Password current_nt_hash
;
779 struct samr_Password
*previous_nt_hash
= NULL
;
781 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
782 current_nt_hash
.hash
, &_account_name
,
785 DEBUG(0, ("Failed to fetch trust password for %s to connect to %s.\n",
786 get_cmdline_auth_info_domain(auth_info
),
787 cmd_entry
->table
->name
));
788 TALLOC_FREE(cmd_entry
->rpc_pipe
);
789 talloc_free(mem_ctx
);
790 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
793 account_name
= talloc_asprintf(mem_ctx
, "%s$", _account_name
);
794 if (account_name
== NULL
) {
795 DEBUG(0, ("Out of memory creating account name to connect to %s.\n",
796 cmd_entry
->table
->name
));
797 TALLOC_FREE(cmd_entry
->rpc_pipe
);
798 SAFE_FREE(previous_nt_hash
);
799 TALLOC_FREE(mem_ctx
);
800 return NT_STATUS_NO_MEMORY
;
803 ntresult
= rpccli_create_netlogon_creds(dc_name
,
808 talloc_autofree_context(),
809 &rpcclient_netlogon_creds
);
810 if (!NT_STATUS_IS_OK(ntresult
)) {
811 DEBUG(0, ("Could not initialise credentials for %s.\n",
812 cmd_entry
->table
->name
));
813 TALLOC_FREE(cmd_entry
->rpc_pipe
);
814 SAFE_FREE(previous_nt_hash
);
815 TALLOC_FREE(mem_ctx
);
819 ntresult
= rpccli_setup_netlogon_creds(cli
,
820 rpcclient_netlogon_creds
,
821 false, /* force_reauth */
824 SAFE_FREE(previous_nt_hash
);
825 if (!NT_STATUS_IS_OK(ntresult
)) {
826 DEBUG(0, ("Could not initialise credentials for %s.\n",
827 cmd_entry
->table
->name
));
828 TALLOC_FREE(cmd_entry
->rpc_pipe
);
829 TALLOC_FREE(rpcclient_netlogon_creds
);
830 TALLOC_FREE(mem_ctx
);
838 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
839 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, argv
);
840 if (!NT_STATUS_IS_OK(ntresult
)) {
841 printf("result was %s\n", nt_errstr(ntresult
));
844 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, argv
);
845 /* print out the DOS error */
846 if (!W_ERROR_IS_OK(wresult
)) {
847 printf( "result was %s\n", win_errstr(wresult
));
849 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
854 talloc_free(mem_ctx
);
861 * Process a command entered at the prompt or as part of -c
863 * @returns The NTSTATUS from running the command.
865 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
866 struct cli_state
*cli
,
867 struct dcerpc_binding
*binding
,
870 struct cmd_list
*temp_list
;
871 NTSTATUS result
= NT_STATUS_OK
;
874 const char **argv
= NULL
;
876 if ((ret
= poptParseArgvString(cmd
, &argc
, &argv
)) != 0) {
877 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
878 return NT_STATUS_UNSUCCESSFUL
;
882 /* Walk through a dlist of arrays of commands. */
883 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
884 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
886 while (temp_set
->name
) {
887 if (strequal(argv
[0], temp_set
->name
)) {
888 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
889 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
890 fprintf (stderr
, "Invalid command\n");
894 result
= do_cmd(cli
, auth_info
, temp_set
,
895 binding
, argc
, argv
);
904 printf("command not found: %s\n", argv
[0]);
909 if (!NT_STATUS_IS_OK(result)) {
910 printf("result was %s\n", nt_errstr(result));
914 /* NOTE: popt allocates the whole argv, including the
915 * strings, as a single block. So a single free is
916 * enough to release it -- we don't free the
917 * individual strings. rtfm. */
926 int main(int argc
, char *argv
[])
928 const char **const_argv
= discard_const_p(const char *, argv
);
930 static char *cmdstr
= NULL
;
932 struct cli_state
*cli
= NULL
;
933 static char *opt_ipaddr
=NULL
;
934 struct cmd_set
**cmd_set
;
935 struct sockaddr_storage server_ss
;
937 static int opt_port
= 0;
939 TALLOC_CTX
*frame
= talloc_stackframe();
941 struct dcerpc_binding
*binding
= NULL
;
942 enum dcerpc_transport_t transport
;
944 const char *binding_string
= NULL
;
945 char *user
, *domain
, *q
;
948 /* make sure the vars that get altered (4th field) are in
949 a fixed location or certain compilers complain */
951 struct poptOption long_options
[] = {
953 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
954 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
955 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
957 POPT_COMMON_CONNECTION
958 POPT_COMMON_CREDENTIALS
964 zero_sockaddr(&server_ss
);
968 /* the following functions are part of the Samba debugging
969 facilities. See lib/debug.c */
970 setup_logging("rpcclient", DEBUG_STDOUT
);
972 rpcclient_auth_info
= user_auth_info_init(frame
);
973 if (rpcclient_auth_info
== NULL
) {
976 popt_common_set_auth_info(rpcclient_auth_info
);
980 pc
= poptGetContext("rpcclient", argc
, const_argv
,
984 poptPrintHelp(pc
, stderr
, 0);
988 while((opt
= poptGetNextOpt(pc
)) != -1) {
992 if (!interpret_string_addr(&server_ss
,
995 fprintf(stderr
, "%s not a valid IP address\n",
1003 /* Get server as remaining unparsed argument. Print usage if more
1004 than one unparsed argument is present. */
1006 server
= poptGetArg(pc
);
1008 if (!server
|| poptGetArg(pc
)) {
1009 poptPrintHelp(pc
, stderr
, 0);
1014 poptFreeContext(pc
);
1015 popt_burn_cmdline_password(argc
, argv
);
1017 if (!init_names()) {
1022 /* Load smb.conf file */
1024 if (!lp_load_global(get_dyn_CONFIGFILE()))
1025 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
1027 /* We must load interfaces after we load the smb.conf */
1030 rpcclient_msg_ctx
= messaging_init(talloc_autofree_context(),
1031 samba_tevent_context_init(talloc_autofree_context()));
1035 * from stdin if necessary
1038 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
1039 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
1044 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
1046 if ((server
[0] == '/' && server
[1] == '/') ||
1047 (server
[0] == '\\' && server
[1] == '\\')) {
1051 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1053 if (!NT_STATUS_IS_OK(nt_status
)) {
1055 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1056 strip_hostname(server
));
1057 if (!binding_string
) {
1062 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1063 if (!NT_STATUS_IS_OK(nt_status
)) {
1069 transport
= dcerpc_binding_get_transport(binding
);
1071 if (transport
== NCA_UNKNOWN
) {
1072 nt_status
= dcerpc_binding_set_transport(binding
, NCACN_NP
);
1073 if (!NT_STATUS_IS_OK(nt_status
)) {
1079 host
= dcerpc_binding_get_string_option(binding
, "host");
1081 bflags
= dcerpc_binding_get_flags(binding
);
1082 if (bflags
& DCERPC_CONNECT
) {
1083 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1084 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1086 if (bflags
& DCERPC_SIGN
) {
1087 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
1088 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1090 if (bflags
& DCERPC_SEAL
) {
1091 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
1092 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1094 if (bflags
& DCERPC_AUTH_SPNEGO
) {
1095 pipe_default_auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
1096 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1098 if (bflags
& DCERPC_AUTH_NTLM
) {
1099 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1100 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
1102 pipe_default_auth_type
= DCERPC_AUTH_TYPE_NTLMSSP
;
1105 if (bflags
& DCERPC_AUTH_KRB5
) {
1106 if (pipe_default_auth_type
== DCERPC_AUTH_TYPE_SPNEGO
) {
1107 pipe_default_auth_spnego_type
= PIPE_AUTH_TYPE_SPNEGO_KRB5
;
1109 pipe_default_auth_type
= DCERPC_AUTH_TYPE_KRB5
;
1112 if (pipe_default_auth_type
!= DCERPC_AUTH_TYPE_NONE
) {
1113 /* If neither Integrity or Privacy are requested then
1114 * Use just Connect level */
1115 if (pipe_default_auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
1116 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
1120 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
1121 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
1122 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
1124 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info
)) {
1125 flags
|= CLI_FULL_CONNECTION_USE_CCACHE
;
1128 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
1129 SMB_ASSERT(user
!= NULL
);
1130 domain
= talloc_strdup(frame
, lp_workgroup());
1131 SMB_ASSERT(domain
!= NULL
);
1132 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
1134 if ((q
= strchr_m(user
,'\\'))) {
1136 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
1137 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
1140 nt_status
= cli_full_connection(&cli
, lp_netbios_name(), host
,
1141 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
1143 get_cmdline_auth_info_username(rpcclient_auth_info
),
1144 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1145 get_cmdline_auth_info_password(rpcclient_auth_info
),
1147 get_cmdline_auth_info_signing_state(rpcclient_auth_info
));
1149 if (!NT_STATUS_IS_OK(nt_status
)) {
1150 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1155 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1156 nt_status
= cli_cm_force_encryption(cli
,
1157 get_cmdline_auth_info_username(rpcclient_auth_info
),
1158 get_cmdline_auth_info_password(rpcclient_auth_info
),
1159 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1161 if (!NT_STATUS_IS_OK(nt_status
)) {
1167 #if 0 /* COMMENT OUT FOR TESTING */
1168 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1171 /* Load command lists */
1172 rpcclient_cli_state
= cli
;
1173 timeout
= cli_set_timeout(cli
, 10000);
1175 cmd_set
= rpcclient_command_list
;
1178 add_command_set(*cmd_set
);
1179 add_command_set(separator_command
);
1183 default_transport
= dcerpc_binding_get_transport(binding
);
1185 fetch_machine_sid(cli
);
1187 /* Do anything specified with -c */
1188 if (cmdstr
&& cmdstr
[0]) {
1194 while((cmd
=next_command(&p
)) != NULL
) {
1195 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1198 result
= NT_STATUS_IS_ERR(cmd_result
);
1204 /* Loop around accepting commands */
1209 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1214 if (line
[0] != '\n')
1215 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);
1220 rpcclient_cli_state
= NULL
;