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