Got medieval on another pointless extern. Removed extern struct ipzero
[samba.git] / source / utils / smbtree.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Network neighbourhood browser.
4    Version 3.0
5    
6    Copyright (C) Tim Potter      2000
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25 static BOOL use_bcast;
26
27 struct user_auth_info {
28         pstring username;
29         pstring password;
30         pstring workgroup;
31 };
32
33 /* How low can we go? */
34
35 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
36 enum tree_level level = LEV_SHARE;
37
38 static void usage(void)
39 {
40         printf(
41 "Usage: smbtree [options]\n\
42 \n\
43 \t-d debuglevel           set debug output level\n\
44 \t-U username             user to autheticate as\n\
45 \t-W workgroup            workgroup of user to authenticate as\n\
46 \t-D                      list only domains (workgroups) of tree\n\
47 \t-S                      list domains and servers of tree\n\
48 \t-b                      use bcast instead of using the master browser\n\
49 \n\
50 The username can be of the form username%%password or\n\
51 workgroup\\username%%password.\n\n\
52 ");
53 }
54
55 /* Holds a list of workgroups or servers */
56
57 struct name_list {
58         struct name_list *prev, *next;
59         pstring name, comment;
60         uint32 server_type;
61 };
62
63 static struct name_list *workgroups, *servers, *shares;
64
65 static void free_name_list(struct name_list *list)
66 {
67         while(list)
68                 DLIST_REMOVE(list, list);
69 }
70
71 static void add_name(const char *machine_name, uint32 server_type,
72                      const char *comment, void *state)
73 {
74         struct name_list **name_list = (struct name_list **)state;
75         struct name_list *new_name;
76
77         new_name = (struct name_list *)malloc(sizeof(struct name_list));
78
79         if (!new_name)
80                 return;
81
82         ZERO_STRUCTP(new_name);
83
84         pstrcpy(new_name->name, machine_name);
85         pstrcpy(new_name->comment, comment);
86         new_name->server_type = server_type;
87
88         DLIST_ADD(*name_list, new_name);
89 }
90
91 /* Return a cli_state pointing at the IPC$ share for the given workgroup */
92
93 static struct cli_state *get_ipc_connect(char *server,
94                                          struct user_auth_info *user_info)
95 {
96         struct nmb_name calling, called;
97         struct in_addr server_ip;
98         struct cli_state *cli;
99         pstring myname;
100
101         zero_ip(&server_ip);
102
103         get_myname(myname);
104
105         make_nmb_name(&called, myname, 0x0);
106         make_nmb_name(&calling, server, 0x20);
107
108         if (is_ipaddress(server))
109                 if (!resolve_name(server, &server_ip, 0x20))
110                         return False;
111                 
112  again:
113         if (!(cli = cli_initialise(NULL))) {
114                 DEBUG(4, ("Unable to initialise cli structure\n"));
115                 goto error;
116         }
117
118         if (!cli_connect(cli, server, &server_ip)) {
119                 DEBUG(4, ("Unable to connect to %s\n", server));
120                 goto error;
121         }
122
123         if (!cli_session_request(cli, &calling, &called)) {
124                 cli_shutdown(cli);
125                 if (!strequal(called.name, "*SMBSERVER")) {
126                         make_nmb_name(&called , "*SMBSERVER", 0x20);
127                         goto again;
128                 }
129                 DEBUG(4, ("Session request failed to %s\n", called.name));
130                 goto error;
131         }
132
133         if (!cli_negprot(cli)) {
134                 DEBUG(4, ("Negprot failed\n"));
135                 goto error;
136         }
137
138         if (!cli_session_setup(cli, user_info->username, user_info->password, 
139                                strlen(user_info->password),
140                                user_info->password, 
141                                strlen(user_info->password), server) &&
142             /* try an anonymous login if it failed */
143             !cli_session_setup(cli, "", "", 1,"", 0, server)) {
144                 DEBUG(4, ("Session setup failed\n"));
145                 goto error;
146         }
147
148         DEBUG(4,(" session setup ok\n"));
149
150         if (!cli_send_tconX(cli, "IPC$", "?????",
151                             user_info->password, 
152                             strlen(user_info->password)+1)) {
153                 DEBUG(4, ("Tconx failed\n"));
154                 goto error;
155         }
156
157         return cli;
158
159         /* Clean up after error */
160
161  error:
162         if (cli && cli->initialised)
163                 cli_shutdown(cli);
164
165         SAFE_FREE(cli);
166         return NULL;
167 }
168
169 /* Return the IP address and workgroup of a master browser on the 
170    network. */
171
172 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
173 {
174         struct in_addr *ip_list;
175         int i, count;
176
177         /* Go looking for workgroups by broadcasting on the local network */ 
178
179         if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
180                 return False;
181         }
182
183         for (i = 0; i < count; i++) {
184                 static fstring name;
185
186                 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
187                         continue;
188
189                 if (!find_master_ip(name, server_ip))
190                         continue;
191
192                 pstrcpy(workgroup, name);
193
194                 DEBUG(4, ("found master browser %s, %s\n", 
195                           name, inet_ntoa(ip_list[i])));
196
197                 return True;
198         }
199
200         return False;
201 }
202
203 /****************************************************************************
204   display tree of smb workgroups, servers and shares
205 ****************************************************************************/
206 static BOOL get_workgroups(struct user_auth_info *user_info)
207 {
208         struct cli_state *cli;
209         struct in_addr server_ip;
210         pstring master_workgroup;
211
212         /* Try to connect to a #1d name of our current workgroup.  If that
213            doesn't work broadcast for a master browser and then jump off
214            that workgroup. */
215
216         pstrcpy(master_workgroup, lp_workgroup());
217
218         if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
219                 DEBUG(4, ("Unable to find master browser for workgroup %s\n", 
220                           master_workgroup));
221                 if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
222                         DEBUG(4, ("Unable to find master browser by "
223                                   "broadcast\n"));
224                         return False;
225                 }
226         }
227
228         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
229                 return False;
230
231         if (!cli_NetServerEnum(cli, master_workgroup, 
232                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
233                 return False;
234
235         return True;
236 }
237
238 /* Retrieve the list of servers for a given workgroup */
239
240 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
241 {
242         struct cli_state *cli;
243         struct in_addr server_ip;
244
245         /* Open an IPC$ connection to the master browser for the workgroup */
246
247         if (!find_master_ip(workgroup, &server_ip)) {
248                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
249                           workgroup));
250                 return False;
251         }
252
253         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
254                 return False;
255
256         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name, 
257                                &servers))
258                 return False;
259
260         return True;
261 }
262
263 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
264 {
265         struct cli_state *cli;
266
267         if (!(cli = get_ipc_connect(server_name, user_info)))
268                 return False;
269
270         if (!cli_RNetShareEnum(cli, add_name, &shares))
271                 return False;
272
273         return True;
274 }
275
276 static BOOL print_tree(struct user_auth_info *user_info)
277 {
278         struct name_list *wg, *sv, *sh;
279
280         /* List workgroups */
281
282         if (!get_workgroups(user_info))
283                 return False;
284
285         for (wg = workgroups; wg; wg = wg->next) {
286
287                 printf("%s\n", wg->name);
288
289                 /* List servers */
290
291                 free_name_list(servers);
292                 servers = NULL;
293
294                 if (level == LEV_WORKGROUP || 
295                     !get_servers(wg->name, user_info))
296                         continue;
297
298                 for (sv = servers; sv; sv = sv->next) {
299
300                         printf("\t\\\\%-15s\t\t%s\n", 
301                                sv->name, sv->comment);
302
303                         /* List shares */
304
305                         free_name_list(shares);
306                         shares = NULL;
307
308                         if (level == LEV_SERVER ||
309                             !get_shares(sv->name, user_info))
310                                 continue;
311
312                         for (sh = shares; sh; sh = sh->next) {
313                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
314                                        sv->name, sh->name, sh->comment);
315                         }
316                 }
317         }
318
319         return True;
320 }
321
322 /****************************************************************************
323   main program
324 ****************************************************************************/
325  int main(int argc,char *argv[])
326 {
327         extern char *optarg;
328         extern int optind;
329         int opt;
330         char *p;
331         struct user_auth_info user_info;
332         BOOL got_pass = False;
333
334         /* Initialise samba stuff */
335
336         setlinebuf(stdout);
337
338         dbf = x_stderr;
339
340         setup_logging(argv[0],True);
341
342         lp_load(dyn_CONFIGFILE,True,False,False);
343         load_interfaces();
344
345         if (getenv("USER")) {
346                 pstrcpy(user_info.username, getenv("USER"));
347
348                 if ((p=strchr(user_info.username, '%'))) {
349                         *p = 0;
350                         pstrcpy(user_info.password, p+1);
351                         got_pass = True;
352                         memset(strchr(getenv("USER"), '%') + 1, 'X',
353                                strlen(user_info.password));
354                 }
355         }
356
357         pstrcpy(user_info.workgroup, lp_workgroup());
358
359         /* Parse command line args */
360
361         while ((opt = getopt(argc, argv, "U:hd:W:DSb")) != EOF) {
362                 switch (opt) {
363                 case 'U':
364                         pstrcpy(user_info.username,optarg);
365                         p = strchr(user_info.username,'%');
366                         if (p) {
367                                 *p = 0;
368                                 pstrcpy(user_info.password, p+1);
369                                 got_pass = 1;
370                         }
371                         break;
372
373                 case 'b':
374                         use_bcast = True;
375                         break;
376
377                 case 'h':
378                         usage();
379                         exit(1);
380
381                 case 'd':
382                         DEBUGLEVEL = atoi(optarg);
383                         break;
384
385                 case 'W':
386                         pstrcpy(user_info.workgroup, optarg);
387                         break;
388
389                 case 'D':
390                         level = LEV_WORKGROUP;
391                         break;
392
393                 case 'S':
394                         level = LEV_SERVER;
395                         break;
396
397                 default:
398                         printf("Unknown option %c (%d)\n", (char)opt, opt);
399                         exit(1);
400                 }
401         }
402
403         argc -= optind;
404         argv += optind;
405         
406         if (argc > 0) {
407                 usage();
408                 exit(1);
409         }
410
411         if (!got_pass) {
412                 char *pass = getpass("Password: ");
413                 if (pass) {
414                         pstrcpy(user_info.password, pass);
415                 }
416                 got_pass = True;
417         }
418
419         /* Now do our stuff */
420
421         if (!print_tree(&user_info))
422                 return 1;
423
424         return 0;
425 }