sys_select added one more argument (read, write selectors).
[kai/samba-autobuild/.git] / source3 / rpcclient / rpcclient.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    SMB client
5    Copyright (C) Andrew Tridgell 1994-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifdef SYSLOG
23 #undef SYSLOG
24 #endif
25
26 #include "includes.h"
27
28 #ifndef REGISTER
29 #define REGISTER 0
30 #endif
31
32 extern pstring debugf;
33 extern pstring scope;
34 extern pstring global_myname;
35
36 extern pstring user_socket_options;
37
38
39 extern int DEBUGLEVEL;
40
41
42 #define CNV_LANG(s) dos2unix_format(s,False)
43 #define CNV_INPUT(s) unix2dos_format(s,True)
44
45 static int process_tok(fstring tok);
46 static void cmd_help(struct client_info *info, int argc, char *argv[]);
47 static void cmd_quit(struct client_info *info, int argc, char *argv[]);
48 static void cmd_set (struct client_info *info, int argc, char *argv[]);
49 static void cmd_net (struct client_info *info, int argc, char *argv[]);
50
51 static struct user_credentials usr;
52
53 static struct client_info cli_info;
54
55 static char  **cmd_argv = NULL;
56 static uint32 cmd_argc = 0;
57
58 FILE *out_hnd;
59
60 #define COMPL_NONE 0
61 #define COMPL_REGKEY 1
62 #define COMPL_SAMUSR 3
63 #define COMPL_SAMGRP 4
64 #define COMPL_SAMALS 5
65 #define COMPL_SVCLST 6
66 #define COMPL_PRTLST 7
67
68 /****************************************************************************
69  This defines the commands supported by this client
70  ****************************************************************************/
71 struct command_set commands[] = 
72 {
73         /*
74          * eventlog
75          */
76
77         {
78                 "eventlog",
79                 cmd_eventlog,
80                 "list the events",
81                 {COMPL_NONE, COMPL_NONE}
82         },
83
84         /*
85          * service control
86          */
87
88         {
89                 "svcenum",
90                 cmd_svc_enum,
91                 "[-i] Lists Services Manager",
92                 {COMPL_NONE, COMPL_NONE}
93         },
94
95         {
96                 "svcinfo",
97                 cmd_svc_info,
98                 "<service> Service Information",
99                 {COMPL_SVCLST, COMPL_NONE}
100         },
101
102         {
103                 "svcstart",
104                 cmd_svc_start,
105                 "<service> [arg 0] [arg 1] ... Start Service",
106                 {COMPL_SVCLST, COMPL_NONE}
107         },
108
109         {
110                 "svcset",
111                 cmd_svc_set,
112                 "<service> Test Set Service",
113                 {COMPL_SVCLST, COMPL_NONE}
114         },
115
116         {
117                 "svcstop",
118                 cmd_svc_stop,
119                 "<service> Stop Service",
120                 {COMPL_SVCLST, COMPL_NONE}
121         },
122
123         /*
124          * scheduler
125          */
126
127         {
128                 "at",
129                 cmd_at,
130                 "Scheduler control (at /? for syntax)",
131                 {COMPL_NONE, COMPL_NONE}
132         },
133
134         /*
135          * registry
136          */
137
138         {
139                 "regenum",
140                 cmd_reg_enum,
141                 "<keyname> Registry Enumeration (keys, values)",
142                 {COMPL_REGKEY, COMPL_NONE}
143         },
144         {
145                 "regdeletekey",
146                 cmd_reg_delete_key,
147                 "<keyname> Registry Key Delete",
148                 {COMPL_REGKEY, COMPL_NONE}
149         },
150         {
151                 "regcreatekey",
152                 cmd_reg_create_key,
153                 "<keyname> [keyclass] Registry Key Create",
154                 {COMPL_REGKEY, COMPL_NONE}
155         },
156         {
157                 "shutdown",
158                 cmd_reg_shutdown,
159                 "[-m message] [-t timeout] [-r or --reboot] [-f or --force-close] Remote Shutdown",
160                 {COMPL_NONE, COMPL_NONE}
161         },
162         {
163                 "regqueryval",
164                 cmd_reg_query_info,
165                 "<valname> Registry Value Query",
166                 {COMPL_REGKEY, COMPL_NONE}
167         },
168         {
169                 "regquerykey",
170                 cmd_reg_query_key,
171                 "<keyname> Registry Key Query",
172                 {COMPL_REGKEY, COMPL_NONE}
173         },
174         {
175                 "regdeleteval",
176                 cmd_reg_delete_val,
177                 "<valname> Registry Value Delete",
178                 {COMPL_REGKEY, COMPL_REGKEY}
179         },
180         {
181                 "regcreateval",
182                 cmd_reg_create_val,
183                 "<valname> <valtype> <value> Registry Key Create",
184                 {COMPL_REGKEY, COMPL_NONE}
185         },
186         {
187                 "reggetsec",
188                 cmd_reg_get_key_sec,
189                 "<keyname> Registry Key Security",
190                 {COMPL_REGKEY, COMPL_NONE}
191         },
192         {
193                 "regtestsec",
194                 cmd_reg_test_key_sec,
195                 "<keyname> Test Registry Key Security",
196                 {COMPL_REGKEY, COMPL_NONE}
197         },
198
199         /*
200          * printer testing
201          */
202
203         {
204                 "spoolenum",
205                 cmd_spoolss_enum_printers,
206                 "Enumerate Printers",
207                 {COMPL_NONE, COMPL_NONE}
208         },
209         {
210                 "spooljobs",
211                 cmd_spoolss_enum_jobs,
212                 "<printer name> Enumerate Printer Jobs",
213                 {COMPL_PRTLST, COMPL_NONE}
214         },
215         {
216                 "spoolopen",
217                 cmd_spoolss_open_printer_ex,
218                 "<printer name> Spool Printer Open Test",
219                 {COMPL_PRTLST, COMPL_NONE}
220         },
221         /*
222          * server
223          */
224         {
225                 "time",
226                 cmd_time,
227                 "Display remote time",
228                 {COMPL_NONE, COMPL_NONE}
229         },
230         {
231                 "brsinfo",
232                 cmd_brs_query_info,
233                 "Browser Query Info",
234                 {COMPL_NONE, COMPL_NONE}
235         },
236         {
237                 "wksinfo",
238                 cmd_wks_query_info,
239                 "Workstation Query Info",
240                 {COMPL_NONE, COMPL_NONE}
241         },
242         {
243                 "srvinfo",
244                 cmd_srv_query_info,
245                 "Server Query Info",
246                 {COMPL_NONE, COMPL_NONE}
247         },
248         {
249                 "srvsessions",
250                 cmd_srv_enum_sess,
251                 "List sessions on a server",
252                 {COMPL_NONE, COMPL_NONE}
253         },
254         {
255                 "srvshares",
256                 cmd_srv_enum_shares,
257                 "List shares on a server",
258                 {COMPL_NONE, COMPL_NONE}
259         },
260         {
261                 "srvtransports",
262                 cmd_srv_enum_tprt,
263                 "List transports on a server",
264                 {COMPL_NONE, COMPL_NONE}
265         },
266         {
267                 "srvconnections",
268                 cmd_srv_enum_conn,
269                 "List connections on a server",
270                 {COMPL_NONE, COMPL_NONE}
271         },
272         {
273                 "srvfiles",
274                 cmd_srv_enum_files,
275                 "List files on a server",
276                 {COMPL_NONE, COMPL_NONE}
277         },
278
279         /*
280          * lsa
281          */
282
283         {
284                 "lsaquery",
285                 cmd_lsa_query_info,
286                 "Query Info Policy (domain member or server)",
287                 {COMPL_NONE, COMPL_NONE}
288         },
289         {
290                 "lsaenumdomains",
291                 cmd_lsa_enum_trust_dom,
292                 "Enumerate Trusted Domains",
293                 {COMPL_NONE, COMPL_NONE}
294         },
295         {
296                 "lookupsids",
297                 cmd_lsa_lookup_sids,
298                 "Resolve names from SIDs",
299                 {COMPL_NONE, COMPL_NONE}
300         },
301         {
302                 "lookupnames",
303                 cmd_lsa_lookup_names,
304                 "Resolve SIDs from names",
305                 {COMPL_NONE, COMPL_NONE}
306         },
307         {
308                 "querysecret",
309                 cmd_lsa_query_secret,
310                 "LSA Query Secret (developer use)",
311                 {COMPL_NONE, COMPL_NONE}
312         },
313
314         /*
315          * netlogon
316          */
317
318         {
319                 "ntlogin",
320                 cmd_netlogon_login_test,
321                 "[username] [password] NT Domain login test",
322                 {COMPL_NONE, COMPL_NONE}
323         },
324         {
325                 "domtrust",
326                 cmd_netlogon_domain_test,
327                 "<domain> NT Inter-Domain test",
328                 {COMPL_NONE, COMPL_NONE}
329         },
330
331         /*
332          * sam
333          */
334
335         {
336                 "lookupdomain",
337                 cmd_sam_lookup_domain,
338                 "Obtain SID for a local domain",
339                 {COMPL_NONE, COMPL_NONE}
340         },
341         {
342                 "enumusers",
343                 cmd_sam_enum_users,
344                 "SAM User Database Query (experimental!)",
345                 {COMPL_NONE, COMPL_NONE}
346         },
347         {
348                 "addgroupmem",
349                 cmd_sam_add_groupmem,
350                 "<group rid> [user] [user] ... SAM Add Domain Group Member",
351                 {COMPL_SAMGRP, COMPL_SAMUSR}
352         },
353
354         {
355                 "addaliasmem",
356                 cmd_sam_add_aliasmem,
357                 "<alias rid> [member sid1] [member sid2] ... SAM Add Domain Alias Member",
358                 {COMPL_SAMALS, COMPL_NONE}
359         },
360         {
361                 "delgroupmem",
362                 cmd_sam_del_groupmem,
363                 "<group rid> [user] [user] ... SAM Delete Domain Group Member",
364                 {COMPL_SAMGRP, COMPL_SAMUSR}
365         },
366         {
367                 "delaliasmem",
368                 cmd_sam_del_aliasmem,
369                 "<alias rid> [member sid1] [member sid2] ... SAM Delete Domain Alias Member",
370                 {COMPL_SAMALS, COMPL_NONE}
371         },
372         {
373                 "creategroup",
374                 cmd_sam_create_dom_group,
375                 "SAM Create Domain Group",
376                 {COMPL_NONE, COMPL_NONE}
377         },
378         {
379                 "createalias",
380                 cmd_sam_create_dom_alias,
381                 "SAM Create Domain Alias",
382                 {COMPL_NONE, COMPL_NONE}
383         },
384         {
385                 "createuser",
386                 cmd_sam_create_dom_user,
387                 "<username> SAM Create Domain User",
388                 {COMPL_NONE, COMPL_NONE}
389         },
390         {
391                 "delgroup",
392                 cmd_sam_delete_dom_group,
393                 "SAM Delete Domain Group",
394                 {COMPL_SAMGRP, COMPL_NONE}
395         },
396         {
397                 "delalias",
398                 cmd_sam_delete_dom_alias,
399                 "SAM Delete Domain Alias",
400                 {COMPL_SAMALS, COMPL_NONE}
401         },
402         {
403                 "ntpass",
404                 cmd_sam_ntchange_pwd,
405                 "NT SAM Password Change",
406                 {COMPL_NONE, COMPL_NONE}
407         },
408         {
409                 "samuserset2",
410                 cmd_sam_set_userinfo2,
411                 "<username> [-s acb_bits] SAM User Set Info 2 (experimental!)",
412                 {COMPL_SAMUSR, COMPL_NONE}
413         },
414         {
415                 "samuserset",
416                 cmd_sam_set_userinfo,
417                 "<username> [-p password] SAM User Set Info (experimental!)",
418                 {COMPL_SAMUSR, COMPL_NONE}
419         },
420         {
421                 "samuser",
422                 cmd_sam_query_user,
423                 "<username> [-g] [-u] [-a] SAM User Query (experimental!)",
424                 {COMPL_SAMUSR, COMPL_NONE}
425         },
426         {
427                 "samgroup",
428                 cmd_sam_query_group,
429                 "<groupname> SAM Group Query (experimental!)",
430                 {COMPL_SAMGRP, COMPL_NONE}
431         },
432         {
433                 "samalias",
434                 cmd_sam_query_alias,
435                 "<aliasname> SAM Alias Query",
436                 {COMPL_SAMALS, COMPL_NONE}
437         },
438         {
439                 "samaliasmem",
440                 cmd_sam_query_aliasmem,
441                 "<aliasname> SAM Alias Members",
442                 {COMPL_SAMALS, COMPL_NONE}
443         },
444         {
445                 "samgroupmem",
446                 cmd_sam_query_groupmem,
447                 "SAM Group Members",
448                 {COMPL_SAMGRP, COMPL_NONE}
449         },
450         {
451                 "samtest",
452                 cmd_sam_test      ,
453                 "SAM User Encrypted RPC test (experimental!)",
454                 {COMPL_NONE, COMPL_NONE}
455         },
456         {
457                 "enumaliases",
458                 cmd_sam_enum_aliases,
459                 "SAM Aliases Database Query (experimental!)",
460                 {COMPL_NONE, COMPL_NONE}
461         },
462         {
463                 "enumdomains",
464                 cmd_sam_enum_domains,
465                 "SAM Domains Database Query (experimental!)",
466                 {COMPL_NONE, COMPL_NONE}
467         },
468         {
469                 "enumgroups",
470                 cmd_sam_enum_groups,
471                 "SAM Group Database Query (experimental!)",
472                 {COMPL_NONE, COMPL_NONE}
473         },
474         {
475                 "dominfo",
476                 cmd_sam_query_dominfo,
477                 "SAM Query Domain Info",
478                 {COMPL_NONE, COMPL_NONE}
479         },
480         {
481                 "dispinfo",
482                 cmd_sam_query_dispinfo,
483                 "SAM Query Display Info",
484                 {COMPL_NONE, COMPL_NONE}
485         },
486         {
487                 "samsync",
488                 cmd_sam_sync,
489                 "SAM Synchronization Test (experimental)",
490                 {COMPL_NONE, COMPL_NONE}
491         },
492
493         /* maintenance */
494
495         {
496                 "rpcclient",
497                 cmd_set,
498                 "run rpcclient inside rpcclient (change options etc.)",
499                 {COMPL_NONE, COMPL_NONE}
500         },
501
502         {
503                 "net",
504                 cmd_net,
505                 "net use and net view",
506                 {COMPL_NONE, COMPL_NONE}
507         },
508         /*
509          * bye bye
510          */
511
512         {
513                 "quit",
514                 cmd_quit,
515                 "logoff the server",
516                 {COMPL_NONE, COMPL_NONE}
517         },
518         {
519                 "q",
520                 cmd_quit,
521                 "logoff the server",
522                 {COMPL_NONE, COMPL_NONE}
523         },
524         {
525                 "exit",
526                 cmd_quit,
527                 "logoff the server",
528                 {COMPL_NONE, COMPL_NONE}
529         },
530         {
531                 "bye",
532                 cmd_quit,
533                 "logoff the server",
534                 {COMPL_NONE, COMPL_NONE}
535         },
536
537         /*
538          * eek!
539          */
540
541         {
542                 "help",
543                 cmd_help,
544                 "[command] give help on a command",
545                 {COMPL_NONE, COMPL_NONE}
546         },
547         {
548                 "?",
549                 cmd_help,
550                 "[command] give help on a command",
551                 {COMPL_NONE, COMPL_NONE}
552         },
553
554         /*
555          * shell
556          */
557
558         {
559                 "!",
560                 NULL,
561                 "run a shell command on the local system",
562                 {COMPL_NONE, COMPL_NONE}
563         },
564
565         /*
566          * oop!
567          */
568
569         {
570                 "",
571                 NULL,
572                 NULL,
573                 {COMPL_NONE, COMPL_NONE}
574         }
575 };
576
577
578 /****************************************************************************
579 do a (presumably graceful) quit...
580 ****************************************************************************/
581 static void cmd_quit(struct client_info *info, int argc, char *argv[])
582 {
583 #ifdef MEM_MAN
584         {
585                 extern FILE* dbf;
586                 smb_mem_write_status(dbf);
587                 smb_mem_write_errors(dbf);
588                 smb_mem_write_verbose(dbf);
589                 dbgflush();
590         }
591 #endif
592         free_connections();
593         exit(0);
594 }
595
596 /****************************************************************************
597 help
598 ****************************************************************************/
599 static void cmd_help(struct client_info *info, int argc, char *argv[])
600 {
601   int i=0,j;
602
603   if (argc > 1)
604     {
605       if ((i = process_tok(argv[1])) >= 0)
606         fprintf(out_hnd, "HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);                    
607     }
608   else
609     while (commands[i].description)
610       {
611         for (j=0; commands[i].description && (j<5); j++) {
612           fprintf(out_hnd, "%-15s",commands[i].name);
613           i++;
614         }
615         fprintf(out_hnd, "\n");
616       }
617 }
618
619 /*******************************************************************
620   lookup a command string in the list of commands, including 
621   abbreviations
622   ******************************************************************/
623 static int process_tok(char *tok)
624 {
625   int i = 0, matches = 0;
626   int cmd=0;
627   int tok_len = strlen(tok);
628   
629   while (commands[i].fn != NULL)
630     {
631       if (strequal(commands[i].name,tok))
632         {
633           matches = 1;
634           cmd = i;
635           break;
636         }
637       else if (strnequal(commands[i].name, tok, tok_len))
638         {
639           matches++;
640           cmd = i;
641         }
642       i++;
643     }
644   
645   if (matches == 0)
646     return(-1);
647   else if (matches == 1)
648     return(cmd);
649   else
650     return(-2);
651 }
652
653 /****************************************************************************
654   turn command line into command argument array
655 ****************************************************************************/
656 static BOOL get_cmd_args(char *line)
657 {
658         char *ptr = line;
659         pstring tok;
660         cmd_argc = 0;
661         cmd_argv = NULL;
662
663         /* get the first part of the command */
664         if (!next_token(&ptr,tok,NULL, sizeof(tok)))
665         {
666                 return False;
667         }
668
669         do
670         {
671                 add_chars_to_array(&cmd_argc, &cmd_argv, tok);
672
673         } while (next_token(NULL, tok, NULL, sizeof(tok)));
674
675         return True;
676 }
677
678 /* command options mask */
679 static uint32 cmd_set_options = 0xffffffff;
680
681 /****************************************************************************
682   process commands from the client
683 ****************************************************************************/
684 static BOOL do_command(struct client_info *info, char *line)
685 {
686         int i;
687
688         if (!get_cmd_args(line)) return False;
689
690         if (cmd_argc == 0)
691         {
692                 return False;
693         }
694
695         cmd_set_options = 0x0;
696
697         if ((i = process_tok(cmd_argv[0])) >= 0)
698         {
699                 int argc = (int)cmd_argc;
700                 char **argv = cmd_argv;
701                 optind = 0;
702
703                 commands[i].fn(info, argc, argv);
704         }
705         else if (i == -2)
706         {
707                 fprintf(out_hnd, "%s: command abbreviation ambiguous\n",
708                                  CNV_LANG(cmd_argv[0]));
709         }
710         else
711         {
712                 fprintf(out_hnd, "%s: command not found\n",
713                                  CNV_LANG(cmd_argv[0]));
714         }
715
716         free_char_array(cmd_argc, cmd_argv);
717
718         return True;
719 }
720
721 #ifndef HAVE_LIBREADLINE
722 /****************************************************************************
723 wait for keyboard activity, swallowing network packets
724 ****************************************************************************/
725 static void wait_keyboard(struct cli_state *cli)
726 {
727         fd_set fds;
728         struct timeval timeout;
729   
730         while (1) {
731                 FD_ZERO(&fds);
732                 FD_SET(cli->fd,&fds);
733                 FD_SET(fileno(stdin),&fds);
734
735                 timeout.tv_sec = 20;
736                 timeout.tv_usec = 0;
737                 sys_select(MAX(cli->fd,fileno(stdin))+1,NULL, &fds,&timeout);
738       
739                 if (FD_ISSET(fileno(stdin),&fds))
740                         return;
741
742                 /* We deliberately use receive_smb instead of
743                    client_receive_smb as we want to receive
744                    session keepalives and then drop them here.
745                 */
746                 if (FD_ISSET(cli->fd,&fds))
747                         receive_smb(cli->fd,cli->inbuf,0);
748         }  
749 }
750 #endif
751
752 /****************************************************************************
753   process commands from the client
754 ****************************************************************************/
755 static BOOL process( struct client_info *info, char *cmd_str)
756 {
757         pstring line;
758         char *cmd = cmd_str;
759
760         if (cmd != NULL)
761         {
762                 while (cmd[0] != '\0')
763                 {
764                         char *p;
765
766                         if ((p = strchr(cmd, ';')) == 0)
767                         {
768                                 strncpy(line, cmd, 999);
769                                 line[1000] = '\0';
770                                 cmd += strlen(cmd);
771                         }
772                         else
773                         {
774                                 if (p - cmd > 999) p = cmd + 999;
775                                 strncpy(line, cmd, p - cmd);
776                                 line[p - cmd] = '\0';
777                                 cmd = p + 1;
778                         }
779
780                         /* input language code to internal one */
781                         CNV_INPUT (line);
782
783                         if (!do_command(info, line)) continue;
784                 }
785         }
786         else while (!feof(stdin))
787         {
788 #ifdef HAVE_LIBREADLINE
789                 pstring promptline;
790 #endif
791
792 #ifndef HAVE_LIBREADLINE
793
794                 /* display a prompt */
795                 fprintf(out_hnd, "%s$ ", CNV_LANG(cli_info.dest_host));
796                 fflush(out_hnd);
797
798 #ifdef CLIX
799                 line[0] = wait_keyboard(NULL);
800                 /* this might not be such a good idea... */
801                 if ( line[0] == EOF)
802                 {
803                         break;
804                 }
805 #else
806                 wait_keyboard(NULL);
807 #endif
808
809                 /* and get a response */
810 #ifdef CLIX
811                 fgets( &line[1],999, stdin);
812 #else
813                 if (!fgets(line,1000,stdin))
814                 {
815                         break;
816                 }
817 #endif
818
819 #else /* HAVE_LIBREADLINE */
820
821                 slprintf(promptline, sizeof(promptline) - 1, "%s$ ",
822                          CNV_LANG(cli_info.dest_host));
823
824                 if (!readline(promptline))
825                     break;
826
827                 /* Copy read line to samba buffer */
828
829                 pstrcpy(line, rl_line_buffer);
830
831                 /* Add to history */
832
833                 if (strlen(line) > 0) 
834                     add_history(line);
835 #endif
836                 /* input language code to internal one */
837                 CNV_INPUT (line);
838
839                 /* special case - first char is ! */
840                 if (*line == '!')
841                 {
842                         system(line + 1);
843                         continue;
844                 }
845
846                 fprintf(out_hnd, "%s\n", line);
847
848                 if (!do_command(info, line)) continue;
849         }
850
851         return(True);
852 }
853
854 /****************************************************************************
855 usage on the program
856 ****************************************************************************/
857 static void usage(char *pname)
858 {
859   fprintf(out_hnd, "Usage: %s [password] [-S server] [-U user] -[W domain] [-l log] ",
860            pname);
861
862   fprintf(out_hnd, "\nVersion %s\n",VERSION);
863   fprintf(out_hnd, "\t-d debuglevel         set the debuglevel\n");
864   fprintf(out_hnd, "\t-S server             connect to \\\\server\\IPC$ \n");
865   fprintf(out_hnd, "\t-l log basename.      Basename for log/debug files\n");
866   fprintf(out_hnd, "\t-n netbios name.      Use this name as my netbios name\n");
867   fprintf(out_hnd, "\t-N                    don't ask for a password\n");
868   fprintf(out_hnd, "\t-m max protocol       set the max protocol level\n");
869   fprintf(out_hnd, "\t-I dest IP            use this IP to connect to\n");
870   fprintf(out_hnd, "\t-E                    write messages to stderr instead of stdout\n");
871   fprintf(out_hnd, "\t-U username           set the network username\n");
872   fprintf(out_hnd, "\t-U username%%pass      set the network username and password\n");
873   fprintf(out_hnd, "\t-W domain             set the domain name\n");
874   fprintf(out_hnd, "\t-c 'command string'   execute semicolon separated commands\n");
875   fprintf(out_hnd, "\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
876   fprintf(out_hnd, "\n");
877 }
878
879 #ifdef HAVE_LIBREADLINE
880
881 /****************************************************************************
882 GNU readline completion functions
883 ****************************************************************************/
884
885 /* Complete a remote registry enum */
886
887 static uint32 reg_list_len = 0;
888 static char **reg_name = NULL;
889
890 static void reg_init(int val, const char *full_keyname, int num)
891 {
892         switch (val)
893         {
894                 case 0:
895                 {
896                         free_char_array(reg_list_len, reg_name);
897                         reg_list_len = 0;
898                         reg_name = NULL;
899                         break;
900                 }
901                 default:
902                 {
903                         break;
904                 }
905         }
906 }
907
908 static void reg_key_list(const char *full_name,
909                                 const char *name, time_t key_mod_time)
910 {
911         fstring key_name;
912         slprintf(key_name, sizeof(key_name)-1, "%s\\", name);
913         add_chars_to_array(&reg_list_len, &reg_name, key_name);
914 }
915
916 static void reg_val_list(const char *full_name,
917                                 const char* name,
918                                 uint32 type,
919                                 const BUFFER2 *value)
920 {
921         add_chars_to_array(&reg_list_len, &reg_name, name);
922 }
923
924 static char *complete_regenum(char *text, int state)
925 {
926         pstring full_keyname;
927         static uint32 i = 0;
928     
929         if (state == 0)
930         {
931                 fstring srv_name;
932                 fstrcpy(srv_name, "\\\\");
933                 fstrcat(srv_name, cli_info.dest_host);
934                 strupper(srv_name);
935
936                 if (cmd_argc >= 2 && cmd_argv != NULL && cmd_argv[1] != NULL)
937                 {
938                         char *sep;
939                         split_server_keyname(srv_name, full_keyname,
940                                              cmd_argv[1]);
941
942                         sep = strrchr(full_keyname, '\\');
943                         if (sep != NULL)
944                         {
945                                 *sep = 0;
946                         }
947                 }
948
949                 /* Iterate all keys / values */
950                 if (!msrpc_reg_enum_key(srv_name, full_keyname,
951                                    reg_init, reg_key_list, reg_val_list))
952                 {
953                         return NULL;
954                 }
955
956                 i = 0;
957         }
958
959         for (; i < reg_list_len; i++)
960         {
961                 if (text == NULL || text[0] == 0 ||
962                     strnequal(text, reg_name[i], strlen(text)))
963                 {
964                         char *name = strdup(reg_name[i]);
965                         i++;
966                         return name;
967                 }
968         }
969         
970         return NULL;
971 }
972
973
974 static char *complete_samenum_usr(char *text, int state)
975 {
976         static uint32 i = 0;
977         static uint32 num_usrs = 0;
978         static struct acct_info *sam = NULL;
979     
980         if (state == 0)
981         {
982                 fstring srv_name;
983                 fstring domain;
984                 fstring sid;
985                 DOM_SID sid1;
986                 sid_copy(&sid1, &cli_info.dom.level5_sid);
987                 sid_to_string(sid, &sid1);
988                 fstrcpy(domain, cli_info.dom.level5_dom);
989
990                 if (sid1.num_auths == 0)
991                 {
992                         return NULL;
993                 }
994
995                 fstrcpy(srv_name, "\\\\");
996                 fstrcat(srv_name, cli_info.dest_host);
997                 strupper(srv_name);
998
999                 free(sam);
1000                 sam = NULL;
1001                 num_usrs = 0;
1002
1003                 /* Iterate all users */
1004                 if (msrpc_sam_enum_users(srv_name, domain, &sid1, 
1005                                    &sam, &num_usrs,
1006                                    NULL, NULL, NULL, NULL) == 0)
1007                 {
1008                         return NULL;
1009                 }
1010
1011                 i = 0;
1012         }
1013
1014         for (; i < num_usrs; i++)
1015         {
1016                 char *usr_name = sam[i].acct_name;
1017                 if (text == NULL || text[0] == 0 ||
1018                     strnequal(text, usr_name, strlen(text)))
1019                 {
1020                         char *name = strdup(usr_name);
1021                         i++;
1022                         return name;
1023                 }
1024         }
1025         
1026         return NULL;
1027 }
1028
1029 static char *complete_samenum_als(char *text, int state)
1030 {
1031         static uint32 i = 0;
1032         static uint32 num_als = 0;
1033         static struct acct_info *sam = NULL;
1034     
1035         if (state == 0)
1036         {
1037                 fstring srv_name;
1038                 fstring domain;
1039                 fstring sid;
1040                 DOM_SID sid1;
1041                 sid_copy(&sid1, &cli_info.dom.level5_sid);
1042                 sid_to_string(sid, &sid1);
1043                 fstrcpy(domain, cli_info.dom.level5_dom);
1044
1045                 if (sid1.num_auths == 0)
1046                 {
1047                         return NULL;
1048                 }
1049
1050                 fstrcpy(srv_name, "\\\\");
1051                 fstrcat(srv_name, cli_info.dest_host);
1052                 strupper(srv_name);
1053
1054                 free(sam);
1055                 sam = NULL;
1056                 num_als = 0;
1057
1058                 /* Iterate all aliases */
1059                 if (msrpc_sam_enum_aliases(srv_name, domain, &sid1, 
1060                                    &sam, &num_als,
1061                                    NULL, NULL, NULL) == 0)
1062                 {
1063                         return NULL;
1064                 }
1065
1066                 i = 0;
1067         }
1068
1069         for (; i < num_als; i++)
1070         {
1071                 char *als_name = sam[i].acct_name;
1072                 if (text == NULL || text[0] == 0 ||
1073                     strnequal(text, als_name, strlen(text)))
1074                 {
1075                         char *name = strdup(als_name);
1076                         i++;
1077                         return name;
1078                 }
1079         }
1080         
1081         return NULL;
1082 }
1083
1084 static char *complete_samenum_grp(char *text, int state)
1085 {
1086         static uint32 i = 0;
1087         static uint32 num_grps = 0;
1088         static struct acct_info *sam = NULL;
1089     
1090         if (state == 0)
1091         {
1092                 fstring srv_name;
1093                 fstring domain;
1094                 fstring sid;
1095                 DOM_SID sid1;
1096                 sid_copy(&sid1, &cli_info.dom.level5_sid);
1097                 sid_to_string(sid, &sid1);
1098                 fstrcpy(domain, cli_info.dom.level5_dom);
1099
1100                 if (sid1.num_auths == 0)
1101                 {
1102                         return NULL;
1103                 }
1104
1105                 fstrcpy(srv_name, "\\\\");
1106                 fstrcat(srv_name, cli_info.dest_host);
1107                 strupper(srv_name);
1108
1109                 free(sam);
1110                 sam = NULL;
1111                 num_grps = 0;
1112
1113                 /* Iterate all groups */
1114                 if (msrpc_sam_enum_groups(srv_name,
1115                                    domain, &sid1, 
1116                                    &sam, &num_grps,
1117                                    NULL, NULL, NULL) == 0)
1118                 {
1119                         return NULL;
1120                 }
1121
1122                 i = 0;
1123         }
1124
1125         for (; i < num_grps; i++)
1126         {
1127                 char *grp_name = sam[i].acct_name;
1128                 if (text == NULL || text[0] == 0 ||
1129                     strnequal(text, grp_name, strlen(text)))
1130                 {
1131                         char *name = strdup(grp_name);
1132                         i++;
1133                         return name;
1134                 }
1135         }
1136         
1137         return NULL;
1138 }
1139
1140 static char *complete_svcenum(char *text, int state)
1141 {
1142         static uint32 i = 0;
1143         static uint32 num_svcs = 0;
1144         static ENUM_SRVC_STATUS *svc = NULL;
1145         fstring srv_name;
1146
1147         fstrcpy(srv_name, "\\\\");
1148         fstrcat(srv_name, cli_info.dest_host);
1149         strupper(srv_name);
1150
1151     
1152         if (state == 0)
1153         {
1154                 free(svc);
1155                 svc = NULL;
1156                 num_svcs = 0;
1157
1158                 /* Iterate all users */
1159                 if (msrpc_svc_enum(srv_name, &svc, &num_svcs,
1160                                    NULL, NULL) == 0)
1161                 {
1162                         return NULL;
1163                 }
1164
1165                 i = 0;
1166         }
1167
1168         for (; i < num_svcs; i++)
1169         {
1170                 fstring svc_name;
1171                 unistr_to_ascii(svc_name, svc[i].uni_srvc_name.buffer,
1172                         sizeof(svc_name)-1);
1173
1174                 if (text == NULL || text[0] == 0 ||
1175                     strnequal(text, svc_name, strlen(text)))
1176                 {
1177                         char *name = strdup(svc_name);
1178                         i++;
1179                         return name;
1180                 }
1181         }
1182         
1183         return NULL;
1184 }
1185
1186 static char *complete_printersenum(char *text, int state)
1187 {
1188         static uint32 i = 0;
1189         static uint32 num = 0;
1190         static PRINTER_INFO_1 **ctr = NULL;
1191     
1192         if (state == 0)
1193         {
1194                 fstring srv_name;
1195                 fstrcpy(srv_name, "\\\\");
1196                 fstrcat(srv_name, cli_info.dest_host);
1197                 strupper(srv_name);
1198
1199                 free_print1_array(num, ctr);
1200                 ctr = NULL;
1201                 num = 0;
1202
1203                 /* Iterate all users */
1204                 if (!msrpc_spoolss_enum_printers(srv_name,
1205                                    1, &num, (void***)&ctr,
1206                                    NULL))
1207                 {
1208                         return NULL;
1209                 }
1210
1211                 i = 0;
1212         }
1213
1214         for (; i < num; i++)
1215         {
1216                 fstring name;
1217                 unistr_to_ascii(name, ctr[i]->name.buffer,
1218                         sizeof(name)-1);
1219
1220                 if (text == NULL || text[0] == 0 ||
1221                     strnequal(text, name, strlen(text)))
1222                 {
1223                         char *copy = strdup(name);
1224                         i++;
1225                         return copy;
1226                 }
1227         }
1228         
1229         return NULL;
1230 }
1231
1232 /* Complete an rpcclient command */
1233
1234 static char *complete_cmd(char *text, int state)
1235 {
1236     static int cmd_index;
1237     char *name;
1238
1239     /* Initialise */
1240
1241     if (state == 0) {
1242         cmd_index = 0;
1243     }
1244
1245     /* Return the next name which partially matches the list of commands */
1246     
1247     while (strlen(name = commands[cmd_index++].name) > 0) {
1248         if (strncmp(name, text, strlen(text)) == 0) {
1249             return strdup(name);
1250         }
1251     }
1252     
1253     return NULL;
1254 }
1255
1256 /* Main completion function */
1257
1258 static char **completion_fn(char *text, int start, int end)
1259 {
1260         pstring cmd_partial;
1261         int cmd_index;
1262         int num_words;
1263
1264     int i;
1265     char lastch = ' ';
1266
1267         (void)get_cmd_args(rl_line_buffer);
1268
1269         safe_strcpy(cmd_partial, rl_line_buffer,
1270                     MAX(sizeof(cmd_partial),end)-1);
1271
1272     /* Complete rpcclient command */
1273
1274     if (start == 0)
1275         {
1276         return completion_matches(text, complete_cmd);
1277     }
1278
1279     /* Count # of words in command */
1280     
1281     num_words = 0;
1282     for (i = 0; i <= end; i++) {
1283         if ((rl_line_buffer[i] != ' ') && (lastch == ' '))
1284         {
1285                 num_words++;
1286         }
1287         lastch = rl_line_buffer[i];
1288     }
1289     
1290     if (rl_line_buffer[end] == ' ')
1291         num_words++;
1292
1293     /* Work out which command we are completing for */
1294
1295     for (cmd_index = 0; strcmp(commands[cmd_index].name, "") != 0; 
1296          cmd_index++) {
1297         
1298         /* Check each command in array */
1299         
1300         if (strncmp(rl_line_buffer, commands[cmd_index].name,
1301                     strlen(commands[cmd_index].name)) == 0) {
1302             
1303             /* Call appropriate completion function */
1304
1305       if (num_words == 2 || num_words == 3)
1306       {
1307         switch (commands[cmd_index].compl_args[num_words - 2])
1308         {
1309
1310         case COMPL_SAMGRP:
1311           return completion_matches(text, complete_samenum_grp);
1312
1313         case COMPL_SAMALS:
1314           return completion_matches(text, complete_samenum_als);
1315
1316         case COMPL_SAMUSR:
1317           return completion_matches(text, complete_samenum_usr);
1318
1319         case COMPL_SVCLST:
1320           return completion_matches(text, complete_svcenum);
1321
1322         case COMPL_PRTLST:
1323           return completion_matches(text, complete_printersenum);
1324
1325         case COMPL_REGKEY:
1326           return completion_matches(text, complete_regenum);
1327
1328         default:
1329             /* An invalid completion type */
1330             break;
1331         }
1332       }
1333         }
1334     }
1335
1336     /* Eeek! */
1337
1338     return NULL;
1339 }
1340
1341 /* To avoid filename completion being activated when no valid
1342    completions are found, we assign this stub completion function
1343    to the rl_completion_entry_function variable. */
1344
1345 static char *complete_cmd_null(char *text, int state)
1346 {
1347         return NULL;
1348 }
1349
1350 #endif /* HAVE_LIBREADLINE */
1351
1352 static void cmd_net(struct client_info *info, int argc, char *argv[])
1353 {
1354         int opt;
1355         BOOL net_use = False;
1356         BOOL net_use_del = False;
1357         BOOL net_use_add = False;
1358
1359         while ((opt = getopt(argc, argv, "udS:U:W:")) != EOF)
1360         {
1361                 switch (opt)
1362                 {
1363                         case 'u':
1364                         {
1365                                 net_use = True;
1366                                 break;
1367                         }
1368
1369                         default:
1370                         {
1371                                 report(out_hnd, "net -S \\server [-U user%%pass] [-W domain] [-d]\n");
1372                                 report(out_hnd, "net -u\n");
1373                                 break;
1374                         }
1375                 }
1376         }
1377
1378         if (net_use)
1379         {
1380                 int i;
1381                 uint32 num_uses;
1382                 struct use_info **use;
1383                 cli_net_use_enum(&num_uses, &use);
1384
1385                 if (num_uses == 0)
1386                 {
1387                         report(out_hnd, "No connections\n");
1388                 }
1389                 else
1390                 {
1391                         report(out_hnd, "Connections:\n");
1392
1393                         for (i = 0; i < num_uses; i++)
1394                         {
1395                                 if (use[i] != NULL && use[i]->connected)
1396                                 {
1397                                         report(out_hnd, "Server:\t%s\t",
1398                                                          use[i]->srv_name);
1399                                         report(out_hnd, "User:\t%s\t",
1400                                                          use[i]->user_name);
1401                                         report(out_hnd, "Domain:\t%s\n",
1402                                                          use[i]->domain);
1403                                 }
1404                         }
1405                 }
1406         }
1407 }
1408
1409 static void set_user_password(struct user_credentials *u,
1410                                 BOOL got_pass, char *password)
1411 {
1412         /* set the password cache info */
1413         if (got_pass)
1414         {
1415                 if (password == NULL)
1416                 {
1417                         pwd_set_nullpwd(&(u->pwd));
1418                 }
1419                 else
1420                 {
1421                         /* generate 16 byte hashes */
1422                         pwd_make_lm_nt_16(&(u->pwd), password);
1423                 }
1424         }
1425         else 
1426         {
1427                 pwd_read(&(u->pwd), "Enter Password:", True);
1428         }
1429 }
1430
1431 #define CMD_STR 0x1
1432 #define CMD_DBF 0x2
1433 #define CMD_SVC 0x4
1434 #define CMD_TERM 0x8
1435 #define CMD_PASS 0x10
1436 #define CMD_USER 0x20
1437 #define CMD_NOPW 0x40
1438 #define CMD_DBLV 0x80
1439 #define CMD_HELP 0x100
1440 #define CMD_SOCK 0x200
1441 #define CMD_IFACE 0x400
1442 #define CMD_DOM 0x800
1443 #define CMD_IP 0x1000
1444 #define CMD_HOST 0x2000
1445 #define CMD_NAME 0x4000
1446 #define CMD_DBG 0x8000
1447 #define CMD_SCOPE 0x10000
1448 #define CMD_INTER 0x20000
1449
1450 static void cmd_set(struct client_info *info, int argc, char *argv[])
1451 {
1452         BOOL interactive = True;
1453         char *cmd_str = NULL;
1454         int opt;
1455         extern FILE *dbf;
1456         extern char *optarg;
1457         static pstring servicesf = CONFIGFILE;
1458         pstring term_code;
1459         pstring password; /* local copy only, if one is entered */
1460
1461 #ifdef KANJI
1462         pstrcpy(term_code, KANJI);
1463 #else /* KANJI */
1464         *term_code = 0;
1465 #endif /* KANJI */
1466
1467         if (argc > 1 && *argv[1] != '-')
1468         {
1469                 if (argc > 1 && (*argv[1] != '-'))
1470                 {
1471                         cmd_set_options |= CMD_PASS;
1472                         pstrcpy(password,argv[1]);  
1473                         memset(argv[1],'X',strlen(argv[1]));
1474                         argc--;
1475                         argv++;
1476                 }
1477         }
1478
1479         while ((opt = getopt(argc, argv, "s:B:O:M:S:i:N:n:d:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
1480         {
1481                 switch (opt)
1482                 {
1483                         case 'm':
1484                         {
1485                                 /* FIXME ... max_protocol seems to be funny here */
1486
1487                                 int max_protocol = 0;
1488                                 max_protocol = interpret_protocol(optarg,max_protocol);
1489                                 fprintf(stderr, "max protocol not currently supported\n");
1490                                 break;
1491                         }
1492
1493                         case 'O':
1494                         {
1495                                 cmd_set_options |= CMD_SOCK;
1496                                 pstrcpy(user_socket_options,optarg);
1497                                 break;  
1498                         }
1499
1500                         case 'S':
1501                         {
1502                                 cmd_set_options |= CMD_HOST;
1503                                 pstrcpy(cli_info.dest_host,optarg);
1504                                 strupper(cli_info.dest_host);
1505                                 break;
1506                         }
1507
1508                         case 'B':
1509                         {
1510                                 cmd_set_options |= CMD_IFACE;
1511                                 iface_set_default(NULL,optarg,NULL);
1512                                 break;
1513                         }
1514
1515                         case 'i':
1516                         {
1517                                 cmd_set_options |= CMD_SCOPE;
1518                                 pstrcpy(scope, optarg);
1519                                 break;
1520                         }
1521
1522                         case 'U':
1523                         {
1524                                 char *lp;
1525                                 cmd_set_options |= CMD_USER;
1526                                 pstrcpy(usr.user_name,optarg);
1527                                 if ((lp=strchr(usr.user_name,'%')))
1528                                 {
1529                                         *lp = 0;
1530                                         pstrcpy(password,lp+1);
1531                                         cmd_set_options |= CMD_PASS;
1532                                         memset(strchr(optarg,'%')+1,'X',strlen(password));
1533                                 }
1534                                 if (usr.user_name[0] == 0 && password[0] == 0)
1535                                 {
1536                                         cmd_set_options |= CMD_NOPW;
1537                                 }
1538                                 break;
1539                         }
1540
1541                         case 'W':
1542                         {
1543                                 cmd_set_options |= CMD_DOM;
1544                                 pstrcpy(usr.domain,optarg);
1545                                 break;
1546                         }
1547
1548                         case 'E':
1549                         {
1550                                 cmd_set_options |= CMD_DBG;
1551                                 dbf = stderr;
1552                                 break;
1553                         }
1554
1555                         case 'I':
1556                         {
1557                                 cmd_set_options |= CMD_IP;
1558                                 cli_info.dest_ip = *interpret_addr2(optarg);
1559                                 if (zero_ip(cli_info.dest_ip))
1560                                 {
1561                                         free_connections();
1562                                         exit(1);
1563                                 }
1564                                 break;
1565                         }
1566
1567                         case 'n':
1568                         {
1569                                 cmd_set_options |= CMD_NAME;
1570                                 fstrcpy(global_myname, optarg);
1571                                 break;
1572                         }
1573
1574                         case 'N':
1575                         {
1576                                 cmd_set_options |= CMD_NOPW | CMD_PASS;
1577                                 break;
1578                         }
1579
1580                         case 'd':
1581                         {
1582                                 cmd_set_options |= CMD_DBLV;
1583                                 if (*optarg == 'A')
1584                                         DEBUGLEVEL = 10000;
1585                                 else
1586                                         DEBUGLEVEL = atoi(optarg);
1587                                 break;
1588                         }
1589
1590                         case 'l':
1591                         {
1592                                 cmd_set_options |= CMD_INTER;
1593                                 slprintf(debugf, sizeof(debugf)-1,
1594                                          "%s.client", optarg);
1595                                 interactive = False;
1596                                 break;
1597                         }
1598
1599                         case 'c':
1600                         {
1601                                 cmd_set_options |= CMD_STR | CMD_PASS;
1602                                 cmd_str = optarg;
1603                                 break;
1604                         }
1605
1606                         case 'h':
1607                         {
1608                                 cmd_set_options |= CMD_HELP;
1609                                 usage(argv[0]);
1610                                 break;
1611                         }
1612
1613                         case 's':
1614                         {
1615                                 cmd_set_options |= CMD_SVC;
1616                                 pstrcpy(servicesf, optarg);
1617                                 break;
1618                         }
1619
1620                         case 't':
1621                         {
1622                                 cmd_set_options |= CMD_TERM;
1623                                 pstrcpy(term_code, optarg);
1624                                 break;
1625                         }
1626
1627                         default:
1628                         {
1629                                 cmd_set_options |= CMD_HELP;
1630                                 usage(argv[0]);
1631                                 break;
1632                         }
1633                 }
1634         }
1635
1636         DEBUG(10,("cmd_set: options: %x\n", cmd_set_options));
1637
1638         if (IS_BITS_SET_ALL(cmd_set_options, CMD_HELP))
1639         {
1640                 return;
1641         }
1642
1643         if (IS_BITS_SET_ALL(cmd_set_options, CMD_INTER))
1644         {
1645                 setup_logging(debugf, interactive);
1646                 reopen_logs();
1647         }
1648
1649         if (IS_BITS_SET_ALL(cmd_set_options, CMD_NOPW))
1650         {
1651                 set_user_password(&usr, True, NULL);
1652         }
1653         else if (IS_BITS_SET_ALL(cmd_set_options, CMD_PASS))
1654         {
1655                 set_user_password(&usr, True, password);
1656         }
1657
1658         /* paranoia: destroy the local copy of the password */
1659         bzero(password, sizeof(password)); 
1660
1661         strupper(global_myname);
1662         fstrcpy(cli_info.myhostname, global_myname);
1663
1664         if (IS_BITS_SET_ALL(cmd_set_options, CMD_SVC))
1665         {
1666                 if (!lp_load(servicesf,True, False, False))
1667                 {
1668                         fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
1669                 }
1670
1671         }
1672
1673         if (IS_BITS_SET_ALL(cmd_set_options, CMD_INTER))
1674         {
1675                 load_interfaces();
1676         }
1677
1678         if (cmd_str != NULL)
1679         {
1680                 process(&cli_info, cmd_str);
1681         }
1682 }
1683
1684 static void read_user_env(struct user_credentials *u)
1685 {
1686         pstring password;
1687
1688         password[0] = 0;
1689
1690         if (getenv("USER"))
1691         {
1692                 char *p;
1693                 pstrcpy(u->user_name,getenv("USER"));
1694
1695                 /* modification to support userid%passwd syntax in the USER var
1696                 25.Aug.97, jdblair@uab.edu */
1697
1698                 if ((p=strchr(u->user_name,'%')))
1699                 {
1700                         *p = 0;
1701                         pstrcpy(password,p+1);
1702                         memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
1703                 }
1704                 strupper(u->user_name);
1705         }
1706
1707         /* modification to support PASSWD environmental var
1708            25.Aug.97, jdblair@uab.edu */
1709         if (getenv("PASSWD"))
1710         {
1711                 pstrcpy(password,getenv("PASSWD"));
1712         }
1713
1714         if (*u->user_name == 0 && getenv("LOGNAME"))
1715         {
1716                 pstrcpy(u->user_name,getenv("LOGNAME"));
1717                 strupper(u->user_name);
1718         }
1719
1720         set_user_password(u, True, password);
1721
1722         /* paranoia: destroy the local copy of the password */
1723         bzero(password, sizeof(password)); 
1724 }
1725
1726 void readline_init(void)
1727 {
1728 #ifdef HAVE_LIBREADLINE
1729
1730         /* Initialise GNU Readline */
1731         
1732         rl_readline_name = "rpcclient";
1733         rl_attempted_completion_function = completion_fn;
1734         rl_completion_entry_function = (Function *)complete_cmd_null;
1735         
1736         /* Initialise history list */
1737         
1738         using_history();
1739
1740 #else
1741         int x;
1742         x = 0; /* stop compiler warnings */
1743 #endif /* HAVE_LIBREADLINE */
1744 }
1745
1746 /****************************************************************************
1747   main program
1748 ****************************************************************************/
1749  int main(int argc,char *argv[])
1750 {
1751         extern struct user_credentials *usr_creds;
1752         mode_t myumask = 0755;
1753
1754         DEBUGLEVEL = 2;
1755
1756         usr.ntlmssp_flags = 0x0;
1757
1758         usr_creds = &usr;
1759         out_hnd = stdout;
1760         fstrcpy(debugf, argv[0]);
1761
1762         init_policy_hnd(64);
1763
1764         pstrcpy(usr.domain, "");
1765         pstrcpy(usr.user_name, "");
1766
1767         pstrcpy(cli_info.myhostname, "");
1768         pstrcpy(cli_info.dest_host, "");
1769         cli_info.dest_ip.s_addr = 0;
1770
1771         ZERO_STRUCT(cli_info.dom.level3_sid);
1772         ZERO_STRUCT(cli_info.dom.level5_sid);
1773         fstrcpy(cli_info.dom.level3_dom, "");
1774         fstrcpy(cli_info.dom.level5_dom, "");
1775
1776         readline_init();
1777         TimeInit();
1778         charset_initialise();
1779         init_connections();
1780
1781         myumask = umask(0);
1782         umask(myumask);
1783
1784         if (!get_myname(global_myname, NULL))
1785         {
1786                 fprintf(stderr, "Failed to get my hostname.\n");
1787         }
1788
1789         if (argc < 2)
1790         {
1791                 usage(argv[0]);
1792                 free_connections();
1793                 exit(1);
1794         }
1795
1796         read_user_env(&usr);
1797
1798         cmd_set_options &= ~CMD_HELP;
1799         cmd_set_options &= ~CMD_NOPW;
1800
1801         cmd_set(&cli_info, argc, argv);
1802
1803         if (IS_BITS_SET_ALL(cmd_set_options, CMD_HELP))
1804         {
1805                 free_connections();
1806                 exit(0);
1807         }
1808
1809         codepage_initialise(lp_client_code_page());
1810
1811         DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
1812
1813         process(&cli_info, NULL);
1814
1815         free_connections();
1816
1817         return(0);
1818 }