s3:net: remove alloc parameter from idmap_store_secret() (net idmap store).
[Samba.git] / source3 / rpcclient / rpcclient.c
blobcd77a942b4accf0bbb4fa346f95b3fb313f493b2
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
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/>.
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/cli_lsa.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31 #include "../libcli/security/security.h"
33 enum pipe_auth_type_spnego {
34 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
35 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
36 PIPE_AUTH_TYPE_SPNEGO_KRB5
39 struct dom_sid domain_sid;
41 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
42 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
43 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
44 static unsigned int timeout = 0;
45 static enum dcerpc_transport_t default_transport = NCACN_NP;
47 struct user_auth_info *rpcclient_auth_info;
49 /* List to hold groups of commands.
51 * Commands are defined in a list of arrays: arrays are easy to
52 * statically declare, and lists are easier to dynamically extend.
55 static struct cmd_list {
56 struct cmd_list *prev, *next;
57 struct cmd_set *cmd_set;
58 } *cmd_list;
60 /****************************************************************************
61 handle completion of commands for readline
62 ****************************************************************************/
63 static char **completion_fn(const char *text, int start, int end)
65 #define MAX_COMPLETIONS 1000
66 char **matches;
67 int i, count=0;
68 struct cmd_list *commands = cmd_list;
70 #if 0 /* JERRY */
71 /* FIXME!!! -- what to do when completing argument? */
72 /* for words not at the start of the line fallback
73 to filename completion */
74 if (start)
75 return NULL;
76 #endif
78 /* make sure we have a list of valid commands */
79 if (!commands) {
80 return NULL;
83 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
84 if (!matches) {
85 return NULL;
88 matches[count++] = SMB_STRDUP(text);
89 if (!matches[0]) {
90 SAFE_FREE(matches);
91 return NULL;
94 while (commands && count < MAX_COMPLETIONS-1) {
95 if (!commands->cmd_set) {
96 break;
99 for (i=0; commands->cmd_set[i].name; i++) {
100 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
101 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
102 commands->cmd_set[i].ntfn ) ||
103 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
104 commands->cmd_set[i].wfn))) {
105 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
106 if (!matches[count]) {
107 for (i = 0; i < count; i++) {
108 SAFE_FREE(matches[count]);
110 SAFE_FREE(matches);
111 return NULL;
113 count++;
116 commands = commands->next;
119 if (count == 2) {
120 SAFE_FREE(matches[0]);
121 matches[0] = SMB_STRDUP(matches[1]);
123 matches[count] = NULL;
124 return matches;
127 static char *next_command (char **cmdstr)
129 char *command;
130 char *p;
132 if (!cmdstr || !(*cmdstr))
133 return NULL;
135 p = strchr_m(*cmdstr, ';');
136 if (p)
137 *p = '\0';
138 command = SMB_STRDUP(*cmdstr);
139 if (p)
140 *cmdstr = p + 1;
141 else
142 *cmdstr = NULL;
144 return command;
147 /* Fetch the SID for this computer */
149 static void fetch_machine_sid(struct cli_state *cli)
151 struct policy_handle pol;
152 NTSTATUS result = NT_STATUS_OK;
153 static bool got_domain_sid;
154 TALLOC_CTX *mem_ctx;
155 struct rpc_pipe_client *lsapipe = NULL;
156 union lsa_PolicyInformation *info = NULL;
158 if (got_domain_sid) return;
160 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
161 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
162 goto error;
165 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
166 &lsapipe);
167 if (!NT_STATUS_IS_OK(result)) {
168 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
169 goto error;
172 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
173 SEC_FLAG_MAXIMUM_ALLOWED,
174 &pol);
175 if (!NT_STATUS_IS_OK(result)) {
176 goto error;
179 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
180 &pol,
181 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
182 &info);
183 if (!NT_STATUS_IS_OK(result)) {
184 goto error;
187 got_domain_sid = True;
188 sid_copy(&domain_sid, info->account_domain.sid);
190 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
191 TALLOC_FREE(lsapipe);
192 talloc_destroy(mem_ctx);
194 return;
196 error:
198 if (lsapipe) {
199 TALLOC_FREE(lsapipe);
202 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
204 if (!NT_STATUS_IS_OK(result)) {
205 fprintf(stderr, "error: %s\n", nt_errstr(result));
208 exit(1);
211 /* List the available commands on a given pipe */
213 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
214 int argc, const char **argv)
216 struct cmd_list *tmp;
217 struct cmd_set *tmp_set;
218 int i;
220 /* Usage */
222 if (argc != 2) {
223 printf("Usage: %s <pipe>\n", argv[0]);
224 return NT_STATUS_OK;
227 /* Help on one command */
229 for (tmp = cmd_list; tmp; tmp = tmp->next)
231 tmp_set = tmp->cmd_set;
233 if (!StrCaseCmp(argv[1], tmp_set->name))
235 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
237 i = 0;
238 tmp_set++;
239 while(tmp_set->name) {
240 printf("%30s", tmp_set->name);
241 tmp_set++;
242 i++;
243 if (i%3 == 0)
244 printf("\n");
247 /* drop out of the loop */
248 break;
251 printf("\n\n");
253 return NT_STATUS_OK;
256 /* Display help on commands */
258 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
259 int argc, const char **argv)
261 struct cmd_list *tmp;
262 struct cmd_set *tmp_set;
264 /* Usage */
266 if (argc > 2) {
267 printf("Usage: %s [command]\n", argv[0]);
268 return NT_STATUS_OK;
271 /* Help on one command */
273 if (argc == 2) {
274 for (tmp = cmd_list; tmp; tmp = tmp->next) {
276 tmp_set = tmp->cmd_set;
278 while(tmp_set->name) {
279 if (strequal(argv[1], tmp_set->name)) {
280 if (tmp_set->usage &&
281 tmp_set->usage[0])
282 printf("%s\n", tmp_set->usage);
283 else
284 printf("No help for %s\n", tmp_set->name);
286 return NT_STATUS_OK;
289 tmp_set++;
293 printf("No such command: %s\n", argv[1]);
294 return NT_STATUS_OK;
297 /* List all commands */
299 for (tmp = cmd_list; tmp; tmp = tmp->next) {
301 tmp_set = tmp->cmd_set;
303 while(tmp_set->name) {
305 printf("%15s\t\t%s\n", tmp_set->name,
306 tmp_set->description ? tmp_set->description:
307 "");
309 tmp_set++;
313 return NT_STATUS_OK;
316 /* Change the debug level */
318 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
319 int argc, const char **argv)
321 if (argc > 2) {
322 printf("Usage: %s [debuglevel]\n", argv[0]);
323 return NT_STATUS_OK;
326 if (argc == 2) {
327 lp_set_cmdline("log level", argv[1]);
330 printf("debuglevel is %d\n", DEBUGLEVEL);
332 return NT_STATUS_OK;
335 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
336 int argc, const char **argv)
338 exit(0);
339 return NT_STATUS_OK; /* NOTREACHED */
342 static NTSTATUS cmd_set_ss_level(void)
344 struct cmd_list *tmp;
346 /* Close any existing connections not at this level. */
348 for (tmp = cmd_list; tmp; tmp = tmp->next) {
349 struct cmd_set *tmp_set;
351 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
352 if (tmp_set->rpc_pipe == NULL) {
353 continue;
356 if ((tmp_set->rpc_pipe->auth->auth_type
357 != pipe_default_auth_type)
358 || (tmp_set->rpc_pipe->auth->auth_level
359 != pipe_default_auth_level)) {
360 TALLOC_FREE(tmp_set->rpc_pipe);
361 tmp_set->rpc_pipe = NULL;
365 return NT_STATUS_OK;
368 static NTSTATUS cmd_set_transport(void)
370 struct cmd_list *tmp;
372 /* Close any existing connections not at this level. */
374 for (tmp = cmd_list; tmp; tmp = tmp->next) {
375 struct cmd_set *tmp_set;
377 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
378 if (tmp_set->rpc_pipe == NULL) {
379 continue;
382 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
383 TALLOC_FREE(tmp_set->rpc_pipe);
384 tmp_set->rpc_pipe = NULL;
388 return NT_STATUS_OK;
391 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
392 int argc, const char **argv)
394 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
395 const char *type = "NTLMSSP";
397 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
398 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
400 if (argc > 2) {
401 printf("Usage: %s %s\n", argv[0], p);
402 return NT_STATUS_OK;
405 if (argc == 2) {
406 type = argv[1];
407 if (strequal(type, "KRB5")) {
408 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
409 } else if (strequal(type, "KRB5_SPNEGO")) {
410 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
411 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
412 } else if (strequal(type, "NTLMSSP")) {
413 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
414 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
415 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
416 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
417 } else if (strequal(type, "SCHANNEL")) {
418 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
419 } else {
420 printf("unknown type %s\n", type);
421 printf("Usage: %s %s\n", argv[0], p);
422 return NT_STATUS_INVALID_LEVEL;
426 d_printf("Setting %s - sign\n", type);
428 return cmd_set_ss_level();
431 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
432 int argc, const char **argv)
434 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
435 const char *type = "NTLMSSP";
437 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
438 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
440 if (argc > 2) {
441 printf("Usage: %s %s\n", argv[0], p);
442 return NT_STATUS_OK;
445 if (argc == 2) {
446 type = argv[1];
447 if (strequal(type, "KRB5")) {
448 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
449 } else if (strequal(type, "KRB5_SPNEGO")) {
450 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
451 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
452 } else if (strequal(type, "NTLMSSP")) {
453 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
454 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
455 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
456 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
457 } else if (strequal(type, "SCHANNEL")) {
458 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
459 } else {
460 printf("unknown type %s\n", type);
461 printf("Usage: %s %s\n", argv[0], p);
462 return NT_STATUS_INVALID_LEVEL;
466 d_printf("Setting %s - sign and seal\n", type);
468 return cmd_set_ss_level();
471 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
472 int argc, const char **argv)
474 struct cmd_list *tmp;
476 if (argc > 2) {
477 printf("Usage: %s timeout\n", argv[0]);
478 return NT_STATUS_OK;
481 if (argc == 2) {
482 timeout = atoi(argv[1]);
484 for (tmp = cmd_list; tmp; tmp = tmp->next) {
486 struct cmd_set *tmp_set;
488 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
489 if (tmp_set->rpc_pipe == NULL) {
490 continue;
493 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
498 printf("timeout is %d\n", timeout);
500 return NT_STATUS_OK;
504 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
505 int argc, const char **argv)
507 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
508 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
509 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
511 return cmd_set_ss_level();
514 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
515 int argc, const char **argv)
517 d_printf("Setting schannel - sign and seal\n");
518 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
519 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
521 return cmd_set_ss_level();
524 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
525 int argc, const char **argv)
527 d_printf("Setting schannel - sign only\n");
528 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
529 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
531 return cmd_set_ss_level();
534 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
535 int argc, const char **argv)
537 NTSTATUS status;
539 if (argc != 2) {
540 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
541 return NT_STATUS_OK;
544 if (strequal(argv[1], "NCACN_NP")) {
545 default_transport = NCACN_NP;
546 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
547 default_transport = NCACN_IP_TCP;
548 } else {
549 printf("transport type: %s unknown or not supported\n", argv[1]);
550 return NT_STATUS_NOT_SUPPORTED;
553 status = cmd_set_transport();
554 if (!NT_STATUS_IS_OK(status)) {
555 return status;
558 printf("default transport is now: %s\n", argv[1]);
560 return NT_STATUS_OK;
563 /* Built in rpcclient commands */
565 static struct cmd_set rpcclient_commands[] = {
567 { "GENERAL OPTIONS" },
569 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
570 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
571 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
572 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
573 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
574 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
575 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
576 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
577 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
578 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, NULL, NULL, "Force RPC pipe connections to be sealed with 'schannel'. Assumes valid machine account to this domain controller.", "" },
579 { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'. Assumes valid machine account to this domain controller.", "" },
580 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
581 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
582 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
584 { NULL }
587 static struct cmd_set separator_command[] = {
588 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
589 { NULL }
593 /* Various pipe commands */
595 extern struct cmd_set lsarpc_commands[];
596 extern struct cmd_set samr_commands[];
597 extern struct cmd_set spoolss_commands[];
598 extern struct cmd_set netlogon_commands[];
599 extern struct cmd_set srvsvc_commands[];
600 extern struct cmd_set dfs_commands[];
601 extern struct cmd_set ds_commands[];
602 extern struct cmd_set echo_commands[];
603 extern struct cmd_set epmapper_commands[];
604 extern struct cmd_set shutdown_commands[];
605 extern struct cmd_set test_commands[];
606 extern struct cmd_set wkssvc_commands[];
607 extern struct cmd_set ntsvcs_commands[];
608 extern struct cmd_set drsuapi_commands[];
609 extern struct cmd_set eventlog_commands[];
610 extern struct cmd_set winreg_commands[];
612 static struct cmd_set *rpcclient_command_list[] = {
613 rpcclient_commands,
614 lsarpc_commands,
615 ds_commands,
616 samr_commands,
617 spoolss_commands,
618 netlogon_commands,
619 srvsvc_commands,
620 dfs_commands,
621 echo_commands,
622 epmapper_commands,
623 shutdown_commands,
624 test_commands,
625 wkssvc_commands,
626 ntsvcs_commands,
627 drsuapi_commands,
628 eventlog_commands,
629 winreg_commands,
630 NULL
633 static void add_command_set(struct cmd_set *cmd_set)
635 struct cmd_list *entry;
637 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
638 DEBUG(0, ("out of memory\n"));
639 return;
642 ZERO_STRUCTP(entry);
644 entry->cmd_set = cmd_set;
645 DLIST_ADD(cmd_list, entry);
650 * Call an rpcclient function, passing an argv array.
652 * @param cmd Command to run, as a single string.
654 static NTSTATUS do_cmd(struct cli_state *cli,
655 struct user_auth_info *auth_info,
656 struct cmd_set *cmd_entry,
657 struct dcerpc_binding *binding,
658 int argc, char **argv)
660 NTSTATUS ntresult;
661 WERROR wresult;
663 TALLOC_CTX *mem_ctx;
665 /* Create mem_ctx */
667 if (!(mem_ctx = talloc_init("do_cmd"))) {
668 DEBUG(0, ("talloc_init() failed\n"));
669 return NT_STATUS_NO_MEMORY;
672 /* Open pipe */
674 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
675 switch (pipe_default_auth_type) {
676 case DCERPC_AUTH_TYPE_NONE:
677 ntresult = cli_rpc_pipe_open_noauth_transport(
678 cli, default_transport,
679 cmd_entry->interface,
680 &cmd_entry->rpc_pipe);
681 break;
682 case DCERPC_AUTH_TYPE_SPNEGO:
683 switch (pipe_default_auth_spnego_type) {
684 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
685 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
686 cli, cmd_entry->interface,
687 default_transport,
688 pipe_default_auth_level,
689 get_cmdline_auth_info_domain(auth_info),
690 get_cmdline_auth_info_username(auth_info),
691 get_cmdline_auth_info_password(auth_info),
692 &cmd_entry->rpc_pipe);
693 break;
694 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
695 ntresult = cli_rpc_pipe_open_spnego_krb5(
696 cli, cmd_entry->interface,
697 default_transport,
698 pipe_default_auth_level,
699 cli->desthost,
700 NULL, NULL,
701 &cmd_entry->rpc_pipe);
702 break;
703 default:
704 ntresult = NT_STATUS_INTERNAL_ERROR;
706 break;
707 case DCERPC_AUTH_TYPE_NTLMSSP:
708 ntresult = cli_rpc_pipe_open_ntlmssp(
709 cli, cmd_entry->interface,
710 default_transport,
711 pipe_default_auth_level,
712 get_cmdline_auth_info_domain(auth_info),
713 get_cmdline_auth_info_username(auth_info),
714 get_cmdline_auth_info_password(auth_info),
715 &cmd_entry->rpc_pipe);
716 break;
717 case DCERPC_AUTH_TYPE_SCHANNEL:
718 ntresult = cli_rpc_pipe_open_schannel(
719 cli, cmd_entry->interface,
720 default_transport,
721 pipe_default_auth_level,
722 get_cmdline_auth_info_domain(auth_info),
723 &cmd_entry->rpc_pipe);
724 break;
725 case DCERPC_AUTH_TYPE_KRB5:
726 ntresult = cli_rpc_pipe_open_krb5(
727 cli, cmd_entry->interface,
728 default_transport,
729 pipe_default_auth_level,
730 cli->desthost,
731 NULL, NULL,
732 &cmd_entry->rpc_pipe);
733 break;
734 default:
735 DEBUG(0, ("Could not initialise %s. Invalid "
736 "auth type %u\n",
737 get_pipe_name_from_syntax(
738 talloc_tos(),
739 cmd_entry->interface),
740 pipe_default_auth_type ));
741 return NT_STATUS_UNSUCCESSFUL;
743 if (!NT_STATUS_IS_OK(ntresult)) {
744 DEBUG(0, ("Could not initialise %s. Error was %s\n",
745 get_pipe_name_from_syntax(
746 talloc_tos(), cmd_entry->interface),
747 nt_errstr(ntresult) ));
748 return ntresult;
751 if (ndr_syntax_id_equal(cmd_entry->interface,
752 &ndr_table_netlogon.syntax_id)) {
753 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
754 enum netr_SchannelType sec_channel_type;
755 uchar trust_password[16];
756 const char *machine_account;
758 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
759 trust_password, &machine_account,
760 &sec_channel_type))
762 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
765 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
766 cli->desthost, /* server name */
767 get_cmdline_auth_info_domain(auth_info), /* domain */
768 global_myname(), /* client name */
769 machine_account, /* machine account name */
770 trust_password,
771 sec_channel_type,
772 &neg_flags);
774 if (!NT_STATUS_IS_OK(ntresult)) {
775 DEBUG(0, ("Could not initialise credentials for %s.\n",
776 get_pipe_name_from_syntax(
777 talloc_tos(),
778 cmd_entry->interface)));
779 return ntresult;
784 /* Run command */
786 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
787 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
788 if (!NT_STATUS_IS_OK(ntresult)) {
789 printf("result was %s\n", nt_errstr(ntresult));
791 } else {
792 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
793 /* print out the DOS error */
794 if (!W_ERROR_IS_OK(wresult)) {
795 printf( "result was %s\n", win_errstr(wresult));
797 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
800 /* Cleanup */
802 talloc_destroy(mem_ctx);
804 return ntresult;
809 * Process a command entered at the prompt or as part of -c
811 * @returns The NTSTATUS from running the command.
813 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
814 struct cli_state *cli,
815 struct dcerpc_binding *binding,
816 char *cmd)
818 struct cmd_list *temp_list;
819 NTSTATUS result = NT_STATUS_OK;
820 int ret;
821 int argc;
822 char **argv = NULL;
824 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
825 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
826 return NT_STATUS_UNSUCCESSFUL;
830 /* Walk through a dlist of arrays of commands. */
831 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
832 struct cmd_set *temp_set = temp_list->cmd_set;
834 while (temp_set->name) {
835 if (strequal(argv[0], temp_set->name)) {
836 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
837 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
838 fprintf (stderr, "Invalid command\n");
839 goto out_free;
842 result = do_cmd(cli, auth_info, temp_set,
843 binding, argc, argv);
845 goto out_free;
847 temp_set++;
851 if (argv[0]) {
852 printf("command not found: %s\n", argv[0]);
855 out_free:
856 /* moved to do_cmd()
857 if (!NT_STATUS_IS_OK(result)) {
858 printf("result was %s\n", nt_errstr(result));
862 /* NOTE: popt allocates the whole argv, including the
863 * strings, as a single block. So a single free is
864 * enough to release it -- we don't free the
865 * individual strings. rtfm. */
866 free(argv);
868 return result;
872 /* Main function */
874 int main(int argc, char *argv[])
876 int opt;
877 static char *cmdstr = NULL;
878 const char *server;
879 struct cli_state *cli = NULL;
880 static char *opt_ipaddr=NULL;
881 struct cmd_set **cmd_set;
882 struct sockaddr_storage server_ss;
883 NTSTATUS nt_status;
884 static int opt_port = 0;
885 fstring new_workgroup;
886 int result = 0;
887 TALLOC_CTX *frame = talloc_stackframe();
888 uint32_t flags = 0;
889 struct dcerpc_binding *binding = NULL;
890 const char *binding_string = NULL;
891 char *user, *domain, *q;
893 /* make sure the vars that get altered (4th field) are in
894 a fixed location or certain compilers complain */
895 poptContext pc;
896 struct poptOption long_options[] = {
897 POPT_AUTOHELP
898 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
899 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
900 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
901 POPT_COMMON_SAMBA
902 POPT_COMMON_CONNECTION
903 POPT_COMMON_CREDENTIALS
904 POPT_TABLEEND
907 load_case_tables();
909 zero_sockaddr(&server_ss);
911 setlinebuf(stdout);
913 /* the following functions are part of the Samba debugging
914 facilities. See lib/debug.c */
915 setup_logging("rpcclient", DEBUG_STDOUT);
917 rpcclient_auth_info = user_auth_info_init(frame);
918 if (rpcclient_auth_info == NULL) {
919 exit(1);
921 popt_common_set_auth_info(rpcclient_auth_info);
923 /* Parse options */
925 pc = poptGetContext("rpcclient", argc, (const char **) argv,
926 long_options, 0);
928 if (argc == 1) {
929 poptPrintHelp(pc, stderr, 0);
930 goto done;
933 while((opt = poptGetNextOpt(pc)) != -1) {
934 switch (opt) {
936 case 'I':
937 if (!interpret_string_addr(&server_ss,
938 opt_ipaddr,
939 AI_NUMERICHOST)) {
940 fprintf(stderr, "%s not a valid IP address\n",
941 opt_ipaddr);
942 result = 1;
943 goto done;
948 /* Get server as remaining unparsed argument. Print usage if more
949 than one unparsed argument is present. */
951 server = poptGetArg(pc);
953 if (!server || poptGetArg(pc)) {
954 poptPrintHelp(pc, stderr, 0);
955 result = 1;
956 goto done;
959 poptFreeContext(pc);
961 load_interfaces();
963 if (!init_names()) {
964 result = 1;
965 goto done;
968 /* save the workgroup...
970 FIXME!! do we need to do this for other options as well
971 (or maybe a generic way to keep lp_load() from overwriting
972 everything)? */
974 fstrcpy( new_workgroup, lp_workgroup() );
976 /* Load smb.conf file */
978 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
979 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
981 if ( strlen(new_workgroup) != 0 )
982 set_global_myworkgroup( new_workgroup );
985 * Get password
986 * from stdin if necessary
989 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
990 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
991 result = 1;
992 goto done;
995 set_cmdline_auth_info_getpass(rpcclient_auth_info);
997 if ((server[0] == '/' && server[1] == '/') ||
998 (server[0] == '\\' && server[1] == '\\')) {
999 server += 2;
1002 nt_status = dcerpc_parse_binding(frame, server, &binding);
1004 if (!NT_STATUS_IS_OK(nt_status)) {
1006 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1007 strip_hostname(server));
1008 if (!binding_string) {
1009 result = 1;
1010 goto done;
1013 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1014 if (!NT_STATUS_IS_OK(nt_status)) {
1015 result = -1;
1016 goto done;
1020 if (binding->transport == NCA_UNKNOWN) {
1021 binding->transport = NCACN_NP;
1024 if (binding->flags & DCERPC_SIGN) {
1025 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1026 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1028 if (binding->flags & DCERPC_SEAL) {
1029 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1030 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1032 if (binding->flags & DCERPC_AUTH_SPNEGO) {
1033 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1034 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1036 if (binding->flags & DCERPC_AUTH_NTLM) {
1037 /* If neither Integrity or Privacy are requested then
1038 * Use just Connect level */
1039 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1040 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1043 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1044 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1045 } else {
1046 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1049 if (binding->flags & DCERPC_AUTH_KRB5) {
1050 /* If neither Integrity or Privacy are requested then
1051 * Use just Connect level */
1052 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1053 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1056 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1057 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1058 } else {
1059 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1063 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1064 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1065 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1067 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1068 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1071 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1072 SMB_ASSERT(user != NULL);
1073 domain = talloc_strdup(frame, lp_workgroup());
1074 SMB_ASSERT(domain != NULL);
1075 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1077 if ((q = strchr_m(user,'\\'))) {
1078 *q = 0;
1079 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1080 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1084 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1085 opt_ipaddr ? &server_ss : NULL, opt_port,
1086 "IPC$", "IPC",
1087 get_cmdline_auth_info_username(rpcclient_auth_info),
1088 get_cmdline_auth_info_domain(rpcclient_auth_info),
1089 get_cmdline_auth_info_password(rpcclient_auth_info),
1090 flags,
1091 get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1093 if (!NT_STATUS_IS_OK(nt_status)) {
1094 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1095 result = 1;
1096 goto done;
1099 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1100 nt_status = cli_cm_force_encryption(cli,
1101 get_cmdline_auth_info_username(rpcclient_auth_info),
1102 get_cmdline_auth_info_password(rpcclient_auth_info),
1103 get_cmdline_auth_info_domain(rpcclient_auth_info),
1104 "IPC$");
1105 if (!NT_STATUS_IS_OK(nt_status)) {
1106 result = 1;
1107 goto done;
1111 #if 0 /* COMMENT OUT FOR TESTING */
1112 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1113 #endif
1115 /* Load command lists */
1117 timeout = cli_set_timeout(cli, 10000);
1119 cmd_set = rpcclient_command_list;
1121 while(*cmd_set) {
1122 add_command_set(*cmd_set);
1123 add_command_set(separator_command);
1124 cmd_set++;
1127 default_transport = binding->transport;
1129 fetch_machine_sid(cli);
1131 /* Do anything specified with -c */
1132 if (cmdstr && cmdstr[0]) {
1133 char *cmd;
1134 char *p = cmdstr;
1136 result = 0;
1138 while((cmd=next_command(&p)) != NULL) {
1139 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1140 binding, cmd);
1141 SAFE_FREE(cmd);
1142 result = NT_STATUS_IS_ERR(cmd_result);
1145 goto done;
1148 /* Loop around accepting commands */
1150 while(1) {
1151 char *line = NULL;
1153 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1155 if (line == NULL)
1156 break;
1158 if (line[0] != '\n')
1159 process_cmd(rpcclient_auth_info, cli, binding, line);
1160 SAFE_FREE(line);
1163 done:
1164 if (cli != NULL) {
1165 cli_shutdown(cli);
1167 TALLOC_FREE(frame);
1168 return result;