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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 static BOOL use_bcast;
27 /* How low can we go? */
29 enum tree_level {LEV_WORKGROUP, LEV_SERVER, LEV_SHARE};
30 static enum tree_level level = LEV_SHARE;
32 /* Holds a list of workgroups or servers */
35 struct name_list *prev, *next;
36 pstring name, comment;
40 static struct name_list *workgroups, *servers, *shares;
42 static void free_name_list(struct name_list *list)
45 DLIST_REMOVE(list, list);
48 static void add_name(const char *machine_name, uint32 server_type,
49 const char *comment, void *state)
51 struct name_list **name_list = (struct name_list **)state;
52 struct name_list *new_name;
54 new_name = (struct name_list *)malloc(sizeof(struct name_list));
59 ZERO_STRUCTP(new_name);
61 pstrcpy(new_name->name, machine_name);
62 pstrcpy(new_name->comment, comment);
63 new_name->server_type = server_type;
65 DLIST_ADD(*name_list, new_name);
68 /* Return a cli_state pointing at the IPC$ share for the given server */
70 static struct cli_state *get_ipc_connect(char *server, struct in_addr *server_ip,
71 struct user_auth_info *user_info)
73 struct cli_state *cli;
79 nt_status = cli_full_connection(&cli, myname, server, server_ip, 0, "IPC$", "IPC",
80 user_info->username, lp_workgroup(), user_info->password,
81 CLI_FULL_CONNECTION_ANNONYMOUS_FALLBACK, NULL);
83 if (NT_STATUS_IS_OK(nt_status)) {
90 /* Return the IP address and workgroup of a master browser on the
93 static BOOL find_master_ip_bcast(pstring workgroup, struct in_addr *server_ip)
95 struct in_addr *ip_list;
98 /* Go looking for workgroups by broadcasting on the local network */
100 if (!name_resolve_bcast(MSBROWSE, 1, &ip_list, &count)) {
104 for (i = 0; i < count; i++) {
107 if (!name_status_find("*", 0, 0x1d, ip_list[i], name))
110 if (!find_master_ip(name, server_ip))
113 pstrcpy(workgroup, name);
115 DEBUG(4, ("found master browser %s, %s\n",
116 name, inet_ntoa(ip_list[i])));
124 /****************************************************************************
125 display tree of smb workgroups, servers and shares
126 ****************************************************************************/
127 static BOOL get_workgroups(struct user_auth_info *user_info)
129 struct cli_state *cli;
130 struct in_addr server_ip;
131 pstring master_workgroup;
133 /* Try to connect to a #1d name of our current workgroup. If that
134 doesn't work broadcast for a master browser and then jump off
137 pstrcpy(master_workgroup, lp_workgroup());
139 if (use_bcast || !find_master_ip(lp_workgroup(), &server_ip)) {
140 DEBUG(4, ("Unable to find master browser for workgroup %s\n",
142 if (!find_master_ip_bcast(master_workgroup, &server_ip)) {
143 DEBUG(4, ("Unable to find master browser by "
149 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
152 if (!cli_NetServerEnum(cli, master_workgroup,
153 SV_TYPE_DOMAIN_ENUM, add_name, &workgroups))
159 /* Retrieve the list of servers for a given workgroup */
161 static BOOL get_servers(char *workgroup, struct user_auth_info *user_info)
163 struct cli_state *cli;
164 struct in_addr server_ip;
166 /* Open an IPC$ connection to the master browser for the workgroup */
168 if (!find_master_ip(workgroup, &server_ip)) {
169 DEBUG(4, ("Cannot find master browser for workgroup %s\n",
174 if (!(cli = get_ipc_connect(inet_ntoa(server_ip), &server_ip, user_info)))
177 if (!cli_NetServerEnum(cli, workgroup, SV_TYPE_ALL, add_name,
184 static BOOL get_shares(char *server_name, struct user_auth_info *user_info)
186 struct cli_state *cli;
188 if (!(cli = get_ipc_connect(server_name, NULL, user_info)))
191 if (!cli_RNetShareEnum(cli, add_name, &shares))
197 static BOOL print_tree(struct user_auth_info *user_info)
199 struct name_list *wg, *sv, *sh;
201 /* List workgroups */
203 if (!get_workgroups(user_info))
206 for (wg = workgroups; wg; wg = wg->next) {
208 printf("%s\n", wg->name);
212 free_name_list(servers);
215 if (level == LEV_WORKGROUP ||
216 !get_servers(wg->name, user_info))
219 for (sv = servers; sv; sv = sv->next) {
221 printf("\t\\\\%-15s\t\t%s\n",
222 sv->name, sv->comment);
226 free_name_list(shares);
229 if (level == LEV_SERVER ||
230 !get_shares(sv->name, user_info))
233 for (sh = shares; sh; sh = sh->next) {
234 printf("\t\t\\\\%s\\%-15s\t%s\n",
235 sv->name, sh->name, sh->comment);
243 /****************************************************************************
245 ****************************************************************************/
246 int main(int argc,char *argv[])
248 struct poptOption long_options[] = {
250 { "broadcast", 'b', POPT_ARG_VAL, &use_bcast, True, "Use broadcast instead of using the master browser" },
251 { "domains", 'D', POPT_ARG_VAL, &level, LEV_WORKGROUP, "List only domains (workgroups) of tree" },
252 { "servers", 'S', POPT_ARG_VAL, &level, LEV_SERVER, "List domains(workgroups) and servers of tree" },
254 POPT_COMMON_CREDENTIALS
259 /* Initialise samba stuff */
265 setup_logging(argv[0],True);
267 pc = poptGetContext("smbtree", argc, (const char **)argv, long_options,
268 POPT_CONTEXT_KEEP_FIRST);
269 while(poptGetNextOpt(pc) != -1);
272 lp_load(dyn_CONFIGFILE,True,False,False);
275 /* Parse command line args */
277 if (!cmdline_auth_info.got_pass) {
278 char *pass = getpass("Password: ");
280 pstrcpy(cmdline_auth_info.password, pass);
282 cmdline_auth_info.got_pass = True;
285 /* Now do our stuff */
287 if (!print_tree(&cmdline_auth_info))