selftest pass in srcdir into Samba3 target module
[Samba.git] / source3 / rpcclient / rpcclient.c
blobfc5e518d651cb7a743d55ee7797565302687fcae
1 /*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Martin Pool 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "rpcclient.h"
25 #include "../libcli/auth/libcli_auth.h"
26 #include "../librpc/gen_ndr/ndr_lsa_c.h"
27 #include "rpc_client/cli_lsarpc.h"
28 #include "../librpc/gen_ndr/ndr_netlogon.h"
29 #include "rpc_client/cli_netlogon.h"
30 #include "../libcli/smbreadline/smbreadline.h"
31 #include "../libcli/security/security.h"
33 enum pipe_auth_type_spnego {
34 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
35 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
36 PIPE_AUTH_TYPE_SPNEGO_KRB5
39 struct dom_sid domain_sid;
41 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
42 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
43 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
44 static unsigned int timeout = 0;
45 static enum dcerpc_transport_t default_transport = NCACN_NP;
47 struct user_auth_info *rpcclient_auth_info;
49 /* List to hold groups of commands.
51 * Commands are defined in a list of arrays: arrays are easy to
52 * statically declare, and lists are easier to dynamically extend.
55 static struct cmd_list {
56 struct cmd_list *prev, *next;
57 struct cmd_set *cmd_set;
58 } *cmd_list;
60 /****************************************************************************
61 handle completion of commands for readline
62 ****************************************************************************/
63 static char **completion_fn(const char *text, int start, int end)
65 #define MAX_COMPLETIONS 1000
66 char **matches;
67 int i, count=0;
68 struct cmd_list *commands = cmd_list;
70 #if 0 /* JERRY */
71 /* FIXME!!! -- what to do when completing argument? */
72 /* for words not at the start of the line fallback
73 to filename completion */
74 if (start)
75 return NULL;
76 #endif
78 /* make sure we have a list of valid commands */
79 if (!commands) {
80 return NULL;
83 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
84 if (!matches) {
85 return NULL;
88 matches[count++] = SMB_STRDUP(text);
89 if (!matches[0]) {
90 SAFE_FREE(matches);
91 return NULL;
94 while (commands && count < MAX_COMPLETIONS-1) {
95 if (!commands->cmd_set) {
96 break;
99 for (i=0; commands->cmd_set[i].name; i++) {
100 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
101 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
102 commands->cmd_set[i].ntfn ) ||
103 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
104 commands->cmd_set[i].wfn))) {
105 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
106 if (!matches[count]) {
107 for (i = 0; i < count; i++) {
108 SAFE_FREE(matches[count]);
110 SAFE_FREE(matches);
111 return NULL;
113 count++;
116 commands = commands->next;
119 if (count == 2) {
120 SAFE_FREE(matches[0]);
121 matches[0] = SMB_STRDUP(matches[1]);
123 matches[count] = NULL;
124 return matches;
127 static char *next_command (char **cmdstr)
129 char *command;
130 char *p;
132 if (!cmdstr || !(*cmdstr))
133 return NULL;
135 p = strchr_m(*cmdstr, ';');
136 if (p)
137 *p = '\0';
138 command = SMB_STRDUP(*cmdstr);
139 if (p)
140 *cmdstr = p + 1;
141 else
142 *cmdstr = NULL;
144 return command;
147 /* Fetch the SID for this computer */
149 static void fetch_machine_sid(struct cli_state *cli)
151 struct policy_handle pol;
152 NTSTATUS result = NT_STATUS_OK, status;
153 static bool got_domain_sid;
154 TALLOC_CTX *mem_ctx;
155 struct rpc_pipe_client *lsapipe = NULL;
156 union lsa_PolicyInformation *info = NULL;
157 struct dcerpc_binding_handle *b;
159 if (got_domain_sid) return;
161 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
162 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
163 goto error;
166 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
167 &lsapipe);
168 if (!NT_STATUS_IS_OK(result)) {
169 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
170 goto error;
173 b = lsapipe->binding_handle;
175 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
176 SEC_FLAG_MAXIMUM_ALLOWED,
177 &pol);
178 if (!NT_STATUS_IS_OK(result)) {
179 goto error;
182 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
183 &pol,
184 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
185 &info,
186 &result);
187 if (!NT_STATUS_IS_OK(status)) {
188 result = status;
189 goto error;
191 if (!NT_STATUS_IS_OK(result)) {
192 goto error;
195 got_domain_sid = True;
196 sid_copy(&domain_sid, info->account_domain.sid);
198 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
199 TALLOC_FREE(lsapipe);
200 talloc_destroy(mem_ctx);
202 return;
204 error:
206 if (lsapipe) {
207 TALLOC_FREE(lsapipe);
210 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
212 if (!NT_STATUS_IS_OK(result)) {
213 fprintf(stderr, "error: %s\n", nt_errstr(result));
216 exit(1);
219 /* List the available commands on a given pipe */
221 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
222 int argc, const char **argv)
224 struct cmd_list *tmp;
225 struct cmd_set *tmp_set;
226 int i;
228 /* Usage */
230 if (argc != 2) {
231 printf("Usage: %s <pipe>\n", argv[0]);
232 return NT_STATUS_OK;
235 /* Help on one command */
237 for (tmp = cmd_list; tmp; tmp = tmp->next)
239 tmp_set = tmp->cmd_set;
241 if (!StrCaseCmp(argv[1], tmp_set->name))
243 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
245 i = 0;
246 tmp_set++;
247 while(tmp_set->name) {
248 printf("%30s", tmp_set->name);
249 tmp_set++;
250 i++;
251 if (i%3 == 0)
252 printf("\n");
255 /* drop out of the loop */
256 break;
259 printf("\n\n");
261 return NT_STATUS_OK;
264 /* Display help on commands */
266 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
267 int argc, const char **argv)
269 struct cmd_list *tmp;
270 struct cmd_set *tmp_set;
272 /* Usage */
274 if (argc > 2) {
275 printf("Usage: %s [command]\n", argv[0]);
276 return NT_STATUS_OK;
279 /* Help on one command */
281 if (argc == 2) {
282 for (tmp = cmd_list; tmp; tmp = tmp->next) {
284 tmp_set = tmp->cmd_set;
286 while(tmp_set->name) {
287 if (strequal(argv[1], tmp_set->name)) {
288 if (tmp_set->usage &&
289 tmp_set->usage[0])
290 printf("%s\n", tmp_set->usage);
291 else
292 printf("No help for %s\n", tmp_set->name);
294 return NT_STATUS_OK;
297 tmp_set++;
301 printf("No such command: %s\n", argv[1]);
302 return NT_STATUS_OK;
305 /* List all commands */
307 for (tmp = cmd_list; tmp; tmp = tmp->next) {
309 tmp_set = tmp->cmd_set;
311 while(tmp_set->name) {
313 printf("%15s\t\t%s\n", tmp_set->name,
314 tmp_set->description ? tmp_set->description:
315 "");
317 tmp_set++;
321 return NT_STATUS_OK;
324 /* Change the debug level */
326 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
327 int argc, const char **argv)
329 if (argc > 2) {
330 printf("Usage: %s [debuglevel]\n", argv[0]);
331 return NT_STATUS_OK;
334 if (argc == 2) {
335 lp_set_cmdline("log level", argv[1]);
338 printf("debuglevel is %d\n", DEBUGLEVEL);
340 return NT_STATUS_OK;
343 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
344 int argc, const char **argv)
346 exit(0);
347 return NT_STATUS_OK; /* NOTREACHED */
350 static NTSTATUS cmd_set_ss_level(void)
352 struct cmd_list *tmp;
354 /* Close any existing connections not at this level. */
356 for (tmp = cmd_list; tmp; tmp = tmp->next) {
357 struct cmd_set *tmp_set;
359 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
360 if (tmp_set->rpc_pipe == NULL) {
361 continue;
364 if ((tmp_set->rpc_pipe->auth->auth_type
365 != pipe_default_auth_type)
366 || (tmp_set->rpc_pipe->auth->auth_level
367 != pipe_default_auth_level)) {
368 TALLOC_FREE(tmp_set->rpc_pipe);
369 tmp_set->rpc_pipe = NULL;
373 return NT_STATUS_OK;
376 static NTSTATUS cmd_set_transport(void)
378 struct cmd_list *tmp;
380 /* Close any existing connections not at this level. */
382 for (tmp = cmd_list; tmp; tmp = tmp->next) {
383 struct cmd_set *tmp_set;
385 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
386 if (tmp_set->rpc_pipe == NULL) {
387 continue;
390 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
391 TALLOC_FREE(tmp_set->rpc_pipe);
392 tmp_set->rpc_pipe = NULL;
396 return NT_STATUS_OK;
399 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
400 int argc, const char **argv)
402 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
403 const char *type = "NTLMSSP";
405 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
406 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
408 if (argc > 2) {
409 printf("Usage: %s %s\n", argv[0], p);
410 return NT_STATUS_OK;
413 if (argc == 2) {
414 type = argv[1];
415 if (strequal(type, "KRB5")) {
416 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
417 } else if (strequal(type, "KRB5_SPNEGO")) {
418 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
419 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
420 } else if (strequal(type, "NTLMSSP")) {
421 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
422 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
423 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
424 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
425 } else if (strequal(type, "SCHANNEL")) {
426 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
427 } else {
428 printf("unknown type %s\n", type);
429 printf("Usage: %s %s\n", argv[0], p);
430 return NT_STATUS_INVALID_LEVEL;
434 d_printf("Setting %s - sign\n", type);
436 return cmd_set_ss_level();
439 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
440 int argc, const char **argv)
442 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
443 const char *type = "NTLMSSP";
445 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
446 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
448 if (argc > 2) {
449 printf("Usage: %s %s\n", argv[0], p);
450 return NT_STATUS_OK;
453 if (argc == 2) {
454 type = argv[1];
455 if (strequal(type, "KRB5")) {
456 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
457 } else if (strequal(type, "KRB5_SPNEGO")) {
458 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
459 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
460 } else if (strequal(type, "NTLMSSP")) {
461 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
462 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
463 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
464 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
465 } else if (strequal(type, "SCHANNEL")) {
466 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
467 } else {
468 printf("unknown type %s\n", type);
469 printf("Usage: %s %s\n", argv[0], p);
470 return NT_STATUS_INVALID_LEVEL;
474 d_printf("Setting %s - sign and seal\n", type);
476 return cmd_set_ss_level();
479 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
480 int argc, const char **argv)
482 struct cmd_list *tmp;
484 if (argc > 2) {
485 printf("Usage: %s timeout\n", argv[0]);
486 return NT_STATUS_OK;
489 if (argc == 2) {
490 timeout = atoi(argv[1]);
492 for (tmp = cmd_list; tmp; tmp = tmp->next) {
494 struct cmd_set *tmp_set;
496 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
497 if (tmp_set->rpc_pipe == NULL) {
498 continue;
501 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
506 printf("timeout is %d\n", timeout);
508 return NT_STATUS_OK;
512 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
513 int argc, const char **argv)
515 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
516 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
517 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
519 return cmd_set_ss_level();
522 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
523 int argc, const char **argv)
525 d_printf("Setting schannel - sign and seal\n");
526 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
527 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
529 return cmd_set_ss_level();
532 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
533 int argc, const char **argv)
535 d_printf("Setting schannel - sign only\n");
536 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
537 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
539 return cmd_set_ss_level();
542 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
543 int argc, const char **argv)
545 NTSTATUS status;
547 if (argc != 2) {
548 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
549 return NT_STATUS_OK;
552 if (strequal(argv[1], "NCACN_NP")) {
553 default_transport = NCACN_NP;
554 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
555 default_transport = NCACN_IP_TCP;
556 } else {
557 printf("transport type: %s unknown or not supported\n", argv[1]);
558 return NT_STATUS_NOT_SUPPORTED;
561 status = cmd_set_transport();
562 if (!NT_STATUS_IS_OK(status)) {
563 return status;
566 printf("default transport is now: %s\n", argv[1]);
568 return NT_STATUS_OK;
571 /* Built in rpcclient commands */
573 static struct cmd_set rpcclient_commands[] = {
575 { "GENERAL OPTIONS" },
577 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
578 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
579 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
580 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
581 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
582 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
583 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
584 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
585 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
586 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, NULL, NULL, "Force RPC pipe connections to be sealed with 'schannel'. Assumes valid machine account to this domain controller.", "" },
587 { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'. Assumes valid machine account to this domain controller.", "" },
588 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
589 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
590 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
592 { NULL }
595 static struct cmd_set separator_command[] = {
596 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
597 { NULL }
601 /* Various pipe commands */
603 extern struct cmd_set lsarpc_commands[];
604 extern struct cmd_set samr_commands[];
605 extern struct cmd_set spoolss_commands[];
606 extern struct cmd_set netlogon_commands[];
607 extern struct cmd_set srvsvc_commands[];
608 extern struct cmd_set dfs_commands[];
609 extern struct cmd_set ds_commands[];
610 extern struct cmd_set echo_commands[];
611 extern struct cmd_set epmapper_commands[];
612 extern struct cmd_set shutdown_commands[];
613 extern struct cmd_set test_commands[];
614 extern struct cmd_set wkssvc_commands[];
615 extern struct cmd_set ntsvcs_commands[];
616 extern struct cmd_set drsuapi_commands[];
617 extern struct cmd_set eventlog_commands[];
618 extern struct cmd_set winreg_commands[];
620 static struct cmd_set *rpcclient_command_list[] = {
621 rpcclient_commands,
622 lsarpc_commands,
623 ds_commands,
624 samr_commands,
625 spoolss_commands,
626 netlogon_commands,
627 srvsvc_commands,
628 dfs_commands,
629 echo_commands,
630 epmapper_commands,
631 shutdown_commands,
632 test_commands,
633 wkssvc_commands,
634 ntsvcs_commands,
635 drsuapi_commands,
636 eventlog_commands,
637 winreg_commands,
638 NULL
641 static void add_command_set(struct cmd_set *cmd_set)
643 struct cmd_list *entry;
645 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
646 DEBUG(0, ("out of memory\n"));
647 return;
650 ZERO_STRUCTP(entry);
652 entry->cmd_set = cmd_set;
653 DLIST_ADD(cmd_list, entry);
658 * Call an rpcclient function, passing an argv array.
660 * @param cmd Command to run, as a single string.
662 static NTSTATUS do_cmd(struct cli_state *cli,
663 struct user_auth_info *auth_info,
664 struct cmd_set *cmd_entry,
665 struct dcerpc_binding *binding,
666 int argc, char **argv)
668 NTSTATUS ntresult;
669 WERROR wresult;
671 TALLOC_CTX *mem_ctx;
673 /* Create mem_ctx */
675 if (!(mem_ctx = talloc_init("do_cmd"))) {
676 DEBUG(0, ("talloc_init() failed\n"));
677 return NT_STATUS_NO_MEMORY;
680 /* Open pipe */
682 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
683 switch (pipe_default_auth_type) {
684 case DCERPC_AUTH_TYPE_NONE:
685 ntresult = cli_rpc_pipe_open_noauth_transport(
686 cli, default_transport,
687 cmd_entry->interface,
688 &cmd_entry->rpc_pipe);
689 break;
690 case DCERPC_AUTH_TYPE_SPNEGO:
691 switch (pipe_default_auth_spnego_type) {
692 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
693 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
694 cli, cmd_entry->interface,
695 default_transport,
696 pipe_default_auth_level,
697 get_cmdline_auth_info_domain(auth_info),
698 get_cmdline_auth_info_username(auth_info),
699 get_cmdline_auth_info_password(auth_info),
700 &cmd_entry->rpc_pipe);
701 break;
702 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
703 ntresult = cli_rpc_pipe_open_spnego_krb5(
704 cli, cmd_entry->interface,
705 default_transport,
706 pipe_default_auth_level,
707 cli->desthost,
708 NULL, NULL,
709 &cmd_entry->rpc_pipe);
710 break;
711 default:
712 ntresult = NT_STATUS_INTERNAL_ERROR;
714 break;
715 case DCERPC_AUTH_TYPE_NTLMSSP:
716 ntresult = cli_rpc_pipe_open_ntlmssp(
717 cli, cmd_entry->interface,
718 default_transport,
719 pipe_default_auth_level,
720 get_cmdline_auth_info_domain(auth_info),
721 get_cmdline_auth_info_username(auth_info),
722 get_cmdline_auth_info_password(auth_info),
723 &cmd_entry->rpc_pipe);
724 break;
725 case DCERPC_AUTH_TYPE_SCHANNEL:
726 ntresult = cli_rpc_pipe_open_schannel(
727 cli, cmd_entry->interface,
728 default_transport,
729 pipe_default_auth_level,
730 get_cmdline_auth_info_domain(auth_info),
731 &cmd_entry->rpc_pipe);
732 break;
733 case DCERPC_AUTH_TYPE_KRB5:
734 ntresult = cli_rpc_pipe_open_krb5(
735 cli, cmd_entry->interface,
736 default_transport,
737 pipe_default_auth_level,
738 cli->desthost,
739 NULL, NULL,
740 &cmd_entry->rpc_pipe);
741 break;
742 default:
743 DEBUG(0, ("Could not initialise %s. Invalid "
744 "auth type %u\n",
745 get_pipe_name_from_syntax(
746 talloc_tos(),
747 cmd_entry->interface),
748 pipe_default_auth_type ));
749 return NT_STATUS_UNSUCCESSFUL;
751 if (!NT_STATUS_IS_OK(ntresult)) {
752 DEBUG(0, ("Could not initialise %s. Error was %s\n",
753 get_pipe_name_from_syntax(
754 talloc_tos(), cmd_entry->interface),
755 nt_errstr(ntresult) ));
756 return ntresult;
759 if (ndr_syntax_id_equal(cmd_entry->interface,
760 &ndr_table_netlogon.syntax_id)) {
761 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
762 enum netr_SchannelType sec_channel_type;
763 uchar trust_password[16];
764 const char *machine_account;
766 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
767 trust_password, &machine_account,
768 &sec_channel_type))
770 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
773 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
774 cli->desthost, /* server name */
775 get_cmdline_auth_info_domain(auth_info), /* domain */
776 global_myname(), /* client name */
777 machine_account, /* machine account name */
778 trust_password,
779 sec_channel_type,
780 &neg_flags);
782 if (!NT_STATUS_IS_OK(ntresult)) {
783 DEBUG(0, ("Could not initialise credentials for %s.\n",
784 get_pipe_name_from_syntax(
785 talloc_tos(),
786 cmd_entry->interface)));
787 return ntresult;
792 /* Run command */
794 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
795 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
796 if (!NT_STATUS_IS_OK(ntresult)) {
797 printf("result was %s\n", nt_errstr(ntresult));
799 } else {
800 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
801 /* print out the DOS error */
802 if (!W_ERROR_IS_OK(wresult)) {
803 printf( "result was %s\n", win_errstr(wresult));
805 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
808 /* Cleanup */
810 talloc_destroy(mem_ctx);
812 return ntresult;
817 * Process a command entered at the prompt or as part of -c
819 * @returns The NTSTATUS from running the command.
821 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
822 struct cli_state *cli,
823 struct dcerpc_binding *binding,
824 char *cmd)
826 struct cmd_list *temp_list;
827 NTSTATUS result = NT_STATUS_OK;
828 int ret;
829 int argc;
830 char **argv = NULL;
832 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
833 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
834 return NT_STATUS_UNSUCCESSFUL;
838 /* Walk through a dlist of arrays of commands. */
839 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
840 struct cmd_set *temp_set = temp_list->cmd_set;
842 while (temp_set->name) {
843 if (strequal(argv[0], temp_set->name)) {
844 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
845 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
846 fprintf (stderr, "Invalid command\n");
847 goto out_free;
850 result = do_cmd(cli, auth_info, temp_set,
851 binding, argc, argv);
853 goto out_free;
855 temp_set++;
859 if (argv[0]) {
860 printf("command not found: %s\n", argv[0]);
863 out_free:
864 /* moved to do_cmd()
865 if (!NT_STATUS_IS_OK(result)) {
866 printf("result was %s\n", nt_errstr(result));
870 /* NOTE: popt allocates the whole argv, including the
871 * strings, as a single block. So a single free is
872 * enough to release it -- we don't free the
873 * individual strings. rtfm. */
874 free(argv);
876 return result;
880 /* Main function */
882 int main(int argc, char *argv[])
884 int opt;
885 static char *cmdstr = NULL;
886 const char *server;
887 struct cli_state *cli = NULL;
888 static char *opt_ipaddr=NULL;
889 struct cmd_set **cmd_set;
890 struct sockaddr_storage server_ss;
891 NTSTATUS nt_status;
892 static int opt_port = 0;
893 fstring new_workgroup;
894 int result = 0;
895 TALLOC_CTX *frame = talloc_stackframe();
896 uint32_t flags = 0;
897 struct dcerpc_binding *binding = NULL;
898 const char *binding_string = NULL;
899 char *user, *domain, *q;
901 /* make sure the vars that get altered (4th field) are in
902 a fixed location or certain compilers complain */
903 poptContext pc;
904 struct poptOption long_options[] = {
905 POPT_AUTOHELP
906 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
907 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
908 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
909 POPT_COMMON_SAMBA
910 POPT_COMMON_CONNECTION
911 POPT_COMMON_CREDENTIALS
912 POPT_TABLEEND
915 load_case_tables();
917 zero_sockaddr(&server_ss);
919 setlinebuf(stdout);
921 /* the following functions are part of the Samba debugging
922 facilities. See lib/debug.c */
923 setup_logging("rpcclient", DEBUG_STDOUT);
925 rpcclient_auth_info = user_auth_info_init(frame);
926 if (rpcclient_auth_info == NULL) {
927 exit(1);
929 popt_common_set_auth_info(rpcclient_auth_info);
931 /* Parse options */
933 pc = poptGetContext("rpcclient", argc, (const char **) argv,
934 long_options, 0);
936 if (argc == 1) {
937 poptPrintHelp(pc, stderr, 0);
938 goto done;
941 while((opt = poptGetNextOpt(pc)) != -1) {
942 switch (opt) {
944 case 'I':
945 if (!interpret_string_addr(&server_ss,
946 opt_ipaddr,
947 AI_NUMERICHOST)) {
948 fprintf(stderr, "%s not a valid IP address\n",
949 opt_ipaddr);
950 result = 1;
951 goto done;
956 /* Get server as remaining unparsed argument. Print usage if more
957 than one unparsed argument is present. */
959 server = poptGetArg(pc);
961 if (!server || poptGetArg(pc)) {
962 poptPrintHelp(pc, stderr, 0);
963 result = 1;
964 goto done;
967 poptFreeContext(pc);
969 load_interfaces();
971 if (!init_names()) {
972 result = 1;
973 goto done;
976 /* save the workgroup...
978 FIXME!! do we need to do this for other options as well
979 (or maybe a generic way to keep lp_load() from overwriting
980 everything)? */
982 fstrcpy( new_workgroup, lp_workgroup() );
984 /* Load smb.conf file */
986 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
987 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
989 if ( strlen(new_workgroup) != 0 )
990 set_global_myworkgroup( new_workgroup );
993 * Get password
994 * from stdin if necessary
997 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
998 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
999 result = 1;
1000 goto done;
1003 set_cmdline_auth_info_getpass(rpcclient_auth_info);
1005 if ((server[0] == '/' && server[1] == '/') ||
1006 (server[0] == '\\' && server[1] == '\\')) {
1007 server += 2;
1010 nt_status = dcerpc_parse_binding(frame, server, &binding);
1012 if (!NT_STATUS_IS_OK(nt_status)) {
1014 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1015 strip_hostname(server));
1016 if (!binding_string) {
1017 result = 1;
1018 goto done;
1021 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1022 if (!NT_STATUS_IS_OK(nt_status)) {
1023 result = -1;
1024 goto done;
1028 if (binding->transport == NCA_UNKNOWN) {
1029 binding->transport = NCACN_NP;
1032 if (binding->flags & DCERPC_SIGN) {
1033 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1034 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1036 if (binding->flags & DCERPC_SEAL) {
1037 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1038 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1040 if (binding->flags & DCERPC_AUTH_SPNEGO) {
1041 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1042 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1044 if (binding->flags & DCERPC_AUTH_NTLM) {
1045 /* If neither Integrity or Privacy are requested then
1046 * Use just Connect level */
1047 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1048 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1051 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1052 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1053 } else {
1054 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1057 if (binding->flags & DCERPC_AUTH_KRB5) {
1058 /* If neither Integrity or Privacy are requested then
1059 * Use just Connect level */
1060 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1061 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1064 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1065 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1066 } else {
1067 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1071 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1072 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1073 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1075 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1076 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1079 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1080 SMB_ASSERT(user != NULL);
1081 domain = talloc_strdup(frame, lp_workgroup());
1082 SMB_ASSERT(domain != NULL);
1083 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1085 if ((q = strchr_m(user,'\\'))) {
1086 *q = 0;
1087 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1088 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1092 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1093 opt_ipaddr ? &server_ss : NULL, opt_port,
1094 "IPC$", "IPC",
1095 get_cmdline_auth_info_username(rpcclient_auth_info),
1096 get_cmdline_auth_info_domain(rpcclient_auth_info),
1097 get_cmdline_auth_info_password(rpcclient_auth_info),
1098 flags,
1099 get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1101 if (!NT_STATUS_IS_OK(nt_status)) {
1102 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1103 result = 1;
1104 goto done;
1107 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1108 nt_status = cli_cm_force_encryption(cli,
1109 get_cmdline_auth_info_username(rpcclient_auth_info),
1110 get_cmdline_auth_info_password(rpcclient_auth_info),
1111 get_cmdline_auth_info_domain(rpcclient_auth_info),
1112 "IPC$");
1113 if (!NT_STATUS_IS_OK(nt_status)) {
1114 result = 1;
1115 goto done;
1119 #if 0 /* COMMENT OUT FOR TESTING */
1120 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1121 #endif
1123 /* Load command lists */
1125 timeout = cli_set_timeout(cli, 10000);
1127 cmd_set = rpcclient_command_list;
1129 while(*cmd_set) {
1130 add_command_set(*cmd_set);
1131 add_command_set(separator_command);
1132 cmd_set++;
1135 default_transport = binding->transport;
1137 fetch_machine_sid(cli);
1139 /* Do anything specified with -c */
1140 if (cmdstr && cmdstr[0]) {
1141 char *cmd;
1142 char *p = cmdstr;
1144 result = 0;
1146 while((cmd=next_command(&p)) != NULL) {
1147 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1148 binding, cmd);
1149 SAFE_FREE(cmd);
1150 result = NT_STATUS_IS_ERR(cmd_result);
1153 goto done;
1156 /* Loop around accepting commands */
1158 while(1) {
1159 char *line = NULL;
1161 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1163 if (line == NULL)
1164 break;
1166 if (line[0] != '\n')
1167 process_cmd(rpcclient_auth_info, cli, binding, line);
1168 SAFE_FREE(line);
1171 done:
1172 if (cli != NULL) {
1173 cli_shutdown(cli);
1175 TALLOC_FREE(frame);
1176 return result;