s3:idmap_tdb2: filter out of range mappings in default idmap config
[Samba/bb.git] / source / rpcclient / rpcclient.c
blob421c681489ccbb52ed1ded3ce965617e1a34d2c1
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"
25 DOM_SID domain_sid;
27 static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
28 static enum pipe_auth_level pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
29 static unsigned int timeout = 0;
31 /* List to hold groups of commands.
33 * Commands are defined in a list of arrays: arrays are easy to
34 * statically declare, and lists are easier to dynamically extend.
37 static struct cmd_list {
38 struct cmd_list *prev, *next;
39 struct cmd_set *cmd_set;
40 } *cmd_list;
42 /****************************************************************************
43 handle completion of commands for readline
44 ****************************************************************************/
45 static char **completion_fn(const char *text, int start, int end)
47 #define MAX_COMPLETIONS 100
48 char **matches;
49 int i, count=0;
50 struct cmd_list *commands = cmd_list;
52 #if 0 /* JERRY */
53 /* FIXME!!! -- what to do when completing argument? */
54 /* for words not at the start of the line fallback
55 to filename completion */
56 if (start)
57 return NULL;
58 #endif
60 /* make sure we have a list of valid commands */
61 if (!commands) {
62 return NULL;
65 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
66 if (!matches) {
67 return NULL;
70 matches[count++] = SMB_STRDUP(text);
71 if (!matches[0]) {
72 SAFE_FREE(matches);
73 return NULL;
76 while (commands && count < MAX_COMPLETIONS-1) {
77 if (!commands->cmd_set) {
78 break;
81 for (i=0; commands->cmd_set[i].name; i++) {
82 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
83 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
84 commands->cmd_set[i].ntfn ) ||
85 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
86 commands->cmd_set[i].wfn))) {
87 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
88 if (!matches[count]) {
89 for (i = 0; i < count; i++) {
90 SAFE_FREE(matches[count]);
92 SAFE_FREE(matches);
93 return NULL;
95 count++;
98 commands = commands->next;
102 if (count == 2) {
103 SAFE_FREE(matches[0]);
104 matches[0] = SMB_STRDUP(matches[1]);
106 matches[count] = NULL;
107 return matches;
110 static char *next_command (char **cmdstr)
112 char *command;
113 char *p;
115 if (!cmdstr || !(*cmdstr))
116 return NULL;
118 p = strchr_m(*cmdstr, ';');
119 if (p)
120 *p = '\0';
121 command = SMB_STRDUP(*cmdstr);
122 if (p)
123 *cmdstr = p + 1;
124 else
125 *cmdstr = NULL;
127 return command;
130 /* Fetch the SID for this computer */
132 static void fetch_machine_sid(struct cli_state *cli)
134 POLICY_HND pol;
135 NTSTATUS result = NT_STATUS_OK;
136 static bool got_domain_sid;
137 TALLOC_CTX *mem_ctx;
138 struct rpc_pipe_client *lsapipe = NULL;
139 union lsa_PolicyInformation *info = NULL;
141 if (got_domain_sid) return;
143 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
144 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
145 goto error;
148 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
149 &lsapipe);
150 if (!NT_STATUS_IS_OK(result)) {
151 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
152 goto error;
155 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
156 SEC_RIGHTS_MAXIMUM_ALLOWED,
157 &pol);
158 if (!NT_STATUS_IS_OK(result)) {
159 goto error;
162 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
163 &pol,
164 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
165 &info);
166 if (!NT_STATUS_IS_OK(result)) {
167 goto error;
170 got_domain_sid = True;
171 sid_copy(&domain_sid, info->account_domain.sid);
173 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
174 TALLOC_FREE(lsapipe);
175 talloc_destroy(mem_ctx);
177 return;
179 error:
181 if (lsapipe) {
182 TALLOC_FREE(lsapipe);
185 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
187 if (!NT_STATUS_IS_OK(result)) {
188 fprintf(stderr, "error: %s\n", nt_errstr(result));
191 exit(1);
194 /* List the available commands on a given pipe */
196 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
197 int argc, const char **argv)
199 struct cmd_list *tmp;
200 struct cmd_set *tmp_set;
201 int i;
203 /* Usage */
205 if (argc != 2) {
206 printf("Usage: %s <pipe>\n", argv[0]);
207 return NT_STATUS_OK;
210 /* Help on one command */
212 for (tmp = cmd_list; tmp; tmp = tmp->next)
214 tmp_set = tmp->cmd_set;
216 if (!StrCaseCmp(argv[1], tmp_set->name))
218 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
220 i = 0;
221 tmp_set++;
222 while(tmp_set->name) {
223 printf("%30s", tmp_set->name);
224 tmp_set++;
225 i++;
226 if (i%3 == 0)
227 printf("\n");
230 /* drop out of the loop */
231 break;
234 printf("\n\n");
236 return NT_STATUS_OK;
239 /* Display help on commands */
241 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
242 int argc, const char **argv)
244 struct cmd_list *tmp;
245 struct cmd_set *tmp_set;
247 /* Usage */
249 if (argc > 2) {
250 printf("Usage: %s [command]\n", argv[0]);
251 return NT_STATUS_OK;
254 /* Help on one command */
256 if (argc == 2) {
257 for (tmp = cmd_list; tmp; tmp = tmp->next) {
259 tmp_set = tmp->cmd_set;
261 while(tmp_set->name) {
262 if (strequal(argv[1], tmp_set->name)) {
263 if (tmp_set->usage &&
264 tmp_set->usage[0])
265 printf("%s\n", tmp_set->usage);
266 else
267 printf("No help for %s\n", tmp_set->name);
269 return NT_STATUS_OK;
272 tmp_set++;
276 printf("No such command: %s\n", argv[1]);
277 return NT_STATUS_OK;
280 /* List all commands */
282 for (tmp = cmd_list; tmp; tmp = tmp->next) {
284 tmp_set = tmp->cmd_set;
286 while(tmp_set->name) {
288 printf("%15s\t\t%s\n", tmp_set->name,
289 tmp_set->description ? tmp_set->description:
290 "");
292 tmp_set++;
296 return NT_STATUS_OK;
299 /* Change the debug level */
301 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
302 int argc, const char **argv)
304 if (argc > 2) {
305 printf("Usage: %s [debuglevel]\n", argv[0]);
306 return NT_STATUS_OK;
309 if (argc == 2) {
310 DEBUGLEVEL = atoi(argv[1]);
313 printf("debuglevel is %d\n", DEBUGLEVEL);
315 return NT_STATUS_OK;
318 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
319 int argc, const char **argv)
321 exit(0);
322 return NT_STATUS_OK; /* NOTREACHED */
325 static NTSTATUS cmd_set_ss_level(void)
327 struct cmd_list *tmp;
329 /* Close any existing connections not at this level. */
331 for (tmp = cmd_list; tmp; tmp = tmp->next) {
332 struct cmd_set *tmp_set;
334 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
335 if (tmp_set->rpc_pipe == NULL) {
336 continue;
339 if ((tmp_set->rpc_pipe->auth->auth_type
340 != pipe_default_auth_type)
341 || (tmp_set->rpc_pipe->auth->auth_level
342 != pipe_default_auth_level)) {
343 TALLOC_FREE(tmp_set->rpc_pipe);
344 tmp_set->rpc_pipe = NULL;
348 return NT_STATUS_OK;
351 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
352 int argc, const char **argv)
354 const char *type = "NTLMSSP";
356 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
357 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
359 if (argc > 2) {
360 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
361 return NT_STATUS_OK;
364 if (argc == 2) {
365 type = argv[1];
366 if (strequal(type, "NTLMSSP")) {
367 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
368 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
369 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
370 } else if (strequal(type, "SCHANNEL")) {
371 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
372 } else {
373 printf("unknown type %s\n", type);
374 return NT_STATUS_INVALID_LEVEL;
378 d_printf("Setting %s - sign\n", type);
380 return cmd_set_ss_level();
383 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
384 int argc, const char **argv)
386 const char *type = "NTLMSSP";
388 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
389 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
391 if (argc > 2) {
392 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
393 return NT_STATUS_OK;
396 if (argc == 2) {
397 type = argv[1];
398 if (strequal(type, "NTLMSSP")) {
399 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
400 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
401 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
402 } else if (strequal(type, "SCHANNEL")) {
403 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
404 } else {
405 printf("unknown type %s\n", type);
406 return NT_STATUS_INVALID_LEVEL;
410 d_printf("Setting %s - sign and seal\n", type);
412 return cmd_set_ss_level();
415 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
416 int argc, const char **argv)
418 struct cmd_list *tmp;
420 if (argc > 2) {
421 printf("Usage: %s timeout\n", argv[0]);
422 return NT_STATUS_OK;
425 if (argc == 2) {
426 timeout = atoi(argv[1]);
428 for (tmp = cmd_list; tmp; tmp = tmp->next) {
430 struct cmd_set *tmp_set;
432 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
433 if (tmp_set->rpc_pipe == NULL) {
434 continue;
437 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
442 printf("timeout is %d\n", timeout);
444 return NT_STATUS_OK;
448 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
449 int argc, const char **argv)
451 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
452 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
454 return cmd_set_ss_level();
457 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
458 int argc, const char **argv)
460 d_printf("Setting schannel - sign and seal\n");
461 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
462 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
464 return cmd_set_ss_level();
467 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
468 int argc, const char **argv)
470 d_printf("Setting schannel - sign only\n");
471 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
472 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
474 return cmd_set_ss_level();
478 /* Built in rpcclient commands */
480 static struct cmd_set rpcclient_commands[] = {
482 { "GENERAL OPTIONS" },
484 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
485 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
486 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
487 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
488 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
489 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
490 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
491 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
492 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
493 { "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.", "" },
494 { "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.", "" },
495 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
496 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
498 { NULL }
501 static struct cmd_set separator_command[] = {
502 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
503 { NULL }
507 /* Various pipe commands */
509 extern struct cmd_set lsarpc_commands[];
510 extern struct cmd_set samr_commands[];
511 extern struct cmd_set spoolss_commands[];
512 extern struct cmd_set netlogon_commands[];
513 extern struct cmd_set srvsvc_commands[];
514 extern struct cmd_set dfs_commands[];
515 extern struct cmd_set ds_commands[];
516 extern struct cmd_set echo_commands[];
517 extern struct cmd_set shutdown_commands[];
518 extern struct cmd_set test_commands[];
519 extern struct cmd_set wkssvc_commands[];
520 extern struct cmd_set ntsvcs_commands[];
521 extern struct cmd_set drsuapi_commands[];
523 static struct cmd_set *rpcclient_command_list[] = {
524 rpcclient_commands,
525 lsarpc_commands,
526 ds_commands,
527 samr_commands,
528 spoolss_commands,
529 netlogon_commands,
530 srvsvc_commands,
531 dfs_commands,
532 echo_commands,
533 shutdown_commands,
534 test_commands,
535 wkssvc_commands,
536 ntsvcs_commands,
537 drsuapi_commands,
538 NULL
541 static void add_command_set(struct cmd_set *cmd_set)
543 struct cmd_list *entry;
545 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
546 DEBUG(0, ("out of memory\n"));
547 return;
550 ZERO_STRUCTP(entry);
552 entry->cmd_set = cmd_set;
553 DLIST_ADD(cmd_list, entry);
558 * Call an rpcclient function, passing an argv array.
560 * @param cmd Command to run, as a single string.
562 static NTSTATUS do_cmd(struct cli_state *cli,
563 struct cmd_set *cmd_entry,
564 int argc, char **argv)
566 NTSTATUS ntresult;
567 WERROR wresult;
569 TALLOC_CTX *mem_ctx;
571 /* Create mem_ctx */
573 if (!(mem_ctx = talloc_init("do_cmd"))) {
574 DEBUG(0, ("talloc_init() failed\n"));
575 return NT_STATUS_NO_MEMORY;
578 /* Open pipe */
580 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
581 switch (pipe_default_auth_type) {
582 case PIPE_AUTH_TYPE_NONE:
583 ntresult = cli_rpc_pipe_open_noauth(
584 cli, cmd_entry->interface,
585 &cmd_entry->rpc_pipe);
586 break;
587 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
588 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
589 cli, cmd_entry->interface,
590 pipe_default_auth_level,
591 lp_workgroup(),
592 get_cmdline_auth_info_username(),
593 get_cmdline_auth_info_password(),
594 &cmd_entry->rpc_pipe);
595 break;
596 case PIPE_AUTH_TYPE_NTLMSSP:
597 ntresult = cli_rpc_pipe_open_ntlmssp(
598 cli, cmd_entry->interface,
599 pipe_default_auth_level,
600 lp_workgroup(),
601 get_cmdline_auth_info_username(),
602 get_cmdline_auth_info_password(),
603 &cmd_entry->rpc_pipe);
604 break;
605 case PIPE_AUTH_TYPE_SCHANNEL:
606 ntresult = cli_rpc_pipe_open_schannel(
607 cli, cmd_entry->interface,
608 pipe_default_auth_level,
609 lp_workgroup(),
610 &cmd_entry->rpc_pipe);
611 break;
612 default:
613 DEBUG(0, ("Could not initialise %s. Invalid "
614 "auth type %u\n",
615 cli_get_pipe_name_from_iface(
616 debug_ctx(), cli,
617 cmd_entry->interface),
618 pipe_default_auth_type ));
619 return NT_STATUS_UNSUCCESSFUL;
621 if (!NT_STATUS_IS_OK(ntresult)) {
622 DEBUG(0, ("Could not initialise %s. Error was %s\n",
623 cli_get_pipe_name_from_iface(
624 debug_ctx(), cli,
625 cmd_entry->interface),
626 nt_errstr(ntresult) ));
627 return ntresult;
630 if (ndr_syntax_id_equal(cmd_entry->interface,
631 &ndr_table_netlogon.syntax_id)) {
632 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
633 uint32 sec_channel_type;
634 uchar trust_password[16];
636 if (!secrets_fetch_trust_account_password(lp_workgroup(),
637 trust_password,
638 NULL, &sec_channel_type)) {
639 return NT_STATUS_UNSUCCESSFUL;
642 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
643 cli->desthost, /* server name */
644 lp_workgroup(), /* domain */
645 global_myname(), /* client name */
646 global_myname(), /* machine account name */
647 trust_password,
648 sec_channel_type,
649 &neg_flags);
651 if (!NT_STATUS_IS_OK(ntresult)) {
652 DEBUG(0, ("Could not initialise credentials for %s.\n",
653 cli_get_pipe_name_from_iface(
654 debug_ctx(), cli,
655 cmd_entry->interface)));
656 return ntresult;
661 /* Run command */
663 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
664 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
665 if (!NT_STATUS_IS_OK(ntresult)) {
666 printf("result was %s\n", nt_errstr(ntresult));
668 } else {
669 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
670 /* print out the DOS error */
671 if (!W_ERROR_IS_OK(wresult)) {
672 printf( "result was %s\n", dos_errstr(wresult));
674 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
677 /* Cleanup */
679 talloc_destroy(mem_ctx);
681 return ntresult;
686 * Process a command entered at the prompt or as part of -c
688 * @returns The NTSTATUS from running the command.
690 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
692 struct cmd_list *temp_list;
693 NTSTATUS result = NT_STATUS_OK;
694 int ret;
695 int argc;
696 char **argv = NULL;
698 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
699 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
700 return NT_STATUS_UNSUCCESSFUL;
704 /* Walk through a dlist of arrays of commands. */
705 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
706 struct cmd_set *temp_set = temp_list->cmd_set;
708 while (temp_set->name) {
709 if (strequal(argv[0], temp_set->name)) {
710 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
711 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
712 fprintf (stderr, "Invalid command\n");
713 goto out_free;
716 result = do_cmd(cli, temp_set, argc, argv);
718 goto out_free;
720 temp_set++;
724 if (argv[0]) {
725 printf("command not found: %s\n", argv[0]);
728 out_free:
729 /* moved to do_cmd()
730 if (!NT_STATUS_IS_OK(result)) {
731 printf("result was %s\n", nt_errstr(result));
735 /* NOTE: popt allocates the whole argv, including the
736 * strings, as a single block. So a single free is
737 * enough to release it -- we don't free the
738 * individual strings. rtfm. */
739 free(argv);
741 return result;
745 /* Main function */
747 int main(int argc, char *argv[])
749 int opt;
750 static char *cmdstr = NULL;
751 const char *server;
752 struct cli_state *cli = NULL;
753 static char *opt_ipaddr=NULL;
754 struct cmd_set **cmd_set;
755 struct sockaddr_storage server_ss;
756 NTSTATUS nt_status;
757 static int opt_port = 0;
758 fstring new_workgroup;
759 int result = 0;
760 TALLOC_CTX *frame = talloc_stackframe();
761 uint32_t flags = 0;
763 /* make sure the vars that get altered (4th field) are in
764 a fixed location or certain compilers complain */
765 poptContext pc;
766 struct poptOption long_options[] = {
767 POPT_AUTOHELP
768 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
769 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
770 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
771 POPT_COMMON_SAMBA
772 POPT_COMMON_CONNECTION
773 POPT_COMMON_CREDENTIALS
774 POPT_TABLEEND
777 load_case_tables();
779 zero_sockaddr(&server_ss);
781 setlinebuf(stdout);
783 /* the following functions are part of the Samba debugging
784 facilities. See lib/debug.c */
785 setup_logging("rpcclient", True);
787 /* Parse options */
789 pc = poptGetContext("rpcclient", argc, (const char **) argv,
790 long_options, 0);
792 if (argc == 1) {
793 poptPrintHelp(pc, stderr, 0);
794 goto done;
797 while((opt = poptGetNextOpt(pc)) != -1) {
798 switch (opt) {
800 case 'I':
801 if (!interpret_string_addr(&server_ss,
802 opt_ipaddr,
803 AI_NUMERICHOST)) {
804 fprintf(stderr, "%s not a valid IP address\n",
805 opt_ipaddr);
806 result = 1;
807 goto done;
812 /* Get server as remaining unparsed argument. Print usage if more
813 than one unparsed argument is present. */
815 server = poptGetArg(pc);
817 if (!server || poptGetArg(pc)) {
818 poptPrintHelp(pc, stderr, 0);
819 result = 1;
820 goto done;
823 poptFreeContext(pc);
825 load_interfaces();
827 if (!init_names()) {
828 result = 1;
829 goto done;
832 /* save the workgroup...
834 FIXME!! do we need to do this for other options as well
835 (or maybe a generic way to keep lp_load() from overwriting
836 everything)? */
838 fstrcpy( new_workgroup, lp_workgroup() );
840 /* Load smb.conf file */
842 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
843 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
845 if ( strlen(new_workgroup) != 0 )
846 set_global_myworkgroup( new_workgroup );
849 * Get password
850 * from stdin if necessary
853 if (get_cmdline_auth_info_use_machine_account() &&
854 !set_cmdline_auth_info_machine_account_creds()) {
855 result = 1;
856 goto done;
859 if (!get_cmdline_auth_info_got_pass()) {
860 char *pass = getpass("Password:");
861 if (pass) {
862 set_cmdline_auth_info_password(pass);
866 if ((server[0] == '/' && server[1] == '/') ||
867 (server[0] == '\\' && server[1] == '\\')) {
868 server += 2;
871 if (get_cmdline_auth_info_use_kerberos()) {
872 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
873 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
877 nt_status = cli_full_connection(&cli, global_myname(), server,
878 opt_ipaddr ? &server_ss : NULL, opt_port,
879 "IPC$", "IPC",
880 get_cmdline_auth_info_username(),
881 lp_workgroup(),
882 get_cmdline_auth_info_password(),
883 flags,
884 get_cmdline_auth_info_signing_state(),NULL);
886 if (!NT_STATUS_IS_OK(nt_status)) {
887 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
888 result = 1;
889 goto done;
892 if (get_cmdline_auth_info_smb_encrypt()) {
893 nt_status = cli_cm_force_encryption(cli,
894 get_cmdline_auth_info_username(),
895 get_cmdline_auth_info_password(),
896 lp_workgroup(),
897 "IPC$");
898 if (!NT_STATUS_IS_OK(nt_status)) {
899 result = 1;
900 goto done;
904 #if 0 /* COMMENT OUT FOR TESTING */
905 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
906 #endif
908 /* Load command lists */
910 timeout = cli_set_timeout(cli, 10000);
912 cmd_set = rpcclient_command_list;
914 while(*cmd_set) {
915 add_command_set(*cmd_set);
916 add_command_set(separator_command);
917 cmd_set++;
920 fetch_machine_sid(cli);
922 /* Do anything specified with -c */
923 if (cmdstr && cmdstr[0]) {
924 char *cmd;
925 char *p = cmdstr;
927 result = 0;
929 while((cmd=next_command(&p)) != NULL) {
930 NTSTATUS cmd_result = process_cmd(cli, cmd);
931 SAFE_FREE(cmd);
932 result = NT_STATUS_IS_ERR(cmd_result);
935 goto done;
938 /* Loop around accepting commands */
940 while(1) {
941 char *line = NULL;
943 line = smb_readline("rpcclient $> ", NULL, completion_fn);
945 if (line == NULL)
946 break;
948 if (line[0] != '\n')
949 process_cmd(cli, line);
950 SAFE_FREE(line);
953 done:
954 if (cli != NULL) {
955 cli_shutdown(cli);
957 TALLOC_FREE(frame);
958 return result;