s3-waf: Add check for dirent.d_off member
[Samba/gebeck_regimport.git] / source3 / rpcclient / rpcclient.c
blob56c0057dd96a5d6a4a778577f98e7bfd007afe2e
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 "rpcclient.h"
24 #include "../libcli/auth/libcli_auth.h"
25 #include "../librpc/gen_ndr/cli_lsa.h"
26 #include "rpc_client/cli_lsarpc.h"
27 #include "../librpc/gen_ndr/ndr_netlogon.h"
28 #include "rpc_client/cli_netlogon.h"
30 struct dom_sid domain_sid;
32 static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
33 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
34 static unsigned int timeout = 0;
35 static enum dcerpc_transport_t default_transport = NCACN_NP;
37 struct user_auth_info *rpcclient_auth_info;
39 /* List to hold groups of commands.
41 * Commands are defined in a list of arrays: arrays are easy to
42 * statically declare, and lists are easier to dynamically extend.
45 static struct cmd_list {
46 struct cmd_list *prev, *next;
47 struct cmd_set *cmd_set;
48 } *cmd_list;
50 /****************************************************************************
51 handle completion of commands for readline
52 ****************************************************************************/
53 static char **completion_fn(const char *text, int start, int end)
55 #define MAX_COMPLETIONS 100
56 char **matches;
57 int i, count=0;
58 struct cmd_list *commands = cmd_list;
60 #if 0 /* JERRY */
61 /* FIXME!!! -- what to do when completing argument? */
62 /* for words not at the start of the line fallback
63 to filename completion */
64 if (start)
65 return NULL;
66 #endif
68 /* make sure we have a list of valid commands */
69 if (!commands) {
70 return NULL;
73 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
74 if (!matches) {
75 return NULL;
78 matches[count++] = SMB_STRDUP(text);
79 if (!matches[0]) {
80 SAFE_FREE(matches);
81 return NULL;
84 while (commands && count < MAX_COMPLETIONS-1) {
85 if (!commands->cmd_set) {
86 break;
89 for (i=0; commands->cmd_set[i].name; i++) {
90 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
91 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
92 commands->cmd_set[i].ntfn ) ||
93 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
94 commands->cmd_set[i].wfn))) {
95 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
96 if (!matches[count]) {
97 for (i = 0; i < count; i++) {
98 SAFE_FREE(matches[count]);
100 SAFE_FREE(matches);
101 return NULL;
103 count++;
106 commands = commands->next;
110 if (count == 2) {
111 SAFE_FREE(matches[0]);
112 matches[0] = SMB_STRDUP(matches[1]);
114 matches[count] = NULL;
115 return matches;
118 static char *next_command (char **cmdstr)
120 char *command;
121 char *p;
123 if (!cmdstr || !(*cmdstr))
124 return NULL;
126 p = strchr_m(*cmdstr, ';');
127 if (p)
128 *p = '\0';
129 command = SMB_STRDUP(*cmdstr);
130 if (p)
131 *cmdstr = p + 1;
132 else
133 *cmdstr = NULL;
135 return command;
138 /* Fetch the SID for this computer */
140 static void fetch_machine_sid(struct cli_state *cli)
142 struct policy_handle pol;
143 NTSTATUS result = NT_STATUS_OK;
144 static bool got_domain_sid;
145 TALLOC_CTX *mem_ctx;
146 struct rpc_pipe_client *lsapipe = NULL;
147 union lsa_PolicyInformation *info = NULL;
149 if (got_domain_sid) return;
151 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
152 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
153 goto error;
156 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
157 &lsapipe);
158 if (!NT_STATUS_IS_OK(result)) {
159 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
160 goto error;
163 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
164 SEC_FLAG_MAXIMUM_ALLOWED,
165 &pol);
166 if (!NT_STATUS_IS_OK(result)) {
167 goto error;
170 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
171 &pol,
172 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
173 &info);
174 if (!NT_STATUS_IS_OK(result)) {
175 goto error;
178 got_domain_sid = True;
179 sid_copy(&domain_sid, info->account_domain.sid);
181 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
182 TALLOC_FREE(lsapipe);
183 talloc_destroy(mem_ctx);
185 return;
187 error:
189 if (lsapipe) {
190 TALLOC_FREE(lsapipe);
193 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
195 if (!NT_STATUS_IS_OK(result)) {
196 fprintf(stderr, "error: %s\n", nt_errstr(result));
199 exit(1);
202 /* List the available commands on a given pipe */
204 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
205 int argc, const char **argv)
207 struct cmd_list *tmp;
208 struct cmd_set *tmp_set;
209 int i;
211 /* Usage */
213 if (argc != 2) {
214 printf("Usage: %s <pipe>\n", argv[0]);
215 return NT_STATUS_OK;
218 /* Help on one command */
220 for (tmp = cmd_list; tmp; tmp = tmp->next)
222 tmp_set = tmp->cmd_set;
224 if (!StrCaseCmp(argv[1], tmp_set->name))
226 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
228 i = 0;
229 tmp_set++;
230 while(tmp_set->name) {
231 printf("%30s", tmp_set->name);
232 tmp_set++;
233 i++;
234 if (i%3 == 0)
235 printf("\n");
238 /* drop out of the loop */
239 break;
242 printf("\n\n");
244 return NT_STATUS_OK;
247 /* Display help on commands */
249 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
250 int argc, const char **argv)
252 struct cmd_list *tmp;
253 struct cmd_set *tmp_set;
255 /* Usage */
257 if (argc > 2) {
258 printf("Usage: %s [command]\n", argv[0]);
259 return NT_STATUS_OK;
262 /* Help on one command */
264 if (argc == 2) {
265 for (tmp = cmd_list; tmp; tmp = tmp->next) {
267 tmp_set = tmp->cmd_set;
269 while(tmp_set->name) {
270 if (strequal(argv[1], tmp_set->name)) {
271 if (tmp_set->usage &&
272 tmp_set->usage[0])
273 printf("%s\n", tmp_set->usage);
274 else
275 printf("No help for %s\n", tmp_set->name);
277 return NT_STATUS_OK;
280 tmp_set++;
284 printf("No such command: %s\n", argv[1]);
285 return NT_STATUS_OK;
288 /* List all commands */
290 for (tmp = cmd_list; tmp; tmp = tmp->next) {
292 tmp_set = tmp->cmd_set;
294 while(tmp_set->name) {
296 printf("%15s\t\t%s\n", tmp_set->name,
297 tmp_set->description ? tmp_set->description:
298 "");
300 tmp_set++;
304 return NT_STATUS_OK;
307 /* Change the debug level */
309 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
310 int argc, const char **argv)
312 if (argc > 2) {
313 printf("Usage: %s [debuglevel]\n", argv[0]);
314 return NT_STATUS_OK;
317 if (argc == 2) {
318 DEBUGLEVEL = atoi(argv[1]);
321 printf("debuglevel is %d\n", DEBUGLEVEL);
323 return NT_STATUS_OK;
326 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
327 int argc, const char **argv)
329 exit(0);
330 return NT_STATUS_OK; /* NOTREACHED */
333 static NTSTATUS cmd_set_ss_level(void)
335 struct cmd_list *tmp;
337 /* Close any existing connections not at this level. */
339 for (tmp = cmd_list; tmp; tmp = tmp->next) {
340 struct cmd_set *tmp_set;
342 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
343 if (tmp_set->rpc_pipe == NULL) {
344 continue;
347 if ((tmp_set->rpc_pipe->auth->auth_type
348 != pipe_default_auth_type)
349 || (tmp_set->rpc_pipe->auth->auth_level
350 != pipe_default_auth_level)) {
351 TALLOC_FREE(tmp_set->rpc_pipe);
352 tmp_set->rpc_pipe = NULL;
356 return NT_STATUS_OK;
359 static NTSTATUS cmd_set_transport(void)
361 struct cmd_list *tmp;
363 /* Close any existing connections not at this level. */
365 for (tmp = cmd_list; tmp; tmp = tmp->next) {
366 struct cmd_set *tmp_set;
368 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
369 if (tmp_set->rpc_pipe == NULL) {
370 continue;
373 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
374 TALLOC_FREE(tmp_set->rpc_pipe);
375 tmp_set->rpc_pipe = NULL;
379 return NT_STATUS_OK;
382 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
383 int argc, const char **argv)
385 const char *type = "NTLMSSP";
387 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
388 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
390 if (argc > 2) {
391 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
392 return NT_STATUS_OK;
395 if (argc == 2) {
396 type = argv[1];
397 if (strequal(type, "NTLMSSP")) {
398 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
399 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
400 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
401 } else if (strequal(type, "SCHANNEL")) {
402 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
403 } else {
404 printf("unknown type %s\n", type);
405 return NT_STATUS_INVALID_LEVEL;
409 d_printf("Setting %s - sign\n", type);
411 return cmd_set_ss_level();
414 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
415 int argc, const char **argv)
417 const char *type = "NTLMSSP";
419 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
420 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
422 if (argc > 2) {
423 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
424 return NT_STATUS_OK;
427 if (argc == 2) {
428 type = argv[1];
429 if (strequal(type, "NTLMSSP")) {
430 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
431 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
432 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
433 } else if (strequal(type, "SCHANNEL")) {
434 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
435 } else {
436 printf("unknown type %s\n", type);
437 return NT_STATUS_INVALID_LEVEL;
441 d_printf("Setting %s - sign and seal\n", type);
443 return cmd_set_ss_level();
446 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
447 int argc, const char **argv)
449 struct cmd_list *tmp;
451 if (argc > 2) {
452 printf("Usage: %s timeout\n", argv[0]);
453 return NT_STATUS_OK;
456 if (argc == 2) {
457 timeout = atoi(argv[1]);
459 for (tmp = cmd_list; tmp; tmp = tmp->next) {
461 struct cmd_set *tmp_set;
463 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
464 if (tmp_set->rpc_pipe == NULL) {
465 continue;
468 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
473 printf("timeout is %d\n", timeout);
475 return NT_STATUS_OK;
479 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
480 int argc, const char **argv)
482 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
483 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
485 return cmd_set_ss_level();
488 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
489 int argc, const char **argv)
491 d_printf("Setting schannel - sign and seal\n");
492 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
493 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
495 return cmd_set_ss_level();
498 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
499 int argc, const char **argv)
501 d_printf("Setting schannel - sign only\n");
502 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
503 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
505 return cmd_set_ss_level();
508 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
509 int argc, const char **argv)
511 NTSTATUS status;
513 if (argc != 2) {
514 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
515 return NT_STATUS_OK;
518 if (strequal(argv[1], "NCACN_NP")) {
519 default_transport = NCACN_NP;
520 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
521 default_transport = NCACN_IP_TCP;
522 } else {
523 printf("transport type: %s unknown or not supported\n", argv[1]);
524 return NT_STATUS_NOT_SUPPORTED;
527 status = cmd_set_transport();
528 if (!NT_STATUS_IS_OK(status)) {
529 return status;
532 printf("default transport is now: %s\n", argv[1]);
534 return NT_STATUS_OK;
537 /* Built in rpcclient commands */
539 static struct cmd_set rpcclient_commands[] = {
541 { "GENERAL OPTIONS" },
543 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
544 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
545 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
546 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
547 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
548 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
549 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
550 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
551 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
552 { "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.", "" },
553 { "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.", "" },
554 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
555 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
556 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
558 { NULL }
561 static struct cmd_set separator_command[] = {
562 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
563 { NULL }
567 /* Various pipe commands */
569 extern struct cmd_set lsarpc_commands[];
570 extern struct cmd_set samr_commands[];
571 extern struct cmd_set spoolss_commands[];
572 extern struct cmd_set netlogon_commands[];
573 extern struct cmd_set srvsvc_commands[];
574 extern struct cmd_set dfs_commands[];
575 extern struct cmd_set ds_commands[];
576 extern struct cmd_set echo_commands[];
577 extern struct cmd_set epmapper_commands[];
578 extern struct cmd_set shutdown_commands[];
579 extern struct cmd_set test_commands[];
580 extern struct cmd_set wkssvc_commands[];
581 extern struct cmd_set ntsvcs_commands[];
582 extern struct cmd_set drsuapi_commands[];
583 extern struct cmd_set eventlog_commands[];
585 static struct cmd_set *rpcclient_command_list[] = {
586 rpcclient_commands,
587 lsarpc_commands,
588 ds_commands,
589 samr_commands,
590 spoolss_commands,
591 netlogon_commands,
592 srvsvc_commands,
593 dfs_commands,
594 echo_commands,
595 epmapper_commands,
596 shutdown_commands,
597 test_commands,
598 wkssvc_commands,
599 ntsvcs_commands,
600 drsuapi_commands,
601 eventlog_commands,
602 NULL
605 static void add_command_set(struct cmd_set *cmd_set)
607 struct cmd_list *entry;
609 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
610 DEBUG(0, ("out of memory\n"));
611 return;
614 ZERO_STRUCTP(entry);
616 entry->cmd_set = cmd_set;
617 DLIST_ADD(cmd_list, entry);
622 * Call an rpcclient function, passing an argv array.
624 * @param cmd Command to run, as a single string.
626 static NTSTATUS do_cmd(struct cli_state *cli,
627 struct user_auth_info *auth_info,
628 struct cmd_set *cmd_entry,
629 struct dcerpc_binding *binding,
630 int argc, char **argv)
632 NTSTATUS ntresult;
633 WERROR wresult;
635 TALLOC_CTX *mem_ctx;
637 /* Create mem_ctx */
639 if (!(mem_ctx = talloc_init("do_cmd"))) {
640 DEBUG(0, ("talloc_init() failed\n"));
641 return NT_STATUS_NO_MEMORY;
644 /* Open pipe */
646 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
647 switch (pipe_default_auth_type) {
648 case PIPE_AUTH_TYPE_NONE:
649 ntresult = cli_rpc_pipe_open_noauth_transport(
650 cli, default_transport,
651 cmd_entry->interface,
652 &cmd_entry->rpc_pipe);
653 break;
654 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
655 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
656 cli, cmd_entry->interface,
657 default_transport,
658 pipe_default_auth_level,
659 get_cmdline_auth_info_domain(auth_info),
660 get_cmdline_auth_info_username(auth_info),
661 get_cmdline_auth_info_password(auth_info),
662 &cmd_entry->rpc_pipe);
663 break;
664 case PIPE_AUTH_TYPE_NTLMSSP:
665 ntresult = cli_rpc_pipe_open_ntlmssp(
666 cli, cmd_entry->interface,
667 default_transport,
668 pipe_default_auth_level,
669 get_cmdline_auth_info_domain(auth_info),
670 get_cmdline_auth_info_username(auth_info),
671 get_cmdline_auth_info_password(auth_info),
672 &cmd_entry->rpc_pipe);
673 break;
674 case PIPE_AUTH_TYPE_SCHANNEL:
675 ntresult = cli_rpc_pipe_open_schannel(
676 cli, cmd_entry->interface,
677 default_transport,
678 pipe_default_auth_level,
679 get_cmdline_auth_info_domain(auth_info),
680 &cmd_entry->rpc_pipe);
681 break;
682 default:
683 DEBUG(0, ("Could not initialise %s. Invalid "
684 "auth type %u\n",
685 get_pipe_name_from_syntax(
686 talloc_tos(),
687 cmd_entry->interface),
688 pipe_default_auth_type ));
689 return NT_STATUS_UNSUCCESSFUL;
691 if (!NT_STATUS_IS_OK(ntresult)) {
692 DEBUG(0, ("Could not initialise %s. Error was %s\n",
693 get_pipe_name_from_syntax(
694 talloc_tos(), cmd_entry->interface),
695 nt_errstr(ntresult) ));
696 return ntresult;
699 if (ndr_syntax_id_equal(cmd_entry->interface,
700 &ndr_table_netlogon.syntax_id)) {
701 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
702 enum netr_SchannelType sec_channel_type;
703 uchar trust_password[16];
704 const char *machine_account;
706 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
707 trust_password, &machine_account,
708 &sec_channel_type))
710 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
713 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
714 cli->desthost, /* server name */
715 get_cmdline_auth_info_domain(auth_info), /* domain */
716 global_myname(), /* client name */
717 machine_account, /* machine account name */
718 trust_password,
719 sec_channel_type,
720 &neg_flags);
722 if (!NT_STATUS_IS_OK(ntresult)) {
723 DEBUG(0, ("Could not initialise credentials for %s.\n",
724 get_pipe_name_from_syntax(
725 talloc_tos(),
726 cmd_entry->interface)));
727 return ntresult;
732 /* Run command */
734 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
735 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
736 if (!NT_STATUS_IS_OK(ntresult)) {
737 printf("result was %s\n", nt_errstr(ntresult));
739 } else {
740 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
741 /* print out the DOS error */
742 if (!W_ERROR_IS_OK(wresult)) {
743 printf( "result was %s\n", win_errstr(wresult));
745 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
748 /* Cleanup */
750 talloc_destroy(mem_ctx);
752 return ntresult;
757 * Process a command entered at the prompt or as part of -c
759 * @returns The NTSTATUS from running the command.
761 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
762 struct cli_state *cli,
763 struct dcerpc_binding *binding,
764 char *cmd)
766 struct cmd_list *temp_list;
767 NTSTATUS result = NT_STATUS_OK;
768 int ret;
769 int argc;
770 char **argv = NULL;
772 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
773 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
774 return NT_STATUS_UNSUCCESSFUL;
778 /* Walk through a dlist of arrays of commands. */
779 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
780 struct cmd_set *temp_set = temp_list->cmd_set;
782 while (temp_set->name) {
783 if (strequal(argv[0], temp_set->name)) {
784 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
785 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
786 fprintf (stderr, "Invalid command\n");
787 goto out_free;
790 result = do_cmd(cli, auth_info, temp_set,
791 binding, argc, argv);
793 goto out_free;
795 temp_set++;
799 if (argv[0]) {
800 printf("command not found: %s\n", argv[0]);
803 out_free:
804 /* moved to do_cmd()
805 if (!NT_STATUS_IS_OK(result)) {
806 printf("result was %s\n", nt_errstr(result));
810 /* NOTE: popt allocates the whole argv, including the
811 * strings, as a single block. So a single free is
812 * enough to release it -- we don't free the
813 * individual strings. rtfm. */
814 free(argv);
816 return result;
820 /* Main function */
822 int main(int argc, char *argv[])
824 int opt;
825 static char *cmdstr = NULL;
826 const char *server;
827 struct cli_state *cli = NULL;
828 static char *opt_ipaddr=NULL;
829 struct cmd_set **cmd_set;
830 struct sockaddr_storage server_ss;
831 NTSTATUS nt_status;
832 static int opt_port = 0;
833 fstring new_workgroup;
834 int result = 0;
835 TALLOC_CTX *frame = talloc_stackframe();
836 uint32_t flags = 0;
837 struct dcerpc_binding *binding = NULL;
838 const char *binding_string = NULL;
839 char *user, *domain, *q;
841 /* make sure the vars that get altered (4th field) are in
842 a fixed location or certain compilers complain */
843 poptContext pc;
844 struct poptOption long_options[] = {
845 POPT_AUTOHELP
846 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
847 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
848 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
849 POPT_COMMON_SAMBA
850 POPT_COMMON_CONNECTION
851 POPT_COMMON_CREDENTIALS
852 POPT_TABLEEND
855 load_case_tables();
857 zero_sockaddr(&server_ss);
859 setlinebuf(stdout);
861 /* the following functions are part of the Samba debugging
862 facilities. See lib/debug.c */
863 setup_logging("rpcclient", True);
865 rpcclient_auth_info = user_auth_info_init(frame);
866 if (rpcclient_auth_info == NULL) {
867 exit(1);
869 popt_common_set_auth_info(rpcclient_auth_info);
871 /* Parse options */
873 pc = poptGetContext("rpcclient", argc, (const char **) argv,
874 long_options, 0);
876 if (argc == 1) {
877 poptPrintHelp(pc, stderr, 0);
878 goto done;
881 while((opt = poptGetNextOpt(pc)) != -1) {
882 switch (opt) {
884 case 'I':
885 if (!interpret_string_addr(&server_ss,
886 opt_ipaddr,
887 AI_NUMERICHOST)) {
888 fprintf(stderr, "%s not a valid IP address\n",
889 opt_ipaddr);
890 result = 1;
891 goto done;
896 /* Get server as remaining unparsed argument. Print usage if more
897 than one unparsed argument is present. */
899 server = poptGetArg(pc);
901 if (!server || poptGetArg(pc)) {
902 poptPrintHelp(pc, stderr, 0);
903 result = 1;
904 goto done;
907 poptFreeContext(pc);
909 load_interfaces();
911 if (!init_names()) {
912 result = 1;
913 goto done;
916 /* save the workgroup...
918 FIXME!! do we need to do this for other options as well
919 (or maybe a generic way to keep lp_load() from overwriting
920 everything)? */
922 fstrcpy( new_workgroup, lp_workgroup() );
924 /* Load smb.conf file */
926 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
927 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
929 if ( strlen(new_workgroup) != 0 )
930 set_global_myworkgroup( new_workgroup );
933 * Get password
934 * from stdin if necessary
937 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
938 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
939 result = 1;
940 goto done;
943 set_cmdline_auth_info_getpass(rpcclient_auth_info);
945 if ((server[0] == '/' && server[1] == '/') ||
946 (server[0] == '\\' && server[1] == '\\')) {
947 server += 2;
950 nt_status = dcerpc_parse_binding(frame, server, &binding);
952 if (!NT_STATUS_IS_OK(nt_status)) {
954 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
955 strip_hostname(server));
956 if (!binding_string) {
957 result = 1;
958 goto done;
961 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
962 if (!NT_STATUS_IS_OK(nt_status)) {
963 result = -1;
964 goto done;
968 if (binding->transport == NCA_UNKNOWN) {
969 binding->transport = NCACN_NP;
972 if (binding->flags & DCERPC_SIGN) {
973 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
974 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
976 if (binding->flags & DCERPC_SEAL) {
977 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
978 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
980 if (binding->flags & DCERPC_AUTH_SPNEGO) {
981 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
983 if (binding->flags & DCERPC_AUTH_NTLM) {
984 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
986 if (binding->flags & DCERPC_AUTH_KRB5) {
987 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_KRB5;
990 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
991 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
992 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
994 if (get_cmdline_auth_info_use_ccache(rpcclient_auth_info)) {
995 flags |= CLI_FULL_CONNECTION_USE_CCACHE;
998 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
999 SMB_ASSERT(user != NULL);
1000 domain = talloc_strdup(frame, lp_workgroup());
1001 SMB_ASSERT(domain != NULL);
1002 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
1004 if ((q = strchr_m(user,'\\'))) {
1005 *q = 0;
1006 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
1007 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
1011 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
1012 opt_ipaddr ? &server_ss : NULL, opt_port,
1013 "IPC$", "IPC",
1014 get_cmdline_auth_info_username(rpcclient_auth_info),
1015 get_cmdline_auth_info_domain(rpcclient_auth_info),
1016 get_cmdline_auth_info_password(rpcclient_auth_info),
1017 flags,
1018 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
1019 NULL);
1021 if (!NT_STATUS_IS_OK(nt_status)) {
1022 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
1023 result = 1;
1024 goto done;
1027 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1028 nt_status = cli_cm_force_encryption(cli,
1029 get_cmdline_auth_info_username(rpcclient_auth_info),
1030 get_cmdline_auth_info_password(rpcclient_auth_info),
1031 get_cmdline_auth_info_domain(rpcclient_auth_info),
1032 "IPC$");
1033 if (!NT_STATUS_IS_OK(nt_status)) {
1034 result = 1;
1035 goto done;
1039 #if 0 /* COMMENT OUT FOR TESTING */
1040 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1041 #endif
1043 /* Load command lists */
1045 timeout = cli_set_timeout(cli, 10000);
1047 cmd_set = rpcclient_command_list;
1049 while(*cmd_set) {
1050 add_command_set(*cmd_set);
1051 add_command_set(separator_command);
1052 cmd_set++;
1055 default_transport = binding->transport;
1057 fetch_machine_sid(cli);
1059 /* Do anything specified with -c */
1060 if (cmdstr && cmdstr[0]) {
1061 char *cmd;
1062 char *p = cmdstr;
1064 result = 0;
1066 while((cmd=next_command(&p)) != NULL) {
1067 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1068 binding, cmd);
1069 SAFE_FREE(cmd);
1070 result = NT_STATUS_IS_ERR(cmd_result);
1073 goto done;
1076 /* Loop around accepting commands */
1078 while(1) {
1079 char *line = NULL;
1081 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1083 if (line == NULL)
1084 break;
1086 if (line[0] != '\n')
1087 process_cmd(rpcclient_auth_info, cli, binding, line);
1088 SAFE_FREE(line);
1091 done:
1092 if (cli != NULL) {
1093 cli_shutdown(cli);
1095 TALLOC_FREE(frame);
1096 return result;