s3: clients: Use netlogon_creds_cli_close_global_db() in all normal exit paths.
[Samba.git] / source3 / rpcclient / rpcclient.c
blob3203df24c07196cf88978b3cfaca3a9dc995a23d
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 cli_state *rpcclient_cli_state;
54 struct netlogon_creds_cli_context *rpcclient_netlogon_creds;
55 static const char *rpcclient_netlogon_domain;
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_packet(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
488 int argc, const char **argv)
490 const char *p = "[KRB5|KRB5_SPNEGO|NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]";
491 const char *type = "NTLMSSP";
493 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PACKET;
494 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
496 if (argc > 2) {
497 printf("Usage: %s %s\n", argv[0], p);
498 return NT_STATUS_OK;
501 if (argc == 2) {
502 type = argv[1];
503 if (strequal(type, "KRB5")) {
504 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
505 } else if (strequal(type, "KRB5_SPNEGO")) {
506 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
507 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
508 } else if (strequal(type, "NTLMSSP")) {
509 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
510 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
511 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
512 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
513 } else if (strequal(type, "SCHANNEL")) {
514 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
515 } else {
516 printf("unknown type %s\n", type);
517 printf("Usage: %s %s\n", argv[0], p);
518 return NT_STATUS_INVALID_LEVEL;
522 d_printf("Setting %s - packet\n", type);
524 return cmd_set_ss_level();
528 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
529 int argc, const char **argv)
531 if (argc > 2) {
532 printf("Usage: %s timeout\n", argv[0]);
533 return NT_STATUS_OK;
536 if (argc == 2) {
537 timeout = atoi(argv[1]);
540 printf("timeout is %d\n", timeout);
542 return NT_STATUS_OK;
546 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
547 int argc, const char **argv)
549 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
550 pipe_default_auth_type = DCERPC_AUTH_TYPE_NONE;
551 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NONE;
553 return cmd_set_ss_level();
556 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
557 int argc, const char **argv)
559 d_printf("Setting schannel - sign and seal\n");
560 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
561 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
563 return cmd_set_ss_level();
566 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
567 int argc, const char **argv)
569 d_printf("Setting schannel - sign only\n");
570 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
571 pipe_default_auth_type = DCERPC_AUTH_TYPE_SCHANNEL;
573 return cmd_set_ss_level();
576 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
577 int argc, const char **argv)
579 NTSTATUS status;
581 if (argc != 2) {
582 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
583 return NT_STATUS_OK;
586 if (strequal(argv[1], "NCACN_NP")) {
587 default_transport = NCACN_NP;
588 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
589 default_transport = NCACN_IP_TCP;
590 } else {
591 printf("transport type: %s unknown or not supported\n", argv[1]);
592 return NT_STATUS_NOT_SUPPORTED;
595 status = cmd_set_transport();
596 if (!NT_STATUS_IS_OK(status)) {
597 return status;
600 printf("default transport is now: %s\n", argv[1]);
602 return NT_STATUS_OK;
605 /* Built in rpcclient commands */
607 static struct cmd_set rpcclient_commands[] = {
609 { "GENERAL OPTIONS" },
611 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
612 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
613 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
614 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
615 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
616 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
617 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
618 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
619 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
620 { "packet", RPC_RTYPE_NTSTATUS, cmd_packet, NULL, NULL, NULL, "Force RPC pipe connections with packet authentication level", "" },
621 { "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.", "" },
622 { "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.", "" },
623 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseconds) for RPC operations", "" },
624 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
625 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
627 { NULL }
630 static struct cmd_set separator_command[] = {
631 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
632 { NULL }
636 /* Various pipe commands */
638 extern struct cmd_set lsarpc_commands[];
639 extern struct cmd_set samr_commands[];
640 extern struct cmd_set spoolss_commands[];
641 extern struct cmd_set iremotewinspool_commands[];
642 extern struct cmd_set netlogon_commands[];
643 extern struct cmd_set srvsvc_commands[];
644 extern struct cmd_set dfs_commands[];
645 extern struct cmd_set ds_commands[];
646 extern struct cmd_set echo_commands[];
647 extern struct cmd_set epmapper_commands[];
648 extern struct cmd_set shutdown_commands[];
649 extern struct cmd_set test_commands[];
650 extern struct cmd_set wkssvc_commands[];
651 extern struct cmd_set ntsvcs_commands[];
652 extern struct cmd_set drsuapi_commands[];
653 extern struct cmd_set eventlog_commands[];
654 extern struct cmd_set winreg_commands[];
655 extern struct cmd_set fss_commands[];
656 extern struct cmd_set witness_commands[];
657 extern struct cmd_set clusapi_commands[];
659 static struct cmd_set *rpcclient_command_list[] = {
660 rpcclient_commands,
661 lsarpc_commands,
662 ds_commands,
663 samr_commands,
664 spoolss_commands,
665 iremotewinspool_commands,
666 netlogon_commands,
667 srvsvc_commands,
668 dfs_commands,
669 echo_commands,
670 epmapper_commands,
671 shutdown_commands,
672 test_commands,
673 wkssvc_commands,
674 ntsvcs_commands,
675 drsuapi_commands,
676 eventlog_commands,
677 winreg_commands,
678 fss_commands,
679 witness_commands,
680 clusapi_commands,
681 NULL
684 static void add_command_set(struct cmd_set *cmd_set)
686 struct cmd_list *entry;
688 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
689 DEBUG(0, ("out of memory\n"));
690 return;
693 ZERO_STRUCTP(entry);
695 entry->cmd_set = cmd_set;
696 DLIST_ADD(cmd_list, entry);
701 * Call an rpcclient function, passing an argv array.
703 * @param cmd Command to run, as a single string.
705 static NTSTATUS do_cmd(struct cli_state *cli,
706 struct user_auth_info *auth_info,
707 struct cmd_set *cmd_entry,
708 struct dcerpc_binding *binding,
709 int argc, const char **argv)
711 NTSTATUS ntresult;
712 WERROR wresult;
714 TALLOC_CTX *mem_ctx;
716 /* Create mem_ctx */
718 if (!(mem_ctx = talloc_stackframe())) {
719 DEBUG(0, ("talloc_init() failed\n"));
720 return NT_STATUS_NO_MEMORY;
723 /* Open pipe */
725 if ((cmd_entry->table != NULL) && (cmd_entry->rpc_pipe == NULL)) {
726 enum credentials_use_kerberos use_kerberos = CRED_AUTO_USE_KERBEROS;
727 switch (pipe_default_auth_type) {
728 case DCERPC_AUTH_TYPE_NONE:
729 ntresult = cli_rpc_pipe_open_noauth_transport(
730 cli, default_transport,
731 cmd_entry->table,
732 &cmd_entry->rpc_pipe);
733 break;
734 case DCERPC_AUTH_TYPE_SPNEGO:
735 switch (pipe_default_auth_spnego_type) {
736 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
737 use_kerberos = CRED_DONT_USE_KERBEROS;
738 break;
739 case PIPE_AUTH_TYPE_SPNEGO_KRB5:
740 use_kerberos = CRED_MUST_USE_KERBEROS;
741 break;
742 case PIPE_AUTH_TYPE_SPNEGO_NONE:
743 use_kerberos = CRED_AUTO_USE_KERBEROS;
744 break;
746 /* Fall through */
747 case DCERPC_AUTH_TYPE_NTLMSSP:
748 case DCERPC_AUTH_TYPE_KRB5:
749 ntresult = cli_rpc_pipe_open_generic_auth(
750 cli, cmd_entry->table,
751 default_transport,
752 use_kerberos,
753 pipe_default_auth_type,
754 pipe_default_auth_level,
755 smbXcli_conn_remote_name(cli->conn),
756 get_cmdline_auth_info_domain(auth_info),
757 get_cmdline_auth_info_username(auth_info),
758 get_cmdline_auth_info_password(auth_info),
759 &cmd_entry->rpc_pipe);
760 break;
761 case DCERPC_AUTH_TYPE_SCHANNEL:
762 TALLOC_FREE(rpcclient_netlogon_creds);
763 ntresult = cli_rpc_pipe_open_schannel(
764 cli, rpcclient_msg_ctx,
765 cmd_entry->table,
766 default_transport,
767 rpcclient_netlogon_domain,
768 &cmd_entry->rpc_pipe,
769 rpcclient_msg_ctx,
770 &rpcclient_netlogon_creds);
771 break;
772 default:
773 DEBUG(0, ("Could not initialise %s. Invalid "
774 "auth type %u\n",
775 cmd_entry->table->name,
776 pipe_default_auth_type ));
777 talloc_free(mem_ctx);
778 return NT_STATUS_UNSUCCESSFUL;
780 if (!NT_STATUS_IS_OK(ntresult)) {
781 DEBUG(0, ("Could not initialise %s. Error was %s\n",
782 cmd_entry->table->name,
783 nt_errstr(ntresult) ));
784 talloc_free(mem_ctx);
785 return ntresult;
788 if (rpcclient_netlogon_creds == NULL && cmd_entry->use_netlogon_creds) {
789 const char *dc_name = cmd_entry->rpc_pipe->desthost;
790 const char *domain = rpcclient_netlogon_domain;
791 struct cli_credentials *creds = NULL;
793 ntresult = pdb_get_trust_credentials(domain, NULL,
794 mem_ctx, &creds);
795 if (!NT_STATUS_IS_OK(ntresult)) {
796 DEBUG(0, ("Failed to fetch trust credentials for "
797 "%s to connect to %s: %s\n",
798 domain, cmd_entry->table->name,
799 nt_errstr(ntresult)));
800 TALLOC_FREE(cmd_entry->rpc_pipe);
801 talloc_free(mem_ctx);
802 return ntresult;
805 ntresult = rpccli_create_netlogon_creds_with_creds(creds,
806 dc_name,
807 rpcclient_msg_ctx,
808 rpcclient_msg_ctx,
809 &rpcclient_netlogon_creds);
810 if (!NT_STATUS_IS_OK(ntresult)) {
811 DEBUG(0, ("Could not initialise credentials for %s.\n",
812 cmd_entry->table->name));
813 TALLOC_FREE(cmd_entry->rpc_pipe);
814 TALLOC_FREE(mem_ctx);
815 return ntresult;
818 ntresult = rpccli_setup_netlogon_creds_with_creds(cli,
819 NCACN_NP,
820 rpcclient_netlogon_creds,
821 false, /* force_reauth */
822 creds);
823 TALLOC_FREE(creds);
824 if (!NT_STATUS_IS_OK(ntresult)) {
825 DEBUG(0, ("Could not initialise credentials for %s.\n",
826 cmd_entry->table->name));
827 TALLOC_FREE(cmd_entry->rpc_pipe);
828 TALLOC_FREE(rpcclient_netlogon_creds);
829 TALLOC_FREE(mem_ctx);
830 return ntresult;
835 /* Set timeout for new connections */
836 if (cmd_entry->rpc_pipe) {
837 rpccli_set_timeout(cmd_entry->rpc_pipe, timeout);
840 /* Run command */
842 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
843 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
844 if (!NT_STATUS_IS_OK(ntresult)) {
845 printf("result was %s\n", nt_errstr(ntresult));
847 } else {
848 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, argv);
849 /* print out the DOS error */
850 if (!W_ERROR_IS_OK(wresult)) {
851 printf( "result was %s\n", win_errstr(wresult));
853 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
856 /* Cleanup */
858 talloc_free(mem_ctx);
860 return ntresult;
865 * Process a command entered at the prompt or as part of -c
867 * @returns The NTSTATUS from running the command.
869 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
870 struct cli_state *cli,
871 struct dcerpc_binding *binding,
872 char *cmd)
874 struct cmd_list *temp_list;
875 NTSTATUS result = NT_STATUS_OK;
876 int ret;
877 int argc;
878 const char **argv = NULL;
880 if ((ret = poptParseArgvString(cmd, &argc, &argv)) != 0) {
881 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
882 return NT_STATUS_UNSUCCESSFUL;
886 /* Walk through a dlist of arrays of commands. */
887 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
888 struct cmd_set *temp_set = temp_list->cmd_set;
890 while (temp_set->name) {
891 if (strequal(argv[0], temp_set->name)) {
892 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
893 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
894 fprintf (stderr, "Invalid command\n");
895 goto out_free;
898 result = do_cmd(cli, auth_info, temp_set,
899 binding, argc, argv);
901 goto out_free;
903 temp_set++;
907 if (argv[0]) {
908 printf("command not found: %s\n", argv[0]);
911 out_free:
912 /* moved to do_cmd()
913 if (!NT_STATUS_IS_OK(result)) {
914 printf("result was %s\n", nt_errstr(result));
918 /* NOTE: popt allocates the whole argv, including the
919 * strings, as a single block. So a single free is
920 * enough to release it -- we don't free the
921 * individual strings. rtfm. */
922 free(argv);
924 return result;
928 /* Main function */
930 int main(int argc, char *argv[])
932 const char **const_argv = discard_const_p(const char *, argv);
933 int opt;
934 static char *cmdstr = NULL;
935 const char *server;
936 struct cli_state *cli = NULL;
937 static char *opt_ipaddr=NULL;
938 struct cmd_set **cmd_set;
939 struct sockaddr_storage server_ss;
940 NTSTATUS nt_status;
941 static int opt_port = 0;
942 int result = 0;
943 TALLOC_CTX *frame = talloc_stackframe();
944 uint32_t flags = 0;
945 struct dcerpc_binding *binding = NULL;
946 enum dcerpc_transport_t transport;
947 uint32_t bflags = 0;
948 const char *binding_string = NULL;
949 const char *host;
950 int signing_state = SMB_SIGNING_IPC_DEFAULT;
951 struct tevent_context *ev_ctx = NULL;
953 /* make sure the vars that get altered (4th field) are in
954 a fixed location or certain compilers complain */
955 poptContext pc;
956 struct poptOption long_options[] = {
957 POPT_AUTOHELP
958 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
959 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
960 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
961 POPT_COMMON_SAMBA
962 POPT_COMMON_CONNECTION
963 POPT_COMMON_CREDENTIALS
964 POPT_TABLEEND
967 smb_init_locale();
969 zero_sockaddr(&server_ss);
971 setlinebuf(stdout);
973 /* the following functions are part of the Samba debugging
974 facilities. See lib/debug.c */
975 setup_logging("rpcclient", DEBUG_STDOUT);
976 lp_set_cmdline("log level", "0");
978 /* Parse options */
980 pc = poptGetContext("rpcclient", argc, const_argv,
981 long_options, 0);
983 if (argc == 1) {
984 poptPrintHelp(pc, stderr, 0);
985 goto done;
988 while((opt = poptGetNextOpt(pc)) != -1) {
989 switch (opt) {
991 case 'I':
992 if (!interpret_string_addr(&server_ss,
993 opt_ipaddr,
994 AI_NUMERICHOST)) {
995 fprintf(stderr, "%s not a valid IP address\n",
996 opt_ipaddr);
997 result = 1;
998 goto done;
1003 /* Get server as remaining unparsed argument. Print usage if more
1004 than one unparsed argument is present. */
1006 server = poptGetArg(pc);
1008 if (!server || poptGetArg(pc)) {
1009 poptPrintHelp(pc, stderr, 0);
1010 result = 1;
1011 goto done;
1014 poptFreeContext(pc);
1015 popt_burn_cmdline_password(argc, argv);
1017 ev_ctx = samba_tevent_context_init(frame);
1018 if (ev_ctx == NULL) {
1019 fprintf(stderr, "Could not init event context\n");
1020 result = 1;
1021 goto done;
1024 nt_status = messaging_init_client(ev_ctx,
1025 ev_ctx,
1026 &rpcclient_msg_ctx);
1027 if (geteuid() != 0 &&
1028 NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCESS_DENIED)) {
1030 * Normal to fail to initialize messaging context
1031 * if we're not root as we don't have ability to
1032 * read lock directory.
1034 DBG_NOTICE("Unable to initialize messaging context. "
1035 "Must be root to do that.\n");
1036 } else if (!NT_STATUS_IS_OK(nt_status)) {
1037 fprintf(stderr, "Could not init messaging context\n");
1038 result = 1;
1039 goto done;
1042 if (!init_names()) {
1043 result = 1;
1044 goto done;
1048 * Get password
1049 * from stdin if necessary
1052 if ((server[0] == '/' && server[1] == '/') ||
1053 (server[0] == '\\' && server[1] == '\\')) {
1054 server += 2;
1057 nt_status = dcerpc_parse_binding(frame, server, &binding);
1059 if (!NT_STATUS_IS_OK(nt_status)) {
1061 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
1062 strip_hostname(server));
1063 if (!binding_string) {
1064 result = 1;
1065 goto done;
1068 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
1069 if (!NT_STATUS_IS_OK(nt_status)) {
1070 result = -1;
1071 goto done;
1075 transport = dcerpc_binding_get_transport(binding);
1077 if (transport == NCA_UNKNOWN) {
1078 nt_status = dcerpc_binding_set_transport(binding, NCACN_NP);
1079 if (!NT_STATUS_IS_OK(nt_status)) {
1080 result = -1;
1081 goto done;
1085 host = dcerpc_binding_get_string_option(binding, "host");
1087 bflags = dcerpc_binding_get_flags(binding);
1088 if (bflags & DCERPC_CONNECT) {
1089 pipe_default_auth_level = DCERPC_AUTH_LEVEL_CONNECT;
1090 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1092 if (bflags & DCERPC_PACKET) {
1093 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PACKET;
1094 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1096 if (bflags & DCERPC_SIGN) {
1097 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1098 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1100 if (bflags & DCERPC_SEAL) {
1101 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
1102 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1104 if (bflags & DCERPC_AUTH_SPNEGO) {
1105 pipe_default_auth_type = DCERPC_AUTH_TYPE_SPNEGO;
1106 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1108 if (bflags & DCERPC_AUTH_NTLM) {
1109 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1110 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
1111 } else {
1112 pipe_default_auth_type = DCERPC_AUTH_TYPE_NTLMSSP;
1115 if (bflags & DCERPC_AUTH_KRB5) {
1116 if (pipe_default_auth_type == DCERPC_AUTH_TYPE_SPNEGO) {
1117 pipe_default_auth_spnego_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
1118 } else {
1119 pipe_default_auth_type = DCERPC_AUTH_TYPE_KRB5;
1122 if (pipe_default_auth_type != DCERPC_AUTH_TYPE_NONE) {
1123 /* If nothing is requested then default to integrity */
1124 if (pipe_default_auth_level == DCERPC_AUTH_LEVEL_NONE) {
1125 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
1129 signing_state = get_cmdline_auth_info_signing_state(
1130 popt_get_cmdline_auth_info());
1131 switch (signing_state) {
1132 case SMB_SIGNING_OFF:
1133 lp_set_cmdline("client ipc signing", "no");
1134 break;
1135 case SMB_SIGNING_REQUIRED:
1136 lp_set_cmdline("client ipc signing", "required");
1137 break;
1140 if (get_cmdline_auth_info_use_kerberos(popt_get_cmdline_auth_info())) {
1141 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
1142 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
1144 if (get_cmdline_auth_info_use_ccache(popt_get_cmdline_auth_info())) {
1145 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
1147 if (get_cmdline_auth_info_use_pw_nt_hash(
1148 popt_get_cmdline_auth_info())) {
1149 flags |= CLI_FULL_CONNECTION_USE_NT_HASH;
1152 rpcclient_netlogon_domain = get_cmdline_auth_info_domain(
1153 popt_get_cmdline_auth_info());
1154 if (rpcclient_netlogon_domain == NULL ||
1155 rpcclient_netlogon_domain[0] == '\0')
1157 rpcclient_netlogon_domain = lp_workgroup();
1160 nt_status = cli_full_connection(&cli, lp_netbios_name(), host,
1161 opt_ipaddr ? &server_ss : NULL, opt_port,
1162 "IPC$", "IPC",
1163 get_cmdline_auth_info_username(
1164 popt_get_cmdline_auth_info()),
1165 get_cmdline_auth_info_domain(
1166 popt_get_cmdline_auth_info()),
1167 get_cmdline_auth_info_password(
1168 popt_get_cmdline_auth_info()),
1169 flags,
1170 SMB_SIGNING_IPC_DEFAULT);
1172 if (!NT_STATUS_IS_OK(nt_status)) {
1173 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1174 result = 1;
1175 goto done;
1178 if (get_cmdline_auth_info_smb_encrypt(popt_get_cmdline_auth_info())) {
1179 nt_status = cli_cm_force_encryption(cli,
1180 get_cmdline_auth_info_username(
1181 popt_get_cmdline_auth_info()),
1182 get_cmdline_auth_info_password(
1183 popt_get_cmdline_auth_info()),
1184 get_cmdline_auth_info_domain(
1185 popt_get_cmdline_auth_info()),
1186 "IPC$");
1187 if (!NT_STATUS_IS_OK(nt_status)) {
1188 result = 1;
1189 goto done;
1193 #if 0 /* COMMENT OUT FOR TESTING */
1194 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1195 #endif
1197 /* Load command lists */
1198 rpcclient_cli_state = cli;
1200 timeout = 10000;
1201 cli_set_timeout(cli, timeout);
1203 cmd_set = rpcclient_command_list;
1205 while(*cmd_set) {
1206 add_command_set(*cmd_set);
1207 add_command_set(separator_command);
1208 cmd_set++;
1211 default_transport = dcerpc_binding_get_transport(binding);
1213 fetch_machine_sid(cli);
1215 /* Do anything specified with -c */
1216 if (cmdstr && cmdstr[0]) {
1217 char *cmd;
1218 char *p = cmdstr;
1220 result = 0;
1222 while((cmd=next_command(&p)) != NULL) {
1223 NTSTATUS cmd_result = process_cmd(
1224 popt_get_cmdline_auth_info(),
1225 cli, binding, cmd);
1226 SAFE_FREE(cmd);
1227 result = NT_STATUS_IS_ERR(cmd_result);
1230 goto done;
1233 /* Loop around accepting commands */
1235 while(1) {
1236 char *line = NULL;
1238 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1240 if (line == NULL) {
1241 printf("\n");
1242 break;
1245 if (line[0] != '\n')
1246 process_cmd(popt_get_cmdline_auth_info(), cli,
1247 binding, line);
1248 SAFE_FREE(line);
1251 done:
1252 rpcclient_cli_state = NULL;
1253 if (cli != NULL) {
1254 cli_shutdown(cli);
1256 popt_free_cmdline_auth_info();
1257 netlogon_creds_cli_close_global_db();
1258 TALLOC_FREE(rpcclient_msg_ctx);
1259 TALLOC_FREE(ev_ctx);
1260 TALLOC_FREE(frame);
1261 return result;