rpcclient: Add unixinfo commands
[Samba.git] / source3 / rpcclient / rpcclient.c
blob7c9982b33e9b5a55c431118d22d8d636f9b95dd3
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();
883 transport = dcerpc_binding_get_transport(binding);
885 /* Open pipe */
887 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
888 if (transport == NCALRPC) {
889 ntresult = rpccli_ncalrpc_connect(
890 cmd_entry->table, cli, &cmd_entry->rpc_pipe);
891 if (!NT_STATUS_IS_OK(ntresult)) {
892 TALLOC_FREE(mem_ctx);
893 return ntresult;
895 } else {
896 enum dcerpc_AuthType auth_type;
897 enum dcerpc_AuthLevel auth_level;
898 enum credentials_use_kerberos krb5_state =
899 cli_credentials_get_kerberos_state(creds);
901 binding_get_auth_info(
902 binding, &auth_type, &auth_level, &krb5_state);
904 switch (auth_type) {
905 case DCERPC_AUTH_TYPE_NONE:
906 ntresult = cli_rpc_pipe_open_noauth_transport(
907 cli, transport,
908 cmd_entry->table,
909 &cmd_entry->rpc_pipe);
910 break;
911 case DCERPC_AUTH_TYPE_SPNEGO:
912 case DCERPC_AUTH_TYPE_NTLMSSP:
913 case DCERPC_AUTH_TYPE_KRB5:
914 cli_credentials_set_kerberos_state(creds,
915 krb5_state,
916 CRED_SPECIFIED);
918 ntresult = cli_rpc_pipe_open_with_creds(
919 cli, cmd_entry->table,
920 transport,
921 auth_type,
922 auth_level,
923 smbXcli_conn_remote_name(cli->conn),
924 creds,
925 &cmd_entry->rpc_pipe);
926 break;
927 case DCERPC_AUTH_TYPE_SCHANNEL:
928 TALLOC_FREE(rpcclient_netlogon_creds);
929 ntresult = cli_rpc_pipe_open_schannel(
930 cli, rpcclient_msg_ctx,
931 cmd_entry->table,
932 transport,
933 rpcclient_netlogon_domain,
934 &cmd_entry->rpc_pipe,
935 rpcclient_msg_ctx,
936 &rpcclient_netlogon_creds);
937 break;
938 default:
939 DEBUG(0, ("Could not initialise %s. Invalid "
940 "auth type %u\n",
941 cmd_entry->table->name,
942 auth_type ));
943 talloc_free(mem_ctx);
944 return NT_STATUS_UNSUCCESSFUL;
946 if (!NT_STATUS_IS_OK(ntresult)) {
947 DBG_ERR("Could not initialise %s. "
948 "Error was %s\n",
949 cmd_entry->table->name,
950 nt_errstr(ntresult));
951 talloc_free(mem_ctx);
952 return ntresult;
955 if (rpcclient_netlogon_creds == NULL &&
956 cmd_entry->use_netlogon_creds) {
957 const char *dc_name =
958 cmd_entry->rpc_pipe->desthost;
959 const char *domain = rpcclient_netlogon_domain;
960 struct cli_credentials *trust_creds = NULL;
962 ntresult = pdb_get_trust_credentials(
963 domain,
964 NULL,
965 mem_ctx,
966 &trust_creds);
967 if (!NT_STATUS_IS_OK(ntresult)) {
968 DBG_ERR("Failed to fetch trust "
969 "credentials for "
970 "%s to connect to %s: %s\n",
971 domain,
972 cmd_entry->table->name,
973 nt_errstr(ntresult));
974 TALLOC_FREE(cmd_entry->rpc_pipe);
975 talloc_free(mem_ctx);
976 return ntresult;
979 ntresult = rpccli_create_netlogon_creds_ctx(
980 trust_creds,
981 dc_name,
982 rpcclient_msg_ctx,
983 rpcclient_msg_ctx,
984 &rpcclient_netlogon_creds);
985 if (!NT_STATUS_IS_OK(ntresult)) {
986 DBG_ERR("Could not initialise "
987 "credentials for %s.\n",
988 cmd_entry->table->name);
989 TALLOC_FREE(cmd_entry->rpc_pipe);
990 TALLOC_FREE(mem_ctx);
991 return ntresult;
994 ntresult = rpccli_setup_netlogon_creds(
995 cli,
996 NCACN_NP,
997 rpcclient_netlogon_creds,
998 false, /* force_reauth */
999 trust_creds);
1000 TALLOC_FREE(trust_creds);
1001 if (!NT_STATUS_IS_OK(ntresult)) {
1002 DBG_ERR("Could not initialise "
1003 "credentials for %s.\n",
1004 cmd_entry->table->name);
1005 TALLOC_FREE(cmd_entry->rpc_pipe);
1006 TALLOC_FREE(rpcclient_netlogon_creds);
1007 TALLOC_FREE(mem_ctx);
1008 return ntresult;
1014 /* Set timeout for new connections */
1015 if (cmd_entry->rpc_pipe) {
1016 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
1019 /* Run command */
1021 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
1022 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1023 if (!NT_STATUS_IS_OK(ntresult)) {
1024 printf("result was %s\n", nt_errstr(ntresult));
1026 } else if (cmd_entry->returntype == RPC_RTYPE_BINDING) {
1027 ntresult = cmd_entry->bfn(binding, mem_ctx, argc, argv);
1028 if (!NT_STATUS_IS_OK(ntresult)) {
1029 printf("result was %s\n", nt_errstr(ntresult));
1031 } else {
1032 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
1033 /* print out the DOS error */
1034 if (!W_ERROR_IS_OK(wresult)) {
1035 printf( "result was %s\n", win_errstr(wresult));
1037 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
1040 /* Cleanup */
1042 talloc_free(mem_ctx);
1044 return ntresult;
1049 * Process a command entered at the prompt or as part of -c
1051 * @returns The NTSTATUS from running the command.
1053 static NTSTATUS process_cmd(struct cli_credentials *creds,
1054 struct cli_state *cli,
1055 struct dcerpc_binding *binding,
1056 char *cmd)
1058 struct cmd_list *temp_list;
1059 NTSTATUS result = NT_STATUS_OK;
1060 int ret;
1061 int argc;
1062 const char **argv = NULL;
1064 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
1065 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
1066 return NT_STATUS_UNSUCCESSFUL;
1070 /* Walk through a dlist of arrays of commands. */
1071 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
1072 struct cmd_set *set = temp_list->cmd_set;
1074 while (set->name != NULL) {
1075 if (!strequal(argv[0], set->name)) {
1076 set += 1;
1077 continue;
1080 if (((set->returntype == RPC_RTYPE_NTSTATUS) &&
1081 (set->ntfn == NULL)) ||
1082 ((set->returntype == RPC_RTYPE_WERROR) &&
1083 (set->wfn == NULL)) ||
1084 ((set->returntype == RPC_RTYPE_BINDING) &&
1085 (set->bfn == NULL))) {
1086 fprintf (stderr, "Invalid command\n");
1087 goto out_free;
1090 result = do_cmd(
1091 cli, creds, set, binding, argc, argv);
1092 goto out_free;
1096 if (argv[0]) {
1097 printf("command not found: %s\n", argv[0]);
1100 out_free:
1101 /* moved to do_cmd()
1102 if (!NT_STATUS_IS_OK(result)) {
1103 printf("result was %s\n", nt_errstr(result));
1107 /* NOTE: popt allocates the whole argv, including the
1108 * strings, as a single block. So a single free is
1109 * enough to release it -- we don't free the
1110 * individual strings. rtfm. */
1111 free(argv);
1113 return result;
1117 /* Main function */
1119 int main(int argc, char *argv[])
1121 const char **const_argv = discard_const_p(const char *, argv);
1122 int opt;
1123 static char *cmdstr = NULL;
1124 const char *server;
1125 struct cli_state *cli = NULL;
1126 static char *opt_ipaddr=NULL;
1127 struct cmd_set **cmd_set;
1128 struct sockaddr_storage server_ss;
1129 NTSTATUS nt_status;
1130 static int opt_port = 0;
1131 int result = 0;
1132 TALLOC_CTX *frame = talloc_stackframe();
1133 uint32_t flags = CLI_FULL_CONNECTION_IPC;
1134 struct dcerpc_binding *binding = NULL;
1135 enum dcerpc_transport_t transport;
1136 const char *binding_string = NULL;
1137 const char *host;
1138 struct cli_credentials *creds = NULL;
1139 bool ok;
1141 /* make sure the vars that get altered (4th field) are in
1142 a fixed location or certain compilers complain */
1143 poptContext pc;
1144 struct poptOption long_options[] = {
1145 POPT_AUTOHELP
1146 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
1147 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
1148 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
1149 POPT_COMMON_SAMBA
1150 POPT_COMMON_CONNECTION
1151 POPT_COMMON_CREDENTIALS
1152 POPT_LEGACY_S3
1153 POPT_COMMON_VERSION
1154 POPT_TABLEEND
1157 smb_init_locale();
1159 zero_sockaddr(&server_ss);
1161 setlinebuf(stdout);
1163 ok = samba_cmdline_init(frame,
1164 SAMBA_CMDLINE_CONFIG_CLIENT,
1165 false /* require_smbconf */);
1166 if (!ok) {
1167 DBG_ERR("Failed to init cmdline parser!\n");
1169 lp_set_cmdline("log level", "0");
1171 /* Parse options */
1172 pc = samba_popt_get_context(getprogname(),
1173 argc,
1174 const_argv,
1175 long_options,
1177 if (pc == NULL) {
1178 DBG_ERR("Failed to setup popt context!\n");
1179 exit(1);
1182 poptSetOtherOptionHelp(pc, "[OPTION...] BINDING-STRING|HOST\nOptions:");
1184 if (argc == 1) {
1185 poptPrintHelp(pc, stderr, 0);
1186 goto done;
1189 while((opt = poptGetNextOpt(pc)) != -1) {
1190 switch (opt) {
1192 case 'I':
1193 if (!interpret_string_addr(&server_ss,
1194 opt_ipaddr,
1195 AI_NUMERICHOST)) {
1196 fprintf(stderr, "%s not a valid IP address\n",
1197 opt_ipaddr);
1198 result = 1;
1199 goto done;
1204 /* Get server as remaining unparsed argument. Print usage if more
1205 than one unparsed argument is present. */
1207 server = poptGetArg(pc);
1209 if (!server || poptGetArg(pc)) {
1210 poptPrintHelp(pc, stderr, 0);
1211 result = 1;
1212 goto done;
1215 poptFreeContext(pc);
1216 samba_cmdline_burn(argc, argv);
1218 rpcclient_msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
1219 creds = samba_cmdline_get_creds();
1222 * Get password
1223 * from stdin if necessary
1226 if ((server[0] == '/' && server[1] == '/') ||
1227 (server[0] == '\\' && server[1] == '\\')) {
1228 server += 2;
1231 nt_status = dcerpc_parse_binding(frame, server, &binding);
1233 if (!NT_STATUS_IS_OK(nt_status)) {
1235 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1236 strip_hostname(server));
1237 if (!binding_string) {
1238 result = 1;
1239 goto done;
1242 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1243 if (!NT_STATUS_IS_OK(nt_status)) {
1244 result = -1;
1245 goto done;
1249 transport = dcerpc_binding_get_transport(binding);
1251 if (transport == NCA_UNKNOWN) {
1252 transport = NCACN_NP;
1253 nt_status = dcerpc_binding_set_transport(binding, transport);
1254 if (!NT_STATUS_IS_OK(nt_status)) {
1255 result = -1;
1256 goto done;
1260 host = dcerpc_binding_get_string_option(binding, "host");
1262 rpcclient_netlogon_domain = cli_credentials_get_domain(creds);
1263 if (rpcclient_netlogon_domain == NULL ||
1264 rpcclient_netlogon_domain[0] == '\0')
1266 rpcclient_netlogon_domain = lp_workgroup();
1269 if (transport == NCACN_NP) {
1270 nt_status = cli_full_connection_creds(
1271 &cli,
1272 lp_netbios_name(),
1273 host,
1274 opt_ipaddr ? &server_ss : NULL,
1275 opt_port,
1276 "IPC$",
1277 "IPC",
1278 creds,
1279 flags);
1281 if (!NT_STATUS_IS_OK(nt_status)) {
1282 DEBUG(0, ("Cannot connect to server. Error was %s\n",
1283 nt_errstr(nt_status)));
1284 result = 1;
1285 goto done;
1288 /* Load command lists */
1289 cli_set_timeout(cli, timeout);
1292 #if 0 /* COMMENT OUT FOR TESTING */
1293 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1294 #endif
1296 cmd_set = rpcclient_command_list;
1298 while(*cmd_set) {
1299 add_command_set(*cmd_set);
1300 add_command_set(separator_command);
1301 cmd_set++;
1304 /* Do anything specified with -c */
1305 if (cmdstr && cmdstr[0]) {
1306 char *cmd;
1307 char *p = cmdstr;
1309 result = 0;
1311 while((cmd=next_command(&p)) != NULL) {
1312 NTSTATUS cmd_result = process_cmd(creds,
1313 cli,
1314 binding,
1315 cmd);
1316 SAFE_FREE(cmd);
1317 result = NT_STATUS_IS_ERR(cmd_result);
1320 goto done;
1323 /* Loop around accepting commands */
1325 while(1) {
1326 char *line = NULL;
1328 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1330 if (line == NULL) {
1331 printf("\n");
1332 break;
1335 if (line[0] != '\n')
1336 process_cmd(creds,
1337 cli,
1338 binding,
1339 line);
1340 SAFE_FREE(line);
1343 done:
1344 if (cli != NULL) {
1345 cli_shutdown(cli);
1347 netlogon_creds_cli_close_global_db();
1348 TALLOC_FREE(rpcclient_msg_ctx);
1349 TALLOC_FREE(frame);
1350 return result;