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 "rpcclient.h"
24 #include "../libcli/auth/libcli_auth.h"
28 static enum pipe_auth_type pipe_default_auth_type
= PIPE_AUTH_TYPE_NONE
;
29 static enum dcerpc_AuthLevel pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
30 static unsigned int timeout
= 0;
31 static enum dcerpc_transport_t default_transport
= NCACN_NP
;
33 struct user_auth_info
*rpcclient_auth_info
;
35 /* List to hold groups of commands.
37 * Commands are defined in a list of arrays: arrays are easy to
38 * statically declare, and lists are easier to dynamically extend.
41 static struct cmd_list
{
42 struct cmd_list
*prev
, *next
;
43 struct cmd_set
*cmd_set
;
46 /****************************************************************************
47 handle completion of commands for readline
48 ****************************************************************************/
49 static char **completion_fn(const char *text
, int start
, int end
)
51 #define MAX_COMPLETIONS 100
54 struct cmd_list
*commands
= cmd_list
;
57 /* FIXME!!! -- what to do when completing argument? */
58 /* for words not at the start of the line fallback
59 to filename completion */
64 /* make sure we have a list of valid commands */
69 matches
= SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS
);
74 matches
[count
++] = SMB_STRDUP(text
);
80 while (commands
&& count
< MAX_COMPLETIONS
-1) {
81 if (!commands
->cmd_set
) {
85 for (i
=0; commands
->cmd_set
[i
].name
; i
++) {
86 if ((strncmp(text
, commands
->cmd_set
[i
].name
, strlen(text
)) == 0) &&
87 (( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_NTSTATUS
&&
88 commands
->cmd_set
[i
].ntfn
) ||
89 ( commands
->cmd_set
[i
].returntype
== RPC_RTYPE_WERROR
&&
90 commands
->cmd_set
[i
].wfn
))) {
91 matches
[count
] = SMB_STRDUP(commands
->cmd_set
[i
].name
);
92 if (!matches
[count
]) {
93 for (i
= 0; i
< count
; i
++) {
94 SAFE_FREE(matches
[count
]);
102 commands
= commands
->next
;
107 SAFE_FREE(matches
[0]);
108 matches
[0] = SMB_STRDUP(matches
[1]);
110 matches
[count
] = NULL
;
114 static char *next_command (char **cmdstr
)
119 if (!cmdstr
|| !(*cmdstr
))
122 p
= strchr_m(*cmdstr
, ';');
125 command
= SMB_STRDUP(*cmdstr
);
134 /* Fetch the SID for this computer */
136 static void fetch_machine_sid(struct cli_state
*cli
)
138 struct policy_handle pol
;
139 NTSTATUS result
= NT_STATUS_OK
;
140 static bool got_domain_sid
;
142 struct rpc_pipe_client
*lsapipe
= NULL
;
143 union lsa_PolicyInformation
*info
= NULL
;
145 if (got_domain_sid
) return;
147 if (!(mem_ctx
=talloc_init("fetch_machine_sid"))) {
148 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
152 result
= cli_rpc_pipe_open_noauth(cli
, &ndr_table_lsarpc
.syntax_id
,
154 if (!NT_STATUS_IS_OK(result
)) {
155 fprintf(stderr
, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result
) );
159 result
= rpccli_lsa_open_policy(lsapipe
, mem_ctx
, True
,
160 SEC_FLAG_MAXIMUM_ALLOWED
,
162 if (!NT_STATUS_IS_OK(result
)) {
166 result
= rpccli_lsa_QueryInfoPolicy(lsapipe
, mem_ctx
,
168 LSA_POLICY_INFO_ACCOUNT_DOMAIN
,
170 if (!NT_STATUS_IS_OK(result
)) {
174 got_domain_sid
= True
;
175 sid_copy(&domain_sid
, info
->account_domain
.sid
);
177 rpccli_lsa_Close(lsapipe
, mem_ctx
, &pol
);
178 TALLOC_FREE(lsapipe
);
179 talloc_destroy(mem_ctx
);
186 TALLOC_FREE(lsapipe
);
189 fprintf(stderr
, "could not obtain sid for domain %s\n", cli
->domain
);
191 if (!NT_STATUS_IS_OK(result
)) {
192 fprintf(stderr
, "error: %s\n", nt_errstr(result
));
198 /* List the available commands on a given pipe */
200 static NTSTATUS
cmd_listcommands(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
201 int argc
, const char **argv
)
203 struct cmd_list
*tmp
;
204 struct cmd_set
*tmp_set
;
210 printf("Usage: %s <pipe>\n", argv
[0]);
214 /* Help on one command */
216 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
)
218 tmp_set
= tmp
->cmd_set
;
220 if (!StrCaseCmp(argv
[1], tmp_set
->name
))
222 printf("Available commands on the %s pipe:\n\n", tmp_set
->name
);
226 while(tmp_set
->name
) {
227 printf("%30s", tmp_set
->name
);
234 /* drop out of the loop */
243 /* Display help on commands */
245 static NTSTATUS
cmd_help(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
246 int argc
, const char **argv
)
248 struct cmd_list
*tmp
;
249 struct cmd_set
*tmp_set
;
254 printf("Usage: %s [command]\n", argv
[0]);
258 /* Help on one command */
261 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
263 tmp_set
= tmp
->cmd_set
;
265 while(tmp_set
->name
) {
266 if (strequal(argv
[1], tmp_set
->name
)) {
267 if (tmp_set
->usage
&&
269 printf("%s\n", tmp_set
->usage
);
271 printf("No help for %s\n", tmp_set
->name
);
280 printf("No such command: %s\n", argv
[1]);
284 /* List all commands */
286 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
288 tmp_set
= tmp
->cmd_set
;
290 while(tmp_set
->name
) {
292 printf("%15s\t\t%s\n", tmp_set
->name
,
293 tmp_set
->description
? tmp_set
->description
:
303 /* Change the debug level */
305 static NTSTATUS
cmd_debuglevel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
306 int argc
, const char **argv
)
309 printf("Usage: %s [debuglevel]\n", argv
[0]);
314 DEBUGLEVEL
= atoi(argv
[1]);
317 printf("debuglevel is %d\n", DEBUGLEVEL
);
322 static NTSTATUS
cmd_quit(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
323 int argc
, const char **argv
)
326 return NT_STATUS_OK
; /* NOTREACHED */
329 static NTSTATUS
cmd_set_ss_level(void)
331 struct cmd_list
*tmp
;
333 /* Close any existing connections not at this level. */
335 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
336 struct cmd_set
*tmp_set
;
338 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
339 if (tmp_set
->rpc_pipe
== NULL
) {
343 if ((tmp_set
->rpc_pipe
->auth
->auth_type
344 != pipe_default_auth_type
)
345 || (tmp_set
->rpc_pipe
->auth
->auth_level
346 != pipe_default_auth_level
)) {
347 TALLOC_FREE(tmp_set
->rpc_pipe
);
348 tmp_set
->rpc_pipe
= NULL
;
355 static NTSTATUS
cmd_set_transport(void)
357 struct cmd_list
*tmp
;
359 /* Close any existing connections not at this level. */
361 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
362 struct cmd_set
*tmp_set
;
364 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
365 if (tmp_set
->rpc_pipe
== NULL
) {
369 if (tmp_set
->rpc_pipe
->transport
->transport
!= default_transport
) {
370 TALLOC_FREE(tmp_set
->rpc_pipe
);
371 tmp_set
->rpc_pipe
= NULL
;
378 static NTSTATUS
cmd_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
379 int argc
, const char **argv
)
381 const char *type
= "NTLMSSP";
383 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
384 pipe_default_auth_type
= PIPE_AUTH_TYPE_NTLMSSP
;
387 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
393 if (strequal(type
, "NTLMSSP")) {
394 pipe_default_auth_type
= PIPE_AUTH_TYPE_NTLMSSP
;
395 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
396 pipe_default_auth_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
397 } else if (strequal(type
, "SCHANNEL")) {
398 pipe_default_auth_type
= PIPE_AUTH_TYPE_SCHANNEL
;
400 printf("unknown type %s\n", type
);
401 return NT_STATUS_INVALID_LEVEL
;
405 d_printf("Setting %s - sign\n", type
);
407 return cmd_set_ss_level();
410 static NTSTATUS
cmd_seal(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
411 int argc
, const char **argv
)
413 const char *type
= "NTLMSSP";
415 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
416 pipe_default_auth_type
= PIPE_AUTH_TYPE_NTLMSSP
;
419 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv
[0]);
425 if (strequal(type
, "NTLMSSP")) {
426 pipe_default_auth_type
= PIPE_AUTH_TYPE_NTLMSSP
;
427 } else if (strequal(type
, "NTLMSSP_SPNEGO")) {
428 pipe_default_auth_type
= PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
;
429 } else if (strequal(type
, "SCHANNEL")) {
430 pipe_default_auth_type
= PIPE_AUTH_TYPE_SCHANNEL
;
432 printf("unknown type %s\n", type
);
433 return NT_STATUS_INVALID_LEVEL
;
437 d_printf("Setting %s - sign and seal\n", type
);
439 return cmd_set_ss_level();
442 static NTSTATUS
cmd_timeout(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
443 int argc
, const char **argv
)
445 struct cmd_list
*tmp
;
448 printf("Usage: %s timeout\n", argv
[0]);
453 timeout
= atoi(argv
[1]);
455 for (tmp
= cmd_list
; tmp
; tmp
= tmp
->next
) {
457 struct cmd_set
*tmp_set
;
459 for (tmp_set
= tmp
->cmd_set
; tmp_set
->name
; tmp_set
++) {
460 if (tmp_set
->rpc_pipe
== NULL
) {
464 rpccli_set_timeout(tmp_set
->rpc_pipe
, timeout
);
469 printf("timeout is %d\n", timeout
);
475 static NTSTATUS
cmd_none(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
476 int argc
, const char **argv
)
478 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_NONE
;
479 pipe_default_auth_type
= PIPE_AUTH_TYPE_NONE
;
481 return cmd_set_ss_level();
484 static NTSTATUS
cmd_schannel(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
485 int argc
, const char **argv
)
487 d_printf("Setting schannel - sign and seal\n");
488 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_PRIVACY
;
489 pipe_default_auth_type
= PIPE_AUTH_TYPE_SCHANNEL
;
491 return cmd_set_ss_level();
494 static NTSTATUS
cmd_schannel_sign(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
495 int argc
, const char **argv
)
497 d_printf("Setting schannel - sign only\n");
498 pipe_default_auth_level
= DCERPC_AUTH_LEVEL_INTEGRITY
;
499 pipe_default_auth_type
= PIPE_AUTH_TYPE_SCHANNEL
;
501 return cmd_set_ss_level();
504 static NTSTATUS
cmd_choose_transport(struct rpc_pipe_client
*cli
, TALLOC_CTX
*mem_ctx
,
505 int argc
, const char **argv
)
510 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv
[0]);
514 if (strequal(argv
[1], "NCACN_NP")) {
515 default_transport
= NCACN_NP
;
516 } else if (strequal(argv
[1], "NCACN_IP_TCP")) {
517 default_transport
= NCACN_IP_TCP
;
519 printf("transport type: %s unknown or not supported\n", argv
[1]);
520 return NT_STATUS_NOT_SUPPORTED
;
523 status
= cmd_set_transport();
524 if (!NT_STATUS_IS_OK(status
)) {
528 printf("default transport is now: %s\n", argv
[1]);
533 /* Built in rpcclient commands */
535 static struct cmd_set rpcclient_commands
[] = {
537 { "GENERAL OPTIONS" },
539 { "help", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
540 { "?", RPC_RTYPE_NTSTATUS
, cmd_help
, NULL
, NULL
, NULL
, "Get help on commands", "[command]" },
541 { "debuglevel", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
542 { "debug", RPC_RTYPE_NTSTATUS
, cmd_debuglevel
, NULL
, NULL
, NULL
, "Set debug level", "level" },
543 { "list", RPC_RTYPE_NTSTATUS
, cmd_listcommands
, NULL
, NULL
, NULL
, "List available commands on <pipe>", "pipe" },
544 { "exit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
545 { "quit", RPC_RTYPE_NTSTATUS
, cmd_quit
, NULL
, NULL
, NULL
, "Exit program", "" },
546 { "sign", RPC_RTYPE_NTSTATUS
, cmd_sign
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be signed", "" },
547 { "seal", RPC_RTYPE_NTSTATUS
, cmd_seal
, NULL
, NULL
, NULL
, "Force RPC pipe connections to be sealed", "" },
548 { "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.", "" },
549 { "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.", "" },
550 { "timeout", RPC_RTYPE_NTSTATUS
, cmd_timeout
, NULL
, NULL
, NULL
, "Set timeout (in milliseonds) for RPC operations", "" },
551 { "transport", RPC_RTYPE_NTSTATUS
, cmd_choose_transport
, NULL
, NULL
, NULL
, "Choose ncacn transport for RPC operations", "" },
552 { "none", RPC_RTYPE_NTSTATUS
, cmd_none
, NULL
, NULL
, NULL
, "Force RPC pipe connections to have no special properties", "" },
557 static struct cmd_set separator_command
[] = {
558 { "---------------", MAX_RPC_RETURN_TYPE
, NULL
, NULL
, NULL
, NULL
, "----------------------" },
563 /* Various pipe commands */
565 extern struct cmd_set lsarpc_commands
[];
566 extern struct cmd_set samr_commands
[];
567 extern struct cmd_set spoolss_commands
[];
568 extern struct cmd_set netlogon_commands
[];
569 extern struct cmd_set srvsvc_commands
[];
570 extern struct cmd_set dfs_commands
[];
571 extern struct cmd_set ds_commands
[];
572 extern struct cmd_set echo_commands
[];
573 extern struct cmd_set epmapper_commands
[];
574 extern struct cmd_set shutdown_commands
[];
575 extern struct cmd_set test_commands
[];
576 extern struct cmd_set wkssvc_commands
[];
577 extern struct cmd_set ntsvcs_commands
[];
578 extern struct cmd_set drsuapi_commands
[];
579 extern struct cmd_set eventlog_commands
[];
581 static struct cmd_set
*rpcclient_command_list
[] = {
601 static void add_command_set(struct cmd_set
*cmd_set
)
603 struct cmd_list
*entry
;
605 if (!(entry
= SMB_MALLOC_P(struct cmd_list
))) {
606 DEBUG(0, ("out of memory\n"));
612 entry
->cmd_set
= cmd_set
;
613 DLIST_ADD(cmd_list
, entry
);
618 * Call an rpcclient function, passing an argv array.
620 * @param cmd Command to run, as a single string.
622 static NTSTATUS
do_cmd(struct cli_state
*cli
,
623 struct user_auth_info
*auth_info
,
624 struct cmd_set
*cmd_entry
,
625 struct dcerpc_binding
*binding
,
626 int argc
, char **argv
)
635 if (!(mem_ctx
= talloc_init("do_cmd"))) {
636 DEBUG(0, ("talloc_init() failed\n"));
637 return NT_STATUS_NO_MEMORY
;
642 if ((cmd_entry
->interface
!= NULL
) && (cmd_entry
->rpc_pipe
== NULL
)) {
643 switch (pipe_default_auth_type
) {
644 case PIPE_AUTH_TYPE_NONE
:
645 ntresult
= cli_rpc_pipe_open_noauth_transport(
646 cli
, default_transport
,
647 cmd_entry
->interface
,
648 &cmd_entry
->rpc_pipe
);
650 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP
:
651 ntresult
= cli_rpc_pipe_open_spnego_ntlmssp(
652 cli
, cmd_entry
->interface
,
654 pipe_default_auth_level
,
655 get_cmdline_auth_info_domain(auth_info
),
656 get_cmdline_auth_info_username(auth_info
),
657 get_cmdline_auth_info_password(auth_info
),
658 &cmd_entry
->rpc_pipe
);
660 case PIPE_AUTH_TYPE_NTLMSSP
:
661 ntresult
= cli_rpc_pipe_open_ntlmssp(
662 cli
, cmd_entry
->interface
,
664 pipe_default_auth_level
,
665 get_cmdline_auth_info_domain(auth_info
),
666 get_cmdline_auth_info_username(auth_info
),
667 get_cmdline_auth_info_password(auth_info
),
668 &cmd_entry
->rpc_pipe
);
670 case PIPE_AUTH_TYPE_SCHANNEL
:
671 ntresult
= cli_rpc_pipe_open_schannel(
672 cli
, cmd_entry
->interface
,
674 pipe_default_auth_level
,
675 get_cmdline_auth_info_domain(auth_info
),
676 &cmd_entry
->rpc_pipe
);
679 DEBUG(0, ("Could not initialise %s. Invalid "
681 get_pipe_name_from_iface(
682 cmd_entry
->interface
),
683 pipe_default_auth_type
));
684 return NT_STATUS_UNSUCCESSFUL
;
686 if (!NT_STATUS_IS_OK(ntresult
)) {
687 DEBUG(0, ("Could not initialise %s. Error was %s\n",
688 get_pipe_name_from_iface(
689 cmd_entry
->interface
),
690 nt_errstr(ntresult
) ));
694 if (ndr_syntax_id_equal(cmd_entry
->interface
,
695 &ndr_table_netlogon
.syntax_id
)) {
696 uint32_t neg_flags
= NETLOGON_NEG_AUTH2_ADS_FLAGS
;
697 enum netr_SchannelType sec_channel_type
;
698 uchar trust_password
[16];
699 const char *machine_account
;
701 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info
),
702 trust_password
, &machine_account
,
705 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
708 ntresult
= rpccli_netlogon_setup_creds(cmd_entry
->rpc_pipe
,
709 cli
->desthost
, /* server name */
710 get_cmdline_auth_info_domain(auth_info
), /* domain */
711 global_myname(), /* client name */
712 machine_account
, /* machine account name */
717 if (!NT_STATUS_IS_OK(ntresult
)) {
718 DEBUG(0, ("Could not initialise credentials for %s.\n",
719 get_pipe_name_from_iface(
720 cmd_entry
->interface
)));
728 if ( cmd_entry
->returntype
== RPC_RTYPE_NTSTATUS
) {
729 ntresult
= cmd_entry
->ntfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
730 if (!NT_STATUS_IS_OK(ntresult
)) {
731 printf("result was %s\n", nt_errstr(ntresult
));
734 wresult
= cmd_entry
->wfn(cmd_entry
->rpc_pipe
, mem_ctx
, argc
, (const char **) argv
);
735 /* print out the DOS error */
736 if (!W_ERROR_IS_OK(wresult
)) {
737 printf( "result was %s\n", win_errstr(wresult
));
739 ntresult
= W_ERROR_IS_OK(wresult
)?NT_STATUS_OK
:NT_STATUS_UNSUCCESSFUL
;
744 talloc_destroy(mem_ctx
);
751 * Process a command entered at the prompt or as part of -c
753 * @returns The NTSTATUS from running the command.
755 static NTSTATUS
process_cmd(struct user_auth_info
*auth_info
,
756 struct cli_state
*cli
,
757 struct dcerpc_binding
*binding
,
760 struct cmd_list
*temp_list
;
761 NTSTATUS result
= NT_STATUS_OK
;
766 if ((ret
= poptParseArgvString(cmd
, &argc
, (const char ***) &argv
)) != 0) {
767 fprintf(stderr
, "rpcclient: %s\n", poptStrerror(ret
));
768 return NT_STATUS_UNSUCCESSFUL
;
772 /* Walk through a dlist of arrays of commands. */
773 for (temp_list
= cmd_list
; temp_list
; temp_list
= temp_list
->next
) {
774 struct cmd_set
*temp_set
= temp_list
->cmd_set
;
776 while (temp_set
->name
) {
777 if (strequal(argv
[0], temp_set
->name
)) {
778 if (!(temp_set
->returntype
== RPC_RTYPE_NTSTATUS
&& temp_set
->ntfn
) &&
779 !(temp_set
->returntype
== RPC_RTYPE_WERROR
&& temp_set
->wfn
)) {
780 fprintf (stderr
, "Invalid command\n");
784 result
= do_cmd(cli
, auth_info
, temp_set
,
785 binding
, argc
, argv
);
794 printf("command not found: %s\n", argv
[0]);
799 if (!NT_STATUS_IS_OK(result)) {
800 printf("result was %s\n", nt_errstr(result));
804 /* NOTE: popt allocates the whole argv, including the
805 * strings, as a single block. So a single free is
806 * enough to release it -- we don't free the
807 * individual strings. rtfm. */
816 int main(int argc
, char *argv
[])
819 static char *cmdstr
= NULL
;
821 struct cli_state
*cli
= NULL
;
822 static char *opt_ipaddr
=NULL
;
823 struct cmd_set
**cmd_set
;
824 struct sockaddr_storage server_ss
;
826 static int opt_port
= 0;
827 fstring new_workgroup
;
829 TALLOC_CTX
*frame
= talloc_stackframe();
831 struct dcerpc_binding
*binding
= NULL
;
832 const char *binding_string
= NULL
;
833 char *user
, *domain
, *q
;
835 /* make sure the vars that get altered (4th field) are in
836 a fixed location or certain compilers complain */
838 struct poptOption long_options
[] = {
840 {"command", 'c', POPT_ARG_STRING
, &cmdstr
, 'c', "Execute semicolon separated cmds", "COMMANDS"},
841 {"dest-ip", 'I', POPT_ARG_STRING
, &opt_ipaddr
, 'I', "Specify destination IP address", "IP"},
842 {"port", 'p', POPT_ARG_INT
, &opt_port
, 'p', "Specify port number", "PORT"},
844 POPT_COMMON_CONNECTION
845 POPT_COMMON_CREDENTIALS
851 zero_sockaddr(&server_ss
);
855 /* the following functions are part of the Samba debugging
856 facilities. See lib/debug.c */
857 setup_logging("rpcclient", True
);
859 rpcclient_auth_info
= user_auth_info_init(frame
);
860 if (rpcclient_auth_info
== NULL
) {
863 popt_common_set_auth_info(rpcclient_auth_info
);
867 pc
= poptGetContext("rpcclient", argc
, (const char **) argv
,
871 poptPrintHelp(pc
, stderr
, 0);
875 while((opt
= poptGetNextOpt(pc
)) != -1) {
879 if (!interpret_string_addr(&server_ss
,
882 fprintf(stderr
, "%s not a valid IP address\n",
890 /* Get server as remaining unparsed argument. Print usage if more
891 than one unparsed argument is present. */
893 server
= poptGetArg(pc
);
895 if (!server
|| poptGetArg(pc
)) {
896 poptPrintHelp(pc
, stderr
, 0);
910 /* save the workgroup...
912 FIXME!! do we need to do this for other options as well
913 (or maybe a generic way to keep lp_load() from overwriting
916 fstrcpy( new_workgroup
, lp_workgroup() );
918 /* Load smb.conf file */
920 if (!lp_load(get_dyn_CONFIGFILE(),True
,False
,False
,True
))
921 fprintf(stderr
, "Can't load %s\n", get_dyn_CONFIGFILE());
923 if ( strlen(new_workgroup
) != 0 )
924 set_global_myworkgroup( new_workgroup
);
928 * from stdin if necessary
931 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info
) &&
932 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info
)) {
937 set_cmdline_auth_info_getpass(rpcclient_auth_info
);
939 if ((server
[0] == '/' && server
[1] == '/') ||
940 (server
[0] == '\\' && server
[1] == '\\')) {
944 nt_status
= dcerpc_parse_binding(frame
, server
, &binding
);
946 if (!NT_STATUS_IS_OK(nt_status
)) {
948 binding_string
= talloc_asprintf(frame
, "ncacn_np:%s",
949 strip_hostname(server
));
950 if (!binding_string
) {
955 nt_status
= dcerpc_parse_binding(frame
, binding_string
, &binding
);
956 if (!NT_STATUS_IS_OK(nt_status
)) {
962 if (binding
->transport
== NCA_UNKNOWN
) {
963 binding
->transport
= NCACN_NP
;
966 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info
)) {
967 flags
|= CLI_FULL_CONNECTION_USE_KERBEROS
|
968 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS
;
971 user
= talloc_strdup(frame
, get_cmdline_auth_info_username(rpcclient_auth_info
));
972 SMB_ASSERT(user
!= NULL
);
973 domain
= talloc_strdup(frame
, lp_workgroup());
974 SMB_ASSERT(domain
!= NULL
);
975 set_cmdline_auth_info_domain(rpcclient_auth_info
, domain
);
977 if ((q
= strchr_m(user
,'\\'))) {
979 set_cmdline_auth_info_domain(rpcclient_auth_info
, user
);
980 set_cmdline_auth_info_username(rpcclient_auth_info
, q
+1);
984 nt_status
= cli_full_connection(&cli
, global_myname(), binding
->host
,
985 opt_ipaddr
? &server_ss
: NULL
, opt_port
,
987 get_cmdline_auth_info_username(rpcclient_auth_info
),
988 get_cmdline_auth_info_domain(rpcclient_auth_info
),
989 get_cmdline_auth_info_password(rpcclient_auth_info
),
991 get_cmdline_auth_info_signing_state(rpcclient_auth_info
),
994 if (!NT_STATUS_IS_OK(nt_status
)) {
995 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status
)));
1000 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info
)) {
1001 nt_status
= cli_cm_force_encryption(cli
,
1002 get_cmdline_auth_info_username(rpcclient_auth_info
),
1003 get_cmdline_auth_info_password(rpcclient_auth_info
),
1004 get_cmdline_auth_info_domain(rpcclient_auth_info
),
1006 if (!NT_STATUS_IS_OK(nt_status
)) {
1012 #if 0 /* COMMENT OUT FOR TESTING */
1013 memset(cmdline_auth_info
.password
,'X',sizeof(cmdline_auth_info
.password
));
1016 /* Load command lists */
1018 timeout
= cli_set_timeout(cli
, 10000);
1020 cmd_set
= rpcclient_command_list
;
1023 add_command_set(*cmd_set
);
1024 add_command_set(separator_command
);
1028 default_transport
= binding
->transport
;
1030 fetch_machine_sid(cli
);
1032 /* Do anything specified with -c */
1033 if (cmdstr
&& cmdstr
[0]) {
1039 while((cmd
=next_command(&p
)) != NULL
) {
1040 NTSTATUS cmd_result
= process_cmd(rpcclient_auth_info
, cli
,
1043 result
= NT_STATUS_IS_ERR(cmd_result
);
1049 /* Loop around accepting commands */
1054 line
= smb_readline("rpcclient $> ", NULL
, completion_fn
);
1059 if (line
[0] != '\n')
1060 process_cmd(rpcclient_auth_info
, cli
, binding
, line
);