s3:client: Fix old-style function definition
[Samba.git] / source3 / rpcclient / rpcclient.c
blob27fe5d705c657c1e40b93e1373f46448bfd25da3
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"
41 enum pipe_auth_type_spnego {
42 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
43 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
44 PIPE_AUTH_TYPE_SPNEGO_KRB5
47 static unsigned int timeout = 10000;
49 struct messaging_context *rpcclient_msg_ctx;
50 struct netlogon_creds_cli_context *rpcclient_netlogon_creds;
51 static const char *rpcclient_netlogon_domain;
53 /* List to hold groups of commands.
55 * Commands are defined in a list of arrays: arrays are easy to
56 * statically declare, and lists are easier to dynamically extend.
59 static struct cmd_list {
60 struct cmd_list *prev, *next;
61 struct cmd_set *cmd_set;
62 } *cmd_list;
64 /****************************************************************************
65 handle completion of commands for readline
66 ****************************************************************************/
67 static char **completion_fn(const char *text, int start, int end)
69 #define MAX_COMPLETIONS 1000
70 char **matches;
71 size_t i, count=0;
72 struct cmd_list *commands = cmd_list;
74 #if 0 /* JERRY */
75 /* FIXME!!! -- what to do when completing argument? */
76 /* for words not at the start of the line fallback
77 to filename completion */
78 if (start)
79 return NULL;
80 #endif
82 /* make sure we have a list of valid commands */
83 if (!commands) {
84 return NULL;
87 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
88 if (!matches) {
89 return NULL;
92 matches[count++] = SMB_STRDUP(text);
93 if (!matches[0]) {
94 SAFE_FREE(matches);
95 return NULL;
98 while (commands && count < MAX_COMPLETIONS-1) {
99 if (!commands->cmd_set) {
100 break;
103 for (i=0; commands->cmd_set[i].name; i++) {
104 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
105 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
106 commands->cmd_set[i].ntfn ) ||
107 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
108 commands->cmd_set[i].wfn))) {
109 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
110 if (!matches[count]) {
111 for (i = 0; i < count; i++) {
112 SAFE_FREE(matches[count]);
114 SAFE_FREE(matches);
115 return NULL;
117 count++;
120 commands = commands->next;
123 if (count == 2) {
124 SAFE_FREE(matches[0]);
125 matches[0] = SMB_STRDUP(matches[1]);
127 matches[count] = NULL;
128 return matches;
131 static char *next_command (char **cmdstr)
133 char *command;
134 char *p;
136 if (!cmdstr || !(*cmdstr))
137 return NULL;
139 p = strchr_m(*cmdstr, ';');
140 if (p)
141 *p = '\0';
142 command = SMB_STRDUP(*cmdstr);
143 if (p)
144 *cmdstr = p + 1;
145 else
146 *cmdstr = NULL;
148 return command;
151 static void binding_get_auth_info(
152 const struct dcerpc_binding *b,
153 enum dcerpc_AuthType *_auth_type,
154 enum dcerpc_AuthLevel *_auth_level,
155 enum credentials_use_kerberos *_krb5_state)
157 uint32_t bflags = dcerpc_binding_get_flags(b);
158 enum dcerpc_AuthLevel auth_level = DCERPC_AUTH_LEVEL_NONE;
159 enum dcerpc_AuthType auth_type = DCERPC_AUTH_TYPE_NONE;
160 enum credentials_use_kerberos krb5_state = CRED_USE_KERBEROS_DESIRED;
162 if (_krb5_state != NULL) {
163 krb5_state = *_krb5_state;
166 if (bflags & DCERPC_CONNECT) {
167 auth_level = DCERPC_AUTH_LEVEL_CONNECT;
169 if (bflags & DCERPC_PACKET) {
170 auth_level = DCERPC_AUTH_LEVEL_PACKET;
172 if (bflags & DCERPC_SIGN) {
173 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
175 if (bflags & DCERPC_SEAL) {
176 auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
179 if (bflags & DCERPC_SCHANNEL) {
180 auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
183 if ((auth_level != DCERPC_AUTH_LEVEL_NONE) &&
184 (auth_type == DCERPC_AUTH_TYPE_NONE)) {
185 auth_type = (krb5_state == CRED_USE_KERBEROS_REQUIRED) ?
186 DCERPC_AUTH_TYPE_KRB5 : DCERPC_AUTH_TYPE_NTLMSSP;
189 if (bflags & DCERPC_AUTH_SPNEGO) {
190 auth_type = DCERPC_AUTH_TYPE_SPNEGO;
192 if (bflags & DCERPC_AUTH_NTLM) {
193 krb5_state = CRED_USE_KERBEROS_DISABLED;
195 if (bflags & DCERPC_AUTH_KRB5) {
196 krb5_state = CRED_USE_KERBEROS_REQUIRED;
200 if (auth_type != DCERPC_AUTH_TYPE_NONE) {
201 /* If nothing is requested then default to integrity */
202 if (auth_level == DCERPC_AUTH_LEVEL_NONE) {
203 auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
207 if (_auth_type != NULL) {
208 *_auth_type = auth_type;
210 if (_auth_level != NULL) {
211 *_auth_level = auth_level;
213 if (_krb5_state != NULL) {
214 *_krb5_state = krb5_state;
218 /* List the available commands on a given pipe */
220 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
221 int argc, const char **argv)
223 struct cmd_list *tmp;
224 struct cmd_set *tmp_set;
225 int i;
227 /* Usage */
229 if (argc != 2) {
230 printf("Usage: %s <pipe>\n", argv[0]);
231 return NT_STATUS_OK;
234 /* Help on one command */
236 for (tmp = cmd_list; tmp; tmp = tmp->next)
238 tmp_set = tmp->cmd_set;
240 if (!strcasecmp_m(argv[1], tmp_set->name))
242 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
244 i = 0;
245 tmp_set++;
246 while(tmp_set->name) {
247 printf("%30s", tmp_set->name);
248 tmp_set++;
249 i++;
250 if (i%3 == 0)
251 printf("\n");
254 /* drop out of the loop */
255 break;
258 printf("\n\n");
260 return NT_STATUS_OK;
263 /* Display help on commands */
265 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
266 int argc, const char **argv)
268 struct cmd_list *tmp;
269 struct cmd_set *tmp_set;
271 /* Usage */
273 if (argc > 2) {
274 printf("Usage: %s [command]\n", argv[0]);
275 return NT_STATUS_OK;
278 /* Help on one command */
280 if (argc == 2) {
281 for (tmp = cmd_list; tmp; tmp = tmp->next) {
283 tmp_set = tmp->cmd_set;
285 while(tmp_set->name) {
286 if (strequal(argv[1], tmp_set->name)) {
287 if (tmp_set->usage &&
288 tmp_set->usage[0])
289 printf("%s\n", tmp_set->usage);
290 else
291 printf("No help for %s\n", tmp_set->name);
293 return NT_STATUS_OK;
296 tmp_set++;
300 printf("No such command: %s\n", argv[1]);
301 return NT_STATUS_OK;
304 /* List all commands */
306 for (tmp = cmd_list; tmp; tmp = tmp->next) {
308 tmp_set = tmp->cmd_set;
310 while(tmp_set->name) {
312 printf("%15s\t\t%s\n", tmp_set->name,
313 tmp_set->description ? tmp_set->description:
314 "");
316 tmp_set++;
320 return NT_STATUS_OK;
323 /* Change the debug level */
325 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
326 int argc, const char **argv)
328 if (argc > 2) {
329 printf("Usage: %s [debuglevel]\n", argv[0]);
330 return NT_STATUS_OK;
333 if (argc == 2) {
334 lp_set_cmdline("log level", argv[1]);
337 printf("debuglevel is %d\n", DEBUGLEVEL);
339 return NT_STATUS_OK;
342 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
343 int argc, const char **argv)
345 exit(0);
346 return NT_STATUS_OK; /* NOTREACHED */
349 static NTSTATUS cmd_set_ss_level(struct dcerpc_binding *binding)
351 struct cmd_list *tmp;
352 enum dcerpc_AuthType auth_type;
353 enum dcerpc_AuthLevel auth_level;
355 /* Close any existing connections not at this level. */
357 binding_get_auth_info(binding, &auth_type, &auth_level, NULL);
359 for (tmp = cmd_list; tmp; tmp = tmp->next) {
360 struct cmd_set *tmp_set;
362 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
363 if (tmp_set->rpc_pipe == NULL) {
364 continue;
367 if ((tmp_set->rpc_pipe->auth->auth_type
368 != auth_type)
369 || (tmp_set->rpc_pipe->auth->auth_level
370 != auth_level)) {
371 TALLOC_FREE(tmp_set->rpc_pipe);
372 tmp_set->rpc_pipe = NULL;
376 return NT_STATUS_OK;
379 static NTSTATUS cmd_set_transport(struct dcerpc_binding *b)
381 enum dcerpc_transport_t t = dcerpc_binding_get_transport(b);
382 struct cmd_list *tmp;
384 /* Close any existing connections not at this level. */
386 for (tmp = cmd_list; tmp; tmp = tmp->next) {
387 struct cmd_set *tmp_set;
389 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
390 if (tmp_set->rpc_pipe == NULL) {
391 continue;
394 if (tmp_set->rpc_pipe->transport->transport != t) {
395 TALLOC_FREE(tmp_set->rpc_pipe);
396 tmp_set->rpc_pipe = NULL;
400 return NT_STATUS_OK;
403 static NTSTATUS binding_reset_auth(struct dcerpc_binding *b)
405 NTSTATUS status = dcerpc_binding_set_flags(
408 DCERPC_PACKET|
409 DCERPC_CONNECT|
410 DCERPC_SIGN|
411 DCERPC_SEAL|
412 DCERPC_SCHANNEL|
413 DCERPC_AUTH_SPNEGO|
414 DCERPC_AUTH_KRB5|
415 DCERPC_AUTH_NTLM);
416 return status;
419 static NTSTATUS binding_set_auth(
420 struct dcerpc_binding *b, const char *level, const char *type)
422 NTSTATUS status;
424 status = binding_reset_auth(b);
425 if (!NT_STATUS_IS_OK(status)) {
426 return status;
429 if (level != NULL) {
430 status = dcerpc_binding_set_string_option(b, level, level);
431 if (!NT_STATUS_IS_OK(status)) {
432 return status;
436 if (strequal(type, "SPNEGO")) {
437 status = dcerpc_binding_set_string_option(
438 b, "spnego", "spnego");
439 return status;
441 if (strequal(type, "NTLMSSP")) {
442 status = dcerpc_binding_set_string_option(b, "ntlm", "ntlm");
443 return status;
445 if (strequal(type, "NTLMSSP_SPNEGO")) {
446 status = dcerpc_binding_set_string_option(
447 b, "spnego", "spnego");
448 if (!NT_STATUS_IS_OK(status)) {
449 return status;
451 status = dcerpc_binding_set_string_option(b, "ntlm", "ntlm");
452 return status;
454 if (strequal(type, "KRB5")) {
455 status = dcerpc_binding_set_string_option(b, "krb5", "krb5");
456 return status;
458 if (strequal(type, "KRB5_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, "krb5", "krb5");
465 return status;
467 if (strequal(type, "SCHANNEL")) {
468 status = dcerpc_binding_set_string_option(
469 b, "schannel", "schannel");
470 return status;
473 return NT_STATUS_INVALID_PARAMETER;
476 static NTSTATUS cmd_set_auth(
477 struct dcerpc_binding *binding,
478 const char *level,
479 const char *display,
480 int argc,
481 const char **argv)
483 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
484 const char *type = "NTLMSSP";
485 NTSTATUS status;
487 if (argc > 2) {
488 printf("Usage: %s %s\n", argv[0], p);
489 return NT_STATUS_OK;
492 if (argc == 2) {
493 type = argv[1];
496 status = binding_set_auth(binding, level, type);
497 if (!NT_STATUS_IS_OK(status)) {
498 printf("Usage: %s %s\n", argv[0], p);
499 return status;
502 d_printf("Setting %s - %s: %s\n", type, display, nt_errstr(status));
504 status = cmd_set_ss_level(binding);
505 return status;
508 static NTSTATUS cmd_sign(
509 struct dcerpc_binding *binding,
510 TALLOC_CTX *mem_ctx,
511 int argc,
512 const char **argv)
514 NTSTATUS status = cmd_set_auth(binding, "sign", "sign", argc, argv);
515 return status;
518 static NTSTATUS cmd_seal(
519 struct dcerpc_binding *binding,
520 TALLOC_CTX *mem_ctx,
521 int argc,
522 const char **argv)
524 NTSTATUS status = cmd_set_auth(
525 binding, "seal", "sign and seal", argc, argv);
526 return status;
529 static NTSTATUS cmd_packet(
530 struct dcerpc_binding *binding,
531 TALLOC_CTX *mem_ctx,
532 int argc,
533 const char **argv)
535 NTSTATUS status = cmd_set_auth(
536 binding, "packet", "packet", argc, argv);
537 return status;
540 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
541 int argc, const char **argv)
543 if (argc > 2) {
544 printf("Usage: %s timeout\n", argv[0]);
545 return NT_STATUS_OK;
548 if (argc == 2) {
549 timeout = atoi(argv[1]);
552 printf("timeout is %d\n", timeout);
554 return NT_STATUS_OK;
558 static NTSTATUS cmd_none(
559 struct dcerpc_binding *binding,
560 TALLOC_CTX *mem_ctx,
561 int argc,
562 const char **argv)
564 NTSTATUS status = binding_reset_auth(binding);
565 if (!NT_STATUS_IS_OK(status)) {
566 return status;
568 status = cmd_set_ss_level(binding);
569 return status;
572 static NTSTATUS cmd_schannel(
573 struct dcerpc_binding *binding,
574 TALLOC_CTX *mem_ctx,
575 int argc,
576 const char **_argv)
578 const char *argv[] = { "schannel", "SCHANNEL" };
579 NTSTATUS status = cmd_set_auth(
580 binding, "seal", "sign and seal", 2, argv);
581 return status;
584 static NTSTATUS cmd_schannel_sign(
585 struct dcerpc_binding *binding,
586 TALLOC_CTX *mem_ctx,
587 int argc,
588 const char **_argv)
590 const char *argv[] = { "schannel_sign", "SCHANNEL" };
591 NTSTATUS status = cmd_set_auth(binding, "sign", "sign only", 2, argv);
592 return status;
595 static NTSTATUS cmd_choose_transport(
596 struct dcerpc_binding *binding,
597 TALLOC_CTX *mem_ctx,
598 int argc,
599 const char **argv)
601 NTSTATUS status;
602 enum dcerpc_transport_t transport;
604 if (argc != 2) {
605 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
606 return NT_STATUS_OK;
609 transport = dcerpc_transport_by_name(argv[1]);
610 if (transport == NCA_UNKNOWN) {
611 printf("transport type %s unknown\n", argv[1]);
612 return NT_STATUS_NOT_SUPPORTED;
614 if (!((transport == NCACN_IP_TCP) ||
615 (transport == NCACN_NP) ||
616 (transport == NCALRPC))) {
617 printf("transport %s not supported\n", argv[1]);
618 return NT_STATUS_NOT_SUPPORTED;
621 status = dcerpc_binding_set_transport(binding, transport);
622 if (!NT_STATUS_IS_OK(status)) {
623 return status;
626 status = cmd_set_transport(binding);
627 if (!NT_STATUS_IS_OK(status)) {
628 return status;
631 printf("default transport is now: %s\n", argv[1]);
633 return NT_STATUS_OK;
636 /* Built in rpcclient commands */
638 static struct cmd_set rpcclient_commands[] = {
641 .name = "GENERAL OPTIONS",
645 .name = "help",
646 .returntype = RPC_RTYPE_NTSTATUS,
647 .ntfn = cmd_help,
648 .description = "Get help on commands",
649 .usage = "[command]",
652 .name = "?",
653 .returntype = RPC_RTYPE_NTSTATUS,
654 .ntfn = cmd_help,
655 .description = "Get help on commands",
656 .usage = "[command]",
659 .name = "debuglevel",
660 .returntype = RPC_RTYPE_NTSTATUS,
661 .ntfn = cmd_debuglevel,
662 .description = "Set debug level",
663 .usage = "level",
666 .name = "debug",
667 .returntype = RPC_RTYPE_NTSTATUS,
668 .ntfn = cmd_debuglevel,
669 .description = "Set debug level",
670 .usage = "level",
673 .name = "list",
674 .returntype = RPC_RTYPE_NTSTATUS,
675 .ntfn = cmd_listcommands,
676 .description = "List available commands on <pipe>",
677 .usage = "pipe",
680 .name = "exit",
681 .returntype = RPC_RTYPE_NTSTATUS,
682 .ntfn = cmd_quit,
683 .description = "Exit program",
684 .usage = "",
687 .name = "quit",
688 .returntype = RPC_RTYPE_NTSTATUS,
689 .ntfn = cmd_quit,
690 .description = "Exit program",
691 .usage = "",
694 .name = "sign",
695 .returntype = RPC_RTYPE_BINDING,
696 .bfn = cmd_sign,
697 .description = "Force RPC pipe connections to be signed",
698 .usage = "",
701 .name = "seal",
702 .returntype = RPC_RTYPE_BINDING,
703 .bfn = cmd_seal,
704 .description = "Force RPC pipe connections to be sealed",
705 .usage = "",
708 .name = "packet",
709 .returntype = RPC_RTYPE_BINDING,
710 .bfn = cmd_packet,
711 .description = "Force RPC pipe connections with packet authentication level",
712 .usage = "",
715 .name = "schannel",
716 .returntype = RPC_RTYPE_BINDING,
717 .bfn = cmd_schannel,
718 .description = "Force RPC pipe connections to be sealed with 'schannel'. "
719 "Assumes valid machine account to this domain controller.",
720 .usage = "",
723 .name = "schannelsign",
724 .returntype = RPC_RTYPE_BINDING,
725 .bfn = cmd_schannel_sign,
726 .description = "Force RPC pipe connections to be signed (not sealed) with "
727 "'schannel'. Assumes valid machine account to this domain "
728 "controller.",
729 .usage = "",
732 .name = "timeout",
733 .returntype = RPC_RTYPE_NTSTATUS,
734 .ntfn = cmd_timeout,
735 .description = "Set timeout (in milliseconds) for RPC operations",
736 .usage = "",
739 .name = "transport",
740 .returntype = RPC_RTYPE_BINDING,
741 .bfn = cmd_choose_transport,
742 .description = "Choose ncacn transport for RPC operations",
743 .usage = "",
746 .name = "none",
747 .returntype = RPC_RTYPE_BINDING,
748 .bfn = cmd_none,
749 .description = "Force RPC pipe connections to have no special properties",
750 .usage = "",
753 { .name = NULL, },
756 static struct cmd_set separator_command[] = {
758 .name = "---------------",
759 .returntype = MAX_RPC_RETURN_TYPE,
760 .description = "----------------------"
762 { .name = NULL, },
766 /* Various pipe commands */
768 extern struct cmd_set lsarpc_commands[];
769 extern struct cmd_set samr_commands[];
770 extern struct cmd_set spoolss_commands[];
771 extern struct cmd_set iremotewinspool_commands[];
772 extern struct cmd_set netlogon_commands[];
773 extern struct cmd_set srvsvc_commands[];
774 extern struct cmd_set dfs_commands[];
775 extern struct cmd_set ds_commands[];
776 extern struct cmd_set echo_commands[];
777 extern struct cmd_set epmapper_commands[];
778 extern struct cmd_set shutdown_commands[];
779 extern struct cmd_set wkssvc_commands[];
780 extern struct cmd_set ntsvcs_commands[];
781 extern struct cmd_set drsuapi_commands[];
782 extern struct cmd_set eventlog_commands[];
783 extern struct cmd_set winreg_commands[];
784 extern struct cmd_set fss_commands[];
785 extern struct cmd_set witness_commands[];
786 extern struct cmd_set clusapi_commands[];
787 extern struct cmd_set spotlight_commands[];
788 extern struct cmd_set unixinfo_commands[];
790 static struct cmd_set *rpcclient_command_list[] = {
791 rpcclient_commands,
792 lsarpc_commands,
793 ds_commands,
794 samr_commands,
795 spoolss_commands,
796 iremotewinspool_commands,
797 netlogon_commands,
798 srvsvc_commands,
799 dfs_commands,
800 echo_commands,
801 epmapper_commands,
802 shutdown_commands,
803 wkssvc_commands,
804 ntsvcs_commands,
805 drsuapi_commands,
806 eventlog_commands,
807 winreg_commands,
808 fss_commands,
809 witness_commands,
810 clusapi_commands,
811 spotlight_commands,
812 unixinfo_commands,
813 NULL
816 static void add_command_set(struct cmd_set *cmd_set)
818 struct cmd_list *entry;
820 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
821 DEBUG(0, ("out of memory\n"));
822 return;
825 ZERO_STRUCTP(entry);
827 entry->cmd_set = cmd_set;
828 DLIST_ADD(cmd_list, entry);
831 static NTSTATUS rpccli_ncalrpc_connect(
832 const struct ndr_interface_table *iface,
833 TALLOC_CTX *mem_ctx,
834 struct rpc_pipe_client **prpccli)
836 struct rpc_pipe_client *rpccli = NULL;
837 struct pipe_auth_data *auth = NULL;
838 NTSTATUS status;
840 status = rpc_pipe_open_ncalrpc(mem_ctx, iface, &rpccli);
841 if (!NT_STATUS_IS_OK(status)) {
842 DBG_DEBUG("rpc_pipe_open_ncalrpc failed: %s\n",
843 nt_errstr(status));
844 goto fail;
847 status = rpccli_ncalrpc_bind_data(rpccli, &auth);
848 if (!NT_STATUS_IS_OK(status)) {
849 DBG_DEBUG("rpccli_ncalrpc_bind_data failed: %s\n",
850 nt_errstr(status));
851 goto fail;
854 status = rpc_pipe_bind(rpccli, auth);
855 if (!NT_STATUS_IS_OK(status)) {
856 DBG_DEBUG("rpc_pipe_bind failed: %s\n", nt_errstr(status));
857 goto fail;
860 *prpccli = rpccli;
861 return NT_STATUS_OK;
862 fail:
863 TALLOC_FREE(rpccli);
864 return status;
867 * Call an rpcclient function, passing an argv array.
869 * @param cmd Command to run, as a single string.
871 static NTSTATUS do_cmd(struct cli_state *cli,
872 struct cli_credentials *creds,
873 struct cmd_set *cmd_entry,
874 struct dcerpc_binding *binding,
875 int argc, const char **argv)
877 NTSTATUS ntresult;
878 WERROR wresult;
879 enum dcerpc_transport_t transport;
881 TALLOC_CTX *mem_ctx = talloc_stackframe();
882 const char *remote_name = NULL;
883 const struct sockaddr_storage *remote_sockaddr = NULL;
884 struct sockaddr_storage remote_ss = {
885 .ss_family = AF_UNSPEC,
888 transport = dcerpc_binding_get_transport(binding);
890 if (cli != NULL) {
891 remote_name = smbXcli_conn_remote_name(cli->conn);
892 remote_sockaddr = smbXcli_conn_remote_sockaddr(cli->conn);
893 } else {
894 const char *remote_host =
895 dcerpc_binding_get_string_option(binding, "host");
896 remote_name = dcerpc_binding_get_string_option(
897 binding, "target_hostname");
899 if (remote_host != NULL) {
900 bool ok = interpret_string_addr(
901 &remote_ss, remote_host, 0);
902 if (ok) {
903 remote_sockaddr = &remote_ss;
908 /* Open pipe */
910 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
911 if (transport == NCALRPC) {
912 ntresult = rpccli_ncalrpc_connect(
913 cmd_entry->table, cli, &cmd_entry->rpc_pipe);
914 if (!NT_STATUS_IS_OK(ntresult)) {
915 TALLOC_FREE(mem_ctx);
916 return ntresult;
918 } else {
919 enum dcerpc_AuthType auth_type;
920 enum dcerpc_AuthLevel auth_level;
921 enum credentials_use_kerberos krb5_state =
922 cli_credentials_get_kerberos_state(creds);
924 binding_get_auth_info(
925 binding, &auth_type, &auth_level, &krb5_state);
927 switch (auth_type) {
928 case DCERPC_AUTH_TYPE_NONE:
929 ntresult = cli_rpc_pipe_open_noauth_transport(
930 cli, transport,
931 cmd_entry->table,
932 remote_name,
933 remote_sockaddr,
934 &cmd_entry->rpc_pipe);
935 break;
936 case DCERPC_AUTH_TYPE_SPNEGO:
937 case DCERPC_AUTH_TYPE_NTLMSSP:
938 case DCERPC_AUTH_TYPE_KRB5:
939 cli_credentials_set_kerberos_state(creds,
940 krb5_state,
941 CRED_SPECIFIED);
943 ntresult = cli_rpc_pipe_open_with_creds(
944 cli, cmd_entry->table,
945 transport,
946 auth_type,
947 auth_level,
948 remote_name,
949 remote_sockaddr,
950 creds,
951 &cmd_entry->rpc_pipe);
952 break;
953 case DCERPC_AUTH_TYPE_SCHANNEL:
954 TALLOC_FREE(rpcclient_netlogon_creds);
955 ntresult = cli_rpc_pipe_open_schannel(
956 cli, rpcclient_msg_ctx,
957 cmd_entry->table,
958 transport,
959 rpcclient_netlogon_domain,
960 remote_name,
961 remote_sockaddr,
962 &cmd_entry->rpc_pipe,
963 rpcclient_msg_ctx,
964 &rpcclient_netlogon_creds);
965 break;
966 default:
967 DEBUG(0, ("Could not initialise %s. Invalid "
968 "auth type %u\n",
969 cmd_entry->table->name,
970 auth_type ));
971 talloc_free(mem_ctx);
972 return NT_STATUS_UNSUCCESSFUL;
974 if (!NT_STATUS_IS_OK(ntresult)) {
975 DBG_ERR("Could not initialise %s. "
976 "Error was %s\n",
977 cmd_entry->table->name,
978 nt_errstr(ntresult));
979 talloc_free(mem_ctx);
980 return ntresult;
983 if (rpcclient_netlogon_creds == NULL &&
984 cmd_entry->use_netlogon_creds) {
985 const char *dc_name =
986 cmd_entry->rpc_pipe->desthost;
987 const char *domain = rpcclient_netlogon_domain;
988 struct cli_credentials *trust_creds = NULL;
990 ntresult = pdb_get_trust_credentials(
991 domain,
992 NULL,
993 mem_ctx,
994 &trust_creds);
995 if (!NT_STATUS_IS_OK(ntresult)) {
996 DBG_ERR("Failed to fetch trust "
997 "credentials for "
998 "%s to connect to %s: %s\n",
999 domain,
1000 cmd_entry->table->name,
1001 nt_errstr(ntresult));
1002 TALLOC_FREE(cmd_entry->rpc_pipe);
1003 talloc_free(mem_ctx);
1004 return ntresult;
1007 ntresult = rpccli_create_netlogon_creds_ctx(
1008 trust_creds,
1009 dc_name,
1010 rpcclient_msg_ctx,
1011 rpcclient_msg_ctx,
1012 &rpcclient_netlogon_creds);
1013 if (!NT_STATUS_IS_OK(ntresult)) {
1014 DBG_ERR("Could not initialise "
1015 "credentials for %s.\n",
1016 cmd_entry->table->name);
1017 TALLOC_FREE(cmd_entry->rpc_pipe);
1018 TALLOC_FREE(mem_ctx);
1019 return ntresult;
1022 ntresult = rpccli_setup_netlogon_creds(
1023 cli,
1024 NCACN_NP,
1025 rpcclient_netlogon_creds,
1026 false, /* force_reauth */
1027 trust_creds);
1028 TALLOC_FREE(trust_creds);
1029 if (!NT_STATUS_IS_OK(ntresult)) {
1030 DBG_ERR("Could not initialise "
1031 "credentials for %s.\n",
1032 cmd_entry->table->name);
1033 TALLOC_FREE(cmd_entry->rpc_pipe);
1034 TALLOC_FREE(rpcclient_netlogon_creds);
1035 TALLOC_FREE(mem_ctx);
1036 return ntresult;
1042 /* Set timeout for new connections */
1043 if (cmd_entry->rpc_pipe) {
1044 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
1047 /* Run command */
1049 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
1050 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1051 if (!NT_STATUS_IS_OK(ntresult)) {
1052 printf("result was %s\n", nt_errstr(ntresult));
1054 } else if (cmd_entry->returntype == RPC_RTYPE_BINDING) {
1055 ntresult = cmd_entry->bfn(binding, mem_ctx, argc, argv);
1056 if (!NT_STATUS_IS_OK(ntresult)) {
1057 printf("result was %s\n", nt_errstr(ntresult));
1059 } else {
1060 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1061 /* print out the DOS error */
1062 if (!W_ERROR_IS_OK(wresult)) {
1063 printf( "result was %s\n", win_errstr(wresult));
1065 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
1068 /* Cleanup */
1070 talloc_free(mem_ctx);
1072 return ntresult;
1077 * Process a command entered at the prompt or as part of -c
1079 * @returns The NTSTATUS from running the command.
1081 static NTSTATUS process_cmd(struct cli_credentials *creds,
1082 struct cli_state *cli,
1083 struct dcerpc_binding *binding,
1084 char *cmd)
1086 struct cmd_list *temp_list;
1087 NTSTATUS result = NT_STATUS_OK;
1088 int ret;
1089 int argc;
1090 const char **argv = NULL;
1092 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
1093 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
1094 return NT_STATUS_UNSUCCESSFUL;
1098 /* Walk through a dlist of arrays of commands. */
1099 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
1100 struct cmd_set *set = temp_list->cmd_set;
1102 while (set->name != NULL) {
1103 if (!strequal(argv[0], set->name)) {
1104 set += 1;
1105 continue;
1108 if (((set->returntype == RPC_RTYPE_NTSTATUS) &&
1109 (set->ntfn == NULL)) ||
1110 ((set->returntype == RPC_RTYPE_WERROR) &&
1111 (set->wfn == NULL)) ||
1112 ((set->returntype == RPC_RTYPE_BINDING) &&
1113 (set->bfn == NULL))) {
1114 fprintf (stderr, "Invalid command\n");
1115 goto out_free;
1118 result = do_cmd(
1119 cli, creds, set, binding, argc, argv);
1120 goto out_free;
1124 if (argv[0]) {
1125 printf("command not found: %s\n", argv[0]);
1128 out_free:
1129 /* moved to do_cmd()
1130 if (!NT_STATUS_IS_OK(result)) {
1131 printf("result was %s\n", nt_errstr(result));
1135 /* NOTE: popt allocates the whole argv, including the
1136 * strings, as a single block. So a single free is
1137 * enough to release it -- we don't free the
1138 * individual strings. rtfm. */
1139 free(argv);
1141 return result;
1145 /* Main function */
1147 int main(int argc, char *argv[])
1149 const char **const_argv = discard_const_p(const char *, argv);
1150 int opt;
1151 static char *cmdstr = NULL;
1152 const char *server;
1153 struct cli_state *cli = NULL;
1154 static char *opt_ipaddr=NULL;
1155 struct cmd_set **cmd_set;
1156 struct sockaddr_storage server_ss;
1157 NTSTATUS nt_status;
1158 static int opt_port = 0;
1159 int result = 0;
1160 TALLOC_CTX *frame = talloc_stackframe();
1161 uint32_t flags = CLI_FULL_CONNECTION_IPC;
1162 struct dcerpc_binding *binding = NULL;
1163 enum dcerpc_transport_t transport;
1164 const char *binding_string = NULL;
1165 const char *host;
1166 struct cli_credentials *creds = NULL;
1167 bool ok;
1169 /* make sure the vars that get altered (4th field) are in
1170 a fixed location or certain compilers complain */
1171 poptContext pc;
1172 struct poptOption long_options[] = {
1173 POPT_AUTOHELP
1174 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
1175 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
1176 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
1177 POPT_COMMON_SAMBA
1178 POPT_COMMON_CONNECTION
1179 POPT_COMMON_CREDENTIALS
1180 POPT_LEGACY_S3
1181 POPT_COMMON_VERSION
1182 POPT_TABLEEND
1185 smb_init_locale();
1187 zero_sockaddr(&server_ss);
1189 setlinebuf(stdout);
1191 ok = samba_cmdline_init(frame,
1192 SAMBA_CMDLINE_CONFIG_CLIENT,
1193 false /* require_smbconf */);
1194 if (!ok) {
1195 DBG_ERR("Failed to init cmdline parser!\n");
1197 lp_set_cmdline("log level", "0");
1199 /* Parse options */
1200 pc = samba_popt_get_context(getprogname(),
1201 argc,
1202 const_argv,
1203 long_options,
1205 if (pc == NULL) {
1206 DBG_ERR("Failed to setup popt context!\n");
1207 exit(1);
1210 poptSetOtherOptionHelp(pc, "[OPTION...] BINDING-STRING|HOST\nOptions:");
1212 if (argc == 1) {
1213 poptPrintHelp(pc, stderr, 0);
1214 goto done;
1217 while((opt = poptGetNextOpt(pc)) != -1) {
1218 switch (opt) {
1220 case 'I':
1221 if (!interpret_string_addr(&server_ss,
1222 opt_ipaddr,
1223 AI_NUMERICHOST)) {
1224 fprintf(stderr, "%s not a valid IP address\n",
1225 opt_ipaddr);
1226 result = 1;
1227 goto done;
1229 break;
1230 case POPT_ERROR_BADOPT:
1231 fprintf(stderr, "\nInvalid option %s: %s\n\n",
1232 poptBadOption(pc, 0), poptStrerror(opt));
1233 poptPrintUsage(pc, stderr, 0);
1234 exit(1);
1238 /* Get server as remaining unparsed argument. Print usage if more
1239 than one unparsed argument is present. */
1241 server = talloc_strdup(frame, poptGetArg(pc));
1243 if (!server || poptGetArg(pc)) {
1244 poptPrintHelp(pc, stderr, 0);
1245 result = 1;
1246 goto done;
1249 poptFreeContext(pc);
1250 samba_cmdline_burn(argc, argv);
1252 rpcclient_msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
1253 creds = samba_cmdline_get_creds();
1256 * Get password
1257 * from stdin if necessary
1260 if ((server[0] == '/' && server[1] == '/') ||
1261 (server[0] == '\\' && server[1] == '\\')) {
1262 server += 2;
1265 nt_status = dcerpc_parse_binding(frame, server, &binding);
1267 if (!NT_STATUS_IS_OK(nt_status)) {
1269 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1270 strip_hostname(server));
1271 if (!binding_string) {
1272 result = 1;
1273 goto done;
1276 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1277 if (!NT_STATUS_IS_OK(nt_status)) {
1278 result = -1;
1279 goto done;
1283 transport = dcerpc_binding_get_transport(binding);
1285 if (transport == NCA_UNKNOWN) {
1286 transport = NCACN_NP;
1287 nt_status = dcerpc_binding_set_transport(binding, transport);
1288 if (!NT_STATUS_IS_OK(nt_status)) {
1289 result = -1;
1290 goto done;
1294 host = dcerpc_binding_get_string_option(binding, "host");
1296 rpcclient_netlogon_domain = cli_credentials_get_domain(creds);
1297 if (rpcclient_netlogon_domain == NULL ||
1298 rpcclient_netlogon_domain[0] == '\0')
1300 rpcclient_netlogon_domain = lp_workgroup();
1303 if (transport == NCACN_NP) {
1304 nt_status = cli_full_connection_creds(
1305 &cli,
1306 lp_netbios_name(),
1307 host,
1308 opt_ipaddr ? &server_ss : NULL,
1309 opt_port,
1310 "IPC$",
1311 "IPC",
1312 creds,
1313 flags);
1315 if (!NT_STATUS_IS_OK(nt_status)) {
1316 DEBUG(0, ("Cannot connect to server. Error was %s\n",
1317 nt_errstr(nt_status)));
1318 result = 1;
1319 goto done;
1322 /* Load command lists */
1323 cli_set_timeout(cli, timeout);
1326 #if 0 /* COMMENT OUT FOR TESTING */
1327 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1328 #endif
1330 cmd_set = rpcclient_command_list;
1332 while(*cmd_set) {
1333 add_command_set(*cmd_set);
1334 add_command_set(separator_command);
1335 cmd_set++;
1338 /* Do anything specified with -c */
1339 if (cmdstr && cmdstr[0]) {
1340 char *cmd;
1341 char *p = cmdstr;
1343 result = 0;
1345 while((cmd=next_command(&p)) != NULL) {
1346 NTSTATUS cmd_result = process_cmd(creds,
1347 cli,
1348 binding,
1349 cmd);
1350 SAFE_FREE(cmd);
1351 result = NT_STATUS_IS_ERR(cmd_result);
1354 goto done;
1357 /* Loop around accepting commands */
1359 while(1) {
1360 char *line = NULL;
1362 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1364 if (line == NULL) {
1365 printf("\n");
1366 break;
1369 if (line[0] != '\n')
1370 process_cmd(creds,
1371 cli,
1372 binding,
1373 line);
1374 SAFE_FREE(line);
1377 done:
1378 if (cli != NULL) {
1379 cli_shutdown(cli);
1381 netlogon_creds_cli_close_global_db();
1382 TALLOC_FREE(rpcclient_msg_ctx);
1383 TALLOC_FREE(frame);
1384 return result;