s3: Rename new parameter "ldap ref follow" to "ldap follow referral".
[Samba/gebeck_regimport.git] / source3 / utils / smbtree.c
blob02001f0abbdfce1dddfbce21346bd97dc3b214b2
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"
24 static int use_bcast;
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 */
33 struct smb_name_list {
34 struct smb_name_list *prev, *next;
35 char *name, *comment;
36 uint32 server_type;
39 static struct smb_name_list *workgroups, *servers, *shares;
41 static void free_name_list(struct smb_name_list *list)
43 while(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 smb_name_list **name_list = (struct smb_name_list **)state;
51 struct smb_name_list *new_name;
53 new_name = SMB_MALLOC_P(struct smb_name_list);
55 if (!new_name)
56 return;
58 ZERO_STRUCTP(new_name);
60 new_name->name = SMB_STRDUP(machine_name);
61 new_name->comment = SMB_STRDUP(comment);
62 new_name->server_type = server_type;
64 if (!new_name->name || !new_name->comment) {
65 SAFE_FREE(new_name->name);
66 SAFE_FREE(new_name->comment);
67 SAFE_FREE(new_name);
68 return;
71 DLIST_ADD(*name_list, new_name);
74 /****************************************************************************
75 display tree of smb workgroups, servers and shares
76 ****************************************************************************/
77 static bool get_workgroups(struct user_auth_info *user_info)
79 struct cli_state *cli;
80 struct sockaddr_storage server_ss;
81 TALLOC_CTX *ctx = talloc_tos();
82 char *master_workgroup = NULL;
84 /* Try to connect to a #1d name of our current workgroup. If that
85 doesn't work broadcast for a master browser and then jump off
86 that workgroup. */
88 master_workgroup = talloc_strdup(ctx, lp_workgroup());
89 if (!master_workgroup) {
90 return false;
93 if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ss)) {
94 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
95 master_workgroup));
96 use_bcast = True;
97 } else if(!use_bcast) {
98 char addr[INET6_ADDRSTRLEN];
99 print_sockaddr(addr, sizeof(addr), &server_ss);
100 if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
101 return False;
104 if (!(cli = get_ipc_connect_master_ip_bcast(talloc_tos(),
105 user_info,
106 &master_workgroup))) {
107 DEBUG(4, ("Unable to find master browser by "
108 "broadcast\n"));
109 return False;
112 if (!cli_NetServerEnum(cli, master_workgroup,
113 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
114 return False;
116 return True;
119 /* Retrieve the list of servers for a given workgroup */
121 static bool get_servers(char *workgroup, struct user_auth_info *user_info)
123 struct cli_state *cli;
124 struct sockaddr_storage server_ss;
125 char addr[INET6_ADDRSTRLEN];
127 /* Open an IPC$ connection to the master browser for the workgroup */
129 if (!find_master_ip(workgroup, &server_ss)) {
130 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
131 workgroup));
132 return False;
135 print_sockaddr(addr, sizeof(addr), &server_ss);
136 if (!(cli = get_ipc_connect(addr, &server_ss, user_info)))
137 return False;
139 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
140 &servers))
141 return False;
143 return True;
146 static bool get_rpc_shares(struct cli_state *cli,
147 void (*fn)(const char *, uint32, const char *, void *),
148 void *state)
150 NTSTATUS status;
151 struct rpc_pipe_client *pipe_hnd;
152 TALLOC_CTX *mem_ctx;
153 WERROR werr;
154 struct srvsvc_NetShareInfoCtr info_ctr;
155 struct srvsvc_NetShareCtr1 ctr1;
156 int i;
157 uint32_t resume_handle = 0;
158 uint32_t total_entries = 0;
160 mem_ctx = talloc_new(NULL);
161 if (mem_ctx == NULL) {
162 DEBUG(0, ("talloc_new failed\n"));
163 return False;
166 status = cli_rpc_pipe_open_noauth(cli, &ndr_table_srvsvc.syntax_id,
167 &pipe_hnd);
169 if (!NT_STATUS_IS_OK(status)) {
170 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
171 nt_errstr(status)));
172 TALLOC_FREE(mem_ctx);
173 return False;
176 ZERO_STRUCT(info_ctr);
177 ZERO_STRUCT(ctr1);
179 info_ctr.level = 1;
180 info_ctr.ctr.ctr1 = &ctr1;
182 status = rpccli_srvsvc_NetShareEnumAll(pipe_hnd, mem_ctx,
183 pipe_hnd->desthost,
184 &info_ctr,
185 0xffffffff,
186 &total_entries,
187 &resume_handle,
188 &werr);
190 if (!NT_STATUS_IS_OK(status) || !W_ERROR_IS_OK(werr)) {
191 TALLOC_FREE(mem_ctx);
192 TALLOC_FREE(pipe_hnd);
193 return False;
196 for (i=0; i<total_entries; i++) {
197 struct srvsvc_NetShareInfo1 info = info_ctr.ctr.ctr1->array[i];
198 fn(info.name, info.type, info.comment, state);
201 TALLOC_FREE(mem_ctx);
202 TALLOC_FREE(pipe_hnd);
203 return True;
207 static bool get_shares(char *server_name, struct user_auth_info *user_info)
209 struct cli_state *cli;
211 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
212 return False;
214 if (get_rpc_shares(cli, add_name, &shares))
215 return True;
217 if (!cli_RNetShareEnum(cli, add_name, &shares))
218 return False;
220 return True;
223 static bool print_tree(struct user_auth_info *user_info)
225 struct smb_name_list *wg, *sv, *sh;
227 /* List workgroups */
229 if (!get_workgroups(user_info))
230 return False;
232 for (wg = workgroups; wg; wg = wg->next) {
234 printf("%s\n", wg->name);
236 /* List servers */
238 free_name_list(servers);
239 servers = NULL;
241 if (level == LEV_WORKGROUP ||
242 !get_servers(wg->name, user_info))
243 continue;
245 for (sv = servers; sv; sv = sv->next) {
247 printf("\t\\\\%-15s\t\t%s\n",
248 sv->name, sv->comment);
250 /* List shares */
252 free_name_list(shares);
253 shares = NULL;
255 if (level == LEV_SERVER ||
256 !get_shares(sv->name, user_info))
257 continue;
259 for (sh = shares; sh; sh = sh->next) {
260 printf("\t\t\\\\%s\\%-15s\t%s\n",
261 sv->name, sh->name, sh->comment);
266 return True;
269 /****************************************************************************
270 main program
271 ****************************************************************************/
272 int main(int argc,char *argv[])
274 TALLOC_CTX *frame = talloc_stackframe();
275 struct user_auth_info *auth_info;
276 struct poptOption long_options[] = {
277 POPT_AUTOHELP
278 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
279 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
280 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
281 POPT_COMMON_SAMBA
282 POPT_COMMON_CREDENTIALS
283 POPT_TABLEEND
285 poptContext pc;
287 /* Initialise samba stuff */
288 load_case_tables();
290 setlinebuf(stdout);
292 dbf = x_stderr;
294 setup_logging(argv[0],True);
296 auth_info = user_auth_info_init(frame);
297 if (auth_info == NULL) {
298 exit(1);
300 popt_common_set_auth_info(auth_info);
302 pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
303 POPT_CONTEXT_KEEP_FIRST);
304 while(poptGetNextOpt(pc) != -1);
305 poptFreeContext(pc);
307 lp_load(get_dyn_CONFIGFILE(),True,False,False,True);
308 load_interfaces();
310 /* Parse command line args */
312 if (get_cmdline_auth_info_use_machine_account(auth_info) &&
313 !set_cmdline_auth_info_machine_account_creds(auth_info)) {
314 TALLOC_FREE(frame);
315 return 1;
318 set_cmdline_auth_info_getpass(auth_info);
320 /* Now do our stuff */
322 if (!print_tree(auth_info)) {
323 TALLOC_FREE(frame);
324 return 1;
327 TALLOC_FREE(frame);
328 return 0;