Fix searching master ip by bcast when old master has gone away (based on patch by...
[samba.git] / source / utils / smbtree.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Network neighbourhood browser.
4    
5    Copyright (C) Tim Potter      2000
6    Copyright (C) Jelmer Vernooij 2003
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 /* How low can we go? */
28
29 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
30 static enum tree_level level = LEV_SHARE;
31
32 /* Holds a list of workgroups or servers */
33
34 struct name_list {
35         struct name_list *prev, *next;
36         pstring name, comment;
37         uint32 server_type;
38 };
39
40 static struct name_list *workgroups, *servers, *shares;
41
42 static void free_name_list(struct name_list *list)
43 {
44         while(list)
45                 DLIST_REMOVE(list, list);
46 }
47
48 static void add_name(const char *machine_name, uint32 server_type,
49                      const char *comment, void *state)
50 {
51         struct name_list **name_list = (struct name_list **)state;
52         struct name_list *new_name;
53
54         new_name = (struct name_list *)malloc(sizeof(struct name_list));
55
56         if (!new_name)
57                 return;
58
59         ZERO_STRUCTP(new_name);
60
61         pstrcpy(new_name->name, machine_name);
62         pstrcpy(new_name->comment, comment);
63         new_name->server_type = server_type;
64
65         DLIST_ADD(*name_list, new_name);
66 }
67
68 /* Return a cli_state pointing at the IPC$ share for the given server */
69
70 static struct cli_state *get_ipc_connect(char *server, struct in_addr *server_ip,
71                                          struct user_auth_info *user_info)
72 {
73         struct cli_state *cli;
74         pstring myname;
75         NTSTATUS nt_status;
76
77         get_myname(myname);
78         
79         nt_status = cli_full_connection(&cli, myname, server, server_ip, 0, "IPC$", "IPC", 
80                                         user_info->username, lp_workgroup(), user_info->password, 
81                                         CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK, NULL);
82
83         if (NT_STATUS_IS_OK(nt_status)) {
84                 return cli;
85         } else {
86                 return NULL;
87         }
88 }
89
90 /* Return the IP address and workgroup of a master browser on the 
91    network. */
92
93 static struct cli_state *get_ipc_connect_master_ip_bcast(pstring workgroup, struct user_auth_info *user_info)
94 {
95         struct in_addr *ip_list;
96         struct cli_state *cli;
97         int i, count;
98         struct in_addr server_ip; 
99
100         /* Go looking for workgroups by broadcasting on the local network */ 
101
102         if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
103                 return False;
104         }
105
106         for (i = 0; i < count; i++) {
107                 static fstring name;
108
109                 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
110                         continue;
111
112                 if (!find_master_ip(name, &server_ip))
113                         continue;
114
115                 pstrcpy(workgroup, name);
116
117                 DEBUG(4, ("found master browser %s, %s\n", 
118                           name, inet_ntoa(ip_list[i])));
119
120                 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
121                                 continue;
122                         
123                         return cli;
124         }
125
126         return NULL;
127 }
128
129 /****************************************************************************
130   display tree of smb workgroups, servers and shares
131 ****************************************************************************/
132 static BOOL get_workgroups(struct user_auth_info *user_info)
133 {
134         struct cli_state *cli;
135         struct in_addr server_ip;
136         pstring master_workgroup;
137
138         /* Try to connect to a #1d name of our current workgroup.  If that
139            doesn't work broadcast for a master browser and then jump off
140            that workgroup. */
141
142         pstrcpy(master_workgroup, lp_workgroup());
143
144         if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ip)) {
145                 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
146                           master_workgroup));
147                                 use_bcast = True;
148                 } else if(!use_bcast) {
149                 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
150                                 return False;
151                 }
152                 
153                 if (!(cli = get_ipc_connect_master_ip_bcast(master_workgroup, user_info))) {
154                         DEBUG(4, ("Unable to find master browser by "
155                                   "broadcast\n"));
156                         return False;
157         }
158
159         if (!cli_NetServerEnum(cli, master_workgroup, 
160                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
161                 return False;
162
163         return True;
164 }
165
166 /* Retrieve the list of servers for a given workgroup */
167
168 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
169 {
170         struct cli_state *cli;
171         struct in_addr server_ip;
172
173         /* Open an IPC$ connection to the master browser for the workgroup */
174
175         if (!find_master_ip(workgroup, &server_ip)) {
176                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
177                           workgroup));
178                 return False;
179         }
180
181         if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
182                 return False;
183
184         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name, 
185                                &servers))
186                 return False;
187
188         return True;
189 }
190
191 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
192 {
193         struct cli_state *cli;
194
195         if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
196                 return False;
197
198         if (!cli_RNetShareEnum(cli, add_name, &shares))
199                 return False;
200
201         return True;
202 }
203
204 static BOOL print_tree(struct user_auth_info *user_info)
205 {
206         struct name_list *wg, *sv, *sh;
207
208         /* List workgroups */
209
210         if (!get_workgroups(user_info))
211                 return False;
212
213         for (wg = workgroups; wg; wg = wg->next) {
214
215                 printf("%s\n", wg->name);
216
217                 /* List servers */
218
219                 free_name_list(servers);
220                 servers = NULL;
221
222                 if (level == LEV_WORKGROUP || 
223                     !get_servers(wg->name, user_info))
224                         continue;
225
226                 for (sv = servers; sv; sv = sv->next) {
227
228                         printf("\t\\\\%-15s\t\t%s\n", 
229                                sv->name, sv->comment);
230
231                         /* List shares */
232
233                         free_name_list(shares);
234                         shares = NULL;
235
236                         if (level == LEV_SERVER ||
237                             !get_shares(sv->name, user_info))
238                                 continue;
239
240                         for (sh = shares; sh; sh = sh->next) {
241                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
242                                        sv->name, sh->name, sh->comment);
243                         }
244                 }
245         }
246
247         return True;
248 }
249
250 /****************************************************************************
251   main program
252 ****************************************************************************/
253  int main(int argc,char *argv[])
254 {
255         struct poptOption long_options[] = {
256                 POPT_AUTOHELP
257                 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
258                 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
259                 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
260                 POPT_COMMON_SAMBA
261                 POPT_COMMON_CREDENTIALS
262                 POPT_TABLEEND
263         };
264         poptContext pc;
265         
266         /* Initialise samba stuff */
267
268         setlinebuf(stdout);
269
270         dbf = x_stderr;
271
272         setup_logging(argv[0],True);
273
274         pc = poptGetContext("smbtree", argc, (const char **)argv, long_options, 
275                                                 POPT_CONTEXT_KEEP_FIRST);
276         while(poptGetNextOpt(pc) != -1);
277         poptFreeContext(pc);
278
279         lp_load(dyn_CONFIGFILE,True,False,False);
280         load_interfaces();
281
282         /* Parse command line args */
283
284         if (!cmdline_auth_info.got_pass) {
285                 char *pass = getpass("Password: ");
286                 if (pass) {
287                         pstrcpy(cmdline_auth_info.password, pass);
288                 }
289         cmdline_auth_info.got_pass = True;
290         }
291
292         /* Now do our stuff */
293
294         if (!print_tree(&cmdline_auth_info))
295                 return 1;
296
297         return 0;
298 }