source: branches/samba-3.3.x/source/rpcclient/rpcclient.c@ 342

Last change on this file since 342 was 342, checked in by Herwig Bauernfeind, 16 years ago

Update 3.3 to 3.3.9

File size: 24.6 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 RPC pipe client
4
5 Copyright (C) Tim Potter 2000-2001
6 Copyright (C) Martin Pool 2003
7
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.
12
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.
17
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/>.
20*/
21
22#include "includes.h"
23#include "rpcclient.h"
24
25DOM_SID domain_sid;
26
27static enum pipe_auth_type pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
28static enum pipe_auth_level pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
29static unsigned int timeout = 0;
30
31/* List to hold groups of commands.
32 *
33 * Commands are defined in a list of arrays: arrays are easy to
34 * statically declare, and lists are easier to dynamically extend.
35 */
36
37static struct cmd_list {
38 struct cmd_list *prev, *next;
39 struct cmd_set *cmd_set;
40} *cmd_list;
41
42/****************************************************************************
43handle completion of commands for readline
44****************************************************************************/
45static char **completion_fn(const char *text, int start, int end)
46{
47#define MAX_COMPLETIONS 100
48 char **matches;
49 int i, count=0;
50 struct cmd_list *commands = cmd_list;
51
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
59
60 /* make sure we have a list of valid commands */
61 if (!commands) {
62 return NULL;
63 }
64
65 matches = SMB_MALLOC_ARRAY(char *, MAX_COMPLETIONS);
66 if (!matches) {
67 return NULL;
68 }
69
70 matches[count++] = SMB_STRDUP(text);
71 if (!matches[0]) {
72 SAFE_FREE(matches);
73 return NULL;
74 }
75
76 while (commands && count < MAX_COMPLETIONS-1) {
77 if (!commands->cmd_set) {
78 break;
79 }
80
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]);
91 }
92 SAFE_FREE(matches);
93 return NULL;
94 }
95 count++;
96 }
97 }
98 commands = commands->next;
99
100 }
101
102 if (count == 2) {
103 SAFE_FREE(matches[0]);
104 matches[0] = SMB_STRDUP(matches[1]);
105 }
106 matches[count] = NULL;
107 return matches;
108}
109
110static char *next_command (char **cmdstr)
111{
112 char *command;
113 char *p;
114
115 if (!cmdstr || !(*cmdstr))
116 return NULL;
117
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;
126
127 return command;
128}
129
130/* Fetch the SID for this computer */
131
132static void fetch_machine_sid(struct cli_state *cli)
133{
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;
140
141 if (got_domain_sid) return;
142
143 if (!(mem_ctx=talloc_init("fetch_machine_sid"))) {
144 DEBUG(0,("fetch_machine_sid: talloc_init returned NULL!\n"));
145 goto error;
146 }
147
148 result = cli_rpc_pipe_open_noauth(cli, &ndr_table_lsarpc.syntax_id,
149 &lsapipe);
150 if (!NT_STATUS_IS_OK(result)) {
151 fprintf(stderr, "could not initialise lsa pipe. Error was %s\n", nt_errstr(result) );
152 goto error;
153 }
154
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;
160 }
161
162 result = rpccli_lsa_QueryInfoPolicy(lsapipe, mem_ctx,
163 &pol,
164 LSA_POLICY_INFO_ACCOUNT_DOMAIN,
165 &info);
166 if (!NT_STATUS_IS_OK(result)) {
167 goto error;
168 }
169
170 got_domain_sid = True;
171 sid_copy(&domain_sid, info->account_domain.sid);
172
173 rpccli_lsa_Close(lsapipe, mem_ctx, &pol);
174 TALLOC_FREE(lsapipe);
175 talloc_destroy(mem_ctx);
176
177 return;
178
179 error:
180
181 if (lsapipe) {
182 TALLOC_FREE(lsapipe);
183 }
184
185 fprintf(stderr, "could not obtain sid for domain %s\n", cli->domain);
186
187 if (!NT_STATUS_IS_OK(result)) {
188 fprintf(stderr, "error: %s\n", nt_errstr(result));
189 }
190
191 exit(1);
192}
193
194/* List the available commands on a given pipe */
195
196static NTSTATUS cmd_listcommands(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
197 int argc, const char **argv)
198{
199 struct cmd_list *tmp;
200 struct cmd_set *tmp_set;
201 int i;
202
203 /* Usage */
204
205 if (argc != 2) {
206 printf("Usage: %s <pipe>\n", argv[0]);
207 return NT_STATUS_OK;
208 }
209
210 /* Help on one command */
211
212 for (tmp = cmd_list; tmp; tmp = tmp->next)
213 {
214 tmp_set = tmp->cmd_set;
215
216 if (!StrCaseCmp(argv[1], tmp_set->name))
217 {
218 printf("Available commands on the %s pipe:\n\n", tmp_set->name);
219
220 i = 0;
221 tmp_set++;
222 while(tmp_set->name) {
223 printf("%30s", tmp_set->name);
224 tmp_set++;
225 i++;
226 if (i%3 == 0)
227 printf("\n");
228 }
229
230 /* drop out of the loop */
231 break;
232 }
233 }
234 printf("\n\n");
235
236 return NT_STATUS_OK;
237}
238
239/* Display help on commands */
240
241static NTSTATUS cmd_help(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
242 int argc, const char **argv)
243{
244 struct cmd_list *tmp;
245 struct cmd_set *tmp_set;
246
247 /* Usage */
248
249 if (argc > 2) {
250 printf("Usage: %s [command]\n", argv[0]);
251 return NT_STATUS_OK;
252 }
253
254 /* Help on one command */
255
256 if (argc == 2) {
257 for (tmp = cmd_list; tmp; tmp = tmp->next) {
258
259 tmp_set = tmp->cmd_set;
260
261 while(tmp_set->name) {
262 if (strequal(argv[1], tmp_set->name)) {
263 if (tmp_set->usage &&
264 tmp_set->usage[0])
265 printf("%s\n", tmp_set->usage);
266 else
267 printf("No help for %s\n", tmp_set->name);
268
269 return NT_STATUS_OK;
270 }
271
272 tmp_set++;
273 }
274 }
275
276 printf("No such command: %s\n", argv[1]);
277 return NT_STATUS_OK;
278 }
279
280 /* List all commands */
281
282 for (tmp = cmd_list; tmp; tmp = tmp->next) {
283
284 tmp_set = tmp->cmd_set;
285
286 while(tmp_set->name) {
287
288 printf("%15s\t\t%s\n", tmp_set->name,
289 tmp_set->description ? tmp_set->description:
290 "");
291
292 tmp_set++;
293 }
294 }
295
296 return NT_STATUS_OK;
297}
298
299/* Change the debug level */
300
301static NTSTATUS cmd_debuglevel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
302 int argc, const char **argv)
303{
304 if (argc > 2) {
305 printf("Usage: %s [debuglevel]\n", argv[0]);
306 return NT_STATUS_OK;
307 }
308
309 if (argc == 2) {
310 DEBUGLEVEL = atoi(argv[1]);
311 }
312
313 printf("debuglevel is %d\n", DEBUGLEVEL);
314
315 return NT_STATUS_OK;
316}
317
318static NTSTATUS cmd_quit(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
319 int argc, const char **argv)
320{
321 exit(0);
322 return NT_STATUS_OK; /* NOTREACHED */
323}
324
325static NTSTATUS cmd_set_ss_level(void)
326{
327 struct cmd_list *tmp;
328
329 /* Close any existing connections not at this level. */
330
331 for (tmp = cmd_list; tmp; tmp = tmp->next) {
332 struct cmd_set *tmp_set;
333
334 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
335 if (tmp_set->rpc_pipe == NULL) {
336 continue;
337 }
338
339 if ((tmp_set->rpc_pipe->auth->auth_type
340 != pipe_default_auth_type)
341 || (tmp_set->rpc_pipe->auth->auth_level
342 != pipe_default_auth_level)) {
343 TALLOC_FREE(tmp_set->rpc_pipe);
344 tmp_set->rpc_pipe = NULL;
345 }
346 }
347 }
348 return NT_STATUS_OK;
349}
350
351static NTSTATUS cmd_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
352 int argc, const char **argv)
353{
354 const char *type = "NTLMSSP";
355
356 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
357 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
358
359 if (argc > 2) {
360 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
361 return NT_STATUS_OK;
362 }
363
364 if (argc == 2) {
365 type = argv[1];
366 if (strequal(type, "NTLMSSP")) {
367 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
368 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
369 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
370 } else if (strequal(type, "SCHANNEL")) {
371 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
372 } else {
373 printf("unknown type %s\n", type);
374 return NT_STATUS_INVALID_LEVEL;
375 }
376 }
377
378 d_printf("Setting %s - sign\n", type);
379
380 return cmd_set_ss_level();
381}
382
383static NTSTATUS cmd_seal(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
384 int argc, const char **argv)
385{
386 const char *type = "NTLMSSP";
387
388 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
389 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
390
391 if (argc > 2) {
392 printf("Usage: %s [NTLMSSP|NTLMSSP_SPNEGO|SCHANNEL]\n", argv[0]);
393 return NT_STATUS_OK;
394 }
395
396 if (argc == 2) {
397 type = argv[1];
398 if (strequal(type, "NTLMSSP")) {
399 pipe_default_auth_type = PIPE_AUTH_TYPE_NTLMSSP;
400 } else if (strequal(type, "NTLMSSP_SPNEGO")) {
401 pipe_default_auth_type = PIPE_AUTH_TYPE_SPNEGO_NTLMSSP;
402 } else if (strequal(type, "SCHANNEL")) {
403 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
404 } else {
405 printf("unknown type %s\n", type);
406 return NT_STATUS_INVALID_LEVEL;
407 }
408 }
409
410 d_printf("Setting %s - sign and seal\n", type);
411
412 return cmd_set_ss_level();
413}
414
415static NTSTATUS cmd_timeout(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
416 int argc, const char **argv)
417{
418 struct cmd_list *tmp;
419
420 if (argc > 2) {
421 printf("Usage: %s timeout\n", argv[0]);
422 return NT_STATUS_OK;
423 }
424
425 if (argc == 2) {
426 timeout = atoi(argv[1]);
427
428 for (tmp = cmd_list; tmp; tmp = tmp->next) {
429
430 struct cmd_set *tmp_set;
431
432 for (tmp_set = tmp->cmd_set; tmp_set->name; tmp_set++) {
433 if (tmp_set->rpc_pipe == NULL) {
434 continue;
435 }
436
437 rpccli_set_timeout(tmp_set->rpc_pipe, timeout);
438 }
439 }
440 }
441
442 printf("timeout is %d\n", timeout);
443
444 return NT_STATUS_OK;
445}
446
447
448static NTSTATUS cmd_none(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
449 int argc, const char **argv)
450{
451 pipe_default_auth_level = PIPE_AUTH_LEVEL_NONE;
452 pipe_default_auth_type = PIPE_AUTH_TYPE_NONE;
453
454 return cmd_set_ss_level();
455}
456
457static NTSTATUS cmd_schannel(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
458 int argc, const char **argv)
459{
460 d_printf("Setting schannel - sign and seal\n");
461 pipe_default_auth_level = PIPE_AUTH_LEVEL_PRIVACY;
462 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
463
464 return cmd_set_ss_level();
465}
466
467static NTSTATUS cmd_schannel_sign(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx,
468 int argc, const char **argv)
469{
470 d_printf("Setting schannel - sign only\n");
471 pipe_default_auth_level = PIPE_AUTH_LEVEL_INTEGRITY;
472 pipe_default_auth_type = PIPE_AUTH_TYPE_SCHANNEL;
473
474 return cmd_set_ss_level();
475}
476
477
478/* Built in rpcclient commands */
479
480static struct cmd_set rpcclient_commands[] = {
481
482 { "GENERAL OPTIONS" },
483
484 { "help", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
485 { "?", RPC_RTYPE_NTSTATUS, cmd_help, NULL, NULL, NULL, "Get help on commands", "[command]" },
486 { "debuglevel", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
487 { "debug", RPC_RTYPE_NTSTATUS, cmd_debuglevel, NULL, NULL, NULL, "Set debug level", "level" },
488 { "list", RPC_RTYPE_NTSTATUS, cmd_listcommands, NULL, NULL, NULL, "List available commands on <pipe>", "pipe" },
489 { "exit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
490 { "quit", RPC_RTYPE_NTSTATUS, cmd_quit, NULL, NULL, NULL, "Exit program", "" },
491 { "sign", RPC_RTYPE_NTSTATUS, cmd_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed", "" },
492 { "seal", RPC_RTYPE_NTSTATUS, cmd_seal, NULL, NULL, NULL, "Force RPC pipe connections to be sealed", "" },
493 { "schannel", RPC_RTYPE_NTSTATUS, cmd_schannel, NULL, NULL, NULL, "Force RPC pipe connections to be sealed with 'schannel'. Assumes valid machine account to this domain controller.", "" },
494 { "schannelsign", RPC_RTYPE_NTSTATUS, cmd_schannel_sign, NULL, NULL, NULL, "Force RPC pipe connections to be signed (not sealed) with 'schannel'. Assumes valid machine account to this domain controller.", "" },
495 { "timeout", RPC_RTYPE_NTSTATUS, cmd_timeout, NULL, NULL, NULL, "Set timeout (in milliseonds) for RPC operations", "" },
496 { "none", RPC_RTYPE_NTSTATUS, cmd_none, NULL, NULL, NULL, "Force RPC pipe connections to have no special properties", "" },
497
498 { NULL }
499};
500
501static struct cmd_set separator_command[] = {
502 { "---------------", MAX_RPC_RETURN_TYPE, NULL, NULL, NULL, NULL, "----------------------" },
503 { NULL }
504};
505
506
507/* Various pipe commands */
508
509extern struct cmd_set lsarpc_commands[];
510extern struct cmd_set samr_commands[];
511extern struct cmd_set spoolss_commands[];
512extern struct cmd_set netlogon_commands[];
513extern struct cmd_set srvsvc_commands[];
514extern struct cmd_set dfs_commands[];
515extern struct cmd_set ds_commands[];
516extern struct cmd_set echo_commands[];
517extern struct cmd_set shutdown_commands[];
518extern struct cmd_set test_commands[];
519extern struct cmd_set wkssvc_commands[];
520extern struct cmd_set ntsvcs_commands[];
521extern struct cmd_set drsuapi_commands[];
522
523static struct cmd_set *rpcclient_command_list[] = {
524 rpcclient_commands,
525 lsarpc_commands,
526 ds_commands,
527 samr_commands,
528 spoolss_commands,
529 netlogon_commands,
530 srvsvc_commands,
531 dfs_commands,
532 echo_commands,
533 shutdown_commands,
534 test_commands,
535 wkssvc_commands,
536 ntsvcs_commands,
537 drsuapi_commands,
538 NULL
539};
540
541static void add_command_set(struct cmd_set *cmd_set)
542{
543 struct cmd_list *entry;
544
545 if (!(entry = SMB_MALLOC_P(struct cmd_list))) {
546 DEBUG(0, ("out of memory\n"));
547 return;
548 }
549
550 ZERO_STRUCTP(entry);
551
552 entry->cmd_set = cmd_set;
553 DLIST_ADD(cmd_list, entry);
554}
555
556
557/**
558 * Call an rpcclient function, passing an argv array.
559 *
560 * @param cmd Command to run, as a single string.
561 **/
562static NTSTATUS do_cmd(struct cli_state *cli,
563 struct cmd_set *cmd_entry,
564 int argc, char **argv)
565{
566 NTSTATUS ntresult;
567 WERROR wresult;
568
569 TALLOC_CTX *mem_ctx;
570
571 /* Create mem_ctx */
572
573 if (!(mem_ctx = talloc_init("do_cmd"))) {
574 DEBUG(0, ("talloc_init() failed\n"));
575 return NT_STATUS_NO_MEMORY;
576 }
577
578 /* Open pipe */
579
580 if ((cmd_entry->interface != NULL) && (cmd_entry->rpc_pipe == NULL)) {
581 switch (pipe_default_auth_type) {
582 case PIPE_AUTH_TYPE_NONE:
583 ntresult = cli_rpc_pipe_open_noauth(
584 cli, cmd_entry->interface,
585 &cmd_entry->rpc_pipe);
586 break;
587 case PIPE_AUTH_TYPE_SPNEGO_NTLMSSP:
588 ntresult = cli_rpc_pipe_open_spnego_ntlmssp(
589 cli, cmd_entry->interface,
590 NCACN_NP,
591 pipe_default_auth_level,
592 lp_workgroup(),
593 get_cmdline_auth_info_username(),
594 get_cmdline_auth_info_password(),
595 &cmd_entry->rpc_pipe);
596 break;
597 case PIPE_AUTH_TYPE_NTLMSSP:
598 ntresult = cli_rpc_pipe_open_ntlmssp(
599 cli, cmd_entry->interface,
600 NCACN_NP,
601 pipe_default_auth_level,
602 lp_workgroup(),
603 get_cmdline_auth_info_username(),
604 get_cmdline_auth_info_password(),
605 &cmd_entry->rpc_pipe);
606 break;
607 case PIPE_AUTH_TYPE_SCHANNEL:
608 ntresult = cli_rpc_pipe_open_schannel(
609 cli, cmd_entry->interface,
610 NCACN_NP,
611 pipe_default_auth_level,
612 lp_workgroup(),
613 &cmd_entry->rpc_pipe);
614 break;
615 default:
616 DEBUG(0, ("Could not initialise %s. Invalid "
617 "auth type %u\n",
618 cli_get_pipe_name_from_iface(
619 debug_ctx(), cli,
620 cmd_entry->interface),
621 pipe_default_auth_type ));
622 return NT_STATUS_UNSUCCESSFUL;
623 }
624 if (!NT_STATUS_IS_OK(ntresult)) {
625 DEBUG(0, ("Could not initialise %s. Error was %s\n",
626 cli_get_pipe_name_from_iface(
627 debug_ctx(), cli,
628 cmd_entry->interface),
629 nt_errstr(ntresult) ));
630 return ntresult;
631 }
632
633 if (ndr_syntax_id_equal(cmd_entry->interface,
634 &ndr_table_netlogon.syntax_id)) {
635 uint32_t neg_flags = NETLOGON_NEG_AUTH2_ADS_FLAGS;
636 uint32 sec_channel_type;
637 uchar trust_password[16];
638
639 if (!secrets_fetch_trust_account_password(lp_workgroup(),
640 trust_password,
641 NULL, &sec_channel_type)) {
642 return NT_STATUS_UNSUCCESSFUL;
643 }
644
645 ntresult = rpccli_netlogon_setup_creds(cmd_entry->rpc_pipe,
646 cli->desthost, /* server name */
647 lp_workgroup(), /* domain */
648 global_myname(), /* client name */
649 global_myname(), /* machine account name */
650 trust_password,
651 sec_channel_type,
652 &neg_flags);
653
654 if (!NT_STATUS_IS_OK(ntresult)) {
655 DEBUG(0, ("Could not initialise credentials for %s.\n",
656 cli_get_pipe_name_from_iface(
657 debug_ctx(), cli,
658 cmd_entry->interface)));
659 return ntresult;
660 }
661 }
662 }
663
664 /* Run command */
665
666 if ( cmd_entry->returntype == RPC_RTYPE_NTSTATUS ) {
667 ntresult = cmd_entry->ntfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
668 if (!NT_STATUS_IS_OK(ntresult)) {
669 printf("result was %s\n", nt_errstr(ntresult));
670 }
671 } else {
672 wresult = cmd_entry->wfn(cmd_entry->rpc_pipe, mem_ctx, argc, (const char **) argv);
673 /* print out the DOS error */
674 if (!W_ERROR_IS_OK(wresult)) {
675 printf( "result was %s\n", dos_errstr(wresult));
676 }
677 ntresult = W_ERROR_IS_OK(wresult)?NT_STATUS_OK:NT_STATUS_UNSUCCESSFUL;
678 }
679
680 /* Cleanup */
681
682 talloc_destroy(mem_ctx);
683
684 return ntresult;
685}
686
687
688/**
689 * Process a command entered at the prompt or as part of -c
690 *
691 * @returns The NTSTATUS from running the command.
692 **/
693static NTSTATUS process_cmd(struct cli_state *cli, char *cmd)
694{
695 struct cmd_list *temp_list;
696 NTSTATUS result = NT_STATUS_OK;
697 int ret;
698 int argc;
699 char **argv = NULL;
700
701 if ((ret = poptParseArgvString(cmd, &argc, (const char ***) &argv)) != 0) {
702 fprintf(stderr, "rpcclient: %s\n", poptStrerror(ret));
703 return NT_STATUS_UNSUCCESSFUL;
704 }
705
706
707 /* Walk through a dlist of arrays of commands. */
708 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
709 struct cmd_set *temp_set = temp_list->cmd_set;
710
711 while (temp_set->name) {
712 if (strequal(argv[0], temp_set->name)) {
713 if (!(temp_set->returntype == RPC_RTYPE_NTSTATUS && temp_set->ntfn ) &&
714 !(temp_set->returntype == RPC_RTYPE_WERROR && temp_set->wfn )) {
715 fprintf (stderr, "Invalid command\n");
716 goto out_free;
717 }
718
719 result = do_cmd(cli, temp_set, argc, argv);
720
721 goto out_free;
722 }
723 temp_set++;
724 }
725 }
726
727 if (argv[0]) {
728 printf("command not found: %s\n", argv[0]);
729 }
730
731out_free:
732/* moved to do_cmd()
733 if (!NT_STATUS_IS_OK(result)) {
734 printf("result was %s\n", nt_errstr(result));
735 }
736*/
737
738 /* NOTE: popt allocates the whole argv, including the
739 * strings, as a single block. So a single free is
740 * enough to release it -- we don't free the
741 * individual strings. rtfm. */
742 free(argv);
743
744 return result;
745}
746
747
748/* Main function */
749
750 int main(int argc, char *argv[])
751{
752 int opt;
753 static char *cmdstr = NULL;
754 const char *server;
755 struct cli_state *cli = NULL;
756 static char *opt_ipaddr=NULL;
757 struct cmd_set **cmd_set;
758 struct sockaddr_storage server_ss;
759 NTSTATUS nt_status;
760 static int opt_port = 0;
761 fstring new_workgroup;
762 int result = 0;
763 TALLOC_CTX *frame = talloc_stackframe();
764 uint32_t flags = 0;
765
766 /* make sure the vars that get altered (4th field) are in
767 a fixed location or certain compilers complain */
768 poptContext pc;
769 struct poptOption long_options[] = {
770 POPT_AUTOHELP
771 {"command", 'c', POPT_ARG_STRING, &cmdstr, 'c', "Execute semicolon separated cmds", "COMMANDS"},
772 {"dest-ip", 'I', POPT_ARG_STRING, &opt_ipaddr, 'I', "Specify destination IP address", "IP"},
773 {"port", 'p', POPT_ARG_INT, &opt_port, 'p', "Specify port number", "PORT"},
774 POPT_COMMON_SAMBA
775 POPT_COMMON_CONNECTION
776 POPT_COMMON_CREDENTIALS
777 POPT_TABLEEND
778 };
779
780 load_case_tables();
781
782 zero_sockaddr(&server_ss);
783
784 setlinebuf(stdout);
785
786 /* the following functions are part of the Samba debugging
787 facilities. See lib/debug.c */
788 setup_logging("rpcclient", True);
789
790 /* Parse options */
791
792 pc = poptGetContext("rpcclient", argc, (const char **) argv,
793 long_options, 0);
794
795 if (argc == 1) {
796 poptPrintHelp(pc, stderr, 0);
797 goto done;
798 }
799
800 while((opt = poptGetNextOpt(pc)) != -1) {
801 switch (opt) {
802
803 case 'I':
804 if (!interpret_string_addr(&server_ss,
805 opt_ipaddr,
806 AI_NUMERICHOST)) {
807 fprintf(stderr, "%s not a valid IP address\n",
808 opt_ipaddr);
809 result = 1;
810 goto done;
811 }
812 }
813 }
814
815 /* Get server as remaining unparsed argument. Print usage if more
816 than one unparsed argument is present. */
817
818 server = poptGetArg(pc);
819
820 if (!server || poptGetArg(pc)) {
821 poptPrintHelp(pc, stderr, 0);
822 result = 1;
823 goto done;
824 }
825
826 poptFreeContext(pc);
827
828 load_interfaces();
829
830 if (!init_names()) {
831 result = 1;
832 goto done;
833 }
834
835 /* save the workgroup...
836
837 FIXME!! do we need to do this for other options as well
838 (or maybe a generic way to keep lp_load() from overwriting
839 everything)? */
840
841 fstrcpy( new_workgroup, lp_workgroup() );
842
843 /* Load smb.conf file */
844
845 if (!lp_load(get_dyn_CONFIGFILE(),True,False,False,True))
846 fprintf(stderr, "Can't load %s\n", get_dyn_CONFIGFILE());
847
848 if ( strlen(new_workgroup) != 0 )
849 set_global_myworkgroup( new_workgroup );
850
851 /*
852 * Get password
853 * from stdin if necessary
854 */
855
856 if (get_cmdline_auth_info_use_machine_account() &&
857 !set_cmdline_auth_info_machine_account_creds()) {
858 result = 1;
859 goto done;
860 }
861
862 if (!get_cmdline_auth_info_got_pass()) {
863 char *pass = getpass("Password:");
864 if (pass) {
865 set_cmdline_auth_info_password(pass);
866 }
867 }
868
869 if ((server[0] == '/' && server[1] == '/') ||
870 (server[0] == '\\' && server[1] == '\\')) {
871 server += 2;
872 }
873
874 if (get_cmdline_auth_info_use_kerberos()) {
875 flags |= CLI_FULL_CONNECTION_USE_KERBEROS |
876 CLI_FULL_CONNECTION_FALLBACK_AFTER_KERBEROS;
877 }
878
879
880 nt_status = cli_full_connection(&cli, global_myname(), server,
881 opt_ipaddr ? &server_ss : NULL, opt_port,
882 "IPC$", "IPC",
883 get_cmdline_auth_info_username(),
884 lp_workgroup(),
885 get_cmdline_auth_info_password(),
886 flags,
887 get_cmdline_auth_info_signing_state(),NULL);
888
889 if (!NT_STATUS_IS_OK(nt_status)) {
890 DEBUG(0,("Cannot connect to server. Error was %s\n", nt_errstr(nt_status)));
891 result = 1;
892 goto done;
893 }
894
895 if (get_cmdline_auth_info_smb_encrypt()) {
896 nt_status = cli_cm_force_encryption(cli,
897 get_cmdline_auth_info_username(),
898 get_cmdline_auth_info_password(),
899 lp_workgroup(),
900 "IPC$");
901 if (!NT_STATUS_IS_OK(nt_status)) {
902 result = 1;
903 goto done;
904 }
905 }
906
907#if 0 /* COMMENT OUT FOR TESTING */
908 memset(cmdline_auth_info.password,'X',sizeof(cmdline_auth_info.password));
909#endif
910
911 /* Load command lists */
912
913 timeout = cli_set_timeout(cli, 10000);
914
915 cmd_set = rpcclient_command_list;
916
917 while(*cmd_set) {
918 add_command_set(*cmd_set);
919 add_command_set(separator_command);
920 cmd_set++;
921 }
922
923 fetch_machine_sid(cli);
924
925 /* Do anything specified with -c */
926 if (cmdstr && cmdstr[0]) {
927 char *cmd;
928 char *p = cmdstr;
929
930 result = 0;
931
932 while((cmd=next_command(&p)) != NULL) {
933 NTSTATUS cmd_result = process_cmd(cli, cmd);
934 SAFE_FREE(cmd);
935 result = NT_STATUS_IS_ERR(cmd_result);
936 }
937
938 goto done;
939 }
940
941 /* Loop around accepting commands */
942
943 while(1) {
944 char *line = NULL;
945
946 line = smb_readline("rpcclient $> ", NULL, completion_fn);
947
948 if (line == NULL)
949 break;
950
951 if (line[0] != '\n')
952 process_cmd(cli, line);
953 SAFE_FREE(line);
954 }
955
956done:
957 if (cli != NULL) {
958 cli_shutdown(cli);
959 }
960 TALLOC_FREE(frame);
961 return result;
962}
Note: See TracBrowser for help on using the repository browser.