W2K doesn't seem to respond to *#0 names in node status. Ensure name
[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         extern struct in_addr ipzero;
98         struct in_addr server_ip = ipzero;
99         struct cli_state *cli;
100         pstring myname;
101
102         get_myname(myname);
103
104         make_nmb_name(&called, myname, 0x0);
105         make_nmb_name(&calling, server, 0x20);
106
107         if (is_ipaddress(server))
108                 if (!resolve_name(server, &server_ip, 0x20))
109                         return False;
110                 
111  again:
112         if (!(cli = cli_initialise(NULL))) {
113                 DEBUG(4, ("Unable to initialise cli structure\n"));
114                 goto error;
115         }
116
117         if (!cli_connect(cli, server, &server_ip)) {
118                 DEBUG(4, ("Unable to connect to %s\n", server));
119                 goto error;
120         }
121
122         if (!cli_session_request(cli, &calling, &called)) {
123                 cli_shutdown(cli);
124                 if (!strequal(called.name, "*SMBSERVER")) {
125                         make_nmb_name(&called , "*SMBSERVER", 0x20);
126                         goto again;
127                 }
128                 DEBUG(4, ("Session request failed to %s\n", called.name));
129                 goto error;
130         }
131
132         if (!cli_negprot(cli)) {
133                 DEBUG(4, ("Negprot failed\n"));
134                 goto error;
135         }
136
137         if (!cli_session_setup(cli, user_info->username, user_info->password, 
138                                strlen(user_info->password),
139                                user_info->password, 
140                                strlen(user_info->password), server) &&
141             /* try an anonymous login if it failed */
142             !cli_session_setup(cli, "", "", 1,"", 0, server)) {
143                 DEBUG(4, ("Session setup failed\n"));
144                 goto error;
145         }
146
147         DEBUG(4,(" session setup ok\n"));
148
149         if (!cli_send_tconX(cli, "IPC$", "?????",
150                             user_info->password, 
151                             strlen(user_info->password)+1)) {
152                 DEBUG(4, ("Tconx failed\n"));
153                 goto error;
154         }
155
156         return cli;
157
158         /* Clean up after error */
159
160  error:
161         if (cli && cli->initialised)
162                 cli_shutdown(cli);
163
164         SAFE_FREE(cli);
165         return NULL;
166 }
167
168 /* Return the IP address and workgroup of a master browser on the 
169    network. */
170
171 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
172 {
173         struct in_addr *ip_list;
174         int i, count;
175
176         /* Go looking for workgroups by broadcasting on the local network */ 
177
178         if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
179                 return False;
180         }
181
182         for (i = 0; i < count; i++) {
183                 static fstring name;
184
185                 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
186                         continue;
187
188                 if (!find_master_ip(name, server_ip))
189                         continue;
190
191                 pstrcpy(workgroup, name);
192
193                 DEBUG(4, ("found master browser %s, %s\n", 
194                           name, inet_ntoa(ip_list[i])));
195
196                 return True;
197         }
198
199         return False;
200 }
201
202 /****************************************************************************
203   display tree of smb workgroups, servers and shares
204 ****************************************************************************/
205 static BOOL get_workgroups(struct user_auth_info *user_info)
206 {
207         struct cli_state *cli;
208         struct in_addr server_ip;
209         pstring master_workgroup;
210
211         /* Try to connect to a #1d name of our current workgroup.  If that
212            doesn't work broadcast for a master browser and then jump off
213            that workgroup. */
214
215         pstrcpy(master_workgroup, lp_workgroup());
216
217         if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
218                 DEBUG(4, ("Unable to find master browser for workgroup %s\n", 
219                           master_workgroup));
220                 if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
221                         DEBUG(4, ("Unable to find master browser by "
222                                   "broadcast\n"));
223                         return False;
224                 }
225         }
226
227         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
228                 return False;
229
230         if (!cli_NetServerEnum(cli, master_workgroup, 
231                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
232                 return False;
233
234         return True;
235 }
236
237 /* Retrieve the list of servers for a given workgroup */
238
239 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
240 {
241         struct cli_state *cli;
242         struct in_addr server_ip;
243
244         /* Open an IPC$ connection to the master browser for the workgroup */
245
246         if (!find_master_ip(workgroup, &server_ip)) {
247                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
248                           workgroup));
249                 return False;
250         }
251
252         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), user_info)))
253                 return False;
254
255         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name, 
256                                &servers))
257                 return False;
258
259         return True;
260 }
261
262 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
263 {
264         struct cli_state *cli;
265
266         if (!(cli = get_ipc_connect(server_name, user_info)))
267                 return False;
268
269         if (!cli_RNetShareEnum(cli, add_name, &shares))
270                 return False;
271
272         return True;
273 }
274
275 static BOOL print_tree(struct user_auth_info *user_info)
276 {
277         struct name_list *wg, *sv, *sh;
278
279         /* List workgroups */
280
281         if (!get_workgroups(user_info))
282                 return False;
283
284         for (wg = workgroups; wg; wg = wg->next) {
285
286                 printf("%s\n", wg->name);
287
288                 /* List servers */
289
290                 free_name_list(servers);
291                 servers = NULL;
292
293                 if (level == LEV_WORKGROUP || 
294                     !get_servers(wg->name, user_info))
295                         continue;
296
297                 for (sv = servers; sv; sv = sv->next) {
298
299                         printf("\t\\\\%-15s\t\t%s\n", 
300                                sv->name, sv->comment);
301
302                         /* List shares */
303
304                         free_name_list(shares);
305                         shares = NULL;
306
307                         if (level == LEV_SERVER ||
308                             !get_shares(sv->name, user_info))
309                                 continue;
310
311                         for (sh = shares; sh; sh = sh->next) {
312                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
313                                        sv->name, sh->name, sh->comment);
314                         }
315                 }
316         }
317
318         return True;
319 }
320
321 /****************************************************************************
322   main program
323 ****************************************************************************/
324  int main(int argc,char *argv[])
325 {
326         extern char *optarg;
327         extern int optind;
328         int opt;
329         char *p;
330         struct user_auth_info user_info;
331         BOOL got_pass = False;
332
333         /* Initialise samba stuff */
334
335         setlinebuf(stdout);
336
337         dbf = x_stderr;
338
339         setup_logging(argv[0],True);
340
341         TimeInit();
342
343         lp_load(dyn_CONFIGFILE,True,False,False);
344         load_interfaces();
345
346         if (getenv("USER")) {
347                 pstrcpy(user_info.username, getenv("USER"));
348
349                 if ((p=strchr(user_info.username, '%'))) {
350                         *p = 0;
351                         pstrcpy(user_info.password, p+1);
352                         got_pass = True;
353                         memset(strchr(getenv("USER"), '%') + 1, 'X',
354                                strlen(user_info.password));
355                 }
356         }
357
358         pstrcpy(user_info.workgroup, lp_workgroup());
359
360         /* Parse command line args */
361
362         while ((opt = getopt(argc, argv, "U:hd:W:DSb")) != EOF) {
363                 switch (opt) {
364                 case 'U':
365                         pstrcpy(user_info.username,optarg);
366                         p = strchr(user_info.username,'%');
367                         if (p) {
368                                 *p = 0;
369                                 pstrcpy(user_info.password, p+1);
370                                 got_pass = 1;
371                         }
372                         break;
373
374                 case 'b':
375                         use_bcast = True;
376                         break;
377
378                 case 'h':
379                         usage();
380                         exit(1);
381
382                 case 'd':
383                         DEBUGLEVEL = atoi(optarg);
384                         break;
385
386                 case 'W':
387                         pstrcpy(user_info.workgroup, optarg);
388                         break;
389
390                 case 'D':
391                         level = LEV_WORKGROUP;
392                         break;
393
394                 case 'S':
395                         level = LEV_SERVER;
396                         break;
397
398                 default:
399                         printf("Unknown option %c (%d)\n", (char)opt, opt);
400                         exit(1);
401                 }
402         }
403
404         argc -= optind;
405         argv += optind;
406         
407         if (argc > 0) {
408                 usage();
409                 exit(1);
410         }
411
412         if (!got_pass) {
413                 char *pass = getpass("Password: ");
414                 if (pass) {
415                         pstrcpy(user_info.password, pass);
416                 }
417                 got_pass = True;
418         }
419
420         /* Now do our stuff */
421
422         if (!print_tree(&user_info))
423                 return 1;
424
425         return 0;
426 }