Merge branch 'v3-2-test' of ssh://jra@git.samba.org/data/git/samba into v3-2-test
[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    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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23
24 static int use_bcast;
25
26 /* How low can we go? */
27
28 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
29 static enum tree_level level = LEV_SHARE;
30
31 /* Holds a list of workgroups or servers */
32
33 struct name_list {
34         struct name_list *prev, *next;
35         char *name, *comment;
36         uint32 server_type;
37 };
38
39 static struct name_list *workgroups, *servers, *shares;
40
41 static void free_name_list(struct name_list *list)
42 {
43         while(list)
44                 DLIST_REMOVE(list, list);
45 }
46
47 static void add_name(const char *machine_name, uint32 server_type,
48                      const char *comment, void *state)
49 {
50         struct name_list **name_list = (struct name_list **)state;
51         struct name_list *new_name;
52
53         new_name = SMB_MALLOC_P(struct name_list);
54
55         if (!new_name)
56                 return;
57
58         ZERO_STRUCTP(new_name);
59
60         new_name->name = SMB_STRDUP(machine_name);
61         new_name->comment = SMB_STRDUP(comment);
62         new_name->server_type = server_type;
63
64         if (!new_name->name || !new_name->comment) {
65                 SAFE_FREE(new_name->name);
66                 SAFE_FREE(new_name->comment);
67                 SAFE_FREE(new_name);
68                 return;
69         }
70
71         DLIST_ADD(*name_list, new_name);
72 }
73
74 /****************************************************************************
75   display tree of smb workgroups, servers and shares
76 ****************************************************************************/
77 static bool get_workgroups(struct user_auth_info *user_info)
78 {
79         struct cli_state *cli;
80         struct sockaddr_storage server_ss;
81         TALLOC_CTX *ctx = talloc_tos();
82         char *master_workgroup = NULL;
83
84         /* Try to connect to a #1d name of our current workgroup.  If that
85            doesn't work broadcast for a master browser and then jump off
86            that workgroup. */
87
88         master_workgroup = talloc_strdup(ctx, lp_workgroup());
89         if (!master_workgroup) {
90                 return false;
91         }
92
93         if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
94                 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n", 
95                           master_workgroup));
96                                 use_bcast = True;
97                 } else if(!use_bcast) {
98                         char addr[INET6_ADDRSTRLEN];
99                         print_sockaddr(addr, sizeof(addr), &server_ss);
100                         if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
101                                 return False;
102                 }
103
104                 if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
105                                                         user_info,
106                                                         &master_workgroup))) {
107                         DEBUG(4, ("Unable to find master browser by "
108                                   "broadcast\n"));
109                         return False;
110         }
111
112         if (!cli_NetServerEnum(cli, master_workgroup,
113                                SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
114                 return False;
115
116         return True;
117 }
118
119 /* Retrieve the list of servers for a given workgroup */
120
121 static bool get_servers(char *workgroup, struct user_auth_info *user_info)
122 {
123         struct cli_state *cli;
124         struct sockaddr_storage server_ss;
125         char addr[INET6_ADDRSTRLEN];
126
127         /* Open an IPC$ connection to the master browser for the workgroup */
128
129         if (!find_master_ip(workgroup, &server_ss)) {
130                 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
131                           workgroup));
132                 return False;
133         }
134
135         print_sockaddr(addr, sizeof(addr), &server_ss);
136         if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
137                 return False;
138
139         if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
140                                &servers))
141                 return False;
142
143         return True;
144 }
145
146 static bool get_rpc_shares(struct cli_state *cli,
147                            void (*fn)(const char *, uint32, const char *, void *),
148                            void *state)
149 {
150         NTSTATUS status;
151         struct rpc_pipe_client *pipe_hnd;
152         TALLOC_CTX *mem_ctx;
153         ENUM_HND enum_hnd;
154         WERROR werr;
155         SRV_SHARE_INFO_CTR ctr;
156         int i;
157
158         mem_ctx = talloc_new(NULL);
159         if (mem_ctx == NULL) {
160                 DEBUG(0, ("talloc_new failed\n"));
161                 return False;
162         }
163
164         init_enum_hnd(&enum_hnd, 0);
165
166         pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
167
168         if (pipe_hnd == NULL) {
169                 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
170                            nt_errstr(status)));
171                 TALLOC_FREE(mem_ctx);
172                 return False;
173         }
174
175         werr = rpccli_srvsvc_net_share_enum(pipe_hnd, mem_ctx, 1, &ctr,
176                                             0xffffffff, &enum_hnd);
177
178         if (!W_ERROR_IS_OK(werr)) {
179                 TALLOC_FREE(mem_ctx);
180                 cli_rpc_pipe_close(pipe_hnd);
181                 return False;
182         }
183
184         for (i=0; i<ctr.num_entries; i++) {
185                 SRV_SHARE_INFO_1 *info = &ctr.share.info1[i];
186                 char *name, *comment;
187                 name = rpcstr_pull_unistr2_talloc(
188                         mem_ctx, &info->info_1_str.uni_netname);
189                 comment = rpcstr_pull_unistr2_talloc(
190                         mem_ctx, &info->info_1_str.uni_remark);
191                 fn(name, info->info_1.type, comment, state);
192         }
193
194         TALLOC_FREE(mem_ctx);
195         cli_rpc_pipe_close(pipe_hnd);
196         return True;
197 }
198
199
200 static bool get_shares(char *server_name, struct user_auth_info *user_info)
201 {
202         struct cli_state *cli;
203
204         if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
205                 return False;
206
207         if (get_rpc_shares(cli, add_name, &shares))
208                 return True;
209
210         if (!cli_RNetShareEnum(cli, add_name, &shares))
211                 return False;
212
213         return True;
214 }
215
216 static bool print_tree(struct user_auth_info *user_info)
217 {
218         struct name_list *wg, *sv, *sh;
219
220         /* List workgroups */
221
222         if (!get_workgroups(user_info))
223                 return False;
224
225         for (wg = workgroups; wg; wg = wg->next) {
226
227                 printf("%s\n", wg->name);
228
229                 /* List servers */
230
231                 free_name_list(servers);
232                 servers = NULL;
233
234                 if (level == LEV_WORKGROUP || 
235                     !get_servers(wg->name, user_info))
236                         continue;
237
238                 for (sv = servers; sv; sv = sv->next) {
239
240                         printf("\t\\\\%-15s\t\t%s\n", 
241                                sv->name, sv->comment);
242
243                         /* List shares */
244
245                         free_name_list(shares);
246                         shares = NULL;
247
248                         if (level == LEV_SERVER ||
249                             !get_shares(sv->name, user_info))
250                                 continue;
251
252                         for (sh = shares; sh; sh = sh->next) {
253                                 printf("\t\t\\\\%s\\%-15s\t%s\n", 
254                                        sv->name, sh->name, sh->comment);
255                         }
256                 }
257         }
258
259         return True;
260 }
261
262 /****************************************************************************
263   main program
264 ****************************************************************************/
265  int main(int argc,char *argv[])
266 {
267         TALLOC_CTX *frame = talloc_stackframe();
268         struct user_auth_info local_auth_info;
269         struct poptOption long_options[] = {
270                 POPT_AUTOHELP
271                 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
272                 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
273                 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
274                 POPT_COMMON_SAMBA
275                 POPT_COMMON_CREDENTIALS
276                 POPT_TABLEEND
277         };
278         poptContext pc;
279
280         /* Initialise samba stuff */
281         load_case_tables();
282
283         setlinebuf(stdout);
284
285         dbf = x_stderr;
286
287         setup_logging(argv[0],True);
288
289         pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
290                                                 POPT_CONTEXT_KEEP_FIRST);
291         while(poptGetNextOpt(pc) != -1);
292         poptFreeContext(pc);
293
294         lp_load(dyn_CONFIGFILE,True,False,False,True);
295         load_interfaces();
296
297         /* Parse command line args */
298
299         if (!get_cmdline_auth_info_got_pass()) {
300                 char *pass = getpass("Password: ");
301                 if (pass) {
302                         set_cmdline_auth_info_password(pass);
303                 }
304         }
305
306         /* Now do our stuff */
307
308         if (!get_cmdline_auth_info_copy(&local_auth_info)) {
309                 return 1;
310         }
311
312         if (!print_tree(&local_auth_info)) {
313                 TALLOC_FREE(frame);
314                 return 1;
315         }
316
317         TALLOC_FREE(frame);
318         return 0;
319 }