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