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