s3-secdesc: use SEC_FLAG_MAXIMUM_ALLOWED instead of SEC_RIGHTS_MAXIMUM_ALLOWED.
[Samba/gbeck.git] / source3 / rpcclient / rpcclient.c
blobceeeae7ea68f319891ae16026fd72e062a8f90de
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 pipe_auth_level pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
30 static unsigned int timeout = 0;
32 struct user_auth_info *rpcclient_auth_info;
34 /* List to hold groups of commands.
36 * Commands are defined in a list of arrays: arrays are easy to
37 * statically declare, and lists are easier to dynamically extend.
40 static struct cmd_list {
41 struct cmd_list *prev, *next;
42 struct cmd_set *cmd_set;
43 } *cmd_list;
45 /****************************************************************************
46 handle completion of commands for readline
47 ****************************************************************************/
48 static char **completion_fn(const char *text, int start, int end)
50 #define MAX_COMPLETIONS 100
51 char **matches;
52 int i, count=0;
53 struct cmd_list *commands = cmd_list;
55 #if 0 /* JERRY */
56 /* FIXME!!! -- what to do when completing argument? */
57 /* for words not at the start of the line fallback
58 to filename completion */
59 if (start)
60 return NULL;
61 #endif
63 /* make sure we have a list of valid commands */
64 if (!commands) {
65 return NULL;
68 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
69 if (!matches) {
70 return NULL;
73 matches[count++] = SMB_STRDUP(text);
74 if (!matches[0]) {
75 SAFE_FREE(matches);
76 return NULL;
79 while (commands && count < MAX_COMPLETIONS-1) {
80 if (!commands->cmd_set) {
81 break;
84 for (i=0; commands->cmd_set[i].name; i++) {
85 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
86 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
87 commands->cmd_set[i].ntfn ) ||
88 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
89 commands->cmd_set[i].wfn))) {
90 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
91 if (!matches[count]) {
92 for (i = 0; i < count; i++) {
93 SAFE_FREE(matches[count]);
95 SAFE_FREE(matches);
96 return NULL;
98 count++;
101 commands = commands->next;
105 if (count == 2) {
106 SAFE_FREE(matches[0]);
107 matches[0] = SMB_STRDUP(matches[1]);
109 matches[count] = NULL;
110 return matches;
113 static char *next_command (char **cmdstr)
115 char *command;
116 char *p;
118 if (!cmdstr || !(*cmdstr))
119 return NULL;
121 p = strchr_m(*cmdstr, ';');
122 if (p)
123 *p = '\0';
124 command = SMB_STRDUP(*cmdstr);
125 if (p)
126 *cmdstr = p + 1;
127 else
128 *cmdstr = NULL;
130 return command;
133 /* Fetch the SID for this computer */
135 static void fetch_machine_sid(struct cli_state *cli)
137 struct policy_handle pol;
138 NTSTATUS result = NT_STATUS_OK;
139 static bool got_domain_sid;
140 TALLOC_CTX *mem_ctx;
141 struct rpc_pipe_client *lsapipe = NULL;
142 union lsa_PolicyInformation *info = NULL;
144 if (got_domain_sid) return;
146 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
147 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
148 goto error;
151 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
152 &lsapipe);
153 if (!NT_STATUS_IS_OK(result)) {
154 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
155 goto error;
158 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
159 SEC_FLAG_MAXIMUM_ALLOWED,
160 &pol);
161 if (!NT_STATUS_IS_OK(result)) {
162 goto error;
165 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
166 &pol,
167 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
168 &info);
169 if (!NT_STATUS_IS_OK(result)) {
170 goto error;
173 got_domain_sid = True;
174 sid_copy(&domain_sid, info->account_domain.sid);
176 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
177 TALLOC_FREE(lsapipe);
178 talloc_destroy(mem_ctx);
180 return;
182 error:
184 if (lsapipe) {
185 TALLOC_FREE(lsapipe);
188 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
190 if (!NT_STATUS_IS_OK(result)) {
191 fprintf(stderr, "error: %s\n", nt_errstr(result));
194 exit(1);
197 /* List the available commands on a given pipe */
199 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
200 int argc, const char **argv)
202 struct cmd_list *tmp;
203 struct cmd_set *tmp_set;
204 int i;
206 /* Usage */
208 if (argc != 2) {
209 printf("Usage: %s <pipe>\n", argv[0]);
210 return NT_STATUS_OK;
213 /* Help on one command */
215 for (tmp = cmd_list; tmp; tmp = tmp->next)
217 tmp_set = tmp->cmd_set;
219 if (!StrCaseCmp(argv[1], tmp_set->name))
221 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
223 i = 0;
224 tmp_set++;
225 while(tmp_set->name) {
226 printf("%30s", tmp_set->name);
227 tmp_set++;
228 i++;
229 if (i%3 == 0)
230 printf("\n");
233 /* drop out of the loop */
234 break;
237 printf("\n\n");
239 return NT_STATUS_OK;
242 /* Display help on commands */
244 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
245 int argc, const char **argv)
247 struct cmd_list *tmp;
248 struct cmd_set *tmp_set;
250 /* Usage */
252 if (argc > 2) {
253 printf("Usage: %s [command]\n", argv[0]);
254 return NT_STATUS_OK;
257 /* Help on one command */
259 if (argc == 2) {
260 for (tmp = cmd_list; tmp; tmp = tmp->next) {
262 tmp_set = tmp->cmd_set;
264 while(tmp_set->name) {
265 if (strequal(argv[1], tmp_set->name)) {
266 if (tmp_set->usage &&
267 tmp_set->usage[0])
268 printf("%s\n", tmp_set->usage);
269 else
270 printf("No help for %s\n", tmp_set->name);
272 return NT_STATUS_OK;
275 tmp_set++;
279 printf("No such command: %s\n", argv[1]);
280 return NT_STATUS_OK;
283 /* List all commands */
285 for (tmp = cmd_list; tmp; tmp = tmp->next) {
287 tmp_set = tmp->cmd_set;
289 while(tmp_set->name) {
291 printf("%15s\t\t%s\n", tmp_set->name,
292 tmp_set->description ? tmp_set->description:
293 "");
295 tmp_set++;
299 return NT_STATUS_OK;
302 /* Change the debug level */
304 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
305 int argc, const char **argv)
307 if (argc > 2) {
308 printf("Usage: %s [debuglevel]\n", argv[0]);
309 return NT_STATUS_OK;
312 if (argc == 2) {
313 DEBUGLEVEL = atoi(argv[1]);
316 printf("debuglevel is %d\n", DEBUGLEVEL);
318 return NT_STATUS_OK;
321 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
322 int argc, const char **argv)
324 exit(0);
325 return NT_STATUS_OK; /* NOTREACHED */
328 static NTSTATUS cmd_set_ss_level(void)
330 struct cmd_list *tmp;
332 /* Close any existing connections not at this level. */
334 for (tmp = cmd_list; tmp; tmp = tmp->next) {
335 struct cmd_set *tmp_set;
337 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
338 if (tmp_set->rpc_pipe == NULL) {
339 continue;
342 if ((tmp_set->rpc_pipe->auth->auth_type
343 != pipe_default_auth_type)
344 || (tmp_set->rpc_pipe->auth->auth_level
345 != pipe_default_auth_level)) {
346 TALLOC_FREE(tmp_set->rpc_pipe);
347 tmp_set->rpc_pipe = NULL;
351 return NT_STATUS_OK;
354 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
355 int argc, const char **argv)
357 const char *type = "NTLMSSP";
359 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
360 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
362 if (argc > 2) {
363 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
364 return NT_STATUS_OK;
367 if (argc == 2) {
368 type = argv[1];
369 if (strequal(type, "NTLMSSP")) {
370 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
371 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
372 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
373 } else if (strequal(type, "SCHANNEL")) {
374 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
375 } else {
376 printf("unknown type %s\n", type);
377 return NT_STATUS_INVALID_LEVEL;
381 d_printf("Setting %s - sign\n", type);
383 return cmd_set_ss_level();
386 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
387 int argc, const char **argv)
389 const char *type = "NTLMSSP";
391 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
392 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
394 if (argc > 2) {
395 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
396 return NT_STATUS_OK;
399 if (argc == 2) {
400 type = argv[1];
401 if (strequal(type, "NTLMSSP")) {
402 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
403 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
404 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
405 } else if (strequal(type, "SCHANNEL")) {
406 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
407 } else {
408 printf("unknown type %s\n", type);
409 return NT_STATUS_INVALID_LEVEL;
413 d_printf("Setting %s - sign and seal\n", type);
415 return cmd_set_ss_level();
418 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
419 int argc, const char **argv)
421 struct cmd_list *tmp;
423 if (argc > 2) {
424 printf("Usage: %s timeout\n", argv[0]);
425 return NT_STATUS_OK;
428 if (argc == 2) {
429 timeout = atoi(argv[1]);
431 for (tmp = cmd_list; tmp; tmp = tmp->next) {
433 struct cmd_set *tmp_set;
435 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
436 if (tmp_set->rpc_pipe == NULL) {
437 continue;
440 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
445 printf("timeout is %d\n", timeout);
447 return NT_STATUS_OK;
451 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
452 int argc, const char **argv)
454 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
455 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
457 return cmd_set_ss_level();
460 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
461 int argc, const char **argv)
463 d_printf("Setting schannel - sign and seal\n");
464 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
465 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
467 return cmd_set_ss_level();
470 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
471 int argc, const char **argv)
473 d_printf("Setting schannel - sign only\n");
474 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
475 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
477 return cmd_set_ss_level();
481 /* Built in rpcclient commands */
483 static struct cmd_set rpcclient_commands[] = {
485 { "GENERAL OPTIONS" },
487 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
488 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
489 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
490 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
491 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
492 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
493 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
494 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
495 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
496 { "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.", "" },
497 { "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.", "" },
498 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
499 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
501 { NULL }
504 static struct cmd_set separator_command[] = {
505 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
506 { NULL }
510 /* Various pipe commands */
512 extern struct cmd_set lsarpc_commands[];
513 extern struct cmd_set samr_commands[];
514 extern struct cmd_set spoolss_commands[];
515 extern struct cmd_set netlogon_commands[];
516 extern struct cmd_set srvsvc_commands[];
517 extern struct cmd_set dfs_commands[];
518 extern struct cmd_set ds_commands[];
519 extern struct cmd_set echo_commands[];
520 extern struct cmd_set epmapper_commands[];
521 extern struct cmd_set shutdown_commands[];
522 extern struct cmd_set test_commands[];
523 extern struct cmd_set wkssvc_commands[];
524 extern struct cmd_set ntsvcs_commands[];
525 extern struct cmd_set drsuapi_commands[];
526 extern struct cmd_set eventlog_commands[];
528 static struct cmd_set *rpcclient_command_list[] = {
529 rpcclient_commands,
530 lsarpc_commands,
531 ds_commands,
532 samr_commands,
533 spoolss_commands,
534 netlogon_commands,
535 srvsvc_commands,
536 dfs_commands,
537 echo_commands,
538 epmapper_commands,
539 shutdown_commands,
540 test_commands,
541 wkssvc_commands,
542 ntsvcs_commands,
543 drsuapi_commands,
544 eventlog_commands,
545 NULL
548 static void add_command_set(struct cmd_set *cmd_set)
550 struct cmd_list *entry;
552 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
553 DEBUG(0, ("out of memory\n"));
554 return;
557 ZERO_STRUCTP(entry);
559 entry->cmd_set = cmd_set;
560 DLIST_ADD(cmd_list, entry);
565 * Call an rpcclient function, passing an argv array.
567 * @param cmd Command to run, as a single string.
569 static NTSTATUS do_cmd(struct cli_state *cli,
570 struct user_auth_info *auth_info,
571 struct cmd_set *cmd_entry,
572 int argc, char **argv)
574 NTSTATUS ntresult;
575 WERROR wresult;
577 TALLOC_CTX *mem_ctx;
579 /* Create mem_ctx */
581 if (!(mem_ctx = talloc_init("do_cmd"))) {
582 DEBUG(0, ("talloc_init() failed\n"));
583 return NT_STATUS_NO_MEMORY;
586 /* Open pipe */
588 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
589 switch (pipe_default_auth_type) {
590 case PIPE_AUTH_TYPE_NONE:
591 ntresult = cli_rpc_pipe_open_noauth(
592 cli, cmd_entry->interface,
593 &cmd_entry->rpc_pipe);
594 break;
595 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
596 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
597 cli, cmd_entry->interface,
598 pipe_default_auth_level,
599 lp_workgroup(),
600 get_cmdline_auth_info_username(auth_info),
601 get_cmdline_auth_info_password(auth_info),
602 &cmd_entry->rpc_pipe);
603 break;
604 case PIPE_AUTH_TYPE_NTLMSSP:
605 ntresult = cli_rpc_pipe_open_ntlmssp(
606 cli, cmd_entry->interface,
607 pipe_default_auth_level,
608 lp_workgroup(),
609 get_cmdline_auth_info_username(auth_info),
610 get_cmdline_auth_info_password(auth_info),
611 &cmd_entry->rpc_pipe);
612 break;
613 case PIPE_AUTH_TYPE_SCHANNEL:
614 ntresult = cli_rpc_pipe_open_schannel(
615 cli, cmd_entry->interface,
616 pipe_default_auth_level,
617 lp_workgroup(),
618 &cmd_entry->rpc_pipe);
619 break;
620 default:
621 DEBUG(0, ("Could not initialise %s. Invalid "
622 "auth type %u\n",
623 get_pipe_name_from_iface(
624 cmd_entry->interface),
625 pipe_default_auth_type ));
626 return NT_STATUS_UNSUCCESSFUL;
628 if (!NT_STATUS_IS_OK(ntresult)) {
629 DEBUG(0, ("Could not initialise %s. Error was %s\n",
630 get_pipe_name_from_iface(
631 cmd_entry->interface),
632 nt_errstr(ntresult) ));
633 return ntresult;
636 if (ndr_syntax_id_equal(cmd_entry->interface,
637 &ndr_table_netlogon.syntax_id)) {
638 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
639 uint32 sec_channel_type;
640 uchar trust_password[16];
642 if (!secrets_fetch_trust_account_password(lp_workgroup(),
643 trust_password,
644 NULL, &sec_channel_type)) {
645 return NT_STATUS_UNSUCCESSFUL;
648 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
649 cli->desthost, /* server name */
650 lp_workgroup(), /* domain */
651 global_myname(), /* client name */
652 global_myname(), /* machine account name */
653 trust_password,
654 sec_channel_type,
655 &neg_flags);
657 if (!NT_STATUS_IS_OK(ntresult)) {
658 DEBUG(0, ("Could not initialise credentials for %s.\n",
659 get_pipe_name_from_iface(
660 cmd_entry->interface)));
661 return ntresult;
666 /* Run command */
668 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
669 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
670 if (!NT_STATUS_IS_OK(ntresult)) {
671 printf("result was %s\n", nt_errstr(ntresult));
673 } else {
674 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
675 /* print out the DOS error */
676 if (!W_ERROR_IS_OK(wresult)) {
677 printf( "result was %s\n", win_errstr(wresult));
679 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
682 /* Cleanup */
684 talloc_destroy(mem_ctx);
686 return ntresult;
691 * Process a command entered at the prompt or as part of -c
693 * @returns The NTSTATUS from running the command.
695 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
696 struct cli_state *cli, char *cmd)
698 struct cmd_list *temp_list;
699 NTSTATUS result = NT_STATUS_OK;
700 int ret;
701 int argc;
702 char **argv = NULL;
704 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
705 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
706 return NT_STATUS_UNSUCCESSFUL;
710 /* Walk through a dlist of arrays of commands. */
711 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
712 struct cmd_set *temp_set = temp_list->cmd_set;
714 while (temp_set->name) {
715 if (strequal(argv[0], temp_set->name)) {
716 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
717 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
718 fprintf (stderr, "Invalid command\n");
719 goto out_free;
722 result = do_cmd(cli, auth_info, temp_set,
723 argc, argv);
725 goto out_free;
727 temp_set++;
731 if (argv[0]) {
732 printf("command not found: %s\n", argv[0]);
735 out_free:
736 /* moved to do_cmd()
737 if (!NT_STATUS_IS_OK(result)) {
738 printf("result was %s\n", nt_errstr(result));
742 /* NOTE: popt allocates the whole argv, including the
743 * strings, as a single block. So a single free is
744 * enough to release it -- we don't free the
745 * individual strings. rtfm. */
746 free(argv);
748 return result;
752 /* Main function */
754 int main(int argc, char *argv[])
756 int opt;
757 static char *cmdstr = NULL;
758 const char *server;
759 struct cli_state *cli = NULL;
760 static char *opt_ipaddr=NULL;
761 struct cmd_set **cmd_set;
762 struct sockaddr_storage server_ss;
763 NTSTATUS nt_status;
764 static int opt_port = 0;
765 fstring new_workgroup;
766 int result = 0;
767 TALLOC_CTX *frame = talloc_stackframe();
768 uint32_t flags = 0;
770 /* make sure the vars that get altered (4th field) are in
771 a fixed location or certain compilers complain */
772 poptContext pc;
773 struct poptOption long_options[] = {
774 POPT_AUTOHELP
775 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
776 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
777 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
778 POPT_COMMON_SAMBA
779 POPT_COMMON_CONNECTION
780 POPT_COMMON_CREDENTIALS
781 POPT_TABLEEND
784 load_case_tables();
786 zero_sockaddr(&server_ss);
788 setlinebuf(stdout);
790 /* the following functions are part of the Samba debugging
791 facilities. See lib/debug.c */
792 setup_logging("rpcclient", True);
794 rpcclient_auth_info = user_auth_info_init(frame);
795 if (rpcclient_auth_info == NULL) {
796 exit(1);
798 popt_common_set_auth_info(rpcclient_auth_info);
800 /* Parse options */
802 pc = poptGetContext("rpcclient", argc, (const char **) argv,
803 long_options, 0);
805 if (argc == 1) {
806 poptPrintHelp(pc, stderr, 0);
807 goto done;
810 while((opt = poptGetNextOpt(pc)) != -1) {
811 switch (opt) {
813 case 'I':
814 if (!interpret_string_addr(&server_ss,
815 opt_ipaddr,
816 AI_NUMERICHOST)) {
817 fprintf(stderr, "%s not a valid IP address\n",
818 opt_ipaddr);
819 result = 1;
820 goto done;
825 /* Get server as remaining unparsed argument. Print usage if more
826 than one unparsed argument is present. */
828 server = poptGetArg(pc);
830 if (!server || poptGetArg(pc)) {
831 poptPrintHelp(pc, stderr, 0);
832 result = 1;
833 goto done;
836 poptFreeContext(pc);
838 load_interfaces();
840 if (!init_names()) {
841 result = 1;
842 goto done;
845 /* save the workgroup...
847 FIXME!! do we need to do this for other options as well
848 (or maybe a generic way to keep lp_load() from overwriting
849 everything)? */
851 fstrcpy( new_workgroup, lp_workgroup() );
853 /* Load smb.conf file */
855 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
856 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
858 if ( strlen(new_workgroup) != 0 )
859 set_global_myworkgroup( new_workgroup );
862 * Get password
863 * from stdin if necessary
866 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
867 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
868 result = 1;
869 goto done;
872 set_cmdline_auth_info_getpass(rpcclient_auth_info);
874 if ((server[0] == '/' && server[1] == '/') ||
875 (server[0] == '\\' && server[1] == '\\')) {
876 server += 2;
879 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
880 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
881 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
885 nt_status = cli_full_connection(&cli, global_myname(), server,
886 opt_ipaddr ? &server_ss : NULL, opt_port,
887 "IPC$", "IPC",
888 get_cmdline_auth_info_username(rpcclient_auth_info),
889 lp_workgroup(),
890 get_cmdline_auth_info_password(rpcclient_auth_info),
891 flags,
892 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
893 NULL);
895 if (!NT_STATUS_IS_OK(nt_status)) {
896 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
897 result = 1;
898 goto done;
901 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
902 nt_status = cli_cm_force_encryption(cli,
903 get_cmdline_auth_info_username(rpcclient_auth_info),
904 get_cmdline_auth_info_password(rpcclient_auth_info),
905 lp_workgroup(),
906 "IPC$");
907 if (!NT_STATUS_IS_OK(nt_status)) {
908 result = 1;
909 goto done;
913 #if 0 /* COMMENT OUT FOR TESTING */
914 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
915 #endif
917 /* Load command lists */
919 timeout = cli_set_timeout(cli, 10000);
921 cmd_set = rpcclient_command_list;
923 while(*cmd_set) {
924 add_command_set(*cmd_set);
925 add_command_set(separator_command);
926 cmd_set++;
929 fetch_machine_sid(cli);
931 /* Do anything specified with -c */
932 if (cmdstr && cmdstr[0]) {
933 char *cmd;
934 char *p = cmdstr;
936 result = 0;
938 while((cmd=next_command(&p)) != NULL) {
939 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli, cmd);
940 SAFE_FREE(cmd);
941 result = NT_STATUS_IS_ERR(cmd_result);
944 goto done;
947 /* Loop around accepting commands */
949 while(1) {
950 char *line = NULL;
952 line = smb_readline("rpcclient $> ", NULL, completion_fn);
954 if (line == NULL)
955 break;
957 if (line[0] != '\n')
958 process_cmd(rpcclient_auth_info, cli, line);
959 SAFE_FREE(line);
962 done:
963 if (cli != NULL) {
964 cli_shutdown(cli);
966 TALLOC_FREE(frame);
967 return result;