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/>.
26 /* How low can we go? */
28 enum tree_level
{LEV_WORKGROUP
, LEV_SERVER
, LEV_SHARE
};
29 static enum tree_level level
= LEV_SHARE
;
31 /* Holds a list of workgroups or servers */
34 struct name_list
*prev
, *next
;
35 pstring name
, comment
;
39 static struct name_list
*workgroups
, *servers
, *shares
;
41 static void free_name_list(struct name_list
*list
)
44 DLIST_REMOVE(list
, list
);
47 static void add_name(const char *machine_name
, uint32 server_type
,
48 const char *comment
, void *state
)
50 struct name_list
**name_list
= (struct name_list
**)state
;
51 struct name_list
*new_name
;
53 new_name
= SMB_MALLOC_P(struct name_list
);
58 ZERO_STRUCTP(new_name
);
60 pstrcpy(new_name
->name
, machine_name
);
61 pstrcpy(new_name
->comment
, comment
);
62 new_name
->server_type
= server_type
;
64 DLIST_ADD(*name_list
, new_name
);
67 /****************************************************************************
68 display tree of smb workgroups, servers and shares
69 ****************************************************************************/
70 static bool get_workgroups(struct user_auth_info
*user_info
)
72 struct cli_state
*cli
;
73 struct sockaddr_storage server_ss
;
74 pstring master_workgroup
;
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
80 pstrcpy(master_workgroup
, lp_workgroup());
82 if (!use_bcast
&& !find_master_ip(lp_workgroup(), &server_ss
)) {
83 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
86 } else if(!use_bcast
) {
87 char addr
[INET6_ADDRSTRLEN
];
88 print_sockaddr(addr
, sizeof(addr
), &server_ss
);
89 if (!(cli
= get_ipc_connect(addr
, &server_ss
, user_info
)))
93 if (!(cli
= get_ipc_connect_master_ip_bcast(master_workgroup
, user_info
))) {
94 DEBUG(4, ("Unable to find master browser by "
99 if (!cli_NetServerEnum(cli
, master_workgroup
,
100 SV_TYPE_DOMAIN_ENUM
, add_name
, &workgroups
))
106 /* Retrieve the list of servers for a given workgroup */
108 static bool get_servers(char *workgroup
, struct user_auth_info
*user_info
)
110 struct cli_state
*cli
;
111 struct sockaddr_storage server_ss
;
112 char addr
[INET6_ADDRSTRLEN
];
114 /* Open an IPC$ connection to the master browser for the workgroup */
116 if (!find_master_ip(workgroup
, &server_ss
)) {
117 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
122 print_sockaddr(addr
, sizeof(addr
), &server_ss
);
123 if (!(cli
= get_ipc_connect(addr
, &server_ss
, user_info
)))
126 if (!cli_NetServerEnum(cli
, workgroup
, SV_TYPE_ALL
, add_name
,
133 static bool get_rpc_shares(struct cli_state
*cli
,
134 void (*fn
)(const char *, uint32
, const char *, void *),
138 struct rpc_pipe_client
*pipe_hnd
;
142 SRV_SHARE_INFO_CTR ctr
;
145 mem_ctx
= talloc_new(NULL
);
146 if (mem_ctx
== NULL
) {
147 DEBUG(0, ("talloc_new failed\n"));
151 init_enum_hnd(&enum_hnd
, 0);
153 pipe_hnd
= cli_rpc_pipe_open_noauth(cli
, PI_SRVSVC
, &status
);
155 if (pipe_hnd
== NULL
) {
156 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
158 TALLOC_FREE(mem_ctx
);
162 werr
= rpccli_srvsvc_net_share_enum(pipe_hnd
, mem_ctx
, 1, &ctr
,
163 0xffffffff, &enum_hnd
);
165 if (!W_ERROR_IS_OK(werr
)) {
166 TALLOC_FREE(mem_ctx
);
167 cli_rpc_pipe_close(pipe_hnd
);
171 for (i
=0; i
<ctr
.num_entries
; i
++) {
172 SRV_SHARE_INFO_1
*info
= &ctr
.share
.info1
[i
];
173 char *name
, *comment
;
174 name
= rpcstr_pull_unistr2_talloc(
175 mem_ctx
, &info
->info_1_str
.uni_netname
);
176 comment
= rpcstr_pull_unistr2_talloc(
177 mem_ctx
, &info
->info_1_str
.uni_remark
);
178 fn(name
, info
->info_1
.type
, comment
, state
);
181 TALLOC_FREE(mem_ctx
);
182 cli_rpc_pipe_close(pipe_hnd
);
187 static bool get_shares(char *server_name
, struct user_auth_info
*user_info
)
189 struct cli_state
*cli
;
191 if (!(cli
= get_ipc_connect(server_name
, NULL
, user_info
)))
194 if (get_rpc_shares(cli
, add_name
, &shares
))
197 if (!cli_RNetShareEnum(cli
, add_name
, &shares
))
203 static bool print_tree(struct user_auth_info
*user_info
)
205 struct name_list
*wg
, *sv
, *sh
;
207 /* List workgroups */
209 if (!get_workgroups(user_info
))
212 for (wg
= workgroups
; wg
; wg
= wg
->next
) {
214 printf("%s\n", wg
->name
);
218 free_name_list(servers
);
221 if (level
== LEV_WORKGROUP
||
222 !get_servers(wg
->name
, user_info
))
225 for (sv
= servers
; sv
; sv
= sv
->next
) {
227 printf("\t\\\\%-15s\t\t%s\n",
228 sv
->name
, sv
->comment
);
232 free_name_list(shares
);
235 if (level
== LEV_SERVER
||
236 !get_shares(sv
->name
, user_info
))
239 for (sh
= shares
; sh
; sh
= sh
->next
) {
240 printf("\t\t\\\\%s\\%-15s\t%s\n",
241 sv
->name
, sh
->name
, sh
->comment
);
249 /****************************************************************************
251 ****************************************************************************/
252 int main(int argc
,char *argv
[])
254 TALLOC_CTX
*frame
= talloc_stackframe();
255 struct poptOption long_options
[] = {
257 { "broadcast", 'b', POPT_ARG_VAL
, &use_bcast
, True
, "Use broadcast instead of using the master browser" },
258 { "domains", 'D', POPT_ARG_VAL
, &level
, LEV_WORKGROUP
, "List only domains (workgroups) of tree" },
259 { "servers", 'S', POPT_ARG_VAL
, &level
, LEV_SERVER
, "List domains(workgroups) and servers of tree" },
261 POPT_COMMON_CREDENTIALS
266 /* Initialise samba stuff */
273 setup_logging(argv
[0],True
);
275 pc
= poptGetContext("smbtree", argc
, (const char **)argv
, long_options
,
276 POPT_CONTEXT_KEEP_FIRST
);
277 while(poptGetNextOpt(pc
) != -1);
280 lp_load(dyn_CONFIGFILE
,True
,False
,False
,True
);
283 /* Parse command line args */
285 if (!cmdline_auth_info
.got_pass
) {
286 char *pass
= getpass("Password: ");
288 pstrcpy(cmdline_auth_info
.password
, pass
);
290 cmdline_auth_info
.got_pass
= True
;
293 /* Now do our stuff */
295 if (!print_tree(&cmdline_auth_info
)) {