net: Fix several typos in comments.
[Samba.git] / source / rpcclient / rpcclient.c
blob35ff14ef2d6df6337807f01e65ab5e5d9f716f95
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 if ((lsapipe = cli_rpc_pipe_open_noauth(cli, PI_LSARPC, &result)) == NULL) {
149 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
150 goto error;
153 result = rpccli_lsa_open_policy(lsapipe, mem_ctx, True,
154 SEC_RIGHTS_MAXIMUM_ALLOWED,
155 &pol);
156 if (!NT_STATUS_IS_OK(result)) {
157 goto error;
160 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
161 &pol,
162 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
163 &info);
164 if (!NT_STATUS_IS_OK(result)) {
165 goto error;
168 got_domain_sid = True;
169 sid_copy(&domain_sid, info->account_domain.sid);
171 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
172 TALLOC_FREE(lsapipe);
173 talloc_destroy(mem_ctx);
175 return;
177 error:
179 if (lsapipe) {
180 TALLOC_FREE(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
338 != pipe_default_auth_type)
339 || (tmp_set->rpc_pipe->auth->auth_level
340 != pipe_default_auth_level)) {
341 TALLOC_FREE(tmp_set->rpc_pipe);
342 tmp_set->rpc_pipe = NULL;
346 return NT_STATUS_OK;
349 static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
350 int argc, const char **argv)
352 const char *type = "NTLMSSP";
354 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
355 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
357 if (argc > 2) {
358 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
359 return NT_STATUS_OK;
362 if (argc == 2) {
363 type = argv[1];
364 if (strequal(type, "NTLMSSP")) {
365 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
366 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
367 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
368 } else if (strequal(type, "SCHANNEL")) {
369 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
370 } else {
371 printf("unknown type %s\n", type);
372 return NT_STATUS_INVALID_LEVEL;
376 d_printf("Setting %s - sign\n", type);
378 return cmd_set_ss_level();
381 static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
382 int argc, const char **argv)
384 const char *type = "NTLMSSP";
386 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
387 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
389 if (argc > 2) {
390 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
391 return NT_STATUS_OK;
394 if (argc == 2) {
395 type = argv[1];
396 if (strequal(type, "NTLMSSP")) {
397 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
398 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
399 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
400 } else if (strequal(type, "SCHANNEL")) {
401 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
402 } else {
403 printf("unknown type %s\n", type);
404 return NT_STATUS_INVALID_LEVEL;
408 d_printf("Setting %s - sign and seal\n", type);
410 return cmd_set_ss_level();
413 static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
414 int argc, const char **argv)
416 struct cmd_list *tmp;
418 if (argc > 2) {
419 printf("Usage: %s timeout\n", argv[0]);
420 return NT_STATUS_OK;
423 if (argc == 2) {
424 timeout = atoi(argv[1]);
426 for (tmp = cmd_list; tmp; tmp = tmp->next) {
428 struct cmd_set *tmp_set;
430 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
431 if (tmp_set->rpc_pipe == NULL) {
432 continue;
435 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
440 printf("timeout is %d\n", timeout);
442 return NT_STATUS_OK;
446 static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
447 int argc, const char **argv)
449 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
450 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
452 return cmd_set_ss_level();
455 static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
456 int argc, const char **argv)
458 d_printf("Setting schannel - sign and seal\n");
459 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
460 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
462 return cmd_set_ss_level();
465 static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
466 int argc, const char **argv)
468 d_printf("Setting schannel - sign only\n");
469 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
470 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
472 return cmd_set_ss_level();
476 /* Built in rpcclient commands */
478 static struct cmd_set rpcclient_commands[] = {
480 { "GENERAL OPTIONS" },
482 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, NULL, "Get help on commands", "[command]" },
483 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, -1, NULL, "Get help on commands", "[command]" },
484 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, NULL, "Set debug level", "level" },
485 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, -1, NULL, "Set debug level", "level" },
486 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, -1, NULL, "List available commands on <pipe>", "pipe" },
487 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, NULL, "Exit program", "" },
488 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, -1, NULL, "Exit program", "" },
489 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, -1, NULL, "Force RPC pipe connections to be signed", "" },
490 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, -1, NULL, "Force RPC pipe connections to be sealed", "" },
491 { "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.", "" },
492 { "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.", "" },
493 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, -1, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
494 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, -1, NULL, "Force RPC pipe connections to have no special properties", "" },
496 { NULL }
499 static struct cmd_set separator_command[] = {
500 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, -1, NULL, "----------------------" },
501 { NULL }
505 /* Various pipe commands */
507 extern struct cmd_set lsarpc_commands[];
508 extern struct cmd_set samr_commands[];
509 extern struct cmd_set spoolss_commands[];
510 extern struct cmd_set netlogon_commands[];
511 extern struct cmd_set srvsvc_commands[];
512 extern struct cmd_set dfs_commands[];
513 extern struct cmd_set ds_commands[];
514 extern struct cmd_set echo_commands[];
515 extern struct cmd_set shutdown_commands[];
516 extern struct cmd_set test_commands[];
517 extern struct cmd_set wkssvc_commands[];
518 extern struct cmd_set ntsvcs_commands[];
519 extern struct cmd_set drsuapi_commands[];
521 static struct cmd_set *rpcclient_command_list[] = {
522 rpcclient_commands,
523 lsarpc_commands,
524 ds_commands,
525 samr_commands,
526 spoolss_commands,
527 netlogon_commands,
528 srvsvc_commands,
529 dfs_commands,
530 echo_commands,
531 shutdown_commands,
532 test_commands,
533 wkssvc_commands,
534 ntsvcs_commands,
535 drsuapi_commands,
536 NULL
539 static void add_command_set(struct cmd_set *cmd_set)
541 struct cmd_list *entry;
543 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
544 DEBUG(0, ("out of memory\n"));
545 return;
548 ZERO_STRUCTP(entry);
550 entry->cmd_set = cmd_set;
551 DLIST_ADD(cmd_list, entry);
556 * Call an rpcclient function, passing an argv array.
558 * @param cmd Command to run, as a single string.
560 static NTSTATUS do_cmd(struct cli_state *cli,
561 struct cmd_set *cmd_entry,
562 int argc, char **argv)
564 NTSTATUS ntresult;
565 WERROR wresult;
567 TALLOC_CTX *mem_ctx;
569 /* Create mem_ctx */
571 if (!(mem_ctx = talloc_init("do_cmd"))) {
572 DEBUG(0, ("talloc_init() failed\n"));
573 return NT_STATUS_NO_MEMORY;
576 /* Open pipe */
578 if (cmd_entry->pipe_idx != -1 && cmd_entry->rpc_pipe == NULL) {
579 switch (pipe_default_auth_type) {
580 case PIPE_AUTH_TYPE_NONE:
581 cmd_entry->rpc_pipe = cli_rpc_pipe_open_noauth(cli,
582 cmd_entry->pipe_idx,
583 &ntresult);
584 break;
585 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
586 cmd_entry->rpc_pipe = cli_rpc_pipe_open_spnego_ntlmssp(cli,
587 cmd_entry->pipe_idx,
588 pipe_default_auth_level,
589 lp_workgroup(),
590 get_cmdline_auth_info_username(),
591 get_cmdline_auth_info_password(),
592 &ntresult);
593 break;
594 case PIPE_AUTH_TYPE_NTLMSSP:
595 cmd_entry->rpc_pipe = cli_rpc_pipe_open_ntlmssp(cli,
596 cmd_entry->pipe_idx,
597 pipe_default_auth_level,
598 lp_workgroup(),
599 get_cmdline_auth_info_username(),
600 get_cmdline_auth_info_password(),
601 &ntresult);
602 break;
603 case PIPE_AUTH_TYPE_SCHANNEL:
604 cmd_entry->rpc_pipe = cli_rpc_pipe_open_schannel(cli,
605 cmd_entry->pipe_idx,
606 pipe_default_auth_level,
607 lp_workgroup(),
608 &ntresult);
609 break;
610 default:
611 DEBUG(0, ("Could not initialise %s. Invalid auth type %u\n",
612 cli_get_pipe_name(cmd_entry->pipe_idx),
613 pipe_default_auth_type ));
614 return NT_STATUS_UNSUCCESSFUL;
616 if (!cmd_entry->rpc_pipe) {
617 DEBUG(0, ("Could not initialise %s. Error was %s\n",
618 cli_get_pipe_name(cmd_entry->pipe_idx),
619 nt_errstr(ntresult) ));
620 return ntresult;
623 if (cmd_entry->pipe_idx == PI_NETLOGON) {
624 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
625 uint32 sec_channel_type;
626 uchar trust_password[16];
628 if (!secrets_fetch_trust_account_password(lp_workgroup(),
629 trust_password,
630 NULL, &sec_channel_type)) {
631 return NT_STATUS_UNSUCCESSFUL;
634 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
635 cli->desthost, /* server name */
636 lp_workgroup(), /* domain */
637 global_myname(), /* client name */
638 global_myname(), /* machine account name */
639 trust_password,
640 sec_channel_type,
641 &neg_flags);
643 if (!NT_STATUS_IS_OK(ntresult)) {
644 DEBUG(0, ("Could not initialise credentials for %s.\n",
645 cli_get_pipe_name(cmd_entry->pipe_idx)));
646 return ntresult;
651 /* Run command */
653 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
654 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
655 if (!NT_STATUS_IS_OK(ntresult)) {
656 printf("result was %s\n", nt_errstr(ntresult));
658 } else {
659 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
660 /* print out the DOS error */
661 if (!W_ERROR_IS_OK(wresult)) {
662 printf( "result was %s\n", dos_errstr(wresult));
664 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
667 /* Cleanup */
669 talloc_destroy(mem_ctx);
671 return ntresult;
676 * Process a command entered at the prompt or as part of -c
678 * @returns The NTSTATUS from running the command.
680 static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
682 struct cmd_list *temp_list;
683 NTSTATUS result = NT_STATUS_OK;
684 int ret;
685 int argc;
686 char **argv = NULL;
688 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
689 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
690 return NT_STATUS_UNSUCCESSFUL;
694 /* Walk through a dlist of arrays of commands. */
695 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
696 struct cmd_set *temp_set = temp_list->cmd_set;
698 while (temp_set->name) {
699 if (strequal(argv[0], temp_set->name)) {
700 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
701 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
702 fprintf (stderr, "Invalid command\n");
703 goto out_free;
706 result = do_cmd(cli, temp_set, argc, argv);
708 goto out_free;
710 temp_set++;
714 if (argv[0]) {
715 printf("command not found: %s\n", argv[0]);
718 out_free:
719 /* moved to do_cmd()
720 if (!NT_STATUS_IS_OK(result)) {
721 printf("result was %s\n", nt_errstr(result));
725 /* NOTE: popt allocates the whole argv, including the
726 * strings, as a single block. So a single free is
727 * enough to release it -- we don't free the
728 * individual strings. rtfm. */
729 free(argv);
731 return result;
735 /* Main function */
737 int main(int argc, char *argv[])
739 int opt;
740 static char *cmdstr = NULL;
741 const char *server;
742 struct cli_state *cli = NULL;
743 static char *opt_ipaddr=NULL;
744 struct cmd_set **cmd_set;
745 struct sockaddr_storage server_ss;
746 NTSTATUS nt_status;
747 static int opt_port = 0;
748 fstring new_workgroup;
749 int result = 0;
750 TALLOC_CTX *frame = talloc_stackframe();
751 uint32_t flags = 0;
753 /* make sure the vars that get altered (4th field) are in
754 a fixed location or certain compilers complain */
755 poptContext pc;
756 struct poptOption long_options[] = {
757 POPT_AUTOHELP
758 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
759 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
760 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
761 POPT_COMMON_SAMBA
762 POPT_COMMON_CONNECTION
763 POPT_COMMON_CREDENTIALS
764 POPT_TABLEEND
767 load_case_tables();
769 zero_addr(&server_ss);
771 setlinebuf(stdout);
773 /* the following functions are part of the Samba debugging
774 facilities. See lib/debug.c */
775 setup_logging("rpcclient", True);
777 /* Parse options */
779 pc = poptGetContext("rpcclient", argc, (const char **) argv,
780 long_options, 0);
782 if (argc == 1) {
783 poptPrintHelp(pc, stderr, 0);
784 goto done;
787 while((opt = poptGetNextOpt(pc)) != -1) {
788 switch (opt) {
790 case 'I':
791 if (!interpret_string_addr(&server_ss,
792 opt_ipaddr,
793 AI_NUMERICHOST)) {
794 fprintf(stderr, "%s not a valid IP address\n",
795 opt_ipaddr);
796 result = 1;
797 goto done;
802 /* Get server as remaining unparsed argument. Print usage if more
803 than one unparsed argument is present. */
805 server = poptGetArg(pc);
807 if (!server || poptGetArg(pc)) {
808 poptPrintHelp(pc, stderr, 0);
809 result = 1;
810 goto done;
813 poptFreeContext(pc);
815 load_interfaces();
817 if (!init_names()) {
818 result = 1;
819 goto done;
822 /* save the workgroup...
824 FIXME!! do we need to do this for other options as well
825 (or maybe a generic way to keep lp_load() from overwriting
826 everything)? */
828 fstrcpy( new_workgroup, lp_workgroup() );
830 /* Load smb.conf file */
832 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
833 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
835 if ( strlen(new_workgroup) != 0 )
836 set_global_myworkgroup( new_workgroup );
839 * Get password
840 * from stdin if necessary
843 if (get_cmdline_auth_info_use_machine_account() &&
844 !set_cmdline_auth_info_machine_account_creds()) {
845 result = 1;
846 goto done;
849 if (!get_cmdline_auth_info_got_pass()) {
850 char *pass = getpass("Password:");
851 if (pass) {
852 set_cmdline_auth_info_password(pass);
856 if ((server[0] == '/' && server[1] == '/') ||
857 (server[0] == '\\' && server[1] == '\\')) {
858 server += 2;
861 if (get_cmdline_auth_info_use_kerberos()) {
862 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
863 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
867 nt_status = cli_full_connection(&cli, global_myname(), server,
868 opt_ipaddr ? &server_ss : NULL, opt_port,
869 "IPC$", "IPC",
870 get_cmdline_auth_info_username(),
871 lp_workgroup(),
872 get_cmdline_auth_info_password(),
873 flags,
874 get_cmdline_auth_info_signing_state(),NULL);
876 if (!NT_STATUS_IS_OK(nt_status)) {
877 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
878 result = 1;
879 goto done;
882 if (get_cmdline_auth_info_smb_encrypt()) {
883 nt_status = cli_cm_force_encryption(cli,
884 get_cmdline_auth_info_username(),
885 get_cmdline_auth_info_password(),
886 lp_workgroup(),
887 "IPC$");
888 if (!NT_STATUS_IS_OK(nt_status)) {
889 result = 1;
890 goto done;
894 #if 0 /* COMMENT OUT FOR TESTING */
895 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
896 #endif
898 /* Load command lists */
900 timeout = cli_set_timeout(cli, 10000);
902 cmd_set = rpcclient_command_list;
904 while(*cmd_set) {
905 add_command_set(*cmd_set);
906 add_command_set(separator_command);
907 cmd_set++;
910 fetch_machine_sid(cli);
912 /* Do anything specified with -c */
913 if (cmdstr && cmdstr[0]) {
914 char *cmd;
915 char *p = cmdstr;
917 result = 0;
919 while((cmd=next_command(&p)) != NULL) {
920 NTSTATUS cmd_result = process_cmd(cli, cmd);
921 SAFE_FREE(cmd);
922 result = NT_STATUS_IS_ERR(cmd_result);
925 goto done;
928 /* Loop around accepting commands */
930 while(1) {
931 char *line = NULL;
933 line = smb_readline("rpcclient $> ", NULL, completion_fn);
935 if (line == NULL)
936 break;
938 if (line[0] != '\n')
939 process_cmd(cli, line);
940 SAFE_FREE(line);
943 done:
944 if (cli != NULL) {
945 cli_shutdown(cli);
947 TALLOC_FREE(frame);
948 return result;