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