s3:libsmb: fix the talloc parent of clistr_pull_talloc() in cli_notify_done()
[Samba/gebeck_regimport.git] / source3 / rpcclient / rpcclient.c
blob0c1a594dc38d8697b6e3eb481c9cc70da3d31eef
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;
53 /* List to hold groups of commands.
55 * Commands are defined in a list of arrays: arrays are easy to
56 * statically declare, and lists are easier to dynamically extend.
59 static struct cmd_list {
60 struct cmd_list *prev, *next;
61 struct cmd_set *cmd_set;
62 } *cmd_list;
64 /****************************************************************************
65 handle completion of commands for readline
66 ****************************************************************************/
67 static char **completion_fn(const char *text, int start, int end)
69 #define MAX_COMPLETIONS 1000
70 char **matches;
71 int i, count=0;
72 struct cmd_list *commands = cmd_list;
74 #if 0 /* JERRY */
75 /* FIXME!!! -- what to do when completing argument? */
76 /* for words not at the start of the line fallback
77 to filename completion */
78 if (start)
79 return NULL;
80 #endif
82 /* make sure we have a list of valid commands */
83 if (!commands) {
84 return NULL;
87 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
88 if (!matches) {
89 return NULL;
92 matches[count++] = SMB_STRDUP(text);
93 if (!matches[0]) {
94 SAFE_FREE(matches);
95 return NULL;
98 while (commands && count < MAX_COMPLETIONS-1) {
99 if (!commands->cmd_set) {
100 break;
103 for (i=0; commands->cmd_set[i].name; i++) {
104 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
105 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
106 commands->cmd_set[i].ntfn ) ||
107 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
108 commands->cmd_set[i].wfn))) {
109 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
110 if (!matches[count]) {
111 for (i = 0; i < count; i++) {
112 SAFE_FREE(matches[count]);
114 SAFE_FREE(matches);
115 return NULL;
117 count++;
120 commands = commands->next;
123 if (count == 2) {
124 SAFE_FREE(matches[0]);
125 matches[0] = SMB_STRDUP(matches[1]);
127 matches[count] = NULL;
128 return matches;
131 static char *next_command (char **cmdstr)
133 char *command;
134 char *p;
136 if (!cmdstr || !(*cmdstr))
137 return NULL;
139 p = strchr_m(*cmdstr, ';');
140 if (p)
141 *p = '\0';
142 command = SMB_STRDUP(*cmdstr);
143 if (p)
144 *cmdstr = p + 1;
145 else
146 *cmdstr = NULL;
148 return command;
151 /* Fetch the SID for this computer */
153 static void fetch_machine_sid(struct cli_state *cli)
155 struct policy_handle pol;
156 NTSTATUS result = NT_STATUS_OK, status;
157 static bool got_domain_sid;
158 TALLOC_CTX *mem_ctx;
159 struct rpc_pipe_client *lsapipe = NULL;
160 union lsa_PolicyInformation *info = NULL;
161 struct dcerpc_binding_handle *b;
163 if (got_domain_sid) return;
165 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
166 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
167 goto error;
170 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
171 &lsapipe);
172 if (!NT_STATUS_IS_OK(result)) {
173 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
174 goto error;
177 b = lsapipe->binding_handle;
179 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
180 SEC_FLAG_MAXIMUM_ALLOWED,
181 &pol);
182 if (!NT_STATUS_IS_OK(result)) {
183 goto error;
186 status = dcerpc_lsa_QueryInfoPolicy(b, mem_ctx,
187 &pol,
188 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
189 &info,
190 &result);
191 if (!NT_STATUS_IS_OK(status)) {
192 result = status;
193 goto error;
195 if (!NT_STATUS_IS_OK(result)) {
196 goto error;
199 got_domain_sid = True;
200 sid_copy(&domain_sid, info->account_domain.sid);
202 dcerpc_lsa_Close(b, mem_ctx, &pol, &result);
203 TALLOC_FREE(lsapipe);
204 talloc_destroy(mem_ctx);
206 return;
208 error:
210 if (lsapipe) {
211 TALLOC_FREE(lsapipe);
214 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
216 if (!NT_STATUS_IS_OK(result)) {
217 fprintf(stderr, "error: %s\n", nt_errstr(result));
220 exit(1);
223 /* List the available commands on a given pipe */
225 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
226 int argc, const char **argv)
228 struct cmd_list *tmp;
229 struct cmd_set *tmp_set;
230 int i;
232 /* Usage */
234 if (argc != 2) {
235 printf("Usage: %s <pipe>\n", argv[0]);
236 return NT_STATUS_OK;
239 /* Help on one command */
241 for (tmp = cmd_list; tmp; tmp = tmp->next)
243 tmp_set = tmp->cmd_set;
245 if (!strcasecmp_m(argv[1], tmp_set->name))
247 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
249 i = 0;
250 tmp_set++;
251 while(tmp_set->name) {
252 printf("%30s", tmp_set->name);
253 tmp_set++;
254 i++;
255 if (i%3 == 0)
256 printf("\n");
259 /* drop out of the loop */
260 break;
263 printf("\n\n");
265 return NT_STATUS_OK;
268 /* Display help on commands */
270 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
271 int argc, const char **argv)
273 struct cmd_list *tmp;
274 struct cmd_set *tmp_set;
276 /* Usage */
278 if (argc > 2) {
279 printf("Usage: %s [command]\n", argv[0]);
280 return NT_STATUS_OK;
283 /* Help on one command */
285 if (argc == 2) {
286 for (tmp = cmd_list; tmp; tmp = tmp->next) {
288 tmp_set = tmp->cmd_set;
290 while(tmp_set->name) {
291 if (strequal(argv[1], tmp_set->name)) {
292 if (tmp_set->usage &&
293 tmp_set->usage[0])
294 printf("%s\n", tmp_set->usage);
295 else
296 printf("No help for %s\n", tmp_set->name);
298 return NT_STATUS_OK;
301 tmp_set++;
305 printf("No such command: %s\n", argv[1]);
306 return NT_STATUS_OK;
309 /* List all commands */
311 for (tmp = cmd_list; tmp; tmp = tmp->next) {
313 tmp_set = tmp->cmd_set;
315 while(tmp_set->name) {
317 printf("%15s\t\t%s\n", tmp_set->name,
318 tmp_set->description ? tmp_set->description:
319 "");
321 tmp_set++;
325 return NT_STATUS_OK;
328 /* Change the debug level */
330 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
331 int argc, const char **argv)
333 if (argc > 2) {
334 printf("Usage: %s [debuglevel]\n", argv[0]);
335 return NT_STATUS_OK;
338 if (argc == 2) {
339 lp_set_cmdline("log level", argv[1]);
342 printf("debuglevel is %d\n", DEBUGLEVEL);
344 return NT_STATUS_OK;
347 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
348 int argc, const char **argv)
350 exit(0);
351 return NT_STATUS_OK; /* NOTREACHED */
354 static NTSTATUS cmd_set_ss_level(void)
356 struct cmd_list *tmp;
358 /* Close any existing connections not at this level. */
360 for (tmp = cmd_list; tmp; tmp = tmp->next) {
361 struct cmd_set *tmp_set;
363 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
364 if (tmp_set->rpc_pipe == NULL) {
365 continue;
368 if ((tmp_set->rpc_pipe->auth->auth_type
369 != pipe_default_auth_type)
370 || (tmp_set->rpc_pipe->auth->auth_level
371 != pipe_default_auth_level)) {
372 TALLOC_FREE(tmp_set->rpc_pipe);
373 tmp_set->rpc_pipe = NULL;
377 return NT_STATUS_OK;
380 static NTSTATUS cmd_set_transport(void)
382 struct cmd_list *tmp;
384 /* Close any existing connections not at this level. */
386 for (tmp = cmd_list; tmp; tmp = tmp->next) {
387 struct cmd_set *tmp_set;
389 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
390 if (tmp_set->rpc_pipe == NULL) {
391 continue;
394 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
395 TALLOC_FREE(tmp_set->rpc_pipe);
396 tmp_set->rpc_pipe = NULL;
400 return NT_STATUS_OK;
403 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
404 int argc, const char **argv)
406 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
407 const char *type = "NTLMSSP";
409 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
410 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
412 if (argc > 2) {
413 printf("Usage: %s %s\n", argv[0], p);
414 return NT_STATUS_OK;
417 if (argc == 2) {
418 type = argv[1];
419 if (strequal(type, "KRB5")) {
420 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
421 } else if (strequal(type, "KRB5_SPNEGO")) {
422 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
423 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
424 } else if (strequal(type, "NTLMSSP")) {
425 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
426 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
427 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
428 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
429 } else if (strequal(type, "SCHANNEL")) {
430 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
431 } else {
432 printf("unknown type %s\n", type);
433 printf("Usage: %s %s\n", argv[0], p);
434 return NT_STATUS_INVALID_LEVEL;
438 d_printf("Setting %s - sign\n", type);
440 return cmd_set_ss_level();
443 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
444 int argc, const char **argv)
446 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
447 const char *type = "NTLMSSP";
449 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
450 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
452 if (argc > 2) {
453 printf("Usage: %s %s\n", argv[0], p);
454 return NT_STATUS_OK;
457 if (argc == 2) {
458 type = argv[1];
459 if (strequal(type, "KRB5")) {
460 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
461 } else if (strequal(type, "KRB5_SPNEGO")) {
462 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
463 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
464 } else if (strequal(type, "NTLMSSP")) {
465 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
466 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
467 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
468 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
469 } else if (strequal(type, "SCHANNEL")) {
470 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
471 } else {
472 printf("unknown type %s\n", type);
473 printf("Usage: %s %s\n", argv[0], p);
474 return NT_STATUS_INVALID_LEVEL;
478 d_printf("Setting %s - sign and seal\n", type);
480 return cmd_set_ss_level();
483 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
484 int argc, const char **argv)
486 struct cmd_list *tmp;
488 if (argc > 2) {
489 printf("Usage: %s timeout\n", argv[0]);
490 return NT_STATUS_OK;
493 if (argc == 2) {
494 timeout = atoi(argv[1]);
496 for (tmp = cmd_list; tmp; tmp = tmp->next) {
498 struct cmd_set *tmp_set;
500 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
501 if (tmp_set->rpc_pipe == NULL) {
502 continue;
505 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
510 printf("timeout is %d\n", timeout);
512 return NT_STATUS_OK;
516 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
517 int argc, const char **argv)
519 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
520 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
521 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
523 return cmd_set_ss_level();
526 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
527 int argc, const char **argv)
529 d_printf("Setting schannel - sign and seal\n");
530 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
531 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
533 return cmd_set_ss_level();
536 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
537 int argc, const char **argv)
539 d_printf("Setting schannel - sign only\n");
540 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
541 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
543 return cmd_set_ss_level();
546 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
547 int argc, const char **argv)
549 NTSTATUS status;
551 if (argc != 2) {
552 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
553 return NT_STATUS_OK;
556 if (strequal(argv[1], "NCACN_NP")) {
557 default_transport = NCACN_NP;
558 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
559 default_transport = NCACN_IP_TCP;
560 } else {
561 printf("transport type: %s unknown or not supported\n", argv[1]);
562 return NT_STATUS_NOT_SUPPORTED;
565 status = cmd_set_transport();
566 if (!NT_STATUS_IS_OK(status)) {
567 return status;
570 printf("default transport is now: %s\n", argv[1]);
572 return NT_STATUS_OK;
575 /* Built in rpcclient commands */
577 static struct cmd_set rpcclient_commands[] = {
579 { "GENERAL OPTIONS" },
581 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
582 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
583 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
584 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
585 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
586 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
587 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
588 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
589 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
590 { "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.", "" },
591 { "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.", "" },
592 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
593 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
594 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
596 { NULL }
599 static struct cmd_set separator_command[] = {
600 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
601 { NULL }
605 /* Various pipe commands */
607 extern struct cmd_set lsarpc_commands[];
608 extern struct cmd_set samr_commands[];
609 extern struct cmd_set spoolss_commands[];
610 extern struct cmd_set netlogon_commands[];
611 extern struct cmd_set srvsvc_commands[];
612 extern struct cmd_set dfs_commands[];
613 extern struct cmd_set ds_commands[];
614 extern struct cmd_set echo_commands[];
615 extern struct cmd_set epmapper_commands[];
616 extern struct cmd_set shutdown_commands[];
617 extern struct cmd_set test_commands[];
618 extern struct cmd_set wkssvc_commands[];
619 extern struct cmd_set ntsvcs_commands[];
620 extern struct cmd_set drsuapi_commands[];
621 extern struct cmd_set eventlog_commands[];
622 extern struct cmd_set winreg_commands[];
624 static struct cmd_set *rpcclient_command_list[] = {
625 rpcclient_commands,
626 lsarpc_commands,
627 ds_commands,
628 samr_commands,
629 spoolss_commands,
630 netlogon_commands,
631 srvsvc_commands,
632 dfs_commands,
633 echo_commands,
634 epmapper_commands,
635 shutdown_commands,
636 test_commands,
637 wkssvc_commands,
638 ntsvcs_commands,
639 drsuapi_commands,
640 eventlog_commands,
641 winreg_commands,
642 NULL
645 static void add_command_set(struct cmd_set *cmd_set)
647 struct cmd_list *entry;
649 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
650 DEBUG(0, ("out of memory\n"));
651 return;
654 ZERO_STRUCTP(entry);
656 entry->cmd_set = cmd_set;
657 DLIST_ADD(cmd_list, entry);
662 * Call an rpcclient function, passing an argv array.
664 * @param cmd Command to run, as a single string.
666 static NTSTATUS do_cmd(struct cli_state *cli,
667 struct user_auth_info *auth_info,
668 struct cmd_set *cmd_entry,
669 struct dcerpc_binding *binding,
670 int argc, char **argv)
672 NTSTATUS ntresult;
673 WERROR wresult;
675 TALLOC_CTX *mem_ctx;
677 /* Create mem_ctx */
679 if (!(mem_ctx = talloc_init("do_cmd"))) {
680 DEBUG(0, ("talloc_init() failed\n"));
681 return NT_STATUS_NO_MEMORY;
684 /* Open pipe */
686 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
687 switch (pipe_default_auth_type) {
688 case DCERPC_AUTH_TYPE_NONE:
689 ntresult = cli_rpc_pipe_open_noauth_transport(
690 cli, default_transport,
691 &cmd_entry->table->syntax_id,
692 &cmd_entry->rpc_pipe);
693 break;
694 case DCERPC_AUTH_TYPE_SPNEGO:
696 /* won't happen, but if it does it will fail in cli_rpc_pipe_open_spnego() eventually */
697 const char *oid = "INVALID";
698 switch (pipe_default_auth_spnego_type) {
699 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
700 oid = GENSEC_OID_NTLMSSP;
701 break;
702 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
703 oid = GENSEC_OID_KERBEROS5;
704 break;
706 ntresult = cli_rpc_pipe_open_spnego(
707 cli, cmd_entry->table,
708 default_transport,
709 oid,
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;
718 case DCERPC_AUTH_TYPE_NTLMSSP:
719 case DCERPC_AUTH_TYPE_KRB5:
720 ntresult = cli_rpc_pipe_open_generic_auth(
721 cli, cmd_entry->table,
722 default_transport,
723 pipe_default_auth_type,
724 pipe_default_auth_level,
725 smbXcli_conn_remote_name(cli->conn),
726 get_cmdline_auth_info_domain(auth_info),
727 get_cmdline_auth_info_username(auth_info),
728 get_cmdline_auth_info_password(auth_info),
729 &cmd_entry->rpc_pipe);
730 break;
731 case DCERPC_AUTH_TYPE_SCHANNEL:
732 ntresult = cli_rpc_pipe_open_schannel(
733 cli, &cmd_entry->table->syntax_id,
734 default_transport,
735 pipe_default_auth_level,
736 get_cmdline_auth_info_domain(auth_info),
737 &cmd_entry->rpc_pipe);
738 break;
739 default:
740 DEBUG(0, ("Could not initialise %s. Invalid "
741 "auth type %u\n",
742 cmd_entry->table->name,
743 pipe_default_auth_type ));
744 return NT_STATUS_UNSUCCESSFUL;
746 if (!NT_STATUS_IS_OK(ntresult)) {
747 DEBUG(0, ("Could not initialise %s. Error was %s\n",
748 cmd_entry->table->name,
749 nt_errstr(ntresult) ));
750 return ntresult;
753 if (ndr_syntax_id_equal(&cmd_entry->table->syntax_id,
754 &ndr_table_netlogon.syntax_id)) {
755 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
756 enum netr_SchannelType sec_channel_type;
757 uchar trust_password[16];
758 const char *machine_account;
760 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
761 trust_password, &machine_account,
762 &sec_channel_type))
764 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
767 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
768 cmd_entry->rpc_pipe->desthost, /* server name */
769 get_cmdline_auth_info_domain(auth_info), /* domain */
770 lp_netbios_name(), /* client name */
771 machine_account, /* machine account name */
772 trust_password,
773 sec_channel_type,
774 &neg_flags);
776 if (!NT_STATUS_IS_OK(ntresult)) {
777 DEBUG(0, ("Could not initialise credentials for %s.\n",
778 cmd_entry->table->name));
779 return ntresult;
784 /* Run command */
786 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
787 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
788 if (!NT_STATUS_IS_OK(ntresult)) {
789 printf("result was %s\n", nt_errstr(ntresult));
791 } else {
792 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
793 /* print out the DOS error */
794 if (!W_ERROR_IS_OK(wresult)) {
795 printf( "result was %s\n", win_errstr(wresult));
797 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
800 /* Cleanup */
802 talloc_destroy(mem_ctx);
804 return ntresult;
809 * Process a command entered at the prompt or as part of -c
811 * @returns The NTSTATUS from running the command.
813 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
814 struct cli_state *cli,
815 struct dcerpc_binding *binding,
816 char *cmd)
818 struct cmd_list *temp_list;
819 NTSTATUS result = NT_STATUS_OK;
820 int ret;
821 int argc;
822 char **argv = NULL;
824 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
825 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
826 return NT_STATUS_UNSUCCESSFUL;
830 /* Walk through a dlist of arrays of commands. */
831 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
832 struct cmd_set *temp_set = temp_list->cmd_set;
834 while (temp_set->name) {
835 if (strequal(argv[0], temp_set->name)) {
836 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
837 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
838 fprintf (stderr, "Invalid command\n");
839 goto out_free;
842 result = do_cmd(cli, auth_info, temp_set,
843 binding, argc, argv);
845 goto out_free;
847 temp_set++;
851 if (argv[0]) {
852 printf("command not found: %s\n", argv[0]);
855 out_free:
856 /* moved to do_cmd()
857 if (!NT_STATUS_IS_OK(result)) {
858 printf("result was %s\n", nt_errstr(result));
862 /* NOTE: popt allocates the whole argv, including the
863 * strings, as a single block. So a single free is
864 * enough to release it -- we don't free the
865 * individual strings. rtfm. */
866 free(argv);
868 return result;
872 /* Main function */
874 int main(int argc, char *argv[])
876 int opt;
877 static char *cmdstr = NULL;
878 const char *server;
879 struct cli_state *cli = NULL;
880 static char *opt_ipaddr=NULL;
881 struct cmd_set **cmd_set;
882 struct sockaddr_storage server_ss;
883 NTSTATUS nt_status;
884 static int opt_port = 0;
885 int result = 0;
886 TALLOC_CTX *frame = talloc_stackframe();
887 uint32_t flags = 0;
888 struct dcerpc_binding *binding = NULL;
889 const char *binding_string = NULL;
890 char *user, *domain, *q;
892 /* make sure the vars that get altered (4th field) are in
893 a fixed location or certain compilers complain */
894 poptContext pc;
895 struct poptOption long_options[] = {
896 POPT_AUTOHELP
897 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
898 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
899 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
900 POPT_COMMON_SAMBA
901 POPT_COMMON_CONNECTION
902 POPT_COMMON_CREDENTIALS
903 POPT_TABLEEND
906 load_case_tables();
908 zero_sockaddr(&server_ss);
910 setlinebuf(stdout);
912 /* the following functions are part of the Samba debugging
913 facilities. See lib/debug.c */
914 setup_logging("rpcclient", DEBUG_STDOUT);
916 rpcclient_auth_info = user_auth_info_init(frame);
917 if (rpcclient_auth_info == NULL) {
918 exit(1);
920 popt_common_set_auth_info(rpcclient_auth_info);
922 /* Parse options */
924 pc = poptGetContext("rpcclient", argc, (const char **) argv,
925 long_options, 0);
927 if (argc == 1) {
928 poptPrintHelp(pc, stderr, 0);
929 goto done;
932 while((opt = poptGetNextOpt(pc)) != -1) {
933 switch (opt) {
935 case 'I':
936 if (!interpret_string_addr(&server_ss,
937 opt_ipaddr,
938 AI_NUMERICHOST)) {
939 fprintf(stderr, "%s not a valid IP address\n",
940 opt_ipaddr);
941 result = 1;
942 goto done;
947 /* Get server as remaining unparsed argument. Print usage if more
948 than one unparsed argument is present. */
950 server = poptGetArg(pc);
952 if (!server || poptGetArg(pc)) {
953 poptPrintHelp(pc, stderr, 0);
954 result = 1;
955 goto done;
958 poptFreeContext(pc);
960 if (!init_names()) {
961 result = 1;
962 goto done;
965 /* Load smb.conf file */
967 if (!lp_load_global(get_dyn_CONFIGFILE()))
968 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
970 /* We must load interfaces after we load the smb.conf */
971 load_interfaces();
974 * Get password
975 * from stdin if necessary
978 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
979 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
980 result = 1;
981 goto done;
984 set_cmdline_auth_info_getpass(rpcclient_auth_info);
986 if ((server[0] == '/' && server[1] == '/') ||
987 (server[0] == '\\' && server[1] == '\\')) {
988 server += 2;
991 nt_status = dcerpc_parse_binding(frame, server, &binding);
993 if (!NT_STATUS_IS_OK(nt_status)) {
995 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
996 strip_hostname(server));
997 if (!binding_string) {
998 result = 1;
999 goto done;
1002 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1003 if (!NT_STATUS_IS_OK(nt_status)) {
1004 result = -1;
1005 goto done;
1009 if (binding->transport == NCA_UNKNOWN) {
1010 binding->transport = NCACN_NP;
1013 if (binding->flags & DCERPC_SIGN) {
1014 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1015 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1017 if (binding->flags & DCERPC_SEAL) {
1018 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1019 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1021 if (binding->flags & DCERPC_AUTH_SPNEGO) {
1022 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1023 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1025 if (binding->flags & DCERPC_AUTH_NTLM) {
1026 /* If neither Integrity or Privacy are requested then
1027 * Use just Connect level */
1028 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1029 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1032 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1033 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1034 } else {
1035 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1038 if (binding->flags & DCERPC_AUTH_KRB5) {
1039 /* If neither Integrity or Privacy are requested then
1040 * Use just Connect level */
1041 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1042 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1045 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1046 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1047 } else {
1048 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1052 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
1053 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1054 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1056 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
1057 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1060 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
1061 SMB_ASSERT(user != NULL);
1062 domain = talloc_strdup(frame, lp_workgroup());
1063 SMB_ASSERT(domain != NULL);
1064 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1066 if ((q = strchr_m(user,'\\'))) {
1067 *q = 0;
1068 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1069 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1073 nt_status = cli_full_connection(&cli, lp_netbios_name(), binding->host,
1074 opt_ipaddr ? &server_ss : NULL, opt_port,
1075 "IPC$", "IPC",
1076 get_cmdline_auth_info_username(rpcclient_auth_info),
1077 get_cmdline_auth_info_domain(rpcclient_auth_info),
1078 get_cmdline_auth_info_password(rpcclient_auth_info),
1079 flags,
1080 get_cmdline_auth_info_signing_state(rpcclient_auth_info));
1082 if (!NT_STATUS_IS_OK(nt_status)) {
1083 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1084 result = 1;
1085 goto done;
1088 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1089 nt_status = cli_cm_force_encryption(cli,
1090 get_cmdline_auth_info_username(rpcclient_auth_info),
1091 get_cmdline_auth_info_password(rpcclient_auth_info),
1092 get_cmdline_auth_info_domain(rpcclient_auth_info),
1093 "IPC$");
1094 if (!NT_STATUS_IS_OK(nt_status)) {
1095 result = 1;
1096 goto done;
1100 #if 0 /* COMMENT OUT FOR TESTING */
1101 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1102 #endif
1104 /* Load command lists */
1106 timeout = cli_set_timeout(cli, 10000);
1108 cmd_set = rpcclient_command_list;
1110 while(*cmd_set) {
1111 add_command_set(*cmd_set);
1112 add_command_set(separator_command);
1113 cmd_set++;
1116 default_transport = binding->transport;
1118 fetch_machine_sid(cli);
1120 /* Do anything specified with -c */
1121 if (cmdstr && cmdstr[0]) {
1122 char *cmd;
1123 char *p = cmdstr;
1125 result = 0;
1127 while((cmd=next_command(&p)) != NULL) {
1128 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1129 binding, cmd);
1130 SAFE_FREE(cmd);
1131 result = NT_STATUS_IS_ERR(cmd_result);
1134 goto done;
1137 /* Loop around accepting commands */
1139 while(1) {
1140 char *line = NULL;
1142 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1144 if (line == NULL)
1145 break;
1147 if (line[0] != '\n')
1148 process_cmd(rpcclient_auth_info, cli, binding, line);
1149 SAFE_FREE(line);
1152 done:
1153 if (cli != NULL) {
1154 cli_shutdown(cli);
1156 TALLOC_FREE(frame);
1157 return result;