added wksinfo command to rpcclient, which don't work too good.
[samba.git] / source / 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 scope;
33
34 extern pstring user_socket_options;
35
36
37 extern pstring debugf;
38 extern int DEBUGLEVEL;
39
40
41 extern file_info def_finfo;
42
43 #define CNV_LANG(s) dos2unix_format(s,False)
44 #define CNV_INPUT(s) unix2dos_format(s,True)
45
46 static int process_tok(fstring tok);
47 static void cmd_help(struct client_info *info);
48 static void cmd_quit(struct client_info *info);
49
50 static struct cli_state smbcli;
51 struct cli_state *smb_cli = &smbcli;
52
53 FILE *out_hnd = stdout;
54
55 /****************************************************************************
56 initialise smb client structure
57 ****************************************************************************/
58 void rpcclient_init(void)
59 {
60         bzero(smb_cli, sizeof(smb_cli));
61         cli_initialise(smb_cli);
62 }
63
64 /****************************************************************************
65 make smb client connection
66 ****************************************************************************/
67 static BOOL rpcclient_connect(struct client_info *info)
68 {
69         struct nmb_name calling;
70         struct nmb_name called;
71
72         make_nmb_name(&called , dns_to_netbios_name(info->dest_host ), info->name_type, scope);
73         make_nmb_name(&calling, dns_to_netbios_name(info->myhostname), 0x0            , scope);
74
75         if (!cli_establish_connection(smb_cli, 
76                                   info->dest_host, &info->dest_ip, 
77                                   &calling, &called,
78                                   info->share, info->svc_type,
79                                   False, True))
80         {
81                 DEBUG(0,("rpcclient_connect: connection failed\n"));
82                 cli_shutdown(smb_cli);
83                 return False;
84         }
85
86         return True;
87 }
88
89 /****************************************************************************
90 stop the smb connection(s?)
91 ****************************************************************************/
92 static void rpcclient_stop(void)
93 {
94         cli_shutdown(smb_cli);
95 }
96 /****************************************************************************
97  This defines the commands supported by this client
98  ****************************************************************************/
99 struct
100 {
101   char *name;
102   void (*fn)(struct client_info*);
103   char *description;
104 } commands[] = 
105 {
106 #if 0
107   {"ntlogin",    cmd_netlogon_login_test, "<username> NT Domain login test"},
108 #endif
109   {"wksinfo",    cmd_wks_query_info,   "DCE/RPC - Workstation Query Info"},
110 #if 0
111   {"srvinfo",    cmd_srv_query_info,   "DCE/RPC - Server Query Info"},
112   {"srvsessions",cmd_srv_enum_sess,    "DCE/RPC - List sessions on a server"},
113   {"srvshares",  cmd_srv_enum_shares,  "DCE/RPC - List shares on a server"},
114   {"srvconnections",cmd_srv_enum_conn, "DCE/RPC - List connections on a server"},
115   {"srvfiles",   cmd_srv_enum_files,   "DCE/RPC - List files on a server"},
116 #endif
117   {"lsaquery",   cmd_lsa_query_info,   "Query Info Policy (domain member or server)"},
118 #if 0
119   {"enumusers",  cmd_sam_enum_users,   "SAM User Database Query (experimental!)"},
120   {"samuser",    cmd_sam_query_user,   "<username> SAM User Query (experimental!)"},
121   {"samtest",    cmd_sam_test      ,   "SAM User Encrypted RPC test (experimental!)"},
122   {"enumaliases",cmd_sam_enum_aliases, "SAM Aliases Database Query (experimental!)"},
123 #endif
124 #if 0
125   {"enumgroups", cmd_sam_enum_groups,  "SAM Group Database Query (experimental!)"},
126 #endif
127 #if 0
128   {"samgroups",  cmd_sam_query_groups, "SAM Group Database Query (experimental!)"},
129 #endif
130   {"quit",       cmd_quit,        "logoff the server"},
131   {"q",          cmd_quit,        "logoff the server"},
132   {"exit",       cmd_quit,        "logoff the server"},
133   {"bye",        cmd_quit,        "logoff the server"},
134   {"help",       cmd_help,        "[command] give help on a command"},
135   {"?",          cmd_help,        "[command] give help on a command"},
136   {"!",          NULL,            "run a shell command on the local system"},
137   {"",           NULL,            NULL}
138 };
139
140
141 /****************************************************************************
142 do a (presumably graceful) quit...
143 ****************************************************************************/
144 static void cmd_quit(struct client_info *info)
145 {
146         rpcclient_stop();
147 #ifdef MEM_MAN
148         {
149                 extern FILE* dbf;
150                 smb_mem_write_status(dbf);
151                 smb_mem_write_errors(dbf);
152                 smb_mem_write_verbose(dbf);
153         }
154 #endif
155         exit(0);
156 }
157
158 /****************************************************************************
159 help
160 ****************************************************************************/
161 static void cmd_help(struct client_info *info)
162 {
163   int i=0,j;
164   fstring buf;
165
166   if (next_token(NULL,buf,NULL, sizeof(buf)))
167     {
168       if ((i = process_tok(buf)) >= 0)
169         fprintf(out_hnd, "HELP %s:\n\t%s\n\n",commands[i].name,commands[i].description);                    
170     }
171   else
172     while (commands[i].description)
173       {
174         for (j=0; commands[i].description && (j<5); j++) {
175           fprintf(out_hnd, "%-15s",commands[i].name);
176           i++;
177         }
178         fprintf(out_hnd, "\n");
179       }
180 }
181
182 /*******************************************************************
183   lookup a command string in the list of commands, including 
184   abbreviations
185   ******************************************************************/
186 static int process_tok(fstring tok)
187 {
188   int i = 0, matches = 0;
189   int cmd=0;
190   int tok_len = strlen(tok);
191   
192   while (commands[i].fn != NULL)
193     {
194       if (strequal(commands[i].name,tok))
195         {
196           matches = 1;
197           cmd = i;
198           break;
199         }
200       else if (strnequal(commands[i].name, tok, tok_len))
201         {
202           matches++;
203           cmd = i;
204         }
205       i++;
206     }
207   
208   if (matches == 0)
209     return(-1);
210   else if (matches == 1)
211     return(cmd);
212   else
213     return(-2);
214 }
215
216 /****************************************************************************
217 wait for keyboard activity, swallowing network packets
218 ****************************************************************************/
219 static void wait_keyboard(struct cli_state *cli)
220 {
221   fd_set fds;
222   int selrtn;
223   struct timeval timeout;
224   
225   while (1) 
226     {
227       FD_ZERO(&fds);
228       FD_SET(cli->fd,&fds);
229       FD_SET(fileno(stdin),&fds);
230
231       timeout.tv_sec = 20;
232       timeout.tv_usec = 0;
233       selrtn = sys_select(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout);
234       
235       if (FD_ISSET(fileno(stdin),&fds))
236         return;
237
238       /* We deliberately use receive_smb instead of
239          client_receive_smb as we want to receive
240          session keepalives and then drop them here.
241        */
242       if (FD_ISSET(cli->fd,&fds))
243         receive_smb(cli->fd,cli->inbuf,0);
244     }  
245 }
246
247 /****************************************************************************
248   process commands from the client
249 ****************************************************************************/
250 static void do_command(struct client_info *info, char *tok, char *line)
251 {
252         int i;
253
254         if ((i = process_tok(tok)) >= 0)
255         {
256                 commands[i].fn(info);
257         }
258         else if (i == -2)
259         {
260                 fprintf(out_hnd, "%s: command abbreviation ambiguous\n", CNV_LANG(tok));
261         }
262         else
263         {
264                 fprintf(out_hnd, "%s: command not found\n", CNV_LANG(tok));
265         }
266 }
267
268 /****************************************************************************
269   process commands from the client
270 ****************************************************************************/
271 static BOOL process( struct client_info *info, char *cmd_str)
272 {
273         extern FILE *dbf;
274         pstring line;
275         char *cmd = cmd_str;
276
277         if (cmd[0] != '\0') while (cmd[0] != '\0')
278         {
279                 char *p;
280                 fstring tok;
281
282                 if ((p = strchr(cmd, ';')) == 0)
283                 {
284                         strncpy(line, cmd, 999);
285                         line[1000] = '\0';
286                         cmd += strlen(cmd);
287                 }
288                 else
289                 {
290                         if (p - cmd > 999) p = cmd + 999;
291                         strncpy(line, cmd, p - cmd);
292                         line[p - cmd] = '\0';
293                         cmd = p + 1;
294                 }
295
296                 /* input language code to internal one */
297                 CNV_INPUT (line);
298
299                 /* get the first part of the command */
300                 {
301                         char *ptr = line;
302                         if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
303                 }
304
305                 do_command(info, tok, line);
306         }
307         else while (!feof(stdin))
308         {
309                 fstring tok;
310
311                 /* display a prompt */
312                 fprintf(out_hnd, "smb: %s> ", CNV_LANG(info->cur_dir));
313                 fflush(out_hnd);
314
315 #ifdef CLIX
316                 line[0] = wait_keyboard(smb_cli);
317                 /* this might not be such a good idea... */
318                 if ( line[0] == EOF)
319                 {
320                         break;
321                 }
322 #else
323                 wait_keyboard(smb_cli);
324 #endif
325
326                 /* and get a response */
327 #ifdef CLIX
328                 fgets( &line[1],999, stdin);
329 #else
330                 if (!fgets(line,1000,stdin))
331                 {
332                         break;
333                 }
334 #endif
335
336                 /* input language code to internal one */
337                 CNV_INPUT (line);
338
339                 /* special case - first char is ! */
340                 if (*line == '!')
341                 {
342                         system(line + 1);
343                         continue;
344                 }
345
346                 fprintf(out_hnd, "%s\n", line);
347
348                 /* get the first part of the command */
349                 {
350                         char *ptr = line;
351                         if (!next_token(&ptr,tok,NULL, sizeof(tok))) continue;
352                 }
353
354                 do_command(info, tok, line);
355         }
356
357         return(True);
358 }
359
360 /****************************************************************************
361 usage on the program
362 ****************************************************************************/
363 static void usage(char *pname)
364 {
365   fprintf(out_hnd, "Usage: %s service <password> [-p port] [-d debuglevel] [-l log] ",
366            pname);
367
368   fprintf(out_hnd, "\nVersion %s\n",VERSION);
369   fprintf(out_hnd, "\t-p port               connect to the specified port\n");
370   fprintf(out_hnd, "\t-d debuglevel         set the debuglevel\n");
371   fprintf(out_hnd, "\t-l log basename.      Basename for log/debug files\n");
372   fprintf(out_hnd, "\t-n netbios name.      Use this name as my netbios name\n");
373   fprintf(out_hnd, "\t-N                    don't ask for a password\n");
374   fprintf(out_hnd, "\t-m max protocol       set the max protocol level\n");
375   fprintf(out_hnd, "\t-I dest IP            use this IP to connect to\n");
376   fprintf(out_hnd, "\t-E                    write messages to stderr instead of stdout\n");
377   fprintf(out_hnd, "\t-U username           set the network username\n");
378   fprintf(out_hnd, "\t-W workgroup          set the workgroup name\n");
379   fprintf(out_hnd, "\t-c command string     execute semicolon separated commands\n");
380   fprintf(out_hnd, "\t-t terminal code      terminal i/o code {sjis|euc|jis7|jis8|junet|hex}\n");
381   fprintf(out_hnd, "\n");
382 }
383
384 enum client_action
385 {
386         CLIENT_NONE,
387         CLIENT_IPC,
388         CLIENT_SVC
389 };
390
391 /****************************************************************************
392   main program
393 ****************************************************************************/
394  int main(int argc,char *argv[])
395 {
396         char *pname = argv[0];
397         int port = SMB_PORT;
398         int opt;
399         extern FILE *dbf;
400         extern char *optarg;
401         extern int optind;
402         static pstring servicesf = CONFIGFILE;
403         pstring term_code;
404         char *p;
405         BOOL got_pass = False;
406         char *cmd_str="";
407         int myumask = 0755;
408         enum client_action cli_action = CLIENT_NONE;
409         int ret = 0;
410
411         struct client_info cli_info;
412
413         pstring password; /* local copy only, if one is entered */
414         pstring tmp;
415
416         rpcclient_init();
417
418 #ifdef KANJI
419         pstrcpy(term_code, KANJI);
420 #else /* KANJI */
421         *term_code = 0;
422 #endif /* KANJI */
423
424         DEBUGLEVEL = 2;
425
426         cli_info.put_total_size = 0;
427         cli_info.put_total_time_ms = 0;
428         cli_info.get_total_size = 0;
429         cli_info.get_total_time_ms = 0;
430
431         cli_info.dir_total = 0;
432         cli_info.newer_than = 0;
433         cli_info.archive_level = 0;
434         cli_info.print_mode = 1;
435
436         cli_info.translation = False;
437         cli_info.recurse_dir = False;
438         cli_info.lowercase = False;
439         cli_info.prompt = True;
440         cli_info.abort_mget = True;
441
442         cli_info.dest_ip.s_addr = 0;
443         cli_info.name_type = 0x20;
444
445         pstrcpy(cli_info.cur_dir , "\\");
446         pstrcpy(cli_info.file_sel, "");
447         pstrcpy(cli_info.base_dir, "");
448         pstrcpy(smb_cli->domain, "");
449         pstrcpy(smb_cli->user_name, "");
450         pstrcpy(cli_info.myhostname, "");
451         pstrcpy(cli_info.dest_host, "");
452
453         pstrcpy(cli_info.svc_type, "A:");
454         pstrcpy(cli_info.share, "");
455         pstrcpy(cli_info.service, "");
456
457         pstrcpy(cli_info.dom.level3_sid, "");
458         pstrcpy(cli_info.dom.level3_dom, "");
459         pstrcpy(cli_info.dom.level5_sid, "");
460         pstrcpy(cli_info.dom.level5_dom, "");
461
462         smb_cli->nt_pipe_fnum   = 0xffff;
463
464         setup_logging(pname, True);
465
466         TimeInit();
467         charset_initialise();
468
469         myumask = umask(0);
470         umask(myumask);
471
472         if (getenv("USER"))
473         {
474                 pstrcpy(smb_cli->user_name,getenv("USER"));
475
476                 /* modification to support userid%passwd syntax in the USER var
477                 25.Aug.97, jdblair@uab.edu */
478
479                 if ((p=strchr(smb_cli->user_name,'%')))
480                 {
481                         *p = 0;
482                         pstrcpy(password,p+1);
483                         got_pass = True;
484                         memset(strchr(getenv("USER"),'%')+1,'X',strlen(password));
485                 }
486                 strupper(smb_cli->user_name);
487         }
488
489         password[0] = 0;
490
491         /* modification to support PASSWD environmental var
492            25.Aug.97, jdblair@uab.edu */
493         if (getenv("PASSWD"))
494         {
495                 pstrcpy(password,getenv("PASSWD"));
496         }
497
498         if (*smb_cli->user_name == 0 && getenv("LOGNAME"))
499         {
500                 pstrcpy(smb_cli->user_name,getenv("LOGNAME"));
501                 strupper(smb_cli->user_name);
502         }
503
504         if (argc < 2)
505         {
506                 usage(pname);
507                 exit(1);
508         }
509
510         if (*argv[1] != '-')
511         {
512
513                 pstrcpy(cli_info.service, argv[1]);  
514                 /* Convert any '/' characters in the service name to '\' characters */
515                 string_replace( cli_info.service, '/','\\');
516                 argc--;
517                 argv++;
518
519                 DEBUG(1,("service: %s\n", cli_info.service));
520
521                 if (count_chars(cli_info.service,'\\') < 3)
522                 {
523                         usage(pname);
524                         printf("\n%s: Not enough '\\' characters in service\n", cli_info.service);
525                         exit(1);
526                 }
527
528                 /*
529                 if (count_chars(cli_info.service,'\\') > 3)
530                 {
531                         usage(pname);
532                         printf("\n%s: Too many '\\' characters in service\n", cli_info.service);
533                         exit(1);
534                 }
535                 */
536
537                 if (argc > 1 && (*argv[1] != '-'))
538                 {
539                         got_pass = True;
540                         pstrcpy(password,argv[1]);  
541                         memset(argv[1],'X',strlen(argv[1]));
542                         argc--;
543                         argv++;
544                 }
545
546                 cli_action = CLIENT_SVC;
547         }
548
549         while ((opt = getopt(argc, argv,"s:B:O:M:S:i:N:d:Pp:l:hI:EB:U:L:t:m:W:T:D:c:")) != EOF)
550         {
551                 switch (opt)
552                 {
553                         case 'm':
554                         {
555                                 int max_protocol = interpret_protocol(optarg,max_protocol);
556                                 fprintf(stderr, "max protocol not currently supported\n");
557                                 break;
558                         }
559
560                         case 'O':
561                         {
562                                 pstrcpy(user_socket_options,optarg);
563                                 break;  
564                         }
565
566                         case 'S':
567                         {
568                                 pstrcpy(cli_info.dest_host,optarg);
569                                 strupper(cli_info.dest_host);
570                                 cli_action = CLIENT_IPC;
571                                 break;
572                         }
573
574                         case 'B':
575                         {
576                                 iface_set_default(NULL,optarg,NULL);
577                                 break;
578                         }
579
580                         case 'i':
581                         {
582                                 pstrcpy(scope, optarg);
583                                 break;
584                         }
585
586                         case 'U':
587                         {
588                                 char *lp;
589                                 pstrcpy(smb_cli->user_name,optarg);
590                                 if ((lp=strchr(smb_cli->user_name,'%')))
591                                 {
592                                         *lp = 0;
593                                         pstrcpy(password,lp+1);
594                                         got_pass = True;
595                                         memset(strchr(optarg,'%')+1,'X',strlen(password));
596                                 }
597                                 break;
598                         }
599
600                         case 'W':
601                         {
602                                 pstrcpy(smb_cli->domain,optarg);
603                                 break;
604                         }
605
606                         case 'E':
607                         {
608                                 dbf = stderr;
609                                 break;
610                         }
611
612                         case 'I':
613                         {
614                                 cli_info.dest_ip = *interpret_addr2(optarg);
615                                 if (zero_ip(cli_info.dest_ip))
616                                 {
617                                         exit(1);
618                                 }
619                                 break;
620                         }
621
622                         case 'N':
623                         {
624                                 got_pass = True;
625                                 break;
626                         }
627
628                         case 'd':
629                         {
630                                 if (*optarg == 'A')
631                                         DEBUGLEVEL = 10000;
632                                 else
633                                         DEBUGLEVEL = atoi(optarg);
634                                 break;
635                         }
636
637                         case 'l':
638                         {
639                                 slprintf(debugf, sizeof(debugf)-1,
640                                          "%s.client",optarg);
641                                 break;
642                         }
643
644                         case 'p':
645                         {
646                                 port = atoi(optarg);
647                                 break;
648                         }
649
650                         case 'c':
651                         {
652                                 cmd_str = optarg;
653                                 got_pass = True;
654                                 break;
655                         }
656
657                         case 'h':
658                         {
659                                 usage(pname);
660                                 exit(0);
661                                 break;
662                         }
663
664                         case 's':
665                         {
666                                 pstrcpy(servicesf, optarg);
667                                 break;
668                         }
669
670                         case 't':
671                         {
672                                 pstrcpy(term_code, optarg);
673                                 break;
674                         }
675
676                         default:
677                         {
678                                 usage(pname);
679                                 exit(1);
680                                 break;
681                         }
682                 }
683         }
684
685         if (cli_action == CLIENT_NONE)
686         {
687                 usage(pname);
688                 exit(1);
689         }
690
691         DEBUG(3,("%s client started (version %s)\n",timestring(),VERSION));
692
693         if (!get_myname(cli_info.myhostname, NULL))
694         {
695                 fprintf(stderr, "Failed to get my hostname.\n");
696         }
697
698         if (!lp_load(servicesf,True, False, False))
699         {
700                 fprintf(stderr, "Can't load %s - run testparm to debug it\n", servicesf);
701         }
702
703         codepage_initialise(lp_client_code_page());
704
705         if (*smb_cli->domain == 0) pstrcpy(smb_cli->domain,lp_workgroup());
706
707         load_interfaces();
708
709         if (cli_action == CLIENT_IPC)
710         {
711                 pstrcpy(cli_info.share, "IPC$");
712                 pstrcpy(cli_info.svc_type, "IPC");
713         }
714
715         fstrcpy(cli_info.mach_acct, cli_info.myhostname);
716         strupper(cli_info.mach_acct);
717         fstrcat(cli_info.mach_acct, "$");
718
719         /* set the password cache info */
720         if (got_pass)
721         {
722                 if (password[0] == 0)
723                 {
724                         pwd_set_nullpwd(&(smb_cli->pwd));
725                 }
726                 else
727                 {
728                         pwd_make_lm_nt_16(&(smb_cli->pwd), password); /* generate 16 byte hashes */
729                 }
730         }
731         else 
732         {
733                 pwd_read(&(smb_cli->pwd), "Enter Password:", True);
734         }
735
736         /* paranoia: destroy the local copy of the password */
737         bzero(password, sizeof(password)); 
738
739         /* establish connections.  nothing to stop these being re-established. */
740         rpcclient_connect(&cli_info);
741
742         DEBUG(5,("rpcclient_connect: smb_cli->fd:%d\n", smb_cli->fd));
743         if (smb_cli->fd <= 0)
744         {
745                 fprintf(stderr, "warning: connection could not be established to %s<%02x>\n",
746                                  cli_info.dest_host, cli_info.name_type);
747                 fprintf(stderr, "this version of smbclient may crash if you proceed\n");
748                 exit(-1);
749         }
750
751         ret = 0;
752
753         switch (cli_action)
754         {
755                 case CLIENT_IPC:
756                 {
757                         ret = process(&cli_info, cmd_str) ? 0 : 1;
758                         break;
759                 }
760
761                 default:
762                 {
763                         fprintf(stderr, "unknown client action requested\n");
764                         ret = 1;
765                         break;
766                 }
767         }
768
769         rpcclient_stop();
770
771         return(0);
772 }