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