s4-netlogon: implement dcesrv_netr_DsRAddressToSitenamesExW
[Samba/aatanasov.git] / source3 / rpcclient / rpcclient.c
blob7a20e487f27777e609aae3e4f511558f2c34a67b
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"
26 DOM_SID domain_sid;
28 static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
29 static enum dcerpc_AuthLevel pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
30 static unsigned int timeout = 0;
31 static enum dcerpc_transport_t default_transport = NCACN_NP;
33 struct user_auth_info *rpcclient_auth_info;
35 /* List to hold groups of commands.
37 * Commands are defined in a list of arrays: arrays are easy to
38 * statically declare, and lists are easier to dynamically extend.
41 static struct cmd_list {
42 struct cmd_list *prev, *next;
43 struct cmd_set *cmd_set;
44 } *cmd_list;
46 /****************************************************************************
47 handle completion of commands for readline
48 ****************************************************************************/
49 static char **completion_fn(const char *text, int start, int end)
51 #define MAX_COMPLETIONS 100
52 char **matches;
53 int i, count=0;
54 struct cmd_list *commands = cmd_list;
56 #if 0 /* JERRY */
57 /* FIXME!!! -- what to do when completing argument? */
58 /* for words not at the start of the line fallback
59 to filename completion */
60 if (start)
61 return NULL;
62 #endif
64 /* make sure we have a list of valid commands */
65 if (!commands) {
66 return NULL;
69 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
70 if (!matches) {
71 return NULL;
74 matches[count++] = SMB_STRDUP(text);
75 if (!matches[0]) {
76 SAFE_FREE(matches);
77 return NULL;
80 while (commands && count < MAX_COMPLETIONS-1) {
81 if (!commands->cmd_set) {
82 break;
85 for (i=0; commands->cmd_set[i].name; i++) {
86 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
87 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
88 commands->cmd_set[i].ntfn ) ||
89 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
90 commands->cmd_set[i].wfn))) {
91 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
92 if (!matches[count]) {
93 for (i = 0; i < count; i++) {
94 SAFE_FREE(matches[count]);
96 SAFE_FREE(matches);
97 return NULL;
99 count++;
102 commands = commands->next;
106 if (count == 2) {
107 SAFE_FREE(matches[0]);
108 matches[0] = SMB_STRDUP(matches[1]);
110 matches[count] = NULL;
111 return matches;
114 static char *next_command (char **cmdstr)
116 char *command;
117 char *p;
119 if (!cmdstr || !(*cmdstr))
120 return NULL;
122 p = strchr_m(*cmdstr, ';');
123 if (p)
124 *p = '\0';
125 command = SMB_STRDUP(*cmdstr);
126 if (p)
127 *cmdstr = p + 1;
128 else
129 *cmdstr = NULL;
131 return command;
134 /* Fetch the SID for this computer */
136 static void fetch_machine_sid(struct cli_state *cli)
138 struct policy_handle pol;
139 NTSTATUS result = NT_STATUS_OK;
140 static bool got_domain_sid;
141 TALLOC_CTX *mem_ctx;
142 struct rpc_pipe_client *lsapipe = NULL;
143 union lsa_PolicyInformation *info = NULL;
145 if (got_domain_sid) return;
147 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
148 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
149 goto error;
152 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
153 &lsapipe);
154 if (!NT_STATUS_IS_OK(result)) {
155 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
156 goto error;
159 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
160 SEC_FLAG_MAXIMUM_ALLOWED,
161 &pol);
162 if (!NT_STATUS_IS_OK(result)) {
163 goto error;
166 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
167 &pol,
168 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
169 &info);
170 if (!NT_STATUS_IS_OK(result)) {
171 goto error;
174 got_domain_sid = True;
175 sid_copy(&domain_sid, info->account_domain.sid);
177 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
178 TALLOC_FREE(lsapipe);
179 talloc_destroy(mem_ctx);
181 return;
183 error:
185 if (lsapipe) {
186 TALLOC_FREE(lsapipe);
189 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
191 if (!NT_STATUS_IS_OK(result)) {
192 fprintf(stderr, "error: %s\n", nt_errstr(result));
195 exit(1);
198 /* List the available commands on a given pipe */
200 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
201 int argc, const char **argv)
203 struct cmd_list *tmp;
204 struct cmd_set *tmp_set;
205 int i;
207 /* Usage */
209 if (argc != 2) {
210 printf("Usage: %s <pipe>\n", argv[0]);
211 return NT_STATUS_OK;
214 /* Help on one command */
216 for (tmp = cmd_list; tmp; tmp = tmp->next)
218 tmp_set = tmp->cmd_set;
220 if (!StrCaseCmp(argv[1], tmp_set->name))
222 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
224 i = 0;
225 tmp_set++;
226 while(tmp_set->name) {
227 printf("%30s", tmp_set->name);
228 tmp_set++;
229 i++;
230 if (i%3 == 0)
231 printf("\n");
234 /* drop out of the loop */
235 break;
238 printf("\n\n");
240 return NT_STATUS_OK;
243 /* Display help on commands */
245 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
246 int argc, const char **argv)
248 struct cmd_list *tmp;
249 struct cmd_set *tmp_set;
251 /* Usage */
253 if (argc > 2) {
254 printf("Usage: %s [command]\n", argv[0]);
255 return NT_STATUS_OK;
258 /* Help on one command */
260 if (argc == 2) {
261 for (tmp = cmd_list; tmp; tmp = tmp->next) {
263 tmp_set = tmp->cmd_set;
265 while(tmp_set->name) {
266 if (strequal(argv[1], tmp_set->name)) {
267 if (tmp_set->usage &&
268 tmp_set->usage[0])
269 printf("%s\n", tmp_set->usage);
270 else
271 printf("No help for %s\n", tmp_set->name);
273 return NT_STATUS_OK;
276 tmp_set++;
280 printf("No such command: %s\n", argv[1]);
281 return NT_STATUS_OK;
284 /* List all commands */
286 for (tmp = cmd_list; tmp; tmp = tmp->next) {
288 tmp_set = tmp->cmd_set;
290 while(tmp_set->name) {
292 printf("%15s\t\t%s\n", tmp_set->name,
293 tmp_set->description ? tmp_set->description:
294 "");
296 tmp_set++;
300 return NT_STATUS_OK;
303 /* Change the debug level */
305 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
306 int argc, const char **argv)
308 if (argc > 2) {
309 printf("Usage: %s [debuglevel]\n", argv[0]);
310 return NT_STATUS_OK;
313 if (argc == 2) {
314 DEBUGLEVEL = atoi(argv[1]);
317 printf("debuglevel is %d\n", DEBUGLEVEL);
319 return NT_STATUS_OK;
322 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
323 int argc, const char **argv)
325 exit(0);
326 return NT_STATUS_OK; /* NOTREACHED */
329 static NTSTATUS cmd_set_ss_level(void)
331 struct cmd_list *tmp;
333 /* Close any existing connections not at this level. */
335 for (tmp = cmd_list; tmp; tmp = tmp->next) {
336 struct cmd_set *tmp_set;
338 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
339 if (tmp_set->rpc_pipe == NULL) {
340 continue;
343 if ((tmp_set->rpc_pipe->auth->auth_type
344 != pipe_default_auth_type)
345 || (tmp_set->rpc_pipe->auth->auth_level
346 != pipe_default_auth_level)) {
347 TALLOC_FREE(tmp_set->rpc_pipe);
348 tmp_set->rpc_pipe = NULL;
352 return NT_STATUS_OK;
355 static NTSTATUS cmd_set_transport(void)
357 struct cmd_list *tmp;
359 /* Close any existing connections not at this level. */
361 for (tmp = cmd_list; tmp; tmp = tmp->next) {
362 struct cmd_set *tmp_set;
364 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
365 if (tmp_set->rpc_pipe == NULL) {
366 continue;
369 if (tmp_set->rpc_pipe->transport->transport != default_transport) {
370 TALLOC_FREE(tmp_set->rpc_pipe);
371 tmp_set->rpc_pipe = NULL;
375 return NT_STATUS_OK;
378 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
379 int argc, const char **argv)
381 const char *type = "NTLMSSP";
383 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
384 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
386 if (argc > 2) {
387 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
388 return NT_STATUS_OK;
391 if (argc == 2) {
392 type = argv[1];
393 if (strequal(type, "NTLMSSP")) {
394 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
395 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
396 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
397 } else if (strequal(type, "SCHANNEL")) {
398 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
399 } else {
400 printf("unknown type %s\n", type);
401 return NT_STATUS_INVALID_LEVEL;
405 d_printf("Setting %s - sign\n", type);
407 return cmd_set_ss_level();
410 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
411 int argc, const char **argv)
413 const char *type = "NTLMSSP";
415 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
416 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
418 if (argc > 2) {
419 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
420 return NT_STATUS_OK;
423 if (argc == 2) {
424 type = argv[1];
425 if (strequal(type, "NTLMSSP")) {
426 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
427 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
428 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
429 } else if (strequal(type, "SCHANNEL")) {
430 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
431 } else {
432 printf("unknown type %s\n", type);
433 return NT_STATUS_INVALID_LEVEL;
437 d_printf("Setting %s - sign and seal\n", type);
439 return cmd_set_ss_level();
442 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
443 int argc, const char **argv)
445 struct cmd_list *tmp;
447 if (argc > 2) {
448 printf("Usage: %s timeout\n", argv[0]);
449 return NT_STATUS_OK;
452 if (argc == 2) {
453 timeout = atoi(argv[1]);
455 for (tmp = cmd_list; tmp; tmp = tmp->next) {
457 struct cmd_set *tmp_set;
459 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
460 if (tmp_set->rpc_pipe == NULL) {
461 continue;
464 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
469 printf("timeout is %d\n", timeout);
471 return NT_STATUS_OK;
475 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
476 int argc, const char **argv)
478 pipe_default_auth_level = DCERPC_AUTH_LEVEL_NONE;
479 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
481 return cmd_set_ss_level();
484 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
485 int argc, const char **argv)
487 d_printf("Setting schannel - sign and seal\n");
488 pipe_default_auth_level = DCERPC_AUTH_LEVEL_PRIVACY;
489 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
491 return cmd_set_ss_level();
494 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
495 int argc, const char **argv)
497 d_printf("Setting schannel - sign only\n");
498 pipe_default_auth_level = DCERPC_AUTH_LEVEL_INTEGRITY;
499 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
501 return cmd_set_ss_level();
504 static NTSTATUS cmd_choose_transport(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
505 int argc, const char **argv)
507 NTSTATUS status;
509 if (argc != 2) {
510 printf("Usage: %s [NCACN_NP|NCACN_IP_TCP]\n", argv[0]);
511 return NT_STATUS_OK;
514 if (strequal(argv[1], "NCACN_NP")) {
515 default_transport = NCACN_NP;
516 } else if (strequal(argv[1], "NCACN_IP_TCP")) {
517 default_transport = NCACN_IP_TCP;
518 } else {
519 printf("transport type: %s unknown or not supported\n", argv[1]);
520 return NT_STATUS_NOT_SUPPORTED;
523 status = cmd_set_transport();
524 if (!NT_STATUS_IS_OK(status)) {
525 return status;
528 printf("default transport is now: %s\n", argv[1]);
530 return NT_STATUS_OK;
533 /* Built in rpcclient commands */
535 static struct cmd_set rpcclient_commands[] = {
537 { "GENERAL OPTIONS" },
539 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
540 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
541 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
542 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
543 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
544 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
545 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
546 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
547 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
548 { "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.", "" },
549 { "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.", "" },
550 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
551 { "transport", RPC_RTYPE_NTSTATUS, cmd_choose_transport, NULL, NULL, NULL, "Choose ncacn transport for RPC operations", "" },
552 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
554 { NULL }
557 static struct cmd_set separator_command[] = {
558 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
559 { NULL }
563 /* Various pipe commands */
565 extern struct cmd_set lsarpc_commands[];
566 extern struct cmd_set samr_commands[];
567 extern struct cmd_set spoolss_commands[];
568 extern struct cmd_set netlogon_commands[];
569 extern struct cmd_set srvsvc_commands[];
570 extern struct cmd_set dfs_commands[];
571 extern struct cmd_set ds_commands[];
572 extern struct cmd_set echo_commands[];
573 extern struct cmd_set epmapper_commands[];
574 extern struct cmd_set shutdown_commands[];
575 extern struct cmd_set test_commands[];
576 extern struct cmd_set wkssvc_commands[];
577 extern struct cmd_set ntsvcs_commands[];
578 extern struct cmd_set drsuapi_commands[];
579 extern struct cmd_set eventlog_commands[];
581 static struct cmd_set *rpcclient_command_list[] = {
582 rpcclient_commands,
583 lsarpc_commands,
584 ds_commands,
585 samr_commands,
586 spoolss_commands,
587 netlogon_commands,
588 srvsvc_commands,
589 dfs_commands,
590 echo_commands,
591 epmapper_commands,
592 shutdown_commands,
593 test_commands,
594 wkssvc_commands,
595 ntsvcs_commands,
596 drsuapi_commands,
597 eventlog_commands,
598 NULL
601 static void add_command_set(struct cmd_set *cmd_set)
603 struct cmd_list *entry;
605 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
606 DEBUG(0, ("out of memory\n"));
607 return;
610 ZERO_STRUCTP(entry);
612 entry->cmd_set = cmd_set;
613 DLIST_ADD(cmd_list, entry);
618 * Call an rpcclient function, passing an argv array.
620 * @param cmd Command to run, as a single string.
622 static NTSTATUS do_cmd(struct cli_state *cli,
623 struct user_auth_info *auth_info,
624 struct cmd_set *cmd_entry,
625 struct dcerpc_binding *binding,
626 int argc, char **argv)
628 NTSTATUS ntresult;
629 WERROR wresult;
631 TALLOC_CTX *mem_ctx;
633 /* Create mem_ctx */
635 if (!(mem_ctx = talloc_init("do_cmd"))) {
636 DEBUG(0, ("talloc_init() failed\n"));
637 return NT_STATUS_NO_MEMORY;
640 /* Open pipe */
642 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
643 switch (pipe_default_auth_type) {
644 case PIPE_AUTH_TYPE_NONE:
645 ntresult = cli_rpc_pipe_open_noauth_transport(
646 cli, default_transport,
647 cmd_entry->interface,
648 &cmd_entry->rpc_pipe);
649 break;
650 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
651 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
652 cli, cmd_entry->interface,
653 default_transport,
654 pipe_default_auth_level,
655 get_cmdline_auth_info_domain(auth_info),
656 get_cmdline_auth_info_username(auth_info),
657 get_cmdline_auth_info_password(auth_info),
658 &cmd_entry->rpc_pipe);
659 break;
660 case PIPE_AUTH_TYPE_NTLMSSP:
661 ntresult = cli_rpc_pipe_open_ntlmssp(
662 cli, cmd_entry->interface,
663 default_transport,
664 pipe_default_auth_level,
665 get_cmdline_auth_info_domain(auth_info),
666 get_cmdline_auth_info_username(auth_info),
667 get_cmdline_auth_info_password(auth_info),
668 &cmd_entry->rpc_pipe);
669 break;
670 case PIPE_AUTH_TYPE_SCHANNEL:
671 ntresult = cli_rpc_pipe_open_schannel(
672 cli, cmd_entry->interface,
673 default_transport,
674 pipe_default_auth_level,
675 get_cmdline_auth_info_domain(auth_info),
676 &cmd_entry->rpc_pipe);
677 break;
678 default:
679 DEBUG(0, ("Could not initialise %s. Invalid "
680 "auth type %u\n",
681 get_pipe_name_from_iface(
682 cmd_entry->interface),
683 pipe_default_auth_type ));
684 return NT_STATUS_UNSUCCESSFUL;
686 if (!NT_STATUS_IS_OK(ntresult)) {
687 DEBUG(0, ("Could not initialise %s. Error was %s\n",
688 get_pipe_name_from_iface(
689 cmd_entry->interface),
690 nt_errstr(ntresult) ));
691 return ntresult;
694 if (ndr_syntax_id_equal(cmd_entry->interface,
695 &ndr_table_netlogon.syntax_id)) {
696 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
697 uint32 sec_channel_type;
698 uchar trust_password[16];
699 const char *machine_account;
701 if (!get_trust_pw_hash(get_cmdline_auth_info_domain(auth_info),
702 trust_password, &machine_account,
703 &sec_channel_type))
705 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
708 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
709 cli->desthost, /* server name */
710 get_cmdline_auth_info_domain(auth_info), /* domain */
711 global_myname(), /* client name */
712 machine_account, /* machine account name */
713 trust_password,
714 sec_channel_type,
715 &neg_flags);
717 if (!NT_STATUS_IS_OK(ntresult)) {
718 DEBUG(0, ("Could not initialise credentials for %s.\n",
719 get_pipe_name_from_iface(
720 cmd_entry->interface)));
721 return ntresult;
726 /* Run command */
728 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
729 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
730 if (!NT_STATUS_IS_OK(ntresult)) {
731 printf("result was %s\n", nt_errstr(ntresult));
733 } else {
734 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
735 /* print out the DOS error */
736 if (!W_ERROR_IS_OK(wresult)) {
737 printf( "result was %s\n", win_errstr(wresult));
739 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
742 /* Cleanup */
744 talloc_destroy(mem_ctx);
746 return ntresult;
751 * Process a command entered at the prompt or as part of -c
753 * @returns The NTSTATUS from running the command.
755 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
756 struct cli_state *cli,
757 struct dcerpc_binding *binding,
758 char *cmd)
760 struct cmd_list *temp_list;
761 NTSTATUS result = NT_STATUS_OK;
762 int ret;
763 int argc;
764 char **argv = NULL;
766 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
767 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
768 return NT_STATUS_UNSUCCESSFUL;
772 /* Walk through a dlist of arrays of commands. */
773 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
774 struct cmd_set *temp_set = temp_list->cmd_set;
776 while (temp_set->name) {
777 if (strequal(argv[0], temp_set->name)) {
778 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
779 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
780 fprintf (stderr, "Invalid command\n");
781 goto out_free;
784 result = do_cmd(cli, auth_info, temp_set,
785 binding, argc, argv);
787 goto out_free;
789 temp_set++;
793 if (argv[0]) {
794 printf("command not found: %s\n", argv[0]);
797 out_free:
798 /* moved to do_cmd()
799 if (!NT_STATUS_IS_OK(result)) {
800 printf("result was %s\n", nt_errstr(result));
804 /* NOTE: popt allocates the whole argv, including the
805 * strings, as a single block. So a single free is
806 * enough to release it -- we don't free the
807 * individual strings. rtfm. */
808 free(argv);
810 return result;
814 /* Main function */
816 int main(int argc, char *argv[])
818 int opt;
819 static char *cmdstr = NULL;
820 const char *server;
821 struct cli_state *cli = NULL;
822 static char *opt_ipaddr=NULL;
823 struct cmd_set **cmd_set;
824 struct sockaddr_storage server_ss;
825 NTSTATUS nt_status;
826 static int opt_port = 0;
827 fstring new_workgroup;
828 int result = 0;
829 TALLOC_CTX *frame = talloc_stackframe();
830 uint32_t flags = 0;
831 struct dcerpc_binding *binding = NULL;
832 const char *binding_string = NULL;
833 char *user, *domain, *q;
835 /* make sure the vars that get altered (4th field) are in
836 a fixed location or certain compilers complain */
837 poptContext pc;
838 struct poptOption long_options[] = {
839 POPT_AUTOHELP
840 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
841 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
842 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
843 POPT_COMMON_SAMBA
844 POPT_COMMON_CONNECTION
845 POPT_COMMON_CREDENTIALS
846 POPT_TABLEEND
849 load_case_tables();
851 zero_sockaddr(&server_ss);
853 setlinebuf(stdout);
855 /* the following functions are part of the Samba debugging
856 facilities. See lib/debug.c */
857 setup_logging("rpcclient", True);
859 rpcclient_auth_info = user_auth_info_init(frame);
860 if (rpcclient_auth_info == NULL) {
861 exit(1);
863 popt_common_set_auth_info(rpcclient_auth_info);
865 /* Parse options */
867 pc = poptGetContext("rpcclient", argc, (const char **) argv,
868 long_options, 0);
870 if (argc == 1) {
871 poptPrintHelp(pc, stderr, 0);
872 goto done;
875 while((opt = poptGetNextOpt(pc)) != -1) {
876 switch (opt) {
878 case 'I':
879 if (!interpret_string_addr(&server_ss,
880 opt_ipaddr,
881 AI_NUMERICHOST)) {
882 fprintf(stderr, "%s not a valid IP address\n",
883 opt_ipaddr);
884 result = 1;
885 goto done;
890 /* Get server as remaining unparsed argument. Print usage if more
891 than one unparsed argument is present. */
893 server = poptGetArg(pc);
895 if (!server || poptGetArg(pc)) {
896 poptPrintHelp(pc, stderr, 0);
897 result = 1;
898 goto done;
901 poptFreeContext(pc);
903 load_interfaces();
905 if (!init_names()) {
906 result = 1;
907 goto done;
910 /* save the workgroup...
912 FIXME!! do we need to do this for other options as well
913 (or maybe a generic way to keep lp_load() from overwriting
914 everything)? */
916 fstrcpy( new_workgroup, lp_workgroup() );
918 /* Load smb.conf file */
920 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
921 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
923 if ( strlen(new_workgroup) != 0 )
924 set_global_myworkgroup( new_workgroup );
927 * Get password
928 * from stdin if necessary
931 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
932 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
933 result = 1;
934 goto done;
937 set_cmdline_auth_info_getpass(rpcclient_auth_info);
939 if ((server[0] == '/' && server[1] == '/') ||
940 (server[0] == '\\' && server[1] == '\\')) {
941 server += 2;
944 nt_status = dcerpc_parse_binding(frame, server, &binding);
946 if (!NT_STATUS_IS_OK(nt_status)) {
948 binding_string = talloc_asprintf(frame, "ncacn_np:%s",
949 strip_hostname(server));
950 if (!binding_string) {
951 result = 1;
952 goto done;
955 nt_status = dcerpc_parse_binding(frame, binding_string, &binding);
956 if (!NT_STATUS_IS_OK(nt_status)) {
957 result = -1;
958 goto done;
962 if (binding->transport == NCA_UNKNOWN) {
963 binding->transport = NCACN_NP;
966 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
967 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
968 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
971 user = talloc_strdup(frame, get_cmdline_auth_info_username(rpcclient_auth_info));
972 SMB_ASSERT(user != NULL);
973 domain = talloc_strdup(frame, lp_workgroup());
974 SMB_ASSERT(domain != NULL);
975 set_cmdline_auth_info_domain(rpcclient_auth_info, domain);
977 if ((q = strchr_m(user,'\\'))) {
978 *q = 0;
979 set_cmdline_auth_info_domain(rpcclient_auth_info, user);
980 set_cmdline_auth_info_username(rpcclient_auth_info, q+1);
984 nt_status = cli_full_connection(&cli, global_myname(), binding->host,
985 opt_ipaddr ? &server_ss : NULL, opt_port,
986 "IPC$", "IPC",
987 get_cmdline_auth_info_username(rpcclient_auth_info),
988 get_cmdline_auth_info_domain(rpcclient_auth_info),
989 get_cmdline_auth_info_password(rpcclient_auth_info),
990 flags,
991 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
992 NULL);
994 if (!NT_STATUS_IS_OK(nt_status)) {
995 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
996 result = 1;
997 goto done;
1000 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
1001 nt_status = cli_cm_force_encryption(cli,
1002 get_cmdline_auth_info_username(rpcclient_auth_info),
1003 get_cmdline_auth_info_password(rpcclient_auth_info),
1004 get_cmdline_auth_info_domain(rpcclient_auth_info),
1005 "IPC$");
1006 if (!NT_STATUS_IS_OK(nt_status)) {
1007 result = 1;
1008 goto done;
1012 #if 0 /* COMMENT OUT FOR TESTING */
1013 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
1014 #endif
1016 /* Load command lists */
1018 timeout = cli_set_timeout(cli, 10000);
1020 cmd_set = rpcclient_command_list;
1022 while(*cmd_set) {
1023 add_command_set(*cmd_set);
1024 add_command_set(separator_command);
1025 cmd_set++;
1028 default_transport = binding->transport;
1030 fetch_machine_sid(cli);
1032 /* Do anything specified with -c */
1033 if (cmdstr && cmdstr[0]) {
1034 char *cmd;
1035 char *p = cmdstr;
1037 result = 0;
1039 while((cmd=next_command(&p)) != NULL) {
1040 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli,
1041 binding, cmd);
1042 SAFE_FREE(cmd);
1043 result = NT_STATUS_IS_ERR(cmd_result);
1046 goto done;
1049 /* Loop around accepting commands */
1051 while(1) {
1052 char *line = NULL;
1054 line = smb_readline("rpcclient $> ", NULL, completion_fn);
1056 if (line == NULL)
1057 break;
1059 if (line[0] != '\n')
1060 process_cmd(rpcclient_auth_info, cli, binding, line);
1061 SAFE_FREE(line);
1064 done:
1065 if (cli != NULL) {
1066 cli_shutdown(cli);
1068 TALLOC_FREE(frame);
1069 return result;