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