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 "../libcli/auth/netlogon_creds_cli.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"
37 #include "cmdline_contexts.h"
38 #include "../librpc/gen_ndr/ndr_samr.h"
39 #include "lib/cmdline/cmdline.h"
41 enum pipe_auth_type_spnego
{
42 PIPE_AUTH_TYPE_SPNEGO_NONE
= 0,
43 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
,
44 PIPE_AUTH_TYPE_SPNEGO_KRB5
47 static unsigned int timeout
= 10000;
49 struct messaging_context
*rpcclient_msg_ctx
;
50 struct netlogon_creds_cli_context
*rpcclient_netlogon_creds
;
51 static const char *rpcclient_netlogon_domain
;
53 /* List to hold groups of commands.
55 * Commands are defined in a list of arrays: arrays are easy to
56 * statically declare, and lists are easier to dynamically extend.
59 static struct cmd_list
{
60 struct cmd_list
*prev
, *next
;
61 struct cmd_set
*cmd_set
;
64 /****************************************************************************
65 handle completion of commands for readline
66 ****************************************************************************/
67 static char **completion_fn(const char *text
, int start
, int end
)
69 #define MAX_COMPLETIONS 1000
72 struct cmd_list
*commands
= cmd_list
;
75 /* FIXME!!! -- what to do when completing argument? */
76 /* for words not at the start of the line fallback
77 to filename completion */
82 /* make sure we have a list of valid commands */
87 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
92 matches
[count
++] = SMB_STRDUP(text
);
98 while (commands
&& count
< MAX_COMPLETIONS
-1) {
99 if (!commands
->cmd_set
) {
103 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
104 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
105 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
106 commands
->cmd_set
[i
].ntfn
) ||
107 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
108 commands
->cmd_set
[i
].wfn
))) {
109 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
110 if (!matches
[count
]) {
111 for (i
= 0; i
< count
; i
++) {
112 SAFE_FREE(matches
[count
]);
120 commands
= commands
->next
;
124 SAFE_FREE(matches
[0]);
125 matches
[0] = SMB_STRDUP(matches
[1]);
127 matches
[count
] = NULL
;
131 static char *next_command (char **cmdstr
)
136 if (!cmdstr
|| !(*cmdstr
))
139 p
= strchr_m(*cmdstr
, ';');
142 command
= SMB_STRDUP(*cmdstr
);
151 static void binding_get_auth_info(
152 const struct dcerpc_binding
*b
,
153 enum dcerpc_AuthType
*_auth_type
,
154 enum dcerpc_AuthLevel
*_auth_level
,
155 enum credentials_use_kerberos
*_krb5_state
)
157 uint32_t bflags
= dcerpc_binding_get_flags(b
);
158 enum dcerpc_AuthLevel auth_level
= DCERPC_AUTH_LEVEL_NONE
;
159 enum dcerpc_AuthType auth_type
= DCERPC_AUTH_TYPE_NONE
;
160 enum credentials_use_kerberos krb5_state
= CRED_USE_KERBEROS_DESIRED
;
162 if (_krb5_state
!= NULL
) {
163 krb5_state
= *_krb5_state
;
166 if (bflags
& DCERPC_CONNECT
) {
167 auth_level
= DCERPC_AUTH_LEVEL_CONNECT
;
169 if (bflags
& DCERPC_PACKET
) {
170 auth_level
= DCERPC_AUTH_LEVEL_PACKET
;
172 if (bflags
& DCERPC_SIGN
) {
173 auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
175 if (bflags
& DCERPC_SEAL
) {
176 auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
179 if (bflags
& DCERPC_SCHANNEL
) {
180 auth_type
= DCERPC_AUTH_TYPE_SCHANNEL
;
183 if ((auth_level
!= DCERPC_AUTH_LEVEL_NONE
) &&
184 (auth_type
== DCERPC_AUTH_TYPE_NONE
)) {
185 auth_type
= (krb5_state
== CRED_USE_KERBEROS_REQUIRED
) ?
186 DCERPC_AUTH_TYPE_KRB5
: DCERPC_AUTH_TYPE_NTLMSSP
;
189 if (bflags
& DCERPC_AUTH_SPNEGO
) {
190 auth_type
= DCERPC_AUTH_TYPE_SPNEGO
;
192 if (bflags
& DCERPC_AUTH_NTLM
) {
193 krb5_state
= CRED_USE_KERBEROS_DISABLED
;
195 if (bflags
& DCERPC_AUTH_KRB5
) {
196 krb5_state
= CRED_USE_KERBEROS_REQUIRED
;
200 if (auth_type
!= DCERPC_AUTH_TYPE_NONE
) {
201 /* If nothing is requested then default to integrity */
202 if (auth_level
== DCERPC_AUTH_LEVEL_NONE
) {
203 auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
207 if (_auth_type
!= NULL
) {
208 *_auth_type
= auth_type
;
210 if (_auth_level
!= NULL
) {
211 *_auth_level
= auth_level
;
213 if (_krb5_state
!= NULL
) {
214 *_krb5_state
= krb5_state
;
218 /* List the available commands on a given pipe */
220 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
221 int argc
, const char **argv
)
223 struct cmd_list
*tmp
;
224 struct cmd_set
*tmp_set
;
230 printf("Usage: %s <pipe>\n", argv
[0]);
234 /* Help on one command */
236 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
238 tmp_set
= tmp
->cmd_set
;
240 if (!strcasecmp_m(argv
[1], tmp_set
->name
))
242 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
246 while(tmp_set
->name
) {
247 printf("%30s", tmp_set
->name
);
254 /* drop out of the loop */
263 /* Display help on commands */
265 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
266 int argc
, const char **argv
)
268 struct cmd_list
*tmp
;
269 struct cmd_set
*tmp_set
;
274 printf("Usage: %s [command]\n", argv
[0]);
278 /* Help on one command */
281 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
283 tmp_set
= tmp
->cmd_set
;
285 while(tmp_set
->name
) {
286 if (strequal(argv
[1], tmp_set
->name
)) {
287 if (tmp_set
->usage
&&
289 printf("%s\n", tmp_set
->usage
);
291 printf("No help for %s\n", tmp_set
->name
);
300 printf("No such command: %s\n", argv
[1]);
304 /* List all commands */
306 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
308 tmp_set
= tmp
->cmd_set
;
310 while(tmp_set
->name
) {
312 printf("%15s\t\t%s\n", tmp_set
->name
,
313 tmp_set
->description
? tmp_set
->description
:
323 /* Change the debug level */
325 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
326 int argc
, const char **argv
)
329 printf("Usage: %s [debuglevel]\n", argv
[0]);
334 lp_set_cmdline("log level", argv
[1]);
337 printf("debuglevel is %d\n", DEBUGLEVEL
);
342 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
343 int argc
, const char **argv
)
346 return NT_STATUS_OK
; /* NOTREACHED */
349 static NTSTATUS
cmd_set_ss_level(struct dcerpc_binding
*binding
)
351 struct cmd_list
*tmp
;
352 enum dcerpc_AuthType auth_type
;
353 enum dcerpc_AuthLevel auth_level
;
355 /* Close any existing connections not at this level. */
357 binding_get_auth_info(binding
, &auth_type
, &auth_level
, NULL
);
359 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
360 struct cmd_set
*tmp_set
;
362 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
363 if (tmp_set
->rpc_pipe
== NULL
) {
367 if ((tmp_set
->rpc_pipe
->auth
->auth_type
369 || (tmp_set
->rpc_pipe
->auth
->auth_level
371 TALLOC_FREE(tmp_set
->rpc_pipe
);
372 tmp_set
->rpc_pipe
= NULL
;
379 static NTSTATUS
cmd_set_transport(struct dcerpc_binding
*b
)
381 enum dcerpc_transport_t t
= dcerpc_binding_get_transport(b
);
382 struct cmd_list
*tmp
;
384 /* Close any existing connections not at this level. */
386 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
387 struct cmd_set
*tmp_set
;
389 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
390 if (tmp_set
->rpc_pipe
== NULL
) {
394 if (tmp_set
->rpc_pipe
->transport
->transport
!= t
) {
395 TALLOC_FREE(tmp_set
->rpc_pipe
);
396 tmp_set
->rpc_pipe
= NULL
;
403 static NTSTATUS
binding_reset_auth(struct dcerpc_binding
*b
)
405 NTSTATUS status
= dcerpc_binding_set_flags(
419 static NTSTATUS
binding_set_auth(
420 struct dcerpc_binding
*b
, const char *level
, const char *type
)
424 status
= binding_reset_auth(b
);
425 if (!NT_STATUS_IS_OK(status
)) {
430 status
= dcerpc_binding_set_string_option(b
, level
, level
);
431 if (!NT_STATUS_IS_OK(status
)) {
436 if (strequal(type
, "SPNEGO")) {
437 status
= dcerpc_binding_set_string_option(
438 b
, "spnego", "spnego");
441 if (strequal(type
, "NTLMSSP")) {
442 status
= dcerpc_binding_set_string_option(b
, "ntlm", "ntlm");
445 if (strequal(type
, "NTLMSSP_SPNEGO")) {
446 status
= dcerpc_binding_set_string_option(
447 b
, "spnego", "spnego");
448 if (!NT_STATUS_IS_OK(status
)) {
451 status
= dcerpc_binding_set_string_option(b
, "ntlm", "ntlm");
454 if (strequal(type
, "KRB5")) {
455 status
= dcerpc_binding_set_string_option(b
, "krb5", "krb5");
458 if (strequal(type
, "KRB5_SPNEGO")) {
459 status
= dcerpc_binding_set_string_option(
460 b
, "spnego", "spnego");
461 if (!NT_STATUS_IS_OK(status
)) {
464 status
= dcerpc_binding_set_string_option(b
, "krb5", "krb5");
467 if (strequal(type
, "SCHANNEL")) {
468 status
= dcerpc_binding_set_string_option(
469 b
, "schannel", "schannel");
473 return NT_STATUS_INVALID_PARAMETER
;
476 static NTSTATUS
cmd_set_auth(
477 struct dcerpc_binding
*binding
,
483 const char *p
= "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
484 const char *type
= "NTLMSSP";
488 printf("Usage: %s %s\n", argv
[0], p
);
496 status
= binding_set_auth(binding
, level
, type
);
497 if (!NT_STATUS_IS_OK(status
)) {
498 printf("Usage: %s %s\n", argv
[0], p
);
502 d_printf("Setting %s - %s: %s\n", type
, display
, nt_errstr(status
));
504 status
= cmd_set_ss_level(binding
);
508 static NTSTATUS
cmd_sign(
509 struct dcerpc_binding
*binding
,
514 NTSTATUS status
= cmd_set_auth(binding
, "sign", "sign", argc
, argv
);
518 static NTSTATUS
cmd_seal(
519 struct dcerpc_binding
*binding
,
524 NTSTATUS status
= cmd_set_auth(
525 binding
, "seal", "sign and seal", argc
, argv
);
529 static NTSTATUS
cmd_packet(
530 struct dcerpc_binding
*binding
,
535 NTSTATUS status
= cmd_set_auth(
536 binding
, "packet", "packet", argc
, argv
);
540 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
541 int argc
, const char **argv
)
544 printf("Usage: %s timeout\n", argv
[0]);
549 timeout
= atoi(argv
[1]);
552 printf("timeout is %d\n", timeout
);
558 static NTSTATUS
cmd_none(
559 struct dcerpc_binding
*binding
,
564 NTSTATUS status
= binding_reset_auth(binding
);
565 if (!NT_STATUS_IS_OK(status
)) {
568 status
= cmd_set_ss_level(binding
);
572 static NTSTATUS
cmd_schannel(
573 struct dcerpc_binding
*binding
,
578 const char *argv
[] = { "schannel", "SCHANNEL" };
579 NTSTATUS status
= cmd_set_auth(
580 binding
, "seal", "sign and seal", 2, argv
);
584 static NTSTATUS
cmd_schannel_sign(
585 struct dcerpc_binding
*binding
,
590 const char *argv
[] = { "schannel_sign", "SCHANNEL" };
591 NTSTATUS status
= cmd_set_auth(binding
, "sign", "sign only", 2, argv
);
595 static NTSTATUS
cmd_choose_transport(
596 struct dcerpc_binding
*binding
,
602 enum dcerpc_transport_t transport
;
605 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
609 transport
= dcerpc_transport_by_name(argv
[1]);
610 if (transport
== NCA_UNKNOWN
) {
611 printf("transport type %s unknown\n", argv
[1]);
612 return NT_STATUS_NOT_SUPPORTED
;
614 if (!((transport
== NCACN_IP_TCP
) ||
615 (transport
== NCACN_NP
) ||
616 (transport
== NCALRPC
))) {
617 printf("transport %s not supported\n", argv
[1]);
618 return NT_STATUS_NOT_SUPPORTED
;
621 status
= dcerpc_binding_set_transport(binding
, transport
);
622 if (!NT_STATUS_IS_OK(status
)) {
626 status
= cmd_set_transport(binding
);
627 if (!NT_STATUS_IS_OK(status
)) {
631 printf("default transport is now: %s\n", argv
[1]);
636 /* Built in rpcclient commands */
638 static struct cmd_set rpcclient_commands
[] = {
641 .name
= "GENERAL OPTIONS",
646 .returntype
= RPC_RTYPE_NTSTATUS
,
648 .description
= "Get help on commands",
649 .usage
= "[command]",
653 .returntype
= RPC_RTYPE_NTSTATUS
,
655 .description
= "Get help on commands",
656 .usage
= "[command]",
659 .name
= "debuglevel",
660 .returntype
= RPC_RTYPE_NTSTATUS
,
661 .ntfn
= cmd_debuglevel
,
662 .description
= "Set debug level",
667 .returntype
= RPC_RTYPE_NTSTATUS
,
668 .ntfn
= cmd_debuglevel
,
669 .description
= "Set debug level",
674 .returntype
= RPC_RTYPE_NTSTATUS
,
675 .ntfn
= cmd_listcommands
,
676 .description
= "List available commands on <pipe>",
681 .returntype
= RPC_RTYPE_NTSTATUS
,
683 .description
= "Exit program",
688 .returntype
= RPC_RTYPE_NTSTATUS
,
690 .description
= "Exit program",
695 .returntype
= RPC_RTYPE_BINDING
,
697 .description
= "Force RPC pipe connections to be signed",
702 .returntype
= RPC_RTYPE_BINDING
,
704 .description
= "Force RPC pipe connections to be sealed",
709 .returntype
= RPC_RTYPE_BINDING
,
711 .description
= "Force RPC pipe connections with packet authentication level",
716 .returntype
= RPC_RTYPE_BINDING
,
718 .description
= "Force RPC pipe connections to be sealed with 'schannel'. "
719 "Assumes valid machine account to this domain controller.",
723 .name
= "schannelsign",
724 .returntype
= RPC_RTYPE_BINDING
,
725 .bfn
= cmd_schannel_sign
,
726 .description
= "Force RPC pipe connections to be signed (not sealed) with "
727 "'schannel'. Assumes valid machine account to this domain "
733 .returntype
= RPC_RTYPE_NTSTATUS
,
735 .description
= "Set timeout (in milliseconds) for RPC operations",
740 .returntype
= RPC_RTYPE_BINDING
,
741 .bfn
= cmd_choose_transport
,
742 .description
= "Choose ncacn transport for RPC operations",
747 .returntype
= RPC_RTYPE_BINDING
,
749 .description
= "Force RPC pipe connections to have no special properties",
756 static struct cmd_set separator_command
[] = {
758 .name
= "---------------",
759 .returntype
= MAX_RPC_RETURN_TYPE
,
760 .description
= "----------------------"
766 /* Various pipe commands */
768 extern struct cmd_set lsarpc_commands
[];
769 extern struct cmd_set samr_commands
[];
770 extern struct cmd_set spoolss_commands
[];
771 extern struct cmd_set iremotewinspool_commands
[];
772 extern struct cmd_set netlogon_commands
[];
773 extern struct cmd_set srvsvc_commands
[];
774 extern struct cmd_set dfs_commands
[];
775 extern struct cmd_set ds_commands
[];
776 extern struct cmd_set echo_commands
[];
777 extern struct cmd_set epmapper_commands
[];
778 extern struct cmd_set shutdown_commands
[];
779 extern struct cmd_set wkssvc_commands
[];
780 extern struct cmd_set ntsvcs_commands
[];
781 extern struct cmd_set drsuapi_commands
[];
782 extern struct cmd_set eventlog_commands
[];
783 extern struct cmd_set winreg_commands
[];
784 extern struct cmd_set fss_commands
[];
785 extern struct cmd_set witness_commands
[];
786 extern struct cmd_set clusapi_commands
[];
787 extern struct cmd_set spotlight_commands
[];
788 extern struct cmd_set unixinfo_commands
[];
790 static struct cmd_set
*rpcclient_command_list
[] = {
796 iremotewinspool_commands
,
816 static void add_command_set(struct cmd_set
*cmd_set
)
818 struct cmd_list
*entry
;
820 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
821 DEBUG(0, ("out of memory\n"));
827 entry
->cmd_set
= cmd_set
;
828 DLIST_ADD(cmd_list
, entry
);
831 static NTSTATUS
rpccli_ncalrpc_connect(
832 const struct ndr_interface_table
*iface
,
834 struct rpc_pipe_client
**prpccli
)
836 struct rpc_pipe_client
*rpccli
= NULL
;
837 struct pipe_auth_data
*auth
= NULL
;
840 status
= rpc_pipe_open_ncalrpc(mem_ctx
, iface
, &rpccli
);
841 if (!NT_STATUS_IS_OK(status
)) {
842 DBG_DEBUG("rpc_pipe_open_ncalrpc failed: %s\n",
847 status
= rpccli_ncalrpc_bind_data(rpccli
, &auth
);
848 if (!NT_STATUS_IS_OK(status
)) {
849 DBG_DEBUG("rpccli_ncalrpc_bind_data failed: %s\n",
854 status
= rpc_pipe_bind(rpccli
, auth
);
855 if (!NT_STATUS_IS_OK(status
)) {
856 DBG_DEBUG("rpc_pipe_bind failed: %s\n", nt_errstr(status
));
867 * Call an rpcclient function, passing an argv array.
869 * @param cmd Command to run, as a single string.
871 static NTSTATUS
do_cmd(struct cli_state
*cli
,
872 struct cli_credentials
*creds
,
873 struct cmd_set
*cmd_entry
,
874 struct dcerpc_binding
*binding
,
875 int argc
, const char **argv
)
879 enum dcerpc_transport_t transport
;
881 TALLOC_CTX
*mem_ctx
= talloc_stackframe();
883 transport
= dcerpc_binding_get_transport(binding
);
887 if ((cmd_entry
->table
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
888 if (transport
== NCALRPC
) {
889 ntresult
= rpccli_ncalrpc_connect(
890 cmd_entry
->table
, cli
, &cmd_entry
->rpc_pipe
);
891 if (!NT_STATUS_IS_OK(ntresult
)) {
892 TALLOC_FREE(mem_ctx
);
896 enum dcerpc_AuthType auth_type
;
897 enum dcerpc_AuthLevel auth_level
;
898 enum credentials_use_kerberos krb5_state
=
899 cli_credentials_get_kerberos_state(creds
);
901 binding_get_auth_info(
902 binding
, &auth_type
, &auth_level
, &krb5_state
);
905 case DCERPC_AUTH_TYPE_NONE
:
906 ntresult
= cli_rpc_pipe_open_noauth_transport(
909 &cmd_entry
->rpc_pipe
);
911 case DCERPC_AUTH_TYPE_SPNEGO
:
912 case DCERPC_AUTH_TYPE_NTLMSSP
:
913 case DCERPC_AUTH_TYPE_KRB5
:
914 cli_credentials_set_kerberos_state(creds
,
918 ntresult
= cli_rpc_pipe_open_with_creds(
919 cli
, cmd_entry
->table
,
923 smbXcli_conn_remote_name(cli
->conn
),
925 &cmd_entry
->rpc_pipe
);
927 case DCERPC_AUTH_TYPE_SCHANNEL
:
928 TALLOC_FREE(rpcclient_netlogon_creds
);
929 ntresult
= cli_rpc_pipe_open_schannel(
930 cli
, rpcclient_msg_ctx
,
933 rpcclient_netlogon_domain
,
934 &cmd_entry
->rpc_pipe
,
936 &rpcclient_netlogon_creds
);
939 DEBUG(0, ("Could not initialise %s. Invalid "
941 cmd_entry
->table
->name
,
943 talloc_free(mem_ctx
);
944 return NT_STATUS_UNSUCCESSFUL
;
946 if (!NT_STATUS_IS_OK(ntresult
)) {
947 DBG_ERR("Could not initialise %s. "
949 cmd_entry
->table
->name
,
950 nt_errstr(ntresult
));
951 talloc_free(mem_ctx
);
955 if (rpcclient_netlogon_creds
== NULL
&&
956 cmd_entry
->use_netlogon_creds
) {
957 const char *dc_name
=
958 cmd_entry
->rpc_pipe
->desthost
;
959 const char *domain
= rpcclient_netlogon_domain
;
960 struct cli_credentials
*trust_creds
= NULL
;
962 ntresult
= pdb_get_trust_credentials(
967 if (!NT_STATUS_IS_OK(ntresult
)) {
968 DBG_ERR("Failed to fetch trust "
970 "%s to connect to %s: %s\n",
972 cmd_entry
->table
->name
,
973 nt_errstr(ntresult
));
974 TALLOC_FREE(cmd_entry
->rpc_pipe
);
975 talloc_free(mem_ctx
);
979 ntresult
= rpccli_create_netlogon_creds_ctx(
984 &rpcclient_netlogon_creds
);
985 if (!NT_STATUS_IS_OK(ntresult
)) {
986 DBG_ERR("Could not initialise "
987 "credentials for %s.\n",
988 cmd_entry
->table
->name
);
989 TALLOC_FREE(cmd_entry
->rpc_pipe
);
990 TALLOC_FREE(mem_ctx
);
994 ntresult
= rpccli_setup_netlogon_creds(
997 rpcclient_netlogon_creds
,
998 false, /* force_reauth */
1000 TALLOC_FREE(trust_creds
);
1001 if (!NT_STATUS_IS_OK(ntresult
)) {
1002 DBG_ERR("Could not initialise "
1003 "credentials for %s.\n",
1004 cmd_entry
->table
->name
);
1005 TALLOC_FREE(cmd_entry
->rpc_pipe
);
1006 TALLOC_FREE(rpcclient_netlogon_creds
);
1007 TALLOC_FREE(mem_ctx
);
1014 /* Set timeout for new connections */
1015 if (cmd_entry
->rpc_pipe
) {
1016 rpccli_set_timeout(cmd_entry
->rpc_pipe
, timeout
);
1021 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
1022 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, argv
);
1023 if (!NT_STATUS_IS_OK(ntresult
)) {
1024 printf("result was %s\n", nt_errstr(ntresult
));
1026 } else if (cmd_entry
->returntype
== RPC_RTYPE_BINDING
) {
1027 ntresult
= cmd_entry
->bfn(binding
, mem_ctx
, argc
, argv
);
1028 if (!NT_STATUS_IS_OK(ntresult
)) {
1029 printf("result was %s\n", nt_errstr(ntresult
));
1032 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, argv
);
1033 /* print out the DOS error */
1034 if (!W_ERROR_IS_OK(wresult
)) {
1035 printf( "result was %s\n", win_errstr(wresult
));
1037 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
1042 talloc_free(mem_ctx
);
1049 * Process a command entered at the prompt or as part of -c
1051 * @returns The NTSTATUS from running the command.
1053 static NTSTATUS
process_cmd(struct cli_credentials
*creds
,
1054 struct cli_state
*cli
,
1055 struct dcerpc_binding
*binding
,
1058 struct cmd_list
*temp_list
;
1059 NTSTATUS result
= NT_STATUS_OK
;
1062 const char **argv
= NULL
;
1064 if ((ret
= poptParseArgvString(cmd
, &argc
, &argv
)) != 0) {
1065 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
1066 return NT_STATUS_UNSUCCESSFUL
;
1070 /* Walk through a dlist of arrays of commands. */
1071 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
1072 struct cmd_set
*set
= temp_list
->cmd_set
;
1074 while (set
->name
!= NULL
) {
1075 if (!strequal(argv
[0], set
->name
)) {
1080 if (((set
->returntype
== RPC_RTYPE_NTSTATUS
) &&
1081 (set
->ntfn
== NULL
)) ||
1082 ((set
->returntype
== RPC_RTYPE_WERROR
) &&
1083 (set
->wfn
== NULL
)) ||
1084 ((set
->returntype
== RPC_RTYPE_BINDING
) &&
1085 (set
->bfn
== NULL
))) {
1086 fprintf (stderr
, "Invalid command\n");
1091 cli
, creds
, set
, binding
, argc
, argv
);
1097 printf("command not found: %s\n", argv
[0]);
1101 /* moved to do_cmd()
1102 if (!NT_STATUS_IS_OK(result)) {
1103 printf("result was %s\n", nt_errstr(result));
1107 /* NOTE: popt allocates the whole argv, including the
1108 * strings, as a single block. So a single free is
1109 * enough to release it -- we don't free the
1110 * individual strings. rtfm. */
1119 int main(int argc
, char *argv
[])
1121 const char **const_argv
= discard_const_p(const char *, argv
);
1123 static char *cmdstr
= NULL
;
1125 struct cli_state
*cli
= NULL
;
1126 static char *opt_ipaddr
=NULL
;
1127 struct cmd_set
**cmd_set
;
1128 struct sockaddr_storage server_ss
;
1130 static int opt_port
= 0;
1132 TALLOC_CTX
*frame
= talloc_stackframe();
1133 uint32_t flags
= CLI_FULL_CONNECTION_IPC
;
1134 struct dcerpc_binding
*binding
= NULL
;
1135 enum dcerpc_transport_t transport
;
1136 const char *binding_string
= NULL
;
1138 struct cli_credentials
*creds
= NULL
;
1141 /* make sure the vars that get altered (4th field) are in
1142 a fixed location or certain compilers complain */
1144 struct poptOption long_options
[] = {
1146 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
1147 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
1148 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
1150 POPT_COMMON_CONNECTION
1151 POPT_COMMON_CREDENTIALS
1159 zero_sockaddr(&server_ss
);
1163 ok
= samba_cmdline_init(frame
,
1164 SAMBA_CMDLINE_CONFIG_CLIENT
,
1165 false /* require_smbconf */);
1167 DBG_ERR("Failed to init cmdline parser!\n");
1169 lp_set_cmdline("log level", "0");
1172 pc
= samba_popt_get_context(getprogname(),
1178 DBG_ERR("Failed to setup popt context!\n");
1182 poptSetOtherOptionHelp(pc
, "[OPTION...] BINDING-STRING|HOST\nOptions:");
1185 poptPrintHelp(pc
, stderr
, 0);
1189 while((opt
= poptGetNextOpt(pc
)) != -1) {
1193 if (!interpret_string_addr(&server_ss
,
1196 fprintf(stderr
, "%s not a valid IP address\n",
1204 /* Get server as remaining unparsed argument. Print usage if more
1205 than one unparsed argument is present. */
1207 server
= poptGetArg(pc
);
1209 if (!server
|| poptGetArg(pc
)) {
1210 poptPrintHelp(pc
, stderr
, 0);
1215 poptFreeContext(pc
);
1216 samba_cmdline_burn(argc
, argv
);
1218 rpcclient_msg_ctx
= cmdline_messaging_context(get_dyn_CONFIGFILE());
1219 creds
= samba_cmdline_get_creds();
1223 * from stdin if necessary
1226 if ((server
[0] == '/' && server
[1] == '/') ||
1227 (server
[0] == '\\' && server
[1] == '\\')) {
1231 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
1233 if (!NT_STATUS_IS_OK(nt_status
)) {
1235 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
1236 strip_hostname(server
));
1237 if (!binding_string
) {
1242 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
1243 if (!NT_STATUS_IS_OK(nt_status
)) {
1249 transport
= dcerpc_binding_get_transport(binding
);
1251 if (transport
== NCA_UNKNOWN
) {
1252 transport
= NCACN_NP
;
1253 nt_status
= dcerpc_binding_set_transport(binding
, transport
);
1254 if (!NT_STATUS_IS_OK(nt_status
)) {
1260 host
= dcerpc_binding_get_string_option(binding
, "host");
1262 rpcclient_netlogon_domain
= cli_credentials_get_domain(creds
);
1263 if (rpcclient_netlogon_domain
== NULL
||
1264 rpcclient_netlogon_domain
[0] == '\0')
1266 rpcclient_netlogon_domain
= lp_workgroup();
1269 if (transport
== NCACN_NP
) {
1270 nt_status
= cli_full_connection_creds(
1274 opt_ipaddr
? &server_ss
: NULL
,
1281 if (!NT_STATUS_IS_OK(nt_status
)) {
1282 DEBUG(0, ("Cannot connect to server. Error was %s\n",
1283 nt_errstr(nt_status
)));
1288 /* Load command lists */
1289 cli_set_timeout(cli
, timeout
);
1292 #if 0 /* COMMENT OUT FOR TESTING */
1293 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1296 cmd_set
= rpcclient_command_list
;
1299 add_command_set(*cmd_set
);
1300 add_command_set(separator_command
);
1304 /* Do anything specified with -c */
1305 if (cmdstr
&& cmdstr
[0]) {
1311 while((cmd
=next_command(&p
)) != NULL
) {
1312 NTSTATUS cmd_result
= process_cmd(creds
,
1317 result
= NT_STATUS_IS_ERR(cmd_result
);
1323 /* Loop around accepting commands */
1328 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1335 if (line
[0] != '\n')
1347 netlogon_creds_cli_close_global_db();
1348 TALLOC_FREE(rpcclient_msg_ctx
);