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/>.
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"
32 /* How low can we go? */
34 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
35 static enum tree_level level = LEV_SHARE;
37 /* Holds a list of workgroups or servers */
39 struct smb_name_list {
40 struct smb_name_list *prev, *next;
45 static struct smb_name_list *workgroups, *servers, *shares;
47 static void free_name_list(struct smb_name_list *list)
50 DLIST_REMOVE(list, list);
53 static void add_name(const char *machine_name, uint32_t server_type,
54 const char *comment, void *state)
56 struct smb_name_list **name_list = (struct smb_name_list **)state;
57 struct smb_name_list *new_name;
59 new_name = SMB_MALLOC_P(struct smb_name_list);
64 ZERO_STRUCTP(new_name);
66 new_name->name = SMB_STRDUP(machine_name);
67 new_name->comment = SMB_STRDUP(comment);
68 new_name->server_type = server_type;
70 if (!new_name->name || !new_name->comment) {
71 SAFE_FREE(new_name->name);
72 SAFE_FREE(new_name->comment);
77 DLIST_ADD(*name_list, new_name);
80 /****************************************************************************
81 display tree of smb workgroups, servers and shares
82 ****************************************************************************/
83 static bool get_workgroups(const struct user_auth_info *user_info)
85 struct cli_state *cli;
86 struct sockaddr_storage server_ss;
87 TALLOC_CTX *ctx = talloc_tos();
88 char *master_workgroup = NULL;
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
94 master_workgroup = talloc_strdup(ctx, lp_workgroup());
95 if (!master_workgroup) {
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",
107 char addr[INET6_ADDRSTRLEN];
109 print_sockaddr(addr, sizeof(addr), &server_ss);
111 cli = get_ipc_connect(addr, &server_ss, user_info);
116 cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
120 DEBUG(4, ("Unable to find master browser by "
126 if (!cli_NetServerEnum(cli, master_workgroup,
127 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
133 /* Retrieve the list of servers for a given workgroup */
135 static bool get_servers(char *workgroup, const struct user_auth_info *user_info)
137 struct cli_state *cli;
138 struct sockaddr_storage server_ss;
139 char addr[INET6_ADDRSTRLEN];
141 /* Open an IPC$ connection to the master browser for the workgroup */
143 if (!find_master_ip(workgroup, &server_ss)) {
144 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
149 print_sockaddr(addr, sizeof(addr), &server_ss);
150 if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
153 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
160 static bool get_rpc_shares(struct cli_state *cli,
161 void (*fn)(const char *, uint32_t, const char *, void *),
165 struct rpc_pipe_client *pipe_hnd = NULL;
168 struct srvsvc_NetShareInfoCtr info_ctr;
169 struct srvsvc_NetShareCtr1 ctr1;
171 uint32_t resume_handle = 0;
172 uint32_t total_entries = 0;
173 struct dcerpc_binding_handle *b;
175 mem_ctx = talloc_new(NULL);
176 if (mem_ctx == NULL) {
177 DEBUG(0, ("talloc_new failed\n"));
181 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc,
184 if (!NT_STATUS_IS_OK(status)) {
185 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
187 TALLOC_FREE(mem_ctx);
191 b = pipe_hnd->binding_handle;
193 ZERO_STRUCT(info_ctr);
197 info_ctr.ctr.ctr1 = &ctr1;
199 status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
207 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
208 TALLOC_FREE(mem_ctx);
209 TALLOC_FREE(pipe_hnd);
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);
218 TALLOC_FREE(mem_ctx);
219 TALLOC_FREE(pipe_hnd);
224 static bool get_shares(char *server_name, const struct user_auth_info *user_info)
226 struct cli_state *cli;
228 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
231 if (get_rpc_shares(cli, add_name, &shares))
234 if (!cli_RNetShareEnum(cli, add_name, &shares))
240 static bool print_tree(const struct user_auth_info *user_info)
242 struct smb_name_list *wg, *sv, *sh;
244 /* List workgroups */
246 if (!get_workgroups(user_info))
249 for (wg = workgroups; wg; wg = wg->next) {
251 printf("%s\n", wg->name);
255 free_name_list(servers);
258 if (level == LEV_WORKGROUP ||
259 !get_servers(wg->name, user_info))
262 for (sv = servers; sv; sv = sv->next) {
264 printf("\t\\\\%-15s\t\t%s\n",
265 sv->name, sv->comment);
269 free_name_list(shares);
272 if (level == LEV_SERVER ||
273 !get_shares(sv->name, user_info))
276 for (sh = shares; sh; sh = sh->next) {
277 printf("\t\t\\\\%s\\%-15s\t%s\n",
278 sv->name, sh->name, sh->comment);
286 /****************************************************************************
288 ****************************************************************************/
289 int main(int argc, char *argv[])
291 TALLOC_CTX *frame = talloc_stackframe();
292 const char **argv_const = discard_const_p(const char *, argv);
293 struct poptOption long_options[] = {
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" },
299 POPT_COMMON_CREDENTIALS
304 /* Initialise samba stuff */
309 setup_logging(argv[0], DEBUG_STDERR);
311 popt_common_credentials_set_ignore_missing_conf();
313 pc = poptGetContext("smbtree", argc, argv_const, long_options,
314 POPT_CONTEXT_KEEP_FIRST);
315 while(poptGetNextOpt(pc) != -1);
317 popt_burn_cmdline_password(argc, argv);
319 /* Now do our stuff */
321 if (!print_tree(popt_get_cmdline_auth_info())) {
326 popt_free_cmdline_auth_info();