s3:winbindd: Remove unused set_routing_domain()
[samba.git] / source3 / rpcclient / rpcclient.c
blobdea0b24976d8b700e8a9429181ff9570c592bc99
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 "../libcli/auth/netlogon_creds_cli.h"
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31 #include "../libcli/security/security.h"
32 #include "passdb.h"
33 #include "libsmb/libsmb.h"
34 #include "auth/gensec/gensec.h"
35 #include "../libcli/smb/smbXcli_base.h"
36 #include "messages.h"
37 #include "cmdline_contexts.h"
38 #include "../librpc/gen_ndr/ndr_samr.h"
39 #include "lib/cmdline/cmdline.h"
40 #include "lib/param/param.h"
42 enum pipe_auth_type_spnego {
43 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
44 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
45 PIPE_AUTH_TYPE_SPNEGO_KRB5
48 static unsigned int timeout = 10000;
50 struct messaging_context *rpcclient_msg_ctx;
51 struct netlogon_creds_cli_context *rpcclient_netlogon_creds;
52 static const char *rpcclient_netlogon_domain;
54 /* List to hold groups of commands.
56 * Commands are defined in a list of arrays: arrays are easy to
57 * statically declare, and lists are easier to dynamically extend.
60 static struct cmd_list {
61 struct cmd_list *prev, *next;
62 struct cmd_set *cmd_set;
63 } *cmd_list;
65 /****************************************************************************
66 handle completion of commands for readline
67 ****************************************************************************/
68 static char **completion_fn(const char *text, int start, int end)
70 #define MAX_COMPLETIONS 1000
71 char **matches;
72 size_t i, count=0;
73 struct cmd_list *commands = cmd_list;
75 #if 0 /* JERRY */
76 /* FIXME!!! -- what to do when completing argument? */
77 /* for words not at the start of the line fallback
78 to filename completion */
79 if (start)
80 return NULL;
81 #endif
83 /* make sure we have a list of valid commands */
84 if (!commands) {
85 return NULL;
88 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
89 if (!matches) {
90 return NULL;
93 matches[count++] = SMB_STRDUP(text);
94 if (!matches[0]) {
95 SAFE_FREE(matches);
96 return NULL;
99 while (commands && count < MAX_COMPLETIONS-1) {
100 if (!commands->cmd_set) {
101 break;
104 for (i=0; commands->cmd_set[i].name; i++) {
105 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
106 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
107 commands->cmd_set[i].ntfn ) ||
108 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
109 commands->cmd_set[i].wfn))) {
110 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
111 if (!matches[count]) {
112 for (i = 0; i < count; i++) {
113 SAFE_FREE(matches[count]);
115 SAFE_FREE(matches);
116 return NULL;
118 count++;
121 commands = commands->next;
124 if (count == 2) {
125 SAFE_FREE(matches[0]);
126 matches[0] = SMB_STRDUP(matches[1]);
128 matches[count] = NULL;
129 return matches;
132 static char *next_command (char **cmdstr)
134 char *command;
135 char *p;
137 if (!cmdstr || !(*cmdstr))
138 return NULL;
140 p = strchr_m(*cmdstr, ';');
141 if (p)
142 *p = '\0';
143 command = SMB_STRDUP(*cmdstr);
144 if (p)
145 *cmdstr = p + 1;
146 else
147 *cmdstr = NULL;
149 return command;
152 static void binding_get_auth_info(
153 const struct dcerpc_binding *b,
154 enum dcerpc_AuthType *_auth_type,
155 enum dcerpc_AuthLevel *_auth_level,
156 enum credentials_use_kerberos *_krb5_state)
158 uint32_t bflags = dcerpc_binding_get_flags(b);
159 enum dcerpc_AuthLevel auth_level = DCERPC_AUTH_LEVEL_NONE;
160 enum dcerpc_AuthType auth_type = DCERPC_AUTH_TYPE_NONE;
161 enum credentials_use_kerberos krb5_state = CRED_USE_KERBEROS_DESIRED;
163 if (_krb5_state != NULL) {
164 krb5_state = *_krb5_state;
167 if (bflags & DCERPC_CONNECT) {
168 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
170 if (bflags & DCERPC_PACKET) {
171 auth_level = DCERPC_AUTH_LEVEL_PACKET;
173 if (bflags & DCERPC_SIGN) {
174 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
176 if (bflags & DCERPC_SEAL) {
177 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
180 if (bflags & DCERPC_SCHANNEL) {
181 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
184 if ((auth_level != DCERPC_AUTH_LEVEL_NONE) &&
185 (auth_type == DCERPC_AUTH_TYPE_NONE)) {
186 auth_type = (krb5_state == CRED_USE_KERBEROS_REQUIRED) ?
187 DCERPC_AUTH_TYPE_KRB5 : DCERPC_AUTH_TYPE_NTLMSSP;
190 if (bflags & DCERPC_AUTH_SPNEGO) {
191 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
193 if (bflags & DCERPC_AUTH_NTLM) {
194 krb5_state = CRED_USE_KERBEROS_DISABLED;
196 if (bflags & DCERPC_AUTH_KRB5) {
197 krb5_state = CRED_USE_KERBEROS_REQUIRED;
201 if (auth_type != DCERPC_AUTH_TYPE_NONE) {
202 /* If nothing is requested then default to integrity */
203 if (auth_level == DCERPC_AUTH_LEVEL_NONE) {
204 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
208 if (_auth_type != NULL) {
209 *_auth_type = auth_type;
211 if (_auth_level != NULL) {
212 *_auth_level = auth_level;
214 if (_krb5_state != NULL) {
215 *_krb5_state = krb5_state;
219 /* List the available commands on a given pipe */
221 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
222 int argc, const char **argv)
224 struct cmd_list *tmp;
225 struct cmd_set *tmp_set;
226 int i;
228 /* Usage */
230 if (argc != 2) {
231 printf("Usage: %s <pipe>\n", argv[0]);
232 return NT_STATUS_OK;
235 /* Help on one command */
237 for (tmp = cmd_list; tmp; tmp = tmp->next)
239 tmp_set = tmp->cmd_set;
241 if (!strcasecmp_m(argv[1], tmp_set->name))
243 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
245 i = 0;
246 tmp_set++;
247 while(tmp_set->name) {
248 printf("%30s", tmp_set->name);
249 tmp_set++;
250 i++;
251 if (i%3 == 0)
252 printf("\n");
255 /* drop out of the loop */
256 break;
259 printf("\n\n");
261 return NT_STATUS_OK;
264 /* Display help on commands */
266 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
267 int argc, const char **argv)
269 struct cmd_list *tmp;
270 struct cmd_set *tmp_set;
272 /* Usage */
274 if (argc > 2) {
275 printf("Usage: %s [command]\n", argv[0]);
276 return NT_STATUS_OK;
279 /* Help on one command */
281 if (argc == 2) {
282 for (tmp = cmd_list; tmp; tmp = tmp->next) {
284 tmp_set = tmp->cmd_set;
286 while(tmp_set->name) {
287 if (strequal(argv[1], tmp_set->name)) {
288 if (tmp_set->usage &&
289 tmp_set->usage[0])
290 printf("%s\n", tmp_set->usage);
291 else
292 printf("No help for %s\n", tmp_set->name);
294 return NT_STATUS_OK;
297 tmp_set++;
301 printf("No such command: %s\n", argv[1]);
302 return NT_STATUS_OK;
305 /* List all commands */
307 for (tmp = cmd_list; tmp; tmp = tmp->next) {
309 tmp_set = tmp->cmd_set;
311 while(tmp_set->name) {
313 printf("%15s\t\t%s\n", tmp_set->name,
314 tmp_set->description ? tmp_set->description:
315 "");
317 tmp_set++;
321 return NT_STATUS_OK;
324 /* Change the debug level */
326 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
327 int argc, const char **argv)
329 if (argc > 2) {
330 printf("Usage: %s [debuglevel]\n", argv[0]);
331 return NT_STATUS_OK;
334 if (argc == 2) {
335 struct loadparm_context *lp_ctx = samba_cmdline_get_lp_ctx();
336 lpcfg_set_cmdline(lp_ctx, "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(struct dcerpc_binding *binding)
353 struct cmd_list *tmp;
354 enum dcerpc_AuthType auth_type;
355 enum dcerpc_AuthLevel auth_level;
357 /* Close any existing connections not at this level. */
359 binding_get_auth_info(binding, &auth_type, &auth_level, NULL);
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 struct dcerpc_binding_handle *tmp_b = NULL;
366 enum dcerpc_AuthType tmp_auth_type;
367 enum dcerpc_AuthLevel tmp_auth_level;
369 if (tmp_set->rpc_pipe == NULL) {
370 continue;
373 tmp_b = tmp_set->rpc_pipe->binding_handle;
374 dcerpc_binding_handle_auth_info(tmp_b,
375 &tmp_auth_type,
376 &tmp_auth_level);
378 if (tmp_auth_type != auth_type ||
379 tmp_auth_level != auth_level)
381 TALLOC_FREE(tmp_set->rpc_pipe);
385 return NT_STATUS_OK;
388 static NTSTATUS cmd_set_transport(struct dcerpc_binding *b)
390 enum dcerpc_transport_t t = dcerpc_binding_get_transport(b);
391 struct cmd_list *tmp;
393 /* Close any existing connections not at this level. */
395 for (tmp = cmd_list; tmp; tmp = tmp->next) {
396 struct cmd_set *tmp_set;
398 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
399 struct dcerpc_binding_handle *tmp_b = NULL;
400 enum dcerpc_transport_t tmp_t;
402 if (tmp_set->rpc_pipe == NULL) {
403 continue;
406 tmp_b = tmp_set->rpc_pipe->binding_handle;
407 tmp_t = dcerpc_binding_handle_get_transport(tmp_b);
408 if (tmp_t != t) {
409 TALLOC_FREE(tmp_set->rpc_pipe);
413 return NT_STATUS_OK;
416 static NTSTATUS binding_reset_auth(struct dcerpc_binding *b)
418 NTSTATUS status = dcerpc_binding_set_flags(
421 DCERPC_PACKET|
422 DCERPC_CONNECT|
423 DCERPC_SIGN|
424 DCERPC_SEAL|
425 DCERPC_SCHANNEL|
426 DCERPC_AUTH_SPNEGO|
427 DCERPC_AUTH_KRB5|
428 DCERPC_AUTH_NTLM);
429 return status;
432 static NTSTATUS binding_set_auth(
433 struct dcerpc_binding *b, const char *level, const char *type)
435 NTSTATUS status;
437 status = binding_reset_auth(b);
438 if (!NT_STATUS_IS_OK(status)) {
439 return status;
442 if (level != NULL) {
443 status = dcerpc_binding_set_string_option(b, level, level);
444 if (!NT_STATUS_IS_OK(status)) {
445 return status;
449 if (strequal(type, "SPNEGO")) {
450 status = dcerpc_binding_set_string_option(
451 b, "spnego", "spnego");
452 return status;
454 if (strequal(type, "NTLMSSP")) {
455 status = dcerpc_binding_set_string_option(b, "ntlm", "ntlm");
456 return status;
458 if (strequal(type, "NTLMSSP_SPNEGO")) {
459 status = dcerpc_binding_set_string_option(
460 b, "spnego", "spnego");
461 if (!NT_STATUS_IS_OK(status)) {
462 return status;
464 status = dcerpc_binding_set_string_option(b, "ntlm", "ntlm");
465 return status;
467 if (strequal(type, "KRB5")) {
468 status = dcerpc_binding_set_string_option(b, "krb5", "krb5");
469 return status;
471 if (strequal(type, "KRB5_SPNEGO")) {
472 status = dcerpc_binding_set_string_option(
473 b, "spnego", "spnego");
474 if (!NT_STATUS_IS_OK(status)) {
475 return status;
477 status = dcerpc_binding_set_string_option(b, "krb5", "krb5");
478 return status;
480 if (strequal(type, "SCHANNEL")) {
481 status = dcerpc_binding_set_string_option(
482 b, "schannel", "schannel");
483 return status;
486 return NT_STATUS_INVALID_PARAMETER;
489 static NTSTATUS cmd_set_auth(
490 struct dcerpc_binding *binding,
491 const char *level,
492 const char *display,
493 int argc,
494 const char **argv)
496 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
497 const char *type = "NTLMSSP";
498 NTSTATUS status;
500 if (argc > 2) {
501 printf("Usage: %s %s\n", argv[0], p);
502 return NT_STATUS_OK;
505 if (argc == 2) {
506 type = argv[1];
509 status = binding_set_auth(binding, level, type);
510 if (!NT_STATUS_IS_OK(status)) {
511 printf("Usage: %s %s\n", argv[0], p);
512 return status;
515 d_printf("Setting %s - %s: %s\n", type, display, nt_errstr(status));
517 status = cmd_set_ss_level(binding);
518 return status;
521 static NTSTATUS cmd_sign(
522 struct dcerpc_binding *binding,
523 TALLOC_CTX *mem_ctx,
524 int argc,
525 const char **argv)
527 NTSTATUS status = cmd_set_auth(binding, "sign", "sign", argc, argv);
528 return status;
531 static NTSTATUS cmd_seal(
532 struct dcerpc_binding *binding,
533 TALLOC_CTX *mem_ctx,
534 int argc,
535 const char **argv)
537 NTSTATUS status = cmd_set_auth(
538 binding, "seal", "sign and seal", argc, argv);
539 return status;
542 static NTSTATUS cmd_packet(
543 struct dcerpc_binding *binding,
544 TALLOC_CTX *mem_ctx,
545 int argc,
546 const char **argv)
548 NTSTATUS status = cmd_set_auth(
549 binding, "packet", "packet", argc, argv);
550 return status;
553 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
554 int argc, const char **argv)
556 if (argc > 2) {
557 printf("Usage: %s timeout\n", argv[0]);
558 return NT_STATUS_OK;
561 if (argc == 2) {
562 timeout = atoi(argv[1]);
565 printf("timeout is %d\n", timeout);
567 return NT_STATUS_OK;
571 static NTSTATUS cmd_none(
572 struct dcerpc_binding *binding,
573 TALLOC_CTX *mem_ctx,
574 int argc,
575 const char **argv)
577 NTSTATUS status = binding_reset_auth(binding);
578 if (!NT_STATUS_IS_OK(status)) {
579 return status;
581 status = cmd_set_ss_level(binding);
582 return status;
585 static NTSTATUS cmd_schannel(
586 struct dcerpc_binding *binding,
587 TALLOC_CTX *mem_ctx,
588 int argc,
589 const char **_argv)
591 const char *argv[] = { "schannel", "SCHANNEL" };
592 NTSTATUS status = cmd_set_auth(
593 binding, "seal", "sign and seal", 2, argv);
594 return status;
597 static NTSTATUS cmd_schannel_sign(
598 struct dcerpc_binding *binding,
599 TALLOC_CTX *mem_ctx,
600 int argc,
601 const char **_argv)
603 const char *argv[] = { "schannel_sign", "SCHANNEL" };
604 NTSTATUS status = cmd_set_auth(binding, "sign", "sign only", 2, argv);
605 return status;
608 static NTSTATUS cmd_choose_transport(
609 struct dcerpc_binding *binding,
610 TALLOC_CTX *mem_ctx,
611 int argc,
612 const char **argv)
614 NTSTATUS status;
615 enum dcerpc_transport_t transport;
617 if (argc != 2) {
618 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
619 return NT_STATUS_OK;
622 transport = dcerpc_transport_by_name(argv[1]);
623 if (transport == NCA_UNKNOWN) {
624 printf("transport type %s unknown\n", argv[1]);
625 return NT_STATUS_NOT_SUPPORTED;
627 if (!((transport == NCACN_IP_TCP) ||
628 (transport == NCACN_NP) ||
629 (transport == NCALRPC))) {
630 printf("transport %s not supported\n", argv[1]);
631 return NT_STATUS_NOT_SUPPORTED;
634 status = dcerpc_binding_set_transport(binding, transport);
635 if (!NT_STATUS_IS_OK(status)) {
636 return status;
639 status = cmd_set_transport(binding);
640 if (!NT_STATUS_IS_OK(status)) {
641 return status;
644 printf("default transport is now: %s\n", argv[1]);
646 return NT_STATUS_OK;
649 /* Built in rpcclient commands */
651 static struct cmd_set rpcclient_commands[] = {
654 .name = "GENERAL OPTIONS",
658 .name = "help",
659 .returntype = RPC_RTYPE_NTSTATUS,
660 .ntfn = cmd_help,
661 .description = "Get help on commands",
662 .usage = "[command]",
665 .name = "?",
666 .returntype = RPC_RTYPE_NTSTATUS,
667 .ntfn = cmd_help,
668 .description = "Get help on commands",
669 .usage = "[command]",
672 .name = "debuglevel",
673 .returntype = RPC_RTYPE_NTSTATUS,
674 .ntfn = cmd_debuglevel,
675 .description = "Set debug level",
676 .usage = "level",
679 .name = "debug",
680 .returntype = RPC_RTYPE_NTSTATUS,
681 .ntfn = cmd_debuglevel,
682 .description = "Set debug level",
683 .usage = "level",
686 .name = "list",
687 .returntype = RPC_RTYPE_NTSTATUS,
688 .ntfn = cmd_listcommands,
689 .description = "List available commands on <pipe>",
690 .usage = "pipe",
693 .name = "exit",
694 .returntype = RPC_RTYPE_NTSTATUS,
695 .ntfn = cmd_quit,
696 .description = "Exit program",
697 .usage = "",
700 .name = "quit",
701 .returntype = RPC_RTYPE_NTSTATUS,
702 .ntfn = cmd_quit,
703 .description = "Exit program",
704 .usage = "",
707 .name = "sign",
708 .returntype = RPC_RTYPE_BINDING,
709 .bfn = cmd_sign,
710 .description = "Force RPC pipe connections to be signed",
711 .usage = "",
714 .name = "seal",
715 .returntype = RPC_RTYPE_BINDING,
716 .bfn = cmd_seal,
717 .description = "Force RPC pipe connections to be sealed",
718 .usage = "",
721 .name = "packet",
722 .returntype = RPC_RTYPE_BINDING,
723 .bfn = cmd_packet,
724 .description = "Force RPC pipe connections with packet authentication level",
725 .usage = "",
728 .name = "schannel",
729 .returntype = RPC_RTYPE_BINDING,
730 .bfn = cmd_schannel,
731 .description = "Force RPC pipe connections to be sealed with 'schannel'. "
732 "Assumes valid machine account to this domain controller.",
733 .usage = "",
736 .name = "schannelsign",
737 .returntype = RPC_RTYPE_BINDING,
738 .bfn = cmd_schannel_sign,
739 .description = "Force RPC pipe connections to be signed (not sealed) with "
740 "'schannel'. Assumes valid machine account to this domain "
741 "controller.",
742 .usage = "",
745 .name = "timeout",
746 .returntype = RPC_RTYPE_NTSTATUS,
747 .ntfn = cmd_timeout,
748 .description = "Set timeout (in milliseconds) for RPC operations",
749 .usage = "",
752 .name = "transport",
753 .returntype = RPC_RTYPE_BINDING,
754 .bfn = cmd_choose_transport,
755 .description = "Choose ncacn transport for RPC operations",
756 .usage = "",
759 .name = "none",
760 .returntype = RPC_RTYPE_BINDING,
761 .bfn = cmd_none,
762 .description = "Force RPC pipe connections to have no special properties",
763 .usage = "",
766 { .name = NULL, },
769 static struct cmd_set separator_command[] = {
771 .name = "---------------",
772 .returntype = MAX_RPC_RETURN_TYPE,
773 .description = "----------------------"
775 { .name = NULL, },
779 /* Various pipe commands */
781 extern struct cmd_set lsarpc_commands[];
782 extern struct cmd_set samr_commands[];
783 extern struct cmd_set spoolss_commands[];
784 extern struct cmd_set iremotewinspool_commands[];
785 extern struct cmd_set netlogon_commands[];
786 extern struct cmd_set srvsvc_commands[];
787 extern struct cmd_set dfs_commands[];
788 extern struct cmd_set ds_commands[];
789 extern struct cmd_set echo_commands[];
790 extern struct cmd_set epmapper_commands[];
791 extern struct cmd_set shutdown_commands[];
792 extern struct cmd_set wkssvc_commands[];
793 extern struct cmd_set ntsvcs_commands[];
794 extern struct cmd_set drsuapi_commands[];
795 extern struct cmd_set eventlog_commands[];
796 extern struct cmd_set winreg_commands[];
797 extern struct cmd_set fss_commands[];
798 extern struct cmd_set witness_commands[];
799 extern struct cmd_set clusapi_commands[];
800 extern struct cmd_set spotlight_commands[];
801 extern struct cmd_set unixinfo_commands[];
803 static struct cmd_set *rpcclient_command_list[] = {
804 rpcclient_commands,
805 lsarpc_commands,
806 ds_commands,
807 samr_commands,
808 spoolss_commands,
809 iremotewinspool_commands,
810 netlogon_commands,
811 srvsvc_commands,
812 dfs_commands,
813 echo_commands,
814 epmapper_commands,
815 shutdown_commands,
816 wkssvc_commands,
817 ntsvcs_commands,
818 drsuapi_commands,
819 eventlog_commands,
820 winreg_commands,
821 fss_commands,
822 witness_commands,
823 clusapi_commands,
824 spotlight_commands,
825 unixinfo_commands,
826 NULL
829 static void add_command_set(struct cmd_set *cmd_set)
831 struct cmd_list *entry;
833 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
834 DEBUG(0, ("out of memory\n"));
835 return;
838 ZERO_STRUCTP(entry);
840 entry->cmd_set = cmd_set;
841 DLIST_ADD(cmd_list, entry);
844 static NTSTATUS rpccli_ncalrpc_connect(
845 const struct ndr_interface_table *iface,
846 TALLOC_CTX *mem_ctx,
847 struct rpc_pipe_client **prpccli)
849 struct rpc_pipe_client *rpccli = NULL;
850 struct pipe_auth_data *auth = NULL;
851 NTSTATUS status;
853 status = rpc_pipe_open_ncalrpc(mem_ctx, iface, &rpccli);
854 if (!NT_STATUS_IS_OK(status)) {
855 DBG_DEBUG("rpc_pipe_open_ncalrpc failed: %s\n",
856 nt_errstr(status));
857 goto fail;
860 status = rpccli_ncalrpc_bind_data(rpccli, &auth);
861 if (!NT_STATUS_IS_OK(status)) {
862 DBG_DEBUG("rpccli_ncalrpc_bind_data failed: %s\n",
863 nt_errstr(status));
864 goto fail;
867 status = rpc_pipe_bind(rpccli, auth);
868 if (!NT_STATUS_IS_OK(status)) {
869 DBG_DEBUG("rpc_pipe_bind failed: %s\n", nt_errstr(status));
870 goto fail;
873 *prpccli = rpccli;
874 return NT_STATUS_OK;
875 fail:
876 TALLOC_FREE(rpccli);
877 return status;
880 * Call an rpcclient function, passing an argv array.
882 * @param cmd Command to run, as a single string.
884 static NTSTATUS do_cmd(struct cli_state *cli,
885 struct cli_credentials *creds,
886 struct cmd_set *cmd_entry,
887 struct dcerpc_binding *binding,
888 int argc, const char **argv)
890 NTSTATUS ntresult;
891 WERROR wresult;
892 enum dcerpc_transport_t transport;
894 TALLOC_CTX *mem_ctx = talloc_stackframe();
895 const char *remote_name = NULL;
896 const struct sockaddr_storage *remote_sockaddr = NULL;
897 struct sockaddr_storage remote_ss = {
898 .ss_family = AF_UNSPEC,
901 transport = dcerpc_binding_get_transport(binding);
903 if (cli != NULL) {
904 remote_name = smbXcli_conn_remote_name(cli->conn);
905 remote_sockaddr = smbXcli_conn_remote_sockaddr(cli->conn);
906 } else {
907 const char *remote_host =
908 dcerpc_binding_get_string_option(binding, "host");
909 remote_name = dcerpc_binding_get_string_option(
910 binding, "target_hostname");
912 if (remote_host != NULL) {
913 bool ok = interpret_string_addr(
914 &remote_ss, remote_host, 0);
915 if (ok) {
916 remote_sockaddr = &remote_ss;
921 /* Open pipe */
923 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
924 if (transport == NCALRPC) {
925 ntresult = rpccli_ncalrpc_connect(
926 cmd_entry->table, cli, &cmd_entry->rpc_pipe);
927 if (!NT_STATUS_IS_OK(ntresult)) {
928 TALLOC_FREE(mem_ctx);
929 return ntresult;
931 } else {
932 enum dcerpc_AuthType auth_type;
933 enum dcerpc_AuthLevel auth_level;
934 enum credentials_use_kerberos krb5_state =
935 cli_credentials_get_kerberos_state(creds);
937 binding_get_auth_info(
938 binding, &auth_type, &auth_level, &krb5_state);
940 switch (auth_type) {
941 case DCERPC_AUTH_TYPE_NONE:
942 ntresult = cli_rpc_pipe_open_noauth_transport(
943 cli, transport,
944 cmd_entry->table,
945 remote_name,
946 remote_sockaddr,
947 &cmd_entry->rpc_pipe);
948 break;
949 case DCERPC_AUTH_TYPE_SPNEGO:
950 case DCERPC_AUTH_TYPE_NTLMSSP:
951 case DCERPC_AUTH_TYPE_KRB5:
952 cli_credentials_set_kerberos_state(creds,
953 krb5_state,
954 CRED_SPECIFIED);
956 ntresult = cli_rpc_pipe_open_with_creds(
957 cli, cmd_entry->table,
958 transport,
959 auth_type,
960 auth_level,
961 NULL, /* target_service */
962 remote_name,
963 remote_sockaddr,
964 creds,
965 &cmd_entry->rpc_pipe);
966 break;
967 case DCERPC_AUTH_TYPE_SCHANNEL:
968 TALLOC_FREE(rpcclient_netlogon_creds);
969 ntresult = cli_rpc_pipe_open_schannel(
970 cli, rpcclient_msg_ctx,
971 cmd_entry->table,
972 transport,
973 rpcclient_netlogon_domain,
974 remote_name,
975 remote_sockaddr,
976 &cmd_entry->rpc_pipe,
977 rpcclient_msg_ctx,
978 &rpcclient_netlogon_creds);
979 break;
980 default:
981 DEBUG(0, ("Could not initialise %s. Invalid "
982 "auth type %u\n",
983 cmd_entry->table->name,
984 auth_type ));
985 talloc_free(mem_ctx);
986 return NT_STATUS_UNSUCCESSFUL;
988 if (!NT_STATUS_IS_OK(ntresult)) {
989 DBG_ERR("Could not initialise %s. "
990 "Error was %s\n",
991 cmd_entry->table->name,
992 nt_errstr(ntresult));
993 talloc_free(mem_ctx);
994 return ntresult;
997 if (rpcclient_netlogon_creds == NULL &&
998 cmd_entry->use_netlogon_creds) {
999 const char *dc_name =
1000 cmd_entry->rpc_pipe->desthost;
1001 const char *domain = rpcclient_netlogon_domain;
1002 struct cli_credentials *trust_creds = NULL;
1004 ntresult = pdb_get_trust_credentials(
1005 domain,
1006 NULL,
1007 mem_ctx,
1008 &trust_creds);
1009 if (!NT_STATUS_IS_OK(ntresult)) {
1010 DBG_ERR("Failed to fetch trust "
1011 "credentials for "
1012 "%s to connect to %s: %s\n",
1013 domain,
1014 cmd_entry->table->name,
1015 nt_errstr(ntresult));
1016 TALLOC_FREE(cmd_entry->rpc_pipe);
1017 talloc_free(mem_ctx);
1018 return ntresult;
1021 ntresult = rpccli_create_netlogon_creds_ctx(
1022 trust_creds,
1023 dc_name,
1024 rpcclient_msg_ctx,
1025 rpcclient_msg_ctx,
1026 &rpcclient_netlogon_creds);
1027 if (!NT_STATUS_IS_OK(ntresult)) {
1028 DBG_ERR("Could not initialise "
1029 "credentials for %s.\n",
1030 cmd_entry->table->name);
1031 TALLOC_FREE(cmd_entry->rpc_pipe);
1032 TALLOC_FREE(mem_ctx);
1033 return ntresult;
1036 ntresult = rpccli_setup_netlogon_creds(
1037 cli,
1038 NCACN_NP,
1039 remote_name,
1040 remote_sockaddr,
1041 rpcclient_netlogon_creds,
1042 false, /* force_reauth */
1043 trust_creds);
1044 TALLOC_FREE(trust_creds);
1045 if (!NT_STATUS_IS_OK(ntresult)) {
1046 DBG_ERR("Could not initialise "
1047 "credentials for %s.\n",
1048 cmd_entry->table->name);
1049 TALLOC_FREE(cmd_entry->rpc_pipe);
1050 TALLOC_FREE(rpcclient_netlogon_creds);
1051 TALLOC_FREE(mem_ctx);
1052 return ntresult;
1058 /* Set timeout for new connections */
1059 if (cmd_entry->rpc_pipe) {
1060 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
1063 /* Run command */
1065 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
1066 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1067 if (!NT_STATUS_IS_OK(ntresult)) {
1068 printf("result was %s\n", nt_errstr(ntresult));
1070 } else if (cmd_entry->returntype == RPC_RTYPE_BINDING) {
1071 ntresult = cmd_entry->bfn(binding, mem_ctx, argc, argv);
1072 if (!NT_STATUS_IS_OK(ntresult)) {
1073 printf("result was %s\n", nt_errstr(ntresult));
1075 } else {
1076 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1077 /* print out the DOS error */
1078 if (!W_ERROR_IS_OK(wresult)) {
1079 printf( "result was %s\n", win_errstr(wresult));
1081 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
1084 /* Cleanup */
1086 talloc_free(mem_ctx);
1088 return ntresult;
1093 * Process a command entered at the prompt or as part of -c
1095 * @returns The NTSTATUS from running the command.
1097 static NTSTATUS process_cmd(struct cli_credentials *creds,
1098 struct cli_state *cli,
1099 struct dcerpc_binding *binding,
1100 char *cmd)
1102 struct cmd_list *temp_list;
1103 NTSTATUS result = NT_STATUS_OK;
1104 int ret;
1105 int argc;
1106 const char **argv = NULL;
1108 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
1109 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
1110 return NT_STATUS_UNSUCCESSFUL;
1114 /* Walk through a dlist of arrays of commands. */
1115 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
1116 struct cmd_set *set = temp_list->cmd_set;
1118 while (set->name != NULL) {
1119 if (!strequal(argv[0], set->name)) {
1120 set += 1;
1121 continue;
1124 if (((set->returntype == RPC_RTYPE_NTSTATUS) &&
1125 (set->ntfn == NULL)) ||
1126 ((set->returntype == RPC_RTYPE_WERROR) &&
1127 (set->wfn == NULL)) ||
1128 ((set->returntype == RPC_RTYPE_BINDING) &&
1129 (set->bfn == NULL))) {
1130 fprintf (stderr, "Invalid command\n");
1131 goto out_free;
1134 result = do_cmd(
1135 cli, creds, set, binding, argc, argv);
1136 goto out_free;
1140 if (argv[0]) {
1141 printf("command not found: %s\n", argv[0]);
1144 out_free:
1145 /* moved to do_cmd()
1146 if (!NT_STATUS_IS_OK(result)) {
1147 printf("result was %s\n", nt_errstr(result));
1151 /* NOTE: popt allocates the whole argv, including the
1152 * strings, as a single block. So a single free is
1153 * enough to release it -- we don't free the
1154 * individual strings. rtfm. */
1155 free(argv);
1157 return result;
1161 /* Main function */
1163 int main(int argc, char *argv[])
1165 const char **const_argv = discard_const_p(const char *, argv);
1166 int opt;
1167 static char *cmdstr = NULL;
1168 const char *server;
1169 struct cli_state *cli = NULL;
1170 static char *opt_ipaddr=NULL;
1171 struct cmd_set **cmd_set;
1172 struct sockaddr_storage server_ss;
1173 NTSTATUS nt_status;
1174 static int opt_port = 0;
1175 int result = 0;
1176 TALLOC_CTX *frame = talloc_stackframe();
1177 uint32_t flags = CLI_FULL_CONNECTION_IPC;
1178 struct dcerpc_binding *binding = NULL;
1179 enum dcerpc_transport_t transport;
1180 const char *binding_string = NULL;
1181 const char *host;
1182 struct cli_credentials *creds = NULL;
1183 struct loadparm_context *lp_ctx = NULL;
1184 bool ok;
1186 /* make sure the vars that get altered (4th field) are in
1187 a fixed location or certain compilers complain */
1188 poptContext pc;
1189 struct poptOption long_options[] = {
1190 POPT_AUTOHELP
1191 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
1192 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
1193 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
1194 POPT_COMMON_SAMBA
1195 POPT_COMMON_CONNECTION
1196 POPT_COMMON_CREDENTIALS
1197 POPT_LEGACY_S3
1198 POPT_COMMON_VERSION
1199 POPT_TABLEEND
1202 smb_init_locale();
1204 zero_sockaddr(&server_ss);
1206 setlinebuf(stdout);
1208 ok = samba_cmdline_init(frame,
1209 SAMBA_CMDLINE_CONFIG_CLIENT,
1210 false /* require_smbconf */);
1211 if (!ok) {
1212 DBG_ERR("Failed to init cmdline parser!\n");
1214 lp_ctx = samba_cmdline_get_lp_ctx();
1215 lpcfg_set_cmdline(lp_ctx, "log level", "0");
1217 /* Parse options */
1218 pc = samba_popt_get_context(getprogname(),
1219 argc,
1220 const_argv,
1221 long_options,
1223 if (pc == NULL) {
1224 DBG_ERR("Failed to setup popt context!\n");
1225 exit(1);
1228 poptSetOtherOptionHelp(pc, "[OPTION...] BINDING-STRING|HOST\nOptions:");
1230 if (argc == 1) {
1231 poptPrintHelp(pc, stderr, 0);
1232 goto done;
1235 while((opt = poptGetNextOpt(pc)) != -1) {
1236 switch (opt) {
1238 case 'I':
1239 if (!interpret_string_addr(&server_ss,
1240 opt_ipaddr,
1241 AI_NUMERICHOST)) {
1242 fprintf(stderr, "%s not a valid IP address\n",
1243 opt_ipaddr);
1244 result = 1;
1245 goto done;
1247 break;
1248 case POPT_ERROR_BADOPT:
1249 fprintf(stderr, "\nInvalid option %s: %s\n\n",
1250 poptBadOption(pc, 0), poptStrerror(opt));
1251 poptPrintUsage(pc, stderr, 0);
1252 exit(1);
1256 /* Get server as remaining unparsed argument. Print usage if more
1257 than one unparsed argument is present. */
1259 server = talloc_strdup(frame, poptGetArg(pc));
1261 if (!server || poptGetArg(pc)) {
1262 poptPrintHelp(pc, stderr, 0);
1263 result = 1;
1264 goto done;
1267 poptFreeContext(pc);
1268 samba_cmdline_burn(argc, argv);
1270 rpcclient_msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
1271 creds = samba_cmdline_get_creds();
1274 * Get password
1275 * from stdin if necessary
1278 if ((server[0] == '/' && server[1] == '/') ||
1279 (server[0] == '\\' && server[1] == '\\')) {
1280 server += 2;
1283 nt_status = dcerpc_parse_binding(frame, server, &binding);
1285 if (!NT_STATUS_IS_OK(nt_status)) {
1287 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1288 strip_hostname(server));
1289 if (!binding_string) {
1290 result = 1;
1291 goto done;
1294 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1295 if (!NT_STATUS_IS_OK(nt_status)) {
1296 result = -1;
1297 goto done;
1301 transport = dcerpc_binding_get_transport(binding);
1303 if (transport == NCA_UNKNOWN) {
1304 transport = NCACN_NP;
1305 nt_status = dcerpc_binding_set_transport(binding, transport);
1306 if (!NT_STATUS_IS_OK(nt_status)) {
1307 result = -1;
1308 goto done;
1312 host = dcerpc_binding_get_string_option(binding, "host");
1314 rpcclient_netlogon_domain = cli_credentials_get_domain(creds);
1315 if (rpcclient_netlogon_domain == NULL ||
1316 rpcclient_netlogon_domain[0] == '\0')
1318 rpcclient_netlogon_domain = lp_workgroup();
1321 if (transport == NCACN_NP) {
1322 nt_status = cli_full_connection_creds(frame,
1323 &cli,
1324 lp_netbios_name(),
1325 host,
1326 opt_ipaddr ? &server_ss
1327 : NULL,
1328 opt_port,
1329 "IPC$",
1330 "IPC",
1331 creds,
1332 flags);
1334 if (!NT_STATUS_IS_OK(nt_status)) {
1335 DEBUG(0, ("Cannot connect to server. Error was %s\n",
1336 nt_errstr(nt_status)));
1337 result = 1;
1338 goto done;
1341 /* Load command lists */
1342 cli_set_timeout(cli, timeout);
1345 #if 0 /* COMMENT OUT FOR TESTING */
1346 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1347 #endif
1349 cmd_set = rpcclient_command_list;
1351 while(*cmd_set) {
1352 add_command_set(*cmd_set);
1353 add_command_set(separator_command);
1354 cmd_set++;
1357 /* Do anything specified with -c */
1358 if (cmdstr && cmdstr[0]) {
1359 char *cmd;
1360 char *p = cmdstr;
1362 result = 0;
1364 while((cmd=next_command(&p)) != NULL) {
1365 NTSTATUS cmd_result = process_cmd(creds,
1366 cli,
1367 binding,
1368 cmd);
1369 SAFE_FREE(cmd);
1370 result = NT_STATUS_IS_ERR(cmd_result);
1373 goto done;
1376 /* Loop around accepting commands */
1378 while(1) {
1379 char *line = NULL;
1381 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1383 if (line == NULL) {
1384 printf("\n");
1385 break;
1388 if (line[0] != '\n')
1389 process_cmd(creds,
1390 cli,
1391 binding,
1392 line);
1393 SAFE_FREE(line);
1396 done:
1397 if (cli != NULL) {
1398 cli_shutdown(cli);
1400 netlogon_creds_cli_close_global_db();
1401 TALLOC_FREE(rpcclient_msg_ctx);
1402 TALLOC_FREE(frame);
1403 return result;