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