r24943: Some stackframes
[Samba/gebeck_regimport.git] / source3 / utils / smbtree.c
bloba29aa52fefeeefd068b7f58238c9e0e2681523ff
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 BOOL 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 name_list {
34 struct name_list *prev, *next;
35 pstring name, comment;
36 uint32 server_type;
39 static struct name_list *workgroups, *servers, *shares;
41 static void free_name_list(struct 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 name_list **name_list = (struct name_list **)state;
51 struct name_list *new_name;
53 new_name = SMB_MALLOC_P(struct name_list);
55 if (!new_name)
56 return;
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 in_addr server_ip;
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
78 that workgroup. */
80 pstrcpy(master_workgroup, lp_workgroup());
82 if (!use_bcast && !find_master_ip(lp_workgroup(), &server_ip)) {
83 DEBUG(4, ("Unable to find master browser for workgroup %s, falling back to broadcast\n",
84 master_workgroup));
85 use_bcast = True;
86 } else if(!use_bcast) {
87 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
88 return False;
91 if (!(cli = get_ipc_connect_master_ip_bcast(master_workgroup, user_info))) {
92 DEBUG(4, ("Unable to find master browser by "
93 "broadcast\n"));
94 return False;
97 if (!cli_NetServerEnum(cli, master_workgroup,
98 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
99 return False;
101 return True;
104 /* Retrieve the list of servers for a given workgroup */
106 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
108 struct cli_state *cli;
109 struct in_addr server_ip;
111 /* Open an IPC$ connection to the master browser for the workgroup */
113 if (!find_master_ip(workgroup, &server_ip)) {
114 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
115 workgroup));
116 return False;
119 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
120 return False;
122 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
123 &servers))
124 return False;
126 return True;
129 static BOOL get_rpc_shares(struct cli_state *cli,
130 void (*fn)(const char *, uint32, const char *, void *),
131 void *state)
133 NTSTATUS status;
134 struct rpc_pipe_client *pipe_hnd;
135 TALLOC_CTX *mem_ctx;
136 uint32 enum_hnd;
137 struct srvsvc_NetShareCtr1 ctr1;
138 union srvsvc_NetShareCtr ctr;
139 uint32 numentries;
140 int i;
141 uint32 info_level = 1;
143 mem_ctx = talloc_new(NULL);
144 if (mem_ctx == NULL) {
145 DEBUG(0, ("talloc_new failed\n"));
146 return False;
149 enum_hnd = 0;
150 pipe_hnd = cli_rpc_pipe_open_noauth(cli, PI_SRVSVC, &status);
152 if (pipe_hnd == NULL) {
153 DEBUG(10, ("Could not connect to srvsvc pipe: %s\n",
154 nt_errstr(status)));
155 TALLOC_FREE(mem_ctx);
156 return False;
159 ZERO_STRUCT(ctr1);
160 level = 1;
161 ctr.ctr1 = &ctr1;
163 status = rpccli_srvsvc_NetShareEnum(pipe_hnd, mem_ctx, NULL,
164 &info_level, &ctr,
165 0xffffffff, &numentries,
166 &enum_hnd);
168 if (!NT_STATUS_IS_OK(status)) {
169 TALLOC_FREE(mem_ctx);
170 cli_rpc_pipe_close(pipe_hnd);
171 return False;
174 for (i=0; i<numentries; i++) {
175 fn(ctr.ctr1->array[i].name, ctr.ctr1->array[i].type,
176 ctr.ctr1->array[i].comment, state);
179 TALLOC_FREE(mem_ctx);
180 cli_rpc_pipe_close(pipe_hnd);
181 return True;
185 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
187 struct cli_state *cli;
189 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
190 return False;
192 if (get_rpc_shares(cli, add_name, &shares))
193 return True;
195 if (!cli_RNetShareEnum(cli, add_name, &shares))
196 return False;
198 return True;
201 static BOOL print_tree(struct user_auth_info *user_info)
203 struct name_list *wg, *sv, *sh;
205 /* List workgroups */
207 if (!get_workgroups(user_info))
208 return False;
210 for (wg = workgroups; wg; wg = wg->next) {
212 printf("%s\n", wg->name);
214 /* List servers */
216 free_name_list(servers);
217 servers = NULL;
219 if (level == LEV_WORKGROUP ||
220 !get_servers(wg->name, user_info))
221 continue;
223 for (sv = servers; sv; sv = sv->next) {
225 printf("\t\\\\%-15s\t\t%s\n",
226 sv->name, sv->comment);
228 /* List shares */
230 free_name_list(shares);
231 shares = NULL;
233 if (level == LEV_SERVER ||
234 !get_shares(sv->name, user_info))
235 continue;
237 for (sh = shares; sh; sh = sh->next) {
238 printf("\t\t\\\\%s\\%-15s\t%s\n",
239 sv->name, sh->name, sh->comment);
244 return True;
247 /****************************************************************************
248 main program
249 ****************************************************************************/
250 int main(int argc,char *argv[])
252 TALLOC_CTX *frame = talloc_stackframe();
253 struct poptOption long_options[] = {
254 POPT_AUTOHELP
255 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
256 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
257 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
258 POPT_COMMON_SAMBA
259 POPT_COMMON_CREDENTIALS
260 POPT_TABLEEND
262 poptContext pc;
264 /* Initialise samba stuff */
265 load_case_tables();
267 setlinebuf(stdout);
269 dbf = x_stderr;
271 setup_logging(argv[0],True);
273 pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
274 POPT_CONTEXT_KEEP_FIRST);
275 while(poptGetNextOpt(pc) != -1);
276 poptFreeContext(pc);
278 lp_load(dyn_CONFIGFILE,True,False,False,True);
279 load_interfaces();
281 /* Parse command line args */
283 if (!cmdline_auth_info.got_pass) {
284 char *pass = getpass("Password: ");
285 if (pass) {
286 pstrcpy(cmdline_auth_info.password, pass);
288 cmdline_auth_info.got_pass = True;
291 /* Now do our stuff */
293 if (!print_tree(&cmdline_auth_info)) {
294 TALLOC_FREE(frame);
295 return 1;
298 TALLOC_FREE(frame);
299 return 0;