Make it easier to construct anonymous connections with a new flag and helper
[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 extern int optind, opterr, optopt;
60
61 /* Yes, these buggers are globals.... */
62 char *opt_requester_name = NULL;
63 char *opt_host = NULL; 
64 char *opt_password = NULL;
65 char *opt_user_name = NULL;
66 char *opt_workgroup = NULL;
67 int opt_long_list_entries = 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 char *opt_target_workgroup = NULL;
74
75 static BOOL got_pass = False;
76 static BOOL have_ip = False;
77 static struct in_addr dest_ip;
78
79 extern pstring global_myname;
80
81 /*
82   run a function from a function table. If not found then
83   call the specified usage function 
84 */
85 int net_run_function(int argc, const char **argv, struct functable *table, 
86                      int (*usage_fn)(int argc, const char **argv))
87 {
88         int i;
89         
90         if (argc < 1) {
91                 d_printf("\nUsage: \n");
92                 return usage_fn(argc, argv);
93         }
94         for (i=0; table[i].funcname; i++) {
95                 if (StrCaseCmp(argv[0], table[i].funcname) == 0)
96                         return table[i].fn(argc-1, argv+1);
97         }
98         d_printf("No command: %s\n", argv[0]);
99         return usage_fn(argc, argv);
100 }
101
102
103 /****************************************************************************
104 connect to \\server\ipc$  
105 ****************************************************************************/
106 static struct cli_state *connect_to_ipc(struct in_addr *server_ip, const char *server_name)
107 {
108         struct cli_state *c;
109         NTSTATUS nt_status;
110
111         if (!got_pass) {
112                 char *pass = getpass("Password:");
113                 if (pass) {
114                         opt_password = strdup(pass);
115                 }
116         }
117         
118         nt_status = cli_full_connection(&c, opt_requester_name, server_name, 
119                                         server_ip, opt_port,
120                                         "IPC$", "IPC",  
121                                         opt_user_name, opt_workgroup,
122                                         opt_password, strlen(opt_password));
123         
124         if (NT_STATUS_IS_OK(nt_status)) {
125                 return c;
126         } else {
127                 DEBUG(1,("Cannot connect to server.  Error was %s\n", get_nt_error_msg(nt_status)));
128                 return NULL;
129         }
130 }
131
132 /****************************************************************************
133 connect to \\server\ipc$ anonymously
134 ****************************************************************************/
135 static struct cli_state *connect_to_ipc_anonymous(struct in_addr *server_ip, const char *server_name)
136 {
137         struct cli_state *c;
138         NTSTATUS nt_status;
139
140         nt_status = cli_full_connection(&c, opt_requester_name, server_name, 
141                                         server_ip, opt_port,
142                                         "IPC$", "IPC",  
143                                         "", "",
144                                         "", 0);
145         
146         if (NT_STATUS_IS_OK(nt_status)) {
147                 return c;
148         } else {
149                 DEBUG(1,("Cannot connect to server (anonymously).  Error was %s\n", get_nt_error_msg(nt_status)));
150                 return NULL;
151         }
152 }
153
154 static BOOL net_find_server(unsigned flags, struct in_addr *server_ip, char **server_name)
155 {
156
157         if (opt_host) {
158                 *server_name = strdup(opt_host);
159         }               
160
161         if (have_ip) {
162                 *server_ip = dest_ip;
163                 if (!*server_name) {
164                         *server_name = strdup(inet_ntoa(dest_ip));
165                 }
166         } else if (server_name) {
167                 /* resolve the IP address */
168                 if (!resolve_name(*server_name, server_ip, 0x20))  {
169                         DEBUG(1,("Unable to resolve server name\n"));
170                         return False;
171                 }
172         } else if (flags & NET_FLAGS_PDC) {
173                 struct in_addr *ip_list;
174                 int addr_count;
175                 if (get_dc_list(True /* PDC only*/, opt_target_workgroup, &ip_list, &addr_count)) {
176                         fstring dc_name;
177                         if (addr_count < 1) {
178                                 return False;
179                         }
180                         
181                         *server_ip = *ip_list;
182                         
183                         if (is_zero_ip(*server_ip))
184                                 return False;
185                         
186                         if (!lookup_dc_name(global_myname, opt_target_workgroup, server_ip, dc_name))
187                                 return False;
188                                 
189                         *server_name = strdup(dc_name);
190                 }
191                 
192         } else if (flags & NET_FLAGS_DMB) {
193                 struct in_addr msbrow_ip;
194                 /*  if (!resolve_name(MSBROWSE, &msbrow_ip, 1)) */
195                 if (!resolve_name(opt_target_workgroup, &msbrow_ip, 0x1B))  {
196                         DEBUG(1,("Unable to resolve domain browser via name lookup\n"));
197                         return False;
198                 } else {
199                         *server_ip = msbrow_ip;
200                 }
201                 *server_name = strdup(inet_ntoa(dest_ip));
202         } else if (flags & NET_FLAGS_MASTER) {
203                 struct in_addr brow_ips;
204                 if (!resolve_name(opt_target_workgroup, &brow_ips, 0x1D))  {
205                                 /* go looking for workgroups */
206                         DEBUG(1,("Unable to resolve master browser via name lookup\n"));
207                         return False;
208                 } else {
209                         *server_ip = brow_ips;
210                 }
211                 *server_name = strdup(inet_ntoa(dest_ip));
212         } else if (!(flags & NET_FLAGS_LOCALHOST_DEFAULT_INSANE)) {
213                 extern struct in_addr loopback_ip;
214                 *server_ip = loopback_ip;
215                 *server_name = strdup("127.0.0.1");
216         }
217
218         if (!server_name || !*server_name) {
219                 DEBUG(1,("no server to connect to\n"));
220                 return False;
221         }
222
223         return True;
224 }
225
226 struct cli_state *net_make_ipc_connection(unsigned flags)
227 {
228         char *server_name = NULL;
229         struct in_addr server_ip;
230         struct cli_state *cli;
231
232         if (!net_find_server(flags, &server_ip, &server_name)) {
233                 d_printf("\nUnable to find a suitable server\n");
234                 return NULL;
235         }
236
237         if (flags & NET_FLAGS_ANONYMOUS) {
238                 cli = connect_to_ipc_anonymous(&server_ip, server_name);
239         } else {
240                 cli = connect_to_ipc(&server_ip, server_name);
241         }
242         SAFE_FREE(server_name);
243         if(!cli) {
244                 d_printf("\nUnable to connect to target server\n");
245                 return NULL;
246         }
247         return cli;
248 }
249
250
251 static int net_usage(int argc, const char **argv)
252 {
253         d_printf("  net ads [command]\tto run ADS commands\n"\
254                  "  net rap [command]\tto run RAP (pre-RPC) commands\n"\
255                  "  net rpc [command]\tto run RPC commands\n"\
256                  "  net rap help\n"\
257                  "\nType \"net help <option>\" to get more information on that option\n");
258         return -1;
259 }
260
261 static int help_usage(int argc, const char **argv)
262 {
263         d_printf("\n"\
264 "Usage: net help <function>\n"\
265 "\n"\
266 "Valid functions are:\n"\
267 "  RPC RAP ADS\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                 {"HELP", help_usage},
281                 {NULL, NULL}};
282
283         return net_run_function(argc, argv, func, help_usage);
284 }
285
286 /* main function table */
287 static struct functable net_func[] = {
288         {"RPC", net_rpc},
289         {"RAP", net_rap},
290         {"ADS", net_ads},
291         {"HELP", net_help},
292         {NULL, NULL}
293 };
294
295
296 /****************************************************************************
297   main program
298 ****************************************************************************/
299  int main(int argc, const char **argv)
300 {
301         int opt,i;
302         char *p;
303         int rc = 0;
304         int argc_new = 0;
305         const char ** argv_new;
306         poptContext pc;
307         static char *servicesf = dyn_CONFIGFILE;
308         static int debuglevel;
309
310         struct poptOption long_options[] = {
311                 {"help",        'h', POPT_ARG_NONE,   0,     'h'},
312                 {"workgroup",   'w', POPT_ARG_STRING, &opt_target_workgroup},
313                 {"myworkgroup", 'W', POPT_ARG_STRING, &opt_workgroup},
314                 {"user",        'U', POPT_ARG_STRING, &opt_user_name, 'U'},
315                 {"ipaddress",   'I', POPT_ARG_STRING, 0,     'I'},
316                 {"port",        'p', POPT_ARG_INT,    &opt_port},
317                 {"myname",      'n', POPT_ARG_STRING, &opt_requester_name},
318                 {"conf",        's', POPT_ARG_STRING, &servicesf},
319                 {"debug",       'd', POPT_ARG_INT,    &debuglevel, 'd'},
320                 {"debuglevel",  'd', POPT_ARG_INT,    &debuglevel, 'd'},
321                 {"server",      'S', POPT_ARG_STRING, &opt_host},
322                 {"comment",     'C', POPT_ARG_STRING, &opt_comment},
323                 {"maxusers",    'M', POPT_ARG_INT,    &opt_maxusers},
324                 {"flags",       'F', POPT_ARG_INT,    &opt_flags},
325                 {"jobid",       'j', POPT_ARG_INT,    &opt_jobid},
326                 {"long",        'l', POPT_ARG_NONE,   &opt_long_list_entries},
327                 { 0, 0, 0, 0}
328         };
329
330         got_pass = 0;
331         zero_ip(&dest_ip);
332
333         dbf = x_stdout;
334         
335         pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 
336                             POPT_CONTEXT_KEEP_FIRST);
337         
338         while((opt = poptGetNextOpt(pc)) != -1) {
339                 switch (opt) {
340                 case 'h':
341                         net_usage(argc, argv);
342                         exit(0);
343                         break;
344                 case 'I':
345                         dest_ip = *interpret_addr2(poptGetOptArg(pc));
346                         if (is_zero_ip(dest_ip))
347                                 d_printf("\nInvalid ip address specified\n");
348                         else
349                                 have_ip = True;
350                         break;
351                 case 'U':
352                         opt_user_name = strdup(opt_user_name);
353                         p = strchr(opt_user_name,'%');
354                         if (p) {
355                                 *p = 0;
356                                 opt_password = p+1;
357                                 got_pass = 1;
358                         }
359                         break;
360                 default:
361                         d_printf("\nInvalid option %c (%d)\n", (char)opt, opt);
362                         net_usage(argc, argv);
363                 }
364         }
365
366         DEBUGLEVEL = debuglevel;
367
368         lp_load(servicesf,True,False,False);       
369
370         argv_new = (const char **)poptGetArgs(pc);
371
372         argc_new = argc;
373         for (i=0; i<argc; i++) {
374                 if (argv_new[i] == NULL) {
375                         argc_new = i;
376                         break;
377                 }
378         }
379          
380         if (!opt_requester_name) {
381                 static fstring myname;
382                 get_myname(myname);
383                 opt_requester_name = myname;
384         }
385
386         if (!opt_user_name && getenv("LOGNAME")) {
387                 opt_user_name = getenv("LOGNAME");
388         }
389
390         if (!opt_workgroup) {
391                 opt_workgroup = lp_workgroup();
392         }
393         
394         if (!opt_target_workgroup) {
395                 opt_target_workgroup = lp_workgroup();
396         }
397         
398         if (!*global_myname) {
399                 char *p2;
400
401                 fstrcpy(global_myname, myhostname());
402                 p2 = strchr_m(global_myname, '.');
403                 if (p2) 
404                         *p2 = 0;
405         }
406         
407         load_interfaces();
408
409         rc = net_run_function(argc_new-1, argv_new+1, net_func, net_usage);
410         
411         DEBUG(2,("return code = %d\n", rc));
412         return rc;
413 }