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