s3: remove various references to server side dcerpc structs (which are not needed).
[Samba.git] / source3 / rpcclient / rpcclient.c
blob17a4cc92266e99f0ba08b5312938858645af7d72
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/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"
32 #include "passdb.h"
34 enum pipe_auth_type_spnego {
35 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
36 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
37 PIPE_AUTH_TYPE_SPNEGO_KRB5
40 struct dom_sid domain_sid;
42 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
43 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
44 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
45 static unsigned int timeout = 0;
46 static enum dcerpc_transport_t default_transport = NCACN_NP;
48 struct user_auth_info *rpcclient_auth_info;
50 /* List to hold groups of commands.
52 * Commands are defined in a list of arrays: arrays are easy to
53 * statically declare, and lists are easier to dynamically extend.
56 static struct cmd_list {
57 struct cmd_list *prev, *next;
58 struct cmd_set *cmd_set;
59 } *cmd_list;
61 /****************************************************************************
62 handle completion of commands for readline
63 ****************************************************************************/
64 static char **completion_fn(const char *text, int start, int end)
66 #define MAX_COMPLETIONS 1000
67 char **matches;
68 int i, count=0;
69 struct cmd_list *commands = cmd_list;
71 #if 0 /* JERRY */
72 /* FIXME!!! -- what to do when completing argument? */
73 /* for words not at the start of the line fallback
74 to filename completion */
75 if (start)
76 return NULL;
77 #endif
79 /* make sure we have a list of valid commands */
80 if (!commands) {
81 return NULL;
84 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
85 if (!matches) {
86 return NULL;
89 matches[count++] = SMB_STRDUP(text);
90 if (!matches[0]) {
91 SAFE_FREE(matches);
92 return NULL;
95 while (commands && count < MAX_COMPLETIONS-1) {
96 if (!commands->cmd_set) {
97 break;
100 for (i=0; commands->cmd_set[i].name; i++) {
101 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
102 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
103 commands->cmd_set[i].ntfn ) ||
104 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
105 commands->cmd_set[i].wfn))) {
106 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
107 if (!matches[count]) {
108 for (i = 0; i < count; i++) {
109 SAFE_FREE(matches[count]);
111 SAFE_FREE(matches);
112 return NULL;
114 count++;
117 commands = commands->next;
120 if (count == 2) {
121 SAFE_FREE(matches[0]);
122 matches[0] = SMB_STRDUP(matches[1]);
124 matches[count] = NULL;
125 return matches;
128 static char *next_command (char **cmdstr)
130 char *command;
131 char *p;
133 if (!cmdstr || !(*cmdstr))
134 return NULL;
136 p = strchr_m(*cmdstr, ';');
137 if (p)
138 *p = '\0';
139 command = SMB_STRDUP(*cmdstr);
140 if (p)
141 *cmdstr = p + 1;
142 else
143 *cmdstr = NULL;
145 return command;
148 /* Fetch the SID for this computer */
150 static void fetch_machine_sid(struct cli_state *cli)
152 struct policy_handle pol;
153 NTSTATUS result = NT_STATUS_OK, status;
154 static bool got_domain_sid;
155 TALLOC_CTX *mem_ctx;
156 struct rpc_pipe_client *lsapipe = NULL;
157 union lsa_PolicyInformation *info = NULL;
158 struct dcerpc_binding_handle *b;
160 if (got_domain_sid) return;
162 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
163 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
164 goto error;
167 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
168 &lsapipe);
169 if (!NT_STATUS_IS_OK(result)) {
170 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
171 goto error;
174 b = lsapipe->binding_handle;
176 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
177 SEC_FLAG_MAXIMUM_ALLOWED,
178 &pol);
179 if (!NT_STATUS_IS_OK(result)) {
180 goto error;
183 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
184 &pol,
185 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
186 &info,
187 &result);
188 if (!NT_STATUS_IS_OK(status)) {
189 result = status;
190 goto error;
192 if (!NT_STATUS_IS_OK(result)) {
193 goto error;
196 got_domain_sid = True;
197 sid_copy(&domain_sid, info->account_domain.sid);
199 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
200 TALLOC_FREE(lsapipe);
201 talloc_destroy(mem_ctx);
203 return;
205 error:
207 if (lsapipe) {
208 TALLOC_FREE(lsapipe);
211 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
213 if (!NT_STATUS_IS_OK(result)) {
214 fprintf(stderr, "error: %s\n", nt_errstr(result));
217 exit(1);
220 /* List the available commands on a given pipe */
222 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
223 int argc, const char **argv)
225 struct cmd_list *tmp;
226 struct cmd_set *tmp_set;
227 int i;
229 /* Usage */
231 if (argc != 2) {
232 printf("Usage: %s <pipe>\n", argv[0]);
233 return NT_STATUS_OK;
236 /* Help on one command */
238 for (tmp = cmd_list; tmp; tmp = tmp->next)
240 tmp_set = tmp->cmd_set;
242 if (!StrCaseCmp(argv[1], tmp_set->name))
244 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
246 i = 0;
247 tmp_set++;
248 while(tmp_set->name) {
249 printf("%30s", tmp_set->name);
250 tmp_set++;
251 i++;
252 if (i%3 == 0)
253 printf("\n");
256 /* drop out of the loop */
257 break;
260 printf("\n\n");
262 return NT_STATUS_OK;
265 /* Display help on commands */
267 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
268 int argc, const char **argv)
270 struct cmd_list *tmp;
271 struct cmd_set *tmp_set;
273 /* Usage */
275 if (argc > 2) {
276 printf("Usage: %s [command]\n", argv[0]);
277 return NT_STATUS_OK;
280 /* Help on one command */
282 if (argc == 2) {
283 for (tmp = cmd_list; tmp; tmp = tmp->next) {
285 tmp_set = tmp->cmd_set;
287 while(tmp_set->name) {
288 if (strequal(argv[1], tmp_set->name)) {
289 if (tmp_set->usage &&
290 tmp_set->usage[0])
291 printf("%s\n", tmp_set->usage);
292 else
293 printf("No help for %s\n", tmp_set->name);
295 return NT_STATUS_OK;
298 tmp_set++;
302 printf("No such command: %s\n", argv[1]);
303 return NT_STATUS_OK;
306 /* List all commands */
308 for (tmp = cmd_list; tmp; tmp = tmp->next) {
310 tmp_set = tmp->cmd_set;
312 while(tmp_set->name) {
314 printf("%15s\t\t%s\n", tmp_set->name,
315 tmp_set->description ? tmp_set->description:
316 "");
318 tmp_set++;
322 return NT_STATUS_OK;
325 /* Change the debug level */
327 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
328 int argc, const char **argv)
330 if (argc > 2) {
331 printf("Usage: %s [debuglevel]\n", argv[0]);
332 return NT_STATUS_OK;
335 if (argc == 2) {
336 lp_set_cmdline("log level", argv[1]);
339 printf("debuglevel is %d\n", DEBUGLEVEL);
341 return NT_STATUS_OK;
344 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
345 int argc, const char **argv)
347 exit(0);
348 return NT_STATUS_OK; /* NOTREACHED */
351 static NTSTATUS cmd_set_ss_level(void)
353 struct cmd_list *tmp;
355 /* Close any existing connections not at this level. */
357 for (tmp = cmd_list; tmp; tmp = tmp->next) {
358 struct cmd_set *tmp_set;
360 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
361 if (tmp_set->rpc_pipe == NULL) {
362 continue;
365 if ((tmp_set->rpc_pipe->auth->auth_type
366 != pipe_default_auth_type)
367 || (tmp_set->rpc_pipe->auth->auth_level
368 != pipe_default_auth_level)) {
369 TALLOC_FREE(tmp_set->rpc_pipe);
370 tmp_set->rpc_pipe = NULL;
374 return NT_STATUS_OK;
377 static NTSTATUS cmd_set_transport(void)
379 struct cmd_list *tmp;
381 /* Close any existing connections not at this level. */
383 for (tmp = cmd_list; tmp; tmp = tmp->next) {
384 struct cmd_set *tmp_set;
386 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
387 if (tmp_set->rpc_pipe == NULL) {
388 continue;
391 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
392 TALLOC_FREE(tmp_set->rpc_pipe);
393 tmp_set->rpc_pipe = NULL;
397 return NT_STATUS_OK;
400 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
401 int argc, const char **argv)
403 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
404 const char *type = "NTLMSSP";
406 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
407 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
409 if (argc > 2) {
410 printf("Usage: %s %s\n", argv[0], p);
411 return NT_STATUS_OK;
414 if (argc == 2) {
415 type = argv[1];
416 if (strequal(type, "KRB5")) {
417 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
418 } else if (strequal(type, "KRB5_SPNEGO")) {
419 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
420 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
421 } else if (strequal(type, "NTLMSSP")) {
422 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
423 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
424 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
425 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
426 } else if (strequal(type, "SCHANNEL")) {
427 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
428 } else {
429 printf("unknown type %s\n", type);
430 printf("Usage: %s %s\n", argv[0], p);
431 return NT_STATUS_INVALID_LEVEL;
435 d_printf("Setting %s - sign\n", type);
437 return cmd_set_ss_level();
440 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
441 int argc, const char **argv)
443 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
444 const char *type = "NTLMSSP";
446 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
447 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
449 if (argc > 2) {
450 printf("Usage: %s %s\n", argv[0], p);
451 return NT_STATUS_OK;
454 if (argc == 2) {
455 type = argv[1];
456 if (strequal(type, "KRB5")) {
457 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
458 } else if (strequal(type, "KRB5_SPNEGO")) {
459 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
460 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
461 } else if (strequal(type, "NTLMSSP")) {
462 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
463 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
464 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
465 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
466 } else if (strequal(type, "SCHANNEL")) {
467 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
468 } else {
469 printf("unknown type %s\n", type);
470 printf("Usage: %s %s\n", argv[0], p);
471 return NT_STATUS_INVALID_LEVEL;
475 d_printf("Setting %s - sign and seal\n", type);
477 return cmd_set_ss_level();
480 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
481 int argc, const char **argv)
483 struct cmd_list *tmp;
485 if (argc > 2) {
486 printf("Usage: %s timeout\n", argv[0]);
487 return NT_STATUS_OK;
490 if (argc == 2) {
491 timeout = atoi(argv[1]);
493 for (tmp = cmd_list; tmp; tmp = tmp->next) {
495 struct cmd_set *tmp_set;
497 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
498 if (tmp_set->rpc_pipe == NULL) {
499 continue;
502 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
507 printf("timeout is %d\n", timeout);
509 return NT_STATUS_OK;
513 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
514 int argc, const char **argv)
516 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
517 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
518 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
520 return cmd_set_ss_level();
523 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
524 int argc, const char **argv)
526 d_printf("Setting schannel - sign and seal\n");
527 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
528 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
530 return cmd_set_ss_level();
533 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
534 int argc, const char **argv)
536 d_printf("Setting schannel - sign only\n");
537 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
538 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
540 return cmd_set_ss_level();
543 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
544 int argc, const char **argv)
546 NTSTATUS status;
548 if (argc != 2) {
549 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
550 return NT_STATUS_OK;
553 if (strequal(argv[1], "NCACN_NP")) {
554 default_transport = NCACN_NP;
555 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
556 default_transport = NCACN_IP_TCP;
557 } else {
558 printf("transport type: %s unknown or not supported\n", argv[1]);
559 return NT_STATUS_NOT_SUPPORTED;
562 status = cmd_set_transport();
563 if (!NT_STATUS_IS_OK(status)) {
564 return status;
567 printf("default transport is now: %s\n", argv[1]);
569 return NT_STATUS_OK;
572 /* Built in rpcclient commands */
574 static struct cmd_set rpcclient_commands[] = {
576 { "GENERAL OPTIONS" },
578 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
579 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
580 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
581 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
582 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
583 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
584 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
585 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
586 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
587 { "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.", "" },
588 { "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.", "" },
589 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
590 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
591 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
593 { NULL }
596 static struct cmd_set separator_command[] = {
597 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
598 { NULL }
602 /* Various pipe commands */
604 extern struct cmd_set lsarpc_commands[];
605 extern struct cmd_set samr_commands[];
606 extern struct cmd_set spoolss_commands[];
607 extern struct cmd_set netlogon_commands[];
608 extern struct cmd_set srvsvc_commands[];
609 extern struct cmd_set dfs_commands[];
610 extern struct cmd_set ds_commands[];
611 extern struct cmd_set echo_commands[];
612 extern struct cmd_set epmapper_commands[];
613 extern struct cmd_set shutdown_commands[];
614 extern struct cmd_set test_commands[];
615 extern struct cmd_set wkssvc_commands[];
616 extern struct cmd_set ntsvcs_commands[];
617 extern struct cmd_set drsuapi_commands[];
618 extern struct cmd_set eventlog_commands[];
619 extern struct cmd_set winreg_commands[];
621 static struct cmd_set *rpcclient_command_list[] = {
622 rpcclient_commands,
623 lsarpc_commands,
624 ds_commands,
625 samr_commands,
626 spoolss_commands,
627 netlogon_commands,
628 srvsvc_commands,
629 dfs_commands,
630 echo_commands,
631 epmapper_commands,
632 shutdown_commands,
633 test_commands,
634 wkssvc_commands,
635 ntsvcs_commands,
636 drsuapi_commands,
637 eventlog_commands,
638 winreg_commands,
639 NULL
642 static void add_command_set(struct cmd_set *cmd_set)
644 struct cmd_list *entry;
646 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
647 DEBUG(0, ("out of memory\n"));
648 return;
651 ZERO_STRUCTP(entry);
653 entry->cmd_set = cmd_set;
654 DLIST_ADD(cmd_list, entry);
659 * Call an rpcclient function, passing an argv array.
661 * @param cmd Command to run, as a single string.
663 static NTSTATUS do_cmd(struct cli_state *cli,
664 struct user_auth_info *auth_info,
665 struct cmd_set *cmd_entry,
666 struct dcerpc_binding *binding,
667 int argc, char **argv)
669 NTSTATUS ntresult;
670 WERROR wresult;
672 TALLOC_CTX *mem_ctx;
674 /* Create mem_ctx */
676 if (!(mem_ctx = talloc_init("do_cmd"))) {
677 DEBUG(0, ("talloc_init() failed\n"));
678 return NT_STATUS_NO_MEMORY;
681 /* Open pipe */
683 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
684 switch (pipe_default_auth_type) {
685 case DCERPC_AUTH_TYPE_NONE:
686 ntresult = cli_rpc_pipe_open_noauth_transport(
687 cli, default_transport,
688 cmd_entry->interface,
689 &cmd_entry->rpc_pipe);
690 break;
691 case DCERPC_AUTH_TYPE_SPNEGO:
692 switch (pipe_default_auth_spnego_type) {
693 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
694 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
695 cli, cmd_entry->interface,
696 default_transport,
697 pipe_default_auth_level,
698 get_cmdline_auth_info_domain(auth_info),
699 get_cmdline_auth_info_username(auth_info),
700 get_cmdline_auth_info_password(auth_info),
701 &cmd_entry->rpc_pipe);
702 break;
703 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
704 ntresult = cli_rpc_pipe_open_spnego_krb5(
705 cli, cmd_entry->interface,
706 default_transport,
707 pipe_default_auth_level,
708 cli->desthost,
709 NULL, NULL,
710 &cmd_entry->rpc_pipe);
711 break;
712 default:
713 ntresult = NT_STATUS_INTERNAL_ERROR;
715 break;
716 case DCERPC_AUTH_TYPE_NTLMSSP:
717 ntresult = cli_rpc_pipe_open_ntlmssp(
718 cli, cmd_entry->interface,
719 default_transport,
720 pipe_default_auth_level,
721 get_cmdline_auth_info_domain(auth_info),
722 get_cmdline_auth_info_username(auth_info),
723 get_cmdline_auth_info_password(auth_info),
724 &cmd_entry->rpc_pipe);
725 break;
726 case DCERPC_AUTH_TYPE_SCHANNEL:
727 ntresult = cli_rpc_pipe_open_schannel(
728 cli, cmd_entry->interface,
729 default_transport,
730 pipe_default_auth_level,
731 get_cmdline_auth_info_domain(auth_info),
732 &cmd_entry->rpc_pipe);
733 break;
734 case DCERPC_AUTH_TYPE_KRB5:
735 ntresult = cli_rpc_pipe_open_krb5(
736 cli, cmd_entry->interface,
737 default_transport,
738 pipe_default_auth_level,
739 cli->desthost,
740 NULL, NULL,
741 &cmd_entry->rpc_pipe);
742 break;
743 default:
744 DEBUG(0, ("Could not initialise %s. Invalid "
745 "auth type %u\n",
746 get_pipe_name_from_syntax(
747 talloc_tos(),
748 cmd_entry->interface),
749 pipe_default_auth_type ));
750 return NT_STATUS_UNSUCCESSFUL;
752 if (!NT_STATUS_IS_OK(ntresult)) {
753 DEBUG(0, ("Could not initialise %s. Error was %s\n",
754 get_pipe_name_from_syntax(
755 talloc_tos(), cmd_entry->interface),
756 nt_errstr(ntresult) ));
757 return ntresult;
760 if (ndr_syntax_id_equal(cmd_entry->interface,
761 &ndr_table_netlogon.syntax_id)) {
762 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
763 enum netr_SchannelType sec_channel_type;
764 uchar trust_password[16];
765 const char *machine_account;
767 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
768 trust_password, &machine_account,
769 &sec_channel_type))
771 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
774 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
775 cli->desthost, /* server name */
776 get_cmdline_auth_info_domain(auth_info), /* domain */
777 global_myname(), /* client name */
778 machine_account, /* machine account name */
779 trust_password,
780 sec_channel_type,
781 &neg_flags);
783 if (!NT_STATUS_IS_OK(ntresult)) {
784 DEBUG(0, ("Could not initialise credentials for %s.\n",
785 get_pipe_name_from_syntax(
786 talloc_tos(),
787 cmd_entry->interface)));
788 return ntresult;
793 /* Run command */
795 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
796 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
797 if (!NT_STATUS_IS_OK(ntresult)) {
798 printf("result was %s\n", nt_errstr(ntresult));
800 } else {
801 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
802 /* print out the DOS error */
803 if (!W_ERROR_IS_OK(wresult)) {
804 printf( "result was %s\n", win_errstr(wresult));
806 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
809 /* Cleanup */
811 talloc_destroy(mem_ctx);
813 return ntresult;
818 * Process a command entered at the prompt or as part of -c
820 * @returns The NTSTATUS from running the command.
822 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
823 struct cli_state *cli,
824 struct dcerpc_binding *binding,
825 char *cmd)
827 struct cmd_list *temp_list;
828 NTSTATUS result = NT_STATUS_OK;
829 int ret;
830 int argc;
831 char **argv = NULL;
833 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
834 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
835 return NT_STATUS_UNSUCCESSFUL;
839 /* Walk through a dlist of arrays of commands. */
840 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
841 struct cmd_set *temp_set = temp_list->cmd_set;
843 while (temp_set->name) {
844 if (strequal(argv[0], temp_set->name)) {
845 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
846 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
847 fprintf (stderr, "Invalid command\n");
848 goto out_free;
851 result = do_cmd(cli, auth_info, temp_set,
852 binding, argc, argv);
854 goto out_free;
856 temp_set++;
860 if (argv[0]) {
861 printf("command not found: %s\n", argv[0]);
864 out_free:
865 /* moved to do_cmd()
866 if (!NT_STATUS_IS_OK(result)) {
867 printf("result was %s\n", nt_errstr(result));
871 /* NOTE: popt allocates the whole argv, including the
872 * strings, as a single block. So a single free is
873 * enough to release it -- we don't free the
874 * individual strings. rtfm. */
875 free(argv);
877 return result;
881 /* Main function */
883 int main(int argc, char *argv[])
885 int opt;
886 static char *cmdstr = NULL;
887 const char *server;
888 struct cli_state *cli = NULL;
889 static char *opt_ipaddr=NULL;
890 struct cmd_set **cmd_set;
891 struct sockaddr_storage server_ss;
892 NTSTATUS nt_status;
893 static int opt_port = 0;
894 fstring new_workgroup;
895 int result = 0;
896 TALLOC_CTX *frame = talloc_stackframe();
897 uint32_t flags = 0;
898 struct dcerpc_binding *binding = NULL;
899 const char *binding_string = NULL;
900 char *user, *domain, *q;
902 /* make sure the vars that get altered (4th field) are in
903 a fixed location or certain compilers complain */
904 poptContext pc;
905 struct poptOption long_options[] = {
906 POPT_AUTOHELP
907 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
908 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
909 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
910 POPT_COMMON_SAMBA
911 POPT_COMMON_CONNECTION
912 POPT_COMMON_CREDENTIALS
913 POPT_TABLEEND
916 load_case_tables();
918 zero_sockaddr(&server_ss);
920 setlinebuf(stdout);
922 /* the following functions are part of the Samba debugging
923 facilities. See lib/debug.c */
924 setup_logging("rpcclient", DEBUG_STDOUT);
926 rpcclient_auth_info = user_auth_info_init(frame);
927 if (rpcclient_auth_info == NULL) {
928 exit(1);
930 popt_common_set_auth_info(rpcclient_auth_info);
932 /* Parse options */
934 pc = poptGetContext("rpcclient", argc, (const char **) argv,
935 long_options, 0);
937 if (argc == 1) {
938 poptPrintHelp(pc, stderr, 0);
939 goto done;
942 while((opt = poptGetNextOpt(pc)) != -1) {
943 switch (opt) {
945 case 'I':
946 if (!interpret_string_addr(&server_ss,
947 opt_ipaddr,
948 AI_NUMERICHOST)) {
949 fprintf(stderr, "%s not a valid IP address\n",
950 opt_ipaddr);
951 result = 1;
952 goto done;
957 /* Get server as remaining unparsed argument. Print usage if more
958 than one unparsed argument is present. */
960 server = poptGetArg(pc);
962 if (!server || poptGetArg(pc)) {
963 poptPrintHelp(pc, stderr, 0);
964 result = 1;
965 goto done;
968 poptFreeContext(pc);
970 load_interfaces();
972 if (!init_names()) {
973 result = 1;
974 goto done;
977 /* save the workgroup...
979 FIXME!! do we need to do this for other options as well
980 (or maybe a generic way to keep lp_load() from overwriting
981 everything)? */
983 fstrcpy( new_workgroup, lp_workgroup() );
985 /* Load smb.conf file */
987 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
988 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
990 if ( strlen(new_workgroup) != 0 )
991 set_global_myworkgroup( new_workgroup );
994 * Get password
995 * from stdin if necessary
998 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
999 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
1000 result = 1;
1001 goto done;
1004 set_cmdline_auth_info_getpass(rpcclient_auth_info);
1006 if ((server[0] == '/' && server[1] == '/') ||
1007 (server[0] == '\\' && server[1] == '\\')) {
1008 server += 2;
1011 nt_status = dcerpc_parse_binding(frame, server, &binding);
1013 if (!NT_STATUS_IS_OK(nt_status)) {
1015 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1016 strip_hostname(server));
1017 if (!binding_string) {
1018 result = 1;
1019 goto done;
1022 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1023 if (!NT_STATUS_IS_OK(nt_status)) {
1024 result = -1;
1025 goto done;
1029 if (binding->transport == NCA_UNKNOWN) {
1030 binding->transport = NCACN_NP;
1033 if (binding->flags & DCERPC_SIGN) {
1034 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1035 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1037 if (binding->flags & DCERPC_SEAL) {
1038 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1039 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1041 if (binding->flags & DCERPC_AUTH_SPNEGO) {
1042 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1043 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1045 if (binding->flags & DCERPC_AUTH_NTLM) {
1046 /* If neither Integrity or Privacy are requested then
1047 * Use just Connect level */
1048 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1049 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1052 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1053 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1054 } else {
1055 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1058 if (binding->flags & DCERPC_AUTH_KRB5) {
1059 /* If neither Integrity or Privacy are requested then
1060 * Use just Connect level */
1061 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1062 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1065 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1066 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1067 } else {
1068 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1072 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1073 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1074 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1076 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1077 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1080 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1081 SMB_ASSERT(user != NULL);
1082 domain = talloc_strdup(frame, lp_workgroup());
1083 SMB_ASSERT(domain != NULL);
1084 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1086 if ((q = strchr_m(user,'\\'))) {
1087 *q = 0;
1088 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1089 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1093 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1094 opt_ipaddr ? &server_ss : NULL, opt_port,
1095 "IPC$", "IPC",
1096 get_cmdline_auth_info_username(rpcclient_auth_info),
1097 get_cmdline_auth_info_domain(rpcclient_auth_info),
1098 get_cmdline_auth_info_password(rpcclient_auth_info),
1099 flags,
1100 get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1102 if (!NT_STATUS_IS_OK(nt_status)) {
1103 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1104 result = 1;
1105 goto done;
1108 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1109 nt_status = cli_cm_force_encryption(cli,
1110 get_cmdline_auth_info_username(rpcclient_auth_info),
1111 get_cmdline_auth_info_password(rpcclient_auth_info),
1112 get_cmdline_auth_info_domain(rpcclient_auth_info),
1113 "IPC$");
1114 if (!NT_STATUS_IS_OK(nt_status)) {
1115 result = 1;
1116 goto done;
1120 #if 0 /* COMMENT OUT FOR TESTING */
1121 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1122 #endif
1124 /* Load command lists */
1126 timeout = cli_set_timeout(cli, 10000);
1128 cmd_set = rpcclient_command_list;
1130 while(*cmd_set) {
1131 add_command_set(*cmd_set);
1132 add_command_set(separator_command);
1133 cmd_set++;
1136 default_transport = binding->transport;
1138 fetch_machine_sid(cli);
1140 /* Do anything specified with -c */
1141 if (cmdstr && cmdstr[0]) {
1142 char *cmd;
1143 char *p = cmdstr;
1145 result = 0;
1147 while((cmd=next_command(&p)) != NULL) {
1148 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1149 binding, cmd);
1150 SAFE_FREE(cmd);
1151 result = NT_STATUS_IS_ERR(cmd_result);
1154 goto done;
1157 /* Loop around accepting commands */
1159 while(1) {
1160 char *line = NULL;
1162 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1164 if (line == NULL)
1165 break;
1167 if (line[0] != '\n')
1168 process_cmd(rpcclient_auth_info, cli, binding, line);
1169 SAFE_FREE(line);
1172 done:
1173 if (cli != NULL) {
1174 cli_shutdown(cli);
1176 TALLOC_FREE(frame);
1177 return result;