r25407: Revert Longhorn join patch as it is not correct for the 3.2 tree.
[Samba/ekacnet.git] / source3 / rpcclient / rpcclient.c
blobb43ce09f469fcfc37b2f2f14b4eaa837ae3e386d
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 uint32 info_class = 5;
137 char *domain_name = NULL;
138 static BOOL got_domain_sid;
139 TALLOC_CTX *mem_ctx;
140 DOM_SID *dom_sid = NULL;
141 struct rpc_pipe_client *lsapipe = 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 if ((lsapipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result)) == NULL) {
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_query_info_policy(lsapipe, mem_ctx, &pol, info_class,
163 &domain_name, &dom_sid);
164 if (!NT_STATUS_IS_OK(result)) {
165 goto error;
168 got_domain_sid = True;
169 sid_copy( &domain_sid, dom_sid );
171 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
172 cli_rpc_pipe_close(lsapipe);
173 talloc_destroy(mem_ctx);
175 return;
177 error:
179 if (lsapipe) {
180 cli_rpc_pipe_close(lsapipe);
183 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
185 if (!NT_STATUS_IS_OK(result)) {
186 fprintf(stderr, "error: %s\n", nt_errstr(result));
189 exit(1);
192 /* List the available commands on a given pipe */
194 static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
195 int argc, const char **argv)
197 struct cmd_list *tmp;
198 struct cmd_set *tmp_set;
199 int i;
201 /* Usage */
203 if (argc != 2) {
204 printf("Usage: %s <pipe>\n", argv[0]);
205 return NT_STATUS_OK;
208 /* Help on one command */
210 for (tmp = cmd_list; tmp; tmp = tmp->next)
212 tmp_set = tmp->cmd_set;
214 if (!StrCaseCmp(argv[1], tmp_set->name))
216 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
218 i = 0;
219 tmp_set++;
220 while(tmp_set->name) {
221 printf("%30s", tmp_set->name);
222 tmp_set++;
223 i++;
224 if (i%3 == 0)
225 printf("\n");
228 /* drop out of the loop */
229 break;
232 printf("\n\n");
234 return NT_STATUS_OK;
237 /* Display help on commands */
239 static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
240 int argc, const char **argv)
242 struct cmd_list *tmp;
243 struct cmd_set *tmp_set;
245 /* Usage */
247 if (argc > 2) {
248 printf("Usage: %s [command]\n", argv[0]);
249 return NT_STATUS_OK;
252 /* Help on one command */
254 if (argc == 2) {
255 for (tmp = cmd_list; tmp; tmp = tmp->next) {
257 tmp_set = tmp->cmd_set;
259 while(tmp_set->name) {
260 if (strequal(argv[1], tmp_set->name)) {
261 if (tmp_set->usage &&
262 tmp_set->usage[0])
263 printf("%s\n", tmp_set->usage);
264 else
265 printf("No help for %s\n", tmp_set->name);
267 return NT_STATUS_OK;
270 tmp_set++;
274 printf("No such command: %s\n", argv[1]);
275 return NT_STATUS_OK;
278 /* List all commands */
280 for (tmp = cmd_list; tmp; tmp = tmp->next) {
282 tmp_set = tmp->cmd_set;
284 while(tmp_set->name) {
286 printf("%15s\t\t%s\n", tmp_set->name,
287 tmp_set->description ? tmp_set->description:
288 "");
290 tmp_set++;
294 return NT_STATUS_OK;
297 /* Change the debug level */
299 static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
300 int argc, const char **argv)
302 if (argc > 2) {
303 printf("Usage: %s [debuglevel]\n", argv[0]);
304 return NT_STATUS_OK;
307 if (argc == 2) {
308 DEBUGLEVEL = atoi(argv[1]);
311 printf("debuglevel is %d\n", DEBUGLEVEL);
313 return NT_STATUS_OK;
316 static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
317 int argc, const char **argv)
319 exit(0);
320 return NT_STATUS_OK; /* NOTREACHED */
323 static NTSTATUS cmd_set_ss_level(void)
325 struct cmd_list *tmp;
327 /* Close any existing connections not at this level. */
329 for (tmp = cmd_list; tmp; tmp = tmp->next) {
330 struct cmd_set *tmp_set;
332 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
333 if (tmp_set->rpc_pipe == NULL) {
334 continue;
337 if (tmp_set->rpc_pipe->auth.auth_type != pipe_default_auth_type ||
338 tmp_set->rpc_pipe->auth.auth_level != pipe_default_auth_level) {
339 cli_rpc_pipe_close(tmp_set->rpc_pipe);
340 tmp_set->rpc_pipe = NULL;
344 return NT_STATUS_OK;
347 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
348 int argc, const char **argv)
350 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
351 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
353 if (argc > 2) {
354 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
355 return NT_STATUS_OK;
358 if (argc == 2) {
359 if (strequal(argv[1], "NTLMSSP")) {
360 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
361 } else if (strequal(argv[1], "NTLMSSP_SPNEGO")) {
362 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
363 } else if (strequal(argv[1], "SCHANNEL")) {
364 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
365 } else {
366 printf("unknown type %s\n", argv[1]);
367 return NT_STATUS_INVALID_LEVEL;
371 printf("debuglevel is %d\n", DEBUGLEVEL);
372 return cmd_set_ss_level();
375 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
376 int argc, const char **argv)
378 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
379 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
381 if (argc > 2) {
382 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
383 return NT_STATUS_OK;
386 if (argc == 2) {
387 if (strequal(argv[1], "NTLMSSP")) {
388 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
389 } else if (strequal(argv[1], "NTLMSSP_SPNEGO")) {
390 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
391 } else if (strequal(argv[1], "SCHANNEL")) {
392 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
393 } else {
394 printf("unknown type %s\n", argv[1]);
395 return NT_STATUS_INVALID_LEVEL;
398 return cmd_set_ss_level();
401 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
402 int argc, const char **argv)
404 struct cmd_list *tmp;
406 if (argc > 2) {
407 printf("Usage: %s timeout\n", argv[0]);
408 return NT_STATUS_OK;
411 if (argc == 2) {
412 timeout = atoi(argv[1]);
414 for (tmp = cmd_list; tmp; tmp = tmp->next) {
416 struct cmd_set *tmp_set;
418 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
419 if (tmp_set->rpc_pipe == NULL) {
420 continue;
423 cli_set_timeout(tmp_set->rpc_pipe->cli, timeout);
428 printf("timeout is %d\n", timeout);
430 return NT_STATUS_OK;
434 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
435 int argc, const char **argv)
437 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
438 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
440 return cmd_set_ss_level();
443 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
444 int argc, const char **argv)
446 d_printf("Setting schannel - sign and seal\n");
447 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
448 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
450 return cmd_set_ss_level();
453 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
454 int argc, const char **argv)
456 d_printf("Setting schannel - sign only\n");
457 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
458 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
460 return cmd_set_ss_level();
464 /* Built in rpcclient commands */
466 static struct cmd_set rpcclient_commands[] = {
468 { "GENERAL OPTIONS" },
470 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, NULL, "Get help on commands", "[command]" },
471 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, NULL, "Get help on commands", "[command]" },
472 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, NULL, "Set debug level", "level" },
473 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, NULL, "Set debug level", "level" },
474 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, NULL, "List available commands on <pipe>", "pipe" },
475 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, NULL, "Exit program", "" },
476 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, NULL, "Exit program", "" },
477 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, -1, NULL, "Force RPC pipe connections to be signed", "" },
478 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, -1, NULL, "Force RPC pipe connections to be sealed", "" },
479 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, -1, NULL, "Force RPC pipe connections to be sealed with 'schannel'. Assumes valid machine account to this domain controller.", "" },
480 { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL, -1, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'. Assumes valid machine account to this domain controller.", "" },
481 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, -1, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
482 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, -1, NULL, "Force RPC pipe connections to have no special properties", "" },
484 { NULL }
487 static struct cmd_set separator_command[] = {
488 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, NULL, "----------------------" },
489 { NULL }
493 /* Various pipe commands */
495 extern struct cmd_set unixinfo_commands[];
496 extern struct cmd_set lsarpc_commands[];
497 extern struct cmd_set samr_commands[];
498 extern struct cmd_set spoolss_commands[];
499 extern struct cmd_set netlogon_commands[];
500 extern struct cmd_set srvsvc_commands[];
501 extern struct cmd_set dfs_commands[];
502 extern struct cmd_set ds_commands[];
503 extern struct cmd_set echo_commands[];
504 extern struct cmd_set shutdown_commands[];
505 extern struct cmd_set test_commands[];
507 static struct cmd_set *rpcclient_command_list[] = {
508 rpcclient_commands,
509 unixinfo_commands,
510 lsarpc_commands,
511 ds_commands,
512 samr_commands,
513 spoolss_commands,
514 netlogon_commands,
515 srvsvc_commands,
516 dfs_commands,
517 echo_commands,
518 shutdown_commands,
519 test_commands,
520 NULL
523 static void add_command_set(struct cmd_set *cmd_set)
525 struct cmd_list *entry;
527 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
528 DEBUG(0, ("out of memory\n"));
529 return;
532 ZERO_STRUCTP(entry);
534 entry->cmd_set = cmd_set;
535 DLIST_ADD(cmd_list, entry);
540 * Call an rpcclient function, passing an argv array.
542 * @param cmd Command to run, as a single string.
544 static NTSTATUS do_cmd(struct cli_state *cli,
545 struct cmd_set *cmd_entry,
546 int argc, char **argv)
548 NTSTATUS ntresult;
549 WERROR wresult;
551 TALLOC_CTX *mem_ctx;
553 /* Create mem_ctx */
555 if (!(mem_ctx = talloc_init("do_cmd"))) {
556 DEBUG(0, ("talloc_init() failed\n"));
557 return NT_STATUS_NO_MEMORY;
560 /* Open pipe */
562 if (cmd_entry->pipe_idx != -1 && cmd_entry->rpc_pipe == NULL) {
563 switch (pipe_default_auth_type) {
564 case PIPE_AUTH_TYPE_NONE:
565 cmd_entry->rpc_pipe = cli_rpc_pipe_open_noauth(cli,
566 cmd_entry->pipe_idx,
567 &ntresult);
568 break;
569 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
570 cmd_entry->rpc_pipe = cli_rpc_pipe_open_spnego_ntlmssp(cli,
571 cmd_entry->pipe_idx,
572 pipe_default_auth_level,
573 lp_workgroup(),
574 cmdline_auth_info.username,
575 cmdline_auth_info.password,
576 &ntresult);
577 break;
578 case PIPE_AUTH_TYPE_NTLMSSP:
579 cmd_entry->rpc_pipe = cli_rpc_pipe_open_ntlmssp(cli,
580 cmd_entry->pipe_idx,
581 pipe_default_auth_level,
582 lp_workgroup(),
583 cmdline_auth_info.username,
584 cmdline_auth_info.password,
585 &ntresult);
586 break;
587 case PIPE_AUTH_TYPE_SCHANNEL:
588 cmd_entry->rpc_pipe = cli_rpc_pipe_open_schannel(cli,
589 cmd_entry->pipe_idx,
590 pipe_default_auth_level,
591 lp_workgroup(),
592 &ntresult);
593 break;
594 default:
595 DEBUG(0, ("Could not initialise %s. Invalid auth type %u\n",
596 cli_get_pipe_name(cmd_entry->pipe_idx),
597 pipe_default_auth_type ));
598 return NT_STATUS_UNSUCCESSFUL;
600 if (!cmd_entry->rpc_pipe) {
601 DEBUG(0, ("Could not initialise %s. Error was %s\n",
602 cli_get_pipe_name(cmd_entry->pipe_idx),
603 nt_errstr(ntresult) ));
604 return ntresult;
607 if (cmd_entry->pipe_idx == PI_NETLOGON) {
608 uint32 neg_flags = NETLOGON_NEG_AUTH2_FLAGS;
609 uint32 sec_channel_type;
610 uchar trust_password[16];
612 if (!secrets_fetch_trust_account_password(lp_workgroup(),
613 trust_password,
614 NULL, &sec_channel_type)) {
615 return NT_STATUS_UNSUCCESSFUL;
618 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
619 cli->desthost, /* server name */
620 lp_workgroup(), /* domain */
621 global_myname(), /* client name */
622 global_myname(), /* machine account name */
623 trust_password,
624 sec_channel_type,
625 &neg_flags);
627 if (!NT_STATUS_IS_OK(ntresult)) {
628 DEBUG(0, ("Could not initialise credentials for %s.\n",
629 cli_get_pipe_name(cmd_entry->pipe_idx)));
630 return ntresult;
635 /* Run command */
637 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
638 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
639 if (!NT_STATUS_IS_OK(ntresult)) {
640 printf("result was %s\n", nt_errstr(ntresult));
642 } else {
643 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
644 /* print out the DOS error */
645 if (!W_ERROR_IS_OK(wresult)) {
646 printf( "result was %s\n", dos_errstr(wresult));
648 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
651 /* Cleanup */
653 talloc_destroy(mem_ctx);
655 return ntresult;
660 * Process a command entered at the prompt or as part of -c
662 * @returns The NTSTATUS from running the command.
664 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
666 struct cmd_list *temp_list;
667 NTSTATUS result = NT_STATUS_OK;
668 int ret;
669 int argc;
670 char **argv = NULL;
672 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
673 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
674 return NT_STATUS_UNSUCCESSFUL;
678 /* Walk through a dlist of arrays of commands. */
679 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
680 struct cmd_set *temp_set = temp_list->cmd_set;
682 while (temp_set->name) {
683 if (strequal(argv[0], temp_set->name)) {
684 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
685 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
686 fprintf (stderr, "Invalid command\n");
687 goto out_free;
690 result = do_cmd(cli, temp_set, argc, argv);
692 goto out_free;
694 temp_set++;
698 if (argv[0]) {
699 printf("command not found: %s\n", argv[0]);
702 out_free:
703 /* moved to do_cmd()
704 if (!NT_STATUS_IS_OK(result)) {
705 printf("result was %s\n", nt_errstr(result));
709 /* NOTE: popt allocates the whole argv, including the
710 * strings, as a single block. So a single free is
711 * enough to release it -- we don't free the
712 * individual strings. rtfm. */
713 free(argv);
715 return result;
719 /* Main function */
721 int main(int argc, char *argv[])
723 int opt;
724 static char *cmdstr = NULL;
725 const char *server;
726 struct cli_state *cli;
727 static char *opt_ipaddr=NULL;
728 struct cmd_set **cmd_set;
729 struct in_addr server_ip;
730 NTSTATUS nt_status;
731 static int opt_port = 0;
732 fstring new_workgroup;
734 /* make sure the vars that get altered (4th field) are in
735 a fixed location or certain compilers complain */
736 poptContext pc;
737 struct poptOption long_options[] = {
738 POPT_AUTOHELP
739 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
740 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
741 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
742 POPT_COMMON_SAMBA
743 POPT_COMMON_CONNECTION
744 POPT_COMMON_CREDENTIALS
745 POPT_TABLEEND
748 load_case_tables();
750 ZERO_STRUCT(server_ip);
752 setlinebuf(stdout);
754 /* the following functions are part of the Samba debugging
755 facilities. See lib/debug.c */
756 setup_logging("rpcclient", True);
758 /* Parse options */
760 pc = poptGetContext("rpcclient", argc, (const char **) argv,
761 long_options, 0);
763 if (argc == 1) {
764 poptPrintHelp(pc, stderr, 0);
765 return 0;
768 while((opt = poptGetNextOpt(pc)) != -1) {
769 switch (opt) {
771 case 'I':
772 if ( (server_ip.s_addr=inet_addr(opt_ipaddr)) == INADDR_NONE ) {
773 fprintf(stderr, "%s not a valid IP address\n",
774 opt_ipaddr);
775 return 1;
780 /* Get server as remaining unparsed argument. Print usage if more
781 than one unparsed argument is present. */
783 server = poptGetArg(pc);
785 if (!server || poptGetArg(pc)) {
786 poptPrintHelp(pc, stderr, 0);
787 return 1;
790 poptFreeContext(pc);
792 load_interfaces();
794 if (!init_names())
795 return 1;
797 /* save the workgroup...
799 FIXME!! do we need to do this for other options as well
800 (or maybe a generic way to keep lp_load() from overwriting
801 everything)? */
803 fstrcpy( new_workgroup, lp_workgroup() );
805 /* Load smb.conf file */
807 if (!lp_load(dyn_CONFIGFILE,True,False,False,True))
808 fprintf(stderr, "Can't load %s\n", dyn_CONFIGFILE);
810 if ( strlen(new_workgroup) != 0 )
811 set_global_myworkgroup( new_workgroup );
814 * Get password
815 * from stdin if necessary
818 if (!cmdline_auth_info.got_pass) {
819 char *pass = getpass("Password:");
820 if (pass) {
821 pstrcpy(cmdline_auth_info.password, pass);
825 if ((server[0] == '/' && server[1] == '/') ||
826 (server[0] == '\\' && server[1] == '\\')) {
827 server += 2;
830 nt_status = cli_full_connection(&cli, global_myname(), server,
831 opt_ipaddr ? &server_ip : NULL, opt_port,
832 "IPC$", "IPC",
833 cmdline_auth_info.username,
834 lp_workgroup(),
835 cmdline_auth_info.password,
836 cmdline_auth_info.use_kerberos ? CLI_FULL_CONNECTION_USE_KERBEROS : 0,
837 cmdline_auth_info.signing_state,NULL);
839 if (!NT_STATUS_IS_OK(nt_status)) {
840 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
841 return 1;
844 #if 0 /* COMMENT OUT FOR TESTING */
845 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
846 #endif
848 /* Load command lists */
850 timeout = cli_set_timeout(cli, 10000);
852 cmd_set = rpcclient_command_list;
854 while(*cmd_set) {
855 add_command_set(*cmd_set);
856 add_command_set(separator_command);
857 cmd_set++;
860 fetch_machine_sid(cli);
862 /* Do anything specified with -c */
863 if (cmdstr && cmdstr[0]) {
864 char *cmd;
865 char *p = cmdstr;
866 int result = 0;
868 while((cmd=next_command(&p)) != NULL) {
869 NTSTATUS cmd_result = process_cmd(cli, cmd);
870 SAFE_FREE(cmd);
871 result = NT_STATUS_IS_ERR(cmd_result);
874 cli_shutdown(cli);
875 return result;
878 /* Loop around accepting commands */
880 while(1) {
881 pstring prompt;
882 char *line;
884 slprintf(prompt, sizeof(prompt) - 1, "rpcclient $> ");
886 line = smb_readline(prompt, NULL, completion_fn);
888 if (line == NULL)
889 break;
891 if (line[0] != '\n')
892 process_cmd(cli, line);
895 cli_shutdown(cli);
896 return 0;