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"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "../librpc/gen_ndr/ndr_netlogon.h"
30 static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
31 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
32 static unsigned int timeout = 0;
33 static enum dcerpc_transport_t default_transport = NCACN_NP;
35 struct user_auth_info *rpcclient_auth_info;
37 /* List to hold groups of commands.
39 * Commands are defined in a list of arrays: arrays are easy to
40 * statically declare, and lists are easier to dynamically extend.
43 static struct cmd_list {
44 struct cmd_list *prev, *next;
45 struct cmd_set *cmd_set;
48 /****************************************************************************
49 handle completion of commands for readline
50 ****************************************************************************/
51 static char **completion_fn(const char *text, int start, int end)
53 #define MAX_COMPLETIONS 100
56 struct cmd_list *commands = cmd_list;
59 /* FIXME!!! -- what to do when completing argument? */
60 /* for words not at the start of the line fallback
61 to filename completion */
66 /* make sure we have a list of valid commands */
71 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
76 matches[count++] = SMB_STRDUP(text);
82 while (commands && count < MAX_COMPLETIONS-1) {
83 if (!commands->cmd_set) {
87 for (i=0; commands->cmd_set[i].name; i++) {
88 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
89 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
90 commands->cmd_set[i].ntfn ) ||
91 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
92 commands->cmd_set[i].wfn))) {
93 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
94 if (!matches[count]) {
95 for (i = 0; i < count; i++) {
96 SAFE_FREE(matches[count]);
104 commands = commands->next;
109 SAFE_FREE(matches[0]);
110 matches[0] = SMB_STRDUP(matches[1]);
112 matches[count] = NULL;
116 static char *next_command (char **cmdstr)
121 if (!cmdstr || !(*cmdstr))
124 p = strchr_m(*cmdstr, ';');
127 command = SMB_STRDUP(*cmdstr);
136 /* Fetch the SID for this computer */
138 static void fetch_machine_sid(struct cli_state *cli)
140 struct policy_handle pol;
141 NTSTATUS result = NT_STATUS_OK;
142 static bool got_domain_sid;
144 struct rpc_pipe_client *lsapipe = NULL;
145 union lsa_PolicyInformation *info = NULL;
147 if (got_domain_sid) return;
149 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
150 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
154 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
156 if (!NT_STATUS_IS_OK(result)) {
157 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
161 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
162 SEC_FLAG_MAXIMUM_ALLOWED,
164 if (!NT_STATUS_IS_OK(result)) {
168 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
170 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
172 if (!NT_STATUS_IS_OK(result)) {
176 got_domain_sid = True;
177 sid_copy(&domain_sid, info->account_domain.sid);
179 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
180 TALLOC_FREE(lsapipe);
181 talloc_destroy(mem_ctx);
188 TALLOC_FREE(lsapipe);
191 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
193 if (!NT_STATUS_IS_OK(result)) {
194 fprintf(stderr, "error: %s\n", nt_errstr(result));
200 /* List the available commands on a given pipe */
202 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
203 int argc, const char **argv)
205 struct cmd_list *tmp;
206 struct cmd_set *tmp_set;
212 printf("Usage: %s <pipe>\n", argv[0]);
216 /* Help on one command */
218 for (tmp = cmd_list; tmp; tmp = tmp->next)
220 tmp_set = tmp->cmd_set;
222 if (!StrCaseCmp(argv[1], tmp_set->name))
224 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
228 while(tmp_set->name) {
229 printf("%30s", tmp_set->name);
236 /* drop out of the loop */
245 /* Display help on commands */
247 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
248 int argc, const char **argv)
250 struct cmd_list *tmp;
251 struct cmd_set *tmp_set;
256 printf("Usage: %s [command]\n", argv[0]);
260 /* Help on one command */
263 for (tmp = cmd_list; tmp; tmp = tmp->next) {
265 tmp_set = tmp->cmd_set;
267 while(tmp_set->name) {
268 if (strequal(argv[1], tmp_set->name)) {
269 if (tmp_set->usage &&
271 printf("%s\n", tmp_set->usage);
273 printf("No help for %s\n", tmp_set->name);
282 printf("No such command: %s\n", argv[1]);
286 /* List all commands */
288 for (tmp = cmd_list; tmp; tmp = tmp->next) {
290 tmp_set = tmp->cmd_set;
292 while(tmp_set->name) {
294 printf("%15s\t\t%s\n", tmp_set->name,
295 tmp_set->description ? tmp_set->description:
305 /* Change the debug level */
307 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
308 int argc, const char **argv)
311 printf("Usage: %s [debuglevel]\n", argv[0]);
316 DEBUGLEVEL = atoi(argv[1]);
319 printf("debuglevel is %d\n", DEBUGLEVEL);
324 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
325 int argc, const char **argv)
328 return NT_STATUS_OK; /* NOTREACHED */
331 static NTSTATUS cmd_set_ss_level(void)
333 struct cmd_list *tmp;
335 /* Close any existing connections not at this level. */
337 for (tmp = cmd_list; tmp; tmp = tmp->next) {
338 struct cmd_set *tmp_set;
340 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
341 if (tmp_set->rpc_pipe == NULL) {
345 if ((tmp_set->rpc_pipe->auth->auth_type
346 != pipe_default_auth_type)
347 || (tmp_set->rpc_pipe->auth->auth_level
348 != pipe_default_auth_level)) {
349 TALLOC_FREE(tmp_set->rpc_pipe);
350 tmp_set->rpc_pipe = NULL;
357 static NTSTATUS cmd_set_transport(void)
359 struct cmd_list *tmp;
361 /* Close any existing connections not at this level. */
363 for (tmp = cmd_list; tmp; tmp = tmp->next) {
364 struct cmd_set *tmp_set;
366 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
367 if (tmp_set->rpc_pipe == NULL) {
371 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
372 TALLOC_FREE(tmp_set->rpc_pipe);
373 tmp_set->rpc_pipe = NULL;
380 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
381 int argc, const char **argv)
383 const char *type = "NTLMSSP";
385 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
386 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
389 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
395 if (strequal(type, "NTLMSSP")) {
396 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
397 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
398 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
399 } else if (strequal(type, "SCHANNEL")) {
400 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
402 printf("unknown type %s\n", type);
403 return NT_STATUS_INVALID_LEVEL;
407 d_printf("Setting %s - sign\n", type);
409 return cmd_set_ss_level();
412 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
413 int argc, const char **argv)
415 const char *type = "NTLMSSP";
417 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
418 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
421 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
427 if (strequal(type, "NTLMSSP")) {
428 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
429 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
430 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
431 } else if (strequal(type, "SCHANNEL")) {
432 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
434 printf("unknown type %s\n", type);
435 return NT_STATUS_INVALID_LEVEL;
439 d_printf("Setting %s - sign and seal\n", type);
441 return cmd_set_ss_level();
444 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
445 int argc, const char **argv)
447 struct cmd_list *tmp;
450 printf("Usage: %s timeout\n", argv[0]);
455 timeout = atoi(argv[1]);
457 for (tmp = cmd_list; tmp; tmp = tmp->next) {
459 struct cmd_set *tmp_set;
461 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
462 if (tmp_set->rpc_pipe == NULL) {
466 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
471 printf("timeout is %d\n", timeout);
477 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
478 int argc, const char **argv)
480 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
481 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
483 return cmd_set_ss_level();
486 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
487 int argc, const char **argv)
489 d_printf("Setting schannel - sign and seal\n");
490 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
491 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
493 return cmd_set_ss_level();
496 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
497 int argc, const char **argv)
499 d_printf("Setting schannel - sign only\n");
500 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
501 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
503 return cmd_set_ss_level();
506 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
507 int argc, const char **argv)
512 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
516 if (strequal(argv[1], "NCACN_NP")) {
517 default_transport = NCACN_NP;
518 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
519 default_transport = NCACN_IP_TCP;
521 printf("transport type: %s unknown or not supported\n", argv[1]);
522 return NT_STATUS_NOT_SUPPORTED;
525 status = cmd_set_transport();
526 if (!NT_STATUS_IS_OK(status)) {
530 printf("default transport is now: %s\n", argv[1]);
535 /* Built in rpcclient commands */
537 static struct cmd_set rpcclient_commands[] = {
539 { "GENERAL OPTIONS" },
541 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
542 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
543 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
544 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
545 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
546 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
547 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
548 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
549 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
550 { "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.", "" },
551 { "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.", "" },
552 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
553 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
554 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
559 static struct cmd_set separator_command[] = {
560 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
565 /* Various pipe commands */
567 extern struct cmd_set lsarpc_commands[];
568 extern struct cmd_set samr_commands[];
569 extern struct cmd_set spoolss_commands[];
570 extern struct cmd_set netlogon_commands[];
571 extern struct cmd_set srvsvc_commands[];
572 extern struct cmd_set dfs_commands[];
573 extern struct cmd_set ds_commands[];
574 extern struct cmd_set echo_commands[];
575 extern struct cmd_set epmapper_commands[];
576 extern struct cmd_set shutdown_commands[];
577 extern struct cmd_set test_commands[];
578 extern struct cmd_set wkssvc_commands[];
579 extern struct cmd_set ntsvcs_commands[];
580 extern struct cmd_set drsuapi_commands[];
581 extern struct cmd_set eventlog_commands[];
583 static struct cmd_set *rpcclient_command_list[] = {
603 static void add_command_set(struct cmd_set *cmd_set)
605 struct cmd_list *entry;
607 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
608 DEBUG(0, ("out of memory\n"));
614 entry->cmd_set = cmd_set;
615 DLIST_ADD(cmd_list, entry);
620 * Call an rpcclient function, passing an argv array.
622 * @param cmd Command to run, as a single string.
624 static NTSTATUS do_cmd(struct cli_state *cli,
625 struct user_auth_info *auth_info,
626 struct cmd_set *cmd_entry,
627 struct dcerpc_binding *binding,
628 int argc, char **argv)
637 if (!(mem_ctx = talloc_init("do_cmd"))) {
638 DEBUG(0, ("talloc_init() failed\n"));
639 return NT_STATUS_NO_MEMORY;
644 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
645 switch (pipe_default_auth_type) {
646 case PIPE_AUTH_TYPE_NONE:
647 ntresult = cli_rpc_pipe_open_noauth_transport(
648 cli, default_transport,
649 cmd_entry->interface,
650 &cmd_entry->rpc_pipe);
652 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
653 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
654 cli, cmd_entry->interface,
656 pipe_default_auth_level,
657 get_cmdline_auth_info_domain(auth_info),
658 get_cmdline_auth_info_username(auth_info),
659 get_cmdline_auth_info_password(auth_info),
660 &cmd_entry->rpc_pipe);
662 case PIPE_AUTH_TYPE_NTLMSSP:
663 ntresult = cli_rpc_pipe_open_ntlmssp(
664 cli, cmd_entry->interface,
666 pipe_default_auth_level,
667 get_cmdline_auth_info_domain(auth_info),
668 get_cmdline_auth_info_username(auth_info),
669 get_cmdline_auth_info_password(auth_info),
670 &cmd_entry->rpc_pipe);
672 case PIPE_AUTH_TYPE_SCHANNEL:
673 ntresult = cli_rpc_pipe_open_schannel(
674 cli, cmd_entry->interface,
676 pipe_default_auth_level,
677 get_cmdline_auth_info_domain(auth_info),
678 &cmd_entry->rpc_pipe);
681 DEBUG(0, ("Could not initialise %s. Invalid "
683 get_pipe_name_from_syntax(
685 cmd_entry->interface),
686 pipe_default_auth_type ));
687 return NT_STATUS_UNSUCCESSFUL;
689 if (!NT_STATUS_IS_OK(ntresult)) {
690 DEBUG(0, ("Could not initialise %s. Error was %s\n",
691 get_pipe_name_from_syntax(
692 talloc_tos(), cmd_entry->interface),
693 nt_errstr(ntresult) ));
697 if (ndr_syntax_id_equal(cmd_entry->interface,
698 &ndr_table_netlogon.syntax_id)) {
699 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
700 enum netr_SchannelType sec_channel_type;
701 uchar trust_password[16];
702 const char *machine_account;
704 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
705 trust_password, &machine_account,
708 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
711 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
712 cli->desthost, /* server name */
713 get_cmdline_auth_info_domain(auth_info), /* domain */
714 global_myname(), /* client name */
715 machine_account, /* machine account name */
720 if (!NT_STATUS_IS_OK(ntresult)) {
721 DEBUG(0, ("Could not initialise credentials for %s.\n",
722 get_pipe_name_from_syntax(
724 cmd_entry->interface)));
732 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
733 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
734 if (!NT_STATUS_IS_OK(ntresult)) {
735 printf("result was %s\n", nt_errstr(ntresult));
738 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
739 /* print out the DOS error */
740 if (!W_ERROR_IS_OK(wresult)) {
741 printf( "result was %s\n", win_errstr(wresult));
743 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
748 talloc_destroy(mem_ctx);
755 * Process a command entered at the prompt or as part of -c
757 * @returns The NTSTATUS from running the command.
759 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
760 struct cli_state *cli,
761 struct dcerpc_binding *binding,
764 struct cmd_list *temp_list;
765 NTSTATUS result = NT_STATUS_OK;
770 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
771 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
772 return NT_STATUS_UNSUCCESSFUL;
776 /* Walk through a dlist of arrays of commands. */
777 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
778 struct cmd_set *temp_set = temp_list->cmd_set;
780 while (temp_set->name) {
781 if (strequal(argv[0], temp_set->name)) {
782 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
783 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
784 fprintf (stderr, "Invalid command\n");
788 result = do_cmd(cli, auth_info, temp_set,
789 binding, argc, argv);
798 printf("command not found: %s\n", argv[0]);
803 if (!NT_STATUS_IS_OK(result)) {
804 printf("result was %s\n", nt_errstr(result));
808 /* NOTE: popt allocates the whole argv, including the
809 * strings, as a single block. So a single free is
810 * enough to release it -- we don't free the
811 * individual strings. rtfm. */
820 int main(int argc, char *argv[])
823 static char *cmdstr = NULL;
825 struct cli_state *cli = NULL;
826 static char *opt_ipaddr=NULL;
827 struct cmd_set **cmd_set;
828 struct sockaddr_storage server_ss;
830 static int opt_port = 0;
831 fstring new_workgroup;
833 TALLOC_CTX *frame = talloc_stackframe();
835 struct dcerpc_binding *binding = NULL;
836 const char *binding_string = NULL;
837 char *user, *domain, *q;
839 /* make sure the vars that get altered (4th field) are in
840 a fixed location or certain compilers complain */
842 struct poptOption long_options[] = {
844 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
845 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
846 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
848 POPT_COMMON_CONNECTION
849 POPT_COMMON_CREDENTIALS
855 zero_sockaddr(&server_ss);
859 /* the following functions are part of the Samba debugging
860 facilities. See lib/debug.c */
861 setup_logging("rpcclient", True);
863 rpcclient_auth_info = user_auth_info_init(frame);
864 if (rpcclient_auth_info == NULL) {
867 popt_common_set_auth_info(rpcclient_auth_info);
871 pc = poptGetContext("rpcclient", argc, (const char **) argv,
875 poptPrintHelp(pc, stderr, 0);
879 while((opt = poptGetNextOpt(pc)) != -1) {
883 if (!interpret_string_addr(&server_ss,
886 fprintf(stderr, "%s not a valid IP address\n",
894 /* Get server as remaining unparsed argument. Print usage if more
895 than one unparsed argument is present. */
897 server = poptGetArg(pc);
899 if (!server || poptGetArg(pc)) {
900 poptPrintHelp(pc, stderr, 0);
914 /* save the workgroup...
916 FIXME!! do we need to do this for other options as well
917 (or maybe a generic way to keep lp_load() from overwriting
920 fstrcpy( new_workgroup, lp_workgroup() );
922 /* Load smb.conf file */
924 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
925 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
927 if ( strlen(new_workgroup) != 0 )
928 set_global_myworkgroup( new_workgroup );
932 * from stdin if necessary
935 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
936 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
941 set_cmdline_auth_info_getpass(rpcclient_auth_info);
943 if ((server[0] == '/' && server[1] == '/') ||
944 (server[0] == '\\' && server[1] == '\\')) {
948 nt_status = dcerpc_parse_binding(frame, server, &binding);
950 if (!NT_STATUS_IS_OK(nt_status)) {
952 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
953 strip_hostname(server));
954 if (!binding_string) {
959 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
960 if (!NT_STATUS_IS_OK(nt_status)) {
966 if (binding->transport == NCA_UNKNOWN) {
967 binding->transport = NCACN_NP;
970 if (binding->flags & DCERPC_SIGN) {
971 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
972 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
974 if (binding->flags & DCERPC_SEAL) {
975 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
976 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
978 if (binding->flags & DCERPC_AUTH_SPNEGO) {
979 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
981 if (binding->flags & DCERPC_AUTH_NTLM) {
982 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
984 if (binding->flags & DCERPC_AUTH_KRB5) {
985 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
988 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
989 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
990 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
992 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
993 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
996 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
997 SMB_ASSERT(user != NULL);
998 domain = talloc_strdup(frame, lp_workgroup());
999 SMB_ASSERT(domain != NULL);
1000 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1002 if ((q = strchr_m(user,'\\'))) {
1004 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1005 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1009 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1010 opt_ipaddr ? &server_ss : NULL, opt_port,
1012 get_cmdline_auth_info_username(rpcclient_auth_info),
1013 get_cmdline_auth_info_domain(rpcclient_auth_info),
1014 get_cmdline_auth_info_password(rpcclient_auth_info),
1016 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
1019 if (!NT_STATUS_IS_OK(nt_status)) {
1020 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1025 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1026 nt_status = cli_cm_force_encryption(cli,
1027 get_cmdline_auth_info_username(rpcclient_auth_info),
1028 get_cmdline_auth_info_password(rpcclient_auth_info),
1029 get_cmdline_auth_info_domain(rpcclient_auth_info),
1031 if (!NT_STATUS_IS_OK(nt_status)) {
1037 #if 0 /* COMMENT OUT FOR TESTING */
1038 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1041 /* Load command lists */
1043 timeout = cli_set_timeout(cli, 10000);
1045 cmd_set = rpcclient_command_list;
1048 add_command_set(*cmd_set);
1049 add_command_set(separator_command);
1053 default_transport = binding->transport;
1055 fetch_machine_sid(cli);
1057 /* Do anything specified with -c */
1058 if (cmdstr && cmdstr[0]) {
1064 while((cmd=next_command(&p)) != NULL) {
1065 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1068 result = NT_STATUS_IS_ERR(cmd_result);
1074 /* Loop around accepting commands */
1079 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1084 if (line[0] != '\n')
1085 process_cmd(rpcclient_auth_info, cli, binding, line);