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