VERSION: Bump version up to 4.4.8...
[Samba.git] / source3 / rpcclient / rpcclient.c
blob74ae7082030f28085b4d91c372c234d1a09be42d
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"
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"
38 enum pipe_auth_type_spnego {
39 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
40 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
41 PIPE_AUTH_TYPE_SPNEGO_KRB5
44 struct dom_sid domain_sid;
46 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
47 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
48 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
49 static unsigned int timeout = 0;
50 static enum dcerpc_transport_t default_transport = NCACN_NP;
52 struct messaging_context *rpcclient_msg_ctx;
53 struct user_auth_info *rpcclient_auth_info;
54 struct cli_state *rpcclient_cli_state;
55 struct netlogon_creds_cli_context *rpcclient_netlogon_creds;
57 /* List to hold groups of commands.
59 * Commands are defined in a list of arrays: arrays are easy to
60 * statically declare, and lists are easier to dynamically extend.
63 static struct cmd_list {
64 struct cmd_list *prev, *next;
65 struct cmd_set *cmd_set;
66 } *cmd_list;
68 /****************************************************************************
69 handle completion of commands for readline
70 ****************************************************************************/
71 static char **completion_fn(const char *text, int start, int end)
73 #define MAX_COMPLETIONS 1000
74 char **matches;
75 int i, count=0;
76 struct cmd_list *commands = cmd_list;
78 #if 0 /* JERRY */
79 /* FIXME!!! -- what to do when completing argument? */
80 /* for words not at the start of the line fallback
81 to filename completion */
82 if (start)
83 return NULL;
84 #endif
86 /* make sure we have a list of valid commands */
87 if (!commands) {
88 return NULL;
91 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
92 if (!matches) {
93 return NULL;
96 matches[count++] = SMB_STRDUP(text);
97 if (!matches[0]) {
98 SAFE_FREE(matches);
99 return NULL;
102 while (commands && count < MAX_COMPLETIONS-1) {
103 if (!commands->cmd_set) {
104 break;
107 for (i=0; commands->cmd_set[i].name; i++) {
108 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
109 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
110 commands->cmd_set[i].ntfn ) ||
111 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
112 commands->cmd_set[i].wfn))) {
113 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
114 if (!matches[count]) {
115 for (i = 0; i < count; i++) {
116 SAFE_FREE(matches[count]);
118 SAFE_FREE(matches);
119 return NULL;
121 count++;
124 commands = commands->next;
127 if (count == 2) {
128 SAFE_FREE(matches[0]);
129 matches[0] = SMB_STRDUP(matches[1]);
131 matches[count] = NULL;
132 return matches;
135 static char *next_command (char **cmdstr)
137 char *command;
138 char *p;
140 if (!cmdstr || !(*cmdstr))
141 return NULL;
143 p = strchr_m(*cmdstr, ';');
144 if (p)
145 *p = '\0';
146 command = SMB_STRDUP(*cmdstr);
147 if (p)
148 *cmdstr = p + 1;
149 else
150 *cmdstr = NULL;
152 return command;
155 /* Fetch the SID for this computer */
157 static void fetch_machine_sid(struct cli_state *cli)
159 struct policy_handle pol;
160 NTSTATUS result = NT_STATUS_OK, status;
161 static bool got_domain_sid;
162 TALLOC_CTX *mem_ctx;
163 struct rpc_pipe_client *lsapipe = NULL;
164 union lsa_PolicyInformation *info = NULL;
165 struct dcerpc_binding_handle *b;
167 if (got_domain_sid) return;
169 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
170 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
171 goto error;
174 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
175 &lsapipe);
176 if (!NT_STATUS_IS_OK(result)) {
177 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
178 goto error;
181 b = lsapipe->binding_handle;
183 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
184 SEC_FLAG_MAXIMUM_ALLOWED,
185 &pol);
186 if (!NT_STATUS_IS_OK(result)) {
187 goto error;
190 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
191 &pol,
192 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
193 &info,
194 &result);
195 if (!NT_STATUS_IS_OK(status)) {
196 result = status;
197 goto error;
199 if (!NT_STATUS_IS_OK(result)) {
200 goto error;
203 got_domain_sid = True;
204 sid_copy(&domain_sid, info->account_domain.sid);
206 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
207 TALLOC_FREE(lsapipe);
208 talloc_destroy(mem_ctx);
210 return;
212 error:
214 if (lsapipe) {
215 TALLOC_FREE(lsapipe);
218 fprintf(stderr, "could not obtain sid from server\n");
220 if (!NT_STATUS_IS_OK(result)) {
221 fprintf(stderr, "error: %s\n", nt_errstr(result));
224 exit(1);
227 /* List the available commands on a given pipe */
229 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
230 int argc, const char **argv)
232 struct cmd_list *tmp;
233 struct cmd_set *tmp_set;
234 int i;
236 /* Usage */
238 if (argc != 2) {
239 printf("Usage: %s <pipe>\n", argv[0]);
240 return NT_STATUS_OK;
243 /* Help on one command */
245 for (tmp = cmd_list; tmp; tmp = tmp->next)
247 tmp_set = tmp->cmd_set;
249 if (!strcasecmp_m(argv[1], tmp_set->name))
251 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
253 i = 0;
254 tmp_set++;
255 while(tmp_set->name) {
256 printf("%30s", tmp_set->name);
257 tmp_set++;
258 i++;
259 if (i%3 == 0)
260 printf("\n");
263 /* drop out of the loop */
264 break;
267 printf("\n\n");
269 return NT_STATUS_OK;
272 /* Display help on commands */
274 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
275 int argc, const char **argv)
277 struct cmd_list *tmp;
278 struct cmd_set *tmp_set;
280 /* Usage */
282 if (argc > 2) {
283 printf("Usage: %s [command]\n", argv[0]);
284 return NT_STATUS_OK;
287 /* Help on one command */
289 if (argc == 2) {
290 for (tmp = cmd_list; tmp; tmp = tmp->next) {
292 tmp_set = tmp->cmd_set;
294 while(tmp_set->name) {
295 if (strequal(argv[1], tmp_set->name)) {
296 if (tmp_set->usage &&
297 tmp_set->usage[0])
298 printf("%s\n", tmp_set->usage);
299 else
300 printf("No help for %s\n", tmp_set->name);
302 return NT_STATUS_OK;
305 tmp_set++;
309 printf("No such command: %s\n", argv[1]);
310 return NT_STATUS_OK;
313 /* List all commands */
315 for (tmp = cmd_list; tmp; tmp = tmp->next) {
317 tmp_set = tmp->cmd_set;
319 while(tmp_set->name) {
321 printf("%15s\t\t%s\n", tmp_set->name,
322 tmp_set->description ? tmp_set->description:
323 "");
325 tmp_set++;
329 return NT_STATUS_OK;
332 /* Change the debug level */
334 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
335 int argc, const char **argv)
337 if (argc > 2) {
338 printf("Usage: %s [debuglevel]\n", argv[0]);
339 return NT_STATUS_OK;
342 if (argc == 2) {
343 lp_set_cmdline("log level", argv[1]);
346 printf("debuglevel is %d\n", DEBUGLEVEL);
348 return NT_STATUS_OK;
351 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
352 int argc, const char **argv)
354 exit(0);
355 return NT_STATUS_OK; /* NOTREACHED */
358 static NTSTATUS cmd_set_ss_level(void)
360 struct cmd_list *tmp;
362 /* Close any existing connections not at this level. */
364 for (tmp = cmd_list; tmp; tmp = tmp->next) {
365 struct cmd_set *tmp_set;
367 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
368 if (tmp_set->rpc_pipe == NULL) {
369 continue;
372 if ((tmp_set->rpc_pipe->auth->auth_type
373 != pipe_default_auth_type)
374 || (tmp_set->rpc_pipe->auth->auth_level
375 != pipe_default_auth_level)) {
376 TALLOC_FREE(tmp_set->rpc_pipe);
377 tmp_set->rpc_pipe = NULL;
381 return NT_STATUS_OK;
384 static NTSTATUS cmd_set_transport(void)
386 struct cmd_list *tmp;
388 /* Close any existing connections not at this level. */
390 for (tmp = cmd_list; tmp; tmp = tmp->next) {
391 struct cmd_set *tmp_set;
393 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
394 if (tmp_set->rpc_pipe == NULL) {
395 continue;
398 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
399 TALLOC_FREE(tmp_set->rpc_pipe);
400 tmp_set->rpc_pipe = NULL;
404 return NT_STATUS_OK;
407 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
408 int argc, const char **argv)
410 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
411 const char *type = "NTLMSSP";
413 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
414 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
416 if (argc > 2) {
417 printf("Usage: %s %s\n", argv[0], p);
418 return NT_STATUS_OK;
421 if (argc == 2) {
422 type = argv[1];
423 if (strequal(type, "KRB5")) {
424 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
425 } else if (strequal(type, "KRB5_SPNEGO")) {
426 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
427 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
428 } else if (strequal(type, "NTLMSSP")) {
429 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
430 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
431 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
432 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
433 } else if (strequal(type, "SCHANNEL")) {
434 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
435 } else {
436 printf("unknown type %s\n", type);
437 printf("Usage: %s %s\n", argv[0], p);
438 return NT_STATUS_INVALID_LEVEL;
442 d_printf("Setting %s - sign\n", type);
444 return cmd_set_ss_level();
447 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
448 int argc, const char **argv)
450 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
451 const char *type = "NTLMSSP";
453 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
454 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
456 if (argc > 2) {
457 printf("Usage: %s %s\n", argv[0], p);
458 return NT_STATUS_OK;
461 if (argc == 2) {
462 type = argv[1];
463 if (strequal(type, "KRB5")) {
464 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
465 } else if (strequal(type, "KRB5_SPNEGO")) {
466 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
467 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
468 } else if (strequal(type, "NTLMSSP")) {
469 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
470 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
471 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
472 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
473 } else if (strequal(type, "SCHANNEL")) {
474 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
475 } else {
476 printf("unknown type %s\n", type);
477 printf("Usage: %s %s\n", argv[0], p);
478 return NT_STATUS_INVALID_LEVEL;
482 d_printf("Setting %s - sign and seal\n", type);
484 return cmd_set_ss_level();
487 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
488 int argc, const char **argv)
490 if (argc > 2) {
491 printf("Usage: %s timeout\n", argv[0]);
492 return NT_STATUS_OK;
495 if (argc == 2) {
496 timeout = atoi(argv[1]);
499 printf("timeout is %d\n", timeout);
501 return NT_STATUS_OK;
505 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
506 int argc, const char **argv)
508 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
509 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
510 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
512 return cmd_set_ss_level();
515 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
516 int argc, const char **argv)
518 d_printf("Setting schannel - sign and seal\n");
519 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
520 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
522 return cmd_set_ss_level();
525 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
526 int argc, const char **argv)
528 d_printf("Setting schannel - sign only\n");
529 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
530 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
532 return cmd_set_ss_level();
535 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
536 int argc, const char **argv)
538 NTSTATUS status;
540 if (argc != 2) {
541 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
542 return NT_STATUS_OK;
545 if (strequal(argv[1], "NCACN_NP")) {
546 default_transport = NCACN_NP;
547 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
548 default_transport = NCACN_IP_TCP;
549 } else {
550 printf("transport type: %s unknown or not supported\n", argv[1]);
551 return NT_STATUS_NOT_SUPPORTED;
554 status = cmd_set_transport();
555 if (!NT_STATUS_IS_OK(status)) {
556 return status;
559 printf("default transport is now: %s\n", argv[1]);
561 return NT_STATUS_OK;
564 /* Built in rpcclient commands */
566 static struct cmd_set rpcclient_commands[] = {
568 { "GENERAL OPTIONS" },
570 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
571 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
572 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
573 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
574 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
575 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
576 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
577 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
578 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
579 { "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.", "" },
580 { "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.", "" },
581 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
582 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
583 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
585 { NULL }
588 static struct cmd_set separator_command[] = {
589 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
590 { NULL }
594 /* Various pipe commands */
596 extern struct cmd_set lsarpc_commands[];
597 extern struct cmd_set samr_commands[];
598 extern struct cmd_set spoolss_commands[];
599 extern struct cmd_set netlogon_commands[];
600 extern struct cmd_set srvsvc_commands[];
601 extern struct cmd_set dfs_commands[];
602 extern struct cmd_set ds_commands[];
603 extern struct cmd_set echo_commands[];
604 extern struct cmd_set epmapper_commands[];
605 extern struct cmd_set shutdown_commands[];
606 extern struct cmd_set test_commands[];
607 extern struct cmd_set wkssvc_commands[];
608 extern struct cmd_set ntsvcs_commands[];
609 extern struct cmd_set drsuapi_commands[];
610 extern struct cmd_set eventlog_commands[];
611 extern struct cmd_set winreg_commands[];
612 extern struct cmd_set fss_commands[];
613 extern struct cmd_set witness_commands[];
614 extern struct cmd_set clusapi_commands[];
616 static struct cmd_set *rpcclient_command_list[] = {
617 rpcclient_commands,
618 lsarpc_commands,
619 ds_commands,
620 samr_commands,
621 spoolss_commands,
622 netlogon_commands,
623 srvsvc_commands,
624 dfs_commands,
625 echo_commands,
626 epmapper_commands,
627 shutdown_commands,
628 test_commands,
629 wkssvc_commands,
630 ntsvcs_commands,
631 drsuapi_commands,
632 eventlog_commands,
633 winreg_commands,
634 fss_commands,
635 witness_commands,
636 clusapi_commands,
637 NULL
640 static void add_command_set(struct cmd_set *cmd_set)
642 struct cmd_list *entry;
644 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
645 DEBUG(0, ("out of memory\n"));
646 return;
649 ZERO_STRUCTP(entry);
651 entry->cmd_set = cmd_set;
652 DLIST_ADD(cmd_list, entry);
657 * Call an rpcclient function, passing an argv array.
659 * @param cmd Command to run, as a single string.
661 static NTSTATUS do_cmd(struct cli_state *cli,
662 struct user_auth_info *auth_info,
663 struct cmd_set *cmd_entry,
664 struct dcerpc_binding *binding,
665 int argc, const char **argv)
667 NTSTATUS ntresult;
668 WERROR wresult;
670 TALLOC_CTX *mem_ctx;
672 /* Create mem_ctx */
674 if (!(mem_ctx = talloc_stackframe())) {
675 DEBUG(0, ("talloc_init() failed\n"));
676 return NT_STATUS_NO_MEMORY;
679 /* Open pipe */
681 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
682 enum credentials_use_kerberos use_kerberos = CRED_AUTO_USE_KERBEROS;
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->table,
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 use_kerberos = CRED_DONT_USE_KERBEROS;
694 break;
695 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
696 use_kerberos = CRED_MUST_USE_KERBEROS;
697 break;
698 case PIPE_AUTH_TYPE_SPNEGO_NONE:
699 use_kerberos = CRED_AUTO_USE_KERBEROS;
700 break;
702 /* Fall through */
703 case DCERPC_AUTH_TYPE_NTLMSSP:
704 case DCERPC_AUTH_TYPE_KRB5:
705 ntresult = cli_rpc_pipe_open_generic_auth(
706 cli, cmd_entry->table,
707 default_transport,
708 use_kerberos,
709 pipe_default_auth_type,
710 pipe_default_auth_level,
711 smbXcli_conn_remote_name(cli->conn),
712 get_cmdline_auth_info_domain(auth_info),
713 get_cmdline_auth_info_username(auth_info),
714 get_cmdline_auth_info_password(auth_info),
715 &cmd_entry->rpc_pipe);
716 break;
717 case DCERPC_AUTH_TYPE_SCHANNEL:
718 TALLOC_FREE(rpcclient_netlogon_creds);
719 ntresult = cli_rpc_pipe_open_schannel(
720 cli, rpcclient_msg_ctx,
721 cmd_entry->table,
722 default_transport,
723 get_cmdline_auth_info_domain(auth_info),
724 &cmd_entry->rpc_pipe,
725 talloc_autofree_context(),
726 &rpcclient_netlogon_creds);
727 break;
728 default:
729 DEBUG(0, ("Could not initialise %s. Invalid "
730 "auth type %u\n",
731 cmd_entry->table->name,
732 pipe_default_auth_type ));
733 talloc_free(mem_ctx);
734 return NT_STATUS_UNSUCCESSFUL;
736 if (!NT_STATUS_IS_OK(ntresult)) {
737 DEBUG(0, ("Could not initialise %s. Error was %s\n",
738 cmd_entry->table->name,
739 nt_errstr(ntresult) ));
740 talloc_free(mem_ctx);
741 return ntresult;
744 if (rpcclient_netlogon_creds == NULL && cmd_entry->use_netlogon_creds) {
745 const char *dc_name = cmd_entry->rpc_pipe->desthost;
746 const char *domain = get_cmdline_auth_info_domain(auth_info);
747 struct cli_credentials *creds = NULL;
749 ntresult = pdb_get_trust_credentials(domain, NULL,
750 mem_ctx, &creds);
751 if (!NT_STATUS_IS_OK(ntresult)) {
752 DEBUG(0, ("Failed to fetch trust credentials for "
753 "%s to connect to %s: %s\n",
754 domain, cmd_entry->table->name,
755 nt_errstr(ntresult)));
756 TALLOC_FREE(cmd_entry->rpc_pipe);
757 talloc_free(mem_ctx);
758 return ntresult;
761 ntresult = rpccli_create_netlogon_creds_with_creds(creds,
762 dc_name,
763 rpcclient_msg_ctx,
764 talloc_autofree_context(),
765 &rpcclient_netlogon_creds);
766 if (!NT_STATUS_IS_OK(ntresult)) {
767 DEBUG(0, ("Could not initialise credentials for %s.\n",
768 cmd_entry->table->name));
769 TALLOC_FREE(cmd_entry->rpc_pipe);
770 TALLOC_FREE(mem_ctx);
771 return ntresult;
774 ntresult = rpccli_setup_netlogon_creds_with_creds(cli,
775 NCACN_NP,
776 rpcclient_netlogon_creds,
777 false, /* force_reauth */
778 creds);
779 TALLOC_FREE(creds);
780 if (!NT_STATUS_IS_OK(ntresult)) {
781 DEBUG(0, ("Could not initialise credentials for %s.\n",
782 cmd_entry->table->name));
783 TALLOC_FREE(cmd_entry->rpc_pipe);
784 TALLOC_FREE(rpcclient_netlogon_creds);
785 TALLOC_FREE(mem_ctx);
786 return ntresult;
791 /* Set timeout for new connections */
792 if (cmd_entry->rpc_pipe) {
793 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
796 /* Run command */
798 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
799 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
800 if (!NT_STATUS_IS_OK(ntresult)) {
801 printf("result was %s\n", nt_errstr(ntresult));
803 } else {
804 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
805 /* print out the DOS error */
806 if (!W_ERROR_IS_OK(wresult)) {
807 printf( "result was %s\n", win_errstr(wresult));
809 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
812 /* Cleanup */
814 talloc_free(mem_ctx);
816 return ntresult;
821 * Process a command entered at the prompt or as part of -c
823 * @returns The NTSTATUS from running the command.
825 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
826 struct cli_state *cli,
827 struct dcerpc_binding *binding,
828 char *cmd)
830 struct cmd_list *temp_list;
831 NTSTATUS result = NT_STATUS_OK;
832 int ret;
833 int argc;
834 const char **argv = NULL;
836 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
837 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
838 return NT_STATUS_UNSUCCESSFUL;
842 /* Walk through a dlist of arrays of commands. */
843 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
844 struct cmd_set *temp_set = temp_list->cmd_set;
846 while (temp_set->name) {
847 if (strequal(argv[0], temp_set->name)) {
848 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
849 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
850 fprintf (stderr, "Invalid command\n");
851 goto out_free;
854 result = do_cmd(cli, auth_info, temp_set,
855 binding, argc, argv);
857 goto out_free;
859 temp_set++;
863 if (argv[0]) {
864 printf("command not found: %s\n", argv[0]);
867 out_free:
868 /* moved to do_cmd()
869 if (!NT_STATUS_IS_OK(result)) {
870 printf("result was %s\n", nt_errstr(result));
874 /* NOTE: popt allocates the whole argv, including the
875 * strings, as a single block. So a single free is
876 * enough to release it -- we don't free the
877 * individual strings. rtfm. */
878 free(argv);
880 return result;
884 /* Main function */
886 int main(int argc, char *argv[])
888 const char **const_argv = discard_const_p(const char *, argv);
889 int opt;
890 static char *cmdstr = NULL;
891 const char *server;
892 struct cli_state *cli = NULL;
893 static char *opt_ipaddr=NULL;
894 struct cmd_set **cmd_set;
895 struct sockaddr_storage server_ss;
896 NTSTATUS nt_status;
897 static int opt_port = 0;
898 int result = 0;
899 TALLOC_CTX *frame = talloc_stackframe();
900 uint32_t flags = 0;
901 struct dcerpc_binding *binding = NULL;
902 enum dcerpc_transport_t transport;
903 uint32_t bflags = 0;
904 const char *binding_string = NULL;
905 char *user, *domain, *q;
906 const char *host;
907 int signing_state = SMB_SIGNING_IPC_DEFAULT;
909 /* make sure the vars that get altered (4th field) are in
910 a fixed location or certain compilers complain */
911 poptContext pc;
912 struct poptOption long_options[] = {
913 POPT_AUTOHELP
914 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
915 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
916 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
917 POPT_COMMON_SAMBA
918 POPT_COMMON_CONNECTION
919 POPT_COMMON_CREDENTIALS
920 POPT_TABLEEND
923 smb_init_locale();
925 zero_sockaddr(&server_ss);
927 setlinebuf(stdout);
929 /* the following functions are part of the Samba debugging
930 facilities. See lib/debug.c */
931 setup_logging("rpcclient", DEBUG_STDOUT);
932 lp_set_cmdline("log level", "0");
934 rpcclient_auth_info = user_auth_info_init(frame);
935 if (rpcclient_auth_info == NULL) {
936 exit(1);
938 popt_common_set_auth_info(rpcclient_auth_info);
940 /* Parse options */
942 pc = poptGetContext("rpcclient", argc, const_argv,
943 long_options, 0);
945 if (argc == 1) {
946 poptPrintHelp(pc, stderr, 0);
947 goto done;
950 while((opt = poptGetNextOpt(pc)) != -1) {
951 switch (opt) {
953 case 'I':
954 if (!interpret_string_addr(&server_ss,
955 opt_ipaddr,
956 AI_NUMERICHOST)) {
957 fprintf(stderr, "%s not a valid IP address\n",
958 opt_ipaddr);
959 result = 1;
960 goto done;
965 /* Get server as remaining unparsed argument. Print usage if more
966 than one unparsed argument is present. */
968 server = poptGetArg(pc);
970 if (!server || poptGetArg(pc)) {
971 poptPrintHelp(pc, stderr, 0);
972 result = 1;
973 goto done;
976 poptFreeContext(pc);
977 popt_burn_cmdline_password(argc, argv);
979 if (!init_names()) {
980 result = 1;
981 goto done;
984 /* Load smb.conf file */
986 if (!lp_load_global(get_dyn_CONFIGFILE()))
987 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
989 /* We must load interfaces after we load the smb.conf */
990 load_interfaces();
992 rpcclient_msg_ctx = messaging_init(talloc_autofree_context(),
993 samba_tevent_context_init(talloc_autofree_context()));
996 * Get password
997 * from stdin if necessary
1000 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
1001 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
1002 result = 1;
1003 goto done;
1006 set_cmdline_auth_info_getpass(rpcclient_auth_info);
1008 if ((server[0] == '/' && server[1] == '/') ||
1009 (server[0] == '\\' && server[1] == '\\')) {
1010 server += 2;
1013 nt_status = dcerpc_parse_binding(frame, server, &binding);
1015 if (!NT_STATUS_IS_OK(nt_status)) {
1017 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1018 strip_hostname(server));
1019 if (!binding_string) {
1020 result = 1;
1021 goto done;
1024 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1025 if (!NT_STATUS_IS_OK(nt_status)) {
1026 result = -1;
1027 goto done;
1031 transport = dcerpc_binding_get_transport(binding);
1033 if (transport == NCA_UNKNOWN) {
1034 nt_status = dcerpc_binding_set_transport(binding, NCACN_NP);
1035 if (!NT_STATUS_IS_OK(nt_status)) {
1036 result = -1;
1037 goto done;
1041 host = dcerpc_binding_get_string_option(binding, "host");
1043 bflags = dcerpc_binding_get_flags(binding);
1044 if (bflags & DCERPC_CONNECT) {
1045 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1046 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1048 if (bflags & DCERPC_SIGN) {
1049 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1050 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1052 if (bflags & DCERPC_SEAL) {
1053 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1054 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1056 if (bflags & DCERPC_AUTH_SPNEGO) {
1057 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1058 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1060 if (bflags & DCERPC_AUTH_NTLM) {
1061 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1062 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1063 } else {
1064 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1067 if (bflags & DCERPC_AUTH_KRB5) {
1068 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1069 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1070 } else {
1071 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1074 if (pipe_default_auth_type != DCERPC_AUTH_TYPE_NONE) {
1075 /* If nothing is requested then default to integrity */
1076 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1077 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1081 signing_state = get_cmdline_auth_info_signing_state(rpcclient_auth_info);
1082 switch (signing_state) {
1083 case SMB_SIGNING_OFF:
1084 lp_set_cmdline("client ipc signing", "no");
1085 break;
1086 case SMB_SIGNING_REQUIRED:
1087 lp_set_cmdline("client ipc signing", "required");
1088 break;
1091 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1092 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1093 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1095 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1096 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1098 if (get_cmdline_auth_info_use_pw_nt_hash(rpcclient_auth_info)) {
1099 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
1102 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1103 SMB_ASSERT(user != NULL);
1104 domain = talloc_strdup(frame, lp_workgroup());
1105 SMB_ASSERT(domain != NULL);
1106 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1108 if ((q = strchr_m(user,'\\'))) {
1109 *q = 0;
1110 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1111 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1114 nt_status = cli_full_connection(&cli, lp_netbios_name(), host,
1115 opt_ipaddr ? &server_ss : NULL, opt_port,
1116 "IPC$", "IPC",
1117 get_cmdline_auth_info_username(rpcclient_auth_info),
1118 get_cmdline_auth_info_domain(rpcclient_auth_info),
1119 get_cmdline_auth_info_password(rpcclient_auth_info),
1120 flags,
1121 SMB_SIGNING_IPC_DEFAULT);
1123 if (!NT_STATUS_IS_OK(nt_status)) {
1124 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1125 result = 1;
1126 goto done;
1129 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1130 nt_status = cli_cm_force_encryption(cli,
1131 get_cmdline_auth_info_username(rpcclient_auth_info),
1132 get_cmdline_auth_info_password(rpcclient_auth_info),
1133 get_cmdline_auth_info_domain(rpcclient_auth_info),
1134 "IPC$");
1135 if (!NT_STATUS_IS_OK(nt_status)) {
1136 result = 1;
1137 goto done;
1141 #if 0 /* COMMENT OUT FOR TESTING */
1142 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1143 #endif
1145 /* Load command lists */
1146 rpcclient_cli_state = cli;
1148 timeout = 10000;
1149 cli_set_timeout(cli, timeout);
1151 cmd_set = rpcclient_command_list;
1153 while(*cmd_set) {
1154 add_command_set(*cmd_set);
1155 add_command_set(separator_command);
1156 cmd_set++;
1159 default_transport = dcerpc_binding_get_transport(binding);
1161 fetch_machine_sid(cli);
1163 /* Do anything specified with -c */
1164 if (cmdstr && cmdstr[0]) {
1165 char *cmd;
1166 char *p = cmdstr;
1168 result = 0;
1170 while((cmd=next_command(&p)) != NULL) {
1171 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1172 binding, cmd);
1173 SAFE_FREE(cmd);
1174 result = NT_STATUS_IS_ERR(cmd_result);
1177 goto done;
1180 /* Loop around accepting commands */
1182 while(1) {
1183 char *line = NULL;
1185 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1187 if (line == NULL)
1188 break;
1190 if (line[0] != '\n')
1191 process_cmd(rpcclient_auth_info, cli, binding, line);
1192 SAFE_FREE(line);
1195 done:
1196 rpcclient_cli_state = NULL;
1197 if (cli != NULL) {
1198 cli_shutdown(cli);
1200 TALLOC_FREE(frame);
1201 return result;