docs: use popt.autohelp in smbtree manpage.
[Samba.git] / source3 / rpcclient / rpcclient.c
blobc88e6b30d81489ac3d78f58302d23cf8e1065a15
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"
37 enum pipe_auth_type_spnego {
38 PIPE_AUTH_TYPE_SPNEGO_NONE = 0,
39 PIPE_AUTH_TYPE_SPNEGO_NTLMSSP,
40 PIPE_AUTH_TYPE_SPNEGO_KRB5
43 struct dom_sid domain_sid;
45 static enum dcerpc_AuthType pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
46 static enum pipe_auth_type_spnego pipe_default_auth_spnego_type = 0;
47 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
48 static unsigned int timeout = 0;
49 static enum dcerpc_transport_t default_transport = NCACN_NP;
51 struct user_auth_info *rpcclient_auth_info;
52 struct cli_state *rpcclient_cli_state;
54 /* List to hold groups of commands.
56 * Commands are defined in a list of arrays: arrays are easy to
57 * statically declare, and lists are easier to dynamically extend.
60 static struct cmd_list {
61 struct cmd_list *prev, *next;
62 struct cmd_set *cmd_set;
63 } *cmd_list;
65 /****************************************************************************
66 handle completion of commands for readline
67 ****************************************************************************/
68 static char **completion_fn(const char *text, int start, int end)
70 #define MAX_COMPLETIONS 1000
71 char **matches;
72 int i, count=0;
73 struct cmd_list *commands = cmd_list;
75 #if 0 /* JERRY */
76 /* FIXME!!! -- what to do when completing argument? */
77 /* for words not at the start of the line fallback
78 to filename completion */
79 if (start)
80 return NULL;
81 #endif
83 /* make sure we have a list of valid commands */
84 if (!commands) {
85 return NULL;
88 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
89 if (!matches) {
90 return NULL;
93 matches[count++] = SMB_STRDUP(text);
94 if (!matches[0]) {
95 SAFE_FREE(matches);
96 return NULL;
99 while (commands && count < MAX_COMPLETIONS-1) {
100 if (!commands->cmd_set) {
101 break;
104 for (i=0; commands->cmd_set[i].name; i++) {
105 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
106 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
107 commands->cmd_set[i].ntfn ) ||
108 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
109 commands->cmd_set[i].wfn))) {
110 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
111 if (!matches[count]) {
112 for (i = 0; i < count; i++) {
113 SAFE_FREE(matches[count]);
115 SAFE_FREE(matches);
116 return NULL;
118 count++;
121 commands = commands->next;
124 if (count == 2) {
125 SAFE_FREE(matches[0]);
126 matches[0] = SMB_STRDUP(matches[1]);
128 matches[count] = NULL;
129 return matches;
132 static char *next_command (char **cmdstr)
134 char *command;
135 char *p;
137 if (!cmdstr || !(*cmdstr))
138 return NULL;
140 p = strchr_m(*cmdstr, ';');
141 if (p)
142 *p = '\0';
143 command = SMB_STRDUP(*cmdstr);
144 if (p)
145 *cmdstr = p + 1;
146 else
147 *cmdstr = NULL;
149 return command;
152 /* Fetch the SID for this computer */
154 static void fetch_machine_sid(struct cli_state *cli)
156 struct policy_handle pol;
157 NTSTATUS result = NT_STATUS_OK, status;
158 static bool got_domain_sid;
159 TALLOC_CTX *mem_ctx;
160 struct rpc_pipe_client *lsapipe = NULL;
161 union lsa_PolicyInformation *info = NULL;
162 struct dcerpc_binding_handle *b;
164 if (got_domain_sid) return;
166 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
167 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
168 goto error;
171 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc,
172 &lsapipe);
173 if (!NT_STATUS_IS_OK(result)) {
174 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
175 goto error;
178 b = lsapipe->binding_handle;
180 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
181 SEC_FLAG_MAXIMUM_ALLOWED,
182 &pol);
183 if (!NT_STATUS_IS_OK(result)) {
184 goto error;
187 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
188 &pol,
189 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
190 &info,
191 &result);
192 if (!NT_STATUS_IS_OK(status)) {
193 result = status;
194 goto error;
196 if (!NT_STATUS_IS_OK(result)) {
197 goto error;
200 got_domain_sid = True;
201 sid_copy(&domain_sid, info->account_domain.sid);
203 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
204 TALLOC_FREE(lsapipe);
205 talloc_destroy(mem_ctx);
207 return;
209 error:
211 if (lsapipe) {
212 TALLOC_FREE(lsapipe);
215 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
217 if (!NT_STATUS_IS_OK(result)) {
218 fprintf(stderr, "error: %s\n", nt_errstr(result));
221 exit(1);
224 /* List the available commands on a given pipe */
226 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
227 int argc, const char **argv)
229 struct cmd_list *tmp;
230 struct cmd_set *tmp_set;
231 int i;
233 /* Usage */
235 if (argc != 2) {
236 printf("Usage: %s <pipe>\n", argv[0]);
237 return NT_STATUS_OK;
240 /* Help on one command */
242 for (tmp = cmd_list; tmp; tmp = tmp->next)
244 tmp_set = tmp->cmd_set;
246 if (!strcasecmp_m(argv[1], tmp_set->name))
248 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
250 i = 0;
251 tmp_set++;
252 while(tmp_set->name) {
253 printf("%30s", tmp_set->name);
254 tmp_set++;
255 i++;
256 if (i%3 == 0)
257 printf("\n");
260 /* drop out of the loop */
261 break;
264 printf("\n\n");
266 return NT_STATUS_OK;
269 /* Display help on commands */
271 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
272 int argc, const char **argv)
274 struct cmd_list *tmp;
275 struct cmd_set *tmp_set;
277 /* Usage */
279 if (argc > 2) {
280 printf("Usage: %s [command]\n", argv[0]);
281 return NT_STATUS_OK;
284 /* Help on one command */
286 if (argc == 2) {
287 for (tmp = cmd_list; tmp; tmp = tmp->next) {
289 tmp_set = tmp->cmd_set;
291 while(tmp_set->name) {
292 if (strequal(argv[1], tmp_set->name)) {
293 if (tmp_set->usage &&
294 tmp_set->usage[0])
295 printf("%s\n", tmp_set->usage);
296 else
297 printf("No help for %s\n", tmp_set->name);
299 return NT_STATUS_OK;
302 tmp_set++;
306 printf("No such command: %s\n", argv[1]);
307 return NT_STATUS_OK;
310 /* List all commands */
312 for (tmp = cmd_list; tmp; tmp = tmp->next) {
314 tmp_set = tmp->cmd_set;
316 while(tmp_set->name) {
318 printf("%15s\t\t%s\n", tmp_set->name,
319 tmp_set->description ? tmp_set->description:
320 "");
322 tmp_set++;
326 return NT_STATUS_OK;
329 /* Change the debug level */
331 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
332 int argc, const char **argv)
334 if (argc > 2) {
335 printf("Usage: %s [debuglevel]\n", argv[0]);
336 return NT_STATUS_OK;
339 if (argc == 2) {
340 lp_set_cmdline("log level", argv[1]);
343 printf("debuglevel is %d\n", DEBUGLEVEL);
345 return NT_STATUS_OK;
348 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
349 int argc, const char **argv)
351 exit(0);
352 return NT_STATUS_OK; /* NOTREACHED */
355 static NTSTATUS cmd_set_ss_level(void)
357 struct cmd_list *tmp;
359 /* Close any existing connections not at this level. */
361 for (tmp = cmd_list; tmp; tmp = tmp->next) {
362 struct cmd_set *tmp_set;
364 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
365 if (tmp_set->rpc_pipe == NULL) {
366 continue;
369 if ((tmp_set->rpc_pipe->auth->auth_type
370 != pipe_default_auth_type)
371 || (tmp_set->rpc_pipe->auth->auth_level
372 != pipe_default_auth_level)) {
373 TALLOC_FREE(tmp_set->rpc_pipe);
374 tmp_set->rpc_pipe = NULL;
378 return NT_STATUS_OK;
381 static NTSTATUS cmd_set_transport(void)
383 struct cmd_list *tmp;
385 /* Close any existing connections not at this level. */
387 for (tmp = cmd_list; tmp; tmp = tmp->next) {
388 struct cmd_set *tmp_set;
390 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
391 if (tmp_set->rpc_pipe == NULL) {
392 continue;
395 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
396 TALLOC_FREE(tmp_set->rpc_pipe);
397 tmp_set->rpc_pipe = NULL;
401 return NT_STATUS_OK;
404 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
405 int argc, const char **argv)
407 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
408 const char *type = "NTLMSSP";
410 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
411 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
413 if (argc > 2) {
414 printf("Usage: %s %s\n", argv[0], p);
415 return NT_STATUS_OK;
418 if (argc == 2) {
419 type = argv[1];
420 if (strequal(type, "KRB5")) {
421 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
422 } else if (strequal(type, "KRB5_SPNEGO")) {
423 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
424 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
425 } else if (strequal(type, "NTLMSSP")) {
426 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
427 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
428 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
429 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
430 } else if (strequal(type, "SCHANNEL")) {
431 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
432 } else {
433 printf("unknown type %s\n", type);
434 printf("Usage: %s %s\n", argv[0], p);
435 return NT_STATUS_INVALID_LEVEL;
439 d_printf("Setting %s - sign\n", type);
441 return cmd_set_ss_level();
444 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
445 int argc, const char **argv)
447 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
448 const char *type = "NTLMSSP";
450 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
451 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
453 if (argc > 2) {
454 printf("Usage: %s %s\n", argv[0], p);
455 return NT_STATUS_OK;
458 if (argc == 2) {
459 type = argv[1];
460 if (strequal(type, "KRB5")) {
461 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
462 } else if (strequal(type, "KRB5_SPNEGO")) {
463 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
464 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
465 } else if (strequal(type, "NTLMSSP")) {
466 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
467 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
468 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
469 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
470 } else if (strequal(type, "SCHANNEL")) {
471 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
472 } else {
473 printf("unknown type %s\n", type);
474 printf("Usage: %s %s\n", argv[0], p);
475 return NT_STATUS_INVALID_LEVEL;
479 d_printf("Setting %s - sign and seal\n", type);
481 return cmd_set_ss_level();
484 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
485 int argc, const char **argv)
487 struct cmd_list *tmp;
489 if (argc > 2) {
490 printf("Usage: %s timeout\n", argv[0]);
491 return NT_STATUS_OK;
494 if (argc == 2) {
495 timeout = atoi(argv[1]);
497 for (tmp = cmd_list; tmp; tmp = tmp->next) {
499 struct cmd_set *tmp_set;
501 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
502 if (tmp_set->rpc_pipe == NULL) {
503 continue;
506 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
511 printf("timeout is %d\n", timeout);
513 return NT_STATUS_OK;
517 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
518 int argc, const char **argv)
520 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
521 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
522 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
524 return cmd_set_ss_level();
527 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
528 int argc, const char **argv)
530 d_printf("Setting schannel - sign and seal\n");
531 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
532 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
534 return cmd_set_ss_level();
537 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
538 int argc, const char **argv)
540 d_printf("Setting schannel - sign only\n");
541 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
542 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
544 return cmd_set_ss_level();
547 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
548 int argc, const char **argv)
550 NTSTATUS status;
552 if (argc != 2) {
553 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
554 return NT_STATUS_OK;
557 if (strequal(argv[1], "NCACN_NP")) {
558 default_transport = NCACN_NP;
559 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
560 default_transport = NCACN_IP_TCP;
561 } else {
562 printf("transport type: %s unknown or not supported\n", argv[1]);
563 return NT_STATUS_NOT_SUPPORTED;
566 status = cmd_set_transport();
567 if (!NT_STATUS_IS_OK(status)) {
568 return status;
571 printf("default transport is now: %s\n", argv[1]);
573 return NT_STATUS_OK;
576 /* Built in rpcclient commands */
578 static struct cmd_set rpcclient_commands[] = {
580 { "GENERAL OPTIONS" },
582 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
583 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
584 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
585 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
586 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
587 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
588 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
589 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
590 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
591 { "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.", "" },
592 { "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.", "" },
593 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
594 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
595 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
597 { NULL }
600 static struct cmd_set separator_command[] = {
601 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
602 { NULL }
606 /* Various pipe commands */
608 extern struct cmd_set lsarpc_commands[];
609 extern struct cmd_set samr_commands[];
610 extern struct cmd_set spoolss_commands[];
611 extern struct cmd_set netlogon_commands[];
612 extern struct cmd_set srvsvc_commands[];
613 extern struct cmd_set dfs_commands[];
614 extern struct cmd_set ds_commands[];
615 extern struct cmd_set echo_commands[];
616 extern struct cmd_set epmapper_commands[];
617 extern struct cmd_set shutdown_commands[];
618 extern struct cmd_set test_commands[];
619 extern struct cmd_set wkssvc_commands[];
620 extern struct cmd_set ntsvcs_commands[];
621 extern struct cmd_set drsuapi_commands[];
622 extern struct cmd_set eventlog_commands[];
623 extern struct cmd_set winreg_commands[];
624 extern struct cmd_set fss_commands[];
626 static struct cmd_set *rpcclient_command_list[] = {
627 rpcclient_commands,
628 lsarpc_commands,
629 ds_commands,
630 samr_commands,
631 spoolss_commands,
632 netlogon_commands,
633 srvsvc_commands,
634 dfs_commands,
635 echo_commands,
636 epmapper_commands,
637 shutdown_commands,
638 test_commands,
639 wkssvc_commands,
640 ntsvcs_commands,
641 drsuapi_commands,
642 eventlog_commands,
643 winreg_commands,
644 fss_commands,
645 NULL
648 static void add_command_set(struct cmd_set *cmd_set)
650 struct cmd_list *entry;
652 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
653 DEBUG(0, ("out of memory\n"));
654 return;
657 ZERO_STRUCTP(entry);
659 entry->cmd_set = cmd_set;
660 DLIST_ADD(cmd_list, entry);
665 * Call an rpcclient function, passing an argv array.
667 * @param cmd Command to run, as a single string.
669 static NTSTATUS do_cmd(struct cli_state *cli,
670 struct user_auth_info *auth_info,
671 struct cmd_set *cmd_entry,
672 struct dcerpc_binding *binding,
673 int argc, const char **argv)
675 NTSTATUS ntresult;
676 WERROR wresult;
678 TALLOC_CTX *mem_ctx;
680 /* Create mem_ctx */
682 if (!(mem_ctx = talloc_stackframe())) {
683 DEBUG(0, ("talloc_init() failed\n"));
684 return NT_STATUS_NO_MEMORY;
687 /* Open pipe */
689 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
690 switch (pipe_default_auth_type) {
691 case DCERPC_AUTH_TYPE_NONE:
692 ntresult = cli_rpc_pipe_open_noauth_transport(
693 cli, default_transport,
694 cmd_entry->table,
695 &cmd_entry->rpc_pipe);
696 break;
697 case DCERPC_AUTH_TYPE_SPNEGO:
699 /* won't happen, but if it does it will fail in cli_rpc_pipe_open_spnego() eventually */
700 const char *oid = "INVALID";
701 switch (pipe_default_auth_spnego_type) {
702 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
703 oid = GENSEC_OID_NTLMSSP;
704 break;
705 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
706 oid = GENSEC_OID_KERBEROS5;
707 break;
708 case PIPE_AUTH_TYPE_SPNEGO_NONE:
709 break;
711 ntresult = cli_rpc_pipe_open_spnego(
712 cli, cmd_entry->table,
713 default_transport,
714 oid,
715 pipe_default_auth_level,
716 smbXcli_conn_remote_name(cli->conn),
717 get_cmdline_auth_info_domain(auth_info),
718 get_cmdline_auth_info_username(auth_info),
719 get_cmdline_auth_info_password(auth_info),
720 &cmd_entry->rpc_pipe);
721 break;
723 case DCERPC_AUTH_TYPE_NTLMSSP:
724 case DCERPC_AUTH_TYPE_KRB5:
725 ntresult = cli_rpc_pipe_open_generic_auth(
726 cli, cmd_entry->table,
727 default_transport,
728 pipe_default_auth_type,
729 pipe_default_auth_level,
730 smbXcli_conn_remote_name(cli->conn),
731 get_cmdline_auth_info_domain(auth_info),
732 get_cmdline_auth_info_username(auth_info),
733 get_cmdline_auth_info_password(auth_info),
734 &cmd_entry->rpc_pipe);
735 break;
736 case DCERPC_AUTH_TYPE_SCHANNEL:
737 ntresult = cli_rpc_pipe_open_schannel(
738 cli, cmd_entry->table,
739 default_transport,
740 pipe_default_auth_level,
741 get_cmdline_auth_info_domain(auth_info),
742 &cmd_entry->rpc_pipe);
743 break;
744 default:
745 DEBUG(0, ("Could not initialise %s. Invalid "
746 "auth type %u\n",
747 cmd_entry->table->name,
748 pipe_default_auth_type ));
749 talloc_free(mem_ctx);
750 return NT_STATUS_UNSUCCESSFUL;
752 if (!NT_STATUS_IS_OK(ntresult)) {
753 DEBUG(0, ("Could not initialise %s. Error was %s\n",
754 cmd_entry->table->name,
755 nt_errstr(ntresult) ));
756 talloc_free(mem_ctx);
757 return ntresult;
760 if (ndr_syntax_id_equal(&cmd_entry->table->syntax_id,
761 &ndr_table_netlogon.syntax_id)) {
762 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS |
763 NETLOGON_NEG_SUPPORTS_AES;
764 enum netr_SchannelType sec_channel_type;
765 uchar trust_password[16];
766 const char *machine_account;
768 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
769 trust_password, &machine_account,
770 &sec_channel_type))
772 talloc_free(mem_ctx);
773 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
776 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
777 cmd_entry->rpc_pipe->desthost, /* server name */
778 get_cmdline_auth_info_domain(auth_info), /* domain */
779 lp_netbios_name(), /* client name */
780 machine_account, /* machine account name */
781 trust_password,
782 sec_channel_type,
783 &neg_flags);
785 if (!NT_STATUS_IS_OK(ntresult)) {
786 DEBUG(0, ("Could not initialise credentials for %s.\n",
787 cmd_entry->table->name));
788 talloc_free(mem_ctx);
789 return ntresult;
794 /* Run command */
796 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
797 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
798 if (!NT_STATUS_IS_OK(ntresult)) {
799 printf("result was %s\n", nt_errstr(ntresult));
801 } else {
802 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
803 /* print out the DOS error */
804 if (!W_ERROR_IS_OK(wresult)) {
805 printf( "result was %s\n", win_errstr(wresult));
807 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
810 /* Cleanup */
812 talloc_free(mem_ctx);
814 return ntresult;
819 * Process a command entered at the prompt or as part of -c
821 * @returns The NTSTATUS from running the command.
823 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
824 struct cli_state *cli,
825 struct dcerpc_binding *binding,
826 char *cmd)
828 struct cmd_list *temp_list;
829 NTSTATUS result = NT_STATUS_OK;
830 int ret;
831 int argc;
832 const char **argv = NULL;
834 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
835 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
836 return NT_STATUS_UNSUCCESSFUL;
840 /* Walk through a dlist of arrays of commands. */
841 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
842 struct cmd_set *temp_set = temp_list->cmd_set;
844 while (temp_set->name) {
845 if (strequal(argv[0], temp_set->name)) {
846 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
847 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
848 fprintf (stderr, "Invalid command\n");
849 goto out_free;
852 result = do_cmd(cli, auth_info, temp_set,
853 binding, argc, argv);
855 goto out_free;
857 temp_set++;
861 if (argv[0]) {
862 printf("command not found: %s\n", argv[0]);
865 out_free:
866 /* moved to do_cmd()
867 if (!NT_STATUS_IS_OK(result)) {
868 printf("result was %s\n", nt_errstr(result));
872 /* NOTE: popt allocates the whole argv, including the
873 * strings, as a single block. So a single free is
874 * enough to release it -- we don't free the
875 * individual strings. rtfm. */
876 free(argv);
878 return result;
882 /* Main function */
884 int main(int argc, char *argv[])
886 const char **const_argv = discard_const_p(const char *, argv);
887 int opt;
888 static char *cmdstr = NULL;
889 const char *server;
890 struct cli_state *cli = NULL;
891 static char *opt_ipaddr=NULL;
892 struct cmd_set **cmd_set;
893 struct sockaddr_storage server_ss;
894 NTSTATUS nt_status;
895 static int opt_port = 0;
896 int result = 0;
897 TALLOC_CTX *frame = talloc_stackframe();
898 uint32_t flags = 0;
899 struct dcerpc_binding *binding = NULL;
900 const char *binding_string = NULL;
901 char *user, *domain, *q;
903 /* make sure the vars that get altered (4th field) are in
904 a fixed location or certain compilers complain */
905 poptContext pc;
906 struct poptOption long_options[] = {
907 POPT_AUTOHELP
908 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
909 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
910 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
911 POPT_COMMON_SAMBA
912 POPT_COMMON_CONNECTION
913 POPT_COMMON_CREDENTIALS
914 POPT_TABLEEND
917 load_case_tables();
919 zero_sockaddr(&server_ss);
921 setlinebuf(stdout);
923 /* the following functions are part of the Samba debugging
924 facilities. See lib/debug.c */
925 setup_logging("rpcclient", DEBUG_STDOUT);
927 rpcclient_auth_info = user_auth_info_init(frame);
928 if (rpcclient_auth_info == NULL) {
929 exit(1);
931 popt_common_set_auth_info(rpcclient_auth_info);
933 /* Parse options */
935 pc = poptGetContext("rpcclient", argc, const_argv,
936 long_options, 0);
938 if (argc == 1) {
939 poptPrintHelp(pc, stderr, 0);
940 goto done;
943 while((opt = poptGetNextOpt(pc)) != -1) {
944 switch (opt) {
946 case 'I':
947 if (!interpret_string_addr(&server_ss,
948 opt_ipaddr,
949 AI_NUMERICHOST)) {
950 fprintf(stderr, "%s not a valid IP address\n",
951 opt_ipaddr);
952 result = 1;
953 goto done;
958 /* Get server as remaining unparsed argument. Print usage if more
959 than one unparsed argument is present. */
961 server = poptGetArg(pc);
963 if (!server || poptGetArg(pc)) {
964 poptPrintHelp(pc, stderr, 0);
965 result = 1;
966 goto done;
969 poptFreeContext(pc);
970 popt_burn_cmdline_password(argc, argv);
972 if (!init_names()) {
973 result = 1;
974 goto done;
977 /* Load smb.conf file */
979 if (!lp_load_global(get_dyn_CONFIGFILE()))
980 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
982 /* We must load interfaces after we load the smb.conf */
983 load_interfaces();
986 * Get password
987 * from stdin if necessary
990 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
991 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
992 result = 1;
993 goto done;
996 set_cmdline_auth_info_getpass(rpcclient_auth_info);
998 if ((server[0] == '/' && server[1] == '/') ||
999 (server[0] == '\\' && server[1] == '\\')) {
1000 server += 2;
1003 nt_status = dcerpc_parse_binding(frame, server, &binding);
1005 if (!NT_STATUS_IS_OK(nt_status)) {
1007 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1008 strip_hostname(server));
1009 if (!binding_string) {
1010 result = 1;
1011 goto done;
1014 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1015 if (!NT_STATUS_IS_OK(nt_status)) {
1016 result = -1;
1017 goto done;
1021 if (binding->transport == NCA_UNKNOWN) {
1022 binding->transport = NCACN_NP;
1025 if (binding->flags & DCERPC_SIGN) {
1026 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1027 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1029 if (binding->flags & DCERPC_SEAL) {
1030 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1031 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1033 if (binding->flags & DCERPC_AUTH_SPNEGO) {
1034 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1035 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1037 if (binding->flags & DCERPC_AUTH_NTLM) {
1038 /* If neither Integrity or Privacy are requested then
1039 * Use just Connect level */
1040 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1041 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1044 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1045 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1046 } else {
1047 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1050 if (binding->flags & DCERPC_AUTH_KRB5) {
1051 /* If neither Integrity or Privacy are requested then
1052 * Use just Connect level */
1053 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1054 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1057 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1058 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1059 } else {
1060 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1064 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1065 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1066 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1068 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1069 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1072 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1073 SMB_ASSERT(user != NULL);
1074 domain = talloc_strdup(frame, lp_workgroup());
1075 SMB_ASSERT(domain != NULL);
1076 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1078 if ((q = strchr_m(user,'\\'))) {
1079 *q = 0;
1080 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1081 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1085 nt_status = cli_full_connection(&cli, lp_netbios_name(), binding->host,
1086 opt_ipaddr ? &server_ss : NULL, opt_port,
1087 "IPC$", "IPC",
1088 get_cmdline_auth_info_username(rpcclient_auth_info),
1089 get_cmdline_auth_info_domain(rpcclient_auth_info),
1090 get_cmdline_auth_info_password(rpcclient_auth_info),
1091 flags,
1092 get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1094 if (!NT_STATUS_IS_OK(nt_status)) {
1095 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1096 result = 1;
1097 goto done;
1100 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1101 nt_status = cli_cm_force_encryption(cli,
1102 get_cmdline_auth_info_username(rpcclient_auth_info),
1103 get_cmdline_auth_info_password(rpcclient_auth_info),
1104 get_cmdline_auth_info_domain(rpcclient_auth_info),
1105 "IPC$");
1106 if (!NT_STATUS_IS_OK(nt_status)) {
1107 result = 1;
1108 goto done;
1112 #if 0 /* COMMENT OUT FOR TESTING */
1113 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1114 #endif
1116 /* Load command lists */
1117 rpcclient_cli_state = cli;
1118 timeout = cli_set_timeout(cli, 10000);
1120 cmd_set = rpcclient_command_list;
1122 while(*cmd_set) {
1123 add_command_set(*cmd_set);
1124 add_command_set(separator_command);
1125 cmd_set++;
1128 default_transport = binding->transport;
1130 fetch_machine_sid(cli);
1132 /* Do anything specified with -c */
1133 if (cmdstr && cmdstr[0]) {
1134 char *cmd;
1135 char *p = cmdstr;
1137 result = 0;
1139 while((cmd=next_command(&p)) != NULL) {
1140 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1141 binding, cmd);
1142 SAFE_FREE(cmd);
1143 result = NT_STATUS_IS_ERR(cmd_result);
1146 goto done;
1149 /* Loop around accepting commands */
1151 while(1) {
1152 char *line = NULL;
1154 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1156 if (line == NULL)
1157 break;
1159 if (line[0] != '\n')
1160 process_cmd(rpcclient_auth_info, cli, binding, line);
1161 SAFE_FREE(line);
1164 done:
1165 rpcclient_cli_state = NULL;
1166 if (cli != NULL) {
1167 cli_shutdown(cli);
1169 TALLOC_FREE(frame);
1170 return result;