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 | #include "popt_common.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/clirap.h"
|
---|
28 |
|
---|
29 | static int use_bcast;
|
---|
30 |
|
---|
31 | /* How low can we go? */
|
---|
32 |
|
---|
33 | enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
|
---|
34 | static enum tree_level level = LEV_SHARE;
|
---|
35 |
|
---|
36 | /* Holds a list of workgroups or servers */
|
---|
37 |
|
---|
38 | struct smb_name_list {
|
---|
39 | struct smb_name_list *prev, *next;
|
---|
40 | char *name, *comment;
|
---|
41 | uint32 server_type;
|
---|
42 | };
|
---|
43 |
|
---|
44 | static struct smb_name_list *workgroups, *servers, *shares;
|
---|
45 |
|
---|
46 | static void free_name_list(struct smb_name_list *list)
|
---|
47 | {
|
---|
48 | while(list)
|
---|
49 | DLIST_REMOVE(list, list);
|
---|
50 | }
|
---|
51 |
|
---|
52 | static void add_name(const char *machine_name, uint32 server_type,
|
---|
53 | const char *comment, void *state)
|
---|
54 | {
|
---|
55 | struct smb_name_list **name_list = (struct smb_name_list **)state;
|
---|
56 | struct smb_name_list *new_name;
|
---|
57 |
|
---|
58 | new_name = SMB_MALLOC_P(struct smb_name_list);
|
---|
59 |
|
---|
60 | if (!new_name)
|
---|
61 | return;
|
---|
62 |
|
---|
63 | ZERO_STRUCTP(new_name);
|
---|
64 |
|
---|
65 | new_name->name = SMB_STRDUP(machine_name);
|
---|
66 | new_name->comment = SMB_STRDUP(comment);
|
---|
67 | new_name->server_type = server_type;
|
---|
68 |
|
---|
69 | if (!new_name->name || !new_name->comment) {
|
---|
70 | SAFE_FREE(new_name->name);
|
---|
71 | SAFE_FREE(new_name->comment);
|
---|
72 | SAFE_FREE(new_name);
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | DLIST_ADD(*name_list, new_name);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /****************************************************************************
|
---|
80 | display tree of smb workgroups, servers and shares
|
---|
81 | ****************************************************************************/
|
---|
82 | static bool get_workgroups(struct user_auth_info *user_info)
|
---|
83 | {
|
---|
84 | struct cli_state *cli;
|
---|
85 | struct sockaddr_storage server_ss;
|
---|
86 | TALLOC_CTX *ctx = talloc_tos();
|
---|
87 | char *master_workgroup = NULL;
|
---|
88 |
|
---|
89 | /* Try to connect to a #1d name of our current workgroup. If that
|
---|
90 | doesn't work broadcast for a master browser and then jump off
|
---|
91 | that workgroup. */
|
---|
92 |
|
---|
93 | master_workgroup = talloc_strdup(ctx, lp_workgroup());
|
---|
94 | if (!master_workgroup) {
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
|
---|
99 | DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
|
---|
100 | master_workgroup));
|
---|
101 | use_bcast = True;
|
---|
102 | } else if(!use_bcast) {
|
---|
103 | char addr[INET6_ADDRSTRLEN];
|
---|
104 | print_sockaddr(addr, sizeof(addr), &server_ss);
|
---|
105 | if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
|
---|
106 | return False;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
|
---|
110 | user_info,
|
---|
111 | &master_workgroup))) {
|
---|
112 | DEBUG(4, ("Unable to find master browser by "
|
---|
113 | "broadcast\n"));
|
---|
114 | return False;
|
---|
115 | }
|
---|
116 |
|
---|
117 | if (!cli_NetServerEnum(cli, master_workgroup,
|
---|
118 | SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
|
---|
119 | return False;
|
---|
120 |
|
---|
121 | return True;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Retrieve the list of servers for a given workgroup */
|
---|
125 |
|
---|
126 | static bool get_servers(char *workgroup, struct user_auth_info *user_info)
|
---|
127 | {
|
---|
128 | struct cli_state *cli;
|
---|
129 | struct sockaddr_storage server_ss;
|
---|
130 | char addr[INET6_ADDRSTRLEN];
|
---|
131 |
|
---|
132 | /* Open an IPC$ connection to the master browser for the workgroup */
|
---|
133 |
|
---|
134 | if (!find_master_ip(workgroup, &server_ss)) {
|
---|
135 | DEBUG(4, ("Cannot find master browser for workgroup %s\n",
|
---|
136 | workgroup));
|
---|
137 | return False;
|
---|
138 | }
|
---|
139 |
|
---|
140 | print_sockaddr(addr, sizeof(addr), &server_ss);
|
---|
141 | if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
|
---|
142 | return False;
|
---|
143 |
|
---|
144 | if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
|
---|
145 | &servers))
|
---|
146 | return False;
|
---|
147 |
|
---|
148 | return True;
|
---|
149 | }
|
---|
150 |
|
---|
151 | static bool get_rpc_shares(struct cli_state *cli,
|
---|
152 | void (*fn)(const char *, uint32, const char *, void *),
|
---|
153 | void *state)
|
---|
154 | {
|
---|
155 | NTSTATUS status;
|
---|
156 | struct rpc_pipe_client *pipe_hnd = NULL;
|
---|
157 | TALLOC_CTX *mem_ctx;
|
---|
158 | WERROR werr;
|
---|
159 | struct srvsvc_NetShareInfoCtr info_ctr;
|
---|
160 | struct srvsvc_NetShareCtr1 ctr1;
|
---|
161 | int i;
|
---|
162 | uint32_t resume_handle = 0;
|
---|
163 | uint32_t total_entries = 0;
|
---|
164 | struct dcerpc_binding_handle *b;
|
---|
165 |
|
---|
166 | mem_ctx = talloc_new(NULL);
|
---|
167 | if (mem_ctx == NULL) {
|
---|
168 | DEBUG(0, ("talloc_new failed\n"));
|
---|
169 | return False;
|
---|
170 | }
|
---|
171 |
|
---|
172 | status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
|
---|
173 | &pipe_hnd);
|
---|
174 |
|
---|
175 | if (!NT_STATUS_IS_OK(status)) {
|
---|
176 | DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
|
---|
177 | nt_errstr(status)));
|
---|
178 | TALLOC_FREE(mem_ctx);
|
---|
179 | return False;
|
---|
180 | }
|
---|
181 |
|
---|
182 | b = pipe_hnd->binding_handle;
|
---|
183 |
|
---|
184 | ZERO_STRUCT(info_ctr);
|
---|
185 | ZERO_STRUCT(ctr1);
|
---|
186 |
|
---|
187 | info_ctr.level = 1;
|
---|
188 | info_ctr.ctr.ctr1 = &ctr1;
|
---|
189 |
|
---|
190 | status = dcerpc_srvsvc_NetShareEnumAll(b, mem_ctx,
|
---|
191 | pipe_hnd->desthost,
|
---|
192 | &info_ctr,
|
---|
193 | 0xffffffff,
|
---|
194 | &total_entries,
|
---|
195 | &resume_handle,
|
---|
196 | &werr);
|
---|
197 |
|
---|
198 | if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
|
---|
199 | TALLOC_FREE(mem_ctx);
|
---|
200 | TALLOC_FREE(pipe_hnd);
|
---|
201 | return False;
|
---|
202 | }
|
---|
203 |
|
---|
204 | for (i=0; i<total_entries; i++) {
|
---|
205 | struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
|
---|
206 | fn(info.name, info.type, info.comment, state);
|
---|
207 | }
|
---|
208 |
|
---|
209 | TALLOC_FREE(mem_ctx);
|
---|
210 | TALLOC_FREE(pipe_hnd);
|
---|
211 | return True;
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | static bool get_shares(char *server_name, struct user_auth_info *user_info)
|
---|
216 | {
|
---|
217 | struct cli_state *cli;
|
---|
218 |
|
---|
219 | if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
|
---|
220 | return False;
|
---|
221 |
|
---|
222 | if (get_rpc_shares(cli, add_name, &shares))
|
---|
223 | return True;
|
---|
224 |
|
---|
225 | if (!cli_RNetShareEnum(cli, add_name, &shares))
|
---|
226 | return False;
|
---|
227 |
|
---|
228 | return True;
|
---|
229 | }
|
---|
230 |
|
---|
231 | static bool print_tree(struct user_auth_info *user_info)
|
---|
232 | {
|
---|
233 | struct smb_name_list *wg, *sv, *sh;
|
---|
234 |
|
---|
235 | /* List workgroups */
|
---|
236 |
|
---|
237 | if (!get_workgroups(user_info))
|
---|
238 | return False;
|
---|
239 |
|
---|
240 | for (wg = workgroups; wg; wg = wg->next) {
|
---|
241 |
|
---|
242 | printf("%s\n", wg->name);
|
---|
243 |
|
---|
244 | /* List servers */
|
---|
245 |
|
---|
246 | free_name_list(servers);
|
---|
247 | servers = NULL;
|
---|
248 |
|
---|
249 | if (level == LEV_WORKGROUP ||
|
---|
250 | !get_servers(wg->name, user_info))
|
---|
251 | continue;
|
---|
252 |
|
---|
253 | for (sv = servers; sv; sv = sv->next) {
|
---|
254 |
|
---|
255 | printf("\t\\\\%-15s\t\t%s\n",
|
---|
256 | sv->name, sv->comment);
|
---|
257 |
|
---|
258 | /* List shares */
|
---|
259 |
|
---|
260 | free_name_list(shares);
|
---|
261 | shares = NULL;
|
---|
262 |
|
---|
263 | if (level == LEV_SERVER ||
|
---|
264 | !get_shares(sv->name, user_info))
|
---|
265 | continue;
|
---|
266 |
|
---|
267 | for (sh = shares; sh; sh = sh->next) {
|
---|
268 | printf("\t\t\\\\%s\\%-15s\t%s\n",
|
---|
269 | sv->name, sh->name, sh->comment);
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | return True;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /****************************************************************************
|
---|
278 | main program
|
---|
279 | ****************************************************************************/
|
---|
280 | int main(int argc,char *argv[])
|
---|
281 | {
|
---|
282 | TALLOC_CTX *frame = talloc_stackframe();
|
---|
283 | struct user_auth_info *auth_info;
|
---|
284 | struct poptOption long_options[] = {
|
---|
285 | POPT_AUTOHELP
|
---|
286 | { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
|
---|
287 | { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
|
---|
288 | { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
|
---|
289 | POPT_COMMON_SAMBA
|
---|
290 | POPT_COMMON_CREDENTIALS
|
---|
291 | POPT_TABLEEND
|
---|
292 | };
|
---|
293 | poptContext pc;
|
---|
294 |
|
---|
295 | /* Initialise samba stuff */
|
---|
296 | load_case_tables();
|
---|
297 |
|
---|
298 | setlinebuf(stdout);
|
---|
299 |
|
---|
300 | setup_logging(argv[0], DEBUG_STDERR);
|
---|
301 |
|
---|
302 | auth_info = user_auth_info_init(frame);
|
---|
303 | if (auth_info == NULL) {
|
---|
304 | exit(1);
|
---|
305 | }
|
---|
306 | popt_common_set_auth_info(auth_info);
|
---|
307 |
|
---|
308 | pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
|
---|
309 | POPT_CONTEXT_KEEP_FIRST);
|
---|
310 | while(poptGetNextOpt(pc) != -1);
|
---|
311 | poptFreeContext(pc);
|
---|
312 |
|
---|
313 | lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
|
---|
314 | load_interfaces();
|
---|
315 |
|
---|
316 | /* Parse command line args */
|
---|
317 |
|
---|
318 | if (get_cmdline_auth_info_use_machine_account(auth_info) &&
|
---|
319 | !set_cmdline_auth_info_machine_account_creds(auth_info)) {
|
---|
320 | TALLOC_FREE(frame);
|
---|
321 | return 1;
|
---|
322 | }
|
---|
323 |
|
---|
324 | set_cmdline_auth_info_getpass(auth_info);
|
---|
325 |
|
---|
326 | /* Now do our stuff */
|
---|
327 |
|
---|
328 | if (!print_tree(auth_info)) {
|
---|
329 | TALLOC_FREE(frame);
|
---|
330 | return 1;
|
---|
331 | }
|
---|
332 |
|
---|
333 | TALLOC_FREE(frame);
|
---|
334 | return 0;
|
---|
335 | }
|
---|