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