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