Removed unreferenced getopt() externals.
[ira/wip.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 static BOOL have_ip = False;
75 static struct in_addr 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(1,("Cannot connect to server.  Error was %s\n", get_nt_error_msg(nt_status)));
126                 return NULL;
127         }
128 }
129
130 /****************************************************************************
131 connect to \\server\ipc$ anonymously
132 ****************************************************************************/
133 static struct cli_state *connect_to_ipc_anonymous(struct in_addr *server_ip, const char *server_name)
134 {
135         struct cli_state *c;
136         NTSTATUS nt_status;
137
138         nt_status = cli_full_connection(&c, opt_requester_name, server_name, 
139                                         server_ip, opt_port,
140                                         "IPC$", "IPC",  
141                                         "", "",
142                                         "", 0);
143         
144         if (NT_STATUS_IS_OK(nt_status)) {
145                 return c;
146         } else {
147                 DEBUG(1,("Cannot connect to server (anonymously).  Error was %s\n", get_nt_error_msg(nt_status)));
148                 return NULL;
149         }
150 }
151
152 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
153 {
154
155         if (opt_host) {
156                 *server_name = strdup(opt_host);
157         }               
158
159         if (have_ip) {
160                 *server_ip = dest_ip;
161                 if (!*server_name) {
162                         *server_name = strdup(inet_ntoa(dest_ip));
163                 }
164         } else if (*server_name) {
165                 /* resolve the IP address */
166                 if (!resolve_name(*server_name, server_ip, 0x20))  {
167                         DEBUG(1,("Unable to resolve server name\n"));
168                         return False;
169                 }
170         } else if (flags & NET_FLAGS_PDC) {
171                 struct in_addr *ip_list;
172                 int addr_count;
173                 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
174                         fstring dc_name;
175                         if (addr_count < 1) {
176                                 return False;
177                         }
178                         
179                         *server_ip = *ip_list;
180                         
181                         if (is_zero_ip(*server_ip))
182                                 return False;
183                         
184                         if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
185                                 return False;
186                                 
187                         *server_name = strdup(dc_name);
188                 }
189                 
190         } else if (flags & NET_FLAGS_DMB) {
191                 struct in_addr msbrow_ip;
192                 /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
193                 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B))  {
194                         DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
195                         return False;
196                 } else {
197                         *server_ip = msbrow_ip;
198                 }
199                 *server_name = strdup(inet_ntoa(dest_ip));
200         } else if (flags & NET_FLAGS_MASTER) {
201                 struct in_addr brow_ips;
202                 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D))  {
203                                 /* go looking for workgroups */
204                         DEBUG(1,("Unable to resolve master browser via name lookup\n"));
205                         return False;
206                 } else {
207                         *server_ip = brow_ips;
208                 }
209                 *server_name = strdup(inet_ntoa(dest_ip));
210         } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
211                 extern struct in_addr loopback_ip;
212                 *server_ip = loopback_ip;
213                 *server_name = strdup("127.0.0.1");
214         }
215
216         if (!server_name || !*server_name) {
217                 DEBUG(1,("no server to connect to\n"));
218                 return False;
219         }
220
221         return True;
222 }
223
224 struct cli_state *net_make_ipc_connection(unsigned flags)
225 {
226         char *server_name = NULL;
227         struct in_addr server_ip;
228         struct cli_state *cli;
229
230         if (!net_find_server(flags, &server_ip, &server_name)) {
231                 d_printf("\nUnable to find a suitable server\n");
232                 return NULL;
233         }
234
235         if (flags & NET_FLAGS_ANONYMOUS) {
236                 cli = connect_to_ipc_anonymous(&server_ip, server_name);
237         } else {
238                 cli = connect_to_ipc(&server_ip, server_name);
239         }
240         SAFE_FREE(server_name);
241         if(!cli) {
242                 d_printf("\nUnable to connect to target server\n");
243                 return NULL;
244         }
245         return cli;
246 }
247
248
249 static int net_usage(int argc, const char **argv)
250 {
251         d_printf("  net ads [command]\tto run ADS commands\n"\
252                  "  net rap [command]\tto run RAP (pre-RPC) commands\n"\
253                  "  net rpc [command]\tto run RPC commands\n"\
254                  "  net rap help\n"\
255                  "\nType \"net help <option>\" to get more information on that option\n");
256         return -1;
257 }
258
259 static int help_usage(int argc, const char **argv)
260 {
261         d_printf(
262 "\n"\
263 "Usage: net help <function>\n"\
264 "\n"\
265 "Valid functions are:\n"\
266 "  RPC RAP ADS FILE SHARE SESSION SERVER DOMAIN PRINTQ USER GROUP VALIDATE\n"\
267 "  GROUPMEMBER ADMIN SERVICE PASSWORD\n");
268         return -1;
269 }
270
271 /*
272   handle "net help *" subcommands
273 */
274 static int net_help(int argc, const char **argv)
275 {
276         struct functable func[] = {
277                 {"ADS", net_ads_usage}, 
278                 {"RAP", net_rap_usage},
279                 {"RPC", net_rpc_usage},
280
281                 {"FILE", net_rap_file_usage},
282                 {"SHARE", net_rap_share_usage},
283                 {"SESSION", net_rap_session_usage},
284                 {"SERVER", net_rap_server_usage},
285                 {"DOMAIN", net_rap_domain_usage},
286                 {"PRINTQ", net_rap_printq_usage},
287                 {"USER", net_rap_user_usage},
288                 {"GROUP", net_rap_group_usage},
289                 {"VALIDATE", net_rap_validate_usage},
290                 {"GROUPMEMBER", net_rap_groupmember_usage},
291                 {"ADMIN", net_rap_admin_usage},
292                 {"SERVICE", net_rap_service_usage},
293                 {"PASSWORD", net_rap_password_usage},
294
295                 {"HELP", help_usage},
296                 {NULL, NULL}};
297
298         return net_run_function(argc, argv, func, help_usage);
299 }
300
301 /* main function table */
302 static struct functable net_func[] = {
303         {"RPC", net_rpc},
304         {"RAP", net_rap},
305         {"ADS", net_ads},
306
307         /* eventually these should auto-choose the transport ... */
308         {"FILE", net_rap_file},
309         {"SHARE", net_rap_share},
310         {"SESSION", net_rap_session},
311         {"SERVER", net_rap_server},
312         {"DOMAIN", net_rap_domain},
313         {"PRINTQ", net_rap_printq},
314         {"USER", net_rap_user},
315         {"GROUP", net_rap_group},
316         {"VALIDATE", net_rap_validate},
317         {"GROUPMEMBER", net_rap_groupmember},
318         {"ADMIN", net_rap_admin},
319         {"SERVICE", net_rap_service},   
320         {"PASSWORD", net_rap_password},
321
322         {"HELP", net_help},
323         {NULL, NULL}
324 };
325
326
327 /****************************************************************************
328   main program
329 ****************************************************************************/
330  int main(int argc, const char **argv)
331 {
332         int opt,i;
333         char *p;
334         int rc = 0;
335         int argc_new = 0;
336         const char ** argv_new;
337         poptContext pc;
338         static char *servicesf = dyn_CONFIGFILE;
339         static int debuglevel;
340
341         struct poptOption long_options[] = {
342                 {"help",        'h', POPT_ARG_NONE,   0,     'h'},
343                 {"workgroup",   'w', POPT_ARG_STRING, &opt_target_workgroup},
344                 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
345                 {"user",        'U', POPT_ARG_STRING, &opt_user_name, 'U'},
346                 {"ipaddress",   'I', POPT_ARG_STRING, 0,     'I'},
347                 {"port",        'p', POPT_ARG_INT,    &opt_port},
348                 {"myname",      'n', POPT_ARG_STRING, &opt_requester_name},
349                 {"conf",        's', POPT_ARG_STRING, &servicesf},
350                 {"debug",       'd', POPT_ARG_INT,    &debuglevel, 'd'},
351                 {"debuglevel",  'd', POPT_ARG_INT,    &debuglevel, 'd'},
352                 {"server",      'S', POPT_ARG_STRING, &opt_host},
353                 {"comment",     'C', POPT_ARG_STRING, &opt_comment},
354                 {"maxusers",    'M', POPT_ARG_INT,    &opt_maxusers},
355                 {"flags",       'F', POPT_ARG_INT,    &opt_flags},
356                 {"jobid",       'j', POPT_ARG_INT,    &opt_jobid},
357                 {"long",        'l', POPT_ARG_NONE,   &opt_long_list_entries},
358                 { 0, 0, 0, 0}
359         };
360
361         got_pass = 0;
362         zero_ip(&dest_ip);
363
364         dbf = x_stdout;
365         
366         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
367                             POPT_CONTEXT_KEEP_FIRST);
368         
369         while((opt = poptGetNextOpt(pc)) != -1) {
370                 switch (opt) {
371                 case 'h':
372                         net_usage(argc, argv);
373                         exit(0);
374                         break;
375                 case 'I':
376                         dest_ip = *interpret_addr2(poptGetOptArg(pc));
377                         if (is_zero_ip(dest_ip))
378                                 d_printf("\nInvalid ip address specified\n");
379                         else
380                                 have_ip = True;
381                         break;
382                 case 'U':
383                         opt_user_name = strdup(opt_user_name);
384                         p = strchr(opt_user_name,'%');
385                         if (p) {
386                                 *p = 0;
387                                 opt_password = p+1;
388                                 got_pass = 1;
389                         }
390                         break;
391                 default:
392                         d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
393                         net_usage(argc, argv);
394                 }
395         }
396
397         DEBUGLEVEL = debuglevel;
398
399         lp_load(servicesf,True,False,False);       
400
401         argv_new = (const char **)poptGetArgs(pc);
402
403         argc_new = argc;
404         for (i=0; i<argc; i++) {
405                 if (argv_new[i] == NULL) {
406                         argc_new = i;
407                         break;
408                 }
409         }
410          
411         if (!opt_requester_name) {
412                 static fstring myname;
413                 get_myname(myname);
414                 opt_requester_name = myname;
415         }
416
417         if (!opt_user_name && getenv("LOGNAME")) {
418                 opt_user_name = getenv("LOGNAME");
419         }
420
421         if (!opt_workgroup) {
422                 opt_workgroup = lp_workgroup();
423         }
424         
425         if (!opt_target_workgroup) {
426                 opt_target_workgroup = lp_workgroup();
427         }
428         
429         if (!*global_myname) {
430                 char *p2;
431
432                 fstrcpy(global_myname, myhostname());
433                 p2 = strchr_m(global_myname, '.');
434                 if (p2) 
435                         *p2 = 0;
436         }
437         
438         strupper(global_myname);
439
440         load_interfaces();
441
442         rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
443         
444         DEBUG(2,("return code = %d\n", rc));
445         return rc;
446 }