Add autodetect for net join, as well as some more help updates
[jra/samba/.git] / source3 / utils / net.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    Distributed SMB/CIFS Server Management Utility 
4    Copyright (C) 2001 Steve French  (sfrench@us.ibm.com)
5    Copyright (C) 2001 Jim McDonough (jmcd@us.ibm.com)
6    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
7    Copyright (C) 2001 Andrew Bartlett (abartlet@samba.org)
8
9    Originally written by Steve and Jim. Largely rewritten by tridge in
10    November 2001.
11
12    Reworked again by abartlet in December 2001
13
14    This program is free software; you can redistribute it and/or modify
15    it under the terms of the GNU General Public License as published by
16    the Free Software Foundation; either version 2 of the License, or
17    (at your option) any later version.
18    
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License for more details.
23    
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
27  
28 /*****************************************************/
29 /*                                                   */
30 /*   Distributed SMB/CIFS Server Management Utility  */
31 /*                                                   */
32 /*   The intent was to make the syntax similar       */
33 /*   to the NET utility (first developed in DOS      */
34 /*   with additional interesting & useful functions  */
35 /*   added in later SMB server network operating     */
36 /*   systems).                                       */
37 /*                                                   */
38 /*****************************************************/
39
40 #include "includes.h"
41 #include "../utils/net.h"
42
43 /***********************************************************************/
44 /* Beginning of internationalization section.  Translatable constants  */
45 /* should be kept in this area and referenced in the rest of the code. */
46 /*                                                                     */
47 /* No functions, outside of Samba or LSB (Linux Standards Base) should */
48 /* be used (if possible).                                              */
49 /***********************************************************************/
50
51 #define YES_STRING              "Yes"
52 #define NO_STRING               "No"
53
54 /************************************************************************************/
55 /*                       end of internationalization section                        */
56 /************************************************************************************/
57
58 /* Yes, these buggers are globals.... */
59 char *opt_requester_name = NULL;
60 char *opt_host = NULL; 
61 char *opt_password = NULL;
62 char *opt_user_name = NULL;
63 BOOL opt_user_specified = False;
64 char *opt_workgroup = NULL;
65 int opt_long_list_entries = 0;
66 int opt_reboot = 0;
67 int opt_force = 0;
68 int opt_port = 0;
69 int opt_maxusers = -1;
70 char *opt_comment = "";
71 int opt_flags = -1;
72 int opt_jobid = 0;
73 int opt_timeout = 0;
74 char *opt_target_workgroup = NULL;
75
76 static BOOL got_pass = False;
77 BOOL opt_have_ip = False;
78 struct in_addr opt_dest_ip;
79
80 extern pstring global_myname;
81
82 int net_common_flags_usage(int argc, const char **argv)
83 {
84
85         d_printf("Valid targets: choose one (none defaults to localhost)\n");
86         d_printf("\t-S or --server=<server>\t\tserver name\n");
87         d_printf("\t-I or --ipaddress=<ipaddr>\taddress of target server\n");
88         d_printf("\t-w or --workgroup=<wg>\t\ttarget workgroup or domain\n");
89
90         d_printf("\n");
91         d_printf("Valid miscellaneous options are:\n"); /* misc options */
92         d_printf("\t-p or --port=<port>\tconnection port on target server\n");
93         d_printf("\t-W or --myworkgroup=<wg>\tclient workgroup\n");
94         d_printf("\t-d or --debug=<level>\t\tdebug level (0-10)\n");
95         d_printf("\t-n or --myname=<name>\t\tclient name\n");
96         d_printf("\t-U or --user=<name>\t\tuser name\n");
97         d_printf("\t-s or --conf=<path>\t\tpathname of smb.conf file\n");
98         d_printf("\t-l or --long\t\t\tDisplay full information\n");
99         return -1;
100 }
101
102
103 /*
104   run a function from a function table. If not found then
105   call the specified usage function 
106 */
107 int net_run_function(int argc, const char **argv, struct functable *table, 
108                      int (*usage_fn)(int argc, const char **argv))
109 {
110         int i;
111         
112         if (argc < 1) {
113                 d_printf("\nUsage: \n");
114                 return usage_fn(argc, argv);
115         }
116         for (i=0; table[i].funcname; i++) {
117                 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
118                         return table[i].fn(argc-1, argv+1);
119         }
120         d_printf("No command: %s\n", argv[0]);
121         return usage_fn(argc, argv);
122 }
123
124
125 /****************************************************************************
126 connect to \\server\ipc$  
127 ****************************************************************************/
128 NTSTATUS connect_to_ipc(struct cli_state **c, struct in_addr *server_ip,
129                                         const char *server_name)
130 {
131         NTSTATUS nt_status;
132
133         if (!got_pass) {
134                 char *pass = getpass("Password:");
135                 if (pass) {
136                         opt_password = strdup(pass);
137                 }
138         }
139         
140         nt_status = cli_full_connection(c, opt_requester_name, server_name, 
141                                         server_ip, opt_port,
142                                         "IPC$", "IPC",  
143                                         opt_user_name, opt_workgroup,
144                                         opt_password, strlen(opt_password));
145         
146         if (NT_STATUS_IS_OK(nt_status)) {
147                 return nt_status;
148         } else {
149                 DEBUG(0,("Cannot connect to server.  Error was %s\n", 
150                          get_nt_error_msg(nt_status)));
151
152                 /* Display a nicer message depending on the result */
153
154                 if (NT_STATUS_V(nt_status) == 
155                     NT_STATUS_V(NT_STATUS_LOGON_FAILURE))
156                         d_printf("The username or password was not correct.\n");
157
158                 return nt_status;
159         }
160 }
161
162 /****************************************************************************
163 connect to \\server\ipc$ anonymously
164 ****************************************************************************/
165 NTSTATUS connect_to_ipc_anonymous(struct cli_state **c,
166                         struct in_addr *server_ip, const char *server_name)
167 {
168         NTSTATUS nt_status;
169
170         nt_status = cli_full_connection(c, opt_requester_name, server_name, 
171                                         server_ip, opt_port,
172                                         "IPC$", "IPC",  
173                                         "", "",
174                                         "", 0);
175         
176         if (NT_STATUS_IS_OK(nt_status)) {
177                 return nt_status;
178         } else {
179                 DEBUG(0,("Cannot connect to server (anonymously).  Error was %s\n", get_nt_error_msg(nt_status)));
180                 return nt_status;
181         }
182 }
183
184 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
185 {
186
187         if (opt_host) {
188                 *server_name = strdup(opt_host);
189         }               
190
191         if (opt_have_ip) {
192                 *server_ip = opt_dest_ip;
193                 if (!*server_name) {
194                         *server_name = strdup(inet_ntoa(opt_dest_ip));
195                 }
196         } else if (*server_name) {
197                 /* resolve the IP address */
198                 if (!resolve_name(*server_name, server_ip, 0x20))  {
199                         DEBUG(1,("Unable to resolve server name\n"));
200                         return False;
201                 }
202         } else if (flags & NET_FLAGS_PDC) {
203                 struct in_addr *ip_list;
204                 int addr_count;
205                 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
206                         fstring dc_name;
207                         if (addr_count < 1) {
208                                 return False;
209                         }
210                         
211                         *server_ip = *ip_list;
212                         
213                         if (is_zero_ip(*server_ip))
214                                 return False;
215                         
216                         if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
217                                 return False;
218                                 
219                         *server_name = strdup(dc_name);
220                 }
221                 
222         } else if (flags & NET_FLAGS_DMB) {
223                 struct in_addr msbrow_ip;
224                 /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
225                 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B))  {
226                         DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
227                         return False;
228                 } else {
229                         *server_ip = msbrow_ip;
230                 }
231                 *server_name = strdup(inet_ntoa(opt_dest_ip));
232         } else if (flags & NET_FLAGS_MASTER) {
233                 struct in_addr brow_ips;
234                 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D))  {
235                                 /* go looking for workgroups */
236                         DEBUG(1,("Unable to resolve master browser via name lookup\n"));
237                         return False;
238                 } else {
239                         *server_ip = brow_ips;
240                 }
241                 *server_name = strdup(inet_ntoa(opt_dest_ip));
242         } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
243                 extern struct in_addr loopback_ip;
244                 *server_ip = loopback_ip;
245                 *server_name = strdup("127.0.0.1");
246         }
247
248         if (!server_name || !*server_name) {
249                 DEBUG(1,("no server to connect to\n"));
250                 return False;
251         }
252
253         return True;
254 }
255
256
257 BOOL net_find_dc(struct in_addr *server_ip, fstring server_name, char *domain_name)
258 {
259         struct in_addr *ip_list;
260         int addr_count;
261
262         if (get_dc_list(True /* PDC only*/, domain_name, &ip_list, &addr_count)) {
263                 fstring dc_name;
264                 if (addr_count < 1) {
265                         return False;
266                 }
267                         
268                 *server_ip = *ip_list;
269                 
270                 if (is_zero_ip(*server_ip))
271                         return False;
272                 
273                 if (!lookup_dc_name(global_myname, domain_name, server_ip, dc_name))
274                         return False;
275                         
276                 safe_strcpy(server_name, dc_name, FSTRING_LEN);
277                 return True;
278         } else
279                 return False;
280 }
281
282
283 struct cli_state *net_make_ipc_connection(unsigned flags)
284 {
285         char *server_name = NULL;
286         struct in_addr server_ip;
287         struct cli_state *cli = NULL;
288         NTSTATUS nt_status;
289
290         if (!net_find_server(flags, &server_ip, &server_name)) {
291                 d_printf("\nUnable to find a suitable server\n");
292                 return NULL;
293         }
294
295         if (flags & NET_FLAGS_ANONYMOUS) {
296                 nt_status = connect_to_ipc_anonymous(&cli, &server_ip, server_name);
297         } else {
298                 nt_status = connect_to_ipc(&cli, &server_ip, server_name);
299         }
300         SAFE_FREE(server_name);
301         return cli;
302 }
303
304
305 static int net_join(int argc, const char **argv)
306 {
307         if (lp_security() < SEC_DOMAIN) {
308                 d_printf("Error: security setting must be DOMAIN or ADS to "\
309                          "joing a domain\n\n");
310                 return -1;
311         }
312         if (lp_security() == SEC_ADS) {
313                 if (net_ads_join(argc, argv) == 0)
314                         return 0;
315                 else
316                         d_printf("ADS join did not work, trying RPC...\n");
317         }
318         return net_rpc_join(argc, argv);
319 }
320
321 static int net_usage(int argc, const char **argv)
322 {
323         d_printf("  net time\t\t to view or set time information\n"\
324                  "  net lookup\t\t to lookup host name or ip address\n"\
325                  "\n"\
326                  "  net ads [command]\tto run ADS commands\n"\
327                  "  net rap [command]\tto run RAP (pre-RPC) commands\n"\
328                  "  net rpc [command]\tto run RPC commands\n"\
329                  "\n"\
330                  "Type \"net help <option>\" to get more information on that option\n");
331         return -1;
332 }
333
334 static int help_usage(int argc, const char **argv)
335 {
336         d_printf(
337 "\n"\
338 "Usage: net help <function>\n"\
339 "\n"\
340 "Valid functions are:\n"\
341 "  RPC RAP ADS FILE SHARE SESSION SERVER DOMAIN PRINTQ USER GROUP VALIDATE\n"\
342 "  GROUPMEMBER ADMIN SERVICE PASSWORD TIME LOOKUP\n");
343         return -1;
344 }
345
346 /*
347   handle "net help *" subcommands
348 */
349 static int net_help(int argc, const char **argv)
350 {
351         struct functable func[] = {
352                 {"ADS", net_ads_usage}, 
353                 {"RAP", net_rap_help},
354                 {"RPC", net_rpc_help},
355
356                 {"FILE", net_rap_file_usage},
357                 {"SHARE", net_rap_share_usage},
358                 {"SESSION", net_rap_session_usage},
359                 {"SERVER", net_rap_server_usage},
360                 {"DOMAIN", net_rap_domain_usage},
361                 {"PRINTQ", net_rap_printq_usage},
362                 {"USER", net_rap_user_usage},
363                 {"GROUP", net_rap_group_usage},
364                 {"VALIDATE", net_rap_validate_usage},
365                 {"GROUPMEMBER", net_rap_groupmember_usage},
366                 {"ADMIN", net_rap_admin_usage},
367                 {"SERVICE", net_rap_service_usage},
368                 {"PASSWORD", net_rap_password_usage},
369                 {"TIME", net_time_usage},
370                 {"LOOKUP", net_lookup_usage},
371
372                 {"HELP", help_usage},
373                 {NULL, NULL}};
374
375         return net_run_function(argc, argv, func, net_usage);
376 }
377
378 /* main function table */
379 static struct functable net_func[] = {
380         {"RPC", net_rpc},
381         {"RAP", net_rap},
382         {"ADS", net_ads},
383
384         /* eventually these should auto-choose the transport ... */
385         {"FILE", net_rap_file},
386         {"SHARE", net_rap_share},
387         {"SESSION", net_rap_session},
388         {"SERVER", net_rap_server},
389         {"DOMAIN", net_rap_domain},
390         {"PRINTQ", net_rap_printq},
391         {"USER", net_rap_user},
392         {"GROUP", net_rap_group},
393         {"VALIDATE", net_rap_validate},
394         {"GROUPMEMBER", net_rap_groupmember},
395         {"ADMIN", net_rap_admin},
396         {"SERVICE", net_rap_service},   
397         {"PASSWORD", net_rap_password},
398         {"TIME", net_time},
399         {"LOOKUP", net_lookup},
400         {"JOIN", net_join},
401
402         {"HELP", net_help},
403         {NULL, NULL}
404 };
405
406
407 /****************************************************************************
408   main program
409 ****************************************************************************/
410  int main(int argc, const char **argv)
411 {
412         int opt,i;
413         char *p;
414         int rc = 0;
415         int argc_new = 0;
416         const char ** argv_new;
417         poptContext pc;
418         static char *servicesf = dyn_CONFIGFILE;
419         static int debuglevel;
420
421         struct poptOption long_options[] = {
422                 {"help",        'h', POPT_ARG_NONE,   0, 'h'},
423                 {"workgroup",   'w', POPT_ARG_STRING, &opt_target_workgroup},
424                 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
425                 {"user",        'U', POPT_ARG_STRING, &opt_user_name, 'U'},
426                 {"ipaddress",   'I', POPT_ARG_STRING, 0,'I'},
427                 {"port",        'p', POPT_ARG_INT,    &opt_port},
428                 {"myname",      'n', POPT_ARG_STRING, &opt_requester_name},
429                 {"conf",        's', POPT_ARG_STRING, &servicesf},
430                 {"debug",       'd', POPT_ARG_INT,    &debuglevel, 'd'},
431                 {"debuglevel",  'd', POPT_ARG_INT,    &debuglevel, 'd'},
432                 {"server",      'S', POPT_ARG_STRING, &opt_host},
433                 {"comment",     'C', POPT_ARG_STRING, &opt_comment},
434                 {"maxusers",    'M', POPT_ARG_INT,    &opt_maxusers},
435                 {"flags",       'F', POPT_ARG_INT,    &opt_flags},
436                 {"jobid",       'j', POPT_ARG_INT,    &opt_jobid},
437                 {"long",        'l', POPT_ARG_NONE,   &opt_long_list_entries},
438                 {"reboot",      'r', POPT_ARG_NONE,   &opt_reboot},
439                 {"force",       'f', POPT_ARG_NONE,   &opt_force},
440                 {"timeout",     't', POPT_ARG_INT,    &opt_timeout},
441                 { 0, 0, 0, 0}
442         };
443
444         got_pass = 0;
445         zero_ip(&opt_dest_ip);
446
447         dbf = x_stderr;
448         
449         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
450                             POPT_CONTEXT_KEEP_FIRST);
451         
452         while((opt = poptGetNextOpt(pc)) != -1) {
453                 switch (opt) {
454                 case 'h':
455                         net_usage(argc, argv);
456                         exit(0);
457                         break;
458                 case 'I':
459                         opt_dest_ip = *interpret_addr2(poptGetOptArg(pc));
460                         if (is_zero_ip(opt_dest_ip))
461                                 d_printf("\nInvalid ip address specified\n");
462                         else
463                                 opt_have_ip = True;
464                         break;
465                 case 'U':
466                         opt_user_specified = True;
467                         opt_user_name = strdup(opt_user_name);
468                         p = strchr(opt_user_name,'%');
469                         if (p) {
470                                 *p = 0;
471                                 opt_password = p+1;
472                                 got_pass = 1;
473                         }
474                         break;
475                 default:
476                         d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
477                         net_usage(argc, argv);
478                 }
479         }
480
481         lp_load(servicesf,True,False,False);       
482
483         DEBUGLEVEL = debuglevel;
484
485         argv_new = (const char **)poptGetArgs(pc);
486
487         argc_new = argc;
488         for (i=0; i<argc; i++) {
489                 if (argv_new[i] == NULL) {
490                         argc_new = i;
491                         break;
492                 }
493         }
494          
495         if (!opt_requester_name) {
496                 static fstring myname;
497                 get_myname(myname);
498                 opt_requester_name = myname;
499         }
500
501         if (!opt_user_name && getenv("LOGNAME")) {
502                 opt_user_name = getenv("LOGNAME");
503         }
504
505         if (!opt_workgroup) {
506                 opt_workgroup = lp_workgroup();
507         }
508         
509         if (!opt_target_workgroup) {
510                 opt_target_workgroup = lp_workgroup();
511         }
512         
513         if (!*global_myname) {
514                 char *p2;
515
516                 fstrcpy(global_myname, myhostname());
517                 p2 = strchr_m(global_myname, '.');
518                 if (p2) 
519                         *p2 = 0;
520         }
521         
522         strupper(global_myname);
523
524         load_interfaces();
525
526         rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
527         
528         DEBUG(2,("return code = %d\n", rc));
529         return rc;
530 }