smbd: Remove unused "blocking_lock" from brl_lock_windows_default()
[Samba.git] / source3 / rpcclient / rpcclient.c
blob4497bb622429a35b715483f63787b9127a5cc069
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 "popt_common_cmdline.h"
25 #include "rpcclient.h"
26 #include "../libcli/auth/libcli_auth.h"
27 #include "../librpc/gen_ndr/ndr_lsa_c.h"
28 #include "rpc_client/cli_lsarpc.h"
29 #include "../librpc/gen_ndr/ndr_netlogon.h"
30 #include "rpc_client/cli_netlogon.h"
31 #include "../libcli/smbreadline/smbreadline.h"
32 #include "../libcli/security/security.h"
33 #include "passdb.h"
34 #include "libsmb/libsmb.h"
35 #include "auth/gensec/gensec.h"
36 #include "../libcli/smb/smbXcli_base.h"
37 #include "messages.h"
38 #include "cmdline_contexts.h"
40 enum pipe_auth_type_spnego {
41 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
42 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
43 PIPE_AUTH_TYPE_SPNEGO_KRB5
46 struct dom_sid domain_sid;
48 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
49 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
50 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
51 static unsigned int timeout = 0;
52 static enum dcerpc_transport_t default_transport = NCACN_NP;
54 struct messaging_context *rpcclient_msg_ctx;
55 struct cli_state *rpcclient_cli_state;
56 struct netlogon_creds_cli_context *rpcclient_netlogon_creds;
57 static const char *rpcclient_netlogon_domain;
59 /* List to hold groups of commands.
61 * Commands are defined in a list of arrays: arrays are easy to
62 * statically declare, and lists are easier to dynamically extend.
65 static struct cmd_list {
66 struct cmd_list *prev, *next;
67 struct cmd_set *cmd_set;
68 } *cmd_list;
70 /****************************************************************************
71 handle completion of commands for readline
72 ****************************************************************************/
73 static char **completion_fn(const char *text, int start, int end)
75 #define MAX_COMPLETIONS 1000
76 char **matches;
77 size_t i, count=0;
78 struct cmd_list *commands = cmd_list;
80 #if 0 /* JERRY */
81 /* FIXME!!! -- what to do when completing argument? */
82 /* for words not at the start of the line fallback
83 to filename completion */
84 if (start)
85 return NULL;
86 #endif
88 /* make sure we have a list of valid commands */
89 if (!commands) {
90 return NULL;
93 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
94 if (!matches) {
95 return NULL;
98 matches[count++] = SMB_STRDUP(text);
99 if (!matches[0]) {
100 SAFE_FREE(matches);
101 return NULL;
104 while (commands && count < MAX_COMPLETIONS-1) {
105 if (!commands->cmd_set) {
106 break;
109 for (i=0; commands->cmd_set[i].name; i++) {
110 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
111 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
112 commands->cmd_set[i].ntfn ) ||
113 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
114 commands->cmd_set[i].wfn))) {
115 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
116 if (!matches[count]) {
117 for (i = 0; i < count; i++) {
118 SAFE_FREE(matches[count]);
120 SAFE_FREE(matches);
121 return NULL;
123 count++;
126 commands = commands->next;
129 if (count == 2) {
130 SAFE_FREE(matches[0]);
131 matches[0] = SMB_STRDUP(matches[1]);
133 matches[count] = NULL;
134 return matches;
137 static char *next_command (char **cmdstr)
139 char *command;
140 char *p;
142 if (!cmdstr || !(*cmdstr))
143 return NULL;
145 p = strchr_m(*cmdstr, ';');
146 if (p)
147 *p = '\0';
148 command = SMB_STRDUP(*cmdstr);
149 if (p)
150 *cmdstr = p + 1;
151 else
152 *cmdstr = NULL;
154 return command;
157 /* Fetch the SID for this computer */
159 static void fetch_machine_sid(struct cli_state *cli)
161 struct policy_handle pol;
162 NTSTATUS result = NT_STATUS_OK, status;
163 static bool got_domain_sid;
164 TALLOC_CTX *mem_ctx;
165 struct rpc_pipe_client *lsapipe = NULL;
166 union lsa_PolicyInformation *info = NULL;
167 struct dcerpc_binding_handle *b;
169 if (got_domain_sid) return;
171 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
172 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
173 goto error;
176 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
177 &lsapipe);
178 if (!NT_STATUS_IS_OK(result)) {
179 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
180 goto error;
183 b = lsapipe->binding_handle;
185 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
186 SEC_FLAG_MAXIMUM_ALLOWED,
187 &pol);
188 if (!NT_STATUS_IS_OK(result)) {
189 goto error;
192 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
193 &pol,
194 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
195 &info,
196 &result);
197 if (!NT_STATUS_IS_OK(status)) {
198 result = status;
199 goto error;
201 if (!NT_STATUS_IS_OK(result)) {
202 goto error;
205 got_domain_sid = True;
206 sid_copy(&domain_sid, info->account_domain.sid);
208 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
209 TALLOC_FREE(lsapipe);
210 talloc_destroy(mem_ctx);
212 return;
214 error:
216 if (lsapipe) {
217 TALLOC_FREE(lsapipe);
220 fprintf(stderr, "could not obtain sid from server\n");
222 if (!NT_STATUS_IS_OK(result)) {
223 fprintf(stderr, "error: %s\n", nt_errstr(result));
226 exit(1);
229 /* List the available commands on a given pipe */
231 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
232 int argc, const char **argv)
234 struct cmd_list *tmp;
235 struct cmd_set *tmp_set;
236 int i;
238 /* Usage */
240 if (argc != 2) {
241 printf("Usage: %s <pipe>\n", argv[0]);
242 return NT_STATUS_OK;
245 /* Help on one command */
247 for (tmp = cmd_list; tmp; tmp = tmp->next)
249 tmp_set = tmp->cmd_set;
251 if (!strcasecmp_m(argv[1], tmp_set->name))
253 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
255 i = 0;
256 tmp_set++;
257 while(tmp_set->name) {
258 printf("%30s", tmp_set->name);
259 tmp_set++;
260 i++;
261 if (i%3 == 0)
262 printf("\n");
265 /* drop out of the loop */
266 break;
269 printf("\n\n");
271 return NT_STATUS_OK;
274 /* Display help on commands */
276 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
277 int argc, const char **argv)
279 struct cmd_list *tmp;
280 struct cmd_set *tmp_set;
282 /* Usage */
284 if (argc > 2) {
285 printf("Usage: %s [command]\n", argv[0]);
286 return NT_STATUS_OK;
289 /* Help on one command */
291 if (argc == 2) {
292 for (tmp = cmd_list; tmp; tmp = tmp->next) {
294 tmp_set = tmp->cmd_set;
296 while(tmp_set->name) {
297 if (strequal(argv[1], tmp_set->name)) {
298 if (tmp_set->usage &&
299 tmp_set->usage[0])
300 printf("%s\n", tmp_set->usage);
301 else
302 printf("No help for %s\n", tmp_set->name);
304 return NT_STATUS_OK;
307 tmp_set++;
311 printf("No such command: %s\n", argv[1]);
312 return NT_STATUS_OK;
315 /* List all commands */
317 for (tmp = cmd_list; tmp; tmp = tmp->next) {
319 tmp_set = tmp->cmd_set;
321 while(tmp_set->name) {
323 printf("%15s\t\t%s\n", tmp_set->name,
324 tmp_set->description ? tmp_set->description:
325 "");
327 tmp_set++;
331 return NT_STATUS_OK;
334 /* Change the debug level */
336 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
337 int argc, const char **argv)
339 if (argc > 2) {
340 printf("Usage: %s [debuglevel]\n", argv[0]);
341 return NT_STATUS_OK;
344 if (argc == 2) {
345 lp_set_cmdline("log level", argv[1]);
348 printf("debuglevel is %d\n", DEBUGLEVEL);
350 return NT_STATUS_OK;
353 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
354 int argc, const char **argv)
356 exit(0);
357 return NT_STATUS_OK; /* NOTREACHED */
360 static NTSTATUS cmd_set_ss_level(void)
362 struct cmd_list *tmp;
364 /* Close any existing connections not at this level. */
366 for (tmp = cmd_list; tmp; tmp = tmp->next) {
367 struct cmd_set *tmp_set;
369 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
370 if (tmp_set->rpc_pipe == NULL) {
371 continue;
374 if ((tmp_set->rpc_pipe->auth->auth_type
375 != pipe_default_auth_type)
376 || (tmp_set->rpc_pipe->auth->auth_level
377 != pipe_default_auth_level)) {
378 TALLOC_FREE(tmp_set->rpc_pipe);
379 tmp_set->rpc_pipe = NULL;
383 return NT_STATUS_OK;
386 static NTSTATUS cmd_set_transport(void)
388 struct cmd_list *tmp;
390 /* Close any existing connections not at this level. */
392 for (tmp = cmd_list; tmp; tmp = tmp->next) {
393 struct cmd_set *tmp_set;
395 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
396 if (tmp_set->rpc_pipe == NULL) {
397 continue;
400 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
401 TALLOC_FREE(tmp_set->rpc_pipe);
402 tmp_set->rpc_pipe = NULL;
406 return NT_STATUS_OK;
409 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
410 int argc, const char **argv)
412 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
413 const char *type = "NTLMSSP";
415 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
416 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
418 if (argc > 2) {
419 printf("Usage: %s %s\n", argv[0], p);
420 return NT_STATUS_OK;
423 if (argc == 2) {
424 type = argv[1];
425 if (strequal(type, "KRB5")) {
426 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
427 } else if (strequal(type, "KRB5_SPNEGO")) {
428 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
429 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
430 } else if (strequal(type, "NTLMSSP")) {
431 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
432 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
433 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
434 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
435 } else if (strequal(type, "SCHANNEL")) {
436 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
437 } else {
438 printf("unknown type %s\n", type);
439 printf("Usage: %s %s\n", argv[0], p);
440 return NT_STATUS_INVALID_LEVEL;
444 d_printf("Setting %s - sign\n", type);
446 return cmd_set_ss_level();
449 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
450 int argc, const char **argv)
452 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
453 const char *type = "NTLMSSP";
455 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
456 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
458 if (argc > 2) {
459 printf("Usage: %s %s\n", argv[0], p);
460 return NT_STATUS_OK;
463 if (argc == 2) {
464 type = argv[1];
465 if (strequal(type, "KRB5")) {
466 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
467 } else if (strequal(type, "KRB5_SPNEGO")) {
468 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
469 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
470 } else if (strequal(type, "NTLMSSP")) {
471 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
472 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
473 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
474 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
475 } else if (strequal(type, "SCHANNEL")) {
476 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
477 } else {
478 printf("unknown type %s\n", type);
479 printf("Usage: %s %s\n", argv[0], p);
480 return NT_STATUS_INVALID_LEVEL;
484 d_printf("Setting %s - sign and seal\n", type);
486 return cmd_set_ss_level();
489 static NTSTATUS cmd_packet(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
490 int argc, const char **argv)
492 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
493 const char *type = "NTLMSSP";
495 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PACKET;
496 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
498 if (argc > 2) {
499 printf("Usage: %s %s\n", argv[0], p);
500 return NT_STATUS_OK;
503 if (argc == 2) {
504 type = argv[1];
505 if (strequal(type, "KRB5")) {
506 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
507 } else if (strequal(type, "KRB5_SPNEGO")) {
508 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
509 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
510 } else if (strequal(type, "NTLMSSP")) {
511 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
512 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
513 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
514 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
515 } else if (strequal(type, "SCHANNEL")) {
516 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
517 } else {
518 printf("unknown type %s\n", type);
519 printf("Usage: %s %s\n", argv[0], p);
520 return NT_STATUS_INVALID_LEVEL;
524 d_printf("Setting %s - packet\n", type);
526 return cmd_set_ss_level();
530 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
531 int argc, const char **argv)
533 if (argc > 2) {
534 printf("Usage: %s timeout\n", argv[0]);
535 return NT_STATUS_OK;
538 if (argc == 2) {
539 timeout = atoi(argv[1]);
542 printf("timeout is %d\n", timeout);
544 return NT_STATUS_OK;
548 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
549 int argc, const char **argv)
551 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
552 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
553 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
555 return cmd_set_ss_level();
558 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
559 int argc, const char **argv)
561 d_printf("Setting schannel - sign and seal\n");
562 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
563 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
565 return cmd_set_ss_level();
568 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
569 int argc, const char **argv)
571 d_printf("Setting schannel - sign only\n");
572 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
573 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
575 return cmd_set_ss_level();
578 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
579 int argc, const char **argv)
581 NTSTATUS status;
583 if (argc != 2) {
584 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
585 return NT_STATUS_OK;
588 if (strequal(argv[1], "NCACN_NP")) {
589 default_transport = NCACN_NP;
590 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
591 default_transport = NCACN_IP_TCP;
592 } else {
593 printf("transport type: %s unknown or not supported\n", argv[1]);
594 return NT_STATUS_NOT_SUPPORTED;
597 status = cmd_set_transport();
598 if (!NT_STATUS_IS_OK(status)) {
599 return status;
602 printf("default transport is now: %s\n", argv[1]);
604 return NT_STATUS_OK;
607 /* Built in rpcclient commands */
609 static struct cmd_set rpcclient_commands[] = {
612 .name = "GENERAL OPTIONS",
616 .name = "help",
617 .returntype = RPC_RTYPE_NTSTATUS,
618 .ntfn = cmd_help,
619 .wfn = NULL,
620 .table = NULL,
621 .rpc_pipe = NULL,
622 .description = "Get help on commands",
623 .usage = "[command]",
626 .name = "?",
627 .returntype = RPC_RTYPE_NTSTATUS,
628 .ntfn = cmd_help,
629 .wfn = NULL,
630 .table = NULL,
631 .rpc_pipe = NULL,
632 .description = "Get help on commands",
633 .usage = "[command]",
636 .name = "debuglevel",
637 .returntype = RPC_RTYPE_NTSTATUS,
638 .ntfn = cmd_debuglevel,
639 .wfn = NULL,
640 .table = NULL,
641 .rpc_pipe = NULL,
642 .description = "Set debug level",
643 .usage = "level",
646 .name = "debug",
647 .returntype = RPC_RTYPE_NTSTATUS,
648 .ntfn = cmd_debuglevel,
649 .wfn = NULL,
650 .table = NULL,
651 .rpc_pipe = NULL,
652 .description = "Set debug level",
653 .usage = "level",
656 .name = "list",
657 .returntype = RPC_RTYPE_NTSTATUS,
658 .ntfn = cmd_listcommands,
659 .wfn = NULL,
660 .table = NULL,
661 .rpc_pipe = NULL,
662 .description = "List available commands on <pipe>",
663 .usage = "pipe",
666 .name = "exit",
667 .returntype = RPC_RTYPE_NTSTATUS,
668 .ntfn = cmd_quit,
669 .wfn = NULL,
670 .table = NULL,
671 .rpc_pipe = NULL,
672 .description = "Exit program",
673 .usage = "",
676 .name = "quit",
677 .returntype = RPC_RTYPE_NTSTATUS,
678 .ntfn = cmd_quit,
679 .wfn = NULL,
680 .table = NULL,
681 .rpc_pipe = NULL,
682 .description = "Exit program",
683 .usage = "",
686 .name = "sign",
687 .returntype = RPC_RTYPE_NTSTATUS,
688 .ntfn = cmd_sign,
689 .wfn = NULL,
690 .table = NULL,
691 .rpc_pipe = NULL,
692 .description = "Force RPC pipe connections to be signed",
693 .usage = "",
696 .name = "seal",
697 .returntype = RPC_RTYPE_NTSTATUS,
698 .ntfn = cmd_seal,
699 .wfn = NULL,
700 .table = NULL,
701 .rpc_pipe = NULL,
702 .description = "Force RPC pipe connections to be sealed",
703 .usage = "",
706 .name = "packet",
707 .returntype = RPC_RTYPE_NTSTATUS,
708 .ntfn = cmd_packet,
709 .wfn = NULL,
710 .table = NULL,
711 .rpc_pipe = NULL,
712 .description = "Force RPC pipe connections with packet authentication level",
713 .usage = "",
716 .name = "schannel",
717 .returntype = RPC_RTYPE_NTSTATUS,
718 .ntfn = cmd_schannel,
719 .wfn = NULL,
720 .table = NULL,
721 .rpc_pipe = NULL,
722 .description = "Force RPC pipe connections to be sealed with 'schannel'. "
723 "Assumes valid machine account to this domain controller.",
724 .usage = "",
727 .name = "schannelsign",
728 .returntype = RPC_RTYPE_NTSTATUS,
729 .ntfn = cmd_schannel_sign,
730 .wfn = NULL,
731 .table = NULL,
732 .rpc_pipe = NULL,
733 .description = "Force RPC pipe connections to be signed (not sealed) with "
734 "'schannel'. Assumes valid machine account to this domain "
735 "controller.",
736 .usage = "",
739 .name = "timeout",
740 .returntype = RPC_RTYPE_NTSTATUS,
741 .ntfn = cmd_timeout,
742 .wfn = NULL,
743 .table = NULL,
744 .rpc_pipe = NULL,
745 .description = "Set timeout (in milliseconds) for RPC operations",
746 .usage = "",
749 .name = "transport",
750 .returntype = RPC_RTYPE_NTSTATUS,
751 .ntfn = cmd_choose_transport,
752 .wfn = NULL,
753 .table = NULL,
754 .rpc_pipe = NULL,
755 .description = "Choose ncacn transport for RPC operations",
756 .usage = "",
759 .name = "none",
760 .returntype = RPC_RTYPE_NTSTATUS,
761 .ntfn = cmd_none,
762 .wfn = NULL,
763 .table = NULL,
764 .rpc_pipe = NULL,
765 .description = "Force RPC pipe connections to have no special properties",
766 .usage = "",
769 { .name = NULL, },
772 static struct cmd_set separator_command[] = {
774 .name = "---------------",
775 .returntype = MAX_RPC_RETURN_TYPE,
776 .description = "----------------------"
778 { .name = NULL, },
782 /* Various pipe commands */
784 extern struct cmd_set lsarpc_commands[];
785 extern struct cmd_set samr_commands[];
786 extern struct cmd_set spoolss_commands[];
787 extern struct cmd_set iremotewinspool_commands[];
788 extern struct cmd_set netlogon_commands[];
789 extern struct cmd_set srvsvc_commands[];
790 extern struct cmd_set dfs_commands[];
791 extern struct cmd_set ds_commands[];
792 extern struct cmd_set echo_commands[];
793 extern struct cmd_set epmapper_commands[];
794 extern struct cmd_set shutdown_commands[];
795 extern struct cmd_set test_commands[];
796 extern struct cmd_set wkssvc_commands[];
797 extern struct cmd_set ntsvcs_commands[];
798 extern struct cmd_set drsuapi_commands[];
799 extern struct cmd_set eventlog_commands[];
800 extern struct cmd_set winreg_commands[];
801 extern struct cmd_set fss_commands[];
802 extern struct cmd_set witness_commands[];
803 extern struct cmd_set clusapi_commands[];
805 static struct cmd_set *rpcclient_command_list[] = {
806 rpcclient_commands,
807 lsarpc_commands,
808 ds_commands,
809 samr_commands,
810 spoolss_commands,
811 iremotewinspool_commands,
812 netlogon_commands,
813 srvsvc_commands,
814 dfs_commands,
815 echo_commands,
816 epmapper_commands,
817 shutdown_commands,
818 test_commands,
819 wkssvc_commands,
820 ntsvcs_commands,
821 drsuapi_commands,
822 eventlog_commands,
823 winreg_commands,
824 fss_commands,
825 witness_commands,
826 clusapi_commands,
827 NULL
830 static void add_command_set(struct cmd_set *cmd_set)
832 struct cmd_list *entry;
834 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
835 DEBUG(0, ("out of memory\n"));
836 return;
839 ZERO_STRUCTP(entry);
841 entry->cmd_set = cmd_set;
842 DLIST_ADD(cmd_list, entry);
847 * Call an rpcclient function, passing an argv array.
849 * @param cmd Command to run, as a single string.
851 static NTSTATUS do_cmd(struct cli_state *cli,
852 struct user_auth_info *auth_info,
853 struct cmd_set *cmd_entry,
854 struct dcerpc_binding *binding,
855 int argc, const char **argv)
857 NTSTATUS ntresult;
858 WERROR wresult;
860 TALLOC_CTX *mem_ctx;
862 /* Create mem_ctx */
864 if (!(mem_ctx = talloc_stackframe())) {
865 DEBUG(0, ("talloc_init() failed\n"));
866 return NT_STATUS_NO_MEMORY;
869 /* Open pipe */
871 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
872 enum credentials_use_kerberos use_kerberos = CRED_AUTO_USE_KERBEROS;
873 switch (pipe_default_auth_type) {
874 case DCERPC_AUTH_TYPE_NONE:
875 ntresult = cli_rpc_pipe_open_noauth_transport(
876 cli, default_transport,
877 cmd_entry->table,
878 &cmd_entry->rpc_pipe);
879 break;
880 case DCERPC_AUTH_TYPE_SPNEGO:
881 switch (pipe_default_auth_spnego_type) {
882 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
883 use_kerberos = CRED_DONT_USE_KERBEROS;
884 break;
885 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
886 use_kerberos = CRED_MUST_USE_KERBEROS;
887 break;
888 case PIPE_AUTH_TYPE_SPNEGO_NONE:
889 use_kerberos = CRED_AUTO_USE_KERBEROS;
890 break;
892 FALL_THROUGH;
893 case DCERPC_AUTH_TYPE_NTLMSSP:
894 case DCERPC_AUTH_TYPE_KRB5:
895 ntresult = cli_rpc_pipe_open_generic_auth(
896 cli, cmd_entry->table,
897 default_transport,
898 use_kerberos,
899 pipe_default_auth_type,
900 pipe_default_auth_level,
901 smbXcli_conn_remote_name(cli->conn),
902 get_cmdline_auth_info_domain(auth_info),
903 get_cmdline_auth_info_username(auth_info),
904 get_cmdline_auth_info_password(auth_info),
905 &cmd_entry->rpc_pipe);
906 break;
907 case DCERPC_AUTH_TYPE_SCHANNEL:
908 TALLOC_FREE(rpcclient_netlogon_creds);
909 ntresult = cli_rpc_pipe_open_schannel(
910 cli, rpcclient_msg_ctx,
911 cmd_entry->table,
912 default_transport,
913 rpcclient_netlogon_domain,
914 &cmd_entry->rpc_pipe,
915 rpcclient_msg_ctx,
916 &rpcclient_netlogon_creds);
917 break;
918 default:
919 DEBUG(0, ("Could not initialise %s. Invalid "
920 "auth type %u\n",
921 cmd_entry->table->name,
922 pipe_default_auth_type ));
923 talloc_free(mem_ctx);
924 return NT_STATUS_UNSUCCESSFUL;
926 if (!NT_STATUS_IS_OK(ntresult)) {
927 DEBUG(0, ("Could not initialise %s. Error was %s\n",
928 cmd_entry->table->name,
929 nt_errstr(ntresult) ));
930 talloc_free(mem_ctx);
931 return ntresult;
934 if (rpcclient_netlogon_creds == NULL && cmd_entry->use_netlogon_creds) {
935 const char *dc_name = cmd_entry->rpc_pipe->desthost;
936 const char *domain = rpcclient_netlogon_domain;
937 struct cli_credentials *creds = NULL;
939 ntresult = pdb_get_trust_credentials(domain, NULL,
940 mem_ctx, &creds);
941 if (!NT_STATUS_IS_OK(ntresult)) {
942 DEBUG(0, ("Failed to fetch trust credentials for "
943 "%s to connect to %s: %s\n",
944 domain, cmd_entry->table->name,
945 nt_errstr(ntresult)));
946 TALLOC_FREE(cmd_entry->rpc_pipe);
947 talloc_free(mem_ctx);
948 return ntresult;
951 ntresult = rpccli_create_netlogon_creds_ctx(creds,
952 dc_name,
953 rpcclient_msg_ctx,
954 rpcclient_msg_ctx,
955 &rpcclient_netlogon_creds);
956 if (!NT_STATUS_IS_OK(ntresult)) {
957 DEBUG(0, ("Could not initialise credentials for %s.\n",
958 cmd_entry->table->name));
959 TALLOC_FREE(cmd_entry->rpc_pipe);
960 TALLOC_FREE(mem_ctx);
961 return ntresult;
964 ntresult = rpccli_setup_netlogon_creds(
965 cli,
966 NCACN_NP,
967 rpcclient_netlogon_creds,
968 false, /* force_reauth */
969 creds);
970 TALLOC_FREE(creds);
971 if (!NT_STATUS_IS_OK(ntresult)) {
972 DEBUG(0, ("Could not initialise credentials for %s.\n",
973 cmd_entry->table->name));
974 TALLOC_FREE(cmd_entry->rpc_pipe);
975 TALLOC_FREE(rpcclient_netlogon_creds);
976 TALLOC_FREE(mem_ctx);
977 return ntresult;
982 /* Set timeout for new connections */
983 if (cmd_entry->rpc_pipe) {
984 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
987 /* Run command */
989 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
990 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
991 if (!NT_STATUS_IS_OK(ntresult)) {
992 printf("result was %s\n", nt_errstr(ntresult));
994 } else {
995 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
996 /* print out the DOS error */
997 if (!W_ERROR_IS_OK(wresult)) {
998 printf( "result was %s\n", win_errstr(wresult));
1000 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
1003 /* Cleanup */
1005 talloc_free(mem_ctx);
1007 return ntresult;
1012 * Process a command entered at the prompt or as part of -c
1014 * @returns The NTSTATUS from running the command.
1016 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
1017 struct cli_state *cli,
1018 struct dcerpc_binding *binding,
1019 char *cmd)
1021 struct cmd_list *temp_list;
1022 NTSTATUS result = NT_STATUS_OK;
1023 int ret;
1024 int argc;
1025 const char **argv = NULL;
1027 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
1028 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
1029 return NT_STATUS_UNSUCCESSFUL;
1033 /* Walk through a dlist of arrays of commands. */
1034 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
1035 struct cmd_set *temp_set = temp_list->cmd_set;
1037 while (temp_set->name) {
1038 if (strequal(argv[0], temp_set->name)) {
1039 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
1040 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
1041 fprintf (stderr, "Invalid command\n");
1042 goto out_free;
1045 result = do_cmd(cli, auth_info, temp_set,
1046 binding, argc, argv);
1048 goto out_free;
1050 temp_set++;
1054 if (argv[0]) {
1055 printf("command not found: %s\n", argv[0]);
1058 out_free:
1059 /* moved to do_cmd()
1060 if (!NT_STATUS_IS_OK(result)) {
1061 printf("result was %s\n", nt_errstr(result));
1065 /* NOTE: popt allocates the whole argv, including the
1066 * strings, as a single block. So a single free is
1067 * enough to release it -- we don't free the
1068 * individual strings. rtfm. */
1069 free(argv);
1071 return result;
1075 /* Main function */
1077 int main(int argc, char *argv[])
1079 const char **const_argv = discard_const_p(const char *, argv);
1080 int opt;
1081 static char *cmdstr = NULL;
1082 const char *server;
1083 struct cli_state *cli = NULL;
1084 static char *opt_ipaddr=NULL;
1085 struct cmd_set **cmd_set;
1086 struct sockaddr_storage server_ss;
1087 NTSTATUS nt_status;
1088 static int opt_port = 0;
1089 int result = 0;
1090 TALLOC_CTX *frame = talloc_stackframe();
1091 uint32_t flags = 0;
1092 struct dcerpc_binding *binding = NULL;
1093 enum dcerpc_transport_t transport;
1094 uint32_t bflags = 0;
1095 const char *binding_string = NULL;
1096 const char *host;
1097 int signing_state = SMB_SIGNING_IPC_DEFAULT;
1099 /* make sure the vars that get altered (4th field) are in
1100 a fixed location or certain compilers complain */
1101 poptContext pc;
1102 struct poptOption long_options[] = {
1103 POPT_AUTOHELP
1104 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
1105 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
1106 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
1107 POPT_COMMON_SAMBA
1108 POPT_COMMON_CONNECTION
1109 POPT_COMMON_CREDENTIALS
1110 POPT_TABLEEND
1113 smb_init_locale();
1115 zero_sockaddr(&server_ss);
1117 setlinebuf(stdout);
1119 /* the following functions are part of the Samba debugging
1120 facilities. See lib/debug.c */
1121 setup_logging("rpcclient", DEBUG_STDOUT);
1122 lp_set_cmdline("log level", "0");
1124 /* Parse options */
1126 pc = poptGetContext("rpcclient", argc, const_argv,
1127 long_options, 0);
1129 poptSetOtherOptionHelp(pc, "[OPTION...] <server>\nOptions:");
1131 if (argc == 1) {
1132 poptPrintHelp(pc, stderr, 0);
1133 goto done;
1136 while((opt = poptGetNextOpt(pc)) != -1) {
1137 switch (opt) {
1139 case 'I':
1140 if (!interpret_string_addr(&server_ss,
1141 opt_ipaddr,
1142 AI_NUMERICHOST)) {
1143 fprintf(stderr, "%s not a valid IP address\n",
1144 opt_ipaddr);
1145 result = 1;
1146 goto done;
1151 /* Get server as remaining unparsed argument. Print usage if more
1152 than one unparsed argument is present. */
1154 server = poptGetArg(pc);
1156 if (!server || poptGetArg(pc)) {
1157 poptPrintHelp(pc, stderr, 0);
1158 result = 1;
1159 goto done;
1162 poptFreeContext(pc);
1163 popt_burn_cmdline_password(argc, argv);
1165 rpcclient_msg_ctx = cmdline_messaging_context(get_dyn_CONFIGFILE());
1167 if (!init_names()) {
1168 result = 1;
1169 goto done;
1173 * Get password
1174 * from stdin if necessary
1177 if ((server[0] == '/' && server[1] == '/') ||
1178 (server[0] == '\\' && server[1] == '\\')) {
1179 server += 2;
1182 nt_status = dcerpc_parse_binding(frame, server, &binding);
1184 if (!NT_STATUS_IS_OK(nt_status)) {
1186 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1187 strip_hostname(server));
1188 if (!binding_string) {
1189 result = 1;
1190 goto done;
1193 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1194 if (!NT_STATUS_IS_OK(nt_status)) {
1195 result = -1;
1196 goto done;
1200 transport = dcerpc_binding_get_transport(binding);
1202 if (transport == NCA_UNKNOWN) {
1203 nt_status = dcerpc_binding_set_transport(binding, NCACN_NP);
1204 if (!NT_STATUS_IS_OK(nt_status)) {
1205 result = -1;
1206 goto done;
1210 host = dcerpc_binding_get_string_option(binding, "host");
1212 bflags = dcerpc_binding_get_flags(binding);
1213 if (bflags & DCERPC_CONNECT) {
1214 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1215 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1217 if (bflags & DCERPC_PACKET) {
1218 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PACKET;
1219 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1221 if (bflags & DCERPC_SIGN) {
1222 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1223 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1225 if (bflags & DCERPC_SEAL) {
1226 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1227 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1229 if (bflags & DCERPC_AUTH_SPNEGO) {
1230 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1231 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1233 if (bflags & DCERPC_AUTH_NTLM) {
1234 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1235 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1236 } else {
1237 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1240 if (bflags & DCERPC_AUTH_KRB5) {
1241 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1242 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1243 } else {
1244 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1247 if (pipe_default_auth_type != DCERPC_AUTH_TYPE_NONE) {
1248 /* If nothing is requested then default to integrity */
1249 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1250 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1254 signing_state = get_cmdline_auth_info_signing_state(
1255 popt_get_cmdline_auth_info());
1256 switch (signing_state) {
1257 case SMB_SIGNING_OFF:
1258 lp_set_cmdline("client ipc signing", "no");
1259 break;
1260 case SMB_SIGNING_REQUIRED:
1261 lp_set_cmdline("client ipc signing", "required");
1262 break;
1265 if (get_cmdline_auth_info_use_kerberos(popt_get_cmdline_auth_info())) {
1266 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1267 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1269 if (get_cmdline_auth_info_use_ccache(popt_get_cmdline_auth_info())) {
1270 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1272 if (get_cmdline_auth_info_use_pw_nt_hash(
1273 popt_get_cmdline_auth_info())) {
1274 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
1277 rpcclient_netlogon_domain = get_cmdline_auth_info_domain(
1278 popt_get_cmdline_auth_info());
1279 if (rpcclient_netlogon_domain == NULL ||
1280 rpcclient_netlogon_domain[0] == '\0')
1282 rpcclient_netlogon_domain = lp_workgroup();
1285 nt_status = cli_full_connection(&cli, lp_netbios_name(), host,
1286 opt_ipaddr ? &server_ss : NULL, opt_port,
1287 "IPC$", "IPC",
1288 get_cmdline_auth_info_username(
1289 popt_get_cmdline_auth_info()),
1290 get_cmdline_auth_info_domain(
1291 popt_get_cmdline_auth_info()),
1292 get_cmdline_auth_info_password(
1293 popt_get_cmdline_auth_info()),
1294 flags,
1295 SMB_SIGNING_IPC_DEFAULT);
1297 if (!NT_STATUS_IS_OK(nt_status)) {
1298 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1299 result = 1;
1300 goto done;
1303 if (get_cmdline_auth_info_smb_encrypt(popt_get_cmdline_auth_info())) {
1304 nt_status = cli_cm_force_encryption(cli,
1305 get_cmdline_auth_info_username(
1306 popt_get_cmdline_auth_info()),
1307 get_cmdline_auth_info_password(
1308 popt_get_cmdline_auth_info()),
1309 get_cmdline_auth_info_domain(
1310 popt_get_cmdline_auth_info()),
1311 "IPC$");
1312 if (!NT_STATUS_IS_OK(nt_status)) {
1313 result = 1;
1314 goto done;
1318 #if 0 /* COMMENT OUT FOR TESTING */
1319 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1320 #endif
1322 /* Load command lists */
1323 rpcclient_cli_state = cli;
1325 timeout = 10000;
1326 cli_set_timeout(cli, timeout);
1328 cmd_set = rpcclient_command_list;
1330 while(*cmd_set) {
1331 add_command_set(*cmd_set);
1332 add_command_set(separator_command);
1333 cmd_set++;
1336 default_transport = dcerpc_binding_get_transport(binding);
1338 fetch_machine_sid(cli);
1340 /* Do anything specified with -c */
1341 if (cmdstr && cmdstr[0]) {
1342 char *cmd;
1343 char *p = cmdstr;
1345 result = 0;
1347 while((cmd=next_command(&p)) != NULL) {
1348 NTSTATUS cmd_result = process_cmd(
1349 popt_get_cmdline_auth_info(),
1350 cli, binding, cmd);
1351 SAFE_FREE(cmd);
1352 result = NT_STATUS_IS_ERR(cmd_result);
1355 goto done;
1358 /* Loop around accepting commands */
1360 while(1) {
1361 char *line = NULL;
1363 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1365 if (line == NULL) {
1366 printf("\n");
1367 break;
1370 if (line[0] != '\n')
1371 process_cmd(popt_get_cmdline_auth_info(), cli,
1372 binding, line);
1373 SAFE_FREE(line);
1376 done:
1377 rpcclient_cli_state = NULL;
1378 if (cli != NULL) {
1379 cli_shutdown(cli);
1381 popt_free_cmdline_auth_info();
1382 netlogon_creds_cli_close_global_db();
1383 TALLOC_FREE(rpcclient_msg_ctx);
1384 TALLOC_FREE(frame);
1385 return result;