s3: re-run make samba3-idl.
[Samba/gbeck.git] / source3 / rpcclient / rpcclient.c
blob9a02c129b5486be1398247b3b619efce516651ad
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 struct user_auth_info *rpcclient_auth_info;
33 /* List to hold groups of commands.
35 * Commands are defined in a list of arrays: arrays are easy to
36 * statically declare, and lists are easier to dynamically extend.
39 static struct cmd_list {
40 struct cmd_list *prev, *next;
41 struct cmd_set *cmd_set;
42 } *cmd_list;
44 /****************************************************************************
45 handle completion of commands for readline
46 ****************************************************************************/
47 static char **completion_fn(const char *text, int start, int end)
49 #define MAX_COMPLETIONS 100
50 char **matches;
51 int i, count=0;
52 struct cmd_list *commands = cmd_list;
54 #if 0 /* JERRY */
55 /* FIXME!!! -- what to do when completing argument? */
56 /* for words not at the start of the line fallback
57 to filename completion */
58 if (start)
59 return NULL;
60 #endif
62 /* make sure we have a list of valid commands */
63 if (!commands) {
64 return NULL;
67 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
68 if (!matches) {
69 return NULL;
72 matches[count++] = SMB_STRDUP(text);
73 if (!matches[0]) {
74 SAFE_FREE(matches);
75 return NULL;
78 while (commands && count < MAX_COMPLETIONS-1) {
79 if (!commands->cmd_set) {
80 break;
83 for (i=0; commands->cmd_set[i].name; i++) {
84 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
85 (( commands->cmd_set[i].returntype == RPC_RTYPE_NTSTATUS &&
86 commands->cmd_set[i].ntfn ) ||
87 ( commands->cmd_set[i].returntype == RPC_RTYPE_WERROR &&
88 commands->cmd_set[i].wfn))) {
89 matches[count] = SMB_STRDUP(commands->cmd_set[i].name);
90 if (!matches[count]) {
91 for (i = 0; i < count; i++) {
92 SAFE_FREE(matches[count]);
94 SAFE_FREE(matches);
95 return NULL;
97 count++;
100 commands = commands->next;
104 if (count == 2) {
105 SAFE_FREE(matches[0]);
106 matches[0] = SMB_STRDUP(matches[1]);
108 matches[count] = NULL;
109 return matches;
112 static char *next_command (char **cmdstr)
114 char *command;
115 char *p;
117 if (!cmdstr || !(*cmdstr))
118 return NULL;
120 p = strchr_m(*cmdstr, ';');
121 if (p)
122 *p = '\0';
123 command = SMB_STRDUP(*cmdstr);
124 if (p)
125 *cmdstr = p + 1;
126 else
127 *cmdstr = NULL;
129 return command;
132 /* Fetch the SID for this computer */
134 static void fetch_machine_sid(struct cli_state *cli)
136 POLICY_HND pol;
137 NTSTATUS result = NT_STATUS_OK;
138 static bool got_domain_sid;
139 TALLOC_CTX *mem_ctx;
140 struct rpc_pipe_client *lsapipe = NULL;
141 union lsa_PolicyInformation *info = NULL;
143 if (got_domain_sid) return;
145 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
146 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
147 goto error;
150 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
151 &lsapipe);
152 if (!NT_STATUS_IS_OK(result)) {
153 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
154 goto error;
157 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
158 SEC_RIGHTS_MAXIMUM_ALLOWED,
159 &pol);
160 if (!NT_STATUS_IS_OK(result)) {
161 goto error;
164 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
165 &pol,
166 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
167 &info);
168 if (!NT_STATUS_IS_OK(result)) {
169 goto error;
172 got_domain_sid = True;
173 sid_copy(&domain_sid, info->account_domain.sid);
175 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
176 TALLOC_FREE(lsapipe);
177 talloc_destroy(mem_ctx);
179 return;
181 error:
183 if (lsapipe) {
184 TALLOC_FREE(lsapipe);
187 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
189 if (!NT_STATUS_IS_OK(result)) {
190 fprintf(stderr, "error: %s\n", nt_errstr(result));
193 exit(1);
196 /* List the available commands on a given pipe */
198 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
199 int argc, const char **argv)
201 struct cmd_list *tmp;
202 struct cmd_set *tmp_set;
203 int i;
205 /* Usage */
207 if (argc != 2) {
208 printf("Usage: %s <pipe>\n", argv[0]);
209 return NT_STATUS_OK;
212 /* Help on one command */
214 for (tmp = cmd_list; tmp; tmp = tmp->next)
216 tmp_set = tmp->cmd_set;
218 if (!StrCaseCmp(argv[1], tmp_set->name))
220 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
222 i = 0;
223 tmp_set++;
224 while(tmp_set->name) {
225 printf("%30s", tmp_set->name);
226 tmp_set++;
227 i++;
228 if (i%3 == 0)
229 printf("\n");
232 /* drop out of the loop */
233 break;
236 printf("\n\n");
238 return NT_STATUS_OK;
241 /* Display help on commands */
243 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
244 int argc, const char **argv)
246 struct cmd_list *tmp;
247 struct cmd_set *tmp_set;
249 /* Usage */
251 if (argc > 2) {
252 printf("Usage: %s [command]\n", argv[0]);
253 return NT_STATUS_OK;
256 /* Help on one command */
258 if (argc == 2) {
259 for (tmp = cmd_list; tmp; tmp = tmp->next) {
261 tmp_set = tmp->cmd_set;
263 while(tmp_set->name) {
264 if (strequal(argv[1], tmp_set->name)) {
265 if (tmp_set->usage &&
266 tmp_set->usage[0])
267 printf("%s\n", tmp_set->usage);
268 else
269 printf("No help for %s\n", tmp_set->name);
271 return NT_STATUS_OK;
274 tmp_set++;
278 printf("No such command: %s\n", argv[1]);
279 return NT_STATUS_OK;
282 /* List all commands */
284 for (tmp = cmd_list; tmp; tmp = tmp->next) {
286 tmp_set = tmp->cmd_set;
288 while(tmp_set->name) {
290 printf("%15s\t\t%s\n", tmp_set->name,
291 tmp_set->description ? tmp_set->description:
292 "");
294 tmp_set++;
298 return NT_STATUS_OK;
301 /* Change the debug level */
303 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
304 int argc, const char **argv)
306 if (argc > 2) {
307 printf("Usage: %s [debuglevel]\n", argv[0]);
308 return NT_STATUS_OK;
311 if (argc == 2) {
312 DEBUGLEVEL = atoi(argv[1]);
315 printf("debuglevel is %d\n", DEBUGLEVEL);
317 return NT_STATUS_OK;
320 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
321 int argc, const char **argv)
323 exit(0);
324 return NT_STATUS_OK; /* NOTREACHED */
327 static NTSTATUS cmd_set_ss_level(void)
329 struct cmd_list *tmp;
331 /* Close any existing connections not at this level. */
333 for (tmp = cmd_list; tmp; tmp = tmp->next) {
334 struct cmd_set *tmp_set;
336 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
337 if (tmp_set->rpc_pipe == NULL) {
338 continue;
341 if ((tmp_set->rpc_pipe->auth->auth_type
342 != pipe_default_auth_type)
343 || (tmp_set->rpc_pipe->auth->auth_level
344 != pipe_default_auth_level)) {
345 TALLOC_FREE(tmp_set->rpc_pipe);
346 tmp_set->rpc_pipe = NULL;
350 return NT_STATUS_OK;
353 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
354 int argc, const char **argv)
356 const char *type = "NTLMSSP";
358 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
359 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
361 if (argc > 2) {
362 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
363 return NT_STATUS_OK;
366 if (argc == 2) {
367 type = argv[1];
368 if (strequal(type, "NTLMSSP")) {
369 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
370 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
371 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
372 } else if (strequal(type, "SCHANNEL")) {
373 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
374 } else {
375 printf("unknown type %s\n", type);
376 return NT_STATUS_INVALID_LEVEL;
380 d_printf("Setting %s - sign\n", type);
382 return cmd_set_ss_level();
385 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
386 int argc, const char **argv)
388 const char *type = "NTLMSSP";
390 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
391 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
393 if (argc > 2) {
394 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
395 return NT_STATUS_OK;
398 if (argc == 2) {
399 type = argv[1];
400 if (strequal(type, "NTLMSSP")) {
401 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
402 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
403 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
404 } else if (strequal(type, "SCHANNEL")) {
405 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
406 } else {
407 printf("unknown type %s\n", type);
408 return NT_STATUS_INVALID_LEVEL;
412 d_printf("Setting %s - sign and seal\n", type);
414 return cmd_set_ss_level();
417 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
418 int argc, const char **argv)
420 struct cmd_list *tmp;
422 if (argc > 2) {
423 printf("Usage: %s timeout\n", argv[0]);
424 return NT_STATUS_OK;
427 if (argc == 2) {
428 timeout = atoi(argv[1]);
430 for (tmp = cmd_list; tmp; tmp = tmp->next) {
432 struct cmd_set *tmp_set;
434 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
435 if (tmp_set->rpc_pipe == NULL) {
436 continue;
439 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
444 printf("timeout is %d\n", timeout);
446 return NT_STATUS_OK;
450 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
451 int argc, const char **argv)
453 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
454 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
456 return cmd_set_ss_level();
459 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
460 int argc, const char **argv)
462 d_printf("Setting schannel - sign and seal\n");
463 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
464 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
466 return cmd_set_ss_level();
469 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
470 int argc, const char **argv)
472 d_printf("Setting schannel - sign only\n");
473 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
474 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
476 return cmd_set_ss_level();
480 /* Built in rpcclient commands */
482 static struct cmd_set rpcclient_commands[] = {
484 { "GENERAL OPTIONS" },
486 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
487 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
488 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
489 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
490 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
491 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
492 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
493 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
494 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
495 { "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.", "" },
496 { "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.", "" },
497 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
498 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
500 { NULL }
503 static struct cmd_set separator_command[] = {
504 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
505 { NULL }
509 /* Various pipe commands */
511 extern struct cmd_set lsarpc_commands[];
512 extern struct cmd_set samr_commands[];
513 extern struct cmd_set spoolss_commands[];
514 extern struct cmd_set netlogon_commands[];
515 extern struct cmd_set srvsvc_commands[];
516 extern struct cmd_set dfs_commands[];
517 extern struct cmd_set ds_commands[];
518 extern struct cmd_set echo_commands[];
519 extern struct cmd_set epmapper_commands[];
520 extern struct cmd_set shutdown_commands[];
521 extern struct cmd_set test_commands[];
522 extern struct cmd_set wkssvc_commands[];
523 extern struct cmd_set ntsvcs_commands[];
524 extern struct cmd_set drsuapi_commands[];
525 extern struct cmd_set eventlog_commands[];
527 static struct cmd_set *rpcclient_command_list[] = {
528 rpcclient_commands,
529 lsarpc_commands,
530 ds_commands,
531 samr_commands,
532 spoolss_commands,
533 netlogon_commands,
534 srvsvc_commands,
535 dfs_commands,
536 echo_commands,
537 epmapper_commands,
538 shutdown_commands,
539 test_commands,
540 wkssvc_commands,
541 ntsvcs_commands,
542 drsuapi_commands,
543 eventlog_commands,
544 NULL
547 static void add_command_set(struct cmd_set *cmd_set)
549 struct cmd_list *entry;
551 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
552 DEBUG(0, ("out of memory\n"));
553 return;
556 ZERO_STRUCTP(entry);
558 entry->cmd_set = cmd_set;
559 DLIST_ADD(cmd_list, entry);
564 * Call an rpcclient function, passing an argv array.
566 * @param cmd Command to run, as a single string.
568 static NTSTATUS do_cmd(struct cli_state *cli,
569 struct user_auth_info *auth_info,
570 struct cmd_set *cmd_entry,
571 int argc, char **argv)
573 NTSTATUS ntresult;
574 WERROR wresult;
576 TALLOC_CTX *mem_ctx;
578 /* Create mem_ctx */
580 if (!(mem_ctx = talloc_init("do_cmd"))) {
581 DEBUG(0, ("talloc_init() failed\n"));
582 return NT_STATUS_NO_MEMORY;
585 /* Open pipe */
587 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
588 switch (pipe_default_auth_type) {
589 case PIPE_AUTH_TYPE_NONE:
590 ntresult = cli_rpc_pipe_open_noauth(
591 cli, cmd_entry->interface,
592 &cmd_entry->rpc_pipe);
593 break;
594 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
595 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
596 cli, cmd_entry->interface,
597 pipe_default_auth_level,
598 lp_workgroup(),
599 get_cmdline_auth_info_username(auth_info),
600 get_cmdline_auth_info_password(auth_info),
601 &cmd_entry->rpc_pipe);
602 break;
603 case PIPE_AUTH_TYPE_NTLMSSP:
604 ntresult = cli_rpc_pipe_open_ntlmssp(
605 cli, cmd_entry->interface,
606 pipe_default_auth_level,
607 lp_workgroup(),
608 get_cmdline_auth_info_username(auth_info),
609 get_cmdline_auth_info_password(auth_info),
610 &cmd_entry->rpc_pipe);
611 break;
612 case PIPE_AUTH_TYPE_SCHANNEL:
613 ntresult = cli_rpc_pipe_open_schannel(
614 cli, cmd_entry->interface,
615 pipe_default_auth_level,
616 lp_workgroup(),
617 &cmd_entry->rpc_pipe);
618 break;
619 default:
620 DEBUG(0, ("Could not initialise %s. Invalid "
621 "auth type %u\n",
622 get_pipe_name_from_iface(
623 cmd_entry->interface),
624 pipe_default_auth_type ));
625 return NT_STATUS_UNSUCCESSFUL;
627 if (!NT_STATUS_IS_OK(ntresult)) {
628 DEBUG(0, ("Could not initialise %s. Error was %s\n",
629 get_pipe_name_from_iface(
630 cmd_entry->interface),
631 nt_errstr(ntresult) ));
632 return ntresult;
635 if (ndr_syntax_id_equal(cmd_entry->interface,
636 &ndr_table_netlogon.syntax_id)) {
637 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
638 uint32 sec_channel_type;
639 uchar trust_password[16];
641 if (!secrets_fetch_trust_account_password(lp_workgroup(),
642 trust_password,
643 NULL, &sec_channel_type)) {
644 return NT_STATUS_UNSUCCESSFUL;
647 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
648 cli->desthost, /* server name */
649 lp_workgroup(), /* domain */
650 global_myname(), /* client name */
651 global_myname(), /* machine account name */
652 trust_password,
653 sec_channel_type,
654 &neg_flags);
656 if (!NT_STATUS_IS_OK(ntresult)) {
657 DEBUG(0, ("Could not initialise credentials for %s.\n",
658 get_pipe_name_from_iface(
659 cmd_entry->interface)));
660 return ntresult;
665 /* Run command */
667 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
668 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
669 if (!NT_STATUS_IS_OK(ntresult)) {
670 printf("result was %s\n", nt_errstr(ntresult));
672 } else {
673 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
674 /* print out the DOS error */
675 if (!W_ERROR_IS_OK(wresult)) {
676 printf( "result was %s\n", win_errstr(wresult));
678 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
681 /* Cleanup */
683 talloc_destroy(mem_ctx);
685 return ntresult;
690 * Process a command entered at the prompt or as part of -c
692 * @returns The NTSTATUS from running the command.
694 static NTSTATUS process_cmd(struct user_auth_info *auth_info,
695 struct cli_state *cli, char *cmd)
697 struct cmd_list *temp_list;
698 NTSTATUS result = NT_STATUS_OK;
699 int ret;
700 int argc;
701 char **argv = NULL;
703 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
704 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
705 return NT_STATUS_UNSUCCESSFUL;
709 /* Walk through a dlist of arrays of commands. */
710 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
711 struct cmd_set *temp_set = temp_list->cmd_set;
713 while (temp_set->name) {
714 if (strequal(argv[0], temp_set->name)) {
715 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
716 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
717 fprintf (stderr, "Invalid command\n");
718 goto out_free;
721 result = do_cmd(cli, auth_info, temp_set,
722 argc, argv);
724 goto out_free;
726 temp_set++;
730 if (argv[0]) {
731 printf("command not found: %s\n", argv[0]);
734 out_free:
735 /* moved to do_cmd()
736 if (!NT_STATUS_IS_OK(result)) {
737 printf("result was %s\n", nt_errstr(result));
741 /* NOTE: popt allocates the whole argv, including the
742 * strings, as a single block. So a single free is
743 * enough to release it -- we don't free the
744 * individual strings. rtfm. */
745 free(argv);
747 return result;
751 /* Main function */
753 int main(int argc, char *argv[])
755 int opt;
756 static char *cmdstr = NULL;
757 const char *server;
758 struct cli_state *cli = NULL;
759 static char *opt_ipaddr=NULL;
760 struct cmd_set **cmd_set;
761 struct sockaddr_storage server_ss;
762 NTSTATUS nt_status;
763 static int opt_port = 0;
764 fstring new_workgroup;
765 int result = 0;
766 TALLOC_CTX *frame = talloc_stackframe();
767 uint32_t flags = 0;
769 /* make sure the vars that get altered (4th field) are in
770 a fixed location or certain compilers complain */
771 poptContext pc;
772 struct poptOption long_options[] = {
773 POPT_AUTOHELP
774 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
775 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
776 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
777 POPT_COMMON_SAMBA
778 POPT_COMMON_CONNECTION
779 POPT_COMMON_CREDENTIALS
780 POPT_TABLEEND
783 load_case_tables();
785 zero_sockaddr(&server_ss);
787 setlinebuf(stdout);
789 /* the following functions are part of the Samba debugging
790 facilities. See lib/debug.c */
791 setup_logging("rpcclient", True);
793 rpcclient_auth_info = user_auth_info_init(frame);
794 if (rpcclient_auth_info == NULL) {
795 exit(1);
797 popt_common_set_auth_info(rpcclient_auth_info);
799 /* Parse options */
801 pc = poptGetContext("rpcclient", argc, (const char **) argv,
802 long_options, 0);
804 if (argc == 1) {
805 poptPrintHelp(pc, stderr, 0);
806 goto done;
809 while((opt = poptGetNextOpt(pc)) != -1) {
810 switch (opt) {
812 case 'I':
813 if (!interpret_string_addr(&server_ss,
814 opt_ipaddr,
815 AI_NUMERICHOST)) {
816 fprintf(stderr, "%s not a valid IP address\n",
817 opt_ipaddr);
818 result = 1;
819 goto done;
824 /* Get server as remaining unparsed argument. Print usage if more
825 than one unparsed argument is present. */
827 server = poptGetArg(pc);
829 if (!server || poptGetArg(pc)) {
830 poptPrintHelp(pc, stderr, 0);
831 result = 1;
832 goto done;
835 poptFreeContext(pc);
837 load_interfaces();
839 if (!init_names()) {
840 result = 1;
841 goto done;
844 /* save the workgroup...
846 FIXME!! do we need to do this for other options as well
847 (or maybe a generic way to keep lp_load() from overwriting
848 everything)? */
850 fstrcpy( new_workgroup, lp_workgroup() );
852 /* Load smb.conf file */
854 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
855 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
857 if ( strlen(new_workgroup) != 0 )
858 set_global_myworkgroup( new_workgroup );
861 * Get password
862 * from stdin if necessary
865 if (get_cmdline_auth_info_use_machine_account(rpcclient_auth_info) &&
866 !set_cmdline_auth_info_machine_account_creds(rpcclient_auth_info)) {
867 result = 1;
868 goto done;
871 if (!get_cmdline_auth_info_got_pass(rpcclient_auth_info)) {
872 char *pass = getpass("Password:");
873 if (pass) {
874 set_cmdline_auth_info_password(rpcclient_auth_info, pass);
878 if ((server[0] == '/' && server[1] == '/') ||
879 (server[0] == '\\' && server[1] == '\\')) {
880 server += 2;
883 if (get_cmdline_auth_info_use_kerberos(rpcclient_auth_info)) {
884 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
885 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
889 nt_status = cli_full_connection(&cli, global_myname(), server,
890 opt_ipaddr ? &server_ss : NULL, opt_port,
891 "IPC$", "IPC",
892 get_cmdline_auth_info_username(rpcclient_auth_info),
893 lp_workgroup(),
894 get_cmdline_auth_info_password(rpcclient_auth_info),
895 flags,
896 get_cmdline_auth_info_signing_state(rpcclient_auth_info),
897 NULL);
899 if (!NT_STATUS_IS_OK(nt_status)) {
900 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
901 result = 1;
902 goto done;
905 if (get_cmdline_auth_info_smb_encrypt(rpcclient_auth_info)) {
906 nt_status = cli_cm_force_encryption(cli,
907 get_cmdline_auth_info_username(rpcclient_auth_info),
908 get_cmdline_auth_info_password(rpcclient_auth_info),
909 lp_workgroup(),
910 "IPC$");
911 if (!NT_STATUS_IS_OK(nt_status)) {
912 result = 1;
913 goto done;
917 #if 0 /* COMMENT OUT FOR TESTING */
918 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
919 #endif
921 /* Load command lists */
923 timeout = cli_set_timeout(cli, 10000);
925 cmd_set = rpcclient_command_list;
927 while(*cmd_set) {
928 add_command_set(*cmd_set);
929 add_command_set(separator_command);
930 cmd_set++;
933 fetch_machine_sid(cli);
935 /* Do anything specified with -c */
936 if (cmdstr && cmdstr[0]) {
937 char *cmd;
938 char *p = cmdstr;
940 result = 0;
942 while((cmd=next_command(&p)) != NULL) {
943 NTSTATUS cmd_result = process_cmd(rpcclient_auth_info, cli, cmd);
944 SAFE_FREE(cmd);
945 result = NT_STATUS_IS_ERR(cmd_result);
948 goto done;
951 /* Loop around accepting commands */
953 while(1) {
954 char *line = NULL;
956 line = smb_readline("rpcclient $> ", NULL, completion_fn);
958 if (line == NULL)
959 break;
961 if (line[0] != '\n')
962 process_cmd(rpcclient_auth_info, cli, line);
963 SAFE_FREE(line);
966 done:
967 if (cli != NULL) {
968 cli_shutdown(cli);
970 TALLOC_FREE(frame);
971 return result;