tdb: Fix Coverity ID 2192: NO_EFFECT
[Samba.git] / source3 / utils / smbtree.c
blob3f7fc9769865755c33446c9689df6330a178b76c
1 /*
2 Unix SMB/CIFS implementation.
3 Network neighbourhood browser.
5 Copyright (C) Tim Potter 2000
6 Copyright (C) Jelmer Vernooij 2003
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.
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.
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/>.
22 #include "includes.h"
23 #include "popt_common.h"
24 #include "rpc_client/cli_pipe.h"
25 #include "../librpc/gen_ndr/ndr_srvsvc_c.h"
26 #include "libsmb/clirap.h"
28 static int use_bcast;
30 /* How low can we go? */
32 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
33 static enum tree_level level = LEV_SHARE;
35 /* Holds a list of workgroups or servers */
37 struct smb_name_list {
38 struct smb_name_list *prev, *next;
39 char *name, *comment;
40 uint32 server_type;
43 static struct smb_name_list *workgroups, *servers, *shares;
45 static void free_name_list(struct smb_name_list *list)
47 while(list)
48 DLIST_REMOVE(list, list);
51 static void add_name(const char *machine_name, uint32 server_type,
52 const char *comment, void *state)
54 struct smb_name_list **name_list = (struct smb_name_list **)state;
55 struct smb_name_list *new_name;
57 new_name = SMB_MALLOC_P(struct smb_name_list);
59 if (!new_name)
60 return;
62 ZERO_STRUCTP(new_name);
64 new_name->name = SMB_STRDUP(machine_name);
65 new_name->comment = SMB_STRDUP(comment);
66 new_name->server_type = server_type;
68 if (!new_name->name || !new_name->comment) {
69 SAFE_FREE(new_name->name);
70 SAFE_FREE(new_name->comment);
71 SAFE_FREE(new_name);
72 return;
75 DLIST_ADD(*name_list, new_name);
78 /****************************************************************************
79 display tree of smb workgroups, servers and shares
80 ****************************************************************************/
81 static bool get_workgroups(struct user_auth_info *user_info)
83 struct cli_state *cli;
84 struct sockaddr_storage server_ss;
85 TALLOC_CTX *ctx = talloc_tos();
86 char *master_workgroup = NULL;
88 /* Try to connect to a #1d name of our current workgroup. If that
89 doesn't work broadcast for a master browser and then jump off
90 that workgroup. */
92 master_workgroup = talloc_strdup(ctx, lp_workgroup());
93 if (!master_workgroup) {
94 return false;
97 if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
98 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
99 master_workgroup));
100 use_bcast = True;
101 } else if(!use_bcast) {
102 char addr[INET6_ADDRSTRLEN];
103 print_sockaddr(addr, sizeof(addr), &server_ss);
104 if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
105 return False;
108 if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
109 user_info,
110 &master_workgroup))) {
111 DEBUG(4, ("Unable to find master browser by "
112 "broadcast\n"));
113 return False;
116 if (!cli_NetServerEnum(cli, master_workgroup,
117 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
118 return False;
120 return True;
123 /* Retrieve the list of servers for a given workgroup */
125 static bool get_servers(char *workgroup, struct user_auth_info *user_info)
127 struct cli_state *cli;
128 struct sockaddr_storage server_ss;
129 char addr[INET6_ADDRSTRLEN];
131 /* Open an IPC$ connection to the master browser for the workgroup */
133 if (!find_master_ip(workgroup, &server_ss)) {
134 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
135 workgroup));
136 return False;
139 print_sockaddr(addr, sizeof(addr), &server_ss);
140 if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
141 return False;
143 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
144 &servers))
145 return False;
147 return True;
150 static bool get_rpc_shares(struct cli_state *cli,
151 void (*fn)(const char *, uint32, const char *, void *),
152 void *state)
154 NTSTATUS status;
155 struct rpc_pipe_client *pipe_hnd = NULL;
156 TALLOC_CTX *mem_ctx;
157 WERROR werr;
158 struct srvsvc_NetShareInfoCtr info_ctr;
159 struct srvsvc_NetShareCtr1 ctr1;
160 int i;
161 uint32_t resume_handle = 0;
162 uint32_t total_entries = 0;
163 struct dcerpc_binding_handle *b;
165 mem_ctx = talloc_new(NULL);
166 if (mem_ctx == NULL) {
167 DEBUG(0, ("talloc_new failed\n"));
168 return False;
171 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
172 &pipe_hnd);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
176 nt_errstr(status)));
177 TALLOC_FREE(mem_ctx);
178 return False;
181 b = pipe_hnd->binding_handle;
183 ZERO_STRUCT(info_ctr);
184 ZERO_STRUCT(ctr1);
186 info_ctr.level = 1;
187 info_ctr.ctr.ctr1 = &ctr1;
189 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
190 pipe_hnd->desthost,
191 &info_ctr,
192 0xffffffff,
193 &total_entries,
194 &resume_handle,
195 &werr);
197 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
198 TALLOC_FREE(mem_ctx);
199 TALLOC_FREE(pipe_hnd);
200 return False;
203 for (i=0; i<total_entries; i++) {
204 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
205 fn(info.name, info.type, info.comment, state);
208 TALLOC_FREE(mem_ctx);
209 TALLOC_FREE(pipe_hnd);
210 return True;
214 static bool get_shares(char *server_name, struct user_auth_info *user_info)
216 struct cli_state *cli;
218 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
219 return False;
221 if (get_rpc_shares(cli, add_name, &shares))
222 return True;
224 if (!cli_RNetShareEnum(cli, add_name, &shares))
225 return False;
227 return True;
230 static bool print_tree(struct user_auth_info *user_info)
232 struct smb_name_list *wg, *sv, *sh;
234 /* List workgroups */
236 if (!get_workgroups(user_info))
237 return False;
239 for (wg = workgroups; wg; wg = wg->next) {
241 printf("%s\n", wg->name);
243 /* List servers */
245 free_name_list(servers);
246 servers = NULL;
248 if (level == LEV_WORKGROUP ||
249 !get_servers(wg->name, user_info))
250 continue;
252 for (sv = servers; sv; sv = sv->next) {
254 printf("\t\\\\%-15s\t\t%s\n",
255 sv->name, sv->comment);
257 /* List shares */
259 free_name_list(shares);
260 shares = NULL;
262 if (level == LEV_SERVER ||
263 !get_shares(sv->name, user_info))
264 continue;
266 for (sh = shares; sh; sh = sh->next) {
267 printf("\t\t\\\\%s\\%-15s\t%s\n",
268 sv->name, sh->name, sh->comment);
273 return True;
276 /****************************************************************************
277 main program
278 ****************************************************************************/
279 int main(int argc,char *argv[])
281 TALLOC_CTX *frame = talloc_stackframe();
282 struct user_auth_info *auth_info;
283 struct poptOption long_options[] = {
284 POPT_AUTOHELP
285 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
286 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
287 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
288 POPT_COMMON_SAMBA
289 POPT_COMMON_CREDENTIALS
290 POPT_TABLEEND
292 poptContext pc;
294 /* Initialise samba stuff */
295 load_case_tables();
297 setlinebuf(stdout);
299 setup_logging(argv[0], DEBUG_STDERR);
301 auth_info = user_auth_info_init(frame);
302 if (auth_info == NULL) {
303 exit(1);
305 popt_common_set_auth_info(auth_info);
307 pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
308 POPT_CONTEXT_KEEP_FIRST);
309 while(poptGetNextOpt(pc) != -1);
310 poptFreeContext(pc);
312 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
313 load_interfaces();
315 /* Parse command line args */
317 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
318 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
319 TALLOC_FREE(frame);
320 return 1;
323 set_cmdline_auth_info_getpass(auth_info);
325 /* Now do our stuff */
327 if (!print_tree(auth_info)) {
328 TALLOC_FREE(frame);
329 return 1;
332 TALLOC_FREE(frame);
333 return 0;